Skip to content

Commit

Permalink
Add LogoState
Browse files Browse the repository at this point in the history
Adds a state displaying the game logo via fadein and fadeout.
The game then closes.
  • Loading branch information
Notiooo committed Nov 10, 2023
1 parent 9f9792f commit babe040
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 98 deletions.
3 changes: 2 additions & 1 deletion AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ set(PROJECT_SOURCES
Renderer3D/Sprite2D.cpp
States/State.cpp
States/StateStack.cpp
States/CustomStates/SampleState.cpp
States/CustomStates/LogoState.cpp
States/CustomStates/ExitGameState.cpp
)
13 changes: 9 additions & 4 deletions AimGL/src/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Game.h"
#include "States/CustomStates/SampleState.h"
#include "States/CustomStates/ExitGameState.h"
#include "States/CustomStates/LogoState.h"
#include "constants.h"
#include "pch.h"

Expand Down Expand Up @@ -59,11 +60,15 @@ Game::Game()
throw std::runtime_error("Failed to initialize GLEW");
}

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

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

// Initial state of the statestack is TitleState
mAppStack.push(State_ID::SampleState);
mAppStack.push(State_ID::LogoState);
}

void Game::run()
Expand Down Expand Up @@ -215,7 +220,7 @@ void Game::update(const sf::Time& deltaTime)
void Game::render()
{
MTR_SCOPE("Game", "Game::render");
glClearColor(0.43f, 0.69f, 1.0f, 1.0f);
glClearColor(0.f, 0.f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// draw the application
Expand Down
9 changes: 9 additions & 0 deletions AimGL/src/Renderer3D/Renderer3D.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#include "Renderer3D.h"
#include "pch.h"

Renderer3D::Renderer3D(sf::Window& window)
: mWindow(window)
{
}

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

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);
GLCall(glDrawElements(toOpenGl(drawMode), ib.size(), GL_UNSIGNED_INT, nullptr));

#ifdef _DEBUG
Expand Down
3 changes: 3 additions & 0 deletions AimGL/src/Renderer3D/Renderer3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Renderer3D
Quads
};

Renderer3D(sf::Window& window);

/**
* Draws the data given in VertexArray, IndexBuffer to the screen using the interpretation given
* in Shader.
Expand All @@ -29,4 +31,5 @@ class Renderer3D

private:
unsigned toOpenGl(const DrawMode& drawMode) const;
sf::Window& mWindow;
};
23 changes: 14 additions & 9 deletions AimGL/src/Renderer3D/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,52 @@ void Shader::unbind() const
GLCall(glUseProgram(0));
}

void Shader::setUniform(const std::string& name, float f1, float f2, float f3, float f4)
void Shader::setUniform(const std::string& name, glm::mat4 mat) const
{
GLCall(glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, glm::value_ptr(mat[0])));
}

void Shader::setUniform(const std::string& name, float f1, float f2, float f3, float f4) const
{
GLCall(glUniform4f(getUniformLocation(name), f1, f2, f3, f4));
}

void Shader::setUniform(const std::string& name, float f1, float f2, float f3)
void Shader::setUniform(const std::string& name, float f1, float f2, float f3) const
{
GLCall(glUniform3f(getUniformLocation(name), f1, f2, f3));
}

void Shader::setUniform(const std::string& name, float f1, float f2)
void Shader::setUniform(const std::string& name, float f1, float f2) const
{
GLCall(glUniform2f(getUniformLocation(name), f1, f2));
}

void Shader::setUniform(const std::string& name, float f1)
void Shader::setUniform(const std::string& name, float f1) const
{
GLCall(glUniform1f(getUniformLocation(name), f1));
}

void Shader::setUniform(const std::string& name, int i1, int i2, int i3, int i4)
void Shader::setUniform(const std::string& name, int i1, int i2, int i3, int i4) const
{
GLCall(glUniform4i(getUniformLocation(name), i1, i2, i3, i4));
}

void Shader::setUniform(const std::string& name, int i1, int i2, int i3)
void Shader::setUniform(const std::string& name, int i1, int i2, int i3) const
{
GLCall(glUniform3i(getUniformLocation(name), i1, i2, i3));
}

void Shader::setUniform(const std::string& name, int i1, int i2)
void Shader::setUniform(const std::string& name, int i1, int i2) const
{
GLCall(glUniform2i(getUniformLocation(name), i1, i2));
}

void Shader::setUniform(const std::string& name, int i1)
void Shader::setUniform(const std::string& name, int i1) const
{
GLCall(glUniform1i(getUniformLocation(name), i1));
}

unsigned Shader::getUniformLocation(const std::string& name)
unsigned Shader::getUniformLocation(const std::string& name) const
{
if (mUniformLocationCache.find(name) != mUniformLocationCache.end())
{
Expand Down
21 changes: 11 additions & 10 deletions AimGL/src/Renderer3D/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ class Shader
*/
void unbind() const;

