summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lobject.c
blob: 62ad8e93590b653b563bb17e01570309fe10df3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/

#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define lobject_c
#define LUA_CORE

#include "lua.h"

#include "ldo.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
#include "lvm.h"



const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};


/*
** converts an integer to a "floating point byte", represented as
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
** eeeee != 0 and (xxx) otherwise.
*/
int luaO_int2fb (unsigned int x) {
  int e = 0;  /* expoent */
  while (x >= 16) {
    x = (x+1) >> 1;
    e++;
  }
  if (x < 8) return x;
  else return ((e+1) << 3) | (cast_int(x) - 8);
}


/* converts back */
int luaO_fb2int (int x) {
  int e = (x >> 3) & 31;
  if (e == 0) return x;
  else return ((x & 7)+8) << (e - 1);
}


int luaO_log2 (unsigned int x) {
  static const lu_byte log_2[256] = {
    0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
  };
  int l = -1;
  while (x >= 256) { l += 8; x >>= 8; }
  return l + log_2[x];

}


int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
  if (ttype(t1) != ttype(t2)) return 0;
  else switch (ttype(t1)) {
    case LUA_TNIL:
      return 1;
    case LUA_TNUMBER:
      return luai_numeq(nvalue(t1), nvalue(t2));
    case LUA_TBOOLEAN:
      return bvalue(t1) == bvalue(t2);  /* boolean true must be 1 !! */
    case LUA_TLIGHTUSERDATA:
      return pvalue(t1) == pvalue(t2);
    default:
      lua_assert(iscollectable(t1));
      return gcvalue(t1) == gcvalue(t2);
  }
}


int luaO_str2d (const char *s, lua_Number *result) {
  char *endptr;
  *result = lua_str2number(s, &endptr);
  if (endptr == s) return 0;  /* conversion failed */
  if (*endptr == 'x' || *endptr == 'X')  /* maybe an hexadecimal constant? */
    *result = cast_num(strtoul(s, &endptr, 16));
  if (*endptr == '\0') return 1;  /* most common case */
  while (isspace(cast(unsigned char, *endptr))) endptr++;
  if (*endptr != '\0') return 0;  /* invalid trailing characters? */
  return 1;
}



static void pushstr (lua_State *L, const char *str) {
  setsvalue2s(L, L->top, luaS_new(L, str));
  incr_top(L);
}


/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  int n = 1;
  pushstr(L, "");
  for (;;) {
    const char *e = strchr(fmt, '%');
    if (e == NULL) break;
    setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
    incr_top(L);
    switch (*(e+1)) {
      case 's': {
        const char *s = va_arg(argp, char *);
        if (s == NULL) s = "(null)";
        pushstr(L, s);
        break;
      }
      case 'c': {
        char buff[2];
        buff[0] = cast(char, va_arg(argp, int));
        buff[1] = '\0';
        pushstr(L, buff);
        break;
      }
      case 'd': {
        setnvalue(L->top, cast_num(va_arg(argp, int)));
        incr_top(L);
        break;
      }
      case 'f': {
        setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
        incr_top(L);
        break;
      }
      case 'p': {
        char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
        snprintf(buff, 4*sizeof(void *) + 8, "%p", va_arg(argp, void *));
        pushstr(L, buff);
        break;
      }
      case '%': {
        pushstr(L, "%");
        break;
      }
      default: {
        char buff[3];
        buff[0] = '%';
        buff[1] = *(e+1);
        buff[2] = '\0';
        pushstr(L, buff);
        break;
      }
    }
    n += 2;
    fmt = e+2;
  }
  pushstr(L, fmt);
  luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
  L->top -= n;
  return svalue(L->top - 1);
}


const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  const char *msg;
  va_list argp;
  va_start(argp, fmt);
  msg = luaO_pushvfstring(L, fmt, argp);
  va_end(argp);
  return msg;
}


