Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions game/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion include/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 30 additions & 0 deletions include/core/text/DrawText.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <SFML/Graphics/Text.hpp>

#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;
};
43 changes: 43 additions & 0 deletions src/core/text/DrawText.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "core/text/DrawText.hpp"
#include "Engine.hpp"

#include <iostream>
#include <ostream>

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);
}