Skip to content

Commit

Permalink
Get rid of SFML Graphics module in code
Browse files Browse the repository at this point in the history
The project does not allow the use of the SFML graphics library.
To that end, it has been removed from the code, and the only
reason it is still built and plugged in is for debug purposes,
that is, to display ImGui. This is a temporary solution.
  • Loading branch information
Notiooo committed Oct 30, 2023
1 parent 9291b4b commit a7ac16f
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 66 deletions.
3 changes: 2 additions & 1 deletion AimGL/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
out/
out/
.vshistory/
26 changes: 17 additions & 9 deletions AimGL/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@ Game::Game()
settings.depthBits = 24;
settings.stencilBits = 8;

mGameWindow =
std::make_unique<sf::RenderWindow>(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "AimGL",
sf::Style::Titlebar | sf::Style::Close, settings);
// settings.attributeFlags = sf::ContextSettings::Core;

// If Core should be used, then there is
// a need to change the way ImGui is plugged in. For the moment ImGui-SFML uses the SFML
// graphics module - so ImGui will not draw on Core setting. This project is developed without
// the SFML graphics module and works without it. In the future, it would be useful to plug in
// ImGui in such a way that it no longer relies on the SFML graphics module.

mGameWindow = std::make_unique<sf::Window>(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "AimGL",
sf::Style::Titlebar | sf::Style::Close, settings);

mGameWindow->setFramerateLimit(FRAMES_PER_SECOND);
mGameWindow->setActive(true);
loadResources();

#ifdef _DEBUG
ImGui::SFML::Init(*mGameWindow);
ImGui::SFML::Init(*mGameWindow, sf::Vector2f(mGameWindow->getSize()), true);
#endif

// GLEW setup
Expand Down Expand Up @@ -72,6 +79,7 @@ void Game::run()

