X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp.cpp;h=57651e3b754370602eca2d92706dd000573df1fb;hb=e53a0e2e711a7d8bd9b0ddacd1360aa14370643f;hp=d54e8df56d1d0e875da4e599cca3ae67ea4b67cb;hpb=b314df303ffedbd6d2e81872908d12fc9712801a;p=blank.git diff --git a/src/app.cpp b/src/app.cpp index d54e8df..57651e3 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -4,89 +4,70 @@ #include -namespace { - -constexpr GLfloat vtx_coords[] = { - -1.0f, -1.0f, -1.0f, - 1.0f, -1.0f, -1.0f, - 0.0f, 1.0f, -1.0f, -}; - -} - namespace blank { -Application::Application() +Application::Application(const Config &config) : init_sdl() -, init_gl() +, init_img() +, init_gl(config.doublebuf, config.multisampling) , window() , ctx(window.CreateContext()) , init_glew() -, program() -, vtx_buf(0) -, mvp() -, mvp_handle(0) +, chunk_prog() +, entity_prog() +, cam() +, world(config.world) +, interface(config.interface, world) +, test_controller(MakeTestEntity(world)) , running(false) { - GLContext::EnableVSync(); - program.LoadShader( - GL_VERTEX_SHADER, - "#version 330 core\n" - "layout(location = 0) in vec3 vertexPosition_modelspace;\n" - "uniform mat4 MVP;\n" - "void main() {\n" - "vec4 v = vec4(vertexPosition_modelspace, 1);\n" - "gl_Position = MVP * v;\n" - "}\n" - ); - program.LoadShader( - GL_FRAGMENT_SHADER, - "#version 330 core\n" - "out vec3 color;\n" - "void main() {\n" - "color = vec3(1, 1, 1);\n" - "}\n" - ); - program.Link(); - if (!program.Linked()) { - program.Log(std::cerr); - throw std::runtime_error("link program"); + if (config.vsync) { + GLContext::EnableVSync(); } - GLuint VertexArrayID; - glGenVertexArrays(1, &VertexArrayID); - glBindVertexArray(VertexArrayID); - - glGenBuffers(1, &vtx_buf); - glBindBuffer(GL_ARRAY_BUFFER, vtx_buf); - glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW); - - glm::mat4 projection = glm::perspective( - 45.0f, // FOV in degrees - 1.0f, // aspect ratio - 0.1f, // near clip - 100.0f // far clip - ); - glm::mat4 view = glm::lookAt( - glm::vec3(0, 0, 0), // observer - glm::vec3(0, 0, -1), // target - glm::vec3(0, 1, 0) // up - ); - glm::mat4 model(1.0f); // identity: no transformation - mvp = projection * view * model; - - mvp_handle = program.UniformLocation("MVP"); - glClearColor(0.0, 0.0, 0.0, 1.0); } -Application::~Application() { +Entity &Application::MakeTestEntity(World &world) { + Entity &e = world.AddEntity(); + e.Position({ 0.0f, 0.0f, 0.0f }); + e.SetShape(world.BlockTypes()[1].shape, { 1.0f, 1.0f, 0.0f }); + e.AngularVelocity(glm::quat(glm::vec3{ 0.00001f, 0.000006f, 0.000013f })); + return e; +} + + +void Application::RunN(size_t n) { + Uint32 last = SDL_GetTicks(); + for (size_t i = 0; i < n; ++i) { + Uint32 now = SDL_GetTicks(); + int delta = now - last; + Loop(delta); + last = now; + } +} + +void Application::RunT(size_t t) { + Uint32 last = SDL_GetTicks(); + Uint32 finish = last + t; + while (last < finish) { + Uint32 now = SDL_GetTicks(); + int delta = now - last; + Loop(delta); + last = now; + } +} +void Application::RunS(size_t n, size_t t) { + for (size_t i = 0; i < n; ++i) { + Loop(t); + } } void Application::Run() { running = true; Uint32 last = SDL_GetTicks(); + window.GrabMouse(); while (running) { Uint32 now = SDL_GetTicks(); int delta = now - last; @@ -97,6 +78,7 @@ void Application::Run() { void Application::Loop(int dt) { HandleEvents(); + Update(dt); Render(); } @@ -105,38 +87,65 @@ void Application::HandleEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { + case SDL_KEYDOWN: + interface.HandlePress(event.key); + break; + case SDL_KEYUP: + interface.HandleRelease(event.key); + break; + case SDL_MOUSEBUTTONDOWN: + interface.HandlePress(event.button); + break; + case SDL_MOUSEBUTTONUP: + interface.HandleRelease(event.button); + break; + case SDL_MOUSEMOTION: + interface.Handle(event.motion); + break; + case SDL_MOUSEWHEEL: + interface.Handle(event.wheel); + break; case SDL_QUIT: running = false; break; + case SDL_WINDOWEVENT: + switch (event.window.event) { + case SDL_WINDOWEVENT_FOCUS_GAINED: + window.GrabMouse(); + break; + case SDL_WINDOWEVENT_FOCUS_LOST: + window.ReleaseMouse(); + break; + case SDL_WINDOWEVENT_RESIZED: + cam.Viewport(event.window.data1, event.window.data2); + interface.Handle(event.window); + break; + default: + interface.Handle(event.window); + break; + } + break; default: break; } } } +void Application::Update(int dt) { + interface.Update(dt); + test_controller.Update(dt); + world.Update(dt); +} + void Application::Render() { - glClear(GL_COLOR_BUFFER_BIT); - - program.Use(); - - glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); - - glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, vtx_buf); - glVertexAttribPointer( - 0, // attribute 0 (for shader) - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized - 0, // stride - nullptr // offset - ); - glDrawArrays( - GL_TRIANGLES, // how - 0, // start - 3 // len - ); - glDisableVertexAttribArray(0); + GLContext::Clear(); + + chunk_prog.SetProjection(cam.Projection()); + entity_prog.SetProjection(cam.Projection()); + + world.Render(chunk_prog, entity_prog); + + interface.Render(entity_prog); window.Flip(); }