1 #include "Application.hpp"
4 #include "../graphics/Font.hpp"
5 #include "../world/BlockType.hpp"
6 #include "../world/Entity.hpp"
16 string get_asset_path() {
17 char *base = SDL_GetBasePath();
28 Application::Application(const Config &config)
32 , init_gl(config.doublebuf, config.multisampling)
34 , ctx(window.CreateContext())
36 , assets(get_asset_path())
42 , interface(config.interface, assets, world)
43 , test_controller(MakeTestEntity(world))
46 GLContext::EnableVSync();
49 glClearColor(0.0, 0.0, 0.0, 1.0);
52 Entity &Application::MakeTestEntity(World &world) {
53 Entity &e = world.AddEntity();
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 }));
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;
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;
85 void Application::RunS(size_t n, size_t t) {
86 for (size_t i = 0; i < n; ++i) {
92 void Application::Run() {
94 Uint32 last = SDL_GetTicks();
97 Uint32 now = SDL_GetTicks();
98 int delta = now - last;
104 void Application::Loop(int dt) {
111 void Application::HandleEvents() {
113 while (SDL_PollEvent(&event)) {
114 switch (event.type) {
116 interface.HandlePress(event.key);
119 interface.HandleRelease(event.key);
121 case SDL_MOUSEBUTTONDOWN:
122 interface.HandlePress(event.button);
124 case SDL_MOUSEBUTTONUP:
125 interface.HandleRelease(event.button);
127 case SDL_MOUSEMOTION:
128 interface.Handle(event.motion);
131 interface.Handle(event.wheel);
136 case SDL_WINDOWEVENT:
137 Handle(event.window);
145 void Application::Handle(const SDL_WindowEvent &event) {
146 switch (event.event) {
147 case SDL_WINDOWEVENT_FOCUS_GAINED:
150 case SDL_WINDOWEVENT_FOCUS_LOST:
151 window.ReleaseMouse();
153 case SDL_WINDOWEVENT_RESIZED:
154 cam.Viewport(event.data1, event.data2);
155 interface.Handle(event);
158 interface.Handle(event);
163 void Application::Update(int dt) {
164 interface.Update(dt);
165 test_controller.Update(dt);
169 void Application::Render() {
172 chunk_prog.SetProjection(cam.Projection());
173 entity_prog.SetProjection(cam.Projection());
175 world.Render(chunk_prog, entity_prog);
177 interface.Render(entity_prog, sprite_prog);
183 Assets::Assets(const string &base)
184 : fonts(base + "fonts/") {
188 Font Assets::LoadFont(const string &name, int size) const {
189 string full = fonts + name + ".ttf";
190 return Font(full.c_str(), size);