]> git.localhorst.tv Git - blank.git/blob - src/app.cpp
minor optimizations in chunk
[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(const Config &config)
10 : init_sdl()
11 , init_img()
12 , init_gl(config.doublebuf, config.multisampling)
13 , window()
14 , ctx(window.CreateContext())
15 , init_glew()
16 , chunk_prog()
17 , entity_prog()
18 , cam()
19 , world(config.world)
20 , interface(config.interface, world)
21 , test_controller(MakeTestEntity(world))
22 , running(false) {
23         if (config.vsync) {
24                 GLContext::EnableVSync();
25         }
26
27         glClearColor(0.0, 0.0, 0.0, 1.0);
28 }
29
30 Entity &Application::MakeTestEntity(World &world) {
31         Entity &e = world.AddEntity();
32         e.Position({ 0.0f, 0.0f, 0.0f });
33         e.SetShape(world.BlockTypes()[1].shape, { 1.0f, 1.0f, 0.0f });
34         e.AngularVelocity(glm::quat(glm::vec3{ 0.00001f, 0.000006f, 0.000013f }));
35         return e;
36 }
37
38
39 void Application::RunN(size_t n) {
40         Uint32 last = SDL_GetTicks();
41         for (size_t i = 0; i < n; ++i) {
42                 Uint32 now = SDL_GetTicks();
43                 int delta = now - last;
44                 Loop(delta);
45                 last = now;
46         }
47 }
48
49 void Application::RunT(size_t t) {
50         Uint32 last = SDL_GetTicks();
51         Uint32 finish = last + t;
52         while (last < finish) {
53                 Uint32 now = SDL_GetTicks();
54                 int delta = now - last;
55                 Loop(delta);
56                 last = now;
57         }
58 }
59
60 void Application::RunS(size_t n, size_t t) {
61         for (size_t i = 0; i < n; ++i) {
62                 Loop(t);
63         }
64 }
65
66
67 void Application::Run() {
68         running = true;
69         Uint32 last = SDL_GetTicks();
70         window.GrabMouse();
71         while (running) {
72                 Uint32 now = SDL_GetTicks();
73                 int delta = now - last;
74                 Loop(delta);
75                 last = now;
76         }
77 }
78
79 void Application::Loop(int dt) {
80         HandleEvents();
81         Update(dt);
82         Render();
83 }
84
85
86 void Application::HandleEvents() {
87         SDL_Event event;
88         while (SDL_PollEvent(&event)) {
89                 switch (event.type) {
90                         case SDL_KEYDOWN:
91                                 interface.HandlePress(event.key);
92                                 break;
93                         case SDL_KEYUP:
94                                 interface.HandleRelease(event.key);
95                                 break;
96                         case SDL_MOUSEBUTTONDOWN:
97                                 interface.HandlePress(event.button);
98                                 break;
99                         case SDL_MOUSEBUTTONUP:
100                                 interface.HandleRelease(event.button);
101                                 break;
102                         case SDL_MOUSEMOTION:
103                                 interface.Handle(event.motion);
104                                 break;
105                         case SDL_MOUSEWHEEL:
106                                 interface.Handle(event.wheel);
107                                 break;
108                         case SDL_QUIT:
109                                 running = false;
110                                 break;
111                         case SDL_WINDOWEVENT:
112                                 switch (event.window.event) {
113                                         case SDL_WINDOWEVENT_FOCUS_GAINED:
114                                                 window.GrabMouse();
115                                                 break;
116                                         case SDL_WINDOWEVENT_FOCUS_LOST:
117                                                 window.ReleaseMouse();
118                                                 break;
119                                         case SDL_WINDOWEVENT_RESIZED:
120                                                 cam.Viewport(event.window.data1, event.window.data2);
121                                                 interface.Handle(event.window);
122                                                 break;
123                                         default:
124                                                 interface.Handle(event.window);
125                                                 break;
126                                 }
127                                 break;
128                         default:
129                                 break;
130                 }
131         }
132 }
133
134 void Application::Update(int dt) {
135         interface.Update(dt);
136         test_controller.Update(dt);
137         world.Update(dt);
138 }
139
140 void Application::Render() {
141         GLContext::Clear();
142
143         chunk_prog.SetProjection(cam.Projection());
144         entity_prog.SetProjection(cam.Projection());
145
146         world.Render(chunk_prog, entity_prog);
147
148         interface.Render(entity_prog);
149
150         window.Flip();
151 }
152
153 }