diff --git a/source/mini/draw/draw.cpp b/source/mini/draw/draw.cpp index f6fa4b0..5c8177b 100644 --- a/source/mini/draw/draw.cpp +++ b/source/mini/draw/draw.cpp @@ -3,7 +3,7 @@ #include "mini/view/view.h" #include "mini/font/font.h" -#include +#include #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] @@ -14,6 +14,16 @@ namespace mini { state_t state; + void init() { + state.mode = DRAWMODE_NORMAL; + state.fill_pattern = 0b1111111111111111; + } + + void quit() { + + } + + namespace pixel { static inline void pset_fast(int x, int y, uint8_t color) { if (state.trans != color) DEST(x, y) = state.draw_palette[color]; @@ -343,8 +353,8 @@ namespace mini // Ángulo en radianes float a = angle_deg * 3.14159265f / 180.0f; - float ca = SDL_cosf(a); - float sa = SDL_sinf(a); + float ca = cosf(a); + float sa = sinf(a); // --- 1. Bounding box rotado del rectángulo destino --- @@ -370,10 +380,10 @@ namespace mini if (dyp > max_y) max_y = dyp; } - int bb_x0 = (int)SDL_floorf(min_x); - int bb_x1 = (int)SDL_ceilf (max_x); - int bb_y0 = (int)SDL_floorf(min_y); - int bb_y1 = (int)SDL_ceilf (max_y); + int bb_x0 = (int)floorf(min_x); + int bb_x1 = (int)ceilf (max_x); + int bb_y0 = (int)floorf(min_y); + int bb_y1 = (int)ceilf (max_y); // --- 2. Rotación inversa + escalado + clipping estricto --- diff --git a/source/mini/draw/draw.h b/source/mini/draw/draw.h index c0a93cb..7f4e8ed 100644 --- a/source/mini/draw/draw.h +++ b/source/mini/draw/draw.h @@ -21,6 +21,9 @@ namespace mini }; extern state_t state; + void init(); + void quit(); + namespace pixel { void set(int x, int y, uint8_t color); uint8_t get(int x, int y); diff --git a/source/mini/lua/lua.debug.cpp b/source/mini/lua/lua.debug.cpp index faedc15..52c08d1 100644 --- a/source/mini/lua/lua.debug.cpp +++ b/source/mini/lua/lua.debug.cpp @@ -2,7 +2,7 @@ #include "external/lua/lua.hpp" #include "mini/win/win.h" -#include +//#include #include #include @@ -1136,7 +1136,7 @@ namespace mini } } - SDL_Delay(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } @@ -1293,7 +1293,7 @@ namespace mini } sendLogOutput(lastExceptionMessage); - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Mini Runtime Exception", lastExceptionTraceback.c_str(), NULL); + //SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Mini Runtime Exception", lastExceptionTraceback.c_str(), NULL); return false; } return true; @@ -1320,7 +1320,7 @@ namespace mini printf("COMANDO RECIBIDO: %s\n", line.c_str()); } } else { - SDL_Delay(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } }); diff --git a/source/mini/lua/lua.wrappers.cpp b/source/mini/lua/lua.wrappers.cpp index 815ab29..08d0ae4 100644 --- a/source/mini/lua/lua.wrappers.cpp +++ b/source/mini/lua/lua.wrappers.cpp @@ -4,6 +4,7 @@ #include "mini/mini.h" #include "mini/surf/surf.h" #include "mini/draw/draw.h" +#include "mini/map/map.h" #include "mini/pal/pal.h" #include "mini/font/font.h" #include "mini/win/win.h" @@ -111,7 +112,8 @@ namespace mini const char* str = luaL_checkstring(L, 2); if (lua_istable(L, -1)) { - const int len = SDL_min(256, lua_rawlen(L, -1)); + int len = lua_rawlen(L, -1); + if (len > 256) len = 256; uint8_t *pal = (uint8_t*)malloc(len*3); uint8_t *p=pal; for (int i=1;i<=len;++i) { @@ -283,7 +285,8 @@ namespace mini int r,g,b; if (lua_istable(L, -1)) { uint32_t pal[256]; - const int len = SDL_min(256, lua_rawlen(L, -1)); + int len = lua_rawlen(L, -1); + if (len > 256) len = 256; for (int i=0;ip[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; + + void init() { + + } + + void quit() { + map_surface = nullptr; + } + + namespace surf { + void set(uint8_t surface) { + map_surface = &mini::surf::state.surfaces[surface]; + } + + uint8_t get() + { + for (unsigned int i=0; iw; + int celh = map_surface->h; + + int celx = 0; + int cely = 0; + + int ox = view::state.origin[0]; + int oy = view::state.origin[1]; + + int sx = ox; + int sy = oy; + + // 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]) + return; + + // Recorte por izquierda + if (sx < mini::surf::state.dest_surface->clip[0]) { + int diff = (mini::surf::state.dest_surface->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; + cely += diff; + celh -= diff; + sy += diff * th; + } + + // Recorte por derecha + int max_x = mini::surf::state.dest_surface->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; + if (max_y < celh * th) celh = max_y / th + 1; + + // Ahora sx, sy son relativos al origen y ya están recortados + for (int y = 0; y < celh; ++y) { + int ty = sy + y * th; + for (int x = 0; x < celw; ++x) { + uint8_t tile = tile::get(celx + x, cely + y); + if (!tile) continue; + int tx = sx + x * tw; + draw::tileblit(tile, tx - ox, ty - oy, tile_width, tile_height); + } + } + } + + 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); + } + + 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; + } + } + + namespace cell { + uint8_t getw() { return tile_width; } + uint8_t geth() { return tile_height; } + + void set(int w, int h) { + tile_width = w; + tile_height = h; + } + } + + } +} \ No newline at end of file diff --git a/source/mini/map/map.h b/source/mini/map/map.h new file mode 100644 index 0000000..bb7f89f --- /dev/null +++ b/source/mini/map/map.h @@ -0,0 +1,28 @@ +#pragma once +#include + +namespace mini +{ + namespace map + { + void init(); + void quit(); + + namespace surf { + void set(uint8_t surface); + uint8_t get(); + } + void draw(); //int celx, int cely, int sx, int sy, uint8_t celw, uint8_t celh, uint8_t layer=0); + + namespace tile { + uint8_t get(int celx, int cely); + void set(int celx, int cely, uint8_t snum); + } + + namespace cell { + uint8_t getw(); + uint8_t geth(); + void set(int w, int h); + } + } +} diff --git a/source/mini/mini.cpp b/source/mini/mini.cpp index bed3472..38da8de 100644 --- a/source/mini/mini.cpp +++ b/source/mini/mini.cpp @@ -4,6 +4,7 @@ #include "surf/surf.h" #include "pal/pal.h" #include "font/font.h" +#include "map/map.h" #include "draw/draw.h" #include "win/win.h" #include "view/view.h" @@ -22,8 +23,6 @@ #include - - #define UPDATE_ALWAYS 0 #define UPDATE_WAIT 1 #define UPDATE_TIMEOUT 2 @@ -32,37 +31,21 @@ #include #endif - - -int fps=0; - char config_folder[256]; - - - -uint8_t tile_width = 8; -uint8_t tile_height = 8; - - char main_lua_file[200] = "main.lua"; bool override_ini = false; int update_mode = UPDATE_ALWAYS; int timeout = 0; + +int fps=0; uint32_t last_update = 0; float delta_time = 0.0f; bool should_exit = false; bool should_quit = false; -Uint32 *pixels; -int pitch; - - -mini::surf::surface_t* map_surface = NULL; - - int16_t beats, num_beats = 0; void createNewProject(); @@ -100,115 +83,11 @@ void read_ini() { //SDL_Log("'game.ini' carregat!\n"); } -#define TILES(x, y) map_surface->p[x+y*map_surface->w] #define CURRENT(x, y) surfaces[i].p[(x)+(y)*surfaces[i].w] namespace mini { - namespace map - { - namespace surf { - void set(uint8_t surface) { - map_surface = &mini::surf::state.surfaces[surface]; - } - - uint8_t get() - { - for (unsigned int i=0; iw; - int celh = map_surface->h; - - int celx = 0; - int cely = 0; - - int ox = view::state.origin[0]; - int oy = view::state.origin[1]; - - int sx = ox; - int sy = oy; - - // 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]) - return; - - // Recorte por izquierda - if (sx < mini::surf::state.dest_surface->clip[0]) { - int diff = (mini::surf::state.dest_surface->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; - cely += diff; - celh -= diff; - sy += diff * th; - } - - // Recorte por derecha - int max_x = mini::surf::state.dest_surface->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; - if (max_y < celh * th) celh = max_y / th + 1; - - // Ahora sx, sy son relativos al origen y ya están recortados - for (int y = 0; y < celh; ++y) { - int ty = sy + y * th; - for (int x = 0; x < celw; ++x) { - uint8_t tile = tile::get(celx + x, cely + y); - if (!tile) continue; - int tx = sx + x * tw; - draw::tileblit(tile, tx - ox, ty - oy, tile_width, tile_height); - } - } - } - - 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); - } - - 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; - } - } - - namespace cell { - uint8_t getw() { return tile_width; } - uint8_t geth() { return tile_height; } - - void set(int w, int h) { - tile_width = w; - tile_height = h; - } - } - - } - - - namespace sys { @@ -254,26 +133,6 @@ namespace mini -void reinit() { - log_msg(LOG_INFO, "STARTING A SYSTEM REINITIALIZATION\n"); - - // Reset view state - mini::view::init(); - - // Reset draw state - mini::draw::state.mode = DRAWMODE_NORMAL; - mini::draw::state.fill_pattern = 0b1111111111111111; - - // Reset surf state - for (unsigned int i=0; i13) { dt = SDL_GetTicks(); @@ -430,19 +295,27 @@ int main(int argc,char*argv[]){ } else { exception_loop(); } - if (beats==0)beats=num_beats; - if (beats>0)beats--; + + if (beats == 0) beats = num_beats; + if (beats > 0) beats--; + + // Reset keyboard, mouse and pad key::state.just_pressed = 0; - mouse::state.just_pressed = 0; key::state.has_text_input = false; key::state.text_input_buffer[0] = '\0'; + mouse::state.just_pressed = 0; mouse::state.double_click = false; mouse::state.wheel = 0; pad::state.just_pressed = SDL_GAMEPAD_BUTTON_INVALID; } + + // Render frame SDL_SetRenderTarget(win::state.renderer, win::state.tex_shader); - SDL_SetRenderDrawColor(win::state.renderer, 0, 0, 0, 255); - SDL_RenderClear(win::state.renderer); + //SDL_SetRenderDrawColor(win::state.renderer, 0, 0, 0, 255); + //SDL_RenderClear(win::state.renderer); + + uint32_t *pixels; + int pitch; SDL_LockTexture(win::state.tex_back, NULL, (void**)&pixels, &pitch); for (uint32_t i=0;isize;++i) pixels[i] = pal::palette[surf::state.screen_surface->p[i]]; SDL_UnlockTexture(win::state.tex_back); @@ -459,17 +332,19 @@ int main(int argc,char*argv[]){ fps_timer = SDL_GetTicks(); } } + lua::quit(); audio::quit(); //Mix_Quit(); - for (unsigned int i=0;i - #include +#define clamp(x,a,b) (((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x))) + namespace mini { namespace pal @@ -13,6 +13,11 @@ namespace mini uint32_t palette[256] = { 0xFF1a1c2c, 0xFF5d275d, 0xFFb13e53, 0xFFef7d57, 0xFFffcd75, 0xFFa7f070, 0xFF38b764, 0xFF257179, 0xFF29366f, 0xFF3b5dc9, 0xFF41a6f6, 0xFF73eff7, 0xFFf4f4f4, 0xFF94b0c2, 0xFF566c86, 0xFF333c57 }; + void init() { + pal::subpal::reset(); + draw::state.trans=0; + } + uint32_t* LoadPalette(uint8_t *buffer, uint16_t *size = NULL ) { buffer += 10; @@ -67,8 +72,8 @@ namespace mini namespace subpal { uint8_t set(uint8_t index, uint8_t color) { - const uint8_t old = draw::state.draw_palette[SDL_clamp(index,0,255)]; - draw::state.draw_palette[SDL_clamp(index,0,255)] = SDL_clamp(color,0,255); + const uint8_t old = draw::state.draw_palette[clamp(index,0,255)]; + draw::state.draw_palette[clamp(index,0,255)] = clamp(color,0,255); return old; } diff --git a/source/mini/pal/pal.h b/source/mini/pal/pal.h index c6f3569..365ab66 100644 --- a/source/mini/pal/pal.h +++ b/source/mini/pal/pal.h @@ -8,6 +8,8 @@ namespace mini { extern uint32_t palette[256]; + void init(); + uint32_t *load(const char* filename, uint16_t *palsize=nullptr); void set(uint32_t *pal); namespace color { diff --git a/source/mini/surf/surf.cpp b/source/mini/surf/surf.cpp index 063e65a..c0b3469 100644 --- a/source/mini/surf/surf.cpp +++ b/source/mini/surf/surf.cpp @@ -5,8 +5,7 @@ #include "aux/log.h" #include "mini/file/file.h" #include "mini/draw/draw.h" - -#include +#include "mini/win/win.h" #include @@ -17,6 +16,16 @@ namespace mini state_t state; + void init() { + target::set(create(win::state.width, win::state.height)); + state.dest_surface = state.screen_surface; + } + + void quit() { + for (unsigned int i=0;ip, col, state.dest_surface->size); + memset(state.dest_surface->p, col, state.dest_surface->size); } namespace target { diff --git a/source/mini/surf/surf.h b/source/mini/surf/surf.h index ff90297..90ce0a8 100644 --- a/source/mini/surf/surf.h +++ b/source/mini/surf/surf.h @@ -29,6 +29,9 @@ namespace mini }; extern state_t state; + void init(); + void quit(); + uint8_t create(int w, int h); uint8_t load(const char* filename, const bool external = false); uint8_t load(uint8_t* buffer, const char* name);