X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp.cpp;h=cfb16822897a01027da39d62c3624da6fe7fd52b;hb=46509f82dcea114b004c53a7f3a9608f2518077f;hp=3de246c8bff2b84b56eb33ad8fb59a7d3148f00f;hpb=59fe611e6ee06f8e9445f1b19339a1f158c9322c;p=blank.git diff --git a/src/app.cpp b/src/app.cpp index 3de246c..cfb1682 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -4,16 +4,6 @@ #include -namespace { - -constexpr GLfloat vtx_coords[] = { - -1.0f, -1.0f, 0.0f, - 1.0f, -1.0f, 0.0f, - 0.0f, 1.0f, 0.0f, -}; - -} - namespace blank { Application::Application() @@ -24,64 +14,50 @@ Application::Application() , ctx(window.CreateContext()) , init_glew() , program() -, move_velocity(0.001f) -, pitch_sensitivity(-0.0025f) -, yaw_sensitivity(-0.001f) , cam() -, model() -, vtx_buf(0) -, mvp_handle(0) -, running(false) -, front(false) -, back(false) -, left(false) -, right(false) -, up(false) -, down(false) { +, world() +, interface(world) +, test_controller(MakeTestEntity(world)) +, running(false) { GLContext::EnableVSync(); - GLContext::EnableBackfaceCulling(); - 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"); - } - - 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); + glClearColor(0.0, 0.0, 0.0, 1.0); +} - model.Position(glm::vec3(0, 0, -4)); - cam.Position(glm::vec3(0, 0, 4)); +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; +} - mvp_handle = program.UniformLocation("MVP"); - glClearColor(0.0, 0.0, 0.0, 1.0); +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; + } } -Application::~Application() { +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); + } } @@ -110,40 +86,34 @@ void Application::HandleEvents() { switch (event.type) { case SDL_KEYDOWN: case SDL_KEYUP: - switch (event.key.keysym.sym) { - case SDLK_w: - front = event.key.state == SDL_PRESSED; - break; - case SDLK_s: - back = event.key.state == SDL_PRESSED; - break; - case SDLK_a: - left = event.key.state == SDL_PRESSED; - break; - case SDLK_d: - right = event.key.state == SDL_PRESSED; - break; - case SDLK_q: - up = event.key.state == SDL_PRESSED; - break; - case SDLK_e: - down = event.key.state == SDL_PRESSED; - break; - } + interface.Handle(event.key); + break; + case SDL_MOUSEBUTTONDOWN: + interface.Handle(event.button); break; case SDL_MOUSEMOTION: - cam.RotateYaw(event.motion.xrel * yaw_sensitivity); - cam.RotatePitch(event.motion.yrel * pitch_sensitivity); + 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; @@ -154,52 +124,20 @@ void Application::HandleEvents() { } void Application::Update(int dt) { - glm::vec3 vel; - if (right && !left) { - vel.x = move_velocity; - } else if (left && !right) { - vel.x = -move_velocity; - } - if (up && !down) { - vel.y = move_velocity; - } else if (down && !up) { - vel.y = -move_velocity; - } - if (back && !front) { - vel.z = move_velocity; - } else if (front && !back) { - vel.z = -move_velocity; - } - cam.Velocity(vel); - - cam.Update(dt); - model.Update(dt); + interface.Update(dt); + test_controller.Update(dt); + world.Update(dt); } void Application::Render() { - glClear(GL_COLOR_BUFFER_BIT); - - program.Use(); - - glm::mat4 mvp(cam.MakeMVP(model.Transform())); - 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(); + + program.Activate(); + + program.SetProjection(cam.Projection()); + world.Render(program); + + interface.Render(program); window.Flip(); }