From 38ca5f45faa55942803ca1fa9d929e2b8ee0d56f Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Fri, 1 Oct 2021 00:27:53 +0200 Subject: [PATCH] Add settings save/load --- cgdoom/cgdoom-kbd.c | 140 ++++++++++++++++++++++++++++++++++++++++---- cgdoom/cgdoom-kbd.h | 54 +++++++++++++---- cgdoom/cgdoom-ui.c | 41 +------------ cgdoom/cgdoom.c | 113 ++++++++++++++++++++++++++++++++--- cgdoom/cgdoom.h | 13 +--- cgdoom/g_game.c | 3 +- cgdoom/i_system.c | 4 ++ cgdoom/m_misc.c | 46 +++++++++++++-- cgdoom/m_misc.h | 2 +- src-sdl2/emul.c | 20 +++---- 10 files changed, 340 insertions(+), 96 deletions(-) diff --git a/cgdoom/cgdoom-kbd.c b/cgdoom/cgdoom-kbd.c index e1f382c..d58e050 100644 --- a/cgdoom/cgdoom-kbd.c +++ b/cgdoom/cgdoom-kbd.c @@ -252,31 +252,149 @@ int KeyWasJustReleased(int key) return KeyDown(st_prev, key) && !KeyDown(st_now, key); } -static const char * const keynames[] = { +//--- +// Key names and identification +//--- + +static const char * const display_keynames[] = { "F1", "F2", "F3", "F4", "F5", "F6", "SHIFT", "OPTN", "VARS", "MENU", "Left", "Up", "ALPHA", "x^2", "^", "EXIT", "Down", "Right", "X,O,T", "log", "ln", "sin", "cos", "tan", "Frac", "S<>D", "(", ")", ",", "->", - "7", "8", "9", "DEL", NULL, NULL, + "7", "8", "9", "DEL", "AC/ON", "(None)", "4", "5", "6", "x", "/", NULL, "1", "2", "3", "+", "-", NULL, "0", ".", "x10^x", "(-)", "EXE", NULL, }; +static const char * const technical_keynames[] = { + "F1", "F2", "F3", "F4", "F5", "F6", + "SHIFT", "OPTN", "VARS", "MENU", "LEFT", "UP", + "ALPHA", "SQUARE", "POWER", "EXIT", "DOWN", "RIGHT", + "XOT", "LOG", "LN", "SIN", "COS", "TAN", + "FRAC", "FD", "LEFTP", "RIGHTP", "COMMA", "ARROW", + "7", "8", "9", "DEL", "ACON", "NONE", + "4", "5", "6", "MUL", "DIV", NULL, + "1", "2", "3", "PLUS", "MINUS", NULL, + "0", "DOT", "EXP", "NEG", "EXE", NULL, +}; -const char *CGD_KeyName(int keycode) +static const char *KeyName(int key, const char * const *names) { - if(keycode < 0) - return "(None)"; - if(keycode == KEYCODE_ACON) - return "AC/ON"; + if(key == KEYCODE_ACON) + key = 0x45; + if(key < 0) + key = 0x46; - int row = 9 - (keycode >> 4); - int col = (keycode & 0xf) - 1; - - return keynames[6*row + col]; + int row = 9 - (key >> 4); + int col = (key & 0xf) - 1; + return names[6*row + col]; } +const char *CGD_PhysicalKey_DisplayName(int key) +{ + return KeyName(key, display_keynames); +} + +const char *CGD_PhysicalKey_TechnicalName(int key) +{ + return KeyName(key, technical_keynames); +} + +int CGD_PhysicalKey_FromTechnicalName(const char *name, int length) +{ + int i, n = sizeof(technical_keynames) / sizeof(technical_keynames[0]); + if(length < 0) + length = strlen(name); + + for(i = 0; i < n; i++) { + const char *t = technical_keynames[i]; + if(t && !strncmp(name, t, length)) break; + } + + int row = 9 - (i / 6); + int col = (i % 6) + 1; + int key = (row << 4) | col; + + if(i >= n || key == 0x46) + key = -1; + if(key == 0x45) + key = KEYCODE_ACON; + + return key; +} + +struct DoomkeyInfo { + int key; + const char *technical_name; + const char *display_name; +}; + +static const struct DoomkeyInfo doomkeys[] = { + { KEY_LEFTARROW, "Left", "Look left" }, + { KEY_RIGHTARROW, "Right", "Look right" }, + { KEY_UPARROW, "Up", "Move forward" }, + { KEY_DOWNARROW, "Down", "Move backward" }, + { ' ', "Inteact", "Interact" }, + { KEY_RCTRL, "Shoot", "Shoot" }, + { '1', "Weapon1", "1-Hand" }, + { '2', "Weapon2", "2-Pistol" }, + { '3', "Weapon3", "3-Shotgun" }, + { '4', "Weapon4", "4-Chaingun" }, + { '5', "Weapon5", "5-Rockets" }, + { '6', "Weapon6", "6-Plasma" }, + { '7', "Weapon7", "7-BFG9000" }, + { KEY_TAB, "Map", "Map" }, + { KEY_PAUSE, "Pause", "Pause" }, + { KEY_SLEFTARROW, "StrafeLeft", "Strafe left" }, + { KEY_SRIGHTARROW, "StrafeRight", "Strafe right" }, + { KEY_RSHIFT, "Run", "Toggle run" }, + { SKEY_DECVP, "DecreaseViewport", "Smaller view" }, + { SKEY_INCVP, "IncreaseViewport", "Larger view" }, + { SKEY_CHEAT, "Cheat", "Cheat" }, + { SKEY_NOCLIP, "ToggleNoclip", "Noclip" }, + { SKEY_GAMMA, "GammaCorrection", "Brightness" }, + { SKEY_FREEMEM, "HeapStatistics", "Heap stats" }, + { SKEY_FPSCOUNTER, "FPSCounter", "FPS counter" }, + { SKEY_FRAMESKIP, "CycleFrameskip", "Frameskip" }, + { SKEY_PROFILER, "Profiler", "Run profiler" }, + { -1, NULL, NULL }, +}; + +const char *CGD_DoomKey_DisplayName(int key) +{ + for(int i = 0; doomkeys[i].key >= 0; i++) { + if(doomkeys[i].key == key) + return doomkeys[i].display_name; + } + return NULL; +} + +const char *CGD_DoomKey_TechnicalName(int key) +{ + for(int i = 0; doomkeys[i].key >= 0; i++) { + if(doomkeys[i].key == key) + return doomkeys[i].technical_name; + } + return NULL; +} + +int CGD_DoomKey_FromTechnicalName(const char *name, int length) +{ + if(length < 0) + length = strlen(name); + + for(int i = 0; doomkeys[i].key >= 0; i++) { + if(!strncmp(doomkeys[i].technical_name, name, length)) + return doomkeys[i].key; + } + return -1; +} + +//--- +// Direct keyboard functions +//--- + int CGD_PRGM_Getkey(void) { CGD_KeyboardState st; diff --git a/cgdoom/cgdoom-kbd.h b/cgdoom/cgdoom-kbd.h index 9404d99..6a7d422 100644 --- a/cgdoom/cgdoom-kbd.h +++ b/cgdoom/cgdoom-kbd.h @@ -1,12 +1,17 @@ #ifndef CGDOOM_KBD_H #define CGDOOM_KBD_H -/* CGDoom's keyboard abstraction +/* CGDoom's keyboard driver and control settings Instead of using libfxcg functions which only provide single-key analysis (GetKey, GetKeyWait, and PRGM_GetKey mainly), CGDoom uses a simple KEYSC driver adapter from gint's. The following matrix codes identify all keys on - the keyboard. The PC build maps a subset of them to PC keys. */ + the keyboard. The PC build maps a subset of them to PC keys. + + CGDoom also defines symbolic keys other than Doom's. The particular encoding + used by Doom for keys is quite unclear to me (ASCII characters and the like) + and not necessarily suitable for the calculator, so this module also handles + input configuration. */ /* Copy of the keyboard state, one byte per row */ typedef uint8_t CGD_KeyboardState[12] __attribute__((aligned(2))); @@ -31,66 +36,91 @@ const char *CGD_KeyName(int keycode); /* PRGM_Getkey() but with the direct driver */ int CGD_PRGM_Getkey(void); -/* Keyboard matrix codes for the keyboard driver */ +//--- +// Doom keys +//--- +#include "doomdef.h" + +/* Key extensions for cheats, debugs, performance, etc. */ +#define SKEY_CHEAT 0x101 +#define SKEY_DECVP 0x102 +#define SKEY_INCVP 0x103 +#define SKEY_NOCLIP 0x104 +#define SKEY_GAMMA 0x105 +#define SKEY_FREEMEM 0x106 +#define SKEY_FPSCOUNTER 0x107 +#define SKEY_FRAMESKIP 0x108 +#define SKEY_PROFILER 0x109 + +/* Display name for a key (eg. "Strafe left") */ +const char *CGD_DoomKey_DisplayName(int key); +/* Technical name for a key (eg. "STRAFE_LEFT") */ +const char *CGD_DoomKey_TechnicalName(int key); +/* Key from technical name */ +int CGD_DoomKey_FromTechnicalName(const char *technical_name, int length); + +//--- +// Physical keys +//--- + +/* Matrix codes generated by the keyboard driver. */ #define KEYCODE_F1 0x91 #define KEYCODE_F2 0x92 #define KEYCODE_F3 0x93 #define KEYCODE_F4 0x94 #define KEYCODE_F5 0x95 #define KEYCODE_F6 0x96 - #define KEYCODE_SHIFT 0x81 #define KEYCODE_OPTN 0x82 #define KEYCODE_VARS 0x83 #define KEYCODE_MENU 0x84 #define KEYCODE_LEFT 0x85 #define KEYCODE_UP 0x86 - #define KEYCODE_ALPHA 0x71 #define KEYCODE_SQUARE 0x72 #define KEYCODE_POWER 0x73 #define KEYCODE_EXIT 0x74 #define KEYCODE_DOWN 0x75 #define KEYCODE_RIGHT 0x76 - #define KEYCODE_XOT 0x61 #define KEYCODE_LOG 0x62 #define KEYCODE_LN 0x63 #define KEYCODE_SIN 0x64 #define KEYCODE_COS 0x65 #define KEYCODE_TAN 0x66 - #define KEYCODE_FRAC 0x51 #define KEYCODE_FD 0x52 #define KEYCODE_LEFTP 0x53 #define KEYCODE_RIGHTP 0x54 #define KEYCODE_COMMA 0x55 #define KEYCODE_ARROW 0x56 - #define KEYCODE_7 0x41 #define KEYCODE_8 0x42 #define KEYCODE_9 0x43 #define KEYCODE_DEL 0x44 - #define KEYCODE_4 0x31 #define KEYCODE_5 0x32 #define KEYCODE_6 0x33 #define KEYCODE_MUL 0x34 #define KEYCODE_DIV 0x35 - #define KEYCODE_1 0x21 #define KEYCODE_2 0x22 #define KEYCODE_3 0x23 #define KEYCODE_PLUS 0x24 #define KEYCODE_MINUS 0x25 - #define KEYCODE_0 0x11 #define KEYCODE_DOT 0x12 #define KEYCODE_EXP 0x13 #define KEYCODE_NEG 0x14 #define KEYCODE_EXE 0x15 - #define KEYCODE_ACON 0x07 +/* Display name for a key (eg. "x^2") */ +const char *CGD_PhysicalKey_DisplayName(int key); +/* Technical name for a key (eg. "SQUARE") */ +const char *CGD_PhysicalKey_TechnicalName(int key); +/* Key from technical name */ +int CGD_PhysicalKey_FromTechnicalName(const char *technical_name, int length); + #endif /* CGDOOM_KBD_H */ diff --git a/cgdoom/cgdoom-ui.c b/cgdoom/cgdoom-ui.c index 8836fbe..d48efb3 100644 --- a/cgdoom/cgdoom-ui.c +++ b/cgdoom/cgdoom-ui.c @@ -429,43 +429,6 @@ void UI_AdvancedOptions(int *dev_info, int *use_mmap, int *trustunaligned) } } -static const char *UI_ControlName(int key) -{ - switch(key) - { - case KEY_LEFTARROW: return "Look left"; - case KEY_RIGHTARROW: return "Look right"; - case KEY_UPARROW: return "Move forward"; - case KEY_DOWNARROW: return "Move backward"; - case ' ': return "Interact"; - case KEY_RCTRL: return "Shoot"; - case '1': return "1-Hand"; - case '2': return "2-Pistol"; - case '3': return "3-Shotgun"; - case '4': return "4-Chaingun"; - case '5': return "5-Rockets"; - case '6': return "6-Plasma"; - case '7': return "7-BFG9000"; - case KEY_TAB: return "Map"; - case KEY_PAUSE: return "Pause"; - case KEY_SLEFTARROW: return "Strafe left"; - case KEY_SRIGHTARROW: return "Strafe right"; - case KEY_RSHIFT: return "Toggle run"; - - case SKEY_DECVP: return "Smaller view"; - case SKEY_INCVP: return "Larger view"; - case SKEY_CHEAT: return "Cheat"; - case SKEY_NOCLIP: return "Noclip"; - case SKEY_GAMMA: return "Brightness"; - case SKEY_FREEMEM: return "Heap stats"; - case SKEY_FPSCOUNTER: return "FPS counter"; - case SKEY_FRAMESKIP: return "Frameskip"; - case SKEY_PROFILER: return "Run profiler"; - - default: return "???"; - } -} - void UI_Controls(void) { int focus_x=0, focus_y=-1; @@ -501,9 +464,9 @@ void UI_Controls(void) } else { UI_Printf(x1, y, 0x0000, UI_LEFT, "%s", - UI_ControlName(CGD_Keymap[2*i])); + CGD_DoomKey_DisplayName(CGD_Keymap[2*i])); UI_Printf(x2, y, 0x0000, UI_RIGHT, "%s", - CGD_KeyName(CGD_Keymap[2*i+1])); + CGD_PhysicalKey_DisplayName(CGD_Keymap[2*i+1])); } if(focus_y >= 0 && 14 * focus_x + focus_y == i) diff --git a/cgdoom/cgdoom.c b/cgdoom/cgdoom.c index b4e68df..501bc96 100644 --- a/cgdoom/cgdoom.c +++ b/cgdoom/cgdoom.c @@ -7,6 +7,7 @@ #include "doomtype.h" #include "m_misc.h" #include "d_main.h" +#include "v_video.h" #include "cgdoom-alloc.h" #include "cgdoom-kbd.h" @@ -466,6 +467,96 @@ static void DelayedWriteFile(int i) Bfile_CloseFile_OS(fd); } +static void SaveSettings(int dev_info, int autostart) +{ + char *out_start = (char *)CGDOOM_SCREENS_BASE; + char *out = out_start; + + /* Version identifcation (just in case something goes wrong) */ + out += sprintf(out, "Version=%d\n", SETTINGS_FILE_VERSION); + + /* Settings */ + out += sprintf(out, "WADMethod=%s\n", + CGD_WADMethod == CGDOOM_WAD_BFILE ? "BFILE" : "MMAP"); + out += sprintf(out, "DeveloperInfo=%d\n", dev_info); + out += sprintf(out, "TrustUnalignedLumps=%d\n", CGD_TrustUnalignedLumps); + out += sprintf(out, "EnableDemos=%d\n", CGD_EnableDemos); + out += sprintf(out, "Autostart=%d\n", autostart); + out += sprintf(out, "ExperimentalMemory=%d\n", CGD_2MBLineMemory != 0); + + /* Keyboard layout */ + for(int i = 0; CGD_Keymap[2*i] != 0; i++) { + out += sprintf(out, "Keymap.%s=%s\n", + CGD_DoomKey_TechnicalName(CGD_Keymap[2*i]), + CGD_PhysicalKey_TechnicalName(CGD_Keymap[2*i+1])); + } + + M_WriteFile("CGDoom.cfg", out_start, out - out_start); + memset(out_start, 0xff, out - out_start); +} + +static void LoadSettings(int *dev_info, int *autostart) +{ + char *buffer = (char *)CGDOOM_SCREENS_BASE; + int length = M_ReadFile("CGDoom.cfg", (byte **)&buffer, 0); + if(length <= 0) + return; + + /* Parse every line on the form "=\n" */ + const char *line, *nextline=NULL; + + for(line = buffer; line < buffer + length; line = nextline) { + /* Start of next line, or end of string */ + nextline = strchr(line, '\n'); + nextline = nextline ? nextline + 1 : line + strlen(line); + + if(line[0] == '#' || line[0] == '\n') + continue; + + const char *eq = strchr(line, '='); + const char *value = eq+1; + if(!eq || eq >= nextline) + continue; + + if(!strncmp(line, "Version", eq-line)) { + int version = atoi(value); + if(version != SETTINGS_FILE_VERSION) return; + } + + else if(!strncmp(line, "WADMethod", eq-line)) { + if(!strncmp(value, "BFILE", 5)) + CGD_WADMethod = CGDOOM_WAD_BFILE; + else if(!strncmp(value, "MMAP", 4)) + CGD_WADMethod = CGDOOM_WAD_MMAP; + } + + else if(!strncmp(line, "Keymap.", 7)) { + int k1 = CGD_DoomKey_FromTechnicalName(line+7, eq-line-7); + int k2 = CGD_PhysicalKey_FromTechnicalName(value,nextline-value-1); + + if(k1 > 0 && k2 >= 0) { + for(int i = 0; CGD_Keymap[2*i] != 0; i++) { + if(CGD_Keymap[2*i] == k1) { + CGD_Keymap[2*i+1] = k2; + break; + } + } + } + } + + else if(!strncmp(line, "DeveloperInfo", eq-line)) + *dev_info = atoi(value); + else if(!strncmp(line, "TrustUnalignedLumps", eq-line)) + CGD_TrustUnalignedLumps = atoi(value); + else if(!strncmp(line, "EnableDemos", eq-line)) + CGD_EnableDemos = atoi(value); + else if(!strncmp(line, "Autostart", eq-line)) + *autostart = atoi(value); + else if(!strncmp(line, "ExperimentalMemory", eq-line)) + CGD_2MBLineMemory = atoi(value); + } +} + /////////////////////////////////////////////////////////////////////////////////////////////////// int main(void) { @@ -474,7 +565,10 @@ int main(void) startmap = 1; startepisode = 1; - VRAM = (unsigned short*)GetVRAMAddress(); + VRAM = (void *)GetVRAMAddress(); + + uintptr_t secondary_vram = ((uintptr_t)GetSecondaryVRAMAddress() + 3) & -4; + SaveVRAMBuffer = (void *)secondary_vram; int time, ms_index=0, ms_mmap=0; @@ -490,14 +584,19 @@ int main(void) int dev_info = 0; + /* Set initial keyboard layout to thumbs-only */ + CGD_LoadKeymap(CGD_Keymap_ThumbsOnly); + + /* Load settings from file if there is one */ + LoadSettings(&dev_info, &autostart_); + /* Allow the user to use memory past the 2 MB line on known OS versions */ int *enable_2MBline=NULL; char const *osv = GetOSVersion(); if(!strncmp(osv, "03.", 3) && osv[3] <= '6') // 3.60 or earlier enable_2MBline = &CGD_2MBLineMemory; - - /* Load initial keyboard layout */ - CGD_LoadKeymap(CGD_Keymap_ThumbsOnly); + else + CGD_2MBLineMemory = 0; int choice = UI_Main(wads, wad_count, &dev_info, &CGD_WADMethod, &startmap, &startepisode, &CGD_TrustUnalignedLumps, &autostart_, @@ -524,9 +623,6 @@ int main(void) if (!strcmp(wads[choice].name, "doomu4.wad")) CGD_SingleEpisodeUltimate = 4; - uintptr_t secondary_vram = ((uintptr_t)GetSecondaryVRAMAddress() + 3) & -4; - SaveVRAMBuffer = (void *)secondary_vram; - /* fx-CG 50 / Graph 90+E: RAM starts at 0x0c000000 in physical memory */ SystemStack = (void *)0xac0f0000; @@ -542,6 +638,9 @@ int main(void) wad_name[i] = wads[choice].name[i]; CGD_WADFileName = wad_name; + /* Save settings for the next load */ + SaveSettings(dev_info, autostart_); + /* Setup access to WAD file */ if(CGD_WADMethod == CGDOOM_WAD_BFILE) { diff --git a/cgdoom/cgdoom.h b/cgdoom/cgdoom.h index dd673f5..18edcb0 100644 --- a/cgdoom/cgdoom.h +++ b/cgdoom/cgdoom.h @@ -7,6 +7,8 @@ // CGDoom-specific definitions that cover both calculator and native builds. +#define SETTINGS_FILE_VERSION 1 + /* VRAM pointer and size */ extern uint16_t *VRAM; #define WIDTH 384 @@ -73,17 +75,6 @@ extern CGD_DelayedFileWrite CGD_DelayedSaves[6]; // Keyboard interface. -// Special key names for cheats, debugs, etc (completes doomdef.h). -#define SKEY_CHEAT 0x101 -#define SKEY_DECVP 0x102 -#define SKEY_INCVP 0x103 -#define SKEY_NOCLIP 0x104 -#define SKEY_GAMMA 0x105 -#define SKEY_FREEMEM 0x106 -#define SKEY_FPSCOUNTER 0x107 -#define SKEY_FRAMESKIP 0x108 -#define SKEY_PROFILER 0x109 - // Scan keyboard (the previous state is also retained). void UpdateKeyboardState(void); diff --git a/cgdoom/g_game.c b/cgdoom/g_game.c index 6be6357..1c5f8b9 100644 --- a/cgdoom/g_game.c +++ b/cgdoom/g_game.c @@ -955,7 +955,8 @@ void G_DoLoadGame (void) } else { sprintf(savename, "%s_%d.dsg", CGD_WADFileName, saveslotnumber); - length = M_ReadFile (savename, &savebuffer); + savebuffer = NULL; + length = M_ReadFile (savename, &savebuffer, 1); I_ReinitAfterError(); } diff --git a/cgdoom/i_system.c b/cgdoom/i_system.c index 82b520b..898bc15 100644 --- a/cgdoom/i_system.c +++ b/cgdoom/i_system.c @@ -25,6 +25,7 @@ #include "platform.h" #include "cgdoom.h" #include "cgdoom-alloc.h" +#include "cgdoom-kbd.h" #include "cgdoom-ui.h" #include @@ -181,6 +182,7 @@ int toupper_int(int i) else return i; } + // // I_Error // @@ -193,6 +195,8 @@ void I_Error (const char *error, ...) printf("Error: "); vprintf(error, args); printf("\n"); + va_end(args); + va_start(args, error); #endif I_ShutdownGraphics(); diff --git a/cgdoom/m_misc.c b/cgdoom/m_misc.c index ada6567..2a46f9b 100644 --- a/cgdoom/m_misc.c +++ b/cgdoom/m_misc.c @@ -86,11 +86,44 @@ int M_DrawText ( int x, int y, boolean direct, char* string ) */ +// +// M_WriteFile +// +boolean M_WriteFile(char const *name, const void *source, int length) +{ + uint16_t fc_path[100] = u"\\\\fls0\\"; + size_t size = length; + int j = 7; + int rc, fd; + + for (int i = 0; name[i]; i++) + fc_path[j++] = name[i]; + fc_path[j++] = 0x0000; + + Bfile_DeleteEntry(fc_path); + + rc = Bfile_CreateEntry_OS(fc_path, BFILE_CREATEMODE_FILE, &size); + if (rc < 0) { + /* Displaying the error message destroys the save data, so quit */ + I_Error("Bfile_CreateEntry_OS(%s, %d bytes): %d", name, length, rc); + return false; + } + + fd = Bfile_OpenFile_OS(fc_path, BFILE_WRITE, 0); + if (fd < 0) { + I_Error("Bfile_OpenFile_OS(%s): %d", name, fd); + return false; + } + + Bfile_WriteFile_OS(fd, source, length); + Bfile_CloseFile_OS(fd); + return true; +} // // M_ReadFile // -int M_ReadFile(const char *name, byte **buffer) +int M_ReadFile(const char *name, byte **buffer, int signal_errors) { uint16_t fc_path[100] = u"\\\\fls0\\"; int j = 7; @@ -102,18 +135,23 @@ int M_ReadFile(const char *name, byte **buffer) fd = Bfile_OpenFile_OS(fc_path, BFILE_READ, 0); if (fd < 0) { - I_Error("Bfile_OpenFile_OS(%s): %d", name, fd); + if(signal_errors) + I_Error("Bfile_OpenFile_OS(%s): %d", name, fd); return -1; } int size = Bfile_GetFileSize_OS(fd); if (size <= 0) { - I_Error("Bfile_GetFileSize_OS(%s): %d", name, size); + if(signal_errors) + I_Error("Bfile_GetFileSize_OS(%s): %d", name, size); Bfile_CloseFile_OS(fd); return -1; } - void *buf = Z_Malloc(size, PU_STATIC, NULL); + byte *buf = *buffer; + + if (!buf) + buf = Z_Malloc(size, PU_STATIC, NULL); if (!buf) { Bfile_CloseFile_OS(fd); return -1; diff --git a/cgdoom/m_misc.h b/cgdoom/m_misc.h index 20c7e91..8cbbb43 100644 --- a/cgdoom/m_misc.h +++ b/cgdoom/m_misc.h @@ -30,7 +30,7 @@ // MISC // boolean M_WriteFile ( char const* name, const void* source, int length ); -int M_ReadFile ( char const* name, byte** buffer ); +int M_ReadFile ( char const* name, byte** buffer, int signal_errors ); int M_DrawText ( int x, int y, boolean direct, char* string ); diff --git a/src-sdl2/emul.c b/src-sdl2/emul.c index 3b4d2e0..0c6c568 100644 --- a/src-sdl2/emul.c +++ b/src-sdl2/emul.c @@ -27,7 +27,7 @@ static void QuitSDL(void) static void InitSDL(void) { if(SDL_Init(SDL_INIT_VIDEO) < 0) { - I_Error("InitSDL: %s", SDL_GetError()); + printf("InitSDL: %s\n", SDL_GetError()); return; } @@ -37,7 +37,7 @@ static void InitSDL(void) VRAM_RGB888 = SDL_CreateRGBSurface(0, WIDTH, HEIGHT, 24, 0, 0, 0, 0); if(!VRAM_RGB888) { - I_Error("InitSDL: Cannot create intermediate surface: %s", + printf("InitSDL: Cannot create intermediate surface: %s\n", SDL_GetError()); QuitSDL(); return; @@ -201,7 +201,7 @@ int Bfile_DeleteEntry(const uint16_t *filename_u16) { char filename_u8[1024]; Bfile_NameToStr_ncpy(filename_u8, filename_u16, 1024); - printf("Deleting %s (virtually)", filename_u8); + printf("Deleting %s (virtually)\n", filename_u8); return 0; } @@ -211,14 +211,14 @@ int Bfile_CreateEntry_OS(const uint16_t *filename_u16, int mode, size_t *size) Bfile_NameToStr_ncpy(filename_u8, filename_u16, 1024); if(mode == BFILE_CREATEMODE_FILE) { - printf("Creating %s with size %zu (virtually)", filename_u8, *size); + printf("Creating %s with size %zu (virtually)\n", filename_u8, *size); } else if(mode == BFILE_CREATEMODE_FOLDER) { - I_Error("Cannot create folder %s: Not Implemented", filename_u8); + printf("Cannot create folder %s: Not Implemented\n", filename_u8); return -1; } else { - I_Error("Bfile_CreateEntry_OS(): Invalid mode %d", mode); + printf("Bfile_CreateEntry_OS(): Invalid mode %d\n", mode); return -1; } @@ -239,13 +239,13 @@ int Bfile_OpenFile_OS(const uint16_t *filename_u16, int mode, int zero) else if(mode == BFILE_READWRITE || mode == BFILE_READWRITE_SHARE) bits = "w+b"; else { - I_Error("Bfile_OpenFile_OS(): invalid mode %d for %s", mode, filename_u8); + printf("Bfile_OpenFile_OS(): invalid mode %d for %s\n", mode, filename_u8); return -1; } FILE *fp = fopen(filename_u8, bits); if(!fp) { - I_Error("Bfile_OpenFile_OS(): cannot open %s: %m", filename_u8); + printf("Bfile_OpenFile_OS(): cannot open %s: %m\n", filename_u8); return -1; } @@ -254,7 +254,7 @@ int Bfile_OpenFile_OS(const uint16_t *filename_u16, int mode, int zero) while(slot < FILE_TABLE_MAX && file_table[slot] != NULL) slot++; if(slot >= FILE_TABLE_MAX) { - I_Error("Bfile_OpenFile_OS(): cannot open %s, table is full", filename_u8); + printf("Bfile_OpenFile_OS(): cannot open %s, table is full\n",filename_u8); fclose(fp); return -1; } @@ -310,7 +310,7 @@ int Bfile_FindFirst(const uint16_t *pattern_u16, int *fd, uint16_t *found, while(slot < FILE_TABLE_MAX && search_table[slot].pos != 0) slot++; if(slot >= FILE_TABLE_MAX) { - I_Error("Bfile_FindFirst(): cannot search %s, table is full", pattern_u8); + printf("Bfile_FindFirst(): cannot search %s, table is full\n", pattern_u8); *fd = -1; return -16; }