1 #include "Application.hpp"
3 #include "../world/BlockType.hpp"
4 #include "../world/Entity.hpp"
12 Application::Application(const Config &config)
16 , init_gl(config.doublebuf, config.multisampling)
18 , ctx(window.CreateContext())
24 , interface(config.interface, world)
25 , test_controller(MakeTestEntity(world))
28 GLContext::EnableVSync();
31 glClearColor(0.0, 0.0, 0.0, 1.0);
34 Entity &Application::MakeTestEntity(World &world) {
35 Entity &e = world.AddEntity();
37 e.Position({ 0.0f, 0.0f, 0.0f });
38 e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
39 e.WorldCollidable(true);
40 e.SetShape(world.BlockTypes()[1].shape, { 1.0f, 1.0f, 0.0f });
41 e.AngularVelocity(glm::quat(glm::vec3{ 0.00001f, 0.000006f, 0.000013f }));
46 void Application::RunN(size_t n) {
47 Uint32 last = SDL_GetTicks();
48 for (size_t i = 0; i < n; ++i) {
49 Uint32 now = SDL_GetTicks();
50 int delta = now - last;
56 void Application::RunT(size_t t) {
57 Uint32 last = SDL_GetTicks();
58 Uint32 finish = last + t;
59 while (last < finish) {
60 Uint32 now = SDL_GetTicks();
61 int delta = now - last;
67 void Application::RunS(size_t n, size_t t) {
68 for (size_t i = 0; i < n; ++i) {
74 void Application::Run() {
76 Uint32 last = SDL_GetTicks();
79 Uint32 now = SDL_GetTicks();
80 int delta = now - last;
86 void Application::Loop(int dt) {
93 void Application::HandleEvents() {
95 while (SDL_PollEvent(&event)) {
98 interface.HandlePress(event.key);
101 interface.HandleRelease(event.key);
103 case SDL_MOUSEBUTTONDOWN:
104 interface.HandlePress(event.button);
106 case SDL_MOUSEBUTTONUP:
107 interface.HandleRelease(event.button);
109 case SDL_MOUSEMOTION:
110 interface.Handle(event.motion);
113 interface.Handle(event.wheel);
118 case SDL_WINDOWEVENT:
119 switch (event.window.event) {
120 case SDL_WINDOWEVENT_FOCUS_GAINED:
123 case SDL_WINDOWEVENT_FOCUS_LOST:
124 window.ReleaseMouse();
126 case SDL_WINDOWEVENT_RESIZED:
127 cam.Viewport(event.window.data1, event.window.data2);
128 interface.Handle(event.window);
131 interface.Handle(event.window);
141 void Application::Update(int dt) {
142 interface.Update(dt);
143 test_controller.Update(dt);
147 void Application::Render() {
150 chunk_prog.SetProjection(cam.Projection());
151 entity_prog.SetProjection(cam.Projection());
153 world.Render(chunk_prog, entity_prog);
155 interface.Render(entity_prog);