2 Commits

Author SHA1 Message Date
2efbf6f717 - [NEW] Gestió més eficient de la memòria que usen els GIFs 2024-07-02 13:19:16 +02:00
77df278568 - Llevats els colors roig i blau, queden rarunos
- Reajustat el pintat dels comptadors.
- Els comptadors canvien de color segón el color de l'habitació
- [FIX] print2 pintava mal el número
2024-07-02 11:18:04 +02:00
8 changed files with 88 additions and 51 deletions

View File

@@ -3,6 +3,7 @@ caixes.gif
doors.gif doors.gif
floor.gif floor.gif
font.gif font.gif
font2.gif
gat.gif gat.gif
objectes.gif objectes.gif
obrer.gif obrer.gif

View File

@@ -2,7 +2,7 @@ width: 2
height: 1 height: 1
door-height-xn: 4 door-height-xn: 4
door-height-yp: 0 door-height-yp: 0
color: BLUE color: PURPLE
floor-texture: 1 floor-texture: 1
wall-texture: 2 wall-texture: 2
door-texture: 0 door-texture: 0

View File

@@ -1,6 +1,6 @@
width: 3 width: 3
height: 3 height: 3
color: CYAN color: PURPLE
floor-texture: 0 floor-texture: 0
wall-texture: 0 wall-texture: 0
door-texture: 0 door-texture: 0

View File

@@ -46,7 +46,7 @@ namespace actor
strcpy(act->bmp, bmp.c_str()); strcpy(act->bmp, bmp.c_str());
act->pos = p; act->pos = p;
act->size = s; act->size = s;
act->surface = draw::loadSurface(bmp.c_str()); act->surface = draw::getSurface(bmp.c_str());
act->bmp_rect = r; act->bmp_rect = r;
act->bmp_offset = o; act->bmp_offset = o;
act->anim_cycle = act->orient = 0; act->anim_cycle = act->orient = 0;
@@ -82,7 +82,7 @@ namespace actor
{ {
actor_t *new_act = createEmptyActor(); actor_t *new_act = createEmptyActor();
actor::templates::copy(new_act, act); actor::templates::copy(new_act, act);
new_act->surface = draw::loadSurface(new_act->bmp); new_act->surface = draw::getSurface(new_act->bmp);
new_act->prev = new_act->next = new_act->above = new_act->below = nullptr; new_act->prev = new_act->next = new_act->above = new_act->below = nullptr;
return new_act; return new_act;
} }
@@ -109,7 +109,7 @@ namespace actor
} else if (util::strcomp(key, "bmp:")) { } else if (util::strcomp(key, "bmp:")) {
const char *val = file::readString(buffer); const char *val = file::readString(buffer);
strcpy(t->bmp, val); strcpy(t->bmp, val);
t->surface = draw::loadSurface(t->bmp); t->surface = draw::getSurface(t->bmp);
} else if (util::strcomp(key, "bmp-rect:")) { } else if (util::strcomp(key, "bmp-rect:")) {
t->bmp_rect.x = file::readInt(buffer); t->bmp_rect.x = file::readInt(buffer);
t->bmp_rect.y = file::readInt(buffer); t->bmp_rect.y = file::readInt(buffer);
@@ -1076,7 +1076,7 @@ namespace actor
if (act==first) first = act->next; if (act==first) first = act->next;
if (act==dirty) dirty = act->next; if (act==dirty) dirty = act->next;
if (act->next) act->next->prev = act->prev; if (act->next) act->next->prev = act->prev;
draw::freeSurface(act->surface); //draw::freeSurface(act->surface);
if (act==selected) selected = nullptr; if (act==selected) selected = nullptr;
free(act); free(act);
} }
@@ -1090,8 +1090,8 @@ namespace actor
if (act->next) act->next->prev = act->prev; if (act->next) act->next->prev = act->prev;
if (act==selected) selected = nullptr; if (act==selected) selected = nullptr;
picked = act; picked = act;
picked->pos.x=36; picked->pos.x=26;
picked->pos.y=82; picked->pos.y=84;
picked->inner_x = 148-act->bmp_offset.x + act->pos.x*2 - act->pos.y*2; picked->inner_x = 148-act->bmp_offset.x + act->pos.x*2 - act->pos.y*2;
picked->inner_y = 91-act->bmp_offset.y + act->pos.x + act->pos.y - act->pos.z*2; picked->inner_y = 91-act->bmp_offset.y + act->pos.x + act->pos.y - act->pos.z*2;
@@ -1109,7 +1109,7 @@ namespace actor
if (act->flags & FLAG_HERO) { if (act->flags & FLAG_HERO) {
hero = act; hero = act;
} else { } else {
draw::freeSurface(act->surface); //draw::freeSurface(act->surface);
free(act); free(act);
} }
act = tmp; act = tmp;
@@ -1122,7 +1122,7 @@ namespace actor
if (act->flags & FLAG_HERO) { if (act->flags & FLAG_HERO) {
hero = act; hero = act;
} else { } else {
draw::freeSurface(act->surface); //draw::freeSurface(act->surface);
free(act); free(act);
} }
act = tmp; act = tmp;

