- [WIP] Puta que susto, una regla mal feta en el .gitignore i ja no comitaba res

- Canvi de comp
This commit is contained in:
2026-04-11 16:39:47 +02:00
parent 14a7fda8b7
commit 0142d79d91
37 changed files with 6375 additions and 2 deletions
+203
View File
@@ -0,0 +1,203 @@
#include "surf.h"
#include "aux/gif.h"
#include "aux/gifenc.h"
#include "aux/log.h"
#include "file/file.h"
#include "draw/draw.h"
#include <SDL3/SDL.h>
#include <stdlib.h>
namespace mini
{
namespace surf
{
state_t state;
uint8_t create(int w, int h) {
unsigned int i = 0;
while (i<MAX_SURFACES && state.surfaces[i].p != NULL) ++i;
if (i==MAX_SURFACES) return 255;
state.surfaces[i].name = nullptr;
state.surfaces[i].w = w;
state.surfaces[i].h = h;
state.surfaces[i].size = w*h;
state.surfaces[i].p = (uint8_t*)calloc(state.surfaces[i].size,1);
state.surfaces[i].flags = SURF_GENERATED;
clip::reset(i);
log_msg(LOG_INFO, "Surface %i creada.\n", i);
return i;
}
uint8_t load(uint8_t* buffer, const char* name) {
// Agafar la pròxima textura lliure
unsigned int i = 0;
while (i<MAX_SURFACES && state.surfaces[i].p != NULL) ++i;
if (i==MAX_SURFACES) return 255;
state.surfaces[i].p = LoadGif(buffer, &state.surfaces[i].w, &state.surfaces[i].h);
state.surfaces[i].size = state.surfaces[i].w * state.surfaces[i].h;
state.surfaces[i].name = (char*)malloc(strlen(name)+1);
state.surfaces[i].flags = SURF_GENERATED;
clip::reset(i);
strcpy(state.surfaces[i].name, name);
log_msg(LOG_INFO, "Buffer '%s' carregat en surface: %i.\n", name, i);
return i;
}
uint8_t load(const char* filename, const bool external) {
// Si el gif ja s'ha carregat en una textura, tornem eixa textura
for (unsigned int i=0; i<MAX_SURFACES; ++i)
if (state.surfaces[i].name && strcmp(state.surfaces[i].name, filename)==0) {
log_msg(LOG_INFO, "Carrega de '%s' abortada: Reusant: %i.\n", filename, i);
return i;
}
// Agafar la pròxima textura lliure
unsigned int i = 0;
while (i<MAX_SURFACES && state.surfaces[i].p != NULL) ++i;
if (i==MAX_SURFACES) return 255;
state.surfaces[i].flags = SURF_NOTHING;
// Carregar l'arxiu de disc
int size;
uint8_t *buffer;
if (external) {
buffer = (uint8_t*)file::getfilebufferex(filename, size);
state.surfaces[i].flags |= SURF_EXTERNAL;
} else {
buffer = (uint8_t*)file::getfilebuffer(filename, size);
}
// Si no s'ha pogut, petar
if (!buffer) {
log_msg(LOG_FAIL, "Error al intentar obrir l'arxiu '%s'\n", filename);
exit(-1);
return 255;
}
// Finalment, carregar el gif en la surface
state.surfaces[i].p = LoadGif(buffer, &state.surfaces[i].w, &state.surfaces[i].h);
state.surfaces[i].size = state.surfaces[i].w * state.surfaces[i].h;
state.surfaces[i].name = (char*)malloc(strlen(filename)+1);
clip::reset(i);
strcpy(state.surfaces[i].name, filename);
free(buffer);
log_msg(LOG_INFO, "Arxiu '%s' carregat en surface: %i.\n", filename, i);
return i;
}
void reloadsurfs() {
for (unsigned int i=0; i<MAX_SURFACES; ++i)
if (state.surfaces[i].name) {
if (state.surfaces[i].flags & SURF_GENERATED) {
log_msg(LOG_INFO, "Ignorant surface generada %i.\n", i);
} else {
log_msg(LOG_INFO, "Recarregant de disc surface %i:'%s'.\n", i, state.surfaces[i].name);
int size;
uint8_t *buffer;
if (state.surfaces[i].flags & SURF_EXTERNAL) {
buffer = (uint8_t*)file::getfilebufferex(state.surfaces[i].name, size);
} else {
buffer = (uint8_t*)file::getfilebuffer(state.surfaces[i].name, size);
}
// Si no s'ha pogut, petar
if (!buffer) {
log_msg(LOG_FAIL, "Error al intentar obrir l'arxiu '%s'\n", state.surfaces[i].name);
exit(-1);
}
state.surfaces[i].p = LoadGif(buffer, &state.surfaces[i].w, &state.surfaces[i].h);
state.surfaces[i].size = state.surfaces[i].w * state.surfaces[i].h;
clip::reset(i);
free(buffer);
}
}
}
void save(uint8_t surface, const char* filename, uint8_t *pal, uint8_t colors)
{
gif::write_gif(filename, state.surfaces[surface].p, state.surfaces[surface].w, state.surfaces[surface].h, pal, colors);
}
void destroy(uint8_t surface) {
if (state.surfaces[surface].name != NULL) free(state.surfaces[surface].name);
state.surfaces[surface].name = NULL;
if (state.surfaces[surface].p != NULL) {
free(state.surfaces[surface].p);
log_msg(LOG_INFO, "Surface %i alliberada.\n", surface);
}
state.surfaces[surface].p = NULL;
state.surfaces[surface].flags = SURF_NOTHING;
}
int width(uint8_t surface) {
return state.surfaces[surface].w;
}
int height(uint8_t surface) {
return state.surfaces[surface].h;
}
void cls(uint8_t color) {
const uint8_t col = draw::state.draw_palette[color];
SDL_memset(state.dest_surface->p, col, state.dest_surface->size);
}
namespace target {
void set(uint8_t surface) {
state.dest_surface = &state.surfaces[surface];
surf::clip::recalculate();
}
uint8_t get() {
for (unsigned int i=0; i<MAX_SURFACES; ++i) if (state.dest_surface == &state.surfaces[i]) return i;
return 0;
}
}
namespace source {
void set(uint8_t surface) {
state.source_surface = &state.surfaces[surface];
}
uint8_t get() {
for (unsigned int i=0; i<MAX_SURFACES; ++i) if (state.source_surface == &state.surfaces[i]) return i;
return 0;
}
}
namespace clip {
void set(int x, int y, int w, int h) {
state.dest_surface->clip[0]=x;
state.dest_surface->clip[1]=y;
state.dest_surface->clip[2]=w-x-1;
state.dest_surface->clip[3]=h-y-1;
recalculate();
}
void reset(int surface) {
surface_t *s = surface==-1 ? state.dest_surface : &state.surfaces[surface];
s->clip[0]=0;
s->clip[1]=0;
s->clip[2]=s->w-1;
s->clip[3]=s->h-1;
//recalculate();
}
void recalculate() {
if (state.dest_surface->clip[0] < 0) state.dest_surface->clip[0] = 0;
if (state.dest_surface->clip[1] < 0) state.dest_surface->clip[1] = 0;
if (state.dest_surface->clip[2] >= state.dest_surface->w) state.dest_surface->clip[2] = state.dest_surface->w-1;
if (state.dest_surface->clip[3] >= state.dest_surface->h) state.dest_surface->clip[3] = state.dest_surface->h-1;
}
}
}
}