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