View File

@@ -2,6 +2,7 @@
#include "SDL2/SDL.h" #include "SDL2/SDL.h"
#include "gif.c" #include "gif.c"
#include "jfile.h" #include "jfile.h"
#include <vector>
namespace draw namespace draw
{ {
@@ -31,6 +32,9 @@ namespace draw
SDL_Rect viewport; SDL_Rect viewport;
surface* managed[50];
int num_managed = 0;
// Inicialització de tot el que fa falta per a carregar gràfics i pintar en pantalla // Inicialització de tot el que fa falta per a carregar gràfics i pintar en pantalla
void init(const std::string &titol, const uint16_t width, const uint16_t height, const int zoom) void init(const std::string &titol, const uint16_t width, const uint16_t height, const int zoom)
{ {
@@ -52,15 +56,27 @@ namespace draw
sel_color = transparent = 0; sel_color = transparent = 0;
for (int i=0;i<256;++i) color_indices[i] = i; for (int i=0;i<256;++i) color_indices[i] = i;
textsurf = loadSurface("font.gif"); int size;
textsurf2 = loadSurface("font2.gif"); char *buffer = file::getFileBuffer("gifs.txt", size, true);
char *p = buffer;
char *n = buffer;
while (*n!=0) {
while (*n!='\n') n++; *n=0;
loadSurface(p);
p=++n;
}
free(buffer);
textsurf = getSurface("font.gif");
textsurf2 = getSurface("font2.gif");
} }
// Finalització del sistema // Finalització del sistema
void quit() void quit()
{ {
if (textsurf) freeSurface(textsurf); for (int i=0; i<num_managed; ++i) if (managed[i]) freeSurface(managed[i]);
if (textsurf2) freeSurface(textsurf2); //if (textsurf) freeSurface(textsurf);
//if (textsurf2) freeSurface(textsurf2);
// Si la superficie "screen" existia, alliberem la seua memòria // Si la superficie "screen" existia, alliberem la seua memòria
if (screen != nullptr) if (screen != nullptr)
@@ -99,12 +115,15 @@ namespace draw
surf->w = w; surf->w = w;
surf->h = h; surf->h = h;
// Es una surface nova, no ve de arxiu ni tindrà varies referències
surf->filename = nullptr;
// ...i tornem la superficie creada, clar // ...i tornem la superficie creada, clar
return surf; return surf;
} }
// Carrega un gràfic d'un arxiu (en format GIF) a una nova superficie, i torna un punter a ella // Carrega un gràfic d'un arxiu (en format GIF) a una nova superficie, i torna un punter a ella
surface *loadSurface(const std::string &filename) void loadSurface(const std::string &filename)
{ {
// Agafem un buffer de bytes de l'arxiu especificat // Agafem un buffer de bytes de l'arxiu especificat
// getFileBuffer() simplement ens torna el arxiu sencer dins de un array de char // getFileBuffer() simplement ens torna el arxiu sencer dins de un array de char
@@ -114,7 +133,8 @@ namespace draw
// Si ens ha tornat nullptr, es que no l'ha trobat, tornem nosaltres també nullptr ja que no s'ha pogut crear la superficie // Si ens ha tornat nullptr, es que no l'ha trobat, tornem nosaltres també nullptr ja que no s'ha pogut crear la superficie
if (buffer == nullptr) if (buffer == nullptr)
{ {
return nullptr; printf("ERROR: GIF no trobat: '%s'\n", filename.c_str());
exit(1);
} }
// Primer reservem memòria per a la estructura "surface" // Primer reservem memòria per a la estructura "surface"
@@ -129,8 +149,23 @@ namespace draw
// Com ja no ens fa falta, alliberem la memòria del buffer del arxiu // Com ja no ens fa falta, alliberem la memòria del buffer del arxiu
free(buffer); free(buffer);
// I finalment tornem la superficie // Especifiquem el nom de l'arxiu del que vé i el guardem a la llista
return surf; surf->filename = (char*)malloc(filename.length()+1);
strcpy(surf->filename, filename.c_str());
managed[num_managed++] = surf;
}
surface *getSurface(const std::string &filename)
{
for(int i=0; i<num_managed; ++i)
{
// Si ja existia, pujem el nombre de referències i tornem eixa surface, i au
if (strcmp(managed[i]->filename, filename.c_str())==0)
{
return managed[i];
}
}
return nullptr;
} }
// Allibera la memòria d'una superficie, els seus pixels inclosos // Allibera la memòria d'una superficie, els seus pixels inclosos
@@ -145,7 +180,12 @@ namespace draw
free(surf->pixels); free(surf->pixels);
} }
// ... alliberem la superficie // Si el nom de l'arxiu existeix, l'alliberem
if (surf->filename != nullptr)
{
free(surf->filename);
}
free(surf); free(surf);
} }
} }
@@ -402,7 +442,7 @@ namespace draw
void print2(const int num, const int positions, const int x, const int y, const uint8_t color, const int zoom) void print2(const int num, const int positions, const int x, const int y, const uint8_t color, const int zoom)
{ {
char buffer[positions]; char buffer[positions+1];
int digit = positions-1; int digit = positions-1;
int value = num; int value = num;
while (digit>=0) while (digit>=0)
@@ -411,6 +451,7 @@ namespace draw
value = value/10; value = value/10;
digit--; digit--;
} }
buffer[positions]=0;
print2(buffer, x, y, color, zoom); print2(buffer, x, y, color, zoom);
} }

