From bd9abbe5bdae99285cdffdc001b1d1e96ff1d958 Mon Sep 17 00:00:00 2001 From: rendo Date: Fri, 13 Mar 2026 12:49:11 +0500 Subject: [PATCH] replaced separate textures with atlasses --- assets/atlas.png | Bin 0 -> 1090 bytes assets/sprites/pacman.png | Bin 760 -> 0 bytes assets/sprites/wall.png | Bin 134 -> 0 bytes src/atlas.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/atlas.h | 21 +++++++++++++++++++++ src/components.h | 7 +++---- src/main.cpp | 1 + src/maploader.cpp | 1 - src/pacman.cpp | 21 +++++++-------------- src/wall.cpp | 11 +++++------ src/world.cpp | 11 +++++++++++ src/world.h | 4 ++++ 12 files changed, 90 insertions(+), 25 deletions(-) create mode 100644 assets/atlas.png delete mode 100644 assets/sprites/pacman.png delete mode 100644 assets/sprites/wall.png create mode 100644 src/atlas.cpp create mode 100644 src/atlas.h diff --git a/assets/atlas.png b/assets/atlas.png new file mode 100644 index 0000000000000000000000000000000000000000..b92a29645d05dc104d0417430d8e482cb9bb3e49 GIT binary patch literal 1090 zcmV-I1ikx-P)Px#1am@3R0s$N2z&@+hyVZs>`6pHRCt{2-OYB}AP_)d(8;E|y#G^Xm!0V*vSq9v zV1OCK{Z_5w0SU$+B+`1fBmB6qciewFzjvH?Tk$zQ3OuSj^(2VbjnDNg?5g6c6CqwV zzSL5q5DmEvwpF9?B=uXeI9GNt+Zc`bqq45-b$-wK_hO{+5`MHymJFRWI$t_;*Crj! zzG|IFs?r9OUZ{5bQ8_IWD1;E4bIyBzc<#*;K)c~bk1w@${`%*mB)Cif>9QsMd2WM* z9~UHzXd!<4_uR5tC(uy$0FpWUs3Em)Af?U{BxMR1Y8e=QTqteu8XYC6UxWX1`WffS z8)wbFk>YE9i>+M0#FS40Ei4Cl5~ygTVZ1e72Hh+^SJs{JD6d0jkI#|x9H3;#+@5Uj z`As`ME<^g_s^ygRaSw1-S;~n>;;)ujt>^9<^=rrP2QI;dWjFRY0;Cqcey;Q`wq<G?v*z53Bd5BRn}i$7y_3P8HJ zgZDc82njH`Me^9we>+dSZhX?`qAB6+9$@{0_WNw&h4Cl%0B6jv0+1%B)1$a|+V8W8 zXFmY(9rpvU3YZ*Iz|G=wu?o0Je5u<^0b0L@u`p21z$h z)=GlX`n5@uz|<5VeKfi)N3;HsTwZMMU1`mmnUu1u#n-->YrQHEPXX4KMb_h`qzO>{ zT|li1*51i8%kE)u0@S|X8s+ue=sH@5$4o3^SO)4X1DDc*wlUK95;d29()veizm<74Ed=3*+0Q4{g+$=s9`vGneUy3PUawZ;R5I^yL0La{G zKLGk80Va2v04<)^_bg-d`g=U0Ha!B2-hm@9od7G1w{6n;M`8Bg2S_RRC_d*{i~3X5 zh%b#@HQNuc9!qK9)W%MUKP#&wCb$0hSgq^7I-gP|rvS;e?p~j{i|3xe)TdHc8QnVm zsEnLWJBydruho8~K1P7lA++Q(HR4-;JEiq&w_h&r{sq~i;NSEAl?t|*yazx(uip#E z{U%FC4w%3QFgX%nawNdyNPx+a0FxsDCPxBHjs%z-2{1VlU~(kDPx%vPnciRCt{2n?aIfsnXH1%<7U+r)<0BG9N=4*D)2_Qaz+JW=V_tYIgblH3Tee{No zZ#s}5Sm4XQM{V`4cl8cL-eI*sXfJ?Jx!Oh?-*n6dj3dy9e<|ez+}t@MJ@K4Rk%jwz zReF|bk0XT&^aF|)VU&sI_F~*>YV>KDOCD@5Ufgl zBg(;-8nI=i|IkKW`!E~*_=jr{XZ>r-R0_?Xl)1kM)9}Ai-Oi4#iqMz&s4Z(e=lxUc z!70?*vf3Muiv2%yxjL*}N%!*g8a#m99p33sX7SouS=rNSq2BTvtc%%tqzo*05@bHy a;b2%5&#u2K_;M0ZFN3G6pUXO@geCwqnj|m) 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);