| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* Copyright (C) 1999 by |
| 8 |
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman |
| 9 |
* Copyright (C) 1999-2002 by |
| 10 |
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze |
| 11 |
* Copyright 2005, 2006 by |
| 12 |
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko |
| 13 |
* |
| 14 |
* This program is free software; you can redistribute it and/or |
| 15 |
* modify it under the terms of the GNU General Public License |
| 16 |
* as published by the Free Software Foundation; either version 2 |
| 17 |
* of the License, or (at your option) any later version. |
| 18 |
* |
| 19 |
* This program is distributed in the hope that it will be useful, |
| 20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
* GNU General Public License for more details. |
| 23 |
* |
| 24 |
* You should have received a copy of the GNU General Public License |
| 25 |
* along with this program; if not, write to the Free Software |
| 26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
* 02111-1307, USA. |
| 28 |
* |
| 29 |
*-----------------------------------------------------------------------------*/ |
| 30 |
|
| 31 |
/* |
| 32 |
**--------------------------------------------------------------------------- |
| 33 |
** Copyright 2004-2006 Randy Heit |
| 34 |
** All rights reserved. |
| 35 |
** |
| 36 |
** Redistribution and use in source and binary forms, with or without |
| 37 |
** modification, are permitted provided that the following conditions |
| 38 |
** are met: |
| 39 |
** |
| 40 |
** 1. Redistributions of source code must retain the above copyright |
| 41 |
** notice, this list of conditions and the following disclaimer. |
| 42 |
** 2. Redistributions in binary form must reproduce the above copyright |
| 43 |
** notice, this list of conditions and the following disclaimer in the |
| 44 |
** documentation and/or other materials provided with the distribution. |
| 45 |
** 3. The name of the author may not be used to endorse or promote products |
| 46 |
** derived from this software without specific prior written permission. |
| 47 |
** |
| 48 |
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 49 |
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 50 |
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 51 |
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 52 |
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 53 |
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 54 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 55 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 56 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 57 |
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 58 |
**--------------------------------------------------------------------------- |
| 59 |
*/ |
| 60 |
|
| 61 |
#include "z_zone.h" |
| 62 |
#include "doomstat.h" |
| 63 |
#include "w_wad.h" |
| 64 |
#include "r_main.h" |
| 65 |
#include "r_sky.h" |
| 66 |
#include "r_bsp.h" |
| 67 |
#include "r_things.h" |
| 68 |
#include "p_tick.h" |
| 69 |
#include "i_system.h" |
| 70 |
#include "r_draw.h" |
| 71 |
#include "lprintf.h" |
| 72 |
#include "r_patch.h" |
| 73 |
#include "v_video.h" |
| 74 |
#include <assert.h> |
| 75 |
|
| 76 |
#include "dsda/palette.h" |
| 77 |
|
| 78 |
// posts are runs of non masked source pixels |
| 79 |
typedef struct |
| 80 |
{ |
| 81 |
byte topdelta; // -1 is the last post in a column |
| 82 |
byte length; // length data bytes follows |
| 83 |
} post_t; |
| 84 |
|
| 85 |
// column_t is a list of 0 or more post_t, (byte)-1 terminated |
| 86 |
typedef post_t column_t; |
| 87 |
|
| 88 |
// |
| 89 |
// Patches. |
| 90 |
// A patch holds one or more columns. |
| 91 |
// Patches are used for sprites and all masked pictures, |
| 92 |
// and we compose textures from the TEXTURE1/2 lists |
| 93 |
// of patches. |
| 94 |
// |
| 95 |
|
| 96 |
typedef struct |
| 97 |
{ |
| 98 |
short width, height; // bounding box size |
| 99 |
short leftoffset; // pixels to the left of origin |
| 100 |
short topoffset; // pixels below the origin |
| 101 |
int columnofs[8]; // only [width] used |
| 102 |
} patch_t; |
| 103 |
|
| 104 |
//--------------------------------------------------------------------------- |
| 105 |
// Re-engineered patch support |
| 106 |
//--------------------------------------------------------------------------- |
| 107 |
static rpatch_t *patches = 0; |
| 108 |
|
| 109 |
static rpatch_t *texture_composites = 0; |
| 110 |
|
| 111 |
// indices of two duplicate PLAYPAL entries, second is -1 if none found |
| 112 |
static int playpal_transparent, playpal_duplicate; |
| 113 |
|
| 114 |
// indices of darkest and lightest colors |
| 115 |
int playpal_darkest; |
| 116 |
int playpal_lightest; |
| 117 |
|
| 118 |
//--------------------------------------------------------------------------- |
| 119 |
void R_InitPatches(void) { |
| 120 |
if (!patches) |
| 121 |
{ |
| 122 |
patches = Z_Malloc(numlumps * sizeof(rpatch_t)); |
| 123 |
// clear out new patches to signal they're uninitialized |
| 124 |
memset(patches, 0, sizeof(rpatch_t)*numlumps); |
| 125 |
} |
| 126 |
if (!texture_composites) |
| 127 |
{ |
| 128 |
texture_composites = Z_Malloc(numtextures * sizeof(rpatch_t)); |
| 129 |
// clear out new patches to signal they're uninitialized |
| 130 |
memset(texture_composites, 0, sizeof(rpatch_t)*numtextures); |
| 131 |
} |
| 132 |
|
| 133 |
dsda_InitPlayPal(); |
| 134 |
R_UpdatePlayPal(); |
| 135 |
} |
| 136 |
|
| 137 |
void R_UpdatePlayPal(void) { |
| 138 |
dsda_playpal_t* playpal_data; |
| 139 |
|
| 140 |
playpal_data = dsda_PlayPalData(); |
| 141 |
playpal_transparent = playpal_data->transparent; |
| 142 |
playpal_duplicate = playpal_data->duplicate; |
| 143 |
playpal_darkest = playpal_data->darkest; |
| 144 |
playpal_lightest = playpal_data->lightest; |
| 145 |
} |
| 146 |
|
| 147 |
//--------------------------------------------------------------------------- |
| 148 |
void R_FlushAllPatches(void) { |
| 149 |
int i; |
| 150 |
|
| 151 |
if (patches) |
| 152 |
{ |
| 153 |
Z_Free(patches); |
| 154 |
patches = NULL; |
| 155 |
} |
| 156 |
if (texture_composites) |
| 157 |
{ |
| 158 |
for (i=0; i<numtextures; i++) |
| 159 |
if (texture_composites[i].data) |
| 160 |
Z_Free(texture_composites[i].data); |
| 161 |
Z_Free(texture_composites); |
| 162 |
texture_composites = NULL; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
//--------------------------------------------------------------------------- |
| 167 |
int R_NumPatchWidth(int lump) |
| 168 |
{ |
| 169 |
const rpatch_t *patch = R_PatchByNum(lump); |
| 170 |
int width = patch->width; |
| 171 |
return width; |
| 172 |
} |
| 173 |
|
| 174 |
//--------------------------------------------------------------------------- |
| 175 |
int R_NumPatchHeight(int lump) |
| 176 |
{ |
| 177 |
const rpatch_t *patch = R_PatchByNum(lump); |
| 178 |
int height = patch->height; |
| 179 |
return height; |
| 180 |
} |
| 181 |
|
| 182 |
//--------------------------------------------------------------------------- |
| 183 |
static int getPatchIsNotTileable(const patch_t *patch) { |
| 184 |
int x=0, numPosts, lastColumnDelta = 0; |
| 185 |
const column_t *column; |
| 186 |
int cornerCount = 0; |
| 187 |
int hasAHole = 0; |
| 188 |
|
| 189 |
for (x=0; x<LittleShort(patch->width); x++) { |
| 190 |
column = (const column_t *)((const byte *)patch + LittleLong(patch->columnofs[x])); |
| 191 |
if (!x) lastColumnDelta = column->topdelta; |
| 192 |
else if (lastColumnDelta != column->topdelta) hasAHole = 1; |
| 193 |
|
| 194 |
numPosts = 0; |
| 195 |
while (column->topdelta != 0xff) { |
| 196 |
// check to see if a corner pixel filled |
| 197 |
if (x == 0 && column->topdelta == 0) cornerCount++; |
| 198 |
else if (x == 0 && column->topdelta + column->length >= LittleShort(patch->height)) cornerCount++; |
| 199 |
else if (x == LittleShort(patch->width)-1 && column->topdelta == 0) cornerCount++; |
| 200 |
else if (x == LittleShort(patch->width)-1 && column->topdelta + column->length >= LittleShort(patch->height)) cornerCount++; |
| 201 |
|
| 202 |
if (numPosts++) hasAHole = 1; |
| 203 |
column = (const column_t *)((const byte *)column + column->length + 4); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if (cornerCount == 4) return 0; |
| 208 |
return hasAHole; |
| 209 |
} |
| 210 |
|
| 211 |
//--------------------------------------------------------------------------- |
| 212 |
static int getIsSolidAtSpot(const column_t *column, int spot) { |
| 213 |
if (!column) return 0; |
| 214 |
while (column->topdelta != 0xff) { |
| 215 |
if (spot < column->topdelta) return 0; |
| 216 |
if ((spot >= column->topdelta) && (spot <= column->topdelta + column->length)) return 1; |
| 217 |
column = (const column_t*)((const byte*)column + 3 + column->length + 1); |
| 218 |
} |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
//--------------------------------------------------------------------------- |
| 223 |
// Used to determine whether a column edge (top or bottom) should slope |
| 224 |
// up or down for smoothed masked edges - POPE |
| 225 |
//--------------------------------------------------------------------------- |
| 226 |
static int getColumnEdgeSlope(const column_t *prevcolumn, const column_t *nextcolumn, int spot) { |
| 227 |
int holeToLeft = !getIsSolidAtSpot(prevcolumn, spot); |
| 228 |
int holeToRight = !getIsSolidAtSpot(nextcolumn, spot); |
| 229 |
|
| 230 |
if (holeToLeft && !holeToRight) return 1; |
| 231 |
if (!holeToLeft && holeToRight) return -1; |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
//--------------------------------------------------------------------------- |
| 236 |
static void FillEmptySpace(rpatch_t *patch) |
| 237 |
{ |
| 238 |
int x, y, w, h, numpix, pass, transparent, has_holes; |
| 239 |
byte *orig, *copy, *src, *dest, *prev, *next; |
| 240 |
|
| 241 |
// loop over patch looking for transparent pixels next to solid ones |
| 242 |
// copy solid pixels into the spaces, dilating the patch outwards |
| 243 |
// repeat either a fixed number of times or until all space is filled |
| 244 |
// this eliminates artifacts surrounding weapon sprites (e.g. chainsaw) |
| 245 |
|
| 246 |
w = patch->width; |
| 247 |
h = patch->height; |
| 248 |
numpix = w * h; |
| 249 |
|
| 250 |
// alternate between two buffers to avoid "overlapping memcpy"-like symptoms |
| 251 |
orig = patch->pixels; |
| 252 |
copy = Z_Malloc(numpix); |
| 253 |
|
| 254 |
for (pass = 0; pass < 8; pass++) // arbitrarily chosen limit (must be even) |
| 255 |
{ |
| 256 |
// copy src to dest, then switch them on next pass |
| 257 |
// (this requires an even number of passes) |
| 258 |
src = ((pass & 1) == 0) ? orig : copy; |
| 259 |
dest = ((pass & 1) == 0) ? copy : orig; |
| 260 |
|
| 261 |
// previous and next columns adjacent to current column x |
| 262 |
// these pointers must not be dereferenced without appropriate checks |
| 263 |
prev = src - h; // only valid when x > 0 |
| 264 |
next = src + h; // only valid when x < w-1 |
| 265 |
|
| 266 |
has_holes = 0; // if the patch has any holes at all |
| 267 |
transparent = 0; // number of pixels this pass did not handle |
| 268 |
|
| 269 |
// detect transparent pixels on edges, copy solid colour into the space |
| 270 |
// the order of directions (up,down,left,right) is arbitrarily chosen |
| 271 |
for (x = 0; x < w; x++) |
| 272 |
{ |
| 273 |
for (y = 0; y < h; y++) |
| 274 |
{ |
| 275 |
if (*src == playpal_transparent) has_holes = 1; |
| 276 |
|
| 277 |
if (*src != playpal_transparent) |
| 278 |
*dest = *src; // already a solid pixel, just copy it over |
| 279 |
else if (y > 0 && *(src-1) != playpal_transparent) |
| 280 |
*dest = *(src - 1); // solid pixel above |
| 281 |
else if (y < h-1 && *(src+1) != playpal_transparent) |
| 282 |
*dest = *(src + 1); // solid pixel below |
| 283 |
else if (x > 0 && *prev != playpal_transparent) |
| 284 |
*dest = *prev; // solid pixel to left |
| 285 |
else if (x < w-1 && *next != playpal_transparent) |
| 286 |
*dest = *next; // solid pixel to right |
| 287 |
else // transparent pixel with no adjacent solid pixels |
| 288 |
*dest = *src, transparent++; // count unhandled pixels |
| 289 |
|
| 290 |
prev++, src++, next++, dest++; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
if (transparent == 0) // no more transparent pixels to fill |
| 295 |
{ |
| 296 |
if ((pass & 1) == 0) // dest was copy, src was orig: orig needs update |
| 297 |
memcpy(orig, copy, numpix); |
| 298 |
break; |
| 299 |
} |
| 300 |
else if (transparent == numpix) |
| 301 |
break; // avoid infinite loop on entirely transparent patches |
| 302 |
} |
| 303 |
|
| 304 |
Z_Free(copy); |
| 305 |
|
| 306 |
// copy top row of patch into any space at bottom, and vice versa |
| 307 |
// a hack to fix erroneous row of pixels at top of firing chaingun |
| 308 |
|
| 309 |
for (x = 0, src = orig, dest = src + h-1; x < w; x++, src += h, dest += h) |
| 310 |
{ |
| 311 |
if (*src != playpal_transparent && *dest == playpal_transparent) |
| 312 |
*dest = *src; // bottom transparent, top solid |
| 313 |
else if (*src == playpal_transparent && *dest != playpal_transparent) |
| 314 |
*src = *dest; // top transparent, bottom solid |
| 315 |
} |
| 316 |
|
| 317 |
if (has_holes) |
| 318 |
patch->flags |= PATCH_HASHOLES; |
| 319 |
} |
| 320 |
|
| 321 |
//========================================================================== |
| 322 |
// |
| 323 |
// Checks if the lump can be a Doom patch |
| 324 |
// |
| 325 |
//========================================================================== |
| 326 |
|
| 327 |
dboolean R_IsPatchLump(int lumpnum) |
| 328 |
{ |
| 329 |
int size; |
| 330 |
int width, height; |
| 331 |
const patch_t * patch; |
| 332 |
dboolean result; |
| 333 |
|
| 334 |
size = W_LumpLength(lumpnum); |
| 335 |
|
| 336 |
// minimum length of a valid Doom patch |
| 337 |
if (size < 13) |
| 338 |
return false; |
| 339 |
|
| 340 |
patch = (const patch_t *)W_LumpByNum(lumpnum); |
| 341 |
|
| 342 |
width = LittleShort(patch->width); |
| 343 |
height = LittleShort(patch->height); |
| 344 |
|
| 345 |
result = (height > 0 && height <= 16384 && width > 0 && width <= 16384 && width < size / 4); |
| 346 |
|
| 347 |
if (result) |
| 348 |
{ |
| 349 |
// The dimensions seem like they might be valid for a patch, so |
| 350 |
// check the column directory for extra security. All columns |
| 351 |
// must begin after the column directory, and none of them must |
| 352 |
// point past the end of the patch. |
| 353 |
int x; |
| 354 |
|
| 355 |
for (x = 0; x < width; x++) |
| 356 |
{ |
| 357 |
unsigned int ofs = LittleLong(patch->columnofs[x]); |
| 358 |
|
| 359 |
// Need one byte for an empty column (but there's patches that don't know that!) |
| 360 |
if (ofs < (unsigned int)width * 4 + 8 || ofs >= (unsigned int)size) |
| 361 |
{ |
| 362 |
result = false; |
| 363 |
break; |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
return result; |
| 369 |
} |
| 370 |
|
| 371 |
//--------------------------------------------------------------------------- |
| 372 |
static void StorePixel(rpatch_t *patch, int x, int y, byte color) |
| 373 |
{ |
| 374 |
// write pixel to patch, substituting for playpal_transparent as needed |
| 375 |
if (color == playpal_transparent && playpal_duplicate >= 0) |
| 376 |
color = playpal_duplicate; |
| 377 |
patch->pixels[x * patch->height + y] = color; |
| 378 |
} |
| 379 |
|
| 380 |
//--------------------------------------------------------------------------- |
| 381 |
static void createPatch(int id) { |
| 382 |
rpatch_t *patch; |
| 383 |
const int patchNum = id; |
| 384 |
const patch_t *oldPatch; |
| 385 |
const column_t *oldColumn, *oldPrevColumn, *oldNextColumn; |
| 386 |
int x, y; |
| 387 |
int pixelDataSize; |
| 388 |
int columnsDataSize; |
| 389 |
int postsDataSize; |
| 390 |
int dataSize; |
| 391 |
int *numPostsInColumn; |
| 392 |
int numPostsTotal; |
| 393 |
const unsigned char *oldColumnPixelData; |
| 394 |
int numPostsUsedSoFar; |
| 395 |
int edgeSlope; |
| 396 |
|
| 397 |
#ifdef RANGECHECK |
| 398 |
if (id >= numlumps) |
| 399 |
I_Error("createPatch: %i >= numlumps", id); |
| 400 |
#endif |
| 401 |
|
| 402 |
if (!R_IsPatchLump(patchNum)) |
| 403 |
{ |
| 404 |
I_Error("createPatch: Unknown patch format %s.", |
| 405 |
(patchNum < numlumps ? lumpinfo[patchNum].name : NULL)); |
| 406 |
} |
| 407 |
|
| 408 |
oldPatch = (const patch_t*)W_LumpByNum(patchNum); |
| 409 |
|
| 410 |
patch = &patches[id]; |
| 411 |
// proff - 2003-02-16 What about endianess? |
| 412 |
patch->width = LittleShort(oldPatch->width); |
| 413 |
patch->widthmask = 0; |
| 414 |
patch->height = LittleShort(oldPatch->height); |
| 415 |
patch->leftoffset = LittleShort(oldPatch->leftoffset); |
| 416 |
patch->topoffset = LittleShort(oldPatch->topoffset); |
| 417 |
patch->flags = 0; |
| 418 |
if (getPatchIsNotTileable(oldPatch)) |
| 419 |
patch->flags |= PATCH_ISNOTTILEABLE; |
| 420 |
|
| 421 |
// Width of M_THERMM patch is 9, but Doom interprets it as 8-columns lump |
| 422 |
// during drawing. It is not a problem for software mode and GL_NEAREST, |
| 423 |
// but looks wrong with filtering. So I need to patch it during loading. |
| 424 |
if (V_IsOpenGLMode()) |
| 425 |
{ |
| 426 |
if (!strncasecmp(lumpinfo[id].name, "M_THERMM", 8) && patch->width > 8) |
| 427 |
{ |
| 428 |
patch->width--; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
// work out how much memory we need to allocate for this patch's data |
| 433 |
pixelDataSize = (patch->width * patch->height + 4) & ~3; |
| 434 |
columnsDataSize = sizeof(rcolumn_t) * patch->width; |
| 435 |
|
| 436 |
// count the number of posts in each column |
| 437 |
numPostsInColumn = Z_Malloc(sizeof(int) * patch->width); |
| 438 |
numPostsTotal = 0; |
| 439 |
|
| 440 |
for (x=0; x<patch->width; x++) { |
| 441 |
oldColumn = (const column_t *)((const byte *)oldPatch + LittleLong(oldPatch->columnofs[x])); |
| 442 |
numPostsInColumn[x] = 0; |
| 443 |
while (oldColumn->topdelta != 0xff) { |
| 444 |
numPostsInColumn[x]++; |
| 445 |
numPostsTotal++; |
| 446 |
oldColumn = (const column_t *)((const byte *)oldColumn + oldColumn->length + 4); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
postsDataSize = numPostsTotal * sizeof(rpost_t); |
| 451 |
|
| 452 |
// allocate our data chunk |
| 453 |
dataSize = pixelDataSize + columnsDataSize + postsDataSize; |
| 454 |
patch->data = (unsigned char*) Z_Malloc(dataSize); |
| 455 |
memset(patch->data, 0, dataSize); |
| 456 |
|
| 457 |
// set out pixel, column, and post pointers into our data array |
| 458 |
patch->pixels = patch->data; |
| 459 |
patch->columns = (rcolumn_t*)((unsigned char*)patch->pixels + pixelDataSize); |
| 460 |
patch->posts = (rpost_t*)((unsigned char*)patch->columns + columnsDataSize); |
| 461 |
|
| 462 |
// sanity check that we've got all the memory allocated we need |
| 463 |
assert((((byte*)patch->posts + numPostsTotal*sizeof(rpost_t)) - (byte*)patch->data) == dataSize); |
| 464 |
|
| 465 |
if (playpal_transparent != 0) |
| 466 |
memset(patch->pixels, playpal_transparent, (patch->width*patch->height)); |
| 467 |
|
| 468 |
// fill in the pixels, posts, and columns |
| 469 |
numPostsUsedSoFar = 0; |
| 470 |
for (x=0; x<patch->width; x++) { |
| 471 |
int top = -1; |
| 472 |
|
| 473 |
oldColumn = (const column_t *)((const byte *)oldPatch + LittleLong(oldPatch->columnofs[x])); |
| 474 |
|
| 475 |
if (patch->flags&PATCH_ISNOTTILEABLE) { |
| 476 |
// non-tiling |
| 477 |
if (x == 0) oldPrevColumn = 0; |
| 478 |
else oldPrevColumn = (const column_t *)((const byte *)oldPatch + LittleLong(oldPatch->columnofs[x-1])); |
| 479 |
if (x == patch->width-1) oldNextColumn = 0; |
| 480 |
else oldNextColumn = (const column_t *)((const byte *)oldPatch + LittleLong(oldPatch->columnofs[x+1])); |
| 481 |
} |
| 482 |
else { |
| 483 |
// tiling |
| 484 |
int prevColumnIndex = x-1; |
| 485 |
int nextColumnIndex = x+1; |
| 486 |
while (prevColumnIndex < 0) prevColumnIndex += patch->width; |
| 487 |
while (nextColumnIndex >= patch->width) nextColumnIndex -= patch->width; |
| 488 |
oldPrevColumn = (const column_t *)((const byte *)oldPatch + LittleLong(oldPatch->columnofs[prevColumnIndex])); |
| 489 |
oldNextColumn = (const column_t *)((const byte *)oldPatch + LittleLong(oldPatch->columnofs[nextColumnIndex])); |
| 490 |
} |
| 491 |
|
| 492 |
// setup the column's data |
| 493 |
patch->columns[x].pixels = patch->pixels + (x*patch->height) + 0; |
| 494 |
patch->columns[x].numPosts = numPostsInColumn[x]; |
| 495 |
patch->columns[x].posts = patch->posts + numPostsUsedSoFar; |
| 496 |
|
| 497 |
while (oldColumn->topdelta != 0xff) { |
| 498 |
int len = oldColumn->length; |
| 499 |
|
| 500 |
//e6y: support for DeePsea's true tall patches |
| 501 |
if (oldColumn->topdelta <= top) |
| 502 |
{ |
| 503 |
top += oldColumn->topdelta; |
| 504 |
} |
| 505 |
else |
| 506 |
{ |
| 507 |
top = oldColumn->topdelta; |
| 508 |
} |
| 509 |
|
| 510 |
// Clip posts that extend past the bottom |
| 511 |
if (top + oldColumn->length > patch->height) |
| 512 |
{ |
| 513 |
len = patch->height - top; |
| 514 |
} |
| 515 |
|
| 516 |
if (len > 0) |
| 517 |
{ |
| 518 |
// set up the post's data |
| 519 |
patch->posts[numPostsUsedSoFar].topdelta = top; |
| 520 |
patch->posts[numPostsUsedSoFar].length = len; |
| 521 |
patch->posts[numPostsUsedSoFar].slope = 0; |
| 522 |
|
| 523 |
edgeSlope = getColumnEdgeSlope(oldPrevColumn, oldNextColumn, top); |
| 524 |
if (edgeSlope == 1) patch->posts[numPostsUsedSoFar].slope |= RDRAW_EDGESLOPE_TOP_UP; |
| 525 |
else if (edgeSlope == -1) patch->posts[numPostsUsedSoFar].slope |= RDRAW_EDGESLOPE_TOP_DOWN; |
| 526 |
|
| 527 |
edgeSlope = getColumnEdgeSlope(oldPrevColumn, oldNextColumn, top+len); |
| 528 |
if (edgeSlope == 1) patch->posts[numPostsUsedSoFar].slope |= RDRAW_EDGESLOPE_BOT_UP; |
| 529 |
else if (edgeSlope == -1) patch->posts[numPostsUsedSoFar].slope |= RDRAW_EDGESLOPE_BOT_DOWN; |
| 530 |
|
| 531 |
// fill in the post's pixels |
| 532 |
oldColumnPixelData = (const byte *)oldColumn + 3; |
| 533 |
for (y=0; y<len; y++) { |
| 534 |
StorePixel(patch, x, top + y, oldColumnPixelData[y]); |
| 535 |
} |
| 536 |
} |
| 537 |
oldColumn = (const column_t *)((const byte *)oldColumn + oldColumn->length + 4); |
| 538 |
numPostsUsedSoFar++; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
FillEmptySpace(patch); |
| 543 |
|
| 544 |
Z_Free(numPostsInColumn); |
| 545 |
} |
| 546 |
|
| 547 |
typedef struct { |
| 548 |
unsigned short patches; |
| 549 |
unsigned short posts; |
| 550 |
unsigned short posts_used; |
| 551 |
} count_t; |
| 552 |
|
| 553 |
static void switchPosts(rpost_t *post1, rpost_t *post2) { |
| 554 |
rpost_t dummy; |
| 555 |
|
| 556 |
dummy.topdelta = post1->topdelta; |
| 557 |
dummy.length = post1->length; |
| 558 |
dummy.slope = post1->slope; |
| 559 |
post1->topdelta = post2->topdelta; |
| 560 |
post1->length = post2->length; |
| 561 |
post1->slope = post2->slope; |
| 562 |
post2->topdelta = dummy.topdelta; |
| 563 |
post2->length = dummy.length; |
| 564 |
post2->slope = dummy.slope; |
| 565 |
} |
| 566 |
|
| 567 |
static void removePostFromColumn(rcolumn_t *column, int post) { |
| 568 |
int i; |
| 569 |
#ifdef RANGECHECK |
| 570 |
if (post >= column->numPosts) |
| 571 |
I_Error("removePostFromColumn: invalid post index"); |
| 572 |
#endif |
| 573 |
if (post < column->numPosts) |
| 574 |
for (i=post; i<(column->numPosts-1); i++) { |
| 575 |
rpost_t *post1 = &column->posts[i]; |
| 576 |
rpost_t *post2 = &column->posts[i+1]; |
| 577 |
post1->topdelta = post2->topdelta; |
| 578 |
post1->length = post2->length; |
| 579 |
post1->slope = post2->slope; |
| 580 |
} |
| 581 |
column->numPosts--; |
| 582 |
} |
| 583 |
|
| 584 |
//--------------------------------------------------------------------------- |
| 585 |
static void createTextureCompositePatch(int id) { |
| 586 |
rpatch_t *composite_patch; |
| 587 |
texture_t *texture; |
| 588 |
texpatch_t *texpatch; |
| 589 |
int patchNum; |
| 590 |
const patch_t *oldPatch; |
| 591 |
const column_t *oldColumn, *oldPrevColumn, *oldNextColumn; |
| 592 |
int i, x, y; |
| 593 |
int oy, count; |
| 594 |
int pixelDataSize; |
| 595 |
int columnsDataSize; |
| 596 |
int postsDataSize; |
| 597 |
int dataSize; |
| 598 |
int numPostsTotal; |
| 599 |
const unsigned char *oldColumnPixelData; |
| 600 |
int numPostsUsedSoFar; |
| 601 |
int edgeSlope; |
| 602 |
count_t *countsInColumn; |
| 603 |
|
| 604 |
#ifdef RANGECHECK |
| 605 |
if (id >= numtextures) |
| 606 |
I_Error("createTextureCompositePatch: %i >= numtextures", id); |
| 607 |
#endif |
| 608 |
|
| 609 |
composite_patch = &texture_composites[id]; |
| 610 |
|
| 611 |
texture = textures[id]; |
| 612 |
|
| 613 |
composite_patch->width = texture->width; |
| 614 |
composite_patch->height = texture->height; |
| 615 |
composite_patch->widthmask = texture->widthmask; |
| 616 |
composite_patch->leftoffset = 0; |
| 617 |
composite_patch->topoffset = 0; |
| 618 |
composite_patch->flags = 0; |
| 619 |
|
| 620 |
// work out how much memory we need to allocate for this patch's data |
| 621 |
pixelDataSize = (composite_patch->width * composite_patch->height + 4) & ~3; |
| 622 |
columnsDataSize = sizeof(rcolumn_t) * composite_patch->width; |
| 623 |
|
| 624 |
// count the number of posts in each column |
| 625 |
countsInColumn = (count_t *)Z_Calloc(sizeof(count_t), composite_patch->width); |
| 626 |
numPostsTotal = 0; |
| 627 |
|
| 628 |
for (i=0; i<texture->patchcount; i++) { |
| 629 |
texpatch = &texture->patches[i]; |
| 630 |
patchNum = texpatch->patch; |
| 631 |
oldPatch = (const patch_t*)W_LumpByNum(patchNum); |
| 632 |
|
| 633 |
for (x=0; x<LittleShort(oldPatch->width); x++) { |
| 634 |
int tx = texpatch->originx + x; |
| 635 |
|
| 636 |
if (tx < 0) |
| 637 |
continue; |
| 638 |
if (tx >= composite_patch->width) |
| 639 |
break; |
| 640 |
|
| 641 |
countsInColumn[tx].patches++; |
| 642 |
|
| 643 |
oldColumn = (const column_t *)((const byte *)oldPatch + LittleLong(oldPatch->columnofs[x])); |
| 644 |
while (oldColumn->topdelta != 0xff) { |
| 645 |
countsInColumn[tx].posts++; |
| 646 |
numPostsTotal++; |
| 647 |
oldColumn = (const column_t *)((const byte *)oldColumn + oldColumn->length + 4); |
| 648 |
} |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
postsDataSize = numPostsTotal * sizeof(rpost_t); |
| 653 |
|
| 654 |
// allocate our data chunk |
| 655 |
dataSize = pixelDataSize + columnsDataSize + postsDataSize; |
| 656 |
composite_patch->data = (unsigned char*) Z_Malloc(dataSize); |
| 657 |
memset(composite_patch->data, 0, dataSize); |
| 658 |
|
| 659 |
// set out pixel, column, and post pointers into our data array |
| 660 |
composite_patch->pixels = composite_patch->data; |
| 661 |
composite_patch->columns = (rcolumn_t*)((unsigned char*)composite_patch->pixels + pixelDataSize); |
| 662 |
composite_patch->posts = (rpost_t*)((unsigned char*)composite_patch->columns + columnsDataSize); |
| 663 |
|
| 664 |
// sanity check that we've got all the memory allocated we need |
| 665 |
assert((((byte*)composite_patch->posts + numPostsTotal*sizeof(rpost_t)) - (byte*)composite_patch->data) == dataSize); |
| 666 |
|
| 667 |
if (playpal_transparent != 0) |
| 668 |
memset(composite_patch->pixels, playpal_transparent, |
| 669 |
(composite_patch->width*composite_patch->height)); |
| 670 |
|
| 671 |
numPostsUsedSoFar = 0; |
| 672 |
|
| 673 |
for (x=0; x<texture->width; x++) { |
| 674 |
// setup the column's data |
| 675 |
composite_patch->columns[x].pixels = composite_patch->pixels + (x*composite_patch->height); |
| 676 |
composite_patch->columns[x].numPosts = countsInColumn[x].posts; |
| 677 |
composite_patch->columns[x].posts = composite_patch->posts + numPostsUsedSoFar; |
| 678 |
numPostsUsedSoFar += countsInColumn[x].posts; |
| 679 |
} |
| 680 |
|
| 681 |
// fill in the pixels, posts, and columns |
| 682 |
for (i=0; i<texture->patchcount; i++) { |
| 683 |
texpatch = &texture->patches[i]; |
| 684 |
patchNum = texpatch->patch; |
| 685 |
oldPatch = (const patch_t*)W_LumpByNum(patchNum); |
| 686 |
|
| 687 |
for (x=0; x<LittleShort(oldPatch->width); x++) { |
| 688 |
int top = -1; |
| 689 |
int tx = texpatch->originx + x; |
| 690 |
|
| 691 |
if (tx < 0) |
| 692 |
continue; |
| 693 |
if (tx >= composite_patch->width) |
| 694 |
break; |
| 695 |
|
| 696 |
oldColumn = (const column_t *)((const byte *)oldPatch + LittleLong(oldPatch->columnofs[x])); |
| 697 |
|
| 698 |
{ |
| 699 |
// tiling |
| 700 |
int prevColumnIndex = x-1; |
| 701 |
int nextColumnIndex = x+1; |
| 702 |
while (prevColumnIndex < 0) prevColumnIndex += LittleShort(oldPatch->width); |
| 703 |
while (nextColumnIndex >= LittleShort(oldPatch->width)) nextColumnIndex -= LittleShort(oldPatch->width); |
| 704 |
oldPrevColumn = (const column_t *)((const byte *)oldPatch + LittleLong(oldPatch->columnofs[prevColumnIndex])); |
| 705 |
oldNextColumn = (const column_t *)((const byte *)oldPatch + LittleLong(oldPatch->columnofs[nextColumnIndex])); |
| 706 |
} |
| 707 |
|
| 708 |
while (oldColumn->topdelta != 0xff) { |
| 709 |
rpost_t *post = &composite_patch->columns[tx].posts[countsInColumn[tx].posts_used]; |
| 710 |
|
| 711 |
//e6y: support for DeePsea's true tall patches |
| 712 |
if (oldColumn->topdelta <= top) |
| 713 |
{ |
| 714 |
top += oldColumn->topdelta; |
| 715 |
} |
| 716 |
else |
| 717 |
{ |
| 718 |
top = oldColumn->topdelta; |
| 719 |
} |
| 720 |
|
| 721 |
oldColumnPixelData = (const byte *)oldColumn + 3; |
| 722 |
oy = texpatch->originy; |
| 723 |
count = oldColumn->length; |
| 724 |
// the original renderer had several bugs which we reproduce here |
| 725 |
if (countsInColumn[tx].patches > 1) { |
| 726 |
// when there are multiple patches, then we need to handle the |
| 727 |
// column differently |
| 728 |
if (i == 0) { |
| 729 |
// draw first patch at original position, it will be partly |
| 730 |
// overdrawn below |
| 731 |
for (y=0; y<count; y++) { |
| 732 |
int ty = oy + top + y; |
| 733 |
if (ty < 0) |
| 734 |
continue; |
| 735 |
if (ty >= composite_patch->height) |
| 736 |
break; |
| 737 |
StorePixel(composite_patch, tx, ty, oldColumnPixelData[y]); |
| 738 |
} |
| 739 |
} |
| 740 |
// do the buggy clipping |
| 741 |
if ((oy + top) < 0) { |
| 742 |
count += oy; |
| 743 |
oy = 0; |
| 744 |
} |
| 745 |
} else { |
| 746 |
// with a single patch only negative y origins are wrong |
| 747 |
oy = 0; |
| 748 |
} |
| 749 |
// set up the post's data |
| 750 |
post->topdelta = top + oy; |
| 751 |
post->length = count; |
| 752 |
if ((post->topdelta + post->length) > composite_patch->height) { |
| 753 |
if (post->topdelta > composite_patch->height) |
| 754 |
post->length = 0; |
| 755 |
else |
| 756 |
post->length = composite_patch->height - post->topdelta; |
| 757 |
} |
| 758 |
if (post->topdelta < 0) { |
| 759 |
if ((post->topdelta + post->length) <= 0) |
| 760 |
post->length = 0; |
| 761 |
else |
| 762 |
post->length -= post->topdelta; |
| 763 |
post->topdelta = 0; |
| 764 |
} |
| 765 |
post->slope = 0; |
| 766 |
|
| 767 |
edgeSlope = getColumnEdgeSlope(oldPrevColumn, oldNextColumn, top); |
| 768 |
if (edgeSlope == 1) post->slope |= RDRAW_EDGESLOPE_TOP_UP; |
| 769 |
else if (edgeSlope == -1) post->slope |= RDRAW_EDGESLOPE_TOP_DOWN; |
| 770 |
|
| 771 |
edgeSlope = getColumnEdgeSlope(oldPrevColumn, oldNextColumn, top+count); |
| 772 |
if (edgeSlope == 1) post->slope |= RDRAW_EDGESLOPE_BOT_UP; |
| 773 |
else if (edgeSlope == -1) post->slope |= RDRAW_EDGESLOPE_BOT_DOWN; |
| 774 |
|
| 775 |
// fill in the post's pixels |
| 776 |
for (y=0; y<count; y++) { |
| 777 |
int ty = oy + top + y; |
| 778 |
if (ty < 0) |
| 779 |
continue; |
| 780 |
if (ty >= composite_patch->height) |
| 781 |
break; |
| 782 |
StorePixel(composite_patch, tx, ty, oldColumnPixelData[y]); |
| 783 |
} |
| 784 |
|
| 785 |
oldColumn = (const column_t *)((const byte *)oldColumn + oldColumn->length + 4); |
| 786 |
countsInColumn[tx].posts_used++; |
| 787 |
assert(countsInColumn[tx].posts_used <= countsInColumn[tx].posts); |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
for (x=0; x<texture->width; x++) { |
| 793 |
rcolumn_t *column; |
| 794 |
|
| 795 |
if (countsInColumn[x].patches <= 1) |
| 796 |
continue; |
| 797 |
|
| 798 |
// cleanup posts on multipatch columns |
| 799 |
column = &composite_patch->columns[x]; |
| 800 |
|
| 801 |
i = 0; |
| 802 |
while (i<(column->numPosts-1)) { |
| 803 |
rpost_t *post1 = &column->posts[i]; |
| 804 |
rpost_t *post2 = &column->posts[i+1]; |
| 805 |
int length; |
| 806 |
|
| 807 |
if ((post2->topdelta - post1->topdelta) < 0) |
| 808 |
switchPosts(post1, post2); |
| 809 |
|
| 810 |
if ((post1->topdelta + post1->length) >= post2->topdelta) { |
| 811 |
length = (post1->length + post2->length) - ((post1->topdelta + post1->length) - post2->topdelta); |
| 812 |
if (post1->length < length) { |
| 813 |
post1->slope = post2->slope; |
| 814 |
post1->length = length; |
| 815 |
} |
| 816 |
removePostFromColumn(column, i+1); |
| 817 |
i = 0; |
| 818 |
continue; |
| 819 |
} |
| 820 |
i++; |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
FillEmptySpace(composite_patch); |
| 825 |
|
| 826 |
Z_Free(countsInColumn); |
| 827 |
} |
| 828 |
|
| 829 |
//--------------------------------------------------------------------------- |
| 830 |
const rpatch_t *R_PatchByNum(int id) { |
| 831 |
if (!patches) |
| 832 |
I_Error("R_PatchByNum: Patches not initialized"); |
| 833 |
|
| 834 |
#ifdef RANGECHECK |
| 835 |
if (id >= numlumps) |
| 836 |
I_Error("createPatch: %i >= numlumps", id); |
| 837 |
#endif |
| 838 |
|
| 839 |
if (!patches[id].data) |
| 840 |
createPatch(id); |
| 841 |
|
| 842 |
return &patches[id]; |
| 843 |
} |
| 844 |
|
| 845 |
//--------------------------------------------------------------------------- |
| 846 |
const rpatch_t *R_TextureCompositePatchByNum(int id) { |
| 847 |
if (!texture_composites) |
| 848 |
I_Error("R_TextureCompositePatchByNum: Composite patches not initialized"); |
| 849 |
|
| 850 |
#ifdef RANGECHECK |
| 851 |
if (id >= numtextures) |
| 852 |
I_Error("createTextureCompositePatch: %i >= numtextures", id); |
| 853 |
#endif |
| 854 |
|
| 855 |
if (!texture_composites[id].data) |
| 856 |
createTextureCompositePatch(id); |
| 857 |
|
| 858 |
return &texture_composites[id]; |
| 859 |
|
| 860 |
} |
| 861 |
|
| 862 |
//--------------------------------------------------------------------------- |
| 863 |
const rcolumn_t *R_GetPatchColumnWrapped(const rpatch_t *patch, int columnIndex) { |
| 864 |
while (columnIndex < 0) columnIndex += patch->width; |
| 865 |
columnIndex %= patch->width; |
| 866 |
return &patch->columns[columnIndex]; |
| 867 |
} |
| 868 |
|
| 869 |
//--------------------------------------------------------------------------- |
| 870 |
const rcolumn_t *R_GetPatchColumnClamped(const rpatch_t *patch, int columnIndex) { |
| 871 |
if (columnIndex < 0) columnIndex = 0; |
| 872 |
if (columnIndex >= patch->width) columnIndex = patch->width-1; |
| 873 |
return &patch->columns[columnIndex]; |
| 874 |
} |
| 875 |
|
| 876 |
//--------------------------------------------------------------------------- |
| 877 |
const rcolumn_t *R_GetPatchColumn(const rpatch_t *patch, int columnIndex) { |
| 878 |
if (patch->flags&PATCH_ISNOTTILEABLE) return R_GetPatchColumnClamped(patch, columnIndex); |
| 879 |
else return R_GetPatchColumnWrapped(patch, columnIndex); |
| 880 |
} |