VERSIÓ 1.3.13

- [NEW]  Executant la versió de debug amb el paràmeter "--new" crea un projecte nou en eixe directori.
- [NEW] Nou sistema de log
- [FIX] Amb el tema de usar std::vector no s'estava inicialitzant la surface de pantalla correctament.
- [FIX] Proteccions per a que no pete quan s'intenta usar funcions que els fa falta una surface de oritge, pero no hi ha ninguna seleccionada.
- [NEW] file_createFolder() (ATENCIÓ: No funcionarà en Windows encara)
This commit is contained in:
2025-12-03 14:05:52 +01:00
parent ace4a0f9f0
commit 3e524fd32d
9 changed files with 133 additions and 69 deletions

116
mini.cpp
View File

@@ -7,6 +7,7 @@
#include "jail_audio.h"
#include "jshader.h"
#include <vector>
#include "log.h"
//#define MAX_TEXTURES 100
@@ -47,8 +48,8 @@ int desktop_width = 0;
int desktop_height = 0;
std::vector<surface_t> surfaces;
surface_t *screen_surface = &surfaces[0];
surface_t *dest_surface = screen_surface;
surface_t *screen_surface = NULL; //&surfaces[0];
surface_t *dest_surface = NULL; //screen_surface;
surface_t *source_surface = NULL;
surface_t *map_surface = NULL;
@@ -122,6 +123,8 @@ JA_Sound_t *sounds[MAX_SOUNDS];
int16_t beats, num_beats = 0;
void createNewProject();
char* get_value_from_line(char* line) {
char* equal_character = strchr(line, '=');
if (equal_character == NULL) return NULL;
@@ -131,21 +134,20 @@ char* get_value_from_line(char* line) {
void read_ini() {
int size;
SDL_Log("Carregant 'game.ini'...");
FILE *f = file_getfilepointer("game.ini", size); // fopen("game.ini", "r");
char line[1024];
if (f == NULL) { SDL_Log("FAIL!\n"); return; }
SDL_Log("OK!\n");
if (f == NULL) { log_msg(LOG_FAIL, "No s'ha pogut obrir 'game.ini'"); exit(-1); }
log_msg(LOG_OK, "'game.ini' carregat");
while (fgets(line, sizeof(line), f)) {
char *value = get_value_from_line(line);
if (value != NULL) {
value[strlen(value)-1] = '\0';
if (strcmp(line, "title") == 0) { strcpy(window_title, value); SDL_Log("-title=%s\n", window_title); }
else if (strcmp(line, "config") == 0) { strcpy(config_folder, value); SDL_Log("-config=%s\n", config_folder); }
else if (strcmp(line, "width") == 0) { screen_width = atoi(value); SDL_Log("-screen width=%i\n", screen_width); }
else if (strcmp(line, "height") == 0) { screen_height = atoi(value); SDL_Log("-screen height=%i\n", screen_height); }
else if (strcmp(line, "zoom") == 0) { screen_zoom = atoi(value); SDL_Log("-screen zoom=%i\n", screen_zoom); }
else if (strcmp(line, "fullscreen") == 0) { screen_fullscreen = atoi(value); SDL_Log("-screen sullscreen=%i\n", screen_fullscreen); }
if (strcmp(line, "title") == 0) { strcpy(window_title, value); log_msg(LOG_VERBOSE, "title = %s\n", window_title); }
else if (strcmp(line, "config") == 0) { strcpy(config_folder, value); log_msg(LOG_VERBOSE, "config = %s\n", config_folder); }
else if (strcmp(line, "width") == 0) { screen_width = atoi(value); log_msg(LOG_VERBOSE, "screen width = %i\n", screen_width); }
else if (strcmp(line, "height") == 0) { screen_height = atoi(value); log_msg(LOG_VERBOSE, "screen height = %i\n", screen_height); }
else if (strcmp(line, "zoom") == 0) { screen_zoom = atoi(value); log_msg(LOG_VERBOSE, "screen zoom = %i\n", screen_zoom); }
else if (strcmp(line, "fullscreen") == 0) { screen_fullscreen = atoi(value); log_msg(LOG_VERBOSE, "screen sullscreen = %i\n", screen_fullscreen); }
//else if (strcmp(line, "files") == 0) {
//lua_files = (char*)malloc(strlen(value));
// strcpy(lua_files, value);
@@ -153,7 +155,7 @@ void read_ini() {
}
}
fclose(f);
SDL_Log("'game.ini' carregat!\n");
//SDL_Log("'game.ini' carregat!\n");
}
void pset_fast(int x, int y) {
@@ -171,7 +173,7 @@ void pset_pattern(int x, int y) {
}
void reinit() {
SDL_Log("REINIT\n");
log_msg(LOG_INFO, "STARTING A SYSTEM REINITIALIZATION\n");
do_pset = pset_fast;
ds::pen_color = 6;
ds::back_color = 0;
@@ -181,13 +183,14 @@ void reinit() {
ds::trans=0;
ds::fill_pattern = 0b1111111111111111;
ds::fill_trans = false;
for (int i=1; i<surfaces.size(); ++i) {
for (unsigned int i=1; i<surfaces.size(); ++i) {
if (surfaces[i].p != NULL) free(surfaces[i].p);
surfaces[i].p = NULL;
if (surfaces[i].name != NULL) free(surfaces[i].name);
surfaces[i].name = NULL;
}
surfaces.clear();
dest_surface = screen_surface;
for (int i=0;i<256;++i) ds::draw_palette[i]=i;
if (file!=NULL) fclose(file);
@@ -214,23 +217,23 @@ int scrh() {
}
uint8_t newsurf(int w, int h) {
int i = 0;
unsigned int i = 0;
while (i<surfaces.size() && surfaces[i].p != NULL) ++i;
if (i==i<surfaces.size()) surfaces.emplace_back();
if (i==surfaces.size()) surfaces.emplace_back();
surfaces[i].name = nullptr;
surfaces[i].w = w;
surfaces[i].h = h;
surfaces[i].size = w*h;
surfaces[i].p = (uint8_t*)calloc(surfaces[i].size,1);
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Surface %i creada.\n", i);
log_msg(LOG_INFO, "Surface %i creada.\n", i);
return i;
}
uint8_t loadsurf(const char* filename, const bool external) {
// Si el gif ja s'ha carregat en una textura, tornem eixa textura
for (int i=0; i<surfaces.size(); ++i)
for (unsigned int i=0; i<surfaces.size(); ++i)
if (surfaces[i].name && strcmp(surfaces[i].name, filename)==0) {
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Carrega de '%s' abortada: Reusant: %i.\n", filename, i);
log_msg(LOG_INFO, "Carrega de '%s' abortada: Reusant: %i.\n", filename, i);
return i;
}
@@ -245,14 +248,15 @@ uint8_t loadsurf(const char* filename, const bool external) {
// Si no s'ha pogut, petar
if (!buffer) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al intentar obrir l'arxiu '%s'\n", filename);
log_msg(LOG_FAIL, "Error al intentar obrir l'arxiu '%s'\n", filename);
exit(-1);
return 255;
}
// Agafar la pròxima textura lliure
int i = 0;
unsigned int i = 0;
while (i<surfaces.size() && surfaces[i].p != NULL) ++i;
if (i==i<surfaces.size()) surfaces.emplace_back();
if (i==surfaces.size()) surfaces.emplace_back();
surfaces[i].p = LoadGif(buffer, &surfaces[i].w, &surfaces[i].h);
surfaces[i].size = surfaces[i].w*surfaces[i].h;
@@ -260,7 +264,7 @@ uint8_t loadsurf(const char* filename, const bool external) {
strcpy(surfaces[i].name, filename);
free(buffer);
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Carregat '%s': Surface: %i.\n", filename, i);
log_msg(LOG_INFO, "Arxiu '%s' carregat en surface: %i.\n", filename, i);
return i;
}
@@ -274,7 +278,7 @@ void freesurf(uint8_t surface) {
surfaces[surface].name = NULL;
if (surfaces[surface].p != NULL) {
free(surfaces[surface].p);
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Surface %i alliberada.\n", surface);
log_msg(LOG_INFO, "Surface %i alliberada.\n", surface);
}
surfaces[surface].p = NULL;
}
@@ -314,7 +318,7 @@ void setmap(uint8_t surface) {
uint8_t getmap()
{
for (int i=0; i<surfaces.size(); ++i) if (map_surface == &surfaces[i]) return i;
for (unsigned int i=0; i<surfaces.size(); ++i) if (map_surface == &surfaces[i]) return i;
return 0;
}
@@ -347,7 +351,7 @@ void createDisplay() {
SDL_PropertiesID props = SDL_GetTextureProperties(mini_bak);
int real_pixelformat = SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_FORMAT_NUMBER, -1);
if (real_pixelformat != SDL_PIXELFORMAT_ARGB8888) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Pixelformat incorrecte: %i\n", real_pixelformat);
log_msg(LOG_FAIL, "Pixelformat incorrecte: %i\n", real_pixelformat);
exit(1);
}
mini_shadertex = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, screen_width*screen_zoom, screen_height*screen_zoom);
@@ -357,6 +361,7 @@ void createDisplay() {
//char *shaderfile = file_getfilebuffer("lynx.glsl", filesize);
shader::init(mini_win, mini_shadertex, nullptr);
//SDL_GetWindowPosition(mini_win, &windowpos_x, &windowpos_y);
log_msg(LOG_OK, "Graphics subsystem initialized\n");
}
void destroyDisplay() {
@@ -374,6 +379,7 @@ void initGamePad() {
gamepad = SDL_OpenGamepad(joysticks[i]);
if (SDL_GamepadConnected(gamepad)) {
SDL_SetGamepadEventsEnabled(true);
log_msg(LOG_OK, "Gamepad found and initialized");
return;
}
}
@@ -387,9 +393,19 @@ int main(int argc,char*argv[]){
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG);
#endif
#ifdef DEBUG
log_msg(LOG_UNSALTED, "MINI v%s\n",MINI_VERSION);
#endif
if (argc>1)
{
if (strstr(argv[1], ".lua")!=nullptr) {
if (argv[1][0]=='-' && argv[1][1]=='-') {
const char *command = &argv[1][2];
if (strcmp(command, "new")==0) {
createNewProject();
exit(0);
}
} else if (strstr(argv[1], ".lua")!=nullptr) {
file_setresourcefolder("./");
file_setsource(SOURCE_FOLDER);
strcpy(main_lua_file, argv[1]);
@@ -402,6 +418,7 @@ int main(int argc,char*argv[]){
}
}
screen_surface = &surfaces.emplace_back();
while (!should_quit) {
should_exit=false;
@@ -440,11 +457,6 @@ int main(int argc,char*argv[]){
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD);
const SDL_DisplayMode *dm = SDL_GetDesktopDisplayMode(SDL_GetPrimaryDisplay());
if (!dm)
{
SDL_Log("SDL_GetDesktopDisplayMode failed: %s", SDL_GetError());
return 1;
}
desktop_width = dm->w;
desktop_height = dm->h;
@@ -458,9 +470,7 @@ int main(int argc,char*argv[]){
reinit();
initaudio();
#ifdef DEBUG
debug("MINI v%s\n",MINI_VERSION);
#endif
lua_init(main_lua_file);
lua_call_init();
@@ -573,7 +583,7 @@ int main(int argc,char*argv[]){
//Mix_Quit();
for (int i=0;i<surfaces.size();++i) freesurf(i);
for (unsigned int i=0;i<surfaces.size();++i) freesurf(i);
surfaces.clear();
dest_surface = source_surface = map_surface = NULL;
destroyDisplay();
@@ -1052,11 +1062,13 @@ void ovalfill(int x0, int y0, int x1, int y1, uint8_t color) {
}
uint8_t sget(int x, int y) {
if (!source_surface) return 0;
if (x < 0 || x > (source_surface->w-1) || y < 0 || y > (source_surface->h-1)) return 0;
return SOURCE(x, y);
}
void sset(int x, int y) {
if (!source_surface) return;
if (x < 0 || x > (source_surface->w-1) || y < 0 || y > (source_surface->h-1)) return;
SOURCE(x, y) = ds::pen_color;
}
@@ -1067,6 +1079,7 @@ void sset(int x, int y, uint8_t color) {
}
void spr(uint8_t n, int x, int y, float w, float h, bool flip_x, bool flip_y) {
if (!source_surface) return;
int tx = (n%(source_surface->w >> 3))<<3;
int ty = (n/(source_surface->w >> 3))<<3;
int tw = w*8 - 1;
@@ -1469,3 +1482,34 @@ void exit() {
should_exit = true;
should_quit = true;
}
void createNewProject() {
if (file_createFolder("data")) {
log_msg(LOG_OK, "Directori 'data' creat\n");
} else {
log_msg(LOG_FAIL, "No s'ha pogut crear la carpeta 'data'\n");
exit(-1);
}
FILE *f = fopen("./data/game.ini", "w");
if (f) {
log_msg(LOG_OK, "Arxiu 'data/game.ini' creat\n");
} else {
log_msg(LOG_FAIL, "No s'ha pogut crear l'arxiu 'data/game.ini'\n");
exit(-1);
}
fprintf(f, "title=NEW MINI PROJECT\nconfig=newminiproject\nwidth=320\nheight=240\nzoom=2\n");
fclose(f);
f = fopen("./data/main.lua", "w");
if (f) {
log_msg(LOG_OK, "Arxiu 'data/main.lua' creat\n");
} else {
log_msg(LOG_FAIL, "No s'ha pogut crear l'arxiu 'data/main.lua'\n");
exit(-1);
}
fprintf(f, "function mini.init()\n\nend\n\nfunction mini.update()\n surf.cls(0)\nend\n");
fclose(f);
log_msg(LOG_OK, "Projecte nou creat. Ja està fet lo més dificil del jailgame!\n");
}