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