#include "app.hpp"
-#include "geometry.hpp"
-
#include <iostream>
#include <stdexcept>
, init_glew()
, program()
, cam()
-, 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) {
+, interface(world)
+, running(false) {
GLContext::EnableVSync();
- hud.Viewport(960, 600);
- hud.Display(*world.BlockTypes()[place_id]);
-
glClearColor(0.0, 0.0, 0.0, 1.0);
}
switch (event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP:
- controller.HandleKeyboard(event.key);
+ interface.Handle(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;
- }
+ interface.Handle(event.button);
break;
case SDL_MOUSEMOTION:
- controller.HandleMouse(event.motion);
+ interface.Handle(event.motion);
break;
case SDL_MOUSEWHEEL:
- if (event.wheel.y < 0) {
- ++place_id;
- if (size_t(place_id) >= world.BlockTypes().Size()) {
- place_id = 1;
- }
- hud.Display(*world.BlockTypes()[place_id]);
- } else if (event.wheel.y > 0) {
- --place_id;
- if (place_id <= 0) {
- place_id = world.BlockTypes().Size() - 1;
- }
- hud.Display(*world.BlockTypes()[place_id]);
- }
+ interface.Handle(event.wheel);
break;
case SDL_QUIT:
running = false;
break;
case SDL_WINDOWEVENT_RESIZED:
cam.Viewport(event.window.data1, event.window.data2);
- hud.Viewport(event.window.data1, event.window.data2);
+ interface.Handle(event.window);
break;
default:
+ interface.Handle(event.window);
break;
}
break;
}
void Application::Update(int dt) {
- controller.Update(dt);
+ interface.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)) {
- outline_visible = true;
- outline.Clear();
- chunk->Type(chunk->BlockAt(blkid)).FillOutlineModel(outline);
- outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
- outline_transform = chunk->Transform(world.Player().ChunkCoords());
- outline_transform *= chunk->ToTransform(blkid);
- } 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() {
program.SetProjection(cam.Projection());
world.Render(program);
- if (outline_visible) {
- program.SetM(outline_transform);
- outline.Draw();
- }
-
- hud.Render(program);
+ interface.Render(program);
window.Flip();
}
#ifndef BLANK_APP_HPP_
#define BLANK_APP_HPP_
-#include <glm/glm.hpp>
-#include <glm/gtc/matrix_transform.hpp>
-
#include "camera.hpp"
-#include "controller.hpp"
-#include "hud.hpp"
#include "init.hpp"
-#include "model.hpp"
+#include "interface.hpp"
#include "shader.hpp"
#include "world.hpp"
DirectionalLighting program;
Camera cam;
- HUD hud;
World world;
- FPSController controller;
-
- OutlineModel outline;
- bool outline_visible;
- glm::mat4 outline_transform;
+ Interface interface;
bool running;
- bool place, remove, pick;
-
- int remove_id;
- int place_id;
-
};
}
FPSController::FPSController(Entity &entity)
: entity(entity)
, pitch(0)
-, yaw(0)
-, move_velocity(0.005f)
-, pitch_sensitivity(-0.0025f)
-, yaw_sensitivity(-0.001f)
-, front(false)
-, back(false)
-, left(false)
-, right(false)
-, up(false)
-, down(false) {
+, yaw(0) {
}
}
-void FPSController::HandleKeyboard(const SDL_KeyboardEvent &event) {
- switch (event.keysym.sym) {
- case SDLK_w:
- front = event.state == SDL_PRESSED;
- break;
- case SDLK_s:
- back = event.state == SDL_PRESSED;
- break;
- case SDLK_a:
- left = event.state == SDL_PRESSED;
- break;
- case SDLK_d:
- right = event.state == SDL_PRESSED;
- break;
- case SDLK_q:
- case SDLK_SPACE:
- up = event.state == SDL_PRESSED;
- break;
- case SDLK_e:
- case SDLK_LSHIFT:
- down = event.state == SDL_PRESSED;
- break;
- }
-}
-
-void FPSController::HandleMouse(const SDL_MouseMotionEvent &event) {
- RotateYaw(event.xrel * yaw_sensitivity);
- RotatePitch(event.yrel * pitch_sensitivity);
-}
-
-
void FPSController::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;
- }
entity.Rotation(glm::eulerAngleYX(yaw, pitch));
- entity.Velocity(glm::rotateY(vel, yaw));
+ entity.Velocity(glm::rotateY(velocity, yaw));
}
}
#include "entity.hpp"
#include "geometry.hpp"
-#include <SDL.h>
#include <glm/glm.hpp>
Ray Aim() const { return entity.Aim(entity.ChunkCoords()); }
+ const glm::vec3 &Velocity() const { return velocity; }
+ void Velocity(const glm::vec3 &vel) { velocity = vel; }
+
// all angles in radians (full circle = 2π)
float Pitch() const { return pitch; }
void Pitch(float p);
void Yaw(float y);
void RotateYaw(float delta);
- void HandleKeyboard(const SDL_KeyboardEvent &);
- void HandleMouse(const SDL_MouseMotionEvent &);
-
void Update(int dt);
private:
Entity &entity;
+ glm::vec3 velocity;
+
float pitch;
float yaw;
- float move_velocity;
- float pitch_sensitivity;
- float yaw_sensitivity;
-
- bool front, back, left, right, up, down;
-
};
}
--- /dev/null
+#include "interface.hpp"
+
+#include "geometry.hpp"
+#include "world.hpp"
+
+#include <glm/glm.hpp>
+#include <glm/gtc/matrix_transform.hpp>
+
+
+namespace blank {
+
+Interface::Interface(World &world)
+: world(world)
+, ctrl(world.Player())
+, hud()
+, aim_chunk(nullptr)
+, aim_block(0)
+, aim_normal()
+, outline()
+, outline_transform(1.0f)
+, move_velocity(0.005f)
+, pitch_sensitivity(-0.0025f)
+, yaw_sensitivity(-0.001f)
+, remove(0)
+, selection(1)
+, front(false)
+, back(false)
+, left(false)
+, right(false)
+, up(false)
+, down(false) {
+ hud.Viewport(960, 600);
+ hud.Display(*world.BlockTypes()[selection.type]);
+}
+
+
+void Interface::Handle(const SDL_KeyboardEvent &event) {
+ switch (event.keysym.sym) {
+ case SDLK_w:
+ front = event.state == SDL_PRESSED;
+ break;
+ case SDLK_s:
+ back = event.state == SDL_PRESSED;
+ break;
+ case SDLK_a:
+ left = event.state == SDL_PRESSED;
+ break;
+ case SDLK_d:
+ right = event.state == SDL_PRESSED;
+ break;
+ case SDLK_q:
+ case SDLK_SPACE:
+ up = event.state == SDL_PRESSED;
+ break;
+ case SDLK_e:
+ case SDLK_LSHIFT:
+ down = event.state == SDL_PRESSED;
+ break;
+ }
+}
+
+void Interface::Handle(const SDL_MouseMotionEvent &event) {
+ ctrl.RotateYaw(event.xrel * yaw_sensitivity);
+ ctrl.RotatePitch(event.yrel * pitch_sensitivity);
+}
+
+void Interface::Handle(const SDL_MouseButtonEvent &event) {
+ if (event.state != SDL_PRESSED) return;
+
+ if (event.button == 1) {
+ RemoveBlock();
+ } else if (event.button == 2) {
+ PickBlock();
+ } else if (event.button == 3) {
+ PlaceBlock();
+ }
+}
+
+void Interface::PickBlock() {
+ if (!aim_chunk) return;
+ selection = aim_chunk->BlockAt(aim_block);
+ hud.Display(*world.BlockTypes()[selection.type]);
+}
+
+void Interface::PlaceBlock() {
+ if (!aim_chunk) return;
+ Chunk *mod_chunk = aim_chunk;
+ glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
+ if (!Chunk::InBounds(next_pos)) {
+ mod_chunk = &world.Next(*aim_chunk, aim_normal);
+ next_pos -= aim_normal * glm::vec3(Chunk::Extent());
+ }
+ mod_chunk->BlockAt(next_pos) = selection;
+ mod_chunk->Invalidate();
+}
+
+void Interface::RemoveBlock() {
+ if (!aim_chunk) return;
+ aim_chunk->BlockAt(aim_block) = remove;
+ aim_chunk->Invalidate();
+}
+
+
+void Interface::Handle(const SDL_MouseWheelEvent &event) {
+ if (event.y < 0) {
+ SelectNext();
+ } else if (event.y > 0) {
+ SelectPrevious();
+ }
+}
+
+void Interface::SelectNext() {
+ ++selection.type;
+ if (size_t(selection.type) >= world.BlockTypes().Size()) {
+ selection.type = 1;
+ }
+ hud.Display(*world.BlockTypes()[selection.type]);
+}
+
+void Interface::SelectPrevious() {
+ --selection.type;
+ if (selection.type <= 0) {
+ selection.type = world.BlockTypes().Size() - 1;
+ }
+ hud.Display(*world.BlockTypes()[selection.type]);
+}
+
+void Interface::Handle(const SDL_WindowEvent &event) {
+ if (event.event == SDL_WINDOWEVENT_RESIZED) {
+ hud.Viewport(event.data1, event.data2);
+ }
+}
+
+
+void Interface::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;
+ }
+ ctrl.Velocity(vel);
+ ctrl.Update(dt);
+
+ Ray aim = ctrl.Aim();
+ float dist;
+ if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
+ outline.Clear();
+ aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
+ outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
+ outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
+ outline_transform *= aim_chunk->ToTransform(aim_block);
+ } else {
+ aim_chunk = nullptr;
+ }
+
+}
+
+
+void Interface::Render(DirectionalLighting &program) {
+ if (aim_chunk) {
+ program.SetM(outline_transform);
+ outline.Draw();
+ }
+
+ hud.Render(program);
+}
+
+}
--- /dev/null
+#ifndef BLANK_INTERFACE_HPP_
+#define BLANK_INTERFACE_HPP_
+
+#include "block.hpp"
+#include "controller.hpp"
+#include "hud.hpp"
+#include "model.hpp"
+#include "shader.hpp"
+
+#include <SDL.h>
+
+
+namespace blank {
+
+class Chunk;
+class World;
+
+class Interface {
+
+public:
+ explicit Interface(World &);
+
+ void Handle(const SDL_KeyboardEvent &);
+ void Handle(const SDL_MouseMotionEvent &);
+ void Handle(const SDL_MouseButtonEvent &);
+ void Handle(const SDL_MouseWheelEvent &);
+ void Handle(const SDL_WindowEvent &);
+
+ void PickBlock();
+ void PlaceBlock();
+ void RemoveBlock();
+
+ void SelectNext();
+ void SelectPrevious();
+
+ void Update(int dt);
+
+ void Render(DirectionalLighting &);
+
+private:
+ World &world;
+ FPSController ctrl;
+ HUD hud;
+
+ Chunk *aim_chunk;
+ int aim_block;
+ glm::vec3 aim_normal;
+
+ OutlineModel outline;
+ glm::mat4 outline_transform;
+
+ float move_velocity;
+ float pitch_sensitivity;
+ float yaw_sensitivity;
+
+ Block remove;
+ Block selection;
+
+ bool front, back, left, right, up, down;
+
+};
+
+}
+
+#endif