From 1578c91fb403a57a15a4e83449087fa58d8284ae Mon Sep 17 00:00:00 2001 From: ItsAxel353 Date: Sat, 6 Jun 2026 12:20:12 +0200 Subject: [PATCH] created draw text featur (not work) --- CMakeLists.txt | 4 +++- game/main.cpp | 11 ++++----- include/Engine.hpp | 3 ++- include/core/text/DrawText.hpp | 30 ++++++++++++++++++++++++ src/core/text/DrawText.cpp | 43 ++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 include/core/text/DrawText.hpp create mode 100644 src/core/text/DrawText.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c46dff..fb1df19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,9 @@ endif() file(GLOB_RECURSE ENGINE_SOURCES "src/*.cpp") # 3. On crée la bibliothèque BlueEngine -add_library(BlueEngine STATIC ${ENGINE_SOURCES}) +add_library(BlueEngine STATIC ${ENGINE_SOURCES} + src/core/text/DrawText.cpp + include/core/text/DrawText.hpp) # 4. On indique où sont les headers publics du moteur target_include_directories(BlueEngine PUBLIC include) diff --git a/game/main.cpp b/game/main.cpp index 1a2d1a2..d4bebbb 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -7,13 +7,12 @@ int main() { while (engine.running()) { engine.HandleEvents(); - engine.update(0.016f); // Assuming a fixed time step of 16ms (60 FPS) - engine.clean(); - - DrawText text("Hello, BlueEngine!", sf::Font(), 24); - text.setColor(sf::Color::White); - engine.render(text); + Blue::Engine::update(0.016f); // Assuming a fixed time step of 16ms (60 FPS) + DrawText myText("Hello from BlueEngine"); + myText.setColor(DrawText::Color::WHITE); + engine.render(myText); + engine.clean(); engine.getWindow().display(); } return 0; diff --git a/include/Engine.hpp b/include/Engine.hpp index 48a8b82..a295548 100644 --- a/include/Engine.hpp +++ b/include/Engine.hpp @@ -7,11 +7,12 @@ namespace Blue { public: void createWindow(const char* title, int width, int height); void HandleEvents(); + static void update(float deltaTime); void render(const sf::Drawable& drawable); void clean(); - bool running() { return isRunning; } + bool running() const { return isRunning; } sf::RenderWindow& getWindow() { return m_window; } private: diff --git a/include/core/text/DrawText.hpp b/include/core/text/DrawText.hpp new file mode 100644 index 0000000..594c3b4 --- /dev/null +++ b/include/core/text/DrawText.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include + +#include "Engine.hpp" + +class DrawText : public sf::Drawable { +private: + sf::Text text; + sf::Font font; + + static sf::Font defaultFont; + +public: + enum class Color { RED, WHITE }; + + static bool initDefaultFont(); + + DrawText(const sf::String &string); + + void setPosition(float x, float y); + + void setCustomFont(sf::Font &font); + + void setColor(Color color); + +protected: + // 2. Le contrat obligatoire avec SFML. Ne change pas un seul mot de cette ligne ! + void draw(sf::RenderTarget& target, sf::RenderStates states) const override; +}; diff --git a/src/core/text/DrawText.cpp b/src/core/text/DrawText.cpp new file mode 100644 index 0000000..21dd2f0 --- /dev/null +++ b/src/core/text/DrawText.cpp @@ -0,0 +1,43 @@ +#include "core/text/DrawText.hpp" +#include "Engine.hpp" + +#include +#include + +sf::Font DrawText::defaultFont; + +bool DrawText::initDefaultFont() { + if (!defaultFont.loadFromFile("assets/fonts/arial.ttf")) { + std::cerr << "Error loading default font" << std::endl; + return false; + } + return true; +} + +DrawText::DrawText(const sf::String &string) { + text.setString(string); + text.setFont(defaultFont); +} + +void DrawText::setColor(Color color) { + switch (color) { + case Color::RED: + text.setFillColor(sf::Color::Red); + break; + case Color::WHITE: + text.setFillColor(sf::Color::White); + break; + } +} + +void DrawText::setPosition(float x, float y) { + text.setPosition(x, y); +} + +void DrawText::setCustomFont(sf::Font &font) { + text.setFont(font); +} + +void DrawText::draw(sf::RenderTarget &target, sf::RenderStates states) const { + target.draw(text); +}