From ce7a20d4f23dceaa3661437bfa1b3d02d829aa2f Mon Sep 17 00:00:00 2001 From: hendricks266 Date: Wed, 23 Sep 2015 17:54:50 +0000 Subject: [PATCH] Restructure loadpalette() so that read errors are not immediately fatal and enforce that the necessary parts have been loaded in E_PostInit (after loaddefinitionsfile()). git-svn-id: https://svn.eduke32.com/eduke32@5346 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/include/build.h | 11 + polymer/eduke32/build/include/cache1d.h | 1 + polymer/eduke32/build/include/editor.h | 2 +- polymer/eduke32/build/src/build.c | 2 - polymer/eduke32/build/src/defs.c | 9 + polymer/eduke32/build/src/engine.c | 307 ++++++++++++-------- polymer/eduke32/build/src/engine_priv.h | 2 + polymer/eduke32/source/astub.c | 3 +- polymer/eduke32/source/game.c | 2 + polymer/eduke32/source/testgame/src/bstub.c | 2 +- polymer/eduke32/source/testgame/src/game.c | 3 +- 11 files changed, 223 insertions(+), 121 deletions(-) diff --git a/polymer/eduke32/build/include/build.h b/polymer/eduke32/build/include/build.h index be1beaddd..a4863883f 100644 --- a/polymer/eduke32/build/include/build.h +++ b/polymer/eduke32/build/include/build.h @@ -695,10 +695,19 @@ static inline int32_t BGetTime(void) { return totalclock; } EXTERN int32_t numframes, randomseed; EXTERN int16_t sintable[2048]; + EXTERN uint8_t palette[768]; EXTERN int16_t numshades; EXTERN char *palookup[MAXPALOOKUPS]; EXTERN uint8_t **basepaltableptr; +EXTERN uint8_t paletteloaded; + +enum { + PALETTE_MAIN = 1<<0, + PALETTE_SHADE = 1<<1, + PALETTE_TRANSLUC = 1<<2, +}; + EXTERN char showinvisibility; EXTERN int32_t g_visibility, parallaxvisibility; EXTERN int32_t g_rotatespriteNoWidescreen; @@ -1003,6 +1012,8 @@ void initspritelists(void); int32_t loadlookups(int32_t fp); void generatefogpals(void); void fillemptylookups(void); +void E_ReplaceTransparentColorWithBlack(void); + int32_t loadboard(const char *filename, char flags, vec3_t *dapos, int16_t *daang, int16_t *dacursectnum); int32_t loadmaphack(const char *filename); void delete_maphack_lights(); diff --git a/polymer/eduke32/build/include/cache1d.h b/polymer/eduke32/build/include/cache1d.h index 6edc5f785..1123efa3c 100644 --- a/polymer/eduke32/build/include/cache1d.h +++ b/polymer/eduke32/build/include/cache1d.h @@ -37,6 +37,7 @@ int32_t initgroupfile(const char *filename); void uninitgroupfile(void); int32_t kopen4load(const char *filename, char searchfirst); // searchfirst: 0 = anywhere, 1 = first group, 2 = any group int32_t kread(int32_t handle, void *buffer, int32_t leng); +#define kread_and_test(handle, buffer, leng) EDUKE32_PREDICT_FALSE(kread((handle), (buffer), (leng)) != (leng)) int32_t klseek(int32_t handle, int32_t offset, int32_t whence); int32_t kfilelength(int32_t handle); int32_t ktell(int32_t handle); diff --git a/polymer/eduke32/build/include/editor.h b/polymer/eduke32/build/include/editor.h index 06921a4e7..9aef685c1 100644 --- a/polymer/eduke32/build/include/editor.h +++ b/polymer/eduke32/build/include/editor.h @@ -265,7 +265,7 @@ extern void showspritedata(int16_t spritenum, int16_t small); extern void drawsmallabel(const char *text, char col, char backcol, char border, int32_t dax, int32_t day, int32_t daz); -extern int32_t whitecol; +extern int32_t whitecol, blackcol; extern int32_t circlewall; extern int32_t searchlock; diff --git a/polymer/eduke32/build/src/build.c b/polymer/eduke32/build/src/build.c index d6b8833ac..45087e5e9 100644 --- a/polymer/eduke32/build/src/build.c +++ b/polymer/eduke32/build/src/build.c @@ -722,8 +722,6 @@ int32_t app_main(int32_t argc, const char **argv) Bfree (g_defModules); g_defModules = NULL; // be defensive... - // Here used to be the 'whitecol' calculation - if (E_PostInit()) M32_FatalEngineError(); diff --git a/polymer/eduke32/build/src/defs.c b/polymer/eduke32/build/src/defs.c index 9bd0c053f..89f82f29d 100644 --- a/polymer/eduke32/build/src/defs.c +++ b/polymer/eduke32/build/src/defs.c @@ -284,6 +284,9 @@ static int32_t Defs_ImportTileFromTexture(char const * const fn, int32_t const t if (EDUKE32_PREDICT_FALSE(xsiz <= 0 || ysiz <= 0)) return -2; + if (!(paletteloaded & PALETTE_MAIN)) + return -3; + set_tilesiz(tile, xsiz, ysiz); tile_from_truecolpic(tile, picptr, alphacut); @@ -821,6 +824,9 @@ static int32_t defsparser(scriptfile *script) } int32_t const texstatus = Defs_ImportTileFromTexture(fn, tile, alphacut, istexture); + if (texstatus == -3) + initprintf("Error: No palette loaded, in tilefromtexture definition near line %s:%d\n", + script->filename, scriptfile_getlinum(script,texturetokptr)); if (texstatus == (-3)<<8) initprintf("Error: \"%s\" has more than one tile, in tilefromtexture definition near line %s:%d\n", fn, script->filename, scriptfile_getlinum(script,texturetokptr)); @@ -963,6 +969,9 @@ static int32_t defsparser(scriptfile *script) break; int32_t const texstatus = Defs_ImportTileFromTexture(fn, tile, 255, 0); + if (texstatus == -3) + initprintf("Error: No palette loaded, in importtile definition near line %s:%d\n", + script->filename, scriptfile_getlinum(script,cmdtokptr)); if (texstatus == (-3)<<8) initprintf("Error: \"%s\" has more than one tile, in importtile definition near line %s:%d\n", fn, script->filename, scriptfile_getlinum(script,cmdtokptr)); diff --git a/polymer/eduke32/build/src/engine.c b/polymer/eduke32/build/src/engine.c index 9f05a9428..281d37802 100644 --- a/polymer/eduke32/build/src/engine.c +++ b/polymer/eduke32/build/src/engine.c @@ -273,7 +273,7 @@ int32_t showfirstwall=0; int32_t showheightindicators=1; int32_t circlewall=-1; -int32_t whitecol; +int32_t whitecol, blackcol; #ifdef POLYMER static int16_t maphacklightcnt=0; @@ -2330,8 +2330,6 @@ int32_t ydim16, qsetmode = 0; int16_t pointhighlight=-1, linehighlight=-1, highlightcnt=0; static int32_t *lastx; -static char paletteloaded = 0; - int32_t halfxdim16, midydim16; #define FASTPALGRIDSIZ 8 @@ -8255,10 +8253,9 @@ static int32_t loadtables(void) // // initfastcolorlookup (internal) // -static void initfastcolorlookup(int32_t rscale, int32_t gscale, int32_t bscale) +static void initfastcolorlookup_scale(int32_t rscale, int32_t gscale, int32_t bscale) { - int32_t i, j, x, y, z; - const char *pal1; + int32_t i, j; j = 0; for (i=64; i>=0; i--) @@ -8269,6 +8266,11 @@ static void initfastcolorlookup(int32_t rscale, int32_t gscale, int32_t bscale) bdist[i] = bdist[128-i] = j*bscale; j += 129-(i<<1); } +} +void initfastcolorlookup_palette(void) +{ + int32_t i, j, x, y, z; + const char *pal1; Bmemset(colhere,0,sizeof(colhere)); Bmemset(colhead,0,sizeof(colhead)); @@ -8305,169 +8307,222 @@ static void alloc_palookup(int32_t pal) #endif } -static int32_t loadpalette_err(const char *msg) -{ - engineerrstr = msg; - initprintf("ERROR: %s\n", engineerrstr); - return -1; -} +static void maybe_alloc_palookup(int32_t palnum); // // loadpalette (internal) // -static int32_t loadpalette(void) +static void loadpalette(void) { - int32_t fil, lamedukep=0; - char *transluc; + initfastcolorlookup_scale(30, 59, 11); - if (paletteloaded != 0) return 0; + int32_t fil; if ((fil = kopen4load("palette.dat",0)) == -1) - return loadpalette_err("Failed to load \"palette.dat\"!"); + return; - kread(fil,palette,768); - kread(fil,&numshades,2); numshades = B_LITTLE16(numshades); + + // PALETTE_MAIN + + if (kread_and_test(fil,palette,768)) + return kclose(fil); + + initfastcolorlookup_palette(); + + paletteloaded |= PALETTE_MAIN; + + + // PALETTE_SHADES + + if (kread_and_test(fil,&numshades,2)) + return kclose(fil); + numshades = B_LITTLE16(numshades); if (numshades <= 1) - return loadpalette_err("Invalid number of shades in \"palette.dat\"!"); - - alloc_palookup(0); - - transluc = (char *)Xcalloc(256, 256); - - globalpalwritten = palookup[0]; globalpal = 0; - setpalookupaddress(globalpalwritten); - - blendtable[0] = transluc; - fixtransluscence(FP_OFF(transluc)); + { + initprintf("Warning: Invalid number of shades in \"palette.dat\"!"); + numshades = 0; + return kclose(fil); + } // Auto-detect LameDuke. Its PALETTE.DAT doesn't have a 'numshades' 16-bit // int after the base palette, but starts directly with the shade tables. // Thus, the first two bytes will be 00 01, which is 256 if read as // little-endian int16_t. - if (numshades == 256) + int32_t lamedukep = 0; + if (numshades == 256) { - if (klseek(fil, -2, BSEEK_CUR) < 0) - return loadpalette_err("klseek() failed in loadpalette()!"); + static char const * const seekfail = "Warning: klseek() failed in loadpalette()!"; - numshades = 32; - lamedukep = 1; + uint16_t temp; + if (kread_and_test(fil,&temp,2)) + return kclose(fil); + temp = B_LITTLE16(temp); + if (temp == 770) // 02 03 + { + if (klseek(fil, -4, BSEEK_CUR) < 0) + { + initprintf(seekfail); + return kclose(fil); + } + + numshades = 32; + lamedukep = 1; + } + else + { + if (klseek(fil, -2, BSEEK_CUR) < 0) + { + initprintf(seekfail); + return kclose(fil); + } + } } // Read base shade table (palookup 0). - kread(fil, palookup[globalpal], numshades<<8); + maybe_alloc_palookup(0); + if (kread_and_test(fil, palookup[0], numshades<<8)) + return kclose(fil); + + paletteloaded |= PALETTE_SHADE; + + + // PALETTE_TRANSLUC + + char * const transluc = blendtable[0] = (char *)Xcalloc(256, 256); // Read translucency (blending) table. if (lamedukep) { - int32_t i, j; - - for (i=0; i<255; i++) + for (int i=0; i<255; i++) { // NOTE: LameDuke's table doesn't have the last row or column (i==255). // Read the entries above and on the diagonal, if the table is // thought as being row-major. - if (kread(fil, &transluc[256*i + i], 256-i-1) != 256-i-1) - return loadpalette_err("Failed reading LameDuke translucency table!"); + if (kread_and_test(fil, &transluc[256*i + i], 256-i-1)) + return kclose(fil); // Duplicate the entries below the diagonal. - for (j=0; j= 1 && lognumalphatabs <= 7)) - return loadpalette_err("invalid lognumalphatabs value, must be in [1 .. 7]"); - numalphatabs = 1<= 1 && lognumalphatabs <= 7)) + initprintf("invalid lognumalphatabs value, must be in [1 .. 7]"); + else + numalphatabs = 1< k) { k = j; whitecol = i; } + } + for (int i=0, j, k=768; i<256; i++) + { + j = palette[i*3] + palette[i*3+1] + palette[i*3+2]; + if (j < k) { k = j; blackcol = i; } + } +} + +void E_ReplaceTransparentColorWithBlack(void) +{ + for (int i=0; i k) { k = j; whitecol = i; } + transluc[(255<<8) + j] = transluc[(blackcol<<8) + j]; + transluc[255 + (j<<8)] = transluc[blackcol + (j<<8)]; } + transluc[(255<<8) + 255] = transluc[(blackcol<<8) + blackcol]; } - - paletteloaded = 1; - - return 0; } // Load LOOKUP.DAT, which contains lookup tables and additional base palettes. @@ -9076,6 +9131,16 @@ static void sighandler(int sig, siginfo_t *info, void *ctx) } #endif +// +// E_FatalError +// +static int32_t E_FatalError(char const * const msg) +{ + engineerrstr = msg; + initprintf("ERROR: %s\n", engineerrstr); + return -1; +} + // // preinitengine // @@ -9229,8 +9294,7 @@ int32_t initengine(void) g_visibility = 512; parallaxvisibility = 512; - if (loadpalette()) - return 1; + loadpalette(); #ifdef USE_OPENGL if (!hicinitcounter) hicinit(); @@ -9239,13 +9303,13 @@ int32_t initengine(void) #ifdef LUNATIC if (L_CreateState(&g_engState, "eng", NULL)) - return loadpalette_err("Failed creating engine Lua state!"); + return E_FatalError("Failed creating engine Lua state!"); { char *luastr = "_LUNATIC_AUX=true; decl=require('ffi').cdef; require'defs_common'"; if (L_RunString(&g_engState, luastr, 0, -1, "eng")) - return loadpalette_err("Failed setting up engine Lua state"); + return E_FatalError("Failed setting up engine Lua state"); } #endif @@ -9258,6 +9322,15 @@ int32_t initengine(void) int32_t E_PostInit(void) { + if (!(paletteloaded & PALETTE_MAIN)) + return E_FatalError("No palette found."); + if (!(paletteloaded & PALETTE_SHADE)) + return E_FatalError("No shade tables found."); + if (!(paletteloaded & PALETTE_TRANSLUC)) + return E_FatalError("No tranlucency tables found."); + + E_PostLoadPalette(); + return 0; } @@ -9266,8 +9339,6 @@ int32_t E_PostInit(void) // void uninitengine(void) { - int32_t i; - #ifdef USE_OPENGL polymost_glreset(); hicinit(); @@ -9284,12 +9355,20 @@ void uninitengine(void) DO_FREE_AND_NULL(lookups); ALIGNED_FREE_AND_NULL(distrecip); - for (i=0; i>4],((option[7]&4)>0)+1,((option[7]&2)>0)+1,60,option[7]&1);