1 #include "Application.hpp"
3 #include "../world/BlockType.hpp"
4 #include "../world/Entity.hpp"
12 Application::Application(const Config &config)
15 , init_gl(config.doublebuf, config.multisampling)
17 , ctx(window.CreateContext())
23 , interface(config.interface, world)
24 , test_controller(MakeTestEntity(world))
27 GLContext::EnableVSync();
30 glClearColor(0.0, 0.0, 0.0, 1.0);
33 Entity &Application::MakeTestEntity(World &world) {
34 Entity &e = world.AddEntity();
36 e.Position({ 0.0f, 0.0f, 0.0f });
37 e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
38 e.WorldCollidable(true);
39 e.SetShape(world.BlockTypes()[1].shape, { 1.0f, 1.0f, 0.0f });
40 e.AngularVelocity(glm::quat(glm::vec3{ 0.00001f, 0.000006f, 0.000013f }));
45 void Application::RunN(size_t n) {
46 Uint32 last = SDL_GetTicks();
47 for (size_t i = 0; i < n; ++i) {
48 Uint32 now = SDL_GetTicks();
49 int delta = now - last;
55 void Application::RunT(size_t t) {
56 Uint32 last = SDL_GetTicks();
57 Uint32 finish = last + t;
58 while (last < finish) {
59 Uint32 now = SDL_GetTicks();
60 int delta = now - last;
66 void Application::RunS(size_t n, size_t t) {
67 for (size_t i = 0; i < n; ++i) {
73 void Application::Run() {
75 Uint32 last = SDL_GetTicks();
78 Uint32 now = SDL_GetTicks();
79 int delta = now - last;
85 void Application::Loop(int dt) {
92 void Application::HandleEvents() {
94 while (SDL_PollEvent(&event)) {
97 interface.HandlePress(event.key);
100 interface.HandleRelease(event.key);
102 case SDL_MOUSEBUTTONDOWN:
103 interface.HandlePress(event.button);
105 case SDL_MOUSEBUTTONUP:
106 interface.HandleRelease(event.button);
108 case SDL_MOUSEMOTION:
109 interface.Handle(event.motion);
112 interface.Handle(event.wheel);
117 case SDL_WINDOWEVENT:
118 switch (event.window.event) {
119 case SDL_WINDOWEVENT_FOCUS_GAINED:
122 case SDL_WINDOWEVENT_FOCUS_LOST:
123 window.ReleaseMouse();
125 case SDL_WINDOWEVENT_RESIZED:
126 cam.Viewport(event.window.data1, event.window.data2);
127 interface.Handle(event.window);
130 interface.Handle(event.window);
140 void Application::Update(int dt) {
141 interface.Update(dt);
142 test_controller.Update(dt);
146 void Application::Render() {
149 chunk_prog.SetProjection(cam.Projection());
150 entity_prog.SetProjection(cam.Projection());
152 world.Render(chunk_prog, entity_prog);
154 interface.Render(entity_prog);