void Game::performGameLoop()
{
MTR_SCOPE("Game", "Game::performGameLoop");
sf::Clock clock;
auto frameTimeElapsed = sf::Time::Zero;
mFixedUpdateClock.restart();
Expand All @@ -80,7 +88,8 @@ void Game::performGameLoop()
MTR_SCOPE("Game", "GameLoop");
frameTimeElapsed = clock.restart();
#ifdef _DEBUG
ImGui::SFML::Update(*mGameWindow, frameTimeElapsed);
ImGui::SFML::Update(sf::Mouse::getPosition(*mGameWindow),
sf::Vector2f(mGameWindow->getSize()), frameTimeElapsed);
#endif
update(frameTimeElapsed);
fixedUpdateAtEqualIntervals();
Expand Down Expand Up @@ -138,6 +147,7 @@ void Game::update(const sf::Time& deltaTime)
MTR_SCOPE("Game", "Game::update");
auto deltaTimeInSeconds = deltaTime.asSeconds();

ImGui::ShowDemoWindow();
mAppStack.update(deltaTimeInSeconds);

if (mAppStack.top() == State_ID::ExitGameState)
Expand All @@ -153,12 +163,10 @@ void Game::render()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// draw the application
mAppStack.draw(*mGameWindow, sf::Transform::Identity);
mAppStack.draw(*mGameWindow);

#ifdef _DEBUG
mGameWindow->pushGLStates();
ImGui::SFML::Render(*mGameWindow);
mGameWindow->popGLStates();
ImGui::SFML::Render();
#endif

// display to the window
Expand Down
4 changes: 2 additions & 2 deletions AimGL/src/Game.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window.hpp>

#include "Resources/Resources.h"
#include "States/StateStack.h"
Expand Down Expand Up @@ -115,7 +115,7 @@ class Game
/**
* @brief The window to which the game image should be drawn.
*/
std::unique_ptr<sf::RenderWindow> mGameWindow;
std::unique_ptr<sf::Window> mGameWindow;

bool isGameRunning = true;

Expand Down
6 changes: 6 additions & 0 deletions AimGL/src/Renderer3D/Font.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once


class Font
{
};
11 changes: 3 additions & 8 deletions AimGL/src/Renderer3D/Renderer3D.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#include "Renderer3D.h"
#include "pch.h"

#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/RenderStates.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Shader.hpp>

void GLClearError()
{
while (glGetError() /* != GL_NO_ERROR*/)
Expand All @@ -25,16 +20,16 @@ bool GLLogCall(const char* function, const char* file, int line)
return true;
}

void Renderer3D::draw(const VertexArray& va, const IndexBuffer& ib, const sf::Shader& shader,
void Renderer3D::draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const DrawMode& drawMode) const
{
sf::Shader::bind(&shader);
// shader.bind();
va.bind();
ib.bind();
GLCall(glDrawElements(toOpenGL(drawMode), ib.size(), GL_UNSIGNED_INT, nullptr));

#ifdef _DEBUG
sf::Shader::bind(nullptr);
// shader.bind();
va.unbind();
ib.unbind();
#endif
Expand Down
3 changes: 2 additions & 1 deletion AimGL/src/Renderer3D/Renderer3D.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Renderer3D/IndexBuffer.h"
#include "Renderer3D/Shader.h"
#include "Renderer3D/VertexArray.h"

/**
Expand Down Expand Up @@ -58,7 +59,7 @@ class Renderer3D
* @param ib Specifies the drawing order of the VertexArray.
* @param shader Shader telling how to draw data.
*/
void draw(const VertexArray& va, const IndexBuffer& ib, const sf::Shader& shader,
void draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const DrawMode& drawMode = DrawMode::Triangles) const;

private:
Expand Down
5 changes: 5 additions & 0 deletions AimGL/src/Renderer3D/Shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

class Shader
{
};
6 changes: 6 additions & 0 deletions AimGL/src/Renderer3D/Texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once


class Texture
{
};
9 changes: 4 additions & 5 deletions AimGL/src/Resources/Resources.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once

#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Texture.hpp>

#include "Resources/ResourceManager.h"
#include <Renderer3D/Font.h>
#include <Renderer3D/Texture.h>


class Player;
Expand All @@ -19,7 +18,7 @@ enum class TextureManagerId
/**
* \brief Object storing textures of the game
*/
using TextureManager = ResourceManager<sf::Texture, TextureManagerId>;
using TextureManager = ResourceManager<Texture, TextureManagerId>;

// ====== Fonts ======= //

Expand All @@ -34,7 +33,7 @@ enum class FontId
/**
* \brief Object storing fonts of the game
*/
using FontManager = ResourceManager<sf::Font, FontId>;
using FontManager = ResourceManager<Font, FontId>;

/**
* @brief Any game assets from textures or fonts
Expand Down
2 changes: 1 addition & 1 deletion AimGL/src/States/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ State::State(StateStack& stack)
{
}

void State::draw(sf::RenderTarget& target, sf::RenderStates states) const
void State::draw(sf::Window& target) const
{
}

Expand Down
6 changes: 2 additions & 4 deletions AimGL/src/States/State.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef STATE_H
#define STATE_H
#include <SFML/Graphics/RenderStates.hpp>
#include <SFML/Graphics/RenderTarget.hpp>

#include <memory>

#include "States.h"
Expand All @@ -28,9 +27,8 @@ class State
/**
* \brief Draws only this state to the passed target
* \param target where it should be drawn to
* \param states provides information about rendering process (transform, shader, blend mode)
*/
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
virtual void draw(sf::Window& target) const;

/**
* \brief Updates the state logic at equal intervals independent of the frame rate.
Expand Down
4 changes: 2 additions & 2 deletions AimGL/src/States/StateStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ void StateStack::update(const float& deltaTime)
}
}

void StateStack::draw(sf::RenderTarget& target, sf::RenderStates states) const
void StateStack::draw(sf::Window& target) const
{
// Drawing starts from the lowest state to the highest state
for (const auto& entry: mStack)
{
entry.state->draw(target, states);
entry.state->draw(target);
}
}

Expand Down
6 changes: 2 additions & 4 deletions AimGL/src/States/StateStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <map>
#include <vector>

#include "SFML/Graphics/RenderStates.hpp"
#include "SFML/System/NonCopyable.hpp"
#include "State.h"
#include "States.h"
Expand Down Expand Up @@ -95,15 +94,14 @@ class StateStack : sf::NonCopyable
void update(const float& deltaTime);

/**
* \brief Draws the states in the stack to the given target with given states.
* \brief Draws the states in the stack to the given target.
* \param target where drawable object should be drawn to.
* \param states provides information about rendering process (transform, shader, blend mode)
*
* Draws the states at the top of the stack. If the state is transparent (returns true)
* then it also draw the state below it. The state below it is also checked for
* transparency and the process repeats itself.
*/
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
void draw(sf::Window& target) const;

/**
* \brief It takes input (event) from the user and sends it to the states on the stack.
Expand Down
2 changes: 1 addition & 1 deletion AimGL/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdlib.h>

#if defined(_DEBUG)
// #define MTR_ENABLED // Uncomment to enable MiniTrace
#undef MTR_ENABLED// Comment to enable MiniTrace
#endif

int main()
Expand Down
7 changes: 1 addition & 6 deletions AimGL/src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@
#include <imgui.h>

// SFML
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Shader.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/Window.hpp>
#include <SFML/Window/Event.hpp>

// Logging
Expand Down
6 changes: 3 additions & 3 deletions AimGL/tests/ut/src/States/StateStackTest.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "States/StateStack.h"
#include "States/States.h"
#include "TestUtils/SFML/EventEqualityOperator.h"
#include "TestUtils/SFML/Stubs/RenderTargetStub.h"
#include "TestUtils/SFML/Stubs/WindowStub.h"
#include "gtest/gtest.h"
#include <States/MockState.h>

Expand Down Expand Up @@ -182,10 +182,10 @@ TEST(StateStackTest, StateStackDrawIsCalled)
{
UglyTestableStateStack s;
auto& mock = s.forcePushStateAndReturn<MockState>(State_ID::GameState);
RenderTargetStub renderTargetStub;
WindowStub windowStub;
sf::RenderStates renderStates;
EXPECT_CALL(mock, draw(testing::_, testing::_));
s.draw(renderTargetStub, renderStates);
s.draw(windowStub);
}

TEST(StateStackTest, StateStackHandleEventBlockingStateBelow)
Expand Down
2 changes: 1 addition & 1 deletion AimGL/tests/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(Utils_Sources
src/TestUtils/SFML/EventEqualityOperator.cpp
src/TestUtils/SFML/Stubs/RenderTargetStub.cpp
src/TestUtils/SFML/Stubs/WindowStub.cpp
)

This file was deleted.

9 changes: 0 additions & 9 deletions AimGL/tests/utils/src/TestUtils/SFML/Stubs/RenderTargetStub.h

This file was deleted.

6 changes: 6 additions & 0 deletions AimGL/tests/utils/src/TestUtils/SFML/Stubs/WindowStub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "WindowStub.h"

sf::Vector2u WindowStub::getSize() const
{
return sf::Vector2u(15, 15);
}
9 changes: 9 additions & 0 deletions AimGL/tests/utils/src/TestUtils/SFML/Stubs/WindowStub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <SFML/Window.hpp>

class WindowStub : public sf::Window
{
public:
sf::Vector2u getSize() const;
};
2 changes: 1 addition & 1 deletion AimGL/vendor/imgui-sfml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ message(STATUS "Fetching ImGui-SFML...")
FetchContent_Declare(
imgui-sfml
GIT_REPOSITORY https://github.com/eliasdaler/imgui-sfml
GIT_TAG a80d9547832c57a149cda452ed9456def5bb6328
GIT_TAG d75897c5170c878c941ca22b132e724829341a90
)

set(IMGUI_DIR ${imgui_SOURCE_DIR})
Expand Down
2 changes: 1 addition & 1 deletion AimGL/vendor/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ message(STATUS "Fetching ImGui...")
FetchContent_Declare(
imgui
GIT_REPOSITORY https://github.com/ocornut/imgui
GIT_TAG e99c4fc6688e218a0e5da50f56638aebab45da9b
GIT_TAG c6e0284ac58b3f205c95365478888f7b53b077e2 # release v1.89.9
)
FetchContent_MakeAvailable(imgui)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
I spent most of my life playing shooters of all kinds. Believe me or not, but what mattered most to me was shooting skills. I am a fan of one-tap (one shot - one frag). Anyone can win a 1 vs 5 clutch, but not everyone can do it by one-tapping the opponent. This cannot be learned without training. That's why this tool was created.

### Project
The game will give several possible training scenarios. However, I will describe it better when I finish the game :3
- It is developed without using the SFML graphics module (pure OpenGL). SFML is only used to create a window, take inputs or play sounds. Well... and still temporarily SFML graphics module is linked to display ImGui for debugging. This will be changed later.

0 comments on commit a7ac16f

Please sign in to comment.