replaced separate textures with atlasses

This commit is contained in:
rendo 2026-03-13 12:49:11 +05:00
commit bd9abbe5bd
12 changed files with 90 additions and 25 deletions

BIN
assets/atlas.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 760 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 134 B

38
src/atlas.cpp Normal file
View file

@ -0,0 +1,38 @@
#include "atlas.h"
#include <raylib.h>
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;
}

21
src/atlas.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef ATLAS_H
#define ATLAS_H
#include <raylib.h>
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

View file

@ -1,6 +1,7 @@
#ifndef COMPONENTS_H #ifndef COMPONENTS_H
#define COMPONENTS_H #define COMPONENTS_H
#include "atlas.h"
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
@ -22,7 +23,7 @@ class Pacman : public Entity{
const int frame_count = 7; const int frame_count = 7;
const int fps = 24; const int fps = 24;
private: private:
Texture2D texture; TextureAtlas texture;
int facing; int facing;
Rectangle getTextureRect() const; Rectangle getTextureRect() const;
public: public:
@ -31,7 +32,6 @@ class Pacman : public Entity{
Pacman(); Pacman();
Pacman(Vector2 position); Pacman(Vector2 position);
~Pacman();
void tick() override; void tick() override;
void process() override; void process() override;
void animation_tick(); void animation_tick();
@ -40,10 +40,9 @@ class Pacman : public Entity{
class Wall : public Entity { class Wall : public Entity {
private: private:
Texture2D texture; TextureAtlas texture;
public: public:
Wall(); Wall();
~Wall();
void ready() override; void ready() override;
void draw() const override; void draw() const override;
}; };

View file

@ -10,6 +10,7 @@ int main() {
InitWindow(window_size.x, window_size.y, "Zoomba"); InitWindow(window_size.x, window_size.y, "Zoomba");
world.load_atlas();
world.set_size(window_size); world.set_size(window_size);
load_world(world,"assets/map"); load_world(world,"assets/map");

View file

@ -44,7 +44,6 @@ void load_world(World& world,const char* path) {
} }
} }
} }

View file

@ -1,6 +1,6 @@
#include <iostream>
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
#include "atlas.h"
#include "components.h" #include "components.h"
#include "world.h" #include "world.h"
@ -8,28 +8,21 @@
Rectangle Pacman::getTextureRect() const{ Rectangle Pacman::getTextureRect() const{
Rectangle result; return this->texture.rect_view(16*frame, facing*16, 16, 16);
result.x = 16*frame; }
result.y = facing*16;
result.height = 16;
result.width = 16;
return result;
}
Pacman::Pacman() { Pacman::Pacman() {
this->texture = LoadTexture("assets/sprites/pacman.png"); this->texture = TextureAtlas(get_world().get_atlas(),0,0,112,64);
this->facing = 0; this->facing = 0;
this->position = {0.,0.}; this->position = {0.,0.};
this->frame = 0; this->frame = 0;
this->time = 0; this->time = 0;
} }
Pacman::Pacman(Vector2 position) { 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->facing = 0;
this->position = {0.,0.}; this->position = {0.,0.};
} }
Pacman::~Pacman() {
UnloadTexture(this->texture);
}
void Pacman::tick() { void Pacman::tick() {
// Input handling // Input handling
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;} if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;}
@ -77,7 +70,7 @@ void Pacman::animation_tick() {
void Pacman::draw() const { void Pacman::draw() const {
// Drawing // Drawing
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); DrawTextureRec(this->texture.get_texture(), this->getTextureRect(), this->position, WHITE);
} }

View file

@ -1,12 +1,11 @@
#include "components.h" #include "components.h"
#include "world.h"
#include <iostream>
#include <raylib.h> #include <raylib.h>
Wall::Wall() { Wall::Wall() {
this->texture = LoadTexture("assets/sprites/wall.png"); std::cout << get_world().get_atlas() <<'\n';
} this->texture = TextureAtlas(get_world().get_atlas(),0,64,16,16);
Wall::~Wall() {
UnloadTexture(this->texture);
} }
void Wall::ready() { void Wall::ready() {
@ -15,5 +14,5 @@ void Wall::ready() {
void Wall::draw() const { void Wall::draw() const {
// Draw based on neighbors // 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);
} }

View file

@ -15,6 +15,11 @@ World::~World(){
for(int i = 0; i < this->entities.size();i++) { for(int i = 0; i < this->entities.size();i++) {
delete this->entities[i]; delete this->entities[i];
} }
UnloadTexture(this->texture_atlas);
}
void World::load_atlas() {
this->texture_atlas = LoadTexture("assets/atlas.png");
} }
void World::setup(){ void World::setup(){
@ -96,6 +101,10 @@ void World::set_size(Vector2i size){
this->height = size.y; this->height = size.y;
} }
Texture2D* World::get_atlas() {
return &this->texture_atlas;
}
void create_world_with(float seconds_per_tick){ void create_world_with(float seconds_per_tick){
get_world().seconds_per_tick = seconds_per_tick; get_world().seconds_per_tick = seconds_per_tick;
} }
@ -104,3 +113,5 @@ World& get_world() {
static World world; static World world;
return world; return world;
} }

View file

@ -3,6 +3,7 @@
#define WORLD_H #define WORLD_H
#include "components.h" #include "components.h"
#include <raylib.h>
#include <string> #include <string>
#include <vector> #include <vector>
@ -38,9 +39,12 @@ class World {
void draw() const; /// Should be called at the end of frame. void draw() const; /// Should be called at the end of frame.
/// Calls draw() on every entity /// Calls draw() on every entity
void set_size(Vector2i); void set_size(Vector2i);
void load_atlas();
Texture2D* get_atlas();
private: private:
float clock; float clock;
void update_grid(); void update_grid();
Texture2D texture_atlas;
}; };
void create_world_with(float seconds_per_tick); void create_world_with(float seconds_per_tick);