Skip to content

Commit

Permalink
Add Game State
Browse files Browse the repository at this point in the history
Adds the state in which the main part of game will
run (player training). In addition, the renderer
improves the drawing of 2D objects to ignore the
depth buffer, which would be problematic if 3D
objects were also drawn.
  • Loading branch information
Notiooo committed Nov 12, 2023
1 parent 4e8b47f commit 50cc6e3
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 14 deletions.
2 changes: 1 addition & 1 deletion AimGL/resources/Shaders/Graphics/Sprite.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ in vec2 TexCoords;
out vec4 color;

uniform sampler2D spriteTexture;
uniform float opacity = 0.f;
uniform float opacity = 1.f;

void main()
{
Expand Down
Binary file added AimGL/resources/Textures/logo_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(PROJECT_SOURCES
States/StateStack.cpp
States/CustomStates/LogoState.cpp
States/CustomStates/ExitGameState.cpp
States/CustomStates/GameState.cpp
World/Camera.cpp
World/InfiniteGridFloor.cpp
Utils/Mouse.cpp
Expand Down
5 changes: 5 additions & 0 deletions AimGL/src/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Game.h"
#include "States/CustomStates/ExitGameState.h"
#include "States/CustomStates/GameState.h"
#include "States/CustomStates/LogoState.h"
#include "Utils/Mouse.h"
#include "constants.h"
#include "pch.h"

Expand Down Expand Up @@ -60,12 +62,14 @@ Game::Game()
throw std::runtime_error("Failed to initialize GLEW");
}

glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Setup all application-flow states
mAppStack.saveState<LogoState>(State_ID::LogoState, *mGameWindow);
mAppStack.saveState<ExitGameState>(State_ID::ExitGameState);
mAppStack.saveState<GameState>(State_ID::GameState, *mGameWindow);

// Initial state of the statestack is TitleState
mAppStack.push(State_ID::LogoState);
Expand Down Expand Up @@ -206,6 +210,7 @@ void Game::update(const sf::Time& deltaTime)
{
MTR_SCOPE("Game", "Game::update");
auto deltaTimeInSeconds = deltaTime.asSeconds();
Mouse::update(deltaTimeInSeconds, *mGameWindow);

updateImGui(deltaTime);

Expand Down
24 changes: 24 additions & 0 deletions AimGL/src/Renderer3D/Renderer3D.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Renderer3D.h"
#include "World/Camera.h"
#include "pch.h"

Renderer3D::Renderer3D(sf::Window& window)
Expand All @@ -16,6 +17,29 @@ void Renderer3D::draw(const VertexArray& va, const IndexBuffer& ib, const Shader
glm::mat4 windowOrthoProjection = glm::ortho(0.f, static_cast<float>(mWindow.getSize().x), 0.f,
static_cast<float>(mWindow.getSize().y));
shader.setUniform("windowOrthoProjection", windowOrthoProjection);

glDisable(GL_DEPTH_TEST);
GLCall(glDrawElements(toOpenGl(drawMode), ib.size(), GL_UNSIGNED_INT, nullptr));
glEnable(GL_DEPTH_TEST);

#ifdef _DEBUG
shader.unbind();
va.unbind();
ib.unbind();
#endif
}

void Renderer3D::draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const Camera& camera, const DrawMode& drawMode) const
{
shader.bind();
va.bind();
ib.bind();

const auto view = camera.view();
const auto projection = camera.projection();
shader.setUniform("view", view);
shader.setUniform("projection", projection);
GLCall(glDrawElements(toOpenGl(drawMode), ib.size(), GL_UNSIGNED_INT, nullptr));

#ifdef _DEBUG
Expand Down
5 changes: 5 additions & 0 deletions AimGL/src/Renderer3D/Renderer3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "Renderer3D/Shader.h"
#include "Renderer3D/VertexArray.h"

class Camera;

/**
* 3D Renderer allows to draw 3D objects to the screen using appropriate buffers and shaders
*/
Expand All @@ -29,6 +31,9 @@ class Renderer3D
void draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const DrawMode& drawMode = DrawMode::Triangles) const;

void draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const Camera& camera, const DrawMode& drawMode = DrawMode::Triangles) const;

