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