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