]> git.localhorst.tv Git - blank.git/blob - src/app.cpp
move human I/O related stuff into separate file
[blank.git] / src / app.cpp
1 #include "app.hpp"
2
3 #include <iostream>
4 #include <stdexcept>
5
6
7 namespace blank {
8
9 Application::Application()
10 : init_sdl()
11 , init_img()
12 , init_gl()
13 , window()
14 , ctx(window.CreateContext())
15 , init_glew()
16 , program()
17 , cam()
18 , world()
19 , interface(world)
20 , running(false) {
21         GLContext::EnableVSync();
22
23         glClearColor(0.0, 0.0, 0.0, 1.0);
24 }
25
26
27 void Application::RunN(size_t n) {
28         Uint32 last = SDL_GetTicks();
29         for (size_t i = 0; i < n; ++i) {
30                 Uint32 now = SDL_GetTicks();
31                 int delta = now - last;
32                 Loop(delta);
33                 last = now;
34         }
35 }
36
37 void Application::RunT(size_t t) {
38         Uint32 last = SDL_GetTicks();
39         Uint32 finish = last + t;
40         while (last < finish) {
41                 Uint32 now = SDL_GetTicks();
42                 int delta = now - last;
43                 Loop(delta);
44                 last = now;
45         }
46 }
47
48 void Application::RunS(size_t n, size_t t) {
49         for (size_t i = 0; i < n; ++i) {
50                 Loop(t);
51         }
52 }
53
54
55 void Application::Run() {
56         running = true;
57         Uint32 last = SDL_GetTicks();
58         window.GrabMouse();
59         while (running) {
60                 Uint32 now = SDL_GetTicks();
61                 int delta = now - last;
62                 Loop(delta);
63                 last = now;
64         }
65 }
66
67 void Application::Loop(int dt) {
68         HandleEvents();
69         Update(dt);
70         Render();
71 }
72
73
74 void Application::HandleEvents() {
75         SDL_Event event;
76         while (SDL_PollEvent(&event)) {
77                 switch (event.type) {
78                         case SDL_KEYDOWN:
79                         case SDL_KEYUP:
80                                 interface.Handle(event.key);
81                                 break;
82                         case SDL_MOUSEBUTTONDOWN:
83                                 interface.Handle(event.button);
84                                 break;
85                         case SDL_MOUSEMOTION:
86                                 interface.Handle(event.motion);
87                                 break;
88                         case SDL_MOUSEWHEEL:
89                                 interface.Handle(event.wheel);
90                                 break;
91                         case SDL_QUIT:
92                                 running = false;
93                                 break;
94                         case SDL_WINDOWEVENT:
95                                 switch (event.window.event) {
96                                         case SDL_WINDOWEVENT_FOCUS_GAINED:
97                                                 window.GrabMouse();
98                                                 break;
99                                         case SDL_WINDOWEVENT_FOCUS_LOST:
100                                                 window.ReleaseMouse();
101                                                 break;
102                                         case SDL_WINDOWEVENT_RESIZED:
103                                                 cam.Viewport(event.window.data1, event.window.data2);
104                                                 interface.Handle(event.window);
105                                                 break;
106                                         default:
107                                                 interface.Handle(event.window);
108                                                 break;
109                                 }
110                                 break;
111                         default:
112                                 break;
113                 }
114         }
115 }
116
117 void Application::Update(int dt) {
118         interface.Update(dt);
119         world.Update(dt);
120 }
121
122 void Application::Render() {
123         GLContext::Clear();
124
125         program.Activate();
126
127         program.SetProjection(cam.Projection());
128         world.Render(program);
129
130         interface.Render(program);
131
132         window.Flip();
133 }
134
135 }