Euphoria
engine.cc
Go to the documentation of this file.
1 #include "window/engine.h"
2 
3 #include "SDL.h"
4 #include "imgui/imgui.h"
5 
6 #include "base/os.h"
7 #include "log/log.h"
8 #include "io/vfs.h"
11 
12 #include "render/defaultfiles.h"
13 #include "render/debuggl.h"
14 #include "render/init.h"
15 
16 #include "window/filesystem.h"
17 #include "window/sdllibrary.h"
18 #include "window/sdlwindow.h"
19 #include "window/sdlglcontext.h"
20 #include "window/imguilibrary.h"
21 
22 namespace eu::window
23 {
25  : window_id(0)
26  {
27  }
28 
29 
31  {
32  imgui.reset();
33  init.reset();
34  context.reset();
35  window.reset();
36  catalog.reset();
37  file_system.reset();
38  sdl.reset();
39  }
40 
41 
42  int
44  {
45  sdl = std::make_unique<SdlLibrary>();
46  if(sdl->ok == false)
47  {
48  LOG_ERROR("Failed to create SDL");
49  return -1;
50  }
51 
52  auto parser = core::argparse::Parser("euphoria engine");
53 
54  auto current_directory = get_current_directory();
55  parser
56  .add("-w", &current_directory)
57  .set_help("Sets the working direction if it's different from the current folder")
58  ;
59  const auto parse_result = parser.parse(args);
60  if(parse_result != core::argparse::ok)
61  {
62  return parse_result.return_value;
63  }
64 
65  LOG_INFO("Current directory: {0}", current_directory);
66 
67  file_system = std::make_unique<io::FileSystem>();
70  (
71  file_system.get(),
72  current_directory
73  );
75  (
76  file_system.get(),
77  io::DirPath{"~/img-plain/"}
78  );
80  (
81  file_system.get(),
82  io::DirPath{"~/shaders/"}
83  );
84 
86 
87  return 0;
88  }
89 
90 
91  bool
93  (
94  const std::string& title,
95  int width,
96  int height,
97  bool blend_hack
98  )
99  {
100  const auto pref_path = get_preference_path();
101 
102  window = std::make_unique<SdlWindow>(title, width, height, true);
103 
104  if(window->window == nullptr)
105  {
106  LOG_ERROR("Failed to create window {0}", SDL_GetError());
107  return false;
108  }
109 
110  window_id = SDL_GetWindowID(window->window);
111 
112  context = std::make_unique<SdlOpenglContext>(window.get());
113 
114  if(context->context == nullptr)
115  {
116  LOG_ERROR("Failed to create SDL context {0}", SDL_GetError());
117  return false;
118  }
119 
120  init = std::make_unique<render::Init>
121  (
122  SDL_GL_GetProcAddress,
123  blend_hack
126  );
127 
128  if(init->is_ok == false)
129  {
130  LOG_ERROR("Failed to create Init");
131  return false;
132  }
133 
135 
136  imgui = std::make_unique<imgui::Library>(window->window, context.get(), pref_path);
137  ImGui::StyleColorsLight();
138 
139  return true;
140  }
141 
142 
143  bool
144  Engine::on_resize(SDL_Event e, int* width, int* height) const
145  {
146  if(e.type == SDL_WINDOWEVENT)
147  {
148  if(e.window.windowID == window_id)
149  {
150  switch(e.window.event)
151  {
152  case SDL_WINDOWEVENT_SIZE_CHANGED:
153  SDL_GetWindowSize(window->window, width, height);
154  return true;
155  default:
156  break;
157  }
158  }
159  }
160 
161  return false;
162  }
163 
164 }
union SDL_Event SDL_Event
Definition: fakesdlevent.h:4
#define LOG_INFO(...)
Definition: log.h:7
#define LOG_ERROR(...)
Definition: log.h:9
constexpr ParseResult ok
all ok
Definition: argparse.h:76
void add_default_shaders(io::FileSystem *fs, const io::DirPath &base)
void setup_opengl_debug()
Definition: debuggl.cc:130
void setup_default_files(std::shared_ptr< io::ReadRootCatalog > catalog)
Definition: defaultfiles.cc:9
std::string get_preference_path()
Definition: filesystem.cc:24
std::string get_current_directory()
Definition: os.cc:35
static void add(io::FileSystem *fs, const io::DirPath &base)
container for arguments passed to main
Definition: argparse.h:98
root parser. start argumentparsing with this one
Definition: argparse.h:575
static std::shared_ptr< ReadRootCatalog > create_and_add(FileSystem *fs)
Definition: vfs.cc:216
static void add(FileSystem *fs, const std::string &folder)
Definition: vfs.cc:317
std::unique_ptr< SdlWindow > window
Definition: engine.h:37
std::unique_ptr< SdlLibrary > sdl
Definition: engine.h:34
std::unique_ptr< SdlOpenglContext > context
Definition: engine.h:39
bool create_window(const std::string &title, int width, int height, bool blend_hack=false)
Definition: engine.cc:93
bool on_resize(SDL_Event e, int *width, int *height) const
Definition: engine.cc:144
std::unique_ptr< render::Init > init
Definition: engine.h:40
unsigned int window_id
Definition: engine.h:38
std::unique_ptr< io::FileSystem > file_system
Definition: engine.h:35
std::unique_ptr< imgui::Library > imgui
Definition: engine.h:41
std::shared_ptr< io::ReadRootCatalog > catalog
Definition: engine.h:36
int setup(const core::argparse::NameAndArguments &args)
Definition: engine.cc:43