- [NEW] Afegit clipboard al backend

- [NEW] surfaces ara usa un vector dinàmic
- [FIX] Ajustades dereferenciacions per a arreglar la caiguda de rendiment
This commit is contained in:
2026-04-17 06:52:31 +02:00
parent 52d2fcf0d3
commit 9c4c94093c
10 changed files with 129 additions and 101 deletions
+54 -48
View File
@@ -18,24 +18,27 @@ namespace mini
void init() {
target::set(create(win::state.width, win::state.height));
state.dest_surface = state.screen_surface;
state.dest_surface = SCREEN;
}
void quit() {
for (unsigned int i=0;i<MAX_SURFACES;++i) surf::destroy(i);
state.dest_surface = state.source_surface = NULL;
for (unsigned int i=0;i<state.surfaces.size();++i) surf::destroy(i);
state.surfaces.clear();
state.dest_surface = state.source_surface = -1;
}
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;
while (i<state.surfaces.size() && state.surfaces[i].p != NULL) ++i;
if (i==state.surfaces.size()) state.surfaces.emplace_back();
surface_t &s = state.surfaces[i];
s.name = nullptr;
s.w = w;
s.h = h;
s.size = w*h;
s.p = (uint8_t*)calloc(s.size,1);
s.flags = SURF_GENERATED;
clip::reset(i);
log_msg(LOG_INFO, "Surface %i creada.\n", i);
return i;
@@ -44,15 +47,16 @@ namespace mini
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;
while (i<state.surfaces.size() && state.surfaces[i].p != NULL) ++i;
if (i==state.surfaces.size()) state.surfaces.emplace_back();
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;
surface_t &s = state.surfaces[i];
s.p = LoadGif(buffer, &s.w, &s.h);
s.size = s.w * s.h;
s.name = (char*)malloc(strlen(name)+1);
strcpy(s.name, name);
s.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);
@@ -61,7 +65,7 @@ namespace mini
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)
for (unsigned int i=0; i<state.surfaces.size(); ++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;
@@ -69,16 +73,18 @@ namespace mini
// 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;
while (i<state.surfaces.size() && state.surfaces[i].p != NULL) ++i;
if (i==state.surfaces.size()) state.surfaces.emplace_back();
state.surfaces[i].flags = SURF_NOTHING;
surface_t &s = state.surfaces[i];
s.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;
s.flags |= SURF_EXTERNAL;
} else {
buffer = (uint8_t*)file::getfilebuffer(filename, size);
}
@@ -91,11 +97,11 @@ namespace mini
}
// 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);
s.p = LoadGif(buffer, &s.w, &s.h);
s.size = s.w * s.h;
s.name = (char*)malloc(strlen(filename)+1);
clip::reset(i);
strcpy(state.surfaces[i].name, filename);
strcpy(s.name, filename);
free(buffer);
log_msg(LOG_INFO, "Arxiu '%s' carregat en surface: %i.\n", filename, i);
@@ -103,7 +109,7 @@ namespace mini
}
void reloadsurfs() {
for (unsigned int i=0; i<MAX_SURFACES; ++i)
for (unsigned int i=0; i<state.surfaces.size(); ++i)
if (state.surfaces[i].name) {
if (state.surfaces[i].flags & SURF_GENERATED) {
log_msg(LOG_INFO, "Ignorant surface generada %i.\n", i);
@@ -159,52 +165,52 @@ namespace mini
void cls(uint8_t color) {
const uint8_t col = draw::state.draw_palette[color];
memset(state.dest_surface->p, col, state.dest_surface->size);
memset(state.surfaces[state.dest_surface].p, col, state.surfaces[state.dest_surface].size);
}
namespace target {
void set(uint8_t surface) {
state.dest_surface = &state.surfaces[surface];
state.dest_surface = 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;
return state.dest_surface;
}
}
namespace source {
void set(uint8_t surface) {
state.source_surface = &state.surfaces[surface];
state.source_surface = surface;
}
uint8_t get() {
for (unsigned int i=0; i<MAX_SURFACES; ++i) if (state.source_surface == &state.surfaces[i]) return i;
return 0;
return state.source_surface;
}
}
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]=x+(w-1);
state.dest_surface->clip[3]=y+(h-1);
auto &s = state.surfaces[state.dest_surface];
s.clip[0]=x;
s.clip[1]=y;
s.clip[2]=x+(w-1);
s.clip[3]=y+(h-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;
auto &s = surface==-1 ? state.surfaces[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;
auto &s = state.surfaces[state.dest_surface];
if (s.clip[0] < 0) s.clip[0] = 0;
if (s.clip[1] < 0) s.clip[1] = 0;
if (s.clip[2] >= s.w) s.clip[2] = s.w-1;
if (s.clip[3] >= s.h) s.clip[3] = s.h-1;
}
}