X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp.cpp;h=7ddbbf189180d43dccd577a91e68a057ef1c8f0f;hb=e70967c971f77a4ac0f5c074e6eb94bdd0e2b7ab;hp=de7a39ea48add859bcebda28a3f9fcfbe6efe00a;hpb=0a13b3fe342db83eef2c0ddc991a44452df77aa3;p=blank.git diff --git a/src/app.cpp b/src/app.cpp index de7a39e..7ddbbf1 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -1,78 +1,77 @@ #include "app.hpp" +#include "geometry.hpp" + #include #include -namespace { - -constexpr GLfloat vtx_coords[] = { - -1.0f, -1.0f, -5.0f, - 1.0f, -1.0f, -5.0f, - 0.0f, 1.0f, -5.0f, -}; - -} - namespace blank { Application::Application() : init_sdl() +, init_img() , init_gl() , window() , ctx(window.CreateContext()) , init_glew() , program() , cam() -, vtx_buf(0) -, mvp_handle(0) -, running(false) { +, hud() +, world() +, controller(world.Player()) +, outline() +, outline_visible(false) +, outline_transform(1.0f) +, running(false) +, place(false) +, remove(false) +, pick(false) +, remove_id(0) +, place_id(1) { 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"); - } - GLuint VertexArrayID; - glGenVertexArrays(1, &VertexArrayID); - glBindVertexArray(VertexArrayID); + world.Generate({ -4, -4, -4 }, { 5, 5, 5}); - glGenBuffers(1, &vtx_buf); - glBindBuffer(GL_ARRAY_BUFFER, vtx_buf); - glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW); - - mvp_handle = program.UniformLocation("MVP"); + hud.Viewport(960, 600); + hud.Display(*world.BlockTypes()[place_id]); glClearColor(0.0, 0.0, 0.0, 1.0); } -Application::~Application() { +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; @@ -83,6 +82,7 @@ void Application::Run() { void Application::Loop(int dt) { HandleEvents(); + Update(dt); Render(); } @@ -91,6 +91,25 @@ void Application::HandleEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { + case SDL_KEYDOWN: + case SDL_KEYUP: + controller.HandleKeyboard(event.key); + break; + case SDL_MOUSEBUTTONDOWN: + if (event.button.button == 1) { + // left + remove = true; + } else if (event.button.button == 2) { + // middle + pick = true; + } else if (event.button.button == 3) { + // right + place = true; + } + break; + case SDL_MOUSEMOTION: + controller.HandleMouse(event.motion); + break; case SDL_QUIT: running = false; break; @@ -98,6 +117,7 @@ void Application::HandleEvents() { switch (event.window.event) { case SDL_WINDOWEVENT_RESIZED: cam.Viewport(event.window.data1, event.window.data2); + hud.Viewport(event.window.data1, event.window.data2); break; default: break; @@ -109,31 +129,69 @@ void Application::HandleEvents() { } } +void Application::Update(int dt) { + controller.Update(dt); + world.Update(dt); + + Ray aim = controller.Aim(); + Chunk *chunk; + int blkid; + float dist; + glm::vec3 normal; + if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) { + glm::vec3 pos = Chunk::ToCoords(blkid); + outline_visible = true; + outline.Clear(); + chunk->Type(chunk->BlockAt(blkid)).FillOutlineModel(outline); + outline_transform = glm::translate(chunk->Transform(world.Player().ChunkCoords()), pos); + outline_transform = glm::scale(outline_transform, glm::vec3(1.0001f)); + } else { + outline_visible = false; + } + + if (pick) { + if (chunk) { + place_id = chunk->BlockAt(blkid).type; + hud.Display(*world.BlockTypes()[place_id]); + } + pick = false; + } + if (remove) { + if (chunk) { + chunk->BlockAt(blkid).type = remove_id; + chunk->Invalidate(); + } + remove = false; + } + if (place) { + if (chunk) { + Chunk *mod_chunk = chunk; + glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal; + if (!Chunk::InBounds(next_pos)) { + mod_chunk = &world.Next(*chunk, normal); + next_pos -= normal * glm::vec3(Chunk::Extent()); + } + mod_chunk->BlockAt(next_pos).type = place_id; + mod_chunk->Invalidate(); + } + place = false; + } +} + void Application::Render() { - glClear(GL_COLOR_BUFFER_BIT); - - program.Use(); - - glm::mat4 model(1.0f); // identity: no transformation - glm::mat4 mvp(cam.MakeMVP(model)); - 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); + + if (outline_visible) { + program.SetM(outline_transform); + outline.Draw(); + } + + hud.Render(program); window.Flip(); }