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();
35 e.Position({ 0.0f, 0.0f, 0.0f });
36 e.SetShape(world.BlockTypes()[1].shape, { 1.0f, 1.0f, 0.0f });
37 e.AngularVelocity(glm::quat(glm::vec3{ 0.00001f, 0.000006f, 0.000013f }));
42 void Application::RunN(size_t n) {
43 Uint32 last = SDL_GetTicks();
44 for (size_t i = 0; i < n; ++i) {
45 Uint32 now = SDL_GetTicks();
46 int delta = now - last;
52 void Application::RunT(size_t t) {
53 Uint32 last = SDL_GetTicks();
54 Uint32 finish = last + t;
55 while (last < finish) {
56 Uint32 now = SDL_GetTicks();
57 int delta = now - last;
63 void Application::RunS(size_t n, size_t t) {
64 for (size_t i = 0; i < n; ++i) {
70 void Application::Run() {
72 Uint32 last = SDL_GetTicks();
75 Uint32 now = SDL_GetTicks();
76 int delta = now - last;
82 void Application::Loop(int dt) {
89 void Application::HandleEvents() {
91 while (SDL_PollEvent(&event)) {
94 interface.HandlePress(event.key);
97 interface.HandleRelease(event.key);
99 case SDL_MOUSEBUTTONDOWN:
100 interface.HandlePress(event.button);
102 case SDL_MOUSEBUTTONUP:
103 interface.HandleRelease(event.button);
105 case SDL_MOUSEMOTION:
106 interface.Handle(event.motion);
109 interface.Handle(event.wheel);
114 case SDL_WINDOWEVENT:
115 switch (event.window.event) {
116 case SDL_WINDOWEVENT_FOCUS_GAINED:
119 case SDL_WINDOWEVENT_FOCUS_LOST:
120 window.ReleaseMouse();
122 case SDL_WINDOWEVENT_RESIZED:
123 cam.Viewport(event.window.data1, event.window.data2);
124 interface.Handle(event.window);
127 interface.Handle(event.window);
137 void Application::Update(int dt) {
138 interface.Update(dt);
139 test_controller.Update(dt);
143 void Application::Render() {
146 chunk_prog.SetProjection(cam.Projection());
147 entity_prog.SetProjection(cam.Projection());
149 world.Render(chunk_prog, entity_prog);
151 interface.Render(entity_prog);