void setUniform(const std::string& name, float f1, float f2, float f3, float f4);
void setUniform(const std::string& name, float f1, float f2, float f3);
void setUniform(const std::string& name, float f1, float f2);
void setUniform(const std::string& name, float f1);
void setUniform(const std::string& name, int i1, int i2, int i3, int i4);
void setUniform(const std::string& name, int i1, int i2, int i3);
void setUniform(const std::string& name, int i1, int i2);
void setUniform(const std::string& name, int i1);
void setUniform(const std::string& name, glm::mat4 mat) const;
void setUniform(const std::string& name, float f1, float f2, float f3, float f4) const;
void setUniform(const std::string& name, float f1, float f2, float f3) const;
void setUniform(const std::string& name, float f1, float f2) const;
void setUniform(const std::string& name, float f1) const;
void setUniform(const std::string& name, int i1, int i2, int i3, int i4) const;
void setUniform(const std::string& name, int i1, int i2, int i3) const;
void setUniform(const std::string& name, int i1, int i2) const;
void setUniform(const std::string& name, int i1) const;


private:
Expand Down Expand Up @@ -113,7 +114,7 @@ class Shader
* \param name Name of uniform
* \return Location, or -1 in case of failure
*/
unsigned getUniformLocation(const std::string& name);
unsigned getUniformLocation(const std::string& name) const;

/**
* \brief Creates a shader based on shader types and source code
Expand All @@ -138,7 +139,7 @@ class Shader

private:
unsigned int mRendererId;
std::unordered_map<std::string, int> mUniformLocationCache;
mutable std::unordered_map<std::string, int> mUniformLocationCache;
};

/**
Expand Down
7 changes: 7 additions & 0 deletions AimGL/src/States/CustomStates/ExitGameState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "ExitGameState.h"
#include "pch.h"

ExitGameState::ExitGameState(StateStack& stack)
: State(stack)
{
}
14 changes: 14 additions & 0 deletions AimGL/src/States/CustomStates/ExitGameState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "States/State.h"

class StateStack;

/**
* @brief The state in which the player wants to close the game
*/
class ExitGameState : public State
{
public:
explicit ExitGameState(StateStack& stack);
};
69 changes: 69 additions & 0 deletions AimGL/src/States/CustomStates/LogoState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "LogoState.h"
#include "States/StateStack.h"
#include "pch.h"

LogoState::LogoState(StateStack& stack, sf::Window& window)
: State(stack)
, mWindow(window)
, mLogoTexture("resources/Textures/logo.png")
, mLogo(mLogoTexture)
, mRenderer3D(window)
, mPhase(Phase::FadeIn)
{
mClock.restart();
mLogo.setPosition(glm::vec2(mWindow.getSize().x / 2.f, mWindow.getSize().y / 2.f),
Sprite2D::Origin::Center);
mLogo.setHeight(mWindow.getSize().y);
mLogo.setOpacity(0);
}

void LogoState::draw(sf::Window& target) const
{
MTR_SCOPE("SampleState", "SampleState::draw");
mLogo.draw(mRenderer3D);
}

bool LogoState::fixedUpdate(const float& deltaTime)
{
MTR_SCOPE("SampleState", "SampleState::fixedUpdate");
return true;
}

bool LogoState::update(const float& deltaTime)
{
MTR_SCOPE("SampleState", "SampleState::update");
switch (mPhase)
{
case Phase::FadeIn:
mLogo.setOpacity((mClock.getElapsedTime().asSeconds() / 2.f) - 0.2f);
if (mClock.getElapsedTime().asSeconds() > 3.f)
{
mPhase = Phase::Fadeout;
mClock.restart();
}
break;

case Phase::Fadeout:
mLogo.setOpacity(2.f - mClock.getElapsedTime().asSeconds() / 2.f);
if (mClock.getElapsedTime().asSeconds() > 4.f)
{
requestPop();
requestPush(State_ID::ExitGameState);
}
break;
}
return true;
}

bool LogoState::handleEvent(const sf::Event& event)
{
MTR_SCOPE("SampleState", "SampleState::handleEvent");
return true;
}

bool LogoState::updateImGui(const float& deltaTime)
{
MTR_SCOPE("SampleState", "SampleState::updateImGui");
ImGui::ShowDemoWindow();
return true;
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
#pragma once

#include "Renderer3D/BufferLayout.h"
#include "Renderer3D/IndexBuffer.h"
#include "Renderer3D/Renderer3D.h"
#include "Renderer3D/Shader.h"
#include "Renderer3D/Texture.h"
#include "Renderer3D/VertexArray.h"
#include "Renderer3D/Sprite2D.h"
#include "States/State.h"

class StateStack;

/**
* \brief ....
* \brief Game state drawing the game logo
*/
class SampleState : public State
class LogoState : public State
{
public:
SampleState(StateStack& stack, sf::Window& window);
LogoState(StateStack& stack, sf::Window& window);

/**
* \brief Draws only this state to the passed target
Expand Down Expand Up @@ -50,11 +46,22 @@ class SampleState : public State
bool updateImGui(const float& deltaTime) override;

private:
Renderer3D render3D;
Texture mLogo;
VertexArray mVao;
VertexBuffer mVbo;
IndexBuffer mEbo;
BufferLayout mBufferLayout;
Shader mShader;
/**
* \brief This state is divided into two states:
* the state in which the logo fades in and
* the state in which the logo fades out.
*/
enum class Phase
{
FadeIn,
Fadeout
};

private:
Renderer3D mRenderer3D;
Texture mLogoTexture;
Sprite2D mLogo;
sf::Clock mClock;
sf::Window& mWindow;
Phase mPhase;
};
56 changes: 0 additions & 56 deletions AimGL/src/States/CustomStates/SampleState.cpp

This file was deleted.

Loading

0 comments on commit babe040

Please sign in to comment.