diff --git a/polymer/eduke32/build/include/mdsprite.h b/polymer/eduke32/build/include/mdsprite.h index 22638bec3..ab7cd7720 100644 --- a/polymer/eduke32/build/include/mdsprite.h +++ b/polymer/eduke32/build/include/mdsprite.h @@ -128,7 +128,7 @@ typedef struct md3shader_t *shaders; md3uv_t *uv; md3xyzn_t *xyzn; - float *geometry; + float *geometry; // used by Polymer } md3surf_t; #define SIZEOF_MD3SURF_T (11*sizeof(int32_t) + 64*sizeof(char)) @@ -244,5 +244,6 @@ voxmodel_t *voxload(const char *filnam); int32_t voxdraw(voxmodel_t *m, spritetype *tspr); int md3postload_polymer(md3model_t* m); +int32_t md_thinoutmodel(int32_t modelid, uint8_t *usedframebitmap); #endif // !_mdsprite_h_ diff --git a/polymer/eduke32/build/src/baselayer.c b/polymer/eduke32/build/src/baselayer.c index 9976b198e..61ef41519 100644 --- a/polymer/eduke32/build/src/baselayer.c +++ b/polymer/eduke32/build/src/baselayer.c @@ -378,3 +378,42 @@ int32_t baselayer_init(void) return 0; } + +#if defined _WIN32 +# define WIN32_LEAN_AND_MEAN +# include +#elif defined __linux || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __APPLE__ +# include +#endif + +void makeasmwriteable(void) +{ +#ifndef ENGINE_USING_A_C + extern int32_t dep_begin, dep_end; +# if defined _WIN32 + DWORD oldprot; + if (!VirtualProtect((LPVOID)&dep_begin, (SIZE_T)&dep_end - (SIZE_T)&dep_begin, PAGE_EXECUTE_READWRITE, &oldprot)) + { + initprintf("Error making code writeable"); + return; + } +# elif defined __linux || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __APPLE__ + int32_t pagesize; + size_t dep_begin_page; + pagesize = sysconf(_SC_PAGE_SIZE); + if (pagesize == -1) + { + initprintf("Error getting system page size\n"); + return; + } + dep_begin_page = ((size_t)&dep_begin) & ~(pagesize-1); + if (mprotect((void *)dep_begin_page, (size_t)&dep_end - dep_begin_page, PROT_READ|PROT_WRITE) < 0) + { + initprintf("Error making code writeable (errno=%d)\n", errno); + return; + } +# else +# error "Don't know how to unprotect the self-modifying assembly on this platform!" +# endif +#endif +} diff --git a/polymer/eduke32/build/src/defs.c b/polymer/eduke32/build/src/defs.c index f877fcdc8..0674a096b 100644 --- a/polymer/eduke32/build/src/defs.c +++ b/polymer/eduke32/build/src/defs.c @@ -730,6 +730,8 @@ static int32_t defsparser(scriptfile *script) break; } md_setmisc(lastmodelid,(float)scale, shadeoffs,0.0,0); + if (glrendmode==4) + md3postload_polymer((md3model_t *)models[lastmodelid]); #endif modelskin = lastmodelskin = 0; seenframe = 0; @@ -764,8 +766,6 @@ static int32_t defsparser(scriptfile *script) { switch (md_defineframe(lastmodelid, framename, tilex, max(0,modelskin), 0.0f,0)) { - case 0: - break; case -1: happy = 0; break; // invalid model id!? case -2: @@ -778,6 +778,8 @@ static int32_t defsparser(scriptfile *script) script->filename, scriptfile_getlinum(script,cmdtokptr)); happy = 0; break; + default: + break; } } #endif @@ -934,6 +936,9 @@ static int32_t defsparser(scriptfile *script) char *modelend, *modelfn; double scale=1.0, mzadd=0.0; int32_t shadeoffs=0, pal=0, flags=0; + uint8_t usedframebitmap[1024>>3]; + + Bmemset(usedframebitmap, 0, sizeof(usedframebitmap)); static const tokenlist modeltokens[] = { @@ -983,7 +988,7 @@ static int32_t defsparser(scriptfile *script) { char *frametokptr = script->ltextptr; char *frameend, *framename = 0, happy=1; - int32_t ftilenume = -1, ltilenume = -1, tilex = 0; + int32_t ftilenume = -1, ltilenume = -1, tilex = 0, framei; double smoothduration = 0.1f; static const tokenlist modelframetokens[] = @@ -1037,10 +1042,9 @@ static int32_t defsparser(scriptfile *script) #ifdef USE_OPENGL for (tilex = ftilenume; tilex <= ltilenume && happy; tilex++) { - switch (md_defineframe(lastmodelid, framename, tilex, max(0,modelskin), smoothduration,pal)) + framei = md_defineframe(lastmodelid, framename, tilex, max(0,modelskin), smoothduration,pal); + switch (framei) { - case 0: - break; case -1: happy = 0; break; // invalid model id!? case -2: @@ -1053,6 +1057,9 @@ static int32_t defsparser(scriptfile *script) script->filename, scriptfile_getlinum(script,frametokptr)); happy = 0; break; + default: + if (framei >= 0 && framei<1024) + usedframebitmap[framei>>3] |= (1<<(framei&7)); } } #endif @@ -1312,6 +1319,24 @@ static int32_t defsparser(scriptfile *script) #ifdef USE_OPENGL md_setmisc(lastmodelid,(float)scale,shadeoffs,(float)mzadd,flags); + + // thin out the loaded model by throwing away unused frames + if (models[lastmodelid]->mdnum==3 && ((md3model_t *)models[lastmodelid])->numframes <= 1024) + { + md3model_t *m = (md3model_t *)models[lastmodelid]; + int32_t i, onumframes; + onumframes = m->numframes; + i = md_thinoutmodel(lastmodelid, usedframebitmap); +#ifdef DEBUG_MODEL_MEM + if (i>=0 && i= (uint32_t)nextmodelid) return -1; + m = (md3model_t *)models[modelid]; + if (m->mdnum != 3) return -2; + + for (anm=m->animations; anm; anm=anm->next) + { + if (anm->endframe <= anm->startframe) + { +// initprintf("backward anim %d-%d\n", anm->startframe, anm->endframe); + return -3; + } + + for (i=anm->startframe; iendframe; i++) + usedframebitmap[i>>3] |= (1<<(i&7)); + } + + sub = 0; + for (i=0; inumframes; i++) + { + if (!(usedframebitmap[i>>3]&(1<<(i&7)))) + { + sub++; + otonframe[i] = -1; + continue; + } + + otonframe[i] = i-sub; + } + + usedframes = m->numframes - sub; + if (usedframes==0 || usedframes==m->numframes) + return usedframes; + + //// THIN OUT! //// + + for (i=0; inumframes; i++) + { + if (otonframe[i]>=0 && otonframe[i] != i) + { + if (m->muladdframes) + Bmemcpy(&m->muladdframes[2*otonframe[i]], &m->muladdframes[2*i], 2*sizeof(point3d)); + Bmemcpy(&m->head.frames[otonframe[i]], &m->head.frames[i], sizeof(md3frame_t)); + } + } + + for (surfi=0; surfi < m->head.numsurfs; surfi++) + { + s = &m->head.surfs[surfi]; + + for (i=0; inumframes; i++) + if (otonframe[i]>=0 && otonframe[i] != i) + Bmemcpy(&s->xyzn[otonframe[i]*s->numverts], &s->xyzn[i*s->numverts], s->numverts*sizeof(md3xyzn_t)); + } + + ////// tweak frame indices in various places + + for (anm=m->animations; anm; anm=anm->next) + { + if (otonframe[anm->startframe]==-1 || otonframe[anm->endframe-1]==-1) + initprintf("md %d WTF: anm %d %d\n", modelid, anm->startframe, anm->endframe); + + anm->startframe = otonframe[anm->startframe]; + anm->endframe = otonframe[anm->endframe-1]; + } + + for (i=0; imuladdframes) + m->muladdframes = Brealloc(m->muladdframes, 2*sizeof(point3d)*usedframes); + m->head.frames = Brealloc(m->head.frames, sizeof(md3frame_t)*usedframes); + + for (surfi=0; surfi < m->head.numsurfs; surfi++) + { + m->head.surfs[surfi].numframes = usedframes; + // CAN'T do that because xyzn is offset from a larger block when loaded from md3: +// m->head.surfs[surfi].xyzn = Brealloc(m->head.surfs[surfi].xyzn, s->numverts*usedframes*sizeof(md3xyzn_t)); + } + + m->head.numframes = usedframes; + m->numframes = usedframes; + + //////////// + return usedframes; +} + int32_t md_defineskin(int32_t modelid, const char *skinfn, int32_t palnum, int32_t skinnum, int32_t surfnum, float param, float specpower, float specfactor) { mdskinmap_t *sk, *skl; @@ -1685,13 +1787,23 @@ int md3postload_polymer(md3model_t *m) int *numtris; float lat, lng, vec1[5], vec2[5], mat[9], r; + if (m->head.surfs[0].geometry) + return -1; // already postprocessed + // let's also repack the geometry to more usable formats surfi = 0; while (surfi < m->head.numsurfs) { s = &m->head.surfs[surfi]; - +#ifdef DEBUG_MODEL_MEM + i = (m->head.numframes * s->numverts * sizeof(float) * 15); + if (i > 1<<20) + initprintf("size %d (%d fr, %d v): md %s surf %d/%d\n", i, m->head.numframes, s->numverts, + m->indices?(char *)m->indices:"null", surfi, m->head.numsurfs); + if (m->indices) + Bfree(m->indices), m->indices=NULL; +#endif s->geometry = Bcalloc(m->head.numframes * s->numverts * sizeof(float), 15); numtris = Bcalloc(s->numverts, sizeof(int)); @@ -3249,19 +3361,18 @@ mdmodel_t *mdload(const char *filnam) if (vm) { +#ifdef DEBUG_MODEL_MEM + ((md3model_t *)vm)->indices = (void *)Bstrdup(filnam); +#endif md3postload_common((md3model_t *)vm); -#ifdef POLYMER -// implies defined(POLYMOST) && defined(USE_OPENGL)? - if (glrendmode==4) - i = md3postload_polymer((md3model_t *)vm); - else - i = md3postload_polymer_check((md3model_t *)vm); - if (!i) - { - mdfree(vm); - vm = (mdmodel_t *)0; - } +#ifdef POLYMER + if (glrendmode!=4) + if (!md3postload_polymer_check((md3model_t *)vm)) + { + mdfree(vm); + vm = (mdmodel_t *)0; + } #endif } diff --git a/polymer/eduke32/build/src/osd.c b/polymer/eduke32/build/src/osd.c index b8f8571ac..08f85cd9b 100644 --- a/polymer/eduke32/build/src/osd.c +++ b/polymer/eduke32/build/src/osd.c @@ -252,12 +252,16 @@ int32_t OSD_Exec(const char *szScript) if (fp != NULL) { - char line[255]; + char line[256], *cp; OSD_Printf("Executing \"%s\"\n", szScript); osdexecscript++; - while (fgets(line, sizeof(line)-1, fp) != NULL) - OSD_Dispatch(strtok(line,"\r\n")); + while (fgets(line, sizeof(line), fp) != NULL) + { + cp = strtok(line,"\r\n"); + if (cp) + OSD_Dispatch(cp); + } osdexecscript--; fclose(fp); return 0; diff --git a/polymer/eduke32/build/src/sdlayer.c b/polymer/eduke32/build/src/sdlayer.c index 5e906a197..2fa16092b 100644 --- a/polymer/eduke32/build/src/sdlayer.c +++ b/polymer/eduke32/build/src/sdlayer.c @@ -2163,41 +2163,3 @@ static int32_t buildkeytranslationtable(void) return 0; } #endif -#if defined _WIN32 -# define WIN32_LEAN_AND_MEAN -# include -#elif defined __linux || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __APPLE__ -# include -#endif - -void makeasmwriteable(void) -{ -#ifndef ENGINE_USING_A_C - extern int32_t dep_begin, dep_end; -# if defined _WIN32 - DWORD oldprot; - if (!VirtualProtect((LPVOID)&dep_begin, (SIZE_T)&dep_end - (SIZE_T)&dep_begin, PAGE_EXECUTE_READWRITE, &oldprot)) - { - initprint("Error making code writeable\n"); - return; - } -# elif defined __linux || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __APPLE__ - int32_t pagesize; - size_t dep_begin_page; - pagesize = sysconf(_SC_PAGE_SIZE); - if (pagesize == -1) - { - initprintf("Error getting system page size\n"); - return; - } - dep_begin_page = ((size_t)&dep_begin) & ~(pagesize-1); - if (mprotect((void *)dep_begin_page, (size_t)&dep_end - dep_begin_page, PROT_READ|PROT_WRITE) < 0) - { - initprintf("Error making code writeable (errno=%d)\n", errno); - return; - } -# else -# error "Don't know how to unprotect the self-modifying assembly on this platform!" -# endif -#endif -} diff --git a/polymer/eduke32/build/src/winlayer.c b/polymer/eduke32/build/src/winlayer.c index 9fd858b79..9e15d85b7 100644 --- a/polymer/eduke32/build/src/winlayer.c +++ b/polymer/eduke32/build/src/winlayer.c @@ -3771,20 +3771,3 @@ static LPTSTR GetWindowsErrorMsg(DWORD code) return lpMsgBuf; } - -// -// makeasmwriteable() -- removes write protection from the self-modifying assembly code -// -void makeasmwriteable(void) -{ -#ifndef ENGINE_USING_A_C - extern int32_t dep_begin, dep_end; - DWORD oldprot; - - if (!VirtualProtect((LPVOID)&dep_begin, (SIZE_T)&dep_end - (SIZE_T)&dep_begin, PAGE_EXECUTE_READWRITE, &oldprot)) - { - ShowErrorBox("Problem making code writeable"); - } -#endif -} - diff --git a/polymer/eduke32/source/config.c b/polymer/eduke32/source/config.c index b545f4e95..7d7b36d33 100644 --- a/polymer/eduke32/source/config.c +++ b/polymer/eduke32/source/config.c @@ -648,6 +648,8 @@ int32_t CONFIG_ReadSetup(void) SCRIPT_GetString(ud.config.scripthandle, "Comm Setup",commmacro,&ud.ridecule[dummy][0]); } + Bmemset(tempbuf, 0, sizeof(tempbuf)); +// Bmemset(dummybuf, 0, sizeof(dummybuf)); SCRIPT_GetString(ud.config.scripthandle, "Comm Setup","PlayerName",&tempbuf[0]); while (Bstrlen(OSD_StripColors(dummybuf,tempbuf)) > 10) diff --git a/polymer/eduke32/source/savegame.c b/polymer/eduke32/source/savegame.c index 291df59cd..d3e7f68e0 100644 --- a/polymer/eduke32/source/savegame.c +++ b/polymer/eduke32/source/savegame.c @@ -613,22 +613,16 @@ int32_t G_LoadPlayer(int32_t spot) #ifdef POLYMER if (getrendermode() == 4) - { - int32_t i = 0; - polymer_loadboard(); - - while (i < MAXSPRITES) - { - if (actor[i].lightptr) - { - actor[i].lightptr = NULL; - actor[i].lightId = -1; - } - i++; - } - } #endif + // this light pointer nulling needs to be outside the getrendermode check + // because we might be loading the savegame using another renderer but + // change to Polymer later + for (i=0; i= j && T2 < k) savegame_bitmap[i>>3][0] |= 1<<(i&7), T2 -= j; @@ -1951,8 +1945,9 @@ static void sv_postactordata() for (i=0; i