void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  if (*source == '=') {
    strncpy(out, source+1, bufflen);  /* remove first char */
    out[bufflen-1] = '\0';  /* ensures null termination */
  }
  else {  /* out = "source", or "...source" */
    if (*source == '@') {
      size_t l;
      source++;  /* skip the `@' */
      bufflen -= sizeof(" '...' ");
      l = strlen(source);
      strcpy(out, "");
      if (l > bufflen) {
        source += (l-bufflen);  /* get last part of file name */
        strcat(out, "...");
      }
      strcat(out, source);
    }
    else {  /* out = [string "string"] */
      size_t len = strcspn(source, "\n\r");  /* stop at first newline */
      bufflen -= sizeof(" [string \"...\"] ");
      if (len > bufflen) len = bufflen;
      strcpy(out, "[string \"");
      if (source[len] != '\0') {  /* must truncate? */
        strncat(out, source, len);
        strcat(out, "...");
      }
      else
        strcat(out, source);
      strcat(out, "\"]");
    }
  }
}
identify transparent regions in merged textures byte *marks = calloc(texture->width, texture->height), *source; for (; --i >=0; patch++) { const patch_t *realpatch = W_CacheLumpNum(patch->patch); // cph int x1 = patch->originx, x2 = x1 + SHORT(realpatch->width); const int *cofs = realpatch->columnofs-x1; if (x1<0) x1 = 0; if (x2 > texture->width) x2 = texture->width; for (; x1<x2 ; x1++) if (collump[x1] == -1) // Column has multiple patches? // killough 1/25/98, 4/9/98: Fix medusa bug. R_DrawColumnInCache((column_t*)((byte*)realpatch+LONG(cofs[x1])), block+colofs[x1],patch->originy,texture->height, marks + x1 * texture->height); W_UnlockLumpNum(patch->patch); // cph - unlock the patch lump } // killough 4/9/98: Next, convert multipatched columns into true columns, // to fix Medusa bug while still allowing for transparent regions. source = malloc(texture->height); // temporary column for (i=0; i < texture->width; i++) if (collump[i] == -1) // process only multipatched columns { column_t *col = (column_t *)(block + colofs[i] - 3); // cached column const byte *mark = marks + i * texture->height; int j = 0; // save column in temporary so we can shuffle it around memcpy(source, (byte *) col + 3, texture->height); for (;;) // reconstruct the column by scanning transparency marks { while (j < texture->height && !mark[j]) // skip transparent cells j++; if (j >= texture->height) // if at end of column { col->topdelta = -1; // end-of-column marker break; } col->topdelta = j; // starting offset of post for (col->length=0; j < texture->height && mark[j]; j++) col->length++; // count opaque cells // copy opaque cells from the temporary back into the column memcpy((byte *) col + 3, source + col->topdelta, col->length); col = (column_t *)((byte *) col + col->length + 4); // next post } } free(source); // free temporary column free(marks); // free transparency marks // Now that the texture has been built in column cache, // it is purgable from zone memory. Z_ChangeTag(block, PU_CACHE); } // // R_GenerateLookup // // Rewritten by Lee Killough for performance and to fix Medusa bug // static void R_GenerateLookup(int texnum, int *const errors) { texture_t *texture = textures[texnum]; // killough 4/9/98: make column offsets 32-bit; // clean up malloc-ing to use sizeof // CPhipps - moved allocing here short *collump = texture->columnlump = Z_Malloc(texture->width*sizeof(*texture->columnlump), PU_STATIC,0); unsigned *colofs = texture->columnofs = Z_Malloc(texture->width*sizeof(*texture->columnofs), PU_STATIC,0); // killough 4/9/98: keep count of posts in addition to patches. // Part of fix for medusa bug for multipatched 2s normals. struct { unsigned short patches, posts; } *count = calloc(sizeof *count, texture->width); { int i = texture->patchcount; const texpatch_t *patch = texture->patches; while (--i >= 0) { int pat = patch->patch; const patch_t *realpatch = W_CacheLumpNum(pat); int x1 = patch++->originx, x2 = x1 + SHORT(realpatch->width), x = x1; const int *cofs = realpatch->columnofs-x1; if (x2 > texture->width) x2 = texture->width; if (x1 < 0) x = 0; for ( ; x<x2 ; x++) { // killough 4/9/98: keep a count of the number of posts in column, // to fix Medusa bug while allowing for transparent multipatches. const column_t *col = (column_t*)((byte*)realpatch+LONG(cofs[x])); for (;col->topdelta != 0xff; count[x].posts++) col = (column_t *)((byte *) col + col->length + 4); count[x].patches++; collump[x] = pat; colofs[x] = LONG(cofs[x])+3; } W_UnlockLumpNum(pat); } } // Composited texture not created yet. texture->composite = NULL; // Now count the number of columns // that are covered by more than one patch. // Fill in the lump / offset, so columns // with only a single patch are all done. { int x = texture->width; int height = texture->height; int csize = 0; while (--x >= 0) { if (!count[x].patches) // killough 4/9/98 { //jff 8/3/98 use logical output routine printf("\nR_GenerateLookup: Column %d is without a patch in texture %s", x, texture->name); if (errors) ++*errors; else I_Error("R_GenerateLookup: Failed"); } if (count[x].patches > 1) // killough 4/9/98 { // killough 1/25/98, 4/9/98: // // Fix Medusa bug, by adding room for column header // and trailer bytes for each post in merged column. // For now, just allocate conservatively 4 bytes // per post per patch per column, since we don't // yet know how many posts the merged column will // require, and it's bounded above by this limit. collump[x] = -1; // mark lump as multipatched colofs[x] = csize + 3; // three header bytes in a column csize += 4*count[x].posts+1; // 1 stop byte plus 4 bytes per post } csize += height; // height bytes of texture data } texture->compositesize = csize; } free(count); // killough 4/9/98 } // // R_GetColumn // const byte *R_GetColumn(int tex, int col) { const texture_t *texture = textures[tex]; if (!texture->columnlump) R_GenerateLookup(tex, NULL); { int lump = texture->columnlump[col &= texture->widthmask]; int ofs = texture->columnofs[col]; // cph - WARNING: must be after the above line // cph - remember the last lump, so we can unlock it if no longer needed, // or reuse it if possible to reduce lump locking/unlocking static int lastlump = -1; static const byte* lastlumpdata; if ((lump<=0) && (lastlump<=0)) lump = lastlump; // cph - force equal if (lump != lastlump) { // cph - must change the cached lump if (lastlump>0) W_UnlockLumpNum(lastlump); if ((lastlump = lump) > 0) lastlumpdata = W_CacheLumpNum(lump); #ifdef RANGECHECK else lastlumpdata = NULL; #endif } if (lump > 0) return lastlumpdata + ofs; if (!texture->composite) R_GenerateComposite(tex); return texture->composite + ofs; } } // // R_InitTextures // Initializes the texture list // with the textures from the world map. // void R_InitTextures (void) { maptexture_t *mtexture; texture_t *texture; mappatch_t *mpatch; texpatch_t *patch; int i, j; int maptex_lump[2] = {-1, -1}; const int *maptex; const int *maptex1, *maptex2; char name[9]; int names_lump; // cph - new wad lump handling const unsigned char *names; // cph - const char *name_p;// const*'s int *patchlookup; int totalwidth; int nummappatches; int offset; int maxoff, maxoff2; int numtextures1, numtextures2; const int *directory; int errors = 0; // Load the patch names from pnames.lmp. name[8] = 0; names = W_CacheLumpNum(names_lump = W_GetNumForName("PNAMES")); nummappatches = (names[3]<<24)|(names[2]<<16)|(names[1]<<8)|names[0]; name_p = names+4; patchlookup = malloc(nummappatches*sizeof(*patchlookup)); // killough for (i=0 ; i<nummappatches ; i++) { strncpy (name,name_p+i*8, 8); patchlookup[i] = W_CheckNumForName(name); if (patchlookup[i] == -1) { // killough 4/17/98: // Some wads use sprites as wall patches, so repeat check and // look for sprites this time, but only if there were no wall // patches found. This is the same as allowing for both, except // that wall patches always win over sprites, even when they // appear first in a wad. This is a kludgy solution to the wad // lump namespace problem. patchlookup[i] = (W_CheckNumForName)(name, ns_sprites); if (patchlookup[i] == -1 && devparm) //jff 8/3/98 use logical output routine printf("\nWarning: patch %.8s, index %d does not exist",name,i); } } W_UnlockLumpNum(names_lump); // cph - release the lump // Load the map texture definitions from textures.lmp. // The data is contained in one or two lumps, // TEXTURE1 for shareware, plus TEXTURE2 for commercial. maptex = maptex1 = W_CacheLumpNum(maptex_lump[0] = W_GetNumForName("TEXTURE1")); numtextures1 = LONG(*maptex); maxoff = W_LumpLength(maptex_lump[0]); directory = maptex+1; if (W_CheckNumForName("TEXTURE2") != -1) { maptex2 = W_CacheLumpNum(maptex_lump[1] = W_GetNumForName("TEXTURE2")); numtextures2 = LONG(*maptex2); maxoff2 = W_LumpLength(maptex_lump[1]); } else { maptex2 = NULL; numtextures2 = 0; maxoff2 = 0; } numtextures = numtextures1 + numtextures2; // killough 4/9/98: make column offsets 32-bit; // clean up malloc-ing to use sizeof textures = Z_Malloc(numtextures*sizeof*textures, PU_STATIC, 0); textureheight = Z_Malloc(numtextures*sizeof*textureheight, PU_STATIC, 0); totalwidth = 0; for (i=0 ; i<numtextures ; i++, directory++) { if (i == numtextures1) { // Start looking in second texture file. maptex = maptex2; maxoff = maxoff2; directory = maptex+1; } offset = LONG(*directory); if (offset > maxoff) I_Error("R_InitTextures: Bad texture directory"); mtexture = (maptexture_t *) ( (byte *)maptex + offset); texture = textures[i] = Z_Malloc(sizeof(texture_t) + sizeof(texpatch_t)*(SHORT(mtexture->patchcount)-1), PU_STATIC, 0); texture->width = SHORT(mtexture->width); texture->height = SHORT(mtexture->height); texture->patchcount = SHORT(mtexture->patchcount); /* Mattias EngdegÄrd emailed me of the following explenation of * why memcpy doesnt work on some systems: * "I suppose it is the mad unaligned allocation * going on (and which gcc in some way manages to cope with * through the __attribute__ ((packed))), and which it forgets * when optimizing memcpy (to a single word move) since it appears * to be aligned. Technically a gcc bug, but I can't blame it when * it's stressed with that amount of * non-standard nonsense." * So in short the unaligned struct confuses gcc's optimizer so * i took the memcpy out alltogether to avoid future problems-Jess */ /* The above was #ifndef SPARC, but i got a mail from * Putera Joseph F NPRI <PuteraJF@Npt.NUWC.Navy.Mil> containing: * I had to use the memcpy function on a sparc machine. The * other one would give me a core dump. * cph - I find it hard to believe that sparc memcpy is broken, * but I don't believe the pointers to memcpy have to be aligned * either. Use fast memcpy on other machines anyway. */ /* proff - I took this out, because Oli Kraus (olikraus@yahoo.com) told me the memcpy produced a buserror. Since this function isn't time- critical I'm using the for loop now. */ /* #ifndef GCC memcpy(texture->name, mtexture->name, sizeof(texture->name)); #else */ { unsigned int j; for(j=0;j<sizeof(texture->name);j++) texture->name[j]=mtexture->name[j]; } /* #endif */ mpatch = mtexture->patches; patch = texture->patches; for (j=0 ; j<texture->patchcount ; j++, mpatch++, patch++) { patch->originx = SHORT(mpatch->originx); patch->originy = SHORT(mpatch->originy); patch->patch = patchlookup[SHORT(mpatch->patch)]; if (patch->patch == -1) { //jff 8/3/98 use logical output routine printf("\nR_InitTextures: Missing patch %d in texture %s", SHORT(mpatch->patch), texture->name); // killough 4/17/98 ++errors; } } texture->columnofs = NULL; texture->columnlump = NULL; for (j=1; j*2 <= texture->width; j<<=1) ; texture->widthmask = j-1; textureheight[i] = texture->height<<FRACBITS; totalwidth += texture->width; } free(patchlookup); // killough for (i=0; i<2; i++) // cph - release the TEXTUREx lumps if (maptex_lump[i] != -1) W_UnlockLumpNum(maptex_lump[i]); if (errors) I_Error("R_InitTextures: %d errors", errors); // Precalculate whatever possible. if (devparm) // cph - If in development mode, generate now so all errors are found at once for (i=0 ; i<numtextures ; i++) R_GenerateLookup(i, &errors); if (errors) I_Error("R_InitTextures: %d errors", errors); // Create translation table for global animation. // killough 4/9/98: make column offsets 32-bit; // clean up malloc-ing to use sizeof texturetranslation = Z_Malloc((numtextures+1)*sizeof*texturetranslation, PU_STATIC, 0); for (i=0 ; i<numtextures ; i++) texturetranslation[i] = i; // killough 1/31/98: Initialize texture hash table for (i = 0; i<numtextures; i++) textures[i]->index = -1; while (--i >= 0) { int j = W_LumpNameHash(textures[i]->name) % (unsigned) numtextures; textures[i]->next = textures[j]->index; // Prepend to chain textures[j]->index = i; } } // // R_InitFlats // void R_InitFlats(void) { int i; firstflat = W_GetNumForName("F_START") + 1; lastflat = W_GetNumForName("F_END") - 1; numflats = lastflat - firstflat + 1; // Create translation table for global animation. // killough 4/9/98: make column offsets 32-bit; // clean up malloc-ing to use sizeof flattranslation = Z_Malloc((numflats+1)*sizeof(*flattranslation), PU_STATIC, 0); for (i=0 ; i<numflats ; i++) flattranslation[i] = i; } // // R_InitSpriteLumps // Finds the width and hoffset of all sprites in the wad, // so the sprite does not need to be cached completely // just for having the header info ready during rendering. // void R_InitSpriteLumps(void) { int i; const patch_t *patch; firstspritelump = W_GetNumForName("S_START") + 1; lastspritelump = W_GetNumForName("S_END") - 1; numspritelumps = lastspritelump - firstspritelump + 1; // killough 4/9/98: make columnd offsets 32-bit; // clean up malloc-ing to use sizeof spritewidth = Z_Malloc(numspritelumps*sizeof*spritewidth, PU_STATIC, 0); spriteoffset = Z_Malloc(numspritelumps*sizeof*spriteoffset, PU_STATIC, 0); spritetopoffset = Z_Malloc(numspritelumps*sizeof*spritetopoffset, PU_STATIC, 0); for (i=0 ; i< numspritelumps ; i++) { patch = W_CacheLumpNum(firstspritelump+i); spritewidth[i] = SHORT(patch->width)<<FRACBITS; spriteoffset[i] = SHORT(patch->leftoffset)<<FRACBITS; spritetopoffset[i] = SHORT(patch->topoffset)<<FRACBITS; W_UnlockLumpNum(firstspritelump+i); } } // // R_InitColormaps // // killough 3/20/98: rewritten to allow dynamic colormaps // and to remove unnecessary 256-byte alignment // // killough 4/4/98: Add support for C_START/C_END markers // void R_InitColormaps(void) { int i; firstcolormaplump = W_GetNumForName("C_START"); lastcolormaplump = W_GetNumForName("C_END"); numcolormaps = lastcolormaplump - firstcolormaplump; colormaps = Z_Malloc(sizeof(*colormaps) * numcolormaps, PU_STATIC, 0); colormaps[0] = (lighttable_t *)W_CacheLumpName("COLORMAP"); for (i=1; i<numcolormaps; i++) colormaps[i] = (lighttable_t *)W_CacheLumpNum(i+firstcolormaplump); // cph - always lock } // killough 4/4/98: get colormap number from name // killough 4/11/98: changed to return -1 for illegal names // killough 4/17/98: changed to use ns_colormaps tag int R_ColormapNumForName(const char *name) { register int i = 0; if (strncasecmp(name,"COLORMAP",8)) // COLORMAP predefined to return 0 if ((i = (W_CheckNumForName)(name, ns_colormaps)) != -1) i -= firstcolormaplump; return i; } // // R_InitTranMap // // Initialize translucency filter map // // By Lee Killough 2/21/98 // int tran_filter_pct = 66; // filter percent #define TSC 12 /* number of fixed point digits in filter percent */ void R_InitTranMap(int progress) { struct _cache { unsigned char pct; unsigned char playpal[256]; } cache; int lump = W_CheckNumForName("TRANMAP"); // If a tranlucency filter map lump is present, use it if (lump != -1) // Set a pointer to the translucency filter maps. main_tranmap = W_CacheLumpNum(lump); // killough 4/11/98 else { // Compose a default transparent filter map based on PLAYPAL. const byte *playpal = W_CacheLumpName("PLAYPAL"); byte *my_tranmap; int cachefd = open(GAMEBASE"tranmap.dat", O_RDWR); main_tranmap = my_tranmap = Z_Malloc(256*256, PU_STATIC, 0); // killough 4/11/98 // Use cached translucency filter if it's available if ((cachefd<0) ? cachefd = open(GAMEBASE"tranmap.dat",O_WRONLY | O_CREAT, 0666) , 1 : read(cachefd, &cache, sizeof(cache)) != sizeof(cache) || cache.pct != tran_filter_pct || memcmp(cache.playpal, playpal, sizeof cache.playpal) || read(cachefd, my_tranmap, 256*256) != 256*256 ) // killough 4/11/98 { long *stackdeath=malloc(256*7*sizeof(long)); // This was a bunch of static varibles, way too big for rockbox long *pal[3], *tot, *pal_w1[3]; pal[0]=&stackdeath[0]; pal[1]=&stackdeath[256]; pal[2]=&stackdeath[256*2]; tot=&stackdeath[256*3]; pal_w1[0]=&stackdeath[256*4]; pal_w1[1]=&stackdeath[256*5]; pal_w1[2]=&stackdeath[256*6]; long w1 = ((unsigned long) tran_filter_pct<<TSC)/100; long w2 = (1l<<TSC)-w1; if (progress) printf("Please wait: Tranmap build"); // First, convert playpal into long int type, and transpose array, // for fast inner-loop calculations. Precompute tot array. { register int i = 255; register const unsigned char *p = playpal+255*3; do { register long t,d; pal_w1[0][i] = (pal[0][i] = t = p[0]) * w1; d = t*t; pal_w1[1][i] = (pal[1][i] = t = p[1]) * w1; d += t*t; pal_w1[2][i] = (pal[2][i] = t = p[2]) * w1; d += t*t; p -= 3; tot[i] = d << (TSC-1); } while (--i>=0); } // Next, compute all entries using minimum arithmetic. { int i,j; byte *tp = my_tranmap; for (i=0;i<256;i++) { long r1 = pal[0][i] * w2; long g1 = pal[1][i] * w2; long b1 = pal[2][i] * w2; if (!(i & 31) && progress) //jff 8/3/98 use logical output routine printf(" Computing: %d", 256/32-i/32); for (j=0;j<256;j++,tp++) { register int color = 255; register long err; long r = pal_w1[0][j] + r1; long g = pal_w1[1][j] + g1; long b = pal_w1[2][j] + b1; long best = LONG_MAX; do if ((err = tot[color] - pal[0][color]*r - pal[1][color]*g - pal[2][color]*b) < best) best = err, *tp = color; while (--color >= 0) ; } } } free(stackdeath); // Free this beast if (cachefd) // write out the cached translucency map { cache.pct = tran_filter_pct; memcpy(cache.playpal, playpal, 256); lseek(cachefd, 0, SEEK_SET); write(cachefd, &cache, sizeof cache); write(cachefd,main_tranmap, 256*256); // CPhipps - leave close for a few lines... } } if (cachefd) // killough 11/98: fix filehandle leak close(cachefd); W_UnlockLumpName("PLAYPAL"); } } // // R_InitData // Locates all the lumps // that will be used by all views // Must be called after W_Init. // void R_InitData (void) { R_InitTextures (); printf ("\nInitTextures"); R_InitFlats (); printf ("\nInitFlats"); R_InitSpriteLumps (); printf ("\nInitSprites"); if (general_translucency) // killough 3/1/98 R_InitTranMap(1); R_InitColormaps (); printf ("\nInitColormaps"); } // // R_FlatNumForName // Retrieval, get a flat number for a flat name. // // killough 4/17/98: changed to use ns_flats namespace // int R_FlatNumForName(const char *name) // killough -- const added { int i = (W_CheckNumForName)(name, ns_flats); if (i == -1) I_Error("R_FlatNumForName: %s not found", name); return i - firstflat; } // // R_CheckTextureNumForName // Check whether texture is available. // Filter out NoTexture indicator. // // Rewritten by Lee Killough to use hash table for fast lookup. Considerably // reduces the time needed to start new levels. See w_wad.c for comments on // the hashing algorithm, which is also used for lump searches. // // killough 1/21/98, 1/31/98 // int R_CheckTextureNumForName(const char *name) { int i = 0; if (*name != '-') // "NoTexture" marker. { i = textures[W_LumpNameHash(name) % (unsigned) numtextures]->index; while (i >= 0 && strncasecmp(textures[i]->name,name,8)) i = textures[i]->next; } return i; } // // R_TextureNumForName // Calls R_CheckTextureNumForName, // aborts with error message. // int R_TextureNumForName(const char *name) // const added -- killough { int i = R_CheckTextureNumForName(name); if (i == -1) I_Error("R_TextureNumForName: %s not found", name); return i; } // // R_PrecacheLevel // Preloads all relevant graphics for the level. // // Totally rewritten by Lee Killough to use less memory, // to avoid using alloca(), and to improve performance. // cph - new wad lump handling, calls cache functions but acquires no locks // Structures from p_spec.c // Used to fully cache animations in the level -> avoids stalls on Hard Drive Systems typedef struct { boolean istexture; int picnum; int basepic; int numpics; int speed; } anim_t; extern anim_t* anims; extern anim_t* lastanim; anim_t * isAnim(int flatnum, boolean texcheck) { anim_t *checkf; for(checkf=anims; checkf<lastanim; checkf++) { if((flatnum>=checkf->basepic || flatnum<=checkf->numpics)&&checkf->istexture==texcheck) return checkf; } return 0; } void R_PrecacheLevel(void) { register int i, j; register byte *hitlist; anim_t *cacheanim; if (demoplayback) return; { size_t size = numflats > numsprites ? numflats : numsprites; hitlist = malloc((size_t)numtextures > size ? (unsigned)numtextures : size); } // Precache flats. memset(hitlist, 0, numflats); for (i = numsectors; --i >= 0; ) hitlist[sectors[i].floorpic] = hitlist[sectors[i].ceilingpic] = 1; // If flat is an animation, load those too // Definately not the most efficient, but better then stalls in game for(i=0; i<numflats; i++) if(hitlist[i]) if((cacheanim=isAnim(i,0))) for(j=0; j<cacheanim->numpics; j++) hitlist[cacheanim->basepic+j]=1; for (i = numflats; --i >= 0; ) if (hitlist[i]) (W_CacheLumpNum)(firstflat + i, 0); // Precache textures. memset(hitlist, 0, numtextures); for (i = numsides; --i >= 0;) hitlist[sides[i].bottomtexture] = hitlist[sides[i].toptexture] = hitlist[sides[i].midtexture] = 1; // If texture is an animation, load those too // Definately not the most efficient, but better then stalls in game for(i=0; i<numsides; i++) if(hitlist[i]) if((cacheanim=isAnim(i,1))) for(j=0; j<cacheanim->numpics; j++) hitlist[cacheanim->basepic+j]=1; // Sky texture is always present. // Note that F_SKY1 is the name used to // indicate a sky floor/ceiling as a flat, // while the sky texture is stored like // a wall texture, with an episode dependend // name. hitlist[skytexture] = 1; for (i = numtextures; --i >= 0; ) if (hitlist[i]) { texture_t *texture = textures[i]; int j = texture->patchcount; while (--j >= 0) (W_CacheLumpNum)(texture->patches[j].patch, 0); } // Precache sprites. memset(hitlist, 0, numsprites); { thinker_t *th; for (th = thinkercap.next ; th != &thinkercap ; th=th->next) if (th->function == P_MobjThinker) hitlist[((mobj_t *)th)->sprite] = 1; } for (i=numsprites; --i >= 0;) if (hitlist[i]) { int j = sprites[i].numframes; while (--j >= 0) { short *sflump = sprites[i].spriteframes[j].lump; int k = 7; do (W_CacheLumpNum)(firstspritelump + sflump[k], 0); while (--k >= 0); } } free(hitlist); } // Proff - Added for OpenGL void R_SetPatchNum(patchnum_t *patchnum, const char *name) { patch_t *patch; patch = (patch_t *) W_CacheLumpName(name); patchnum->width = patch->width; patchnum->height = patch->height; patchnum->leftoffset = patch->leftoffset; patchnum->topoffset = patch->topoffset; patchnum->lumpnum = W_GetNumForName(name); W_UnlockLumpName(name); }