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