VERSIÓ 1.3.14

- [NEW] draw.pattern() sense paràmetres restableix el patró de relleno
- [FIX] Llevat el std::vector que estava donant pel cul. No, si ja sabia jo...
- [NEW] Gestió del cas en que es supere el nombre màxim de textures (en compte d'explotar, tira un error bonico)
This commit is contained in:
2025-12-04 11:35:34 +01:00
parent 3e524fd32d
commit 4858d94378
4 changed files with 50 additions and 28 deletions

25
lua.cpp
View File

@@ -16,19 +16,34 @@ extern "C" {
static int cpp_surf_new(lua_State *L) { static int cpp_surf_new(lua_State *L) {
int w = luaL_checknumber(L, 1); int w = luaL_checknumber(L, 1);
int h = luaL_checknumber(L, 2); int h = luaL_checknumber(L, 2);
lua_pushinteger(L, newsurf(w, h)); uint8_t s = newsurf(w, h);
if (s==255) {
luaL_error(L, "Error while creating new surface: Max surfaces reached");
return 0;
}
lua_pushinteger(L, s);
return 1; return 1;
} }
static int cpp_surf_load(lua_State *L) { static int cpp_surf_load(lua_State *L) {
const char* str = luaL_checkstring(L, 1); const char* str = luaL_checkstring(L, 1);
lua_pushinteger(L, loadsurf(str)); uint8_t s = loadsurf(str);
if (s==255) {
luaL_error(L, "Error while loading surface: Max surfaces reached");
return 0;
}
lua_pushinteger(L, s);
return 1; return 1;
} }
static int cpp_surf_loadex(lua_State *L) { static int cpp_surf_loadex(lua_State *L) {
const char* str = luaL_checkstring(L, 1); const char* str = luaL_checkstring(L, 1);
lua_pushinteger(L, loadsurf(str, true)); uint8_t s = loadsurf(str, true);
if (s==255) {
luaL_error(L, "Error while loading surface: Max surfaces reached");
return 0;
}
lua_pushinteger(L, s);
return 1; return 1;
} }
@@ -472,9 +487,13 @@ extern "C" {
} }
static int cpp_draw_pattern(lua_State *L) { static int cpp_draw_pattern(lua_State *L) {
if (lua_gettop(L) == 0) {
fillp(0xffff);
} else {
uint16_t pat = luaL_checkinteger(L, 1); uint16_t pat = luaL_checkinteger(L, 1);
bool transparent = lua_toboolean(L, 2); bool transparent = lua_toboolean(L, 2);
fillp(pat, transparent); fillp(pat, transparent);
}
return 0; return 0;
} }

View File

@@ -6,10 +6,10 @@
#include "gifenc.h" #include "gifenc.h"
#include "jail_audio.h" #include "jail_audio.h"
#include "jshader.h" #include "jshader.h"
#include <vector> //#include <vector>
#include "log.h" #include "log.h"
//#define MAX_TEXTURES 100 #define MAX_SURFACES 100
#ifdef MACOS_BUNDLE #ifdef MACOS_BUNDLE
#include <libgen.h> #include <libgen.h>
@@ -47,9 +47,9 @@ bool screen_cursor = true;
int desktop_width = 0; int desktop_width = 0;
int desktop_height = 0; int desktop_height = 0;
std::vector<surface_t> surfaces; surface_t surfaces[MAX_SURFACES];
surface_t *screen_surface = NULL; //&surfaces[0]; surface_t *screen_surface = &surfaces[0];
surface_t *dest_surface = NULL; //screen_surface; surface_t *dest_surface = screen_surface;
surface_t *source_surface = NULL; surface_t *source_surface = NULL;
surface_t *map_surface = NULL; surface_t *map_surface = NULL;
@@ -136,8 +136,8 @@ void read_ini() {
int size; int size;
FILE *f = file_getfilepointer("game.ini", size); // fopen("game.ini", "r"); FILE *f = file_getfilepointer("game.ini", size); // fopen("game.ini", "r");
char line[1024]; char line[1024];
if (f == NULL) { log_msg(LOG_FAIL, "No s'ha pogut obrir 'game.ini'"); exit(-1); } if (f == NULL) { log_msg(LOG_FAIL, "No s'ha pogut obrir 'game.ini'\n"); exit(-1); }
log_msg(LOG_OK, "'game.ini' carregat"); log_msg(LOG_OK, "'game.ini' carregat\n");
while (fgets(line, sizeof(line), f)) { while (fgets(line, sizeof(line), f)) {
char *value = get_value_from_line(line); char *value = get_value_from_line(line);
if (value != NULL) { if (value != NULL) {
@@ -183,13 +183,14 @@ void reinit() {
ds::trans=0; ds::trans=0;
ds::fill_pattern = 0b1111111111111111; ds::fill_pattern = 0b1111111111111111;
ds::fill_trans = false; ds::fill_trans = false;
for (unsigned int i=1; i<surfaces.size(); ++i) { for (unsigned int i=1; i<MAX_SURFACES; ++i) freesurf(i);
/*{
if (surfaces[i].p != NULL) free(surfaces[i].p); if (surfaces[i].p != NULL) free(surfaces[i].p);
surfaces[i].p = NULL; surfaces[i].p = NULL;
if (surfaces[i].name != NULL) free(surfaces[i].name); if (surfaces[i].name != NULL) free(surfaces[i].name);
surfaces[i].name = NULL; surfaces[i].name = NULL;
} }
surfaces.clear(); surfaces.clear();*/
dest_surface = screen_surface; dest_surface = screen_surface;
for (int i=0;i<256;++i) ds::draw_palette[i]=i; for (int i=0;i<256;++i) ds::draw_palette[i]=i;
@@ -218,8 +219,8 @@ int scrh() {
uint8_t newsurf(int w, int h) { uint8_t newsurf(int w, int h) {
unsigned int i = 0; unsigned int i = 0;
while (i<surfaces.size() && surfaces[i].p != NULL) ++i; while (i<MAX_SURFACES && surfaces[i].p != NULL) ++i;
if (i==surfaces.size()) surfaces.emplace_back(); if (i==MAX_SURFACES) return 255;
surfaces[i].name = nullptr; surfaces[i].name = nullptr;
surfaces[i].w = w; surfaces[i].w = w;
surfaces[i].h = h; surfaces[i].h = h;
@@ -231,12 +232,17 @@ uint8_t newsurf(int w, int h) {
uint8_t loadsurf(const char* filename, const bool external) { uint8_t loadsurf(const char* filename, const bool external) {
// Si el gif ja s'ha carregat en una textura, tornem eixa textura // Si el gif ja s'ha carregat en una textura, tornem eixa textura
for (unsigned int i=0; i<surfaces.size(); ++i) for (unsigned int i=0; i<MAX_SURFACES; ++i)
if (surfaces[i].name && strcmp(surfaces[i].name, filename)==0) { if (surfaces[i].name && strcmp(surfaces[i].name, filename)==0) {
log_msg(LOG_INFO, "Carrega de '%s' abortada: Reusant: %i.\n", filename, i); log_msg(LOG_INFO, "Carrega de '%s' abortada: Reusant: %i.\n", filename, i);
return i; return i;
} }
// Agafar la pròxima textura lliure
unsigned int i = 0;
while (i<MAX_SURFACES && surfaces[i].p != NULL) ++i;
if (i==MAX_SURFACES) return 255;
// Carregar l'arxiu de disc // Carregar l'arxiu de disc
int size; int size;
uint8_t *buffer; uint8_t *buffer;
@@ -253,11 +259,6 @@ uint8_t loadsurf(const char* filename, const bool external) {
return 255; return 255;
} }
// Agafar la pròxima textura lliure
unsigned int i = 0;
while (i<surfaces.size() && surfaces[i].p != NULL) ++i;
if (i==surfaces.size()) surfaces.emplace_back();
surfaces[i].p = LoadGif(buffer, &surfaces[i].w, &surfaces[i].h); surfaces[i].p = LoadGif(buffer, &surfaces[i].w, &surfaces[i].h);
surfaces[i].size = surfaces[i].w*surfaces[i].h; surfaces[i].size = surfaces[i].w*surfaces[i].h;
surfaces[i].name = (char*)malloc(strlen(filename)+1); surfaces[i].name = (char*)malloc(strlen(filename)+1);
@@ -318,7 +319,7 @@ void setmap(uint8_t surface) {
uint8_t getmap() uint8_t getmap()
{ {
for (unsigned int i=0; i<surfaces.size(); ++i) if (map_surface == &surfaces[i]) return i; for (unsigned int i=0; i<MAX_SURFACES; ++i) if (map_surface == &surfaces[i]) return i;
return 0; return 0;
} }
@@ -418,7 +419,7 @@ int main(int argc,char*argv[]){
} }
} }
screen_surface = &surfaces.emplace_back(); //screen_surface = &surfaces.emplace_back();
while (!should_quit) { while (!should_quit) {
should_exit=false; should_exit=false;
@@ -583,8 +584,7 @@ int main(int argc,char*argv[]){
//Mix_Quit(); //Mix_Quit();
for (unsigned int i=0;i<surfaces.size();++i) freesurf(i); for (unsigned int i=0;i<MAX_SURFACES;++i) freesurf(i);
surfaces.clear();
dest_surface = source_surface = map_surface = NULL; dest_surface = source_surface = map_surface = NULL;
destroyDisplay(); destroyDisplay();
SDL_Quit(); SDL_Quit();

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define MINI_VERSION "1.3.13" #define MINI_VERSION "1.3.14"

View File

@@ -252,6 +252,9 @@ function draw.ovalf(x1, y1, x2, y2, color) end
---Specify a pattern for the drawing functions ---Specify a pattern for the drawing functions
function draw.pattern(pattern) end function draw.pattern(pattern) end
---Reset to no pattern for drawing functions
function draw.pattern() end
---@param sx number ---@param sx number
---@param sy number ---@param sy number
---@param sw number ---@param sw number