View File

@@ -22,6 +22,7 @@ namespace draw
uint16_t w; // Ample de la superficie uint16_t w; // Ample de la superficie
uint16_t h; // Alt de la superficie uint16_t h; // Alt de la superficie
uint8_t *pixels; // pixels de la superficie uint8_t *pixels; // pixels de la superficie
char *filename;
}; };
/// @brief Inicialització de tot el que fa falta per a carregar gràfics i pintar en pantalla. /// @brief Inicialització de tot el que fa falta per a carregar gràfics i pintar en pantalla.
@@ -46,7 +47,10 @@ namespace draw
/// @brief Carrega un gràfic d'un arxiu (en format GIF) a una nova superficie, i torna un punter a ella /// @brief Carrega un gràfic d'un arxiu (en format GIF) a una nova superficie, i torna un punter a ella
/// @param filename nom de l'arxiu GIF d'on carregar la superficie /// @param filename nom de l'arxiu GIF d'on carregar la superficie
/// @return un punter a una nova superficie /// @return un punter a una nova superficie
surface *loadSurface(const std::string &filename); /// ATENCIÓ: EN THEPOOL AÇÒ VAL PER A LA CÀRREGA INCIAL DE TOTS ELS GIFs
void loadSurface(const std::string &filename);
surface *getSurface(const std::string &filename);
/// @brief Allibera la memòria d'una superficie, els seus pixels inclosos /// @brief Allibera la memòria d'una superficie, els seus pixels inclosos
/// @param surf punter a la superficie a alliberar /// @param surf punter a la superficie a alliberar

View File

