From b628beafb99b98efb744cc572e7450a7883118f9 Mon Sep 17 00:00:00 2001 From: Rendo Date: Tue, 10 Mar 2026 15:20:40 +0500 Subject: [PATCH] modularity problem --- makefile | 5 +++-- src/components.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++ src/components.h | 17 +++++++++++--- src/main.cpp | 53 +------------------------------------------ 4 files changed, 74 insertions(+), 57 deletions(-) create mode 100644 src/components.cpp diff --git a/makefile b/makefile index 5481246..5a71e4e 100644 --- a/makefile +++ b/makefile @@ -1,8 +1,9 @@ CFLAGS=$(shell cat compiler_flags.txt) +FILES=$(wildcard src/*.cpp) -build/raylib-test-linux.x86_64 : src/main.cpp +build/raylib-test-linux.x86_64 : $(FILES) mkdir -p build - clang src/main.cpp $(CFLAGS) -o build/raylib-test-linux.x86_64 + clang $(FILES) $(CFLAGS) -o build/raylib-test-linux.x86_64 clean : diff --git a/src/components.cpp b/src/components.cpp new file mode 100644 index 0000000..2eccc6d --- /dev/null +++ b/src/components.cpp @@ -0,0 +1,56 @@ +#include +#include + +class IComponent { + public: + virtual ~IComponent() {} + virtual void process() = 0; +}; + +class Pacman : IComponent { + const int speed = 64; + private: + Texture2D texture; + Vector2 position; + int facing; + + Rectangle getTextureRect(){ + Rectangle result; + result.x = 0; + result.y = facing*16; + result.height = 16; + result.width = 16; + return result; + } + public: + Pacman() { + this->texture = LoadTexture("assets/sprites/pacman.png"); + this->facing = 0; + this->position = {0.,0.}; + } + Pacman(Vector2 position) { + this->texture = LoadTexture("assets/sprites/pacman.png"); + this->facing = 0; + this->position = position; + } + ~Pacman() { + UnloadTexture(this->texture); + } + virtual void process() { + float delta = GetFrameTime(); + + // Input handling + if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;} + if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {this->facing=2;} + if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) {this->facing=3;} + if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) {this->facing=0;} + + // Movement in direction + double angle = PI/2.0*facing; + Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))}; + this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed)); + + // Drawing + DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); + } +}; diff --git a/src/components.h b/src/components.h index 53dda1b..ee8e70b 100644 --- a/src/components.h +++ b/src/components.h @@ -1,3 +1,6 @@ +#include +#include + class IComponent { public: virtual ~IComponent() {} @@ -5,8 +8,16 @@ class IComponent { }; class Pacman : IComponent { - public: - virtual void process() { + const int speed = 64; + private: + Texture2D texture; + Vector2 position; + int facing; - } + Rectangle getTextureRect(); + public: + Pacman(); + Pacman(Vector2 position); + ~Pacman(); + virtual void process(); }; diff --git a/src/main.cpp b/src/main.cpp index 55279bf..9afd159 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,57 +1,6 @@ #include -#include #include - -// Player controlled entity -class Pacman { - const int speed = 64; - private: - Texture2D texture; - Vector2 position; - int facing; - - Rectangle getTextureRect(){ - Rectangle result; - result.x = 0; - result.y = facing*16; - result.height = 16; - result.width = 16; - return result; - } - public: - Pacman() { - this->texture = LoadTexture("assets/sprites/pacman.png"); - this->facing = 0; - this->position = {0.,0.}; - } - Pacman(Vector2 position) { - this->texture = LoadTexture("assets/sprites/pacman.png"); - this->facing = 0; - this->position = position; - } - ~Pacman() { - UnloadTexture(this->texture); - } - - void process() { - float delta = GetFrameTime(); - - // Input handling - if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;} - if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {this->facing=2;} - if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) {this->facing=3;} - if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) {this->facing=0;} - - // Movement in direction - double angle = PI/2.0*facing; - Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))}; - this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed)); - - // Drawing - DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); - } - -}; +#include "components.h" int main() { const int screen_height = 320;