OpenMiner/source/states/GameState.cpp

106 lines
3.1 KiB
C++
Raw Normal View History

2014-12-15 16:47:30 +01:00
/*
* =====================================================================================
*
* Filename: GameState.cpp
*
2018-06-05 01:24:54 +02:00
* Description:
2014-12-15 16:47:30 +01:00
*
* Created: 15/12/2014 03:51:55
*
* Author: Quentin Bazin, <quent42340@gmail.com>
2014-12-15 16:47:30 +01:00
*
* =====================================================================================
*/
#define GLM_FORCE_RADIANS
#include <glm/gtc/matrix_transform.hpp>
#include "OpenGL.hpp"
#include "ApplicationStateStack.hpp"
2018-06-05 16:17:40 +02:00
#include "Config.hpp"
#include "GameClock.hpp"
#include "GameState.hpp"
#include "InventoryState.hpp"
#include "Keyboard.hpp"
#include "Mouse.hpp"
2014-12-15 16:47:30 +01:00
GameState::GameState() {
m_hotbarInventory.addStack(ItemType::Grass, 10);
m_hotbarInventory.addStack(ItemType::Glass, 10);
m_hotbarInventory.addStack(ItemType::Stone, 10);
m_playerInventory.addStack(ItemType::Wood, 4);
m_playerInventory.addStack(ItemType::Planks, 10);
m_playerInventory.addStack(ItemType::Stick, 10);
m_playerInventory.addStack(ItemType::Cobblestone, 10);
m_playerInventory.addStack(ItemType::StoneAxe, 1);
m_playerInventory.addStack(ItemType::StoneHoe, 1);
m_playerInventory.addStack(ItemType::StonePickaxe, 1);
m_playerInventory.addStack(ItemType::StoneShovel, 1);
m_playerInventory.addStack(ItemType::StoneSword, 1);
m_projectionMatrix = glm::perspective(45.0f, (float)SCREEN_WIDTH / SCREEN_HEIGHT, DIST_NEAR, DIST_FAR);
2018-06-05 01:24:54 +02:00
2018-06-25 14:04:25 +02:00
initShaders();
2014-12-15 16:47:30 +01:00
}
void GameState::onEvent(const SDL_Event &event) {
if (event.type == SDL_MOUSEMOTION) {
if(SCREEN_WIDTH / 2 != event.motion.x || SCREEN_HEIGHT / 2 != event.motion.y) {
m_camera.turnH(event.motion.xrel * 0.06);
m_camera.turnV(-event.motion.yrel * 0.06);
Mouse::resetToWindowCenter();
2018-06-14 04:19:16 +02:00
m_camera.update();
}
}
m_hotbar.onEvent(event);
m_blockCursor.onEvent(event, m_playerInventory, m_hotbarInventory, m_hotbar);
}
2014-12-15 16:47:30 +01:00
void GameState::update() {
Shader::bind(&m_shader);
m_shader.setUniform("u_time", GameClock::getTicks());
Shader::bind(nullptr);
m_world.updateChunks();
if (&m_stateStack->top() == this)
m_viewMatrix = m_camera.processInputs(m_world);
// FIXME: Shouldn't be called every tick
m_hotbar.update();
m_blockCursor.update(false);
if (Keyboard::isKeyPressedOnce(Keyboard::E) && &m_stateStack->top() == this) {
m_stateStack->push<InventoryState>(m_playerInventory, m_hotbarInventory, this);
}
2014-12-15 16:47:30 +01:00
}
2018-06-25 14:04:25 +02:00
void GameState::initShaders() {
m_shader.createProgram();
m_shader.addShader(GL_VERTEX_SHADER, "resources/shaders/game.v.glsl");
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/color.f.glsl");
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/light.f.glsl");
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/fog.f.glsl");
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/game.f.glsl");
2018-06-25 14:04:25 +02:00
m_shader.linkProgram();
}
void GameState::draw(RenderTarget &target, RenderStates states) const {
states.shader = &m_shader;
states.projectionMatrix = &m_projectionMatrix;
states.viewMatrix = &m_viewMatrix;
target.draw(m_world, states);
target.draw(m_crosshair, states);
target.draw(m_blockCursor, states);
2018-06-20 05:43:52 +02:00
target.draw(m_hotbar, states);
}