@@ -55,7 +55,7 @@ void game::init()
draw::init("The Pool", 520, 240, 3); draw::init("The Pool", 520, 240, 3);
//room::init(); //room::init();
surf = draw::loadSurface("test.gif"); surf = draw::getSurface("test.gif");
draw::setSource(surf); draw::setSource(surf);
draw::loadPalette("test.gif"); draw::loadPalette("test.gif");
game::setUpdateTicks(64); game::setUpdateTicks(64);
@@ -372,19 +372,17 @@ bool game::loop()
room::draw2(); room::draw2();
draw::swapcol(1, WHITE+LIGHT); draw::swapcol(1, WHITE+LIGHT);
actor::draw(actor::getPicked(), false); actor::draw(actor::getPicked(), false);
draw::print2("a", 4, 24, YELLOW, FONT_ZOOM_NONE);
draw::print2("b", 7, 24, LIGHT+TEAL, FONT_ZOOM_NONE);
draw::print2("c", 10, 24, LIGHT+PURPLE, FONT_ZOOM_NONE);
draw::print2(actor::hero::getBoostJump(), 2, 3, 26, LIGHT+PURPLE, FONT_ZOOM_NONE); const int col1 = 7+(room::getColor()-6)%5;
draw::print2(actor::hero::getBoostGod()/2, 2, 6, 26, YELLOW, FONT_ZOOM_NONE); const int col2 = 7+(room::getColor()-5)%5;
draw::print2(actor::hero::getBoostRun()/2, 2, 9, 26, LIGHT+TEAL, FONT_ZOOM_NONE); const int col3 = 7+(room::getColor()-4)%5;
draw::print2("a", 4, 26, col1, FONT_ZOOM_NONE);
draw::print2("b", 7, 26, col2, FONT_ZOOM_NONE);
draw::print2("c", 10, 26, col3, FONT_ZOOM_NONE);
//print(10, 200, actor::hero::getBoostJump()); draw::print2(actor::hero::getBoostJump(), 2, 3, 28, col3, FONT_ZOOM_NONE);
//print(30, 200, actor::hero::getBoostGod()/2); draw::print2(actor::hero::getBoostGod()/2, 2, 6, 28, col1, FONT_ZOOM_NONE);
//print(50, 200, actor::hero::getBoostRun()/2); draw::print2(actor::hero::getBoostRun()/2, 2, 9, 28, col2, FONT_ZOOM_NONE);
//draw::draw(148+sx*2-sy*2, 67+sx+sy,24,24,24,0);
/* /*
print(0,0,input::mouseX()); print(0,0,input::mouseX());
@@ -579,7 +577,7 @@ switch (section)
ui::label("TEXTURES", 2, line, 96, 11, GRAY); line+=10; ui::label("TEXTURES", 2, line, 96, 11, GRAY); line+=10;
changed |= btn_opt("COLOR", 2, line, room::editor::refColor(), {5, 6, 7, 8, 9, 10, 11}, {"BLUE", "RED", "PURPLE", "GREEN", "CYAN", "YELLOW", "WHITE"}, 47); //ui::label("COLOR", 2, line, 64, 11); changed |= btn_opt("COLOR", 2, line, room::editor::refColor(), {7, 8, 9, 10, 11}, {"PURPLE", "GREEN", "CYAN", "YELLOW", "WHITE"}, 47); //ui::label("COLOR", 2, line, 64, 11);
//changed |= btn_small(64, line, room::editor::refColor(), 5, 11); //changed |= btn_small(64, line, room::editor::refColor(), 5, 11);
line += 10; line += 10;
ui::label("FLOOR", 2, line, 48, 11); ui::label("FLOOR", 2, line, 48, 11);
@@ -665,8 +663,8 @@ switch (section)
ui::label("BITMAP", 2, line, 96, 11, GRAY); line+=10; ui::label("BITMAP", 2, line, 96, 11, GRAY); line+=10;
if (btn_opt2("FILE", 2, line, act->bmp, gifs)) { if (btn_opt2("FILE", 2, line, act->bmp, gifs)) {
draw::freeSurface(act->surface); //draw::freeSurface(act->surface);
act->surface = draw::loadSurface(act->bmp); act->surface = draw::getSurface(act->bmp);
changed = true; changed = true;
} }
line+=10; line+=10;

View File

@@ -40,17 +40,10 @@ namespace room
{ {
actor::clear(); actor::clear();
// [TODO][2024-06-05] Açò es una xorrada. Si al final sempre son els mateixos arxius, floor_surf = draw::getSurface("floor.gif");
// carregar açò al principi del joc i au walls_surf = draw::getSurface("walls.gif");
if (floor_surf) draw::freeSurface(floor_surf); doors_surf = draw::getSurface("doors.gif");
if (walls_surf) draw::freeSurface(walls_surf); aux_surf = draw::getSurface("roomaux.gif");
if (doors_surf) draw::freeSurface(doors_surf);
if (aux_surf) draw::freeSurface(aux_surf);
floor_surf = draw::loadSurface("floor.gif");
walls_surf = draw::loadSurface("walls.gif");
doors_surf = draw::loadSurface("doors.gif");
aux_surf = draw::loadSurface("roomaux.gif");
editor::updateRoomList(); editor::updateRoomList();
} }
@@ -148,7 +141,7 @@ namespace room
door_height[YN] = SDL_clamp(val, -1, 5); door_height[YN] = SDL_clamp(val, -1, 5);
} else if (util::strcomp(key, "color:")) { } else if (util::strcomp(key, "color:")) {
color = util::stringToInt(file::readString(&buffer), {"blue", "red", "purple", "green", "cyan", "yellow", "white"}, {5, 6, 7, 8, 9, 10, 11}); color = util::stringToInt(file::readString(&buffer), {"purple", "green", "cyan", "yellow", "white"}, {7, 8, 9, 10, 11});
} else if (util::strcomp(key, "floor-texture:")) { } else if (util::strcomp(key, "floor-texture:")) {
floor_type = file::readInt(&buffer); floor_type = file::readInt(&buffer);
@@ -446,8 +439,8 @@ namespace room
const char *numToColor(uint8_t value) const char *numToColor(uint8_t value)
{ {
const char* colors[7] = {"BLUE", "RED", "PURPLE", "GREEN", "CYAN", "YELLOW", "WHITE"}; const char* colors[5] = {"PURPLE", "GREEN", "CYAN", "YELLOW", "WHITE"};
return colors[value-5]; return colors[value-7];
} }
void modify() { modified = true; } void modify() { modified = true; }