diff --git a/source/backends/SDL3/base.cpp b/source/backends/SDL3/base.cpp index baa5b2d..d8b0683 100644 --- a/source/backends/SDL3/base.cpp +++ b/source/backends/SDL3/base.cpp @@ -83,6 +83,14 @@ namespace backend return current_state; } + char *clipboard() { + return SDL_GetClipboardText(); + } + + void clipboard(const char* value) { + SDL_SetClipboardText(value); + } + } #endif \ No newline at end of file diff --git a/source/backends/SDL3/video.cpp b/source/backends/SDL3/video.cpp index 84e66c9..189ddaa 100644 --- a/source/backends/SDL3/video.cpp +++ b/source/backends/SDL3/video.cpp @@ -65,7 +65,9 @@ namespace backend uint32_t *pixels; int pitch; SDL_LockTexture(tex_back, NULL, (void**)&pixels, &pitch); - for (uint32_t i=0;isize;++i) pixels[i] = mini::pal::palette[mini::surf::state.screen_surface->p[i]]; + auto &s = mini::surf::state.surfaces[SCREEN]; + uint8_t *p = s.p; + for (uint32_t i=0;i -#define DEST(x, y) surf::state.dest_surface->p[x+y*surf::state.dest_surface->w] -#define SOURCE(x, y) surf::state.source_surface->p[x+y*surf::state.source_surface->w] - namespace mini { namespace draw @@ -26,23 +23,27 @@ namespace mini namespace pixel { static inline void pset_fast(int x, int y, uint8_t color) { - if (state.trans != color) DEST(x, y) = color; + auto &s = surf::state.surfaces[surf::state.dest_surface]; + uint8_t *p = s.p; + if (state.trans != color) p[x+y*s.w] = color; } static inline void pset_bool(int x, int y, uint8_t color) { if (state.trans != color) { + auto &s = surf::state.surfaces[surf::state.dest_surface]; + uint8_t *p = s.p; switch (state.mode) { case DRAWMODE_AND: - DEST(x, y) = DEST(x, y) & color; + p[x+y*s.w] &= color; break; case DRAWMODE_OR: - DEST(x, y) = DEST(x, y) | color; + p[x+y*s.w] |= color; break; case DRAWMODE_XOR: - DEST(x, y) = DEST(x, y) ^ color; + p[x+y*s.w] ^= color; break; case DRAWMODE_NOT: - DEST(x, y) = ~DEST(x, y); + p[x+y*s.w] = ~p[x+y*s.w]; break; } } @@ -51,13 +52,19 @@ namespace mini static inline void pset_pattern(int x, int y, uint8_t color) { int pbx = x % 4, pby = y % 4; int pb = pbx+pby*4; - if (state.fill_pattern & (1 << pb)) if (state.trans != color) DEST(x, y) = color; + if (state.fill_pattern & (1 << pb)) + if (state.trans != color) { + auto &s = surf::state.surfaces[surf::state.dest_surface]; + uint8_t *p = s.p; + p[x+y*s.w] = color; + } } // Per a les funcions que pinten tot del mateix color static inline void direct_pset(int x, int y, uint8_t color) { + auto &s = surf::state.surfaces[surf::state.dest_surface]; x += view::state.origin[0]; y += view::state.origin[1]; - if (x < surf::state.dest_surface->clip[0] || x > surf::state.dest_surface->clip[2] || y < surf::state.dest_surface->clip[1] || y > surf::state.dest_surface->clip[3]) return; + if (x < s.clip[0] || x > s.clip[2] || y < s.clip[1] || y > s.clip[3]) return; switch (state.mode) { case DRAWMODE_NORMAL: pset_fast(x,y,color); break; case DRAWMODE_PATTERN: pset_pattern(x,y,color); break; @@ -75,9 +82,10 @@ namespace mini } uint8_t get(int x, int y) { - if (!surf::state.source_surface) return 0; - if (x < 0 || x > (surf::state.source_surface->w-1) || y < 0 || y > (surf::state.source_surface->h-1)) return 0; - return SOURCE(x, y); + if (surf::state.source_surface==-1) return 0; + auto &s = surf::state.surfaces[surf::state.source_surface]; + if (x < 0 || x > (s.w-1) || y < 0 || y > (s.h-1)) return 0; + return s.p[x+y*s.w]; } } @@ -417,7 +425,7 @@ namespace mini //const int tw = tile_width; //const int th = tile_height; - const int tiles_per_row = mini::surf::state.source_surface->w / tw; + const int tiles_per_row = mini::surf::state.surfaces[mini::surf::state.source_surface].w / tw; // Coordenadas del tile dentro del spritesheet int tx = (n % tiles_per_row) * tw; diff --git a/source/mini/lua/lua.debug.cpp b/source/mini/lua/lua.debug.cpp index 52c08d1..806c1a7 100644 --- a/source/mini/lua/lua.debug.cpp +++ b/source/mini/lua/lua.debug.cpp @@ -2,7 +2,6 @@ #include "external/lua/lua.hpp" #include "mini/win/win.h" -//#include #include #include diff --git a/source/mini/lua/lua.wrappers.cpp b/source/mini/lua/lua.wrappers.cpp index 08d0ae4..7fa085e 100644 --- a/source/mini/lua/lua.wrappers.cpp +++ b/source/mini/lua/lua.wrappers.cpp @@ -18,7 +18,7 @@ #include "mini/version.h" -#include +#include "backends/backend.h" #include #include @@ -693,10 +693,10 @@ namespace mini static uint32_t chrono_time = 0; static int chrono(lua_State *L) { if (lua_gettop(L) == 0) { - lua_pushnumber(L, float(SDL_GetTicks()-chrono_time)/1000.0f); + lua_pushnumber(L, float(backend::get_time_ms()-chrono_time)/1000.0f); return 1; } else { - chrono_time = SDL_GetTicks(); + chrono_time = backend::get_time_ms(); return 0; } } @@ -785,11 +785,11 @@ namespace mini static int clipboard(lua_State *L) { if (lua_gettop(L) == 0) { - lua_pushstring(L, SDL_GetClipboardText()); + lua_pushstring(L, backend::clipboard()); return 1; } else { const char* value = luaL_checkstring(L, 1); - SDL_SetClipboardText(value); + backend::clipboard(value); return 0; } } diff --git a/source/mini/map/map.cpp b/source/mini/map/map.cpp index ffd2267..b9e5e9d 100644 --- a/source/mini/map/map.cpp +++ b/source/mini/map/map.cpp @@ -3,44 +3,43 @@ #include "mini/view/view.h" #include "mini/draw/draw.h" -#define TILES(x, y) map_surface->p[x+y*map_surface->w] - namespace mini { namespace map { uint8_t tile_width = 8; uint8_t tile_height = 8; - mini::surf::surface_t* map_surface = nullptr; + int map_surface = -1; void init() { } void quit() { - map_surface = nullptr; + map_surface = -1; } namespace surf { void set(uint8_t surface) { - map_surface = &mini::surf::state.surfaces[surface]; + map_surface = surface; } uint8_t get() { - for (unsigned int i=0; iw; - int celh = map_surface->h; + auto &m = mini::surf::state.surfaces[map_surface]; + + int celw = m.w; + int celh = m.h; int celx = 0; int cely = 0; @@ -51,35 +50,37 @@ namespace mini int sx = ox; int sy = oy; + auto &s = mini::surf::state.surfaces[mini::surf::state.dest_surface]; + // Clipping global rápido - if (sx + celw * tw < mini::surf::state.dest_surface->clip[0] || - sy + celh * th < mini::surf::state.dest_surface->clip[1] || - sx > mini::surf::state.dest_surface->clip[2] || - sy > mini::surf::state.dest_surface->clip[3]) + if (sx + celw * tw < s.clip[0] || + sy + celh * th < s.clip[1] || + sx > s.clip[2] || + sy > s.clip[3]) return; // Recorte por izquierda - if (sx < mini::surf::state.dest_surface->clip[0]) { - int diff = (mini::surf::state.dest_surface->clip[0] - sx) / tw; + if (sx < s.clip[0]) { + int diff = (s.clip[0] - sx) / tw; celx += diff; celw -= diff; sx += diff * tw; } // Recorte por arriba - if (sy < mini::surf::state.dest_surface->clip[1]) { - int diff = (mini::surf::state.dest_surface->clip[1] - sy) / th; + if (sy < s.clip[1]) { + int diff = (s.clip[1] - sy) / th; cely += diff; celh -= diff; sy += diff * th; } // Recorte por derecha - int max_x = mini::surf::state.dest_surface->clip[2] - sx; + int max_x = s.clip[2] - sx; if (max_x < celw * tw) celw = max_x / tw + 1; // Recorte por abajo - int max_y = mini::surf::state.dest_surface->clip[3] - sy; + int max_y = s.clip[3] - sy; if (max_y < celh * th) celh = max_y / th + 1; // Ahora sx, sy son relativos al origen y ya están recortados @@ -96,15 +97,17 @@ namespace mini namespace tile { uint8_t get(int celx, int cely) { - if (!map_surface) return 0; - if (celx < 0 || celx > (map_surface->w-1) || cely < 0 || cely > (map_surface->h-1)) return 0; - return TILES(celx, cely); + if (map_surface==-1) return 0; + auto &m = mini::surf::state.surfaces[map_surface]; + if (celx < 0 || celx > (m.w-1) || cely < 0 || cely > (m.h-1)) return 0; + return m.p[celx+cely*m.w]; } void set(int celx, int cely, uint8_t snum) { - if (!map_surface) return; - if (celx < 0 || celx > (map_surface->w-1) || cely < 0 || cely > (map_surface->h-1)) return; - TILES(celx, cely) = snum; + if (map_surface==-1) return; + auto &m = mini::surf::state.surfaces[map_surface]; + if (celx < 0 || celx > (m.w-1) || cely < 0 || cely > (m.h-1)) return; + m.p[celx+cely*m.w] = snum; } } diff --git a/source/mini/mini.cpp b/source/mini/mini.cpp index 675304e..32ecef9 100644 --- a/source/mini/mini.cpp +++ b/source/mini/mini.cpp @@ -18,7 +18,6 @@ #include "lua/lua.h" #include "aux/log.h" -//#include #include "backends/backend.h" #include diff --git a/source/mini/surf/surf.cpp b/source/mini/surf/surf.cpp index c0b3469..339eda2 100644 --- a/source/mini/surf/surf.cpp +++ b/source/mini/surf/surf.cpp @@ -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;ip, 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; iclip[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; } } diff --git a/source/mini/surf/surf.h b/source/mini/surf/surf.h index 90ce0a8..9350b2f 100644 --- a/source/mini/surf/surf.h +++ b/source/mini/surf/surf.h @@ -1,12 +1,14 @@ #pragma once #include +#include namespace mini { namespace surf { - #define MAX_SURFACES 100 + #define SCREEN 0 + //#define MAX_SURFACES 100 #define SURF_NOTHING 0 #define SURF_EXTERNAL 1 @@ -22,10 +24,9 @@ namespace mini }; struct state_t { - surface_t surfaces[MAX_SURFACES]; - surface_t* screen_surface = &surfaces[0]; - surface_t* dest_surface = screen_surface; - surface_t* source_surface = nullptr; + std::vector surfaces; + int dest_surface = -1; + int source_surface = -1; }; extern state_t state;