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
|
|
|
|
*
|
2018-06-12 09:24:43 +02:00
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
2014-12-15 16:47:30 +01:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
|
2018-06-13 03:47:20 +02:00
|
|
|
#include "OpenGL.hpp"
|
|
|
|
|
2018-06-05 16:17:40 +02:00
|
|
|
#include "Config.hpp"
|
2018-06-18 18:56:30 +02:00
|
|
|
#include "GameClock.hpp"
|
2018-06-13 21:48:35 +02:00
|
|
|
#include "Keyboard.hpp"
|
2018-06-13 03:47:20 +02:00
|
|
|
#include "Mouse.hpp"
|
2014-12-15 16:47:30 +01:00
|
|
|
#include "GameState.hpp"
|
|
|
|
|
2018-06-15 00:05:17 +02:00
|
|
|
GameState::GameState() {
|
|
|
|
initShaders();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameState::initShaders() {
|
2015-02-06 01:44:16 +01:00
|
|
|
m_shader.createProgram();
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2015-02-06 01:44:16 +01:00
|
|
|
m_shader.addShader(GL_VERTEX_SHADER, "shaders/game.v.glsl");
|
|
|
|
m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/color.f.glsl");
|
|
|
|
m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/light.f.glsl");
|
|
|
|
m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/fog.f.glsl");
|
|
|
|
m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/game.f.glsl");
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2015-02-06 01:44:16 +01:00
|
|
|
m_shader.linkProgram();
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
Shader::bind(&m_shader);
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-13 03:47:20 +02:00
|
|
|
m_projectionMatrix = glm::perspective(45.0f, (float)SCREEN_WIDTH / SCREEN_HEIGHT, DIST_NEAR, DIST_FAR);
|
2015-02-06 01:44:16 +01:00
|
|
|
m_shader.setUniform("u_projectionMatrix", m_projectionMatrix);
|
2014-12-18 07:02:48 +01:00
|
|
|
m_shader.setUniform("u_tex", 0);
|
2018-06-20 03:05:39 +02:00
|
|
|
m_shader.setUniform("u_time", 0);
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-15 16:47:30 +01:00
|
|
|
Shader::bind(nullptr);
|
|
|
|
}
|
|
|
|
|
2018-06-14 02:38:02 +02: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();
|
2018-06-14 02:38:02 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-14 04:35:11 +02:00
|
|
|
|
2018-06-15 00:05:17 +02:00
|
|
|
m_blockCursor.onEvent(event);
|
2018-06-14 02:38:02 +02:00
|
|
|
}
|
|
|
|
|
2014-12-15 16:47:30 +01:00
|
|
|
void GameState::update() {
|
2018-06-20 03:05:39 +02:00
|
|
|
Shader::bind(&m_shader);
|
|
|
|
m_shader.setUniform("u_time", GameClock::getTicks());
|
|
|
|
Shader::bind(nullptr);
|
2018-06-18 18:56:30 +02:00
|
|
|
|
2018-06-14 23:07:20 +02:00
|
|
|
m_world.updateChunks();
|
|
|
|
|
2018-06-18 12:24:46 +02:00
|
|
|
m_viewMatrix = m_camera.processInputs(m_world);
|
2018-06-13 03:47:20 +02:00
|
|
|
|
2018-06-15 00:05:17 +02:00
|
|
|
m_blockCursor.update(false);
|
2014-12-15 16:47:30 +01:00
|
|
|
}
|
|
|
|
|
2018-06-14 23:36:01 +02:00
|
|
|
void GameState::draw(RenderTarget &target, RenderStates states) const {
|
2018-06-14 21:16:56 +02:00
|
|
|
states.shader = &m_shader;
|
2018-06-14 23:36:01 +02:00
|
|
|
states.projectionMatrix = &m_projectionMatrix;
|
|
|
|
states.viewMatrix = &m_viewMatrix;
|
|
|
|
|
|
|
|
target.draw(m_world, states);
|
|
|
|
target.draw(m_crosshair, states);
|
2018-06-15 00:05:17 +02:00
|
|
|
target.draw(m_blockCursor, states);
|
2018-06-13 21:48:35 +02:00
|
|
|
}
|
|
|
|
|