private:
unsigned toOpenGl(const DrawMode& drawMode) const;
sf::Window& mWindow;
Expand Down
44 changes: 33 additions & 11 deletions AimGL/src/Renderer3D/Sprite2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Sprite2D::Sprite2D(const Texture& texture)
, mPosition(0.0f)
, mScale(1.0f)
, mRotation(0.f)
, mDimensions({mTexture.width(), mTexture.height()})
{

initializeBuffers();
Expand Down Expand Up @@ -43,7 +44,6 @@ void Sprite2D::initializeBuffers()

void Sprite2D::draw(const Renderer3D& target) const
{
// Prepare transformations
mShader.bind();
mTexture.bind(0);
target.draw(mVAO, mEBO, mShader);
Expand All @@ -53,8 +53,12 @@ void Sprite2D::updateModel()
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(mPosition));
model = glm::rotate(model, glm::radians(mRotation), glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::scale(model, glm::vec3(mScale, 1.0f));

// Negative mRotation makes it rotate clockwise for positive angles
model = glm::rotate(model, glm::radians(-mRotation), glm::vec3(0.0f, 0.0f, 1.0f));

// Dividing dimensions by two is needed as vertices are from -1 to 1
model = glm::scale(model, glm::vec3(mDimensions * mScale / 2.f, 1.0f));
mShader.bind();
mShader.setUniform("model", model);
mShader.unbind();
Expand All @@ -73,35 +77,53 @@ void Sprite2D::setPosition(const glm::vec2& newPosition, Origin origin)
updateModel();
}

void Sprite2D::showDebugImGui(std::string name)
{
name = "[Sprite2D] " + name;
ImGui::Begin(name.c_str());
ImGui::SliderFloat2("Position", &mPosition[0], -1500.f, 1500.f);
ImGui::SliderFloat2("Dimensions", &mDimensions[0], -0.f, 1500.f);
ImGui::SliderFloat("Rotation", &mRotation, 0.0f, 360.f);
ImGui::SliderFloat("Scale", &mScale, -1.0f, 10.0f);
ImGui::SliderFloat("Opacity", &mOpacity, 0.0f, 1.0f);
ImGui::End();
updateModel();
updateOpacity();
}

void Sprite2D::setScale(float newScale)
{
mScale = {mTexture.width(), mTexture.height()};
mScale *= newScale;
mScale = newScale;
updateModel();
}

void Sprite2D::setRotation(float angle)
{
mRotation = angle;
updateModel();
}

void Sprite2D::setOpacity(float opacity)
void Sprite2D::updateOpacity() const
{
mOpacity = opacity;
mShader.bind();
mShader.setUniform("opacity", mOpacity);
mShader.unbind();
}

void Sprite2D::setOpacity(float opacity)
{
mOpacity = opacity;
updateOpacity();
}

void Sprite2D::setWidth(float width)
{
mScale = {width, width / mTexture.aspectRatio()};
mScale /= 2.f;
mDimensions = {width, width / mTexture.aspectRatio()};
updateModel();
}

void Sprite2D::setHeight(float height)
{
mScale = {height * mTexture.aspectRatio(), height};
mScale /= 2.f;
mDimensions = {height * mTexture.aspectRatio(), height};
updateModel();
}
15 changes: 14 additions & 1 deletion AimGL/src/Renderer3D/Sprite2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,24 @@ class Sprite2D
*/
void setHeight(float height);

/**
* \brief Displays a debug ImGui window that allows to change the internal
* variables of the sprite.
* \param name Optional name of the rectangle (it can be seen in the window name).
*/
void showDebugImGui(std::string name = "");

private:
/**
* \brief Updates the model in the shader (model of MVP)
*/
void updateModel();

/**
* \brief Updates the opacity in the shader
*/
void updateOpacity() const;

/**
* \brief Prepares buffers to work
*/
Expand All @@ -92,7 +104,8 @@ class Sprite2D
Shader mShader;
BufferLayout mBufferLayout;
glm::vec3 mPosition;
glm::vec2 mScale;
float mScale;
glm::vec2 mDimensions;
float mRotation;
float mOpacity;
};
76 changes: 76 additions & 0 deletions AimGL/src/States/CustomStates/GameState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "GameState.h"
#include "Renderer3D/Sprite3D.h"
#include "States/StateStack.h"
#include "Utils/Mouse.h"
#include "pch.h"

