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