Pre-rendering layers template

This commit is contained in:
JackCarterSmith 2024-09-17 17:31:51 +02:00
parent d3f28ab24c
commit 098cded831
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
5 changed files with 162 additions and 25 deletions

View File

@ -0,0 +1,76 @@
#include "UI.hpp"
#include <stdexcept>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/BlendMode.hpp>
#include <SFML/Graphics/RenderStates.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/CircleShape.hpp>
void UI::DrawUIOnRenderWindow(std::shared_ptr<sf::RenderWindow> context) {
sf::Sprite spriteRender;
spriteRender.setTexture(this->mUIRender.getTexture());
spriteRender.setPosition(0.0f, 0.0f);
spriteRender.setScale(1.0f, 1.0f);
context->draw(spriteRender);
}
CockpitUI::CockpitUI() : UI() {
sf::ContextSettings sViewSettings;
sViewSettings.depthBits = 0;
sViewSettings.stencilBits = 0;
sViewSettings.antialiasingLevel = 8;
sViewSettings.sRgbCapable = true;
if (!mUIRender.create(1280, 720, sViewSettings))
throw std::runtime_error("Texture renderer creation failure");
mUIRender.setSmooth(true);
mUIRender.setRepeated(false);
mUIRender.clear(sf::Color::Magenta);
sf::RectangleShape rec1(sf::Vector2f(780,328));
rec1.setPosition(sf::Vector2f(75,40));
rec1.setFillColor(sf::Color::Transparent);
sf::BlendMode sBM = sf::BlendNone;
sf::RenderStates sRS(sBM);
mUIRender.draw(rec1, sRS);
sf::CircleShape radar(190, 60);
radar.setPosition(sf::Vector2f((1280-(2*190)-20),60));
radar.setFillColor(sf::Color::Yellow);
mUIRender.draw(radar, sRS);
mUIRender.display();
}
void CockpitUI::Update() {
}
WorldUI::WorldUI() : UI() {
sf::ContextSettings sViewSettings;
sViewSettings.depthBits = 0;
sViewSettings.stencilBits = 0;
sViewSettings.antialiasingLevel = 8;
sViewSettings.sRgbCapable = true;
if (!mUIRender.create(1280, 560, sViewSettings)) // Only the upper of the screen is renderer as the UI hide whats left.
throw std::runtime_error("Texture renderer creation failure");
mUIRender.setSmooth(true);
mUIRender.setRepeated(false);
mUIRender.clear(sf::Color::Black);
mUIRender.display();
}
void WorldUI::Update() {
}

53
Engine/Graphics/UI.hpp Normal file
View File

@ -0,0 +1,53 @@
#pragma once
#include <memory>
#include <SFML/Graphics/RenderTexture.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
class UI {
public:
virtual ~UI() = 0;
virtual void Update() = 0;
virtual inline void Draw(std::shared_ptr<sf::RenderWindow> c) { DrawUIOnRenderWindow(c); }
protected:
sf::RenderTexture mUIRender; // This is used to create the scene
void DrawUIOnRenderWindow(std::shared_ptr<sf::RenderWindow> context);
};
inline UI::~UI() {}
class CockpitUI : public UI {
public:
CockpitUI();
~CockpitUI() {}
CockpitUI(CockpitUI&&) = default;
CockpitUI& operator= (CockpitUI&&) = default;
CockpitUI(CockpitUI const&) = delete;
CockpitUI& operator= (CockpitUI const&) = delete;
void Update() override;
//void Draw(std::shared_ptr<sf::RenderWindow>) override;
};
class WorldUI : public UI {
public:
WorldUI();
~WorldUI() {}
WorldUI(WorldUI&&) = default;
WorldUI& operator= (WorldUI&&) = default;
WorldUI(WorldUI const&) = delete;
WorldUI& operator= (WorldUI const&) = delete;
void Update() override;
//void Draw(std::shared_ptr<sf::RenderWindow>) override;
};

View File