GameState::GameState(StateStack& stack, WindowToRender& window)
: State(stack)
, mWindow(window)
, mRenderer3D(mWindow)
, mLogoTexture("resources/Textures/logo_background.png")
, mLogo(mLogoTexture)
, mCamera(window)
, mGameBackground(glm::vec2(1280, 720.f), glm::vec4(0.85f, 0.85f, 0.85f, 1.f))
, mPhaseInLogoColor({window.getSize().x, window.getSize().y}, {0.067f, 0.11f, 0.18f, 1.1f})
{
Mouse::lockMouseAtCenter(mWindow);
mLogo.setHeight(2.f);
mLogo.setPosition(glm::vec3(0, 0, 0), Sprite3D::Origin::LeftBottom);
mLogo.setOpacity(1);
mGameBackground.setPosition({0, 0});
mPhaseInLogoColor.setPosition({0, 0});
mPhaseInClock.restart();
mCamera.cameraPosition({1, 1.5, 2});
}

void GameState::draw(sf::Window& target) const
{
MTR_SCOPE("GameState", "GameState::draw");
mGameBackground.draw(mRenderer3D);
mInfiniteGridFloor.draw(target, mCamera);
mLogo.draw(mRenderer3D, mCamera);
mPhaseInLogoColor.draw(mRenderer3D);
}

bool GameState::fixedUpdate(const float& deltaTime)
{
MTR_SCOPE("GameState", "GameState::fixedUpdate");
mCamera.fixedUpdate(deltaTime);
return true;
}

bool GameState::update(const float& deltaTime)
{
MTR_SCOPE("GameState", "GameState::update");
mCamera.update(deltaTime);

if (mPhaseInLogoColor.opacity() > 0)
{
mPhaseInLogoColor.setOpacity(mPhaseInLogoColor.opacity() - deltaTime / 4.f);
}
return true;
}

bool GameState::handleEvent(const sf::Event& event)
{
MTR_SCOPE("GameState", "GameState::handleEvent");
mCamera.handleEvent(event);

if (event.type == sf::Event::KeyPressed)
{
switch (event.key.code)
{
case sf::Keyboard::Escape: Mouse::unlockMouse(mWindow); break;
}
}
return true;
}

bool GameState::updateImGui(const float& deltaTime)
{
MTR_SCOPE("GameState", "GameState::updateImGui");
mCamera.updateImGui();
ImGui::ShowDemoWindow();
return true;
}
61 changes: 61 additions & 0 deletions AimGL/src/States/CustomStates/GameState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include "Renderer3D/Rectangle2D.h"
#include "Renderer3D/Renderer3D.h"
#include "Renderer3D/Sprite3D.h"
#include "States/State.h"
#include "World/Camera.h"
#include "World/InfiniteGridFloor.h"

class StateStack;

/**
* \brief The game state in which all the action of the game (player training) takes place.
*/
class GameState : public State
{
public:
GameState(StateStack& stack, WindowToRender& window);

/**
* \brief Draws only this state to the passed target
* \param target where it should be drawn to
*/
void draw(sf::Window& target) const override;


/**
* \brief Updates the state logic at equal intervals independent of the frame rate.
* \param deltaTime Time interval
*/
bool fixedUpdate(const float& deltaTime) override;

/**
* \brief Updates the game logic dependent, or independent of time, every rendered frame.
* \param deltaTime the time that has passed since the game was last updated.
*/
bool update(const float& deltaTime) override;

/**
* \brief It takes input (event) from the user and interprets it
* \param event user input
*/
bool handleEvent(const sf::Event& event) override;

/**
* \brief Updates the imgui logic dependent, or independent of time, every rendered frame.
* \param deltaTime the time that has passed since the game was last updated.
*/
bool updateImGui(const float& deltaTime) override;

private:
WindowToRender& mWindow;
Renderer3D mRenderer3D;
Texture mLogoTexture;
Sprite3D mLogo;
Camera mCamera;
Rectangle2D mGameBackground;
sf::Clock mPhaseInClock;
Rectangle2D mPhaseInLogoColor;
InfiniteGridFloor mInfiniteGridFloor;
};
2 changes: 1 addition & 1 deletion AimGL/src/States/CustomStates/LogoState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ bool LogoState::update(const float& deltaTime)
if (mClock.getElapsedTime().asSeconds() > 4.f)
{
requestPop();
requestPush(State_ID::ExitGameState);
requestPush(State_ID::GameState);
}
break;
}
Expand Down

0 comments on commit 50cc6e3

Please sign in to comment.