Skip to content

Commit

Permalink
CLIENT-GAME: Add raylib encapsulation
Browse files Browse the repository at this point in the history
MINOR
  • Loading branch information
TTENSHII committed Sep 27, 2023
1 parent 5df5008 commit b4ca35b
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 83 deletions.
20 changes: 1 addition & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,7 @@ install(TARGETS ${PROJECT_NAME_SERVER}

add_subdirectory(deps)

# Clang-Tidy

find_program(CLANG_TIDY_EXE NAMES "clang-tidy")

if(CLANG_TIDY_EXE)
if (MSVC)
set_target_properties(${PROJECT_NAME_CLIENT} PROPERTIES
VS_GLOBAL_RunCodeAnalysis true
VS_GLOBAL_EnableClangTidyCodeAnalysis true
)
set_target_properties(${PROJECT_NAME_SERVER} PROPERTIES
VS_GLOBAL_RunCodeAnalysis true
VS_GLOBAL_EnableClangTidyCodeAnalysis true
)
else()
set(CLANG_TIDY_COMMAND "${CLANG_TIDY_EXE}" "--fix" "--fix-notes" "--fix-errors")
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
endif()
endif()


if(MSVC)
target_compile_options(
Expand Down
74 changes: 74 additions & 0 deletions src/Client/Raylib/Graphics/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ namespace Raylib {
return GetMonitorRefreshRate(monitor);
}

int getCurrentMonitor()
{
return GetCurrentMonitor();
}

void setClipboardText(std::string text)
{
SetClipboardText(text.c_str());
Expand Down Expand Up @@ -611,4 +616,73 @@ namespace Raylib {
return MeasureText(text.c_str(), fontSize);
}

Text::Text(std::string text, Vector2 position, float fontSize, Color color)
: _text(text), _position(position), _fontSize(fontSize), _color(color), _pixelPosition(position)
{
}

void Text::draw()
{
DrawText(
_text.c_str(),
static_cast<int>(_pixelPosition.X()),
static_cast<int>(_pixelPosition.Y()),
static_cast<int>(_fontSize),
{_color.R(), _color.G(), _color.B(), _color.A()});
}

void Text::drawEx(float spacing)
{
DrawTextEx(
GetFontDefault(),
_text.c_str(),
{_pixelPosition.X(), _pixelPosition.Y()},
_fontSize,
spacing,
{_color.R(), _color.G(), _color.B(), _color.A()});
}

void Text::drawPro(Vector2 origin, float rotation, float spacing)
{
DrawTextPro(
GetFontDefault(),
_text.c_str(),
{_pixelPosition.X(), _pixelPosition.Y()},
{origin.X(), origin.Y()},
rotation,
_fontSize,
spacing,
{_color.R(), _color.G(), _color.B(), _color.A()});
}

float Text::x() const
{
return _position.X();
}

float Text::y() const
{
return _position.Y();
}

float Text::getFontSize() const
{
return _fontSize;
}

void Text::setFontSize(float fontSize)
{
_fontSize = fontSize;
}

Vector2 Text::getPosition() const
{
return _position;
}

void Text::setPixelPosition(Vector2 position)
{
_pixelPosition = position;
}

} // namespace Raylib
34 changes: 28 additions & 6 deletions src/Client/Raylib/Graphics/Graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Raylib {
int getMonitorWidth(int monitor);
int getMonitorHeight(int monitor);
int getMonitorRefreshRate(int monitor);
int getCurrentMonitor();
void setClipboardText(std::string);
std::string getClipboardText();
void setWindowIcon(Image image);
Expand Down Expand Up @@ -96,12 +97,6 @@ namespace Raylib {
Color colorFromNormalized(Vector4 normalized);
Color getColor(unsigned int hexValue);

// Text functions
void drawFPS(int posX, int posY);
void
drawText(std::string text, int posX, int posY, int fontSize, Color color);
int measureText(const std::string text, int fontSize);

// Graphic classes

class Image {
Expand Down Expand Up @@ -156,4 +151,31 @@ namespace Raylib {
float _height;
};

// Text functions and classes

class Text {
public:
Text(std::string text, Vector2 position = {0, 0}, float fontSize = 5.0F, Color color = BLACK);
void draw();
void drawEx(float spacing);
void drawPro(Vector2 origin, float rotation, float spacing);

float x() const;
float y() const;
float getFontSize() const;
void setFontSize(float fontSize);
Vector2 getPosition() const;
void setPixelPosition(Vector2 position);

private:
std::string _text;
float _fontSize;
Color _color;
Vector2 _position;
Vector2 _pixelPosition;
};

void drawFPS(int posX, int posY);


} // namespace Raylib
36 changes: 13 additions & 23 deletions src/Client/Systems/ClientSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,42 +204,32 @@ namespace Systems {
}
}

static void drawTextResponsive(Types::Position &position, Types::Text &text)
static void drawTextResponsive(Raylib::Text &text)
{
const float denominator = 100.0;

float x =
(position.x * static_cast<float>(GetScreenWidth())) / denominator;
(text.x() * static_cast<float>(GetScreenWidth())) / denominator;
float y =
(position.y * static_cast<float>(GetScreenHeight())) / denominator;
(text.y() * static_cast<float>(GetScreenHeight())) / denominator;

float fsz = (text.fontSize * static_cast<float>(GetScreenWidth()))
/ denominator;

DrawTextEx(
text.font,
text.text.data(),
Vector2(x, y),
fsz,
0,
text.color);

text.setPixelPosition({x, y});
text.draw();
std::cout << "text drawn at " << x << " " << y << std::endl;
}

void GraphicSystems::textRenderer(std::size_t /*unused*/)
{
Registry::components<Types::Text> arrText =
Registry::getInstance().getComponents<Types::Text>();
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();
Registry::components<Raylib::Text> arrText =
Registry::getInstance().getComponents<Raylib::Text>();

auto positionIt = arrPosition.begin();
auto textIt = arrText.begin();
auto textIt = arrText.begin();

while (positionIt != arrPosition.end() && textIt != arrText.end()) {
if (textIt->has_value() && positionIt->has_value()) {
drawTextResponsive(positionIt->value(), textIt->value());
while (textIt != arrText.end()) {
if (textIt->has_value()) {
drawTextResponsive(textIt->value());
}
positionIt++;
textIt++;
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/Client/Systems/CustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#pragma once

#include <string>
#include "raylib.h"

namespace Types {

Expand Down Expand Up @@ -38,11 +37,4 @@ namespace Types {
bool isMine;
};

struct Text {
std::string text;
Color color;
Font font;
float fontSize;
};

} // namespace Types
56 changes: 29 additions & 27 deletions src/main_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ void init()
const int initialEntityPositionY = 0;
const int collisionRectWidth = 10;
const int collisionRectHeight = 20;
const std::string spriteImagePath = "./assets/R-TypeSheet/r-typesheet18r.gif";
const int spriteWidth = 10;
const int spriteHeight = 20;
const float rectX = 2.0F;
const float rectY = 5.0F;
const float rectWidth = 30.5F;
const float rectHeight = 25.2F;
const int textPositionX = 40;
const int textPositionY = 40;
const std::string fontPath = "assets/Fonts/soliden/SolidenTrial-Black.otf";
const float textFontSize = 5.5F;
const std::string spriteImagePath =
"./assets/R-TypeSheet/r-typesheet18.gif";
const int spriteWidth = 10;
const int spriteHeight = 20;
const float rectX = 2.0F;
const float rectY = 5.0F;
const float rectWidth = 30.5F;
const float rectHeight = 25.2F;
const Raylib::Vector2 textPosition = {50, 50};
const float fontSize = 5.0F;
const std::string fontPath = "assets/Fonts/soliden/SolidenTrial-Black.otf";
const std::string soundEffectPath = "assets/Audio/Sounds/yes.ogg";
const std::string musicStreamPath = "assets/Audio/Musics/Title.mp3";

Expand All @@ -49,11 +49,11 @@ void init()
collisionRectWidth,
collisionRectHeight};
registry.addEntity();
registry.getComponents<Types::Position>().back() = {
textPositionX,
textPositionY};
registry.getComponents<Types::Text>()
.back() = {"Player", BLACK, LoadFont(fontPath.c_str()), textFontSize};
registry.getComponents<Raylib::Text>().back() = {
std::string("Hello World!"),
textPosition,
fontSize,
Raylib::Purple};
registry.addEntity();
registry.getComponents<Raylib::Sound>().back() =
Raylib::Sound(soundEffectPath);
Expand All @@ -66,7 +66,8 @@ int main()
{
const int screenWidth = 800;
const int screenHeight = 600;
const int fps = 60;
int monitor = 0;
int fps = 0;

Raylib::setConfigFlags(FLAG_WINDOW_RESIZABLE);

Expand All @@ -76,22 +77,23 @@ int main()
director.addSystemManager(Systems::GraphicSystems::graphicSystems);
std::vector<std::size_t> managersIds = {1, 0, 2};

InitWindow(
Raylib::initWindow(
screenWidth,
screenHeight,
"raylib [textures] examples - texture source and destination "
"rectangles");
SetTargetFPS(fps);
InitAudioDevice();
"R-Type");
monitor = Raylib::getCurrentMonitor();
fps = Raylib::getMonitorRefreshRate(monitor);
Raylib::setTargetFPS(fps);
Raylib::initAudioDevice();
init();
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
Raylib::beginDrawing();
Raylib::clearBackground(Raylib::Green);
for (auto id : managersIds) {
director.getSystemManager(id).updateSystems();
}
EndDrawing();
Raylib::endDrawing();
}
CloseAudioDevice();
CloseWindow();
Raylib::closeAudioDevice();
Raylib::closeWindow();
}

0 comments on commit b4ca35b

Please sign in to comment.