diff --git a/jail_audio.cpp b/jail_audio.cpp index 7f8c784..42f3a56 100644 --- a/jail_audio.cpp +++ b/jail_audio.cpp @@ -3,6 +3,7 @@ #include "stb_vorbis.h" #include #include +#include "log.h" #define JA_MAX_SIMULTANEOUS_CHANNELS 5 @@ -146,14 +147,13 @@ void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG); #endif - SDL_Log("Iniciant JailAudio..."); JA_audioSpec = {format, channels, freq }; if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec); if (!sdlAudioDevice) { - SDL_Log("Failed to initialize SDL audio: %s\n", SDL_GetError()); + log_msg(LOG_FAIL, "Failed to initialize SDL audio: %s\n", SDL_GetError()); } else { - SDL_Log( "JailAudio OK!\n"); + log_msg(LOG_OK, "Audio subsytem initialized\n"); } //SDL_PauseAudioDevice(sdlAudioDevice); JA_timerID = SDL_AddTimer(30, JA_UpdateCallback, nullptr); @@ -217,9 +217,9 @@ void JA_PlayMusic(JA_Music_t *music, const int loop) current_music->times = loop; current_music->stream = SDL_CreateAudioStream(¤t_music->spec, &JA_audioSpec); - if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) printf("[ERROR] SDL_PutAudioStreamData failed!\n"); + if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) log_msg(LOG_FAIL, "SDL_PutAudioStreamData failed!\n"); SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); - if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n"); + if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) log_msg(LOG_FAIL, "SDL_BindAudioStream failed!\n"); //SDL_ResumeAudioStreamDevice(current_music->stream); } diff --git a/jfile.cpp b/jfile.cpp index 9c8dfaa..334cf1d 100644 --- a/jfile.cpp +++ b/jfile.cpp @@ -271,3 +271,10 @@ void file_setconfigvalue(const char* key, const char* value) { file_saveconfigvalues(); return; } + +bool file_createFolder(const char* name) { + char tmp[256]; + strcpy(tmp, "./"); + strcat(tmp, name); + return mkdir(tmp, 0755)==0; +} \ No newline at end of file diff --git a/jfile.h b/jfile.h index 7dfa6e4..be55c8d 100644 --- a/jfile.h +++ b/jfile.h @@ -19,3 +19,5 @@ char *file_getfilebufferex(const char *filename, int& filesize, const bool zero_ const char* file_getconfigvalue(const char *key); void file_setconfigvalue(const char* key, const char* value); + +bool file_createFolder(const char* name); diff --git a/jshader.cpp b/jshader.cpp index 4cd5e38..115f91a 100644 --- a/jshader.cpp +++ b/jshader.cpp @@ -150,13 +150,13 @@ namespace shader shader::backBuffer = backBuffer; SDL_GetWindowSize(win, &win_size.x, &win_size.y); SDL_GetTextureSize(backBuffer, &tex_size.x, &tex_size.y); - printf("tex size: %fx%f\n", tex_size.x, tex_size.y); + //printf("tex size: %fx%f\n", tex_size.x, tex_size.y); SDL_PropertiesID props = SDL_GetTextureProperties(backBuffer); texture_number = SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER, -1); - printf("texture number: %i\n", texture_number); + //printf("texture number: %i\n", texture_number); int access = SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_ACCESS_NUMBER, -1); nose = SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_OPENGL_TEXTURE_TARGET_NUMBER, -1); - printf("texture target number: %i\n", nose); + //printf("texture target number: %i\n", nose); if (access != SDL_TEXTUREACCESS_TARGET) { @@ -165,7 +165,7 @@ namespace shader } const char * renderer_name = SDL_GetRendererName(renderer); - printf("rendererInfo.name: %s\n", renderer_name); + //printf("rendererInfo.name: %s\n", renderer_name); if(!strncmp(renderer_name, "opengl", 6)) { #ifndef __APPLE__ diff --git a/log.h b/log.h new file mode 100644 index 0000000..1daa14d --- /dev/null +++ b/log.h @@ -0,0 +1,27 @@ +#pragma once +#include +#include + +#ifdef DEBUG +enum LogLevel { LOG_OK, LOG_FAIL, LOG_WARN, LOG_INFO, LOG_LUART, LOG_LUALD, LOG_VERBOSE, LOG_UNSALTED }; + +static inline void log_msg(enum LogLevel level, const char *fmt, ...) { + va_list args; + va_start(args, fmt); + + switch (level) { + case LOG_OK: printf("[\033[1;32m OK \033[0m] "); break; + case LOG_FAIL: printf("[\033[1;31mFAIL\033[0m] "); break; + case LOG_WARN: printf("[\033[1;33mWARN\033[0m] "); break; + case LOG_INFO: printf("[\033[1;34mINFO\033[0m] "); break; + case LOG_LUART: printf("[\033[1;35mLUA RUNTIME ERROR\033[0m] "); break; + case LOG_LUALD: printf("[\033[1;35mLUA LOADING ERROR\033[0m] "); break; + case LOG_VERBOSE: printf(" - "); break; + case LOG_UNSALTED: break; + } + vprintf(fmt, args); + va_end(args); +} +#else +#define log_msg(...) ((void)0) +#endif \ No newline at end of file diff --git a/lua.cpp b/lua.cpp index 2a31630..53f2f3c 100644 --- a/lua.cpp +++ b/lua.cpp @@ -2,7 +2,7 @@ #include "lua/lua.hpp" #include "mini.h" #include "jfile.h" - +#include "log.h" #include namespace fs = std::filesystem; @@ -1181,8 +1181,7 @@ int MiniLoader(lua_State *L) { int size; char* buffer = file_getfilebuffer(filename, size); if (luaL_loadbuffer(L, buffer, size, filename)) { - debug("REQUIRE LOADING ERROR: "); - debug("%s\n",lua_tostring(L, -1)); + log_msg(LOG_LUALD, "%s\n",lua_tostring(L, -1)); lua_pop(L,1); //return ; } @@ -1200,19 +1199,14 @@ void lua_init(const char *main_lua_file) { int size; char* buffer = file_getfilebuffer(main_lua_file, size); if (luaL_loadbuffer(L, buffer, size, "main")) { - debug("LOADING ERROR: "); - debug("%s\n",lua_tostring(L, -1)); + log_msg(LOG_LUALD, "%s\n", lua_tostring(L, -1)); lua_pop(L,1); return; } free(buffer); if (lua_pcall(L,0, LUA_MULTRET, 0)) { - debug("RUNTIME ERROR [BOOT]: "); - //debug("%s\n",lua_tostring(L, -1)); - //lua_pop(L,1); - - luaL_traceback(L, L, NULL, 1); - debug("%s\n", lua_tostring(L, -1)); + //luaL_traceback(L, L, NULL, 1); + log_msg(LOG_LUART, "%s\n", lua_tostring(L, -1)); lua_pop(L,1); return; } @@ -1238,11 +1232,7 @@ void lua_call_init() { lua_getglobal(L, "mini"); lua_getfield(L, -1, "init"); if (lua_pcall(L, 0, 0, 0)) { - debug("RUNTIME ERROR [INIT]: "); - debug("%s\n",lua_tostring(L, -1)); - lua_pop(L,1); - luaL_traceback(L, L, NULL, 1); - debug("%s\n", lua_tostring(L, -1)); + log_msg(LOG_LUART, "%s\n", lua_tostring(L, -1)); lua_pop(L,1); is_playing = false; } @@ -1255,11 +1245,7 @@ void lua_call_update() { lua_getglobal(L, "mini"); lua_getfield(L, -1, "update"); if (lua_pcall(L, 0, 0, 0)) { - debug("RUNTIME ERROR [UPDATE]: "); - debug("%s\n",lua_tostring(L, -1)); - lua_pop(L,1); - luaL_traceback(L, L, NULL, 1); - debug("%s\n", lua_tostring(L, -1)); + log_msg(LOG_LUART, "%s\n", lua_tostring(L, -1)); lua_pop(L,1); is_playing = false; } diff --git a/mini.cpp b/mini.cpp index 46dbb7a..1f029d7 100644 --- a/mini.cpp +++ b/mini.cpp @@ -7,6 +7,7 @@ #include "jail_audio.h" #include "jshader.h" #include +#include "log.h" //#define MAX_TEXTURES 100 @@ -47,8 +48,8 @@ int desktop_width = 0; int desktop_height = 0; std::vector 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; i1) { - 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 (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"); +} diff --git a/mini.h b/mini.h index 1aa2636..16f388d 100644 --- a/mini.h +++ b/mini.h @@ -233,8 +233,6 @@ bool beat(int16_t i); int rnd(int x); int getfps(); -#define debug printf - void playmusic(const char *filename, const int loop=-1); void pausemusic(); void resumemusic(); diff --git a/version.h b/version.h index e6b07d6..d6c6f6c 100644 --- a/version.h +++ b/version.h @@ -1,3 +1,3 @@ #pragma once -#define MINI_VERSION "1.3.12" +#define MINI_VERSION "1.3.13"