@ -15,12 +15,16 @@ using std::make_unique;
#define TARGET_FPS (1000/60) #define TARGET_FPS (1000/60)
std::unique_ptr<SysTimer> mPerfsTimer; std::unique_ptr<SysTimer> mPerfsTimer = nullptr;
Game::Game(bool dbg) : gbDbgModeEnabled(dbg) { Game::Game(bool dbg) : mbDbgModeEnabled(dbg) {
InitWindows(); InitWindows();
if (gbDbgModeEnabled) {
gDbgUI = make_unique<DebugUI>(); mCockpitUI = make_unique<CockpitUI>();
mWorldUI = make_unique<WorldUI>();
if (mbDbgModeEnabled) {
mDbgUI = make_unique<DebugUI>();
mPerfsTimer = make_unique<SysTimer>(); mPerfsTimer = make_unique<SysTimer>();
mPerfsTimer->Reset(); mPerfsTimer->Reset();
} }
@ -34,40 +38,40 @@ void Game::InitWindows() {
sWindowSettings.antialiasingLevel = 8; sWindowSettings.antialiasingLevel = 8;
sWindowSettings.sRgbCapable = true; sWindowSettings.sRgbCapable = true;
gMainWindow = make_shared<sf::RenderWindow>( mMainWindow = make_shared<sf::RenderWindow>(
sf::VideoMode(1080, 720), "ProtoTank", sf::VideoMode(1280, 720), "ProtoTank",
(sf::Style::Close | sf::Style::Titlebar), (sf::Style::Close | sf::Style::Titlebar),
sWindowSettings sWindowSettings
); );
gMainWindow->setVerticalSyncEnabled(false); mMainWindow->setVerticalSyncEnabled(false); // Never use simultaneously with framerate limiter
gMainWindow->setFramerateLimit(60); mMainWindow->setFramerateLimit(60);
gMainWindow->setKeyRepeatEnabled(false); mMainWindow->setKeyRepeatEnabled(false);
gMainWindow->setMouseCursorVisible(true); mMainWindow->setMouseCursorVisible(true);
} }
GAME_STATUS Game::Tick(SysTimer& time) { GAME_STATUS Game::Tick(SysTimer& time) {
static std::vector<double> msTicksMonitoring; static std::vector<double> msTicksMonitoring;
static double mPrevdelta = 0; static double mPrevdelta = 0;
static int mFrameCnt = 0; static unsigned int mFrameCnt = 0;
double mDelta = time.GetDeltaTime(); double mDelta = time.GetDeltaTime();
if (gDbgUI != nullptr && mPerfsTimer != nullptr) { if (mDbgUI != nullptr && mPerfsTimer != nullptr) {
msTicksMonitoring.push_back((mDelta - mPrevdelta) / TARGET_FPS); msTicksMonitoring.push_back((mDelta - mPrevdelta) / TARGET_FPS);
if (mPerfsTimer->GetDeltaTime() >= 1000) { // Re-evaluate CPU usage every seconds (for FP'S') if (mPerfsTimer->GetDeltaTime() >= 1000) { // Re-evaluate CPU usage every seconds (for FP'S')
double cpuUsage = std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), 0) / msTicksMonitoring.size(); double cpuUsage = std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), 0) / msTicksMonitoring.size();
gDbgUI->UpdateDebugData(cpuUsage, (mDelta - mPrevdelta), mFrameCnt, (PerfsGetVirtMem() / 1000), (PerfsGetPhysMem() / 1000)); mDbgUI->UpdateDebugData(cpuUsage, (mDelta - mPrevdelta), mFrameCnt, (PerfsGetVirtMem() / 1000), (PerfsGetPhysMem() / 1000));
mFrameCnt = -1; // Minus one to compensate the direct next frame mFrameCnt = 0;
msTicksMonitoring.clear(); msTicksMonitoring.clear();
mPerfsTimer->Reset(); mPerfsTimer->Reset();
} }
} }
sf::Event event; sf::Event event;
while (gMainWindow->pollEvent(event)) { // Wait for next frame (fixed FPS) while (mMainWindow->pollEvent(event)) { // Wait for next frame (fixed FPS)
if (event.type == sf::Event::Closed) if (event.type == sf::Event::Closed)
gMainWindow->close(); mMainWindow->close();
} }
if (mDelta >= TARGET_FPS) { //TODO: Replace with global var and use dynamic window loader if (mDelta >= TARGET_FPS) { //TODO: Replace with global var and use dynamic window loader
@ -83,7 +87,7 @@ GAME_STATUS Game::Tick(SysTimer& time) {
sf::sleep(sf::milliseconds(TARGET_FPS - (mDelta - mPrevdelta))); sf::sleep(sf::milliseconds(TARGET_FPS - (mDelta - mPrevdelta)));
if (gMainWindow->isOpen()) if (mMainWindow->isOpen())
return GAME_RUNNING; return GAME_RUNNING;
else else
return GAME_QUIT; return GAME_QUIT;
@ -95,15 +99,15 @@ void Game::Update() {
void Game::Render() { void Game::Render() {
// Clear the draw buffer // Clear the draw buffer
gMainWindow->clear(); mMainWindow->clear(sf::Color::Black);
// Draw the arena view // Draw the arena view
//3DRenderer::draw() mWorldUI->Draw(mMainWindow);
// Draw the UI above // Draw the UI above
//UI::draw() mCockpitUI->Draw(mMainWindow);
// Draw the debug informations if enabled // Draw the debug informations if enabled
if (gDbgUI != nullptr) gDbgUI->DrawDebugData(gMainWindow); if (mDbgUI != nullptr) mDbgUI->DrawDebugData(mMainWindow);
// Present the draw buffer // Present the draw buffer
gMainWindow->display(); mMainWindow->display();
} }

View File

@ -4,6 +4,7 @@
#include <memory> #include <memory>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include "Engine/Graphics/UI.hpp"
#include "Engine/Graphics/DebugUI.hpp" #include "Engine/Graphics/DebugUI.hpp"
#include "Engine/Utils/Timers.hpp" #include "Engine/Utils/Timers.hpp"
@ -31,9 +32,11 @@ private:
void Update(); void Update();
void Render(); void Render();
bool gbDbgModeEnabled; bool mbDbgModeEnabled;
std::unique_ptr<DebugUI> mDbgUI = nullptr;
std::shared_ptr<sf::RenderWindow> gMainWindow = nullptr; std::shared_ptr<sf::RenderWindow> mMainWindow = nullptr;
std::unique_ptr<DebugUI> gDbgUI = nullptr; std::unique_ptr<CockpitUI> mCockpitUI = nullptr;
std::unique_ptr<WorldUI> mWorldUI = nullptr;
}; };

View File

@ -26,6 +26,7 @@ set(GRAPHS_SCRS
Engine/Graphics/3DGraphics.cpp Engine/Graphics/3DGraphics.cpp
Engine/Graphics/Camera.cpp Engine/Graphics/Camera.cpp
Engine/Graphics/UI.cpp Engine/Graphics/UI.cpp
Engine/Graphics/UI.hpp
Engine/Graphics/DebugUI.cpp Engine/Graphics/DebugUI.cpp
Engine/Graphics/DebugUI.hpp Engine/Graphics/DebugUI.hpp
) )