Euphoria
app.cc
Go to the documentation of this file.
1 #include "window/app.h"
2 
3 #include "imgui/imgui.h"
4 #include "SDL.h"
5 
6 #include "log/log.h"
7 
8 #include "core/argparse.h"
9 #include "base/colors.h"
10 
11 #include "render/init.h"
12 
13 #include "window/engine.h"
14 #include "window/imgui_extra.h"
15 #include "window/imguilibrary.h"
16 #include "window/sdlwindow.h"
17 #include "window/log.h"
18 
19 
20 namespace eu::window
21 {
23  : clear_color(NamedColor::light_gray)
24  {
25  }
26 
27  void App::on_quit()
28  {
29  running = false;
30  }
31 
32 
33  void App::on_gui()
34  {
35  ImGui::ShowDemoWindow();
36  }
37 
38 
39  int
40  run_app(int argc, char** argv, const std::string& window_title, CreateAppFunction create_app)
41  {
42  SdlLogger sdl_logger;
43  const auto use_sdl_logger = log::ScopedLogger{ &sdl_logger };
44 
45  Engine engine;
46 
47  if (const auto r = engine.setup(core::argparse::NameAndArguments::extract(argc, argv)); r != 0)
48  {
49  return r;
50  }
51 
52 
53  int window_width = 1280;
54  int window_height = 720;
55 
56  if(!engine.create_window(window_title, window_width, window_height, true))
57  {
58  return -1;
59  }
60 
62  // main loop
63  auto app = create_app();
64 
65  while(app->running)
66  {
67  SDL_Event e;
68  while(SDL_PollEvent(&e) != 0)
69  {
71 
72  if(engine.on_resize(e, &window_width, &window_height))
73  {
74  // viewport_handler.set_size(window_width, window_height);
75  }
76 
77  switch(e.type)
78  {
79  case SDL_QUIT:
80  app->on_quit();
81  break;
82  default:
83  // ignore other events
84  break;
85  }
86  }
87 
89 
90  app->on_gui();
91 
92  engine.init->clear_screen(app->clear_color);
94 
95  SDL_GL_SwapWindow(engine.window->window);
96  }
97 
98  return 0;
99  }
100 }
union SDL_Event SDL_Event
Definition: fakesdlevent.h:4
void send_events_to_imgui(const SDL_Event *event)
Definition: imguilibrary.cc:98
std::function< std::unique_ptr< App >() > CreateAppFunction
Definition: app.h:37
int run_app(int argc, char **argv, const std::string &window_title, CreateAppFunction create_app)
Definition: app.cc:40
NamedColor
Definition: colors.h:12
static NameAndArguments extract(int argc, char *argv[])
Definition: argparse.cc:130
virtual void on_quit()
Definition: app.cc:27
bool running
Definition: app.h:17
virtual void on_gui()
Definition: app.cc:33
std::unique_ptr< SdlWindow > window
Definition: engine.h:37
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
int setup(const core::argparse::NameAndArguments &args)
Definition: engine.cc:43