diff --git a/assets/atlas.png b/assets/atlas.png new file mode 100644 index 0000000..b92a296 Binary files /dev/null and b/assets/atlas.png differ diff --git a/assets/sprites/pacman.png b/assets/sprites/pacman.png deleted file mode 100644 index 25dfc88..0000000 Binary files a/assets/sprites/pacman.png and /dev/null differ diff --git a/assets/sprites/wall.png b/assets/sprites/wall.png deleted file mode 100644 index b984984..0000000 Binary files a/assets/sprites/wall.png and /dev/null differ diff --git a/src/atlas.cpp b/src/atlas.cpp new file mode 100644 index 0000000..409177f --- /dev/null +++ b/src/atlas.cpp @@ -0,0 +1,38 @@ +#include "atlas.h" +#include + +TextureAtlas::TextureAtlas(Texture2D* texture, int offset_x, int offset_y, int width, int height) { + this->texture=texture; + this->offset_x=offset_x; + this->offset_y=offset_y; + this->width=width; + this->height=height; +} +TextureAtlas::TextureAtlas(){ + this->texture = nullptr; + this->offset_x = 0; + this->offset_y = 0; + this->width = 0; + this->height = 0; +} + +Rectangle TextureAtlas::rect_view(int x, int y, int width, int height) const { + return { + (float)this->offset_x+x, + (float)this->offset_y+y, + (float)width, + (float)height + }; +} +Rectangle TextureAtlas::full_view() const { + return { + (float)this->offset_x, + (float)this->offset_y, + (float)this->width, + (float)this->height + }; +} + +const Texture2D TextureAtlas::get_texture() const { + return *this->texture; +} diff --git a/src/atlas.h b/src/atlas.h new file mode 100644 index 0000000..1d2cee4 --- /dev/null +++ b/src/atlas.h @@ -0,0 +1,21 @@ +#ifndef ATLAS_H +#define ATLAS_H +#include + + +class TextureAtlas { + public: + TextureAtlas(Texture2D* texture, int offset_x, int offset_y, int width, int height); + TextureAtlas(); + Rectangle rect_view(int x,int y, int width, int height) const; // Get a view into texture + Rectangle full_view() const; + const Texture2D get_texture() const; + private: + int offset_x; + int offset_y; + int width; + int height; + Texture2D* texture; +}; + +#endif diff --git a/src/components.h b/src/components.h index 2db18e3..574c2bd 100644 --- a/src/components.h +++ b/src/components.h @@ -1,6 +1,7 @@ #ifndef COMPONENTS_H #define COMPONENTS_H +#include "atlas.h" #include #include @@ -22,7 +23,7 @@ class Pacman : public Entity{ const int frame_count = 7; const int fps = 24; private: - Texture2D texture; + TextureAtlas texture; int facing; Rectangle getTextureRect() const; public: @@ -31,7 +32,6 @@ class Pacman : public Entity{ Pacman(); Pacman(Vector2 position); - ~Pacman(); void tick() override; void process() override; void animation_tick(); @@ -40,10 +40,9 @@ class Pacman : public Entity{ class Wall : public Entity { private: - Texture2D texture; + TextureAtlas texture; public: Wall(); - ~Wall(); void ready() override; void draw() const override; }; diff --git a/src/main.cpp b/src/main.cpp index 7a91d0c..f05d420 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ int main() { InitWindow(window_size.x, window_size.y, "Zoomba"); + world.load_atlas(); world.set_size(window_size); load_world(world,"assets/map"); diff --git a/src/maploader.cpp b/src/maploader.cpp index 8a96778..d24ed98 100644 --- a/src/maploader.cpp +++ b/src/maploader.cpp @@ -44,7 +44,6 @@ void load_world(World& world,const char* path) { } - } } diff --git a/src/pacman.cpp b/src/pacman.cpp index 56056e3..e8a958f 100644 --- a/src/pacman.cpp +++ b/src/pacman.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include "atlas.h" #include "components.h" #include "world.h" @@ -8,28 +8,21 @@ Rectangle Pacman::getTextureRect() const{ - Rectangle result; - result.x = 16*frame; - result.y = facing*16; - result.height = 16; - result.width = 16; - return result; -} + return this->texture.rect_view(16*frame, facing*16, 16, 16); + } Pacman::Pacman() { - this->texture = LoadTexture("assets/sprites/pacman.png"); + this->texture = TextureAtlas(get_world().get_atlas(),0,0,112,64); this->facing = 0; this->position = {0.,0.}; this->frame = 0; this->time = 0; } Pacman::Pacman(Vector2 position) { - this->texture = LoadTexture("assets/sprites/pacman.png"); + this->texture = TextureAtlas(get_world().get_atlas(),0,0,112,64); this->facing = 0; this->position = {0.,0.}; } -Pacman::~Pacman() { - UnloadTexture(this->texture); -} + void Pacman::tick() { // Input handling if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;} @@ -77,7 +70,7 @@ void Pacman::animation_tick() { void Pacman::draw() const { // Drawing - DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); + DrawTextureRec(this->texture.get_texture(), this->getTextureRect(), this->position, WHITE); } diff --git a/src/wall.cpp b/src/wall.cpp index a71d15c..e3c3f05 100644 --- a/src/wall.cpp +++ b/src/wall.cpp @@ -1,12 +1,11 @@ #include "components.h" +#include "world.h" +#include #include Wall::Wall() { - this->texture = LoadTexture("assets/sprites/wall.png"); -} - -Wall::~Wall() { - UnloadTexture(this->texture); + std::cout << get_world().get_atlas() <<'\n'; + this->texture = TextureAtlas(get_world().get_atlas(),0,64,16,16); } void Wall::ready() { @@ -15,5 +14,5 @@ void Wall::ready() { void Wall::draw() const { // Draw based on neighbors - DrawTexture(this->texture, this->position.x, this->position.y, WHITE); + DrawTextureRec(this->texture.get_texture(), this->texture.full_view(), this->position, WHITE); } diff --git a/src/world.cpp b/src/world.cpp index a362629..2577a32 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -15,6 +15,11 @@ World::~World(){ for(int i = 0; i < this->entities.size();i++) { delete this->entities[i]; } + UnloadTexture(this->texture_atlas); +} + +void World::load_atlas() { + this->texture_atlas = LoadTexture("assets/atlas.png"); } void World::setup(){ @@ -96,6 +101,10 @@ void World::set_size(Vector2i size){ this->height = size.y; } +Texture2D* World::get_atlas() { + return &this->texture_atlas; +} + void create_world_with(float seconds_per_tick){ get_world().seconds_per_tick = seconds_per_tick; } @@ -104,3 +113,5 @@ World& get_world() { static World world; return world; } + + diff --git a/src/world.h b/src/world.h index 3504dba..c4c592e 100644 --- a/src/world.h +++ b/src/world.h @@ -3,6 +3,7 @@ #define WORLD_H #include "components.h" +#include #include #include @@ -38,9 +39,12 @@ class World { void draw() const; /// Should be called at the end of frame. /// Calls draw() on every entity void set_size(Vector2i); + void load_atlas(); + Texture2D* get_atlas(); private: float clock; void update_grid(); + Texture2D texture_atlas; }; void create_world_with(float seconds_per_tick);