You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1089 lines
24 KiB
1089 lines
24 KiB
/** |
|
* @file engine.cpp |
|
* |
|
* Implementation of basic engine helper functions: |
|
* - Sprite blitting |
|
* - Drawing |
|
* - Angle calculation |
|
* - RNG |
|
* - Memory allocation |
|
* - File loading |
|
* - Video playback |
|
*/ |
|
|
|
#include "lighting.h" |
|
#include "movie.h" |
|
#include "options.h" |
|
#include "storm/storm.h" |
|
|
|
namespace devilution { |
|
|
|
/** Seed value before the most recent call to SetRndSeed() */ |
|
Sint32 orgseed; |
|
/** Current game seed */ |
|
Sint32 sglGameSeed; |
|
|
|
/** |
|
* Specifies the increment used in the Borland C/C++ pseudo-random. |
|
*/ |
|
const Uint32 RndInc = 1; |
|
|
|
/** |
|
* Specifies the multiplier used in the Borland C/C++ pseudo-random number generator algorithm. |
|
*/ |
|
const Uint32 RndMult = 0x015A4E35; |
|
|
|
void CelDrawTo(const CelOutputBuffer &out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth) |
|
{ |
|
int nDataSize; |
|
BYTE *pRLEBytes; |
|
|
|
assert(pCelBuff != nullptr); |
|
pRLEBytes = CelGetFrame(pCelBuff, nCel, &nDataSize); |
|
CelBlitSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth); |
|
} |
|
|
|
void CelClippedDrawTo(const CelOutputBuffer &out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth) |
|
{ |
|
BYTE *pRLEBytes; |
|
int nDataSize; |
|
|
|
assert(pCelBuff != nullptr); |
|
|
|
pRLEBytes = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
|
|
CelBlitSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth); |
|
} |
|
|
|
void CelDrawLightTo(const CelOutputBuffer &out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, BYTE *tbl) |
|
{ |
|
int nDataSize; |
|
BYTE *pRLEBytes; |
|
|
|
assert(pCelBuff != nullptr); |
|
|
|
pRLEBytes = CelGetFrame(pCelBuff, nCel, &nDataSize); |
|
|
|
if (light_table_index || tbl) |
|
CelBlitLightSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth, tbl); |
|
else |
|
CelBlitSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth); |
|
} |
|
|
|
void CelClippedDrawLightTo(const CelOutputBuffer &out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth) |
|
{ |
|
int nDataSize; |
|
BYTE *pRLEBytes; |
|
|
|
assert(pCelBuff != nullptr); |
|
|
|
pRLEBytes = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
|
|
if (light_table_index) |
|
CelBlitLightSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth, nullptr); |
|
else |
|
CelBlitSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth); |
|
} |
|
|
|
void CelDrawLightRedTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, char light) |
|
{ |
|
int nDataSize, w, idx; |
|
BYTE *pRLEBytes, *dst, *tbl; |
|
|
|
assert(pCelBuff != nullptr); |
|
|
|
pRLEBytes = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
dst = out.at(sx, sy); |
|
|
|
idx = light4flag ? 1024 : 4096; |
|
if (light == 2) |
|
idx += 256; // gray colors |
|
if (light >= 4) |
|
idx += (light - 1) << 8; |
|
|
|
BYTE width; |
|
BYTE *end; |
|
|
|
tbl = &pLightTbl[idx]; |
|
end = &pRLEBytes[nDataSize]; |
|
|
|
for (; pRLEBytes != end; dst -= out.pitch() + nWidth) { |
|
for (w = nWidth; w;) { |
|
width = *pRLEBytes++; |
|
if (!(width & 0x80)) { |
|
w -= width; |
|
while (width) { |
|
*dst = tbl[*pRLEBytes]; |
|
pRLEBytes++; |
|
dst++; |
|
width--; |
|
} |
|
} else { |
|
width = -(char)width; |
|
dst += width; |
|
w -= width; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void CelBlitSafeTo(CelOutputBuffer out, int sx, int sy, BYTE *pRLEBytes, int nDataSize, int nWidth) |
|
{ |
|
int i, w; |
|
BYTE width; |
|
BYTE *src, *dst; |
|
|
|
assert(pRLEBytes != nullptr); |
|
|
|
src = pRLEBytes; |
|
dst = out.at(sx, sy); |
|
w = nWidth; |
|
|
|
for (; src != &pRLEBytes[nDataSize]; dst -= out.pitch() + w) { |
|
for (i = w; i;) { |
|
width = *src++; |
|
if (!(width & 0x80)) { |
|
i -= width; |
|
if (dst < out.end() && dst >= out.begin()) { |
|
memcpy(dst, src, std::min(static_cast<ptrdiff_t>(width), out.end() - dst)); |
|
} |
|
src += width; |
|
dst += width; |
|
} else { |
|
width = -(char)width; |
|
dst += width; |
|
i -= width; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void CelClippedDrawSafeTo(const CelOutputBuffer &out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth) |
|
{ |
|
BYTE *pRLEBytes; |
|
int nDataSize; |
|
|
|
assert(pCelBuff != nullptr); |
|
|
|
pRLEBytes = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
CelBlitSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth); |
|
} |
|
|
|
void CelBlitLightSafeTo(CelOutputBuffer out, int sx, int sy, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE *tbl) |
|
{ |
|
int i, w; |
|
BYTE width; |
|
BYTE *src, *dst; |
|
|
|
assert(pRLEBytes != nullptr); |
|
|
|
src = pRLEBytes; |
|
dst = out.at(sx, sy); |
|
if (tbl == nullptr) |
|
tbl = &pLightTbl[light_table_index * 256]; |
|
w = nWidth; |
|
|
|
for (; src != &pRLEBytes[nDataSize]; dst -= out.pitch() + w) { |
|
for (i = w; i;) { |
|
width = *src++; |
|
if (!(width & 0x80)) { |
|
i -= width; |
|
if (dst < out.end() && dst > out.begin()) { |
|
if (width & 1) { |
|
dst[0] = tbl[src[0]]; |
|
src++; |
|
dst++; |
|
} |
|
width /= 2; |
|
if (width & 1) { |
|
dst[0] = tbl[src[0]]; |
|
dst[1] = tbl[src[1]]; |
|
src += 2; |
|
dst += 2; |
|
} |
|
width /= 2; |
|
for (; width; width--) { |
|
dst[0] = tbl[src[0]]; |
|
dst[1] = tbl[src[1]]; |
|
dst[2] = tbl[src[2]]; |
|
dst[3] = tbl[src[3]]; |
|
src += 4; |
|
dst += 4; |
|
} |
|
} else { |
|
src += width; |
|
dst += width; |
|
} |
|
} else { |
|
width = -(char)width; |
|
dst += width; |
|
i -= width; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void CelBlitLightTransSafeTo(CelOutputBuffer out, int sx, int sy, BYTE *pRLEBytes, int nDataSize, int nWidth) |
|
{ |
|
int w; |
|
bool shift; |
|
BYTE *tbl; |
|
|
|
assert(pRLEBytes != nullptr); |
|
int i; |
|
BYTE width; |
|
BYTE *src, *dst; |
|
|
|
src = pRLEBytes; |
|
dst = out.at(sx, sy); |
|
tbl = &pLightTbl[light_table_index * 256]; |
|
w = nWidth; |
|
shift = (BYTE)(size_t)dst & 1; |
|
|
|
for (; src != &pRLEBytes[nDataSize]; dst -= out.pitch() + w, shift = (shift + 1) & 1) { |
|
for (i = w; i;) { |
|
width = *src++; |
|
if (!(width & 0x80)) { |
|
i -= width; |
|
if (dst < out.end() && dst > out.begin()) { |
|
if (((BYTE)(size_t)dst & 1) == shift) { |
|
if (!(width & 1)) { |
|
goto L_ODD; |
|
} else { |
|
src++; |
|
dst++; |
|
L_EVEN: |
|
width /= 2; |
|
if (width & 1) { |
|
dst[0] = tbl[src[0]]; |
|
src += 2; |
|
dst += 2; |
|
} |
|
width /= 2; |
|
for (; width; width--) { |
|
dst[0] = tbl[src[0]]; |
|
dst[2] = tbl[src[2]]; |
|
src += 4; |
|
dst += 4; |
|
} |
|
} |
|
} else { |
|
if (!(width & 1)) { |
|
goto L_EVEN; |
|
} else { |
|
dst[0] = tbl[src[0]]; |
|
src++; |
|
dst++; |
|
L_ODD: |
|
width /= 2; |
|
if (width & 1) { |
|
dst[1] = tbl[src[1]]; |
|
src += 2; |
|
dst += 2; |
|
} |
|
width /= 2; |
|
for (; width; width--) { |
|
dst[1] = tbl[src[1]]; |
|
dst[3] = tbl[src[3]]; |
|
src += 4; |
|
dst += 4; |
|
} |
|
} |
|
} |
|
} else { |
|
src += width; |
|
dst += width; |
|
} |
|
} else { |
|
width = -(char)width; |
|
dst += width; |
|
i -= width; |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* @brief Same as CelBlitLightSafe, with blended transparancy applied |
|
* @param out The output buffer |
|
* @param pRLEBytes CEL pixel stream (run-length encoded) |
|
* @param nDataSize Size of CEL in bytes |
|
* @param nWidth Width of sprite |
|
* @param tbl Palette translation table |
|
*/ |
|
static void CelBlitLightBlendedSafeTo(CelOutputBuffer out, int sx, int sy, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE *tbl) |
|
{ |
|
int i, w; |
|
BYTE width; |
|
BYTE *src, *dst; |
|
|
|
assert(pRLEBytes != nullptr); |
|
|
|
src = pRLEBytes; |
|
dst = out.at(sx, sy); |
|
if (tbl == nullptr) |
|
tbl = &pLightTbl[light_table_index * 256]; |
|
w = nWidth; |
|
|
|
for (; src != &pRLEBytes[nDataSize]; dst -= out.pitch() + w) { |
|
for (i = w; i;) { |
|
width = *src++; |
|
if (!(width & 0x80)) { |
|
i -= width; |
|
if (dst < out.end() && dst > out.begin()) { |
|
if (width & 1) { |
|
dst[0] = paletteTransparencyLookup[dst[0]][tbl[src[0]]]; |
|
src++; |
|
dst++; |
|
} |
|
width /= 2; |
|
if (width & 1) { |
|
dst[0] = paletteTransparencyLookup[dst[0]][tbl[src[0]]]; |
|
dst[1] = paletteTransparencyLookup[dst[1]][tbl[src[1]]]; |
|
src += 2; |
|
dst += 2; |
|
} |
|
width /= 2; |
|
for (; width; width--) { |
|
dst[0] = paletteTransparencyLookup[dst[0]][tbl[src[0]]]; |
|
dst[1] = paletteTransparencyLookup[dst[1]][tbl[src[1]]]; |
|
dst[2] = paletteTransparencyLookup[dst[2]][tbl[src[2]]]; |
|
dst[3] = paletteTransparencyLookup[dst[3]][tbl[src[3]]]; |
|
src += 4; |
|
dst += 4; |
|
} |
|
} else { |
|
src += width; |
|
dst += width; |
|
} |
|
} else { |
|
width = -(char)width; |
|
dst += width; |
|
i -= width; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void CelClippedBlitLightTransTo(const CelOutputBuffer &out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth) |
|
{ |
|
int nDataSize; |
|
BYTE *pRLEBytes; |
|
|
|
assert(pCelBuff != nullptr); |
|
|
|
pRLEBytes = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
|
|
if (cel_transparency_active) { |
|
if (sgOptions.Graphics.bBlendedTransparancy) |
|
CelBlitLightBlendedSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth, nullptr); |
|
else |
|
CelBlitLightTransSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth); |
|
} else if (light_table_index) |
|
CelBlitLightSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth, nullptr); |
|
else |
|
CelBlitSafeTo(out, sx, sy, pRLEBytes, nDataSize, nWidth); |
|
} |
|
|
|
void CelDrawLightRedSafeTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, char light) |
|
{ |
|
int nDataSize, w, idx; |
|
BYTE *pRLEBytes, *dst, *tbl; |
|
|
|
assert(pCelBuff != nullptr); |
|
|
|
pRLEBytes = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
dst = out.at(sx, sy); |
|
|
|
idx = light4flag ? 1024 : 4096; |
|
if (light == 2) |
|
idx += 256; // gray colors |
|
if (light >= 4) |
|
idx += (light - 1) << 8; |
|
|
|
tbl = &pLightTbl[idx]; |
|
|
|
BYTE width; |
|
BYTE *end; |
|
|
|
end = &pRLEBytes[nDataSize]; |
|
|
|
for (; pRLEBytes != end; dst -= out.pitch() + nWidth) { |
|
for (w = nWidth; w;) { |
|
width = *pRLEBytes++; |
|
if (!(width & 0x80)) { |
|
w -= width; |
|
if (dst < out.end() && dst > out.begin()) { |
|
while (width) { |
|
*dst = tbl[*pRLEBytes]; |
|
pRLEBytes++; |
|
dst++; |
|
width--; |
|
} |
|
} else { |
|
pRLEBytes += width; |
|
dst += width; |
|
} |
|
} else { |
|
width = -(char)width; |
|
dst += width; |
|
w -= width; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void CelDrawUnsafeTo(CelOutputBuffer out, int x, int y, BYTE *pCelBuff, int nCel, int nWidth) |
|
{ |
|
BYTE *pRLEBytes, *dst, *end; |
|
|
|
assert(pCelBuff != nullptr); |
|
int i, nDataSize; |
|
BYTE width; |
|
|
|
pRLEBytes = CelGetFrame(pCelBuff, nCel, &nDataSize); |
|
end = &pRLEBytes[nDataSize]; |
|
dst = out.at(x, y); |
|
|
|
for (; pRLEBytes != end; dst -= out.pitch() + nWidth) { |
|
for (i = nWidth; i;) { |
|
width = *pRLEBytes++; |
|
if (!(width & 0x80)) { |
|
i -= width; |
|
memcpy(dst, pRLEBytes, width); |
|
dst += width; |
|
pRLEBytes += width; |
|
} else { |
|
width = -(char)width; |
|
dst += width; |
|
i -= width; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void CelBlitOutlineTo(CelOutputBuffer out, BYTE col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, bool skipColorIndexZero) |
|
{ |
|
int nDataSize, w; |
|
BYTE *src, *dst, *end; |
|
BYTE width; |
|
|
|
assert(pCelBuff != nullptr); |
|
src = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
end = &src[nDataSize]; |
|
dst = out.at(sx, sy); |
|
|
|
for (; src != end; dst -= out.pitch() + nWidth) { |
|
for (w = nWidth; w;) { |
|
width = *src++; |
|
if (!(width & 0x80)) { |
|
w -= width; |
|
if (dst < out.end() && dst > out.begin()) { |
|
if (dst >= out.end() - out.pitch()) { |
|
while (width) { |
|
if (!skipColorIndexZero || *src > 0) { |
|
dst[-out.pitch()] = col; |
|
dst[-1] = col; |
|
dst[1] = col; |
|
} |
|
src++; |
|
dst++; |
|
width--; |
|
} |
|
} else { |
|
while (width) { |
|
if (!skipColorIndexZero || *src > 0) { |
|
dst[-out.pitch()] = col; |
|
dst[-1] = col; |
|
dst[1] = col; |
|
dst[out.pitch()] = col; |
|
} |
|
src++; |
|
dst++; |
|
width--; |
|
} |
|
} |
|
} else { |
|
src += width; |
|
dst += width; |
|
} |
|
} else { |
|
width = -(char)width; |
|
dst += width; |
|
w -= width; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void SetPixel(const CelOutputBuffer &out, int sx, int sy, BYTE col) |
|
{ |
|
if (!out.in_bounds(sx, sy)) |
|
return; |
|
*out.at(sx, sy) = col; |
|
} |
|
|
|
void DrawLineTo(const CelOutputBuffer &out, int x0, int y0, int x1, int y1, BYTE color_index) |
|
{ |
|
int i, dx, dy, steps; |
|
float ix, iy, sx, sy; |
|
|
|
dx = x1 - x0; |
|
dy = y1 - y0; |
|
steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy); |
|
ix = dx / (float)steps; |
|
iy = dy / (float)steps; |
|
sx = x0; |
|
sy = y0; |
|
|
|
for (i = 0; i <= steps; i++, sx += ix, sy += iy) { |
|
SetPixel(out, sx, sy, color_index); |
|
} |
|
} |
|
|
|
static void DrawHalfTransparentBlendedRectTo(CelOutputBuffer out, int sx, int sy, int width, int height) |
|
{ |
|
BYTE *pix = out.at(sx, sy); |
|
for (int row = 0; row < height; row++) { |
|
for (int col = 0; col < width; col++) { |
|
*pix = paletteTransparencyLookup[0][*pix]; |
|
pix++; |
|
} |
|
pix += out.pitch() - width; |
|
} |
|
} |
|
|
|
static void DrawHalfTransparentStippledRectTo(CelOutputBuffer out, int sx, int sy, int width, int height) |
|
{ |
|
BYTE *pix = out.at(sx, sy); |
|
for (int row = 0; row < height; row++) { |
|
for (int col = 0; col < width; col++) { |
|
if ((row & 1 && col & 1) || (!(row & 1) && !(col & 1))) |
|
*pix = 0; |
|
pix++; |
|
} |
|
pix += out.pitch() - width; |
|
} |
|
} |
|
|
|
void DrawHalfTransparentRectTo(const CelOutputBuffer &out, int sx, int sy, int width, int height) |
|
{ |
|
if (sgOptions.Graphics.bBlendedTransparancy) { |
|
DrawHalfTransparentBlendedRectTo(out, sx, sy, width, height); |
|
} else { |
|
DrawHalfTransparentStippledRectTo(out, sx, sy, width, height); |
|
} |
|
} |
|
|
|
direction GetDirection(int x1, int y1, int x2, int y2) |
|
{ |
|
int mx, my, ny; |
|
direction md; |
|
|
|
mx = x2 - x1; |
|
my = y2 - y1; |
|
|
|
if (mx >= 0) { |
|
if (my >= 0) { |
|
md = DIR_S; |
|
if (2 * mx < my) |
|
md = DIR_SW; |
|
} else { |
|
my = -my; |
|
md = DIR_E; |
|
if (2 * mx < my) |
|
md = DIR_NE; |
|
} |
|
if (2 * my < mx) |
|
return DIR_SE; |
|
} else { |
|
if (my >= 0) { |
|
ny = -mx; |
|
md = DIR_W; |
|
if (2 * ny < my) |
|
md = DIR_SW; |
|
} else { |
|
ny = -mx; |
|
my = -my; |
|
md = DIR_N; |
|
if (2 * ny < my) |
|
md = DIR_NE; |
|
} |
|
if (2 * my < ny) |
|
return DIR_NW; |
|
} |
|
|
|
return md; |
|
} |
|
|
|
/** |
|
* @brief Set the RNG seed |
|
* @param s RNG seed |
|
*/ |
|
void SetRndSeed(Sint32 s) |
|
{ |
|
sglGameSeed = s; |
|
orgseed = s; |
|
} |
|
|
|
/** |
|
* @brief Advance the internal RNG seed and return the new value |
|
* @return RNG seed |
|
*/ |
|
Sint32 AdvanceRndSeed() |
|
{ |
|
sglGameSeed = (RndMult * static_cast<Uint32>(sglGameSeed)) + RndInc; |
|
return abs(sglGameSeed); |
|
} |
|
|
|
/** |
|
* @brief Get the current RNG seed |
|
* @return RNG seed |
|
*/ |
|
Sint32 GetRndSeed() |
|
{ |
|
return abs(sglGameSeed); |
|
} |
|
|
|
/** |
|
* @brief Main RNG function |
|
* @param idx Unused |
|
* @param v The upper limit for the return value |
|
* @return A random number from 0 to (v-1) |
|
*/ |
|
Sint32 random_(BYTE idx, Sint32 v) |
|
{ |
|
if (v <= 0) |
|
return 0; |
|
if (v < 0xFFFF) |
|
return (AdvanceRndSeed() >> 16) % v; |
|
return AdvanceRndSeed() % v; |
|
} |
|
|
|
/** |
|
* @brief Load a file in to a buffer |
|
* @param pszName Path of file |
|
* @param pdwFileLen Will be set to file size if non-NULL |
|
* @return Buffer with content of file |
|
*/ |
|
BYTE *LoadFileInMem(const char *pszName, DWORD *pdwFileLen) |
|
{ |
|
HANDLE file; |
|
BYTE *buf; |
|
int fileLen; |
|
|
|
SFileOpenFile(pszName, &file); |
|
fileLen = SFileGetFileSize(file, nullptr); |
|
|
|
if (pdwFileLen) |
|
*pdwFileLen = fileLen; |
|
|
|
if (!fileLen) |
|
app_fatal("Zero length SFILE:\n%s", pszName); |
|
|
|
buf = (BYTE *)DiabloAllocPtr(fileLen); |
|
|
|
SFileReadFile(file, buf, fileLen, nullptr, nullptr); |
|
SFileCloseFile(file); |
|
|
|
return buf; |
|
} |
|
|
|
/** |
|
* @brief Load a file in to the given buffer |
|
* @param pszName Path of file |
|
* @param p Target buffer |
|
* @return Size of file |
|
*/ |
|
DWORD LoadFileWithMem(const char *pszName, BYTE *p) |
|
{ |
|
DWORD dwFileLen; |
|
HANDLE hsFile; |
|
|
|
assert(pszName); |
|
if (p == nullptr) { |
|
app_fatal("LoadFileWithMem(NULL):\n%s", pszName); |
|
} |
|
|
|
SFileOpenFile(pszName, &hsFile); |
|
|
|
dwFileLen = SFileGetFileSize(hsFile, nullptr); |
|
if (dwFileLen == 0) { |
|
app_fatal("Zero length SFILE:\n%s", pszName); |
|
} |
|
|
|
SFileReadFile(hsFile, p, dwFileLen, nullptr, nullptr); |
|
SFileCloseFile(hsFile); |
|
|
|
return dwFileLen; |
|
} |
|
|
|
/** |
|
* @brief Apply the color swaps to a CL2 sprite |
|
* @param p CL2 buffer |
|
* @param ttbl Palette translation table |
|
* @param nCel Frame number in CL2 file |
|
*/ |
|
void Cl2ApplyTrans(BYTE *p, BYTE *ttbl, int nCel) |
|
{ |
|
int i, nDataSize; |
|
char width; |
|
BYTE *dst; |
|
|
|
assert(p != nullptr); |
|
assert(ttbl != nullptr); |
|
|
|
for (i = 1; i <= nCel; i++) { |
|
dst = CelGetFrame(p, i, &nDataSize) + 10; |
|
nDataSize -= 10; |
|
while (nDataSize) { |
|
width = *dst++; |
|
nDataSize--; |
|
assert(nDataSize >= 0); |
|
if (width < 0) { |
|
width = -width; |
|
if (width > 65) { |
|
nDataSize--; |
|
assert(nDataSize >= 0); |
|
*dst = ttbl[*dst]; |
|
dst++; |
|
} else { |
|
nDataSize -= width; |
|
assert(nDataSize >= 0); |
|
while (width--) { |
|
*dst = ttbl[*dst]; |
|
dst++; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* @brief Blit CL2 sprite to the given buffer |
|
* @param out Target buffer |
|
* @param sx Target buffer coordinate |
|
* @param sy Target buffer coordinate |
|
* @param pRLEBytes CL2 pixel stream (run-length encoded) |
|
* @param nDataSize Size of CL2 in bytes |
|
* @param nWidth Width of sprite |
|
*/ |
|
static void Cl2BlitSafe(CelOutputBuffer out, int sx, int sy, BYTE *pRLEBytes, int nDataSize, int nWidth) |
|
{ |
|
int w; |
|
char width; |
|
BYTE fill; |
|
BYTE *src, *dst; |
|
|
|
src = pRLEBytes; |
|
dst = out.at(sx, sy); |
|
w = nWidth; |
|
|
|
while (nDataSize) { |
|
width = *src++; |
|
nDataSize--; |
|
if (width < 0) { |
|
width = -width; |
|
if (width > 65) { |
|
width -= 65; |
|
nDataSize--; |
|
fill = *src++; |
|
if (dst < out.end() && dst > out.begin()) { |
|
w -= width; |
|
while (width) { |
|
*dst = fill; |
|
dst++; |
|
width--; |
|
} |
|
if (!w) { |
|
w = nWidth; |
|
dst -= out.pitch() + w; |
|
} |
|
continue; |
|
} |
|
} else { |
|
nDataSize -= width; |
|
if (dst < out.end() && dst > out.begin()) { |
|
w -= width; |
|
while (width) { |
|
*dst = *src; |
|
src++; |
|
dst++; |
|
width--; |
|
} |
|
if (!w) { |
|
w = nWidth; |
|
dst -= out.pitch() + w; |
|
} |
|
continue; |
|
} else { |
|
src += width; |
|
} |
|
} |
|
} |
|
while (width) { |
|
if (width > w) { |
|
dst += w; |
|
width -= w; |
|
w = 0; |
|
} else { |
|
dst += width; |
|
w -= width; |
|
width = 0; |
|
} |
|
if (!w) { |
|
w = nWidth; |
|
dst -= out.pitch() + w; |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* @brief Blit a solid colder shape one pixel larger then the given sprite shape, to the given buffer |
|
* @param out Target buffer |
|
* @param sx Target buffer coordinate |
|
* @param sy Target buffer coordinate |
|
* @param pRLEBytes CL2 pixel stream (run-length encoded) |
|
* @param nDataSize Size of CL2 in bytes |
|
* @param nWidth Width of sprite |
|
* @param col Color index from current palette |
|
*/ |
|
static void Cl2BlitOutlineSafe(CelOutputBuffer out, int sx, int sy, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE col) |
|
{ |
|
int w; |
|
int8_t width; |
|
BYTE *src, *dst; |
|
|
|
src = pRLEBytes; |
|
dst = out.at(sx, sy); |
|
w = nWidth; |
|
|
|
while (nDataSize) { |
|
width = *src++; |
|
nDataSize--; |
|
if (width < 0) { |
|
width = -width; |
|
if (width > 65) { |
|
width -= 65; |
|
nDataSize--; |
|
if (*src++ && dst < out.end() && dst > out.begin()) { |
|
w -= width; |
|
dst[-1] = col; |
|
dst[width] = col; |
|
while (width) { |
|
dst[-out.pitch()] = col; |
|
dst[out.pitch()] = col; |
|
dst++; |
|
width--; |
|
} |
|
if (!w) { |
|
w = nWidth; |
|
dst -= out.pitch() + w; |
|
} |
|
continue; |
|
} |
|
} else { |
|
nDataSize -= width; |
|
if (dst < out.end() && dst > out.begin()) { |
|
w -= width; |
|
while (width) { |
|
if (*src++) { |
|
dst[-1] = col; |
|
dst[1] = col; |
|
dst[-out.pitch()] = col; |
|
// BUGFIX: only set `if (dst+out.pitch() < out.end())` |
|
dst[out.pitch()] = col; |
|
} |
|
dst++; |
|
width--; |
|
} |
|
if (!w) { |
|
w = nWidth; |
|
dst -= out.pitch() + w; |
|
} |
|
continue; |
|
} else { |
|
src += width; |
|
} |
|
} |
|
} |
|
while (width) { |
|
if (width > w) { |
|
dst += w; |
|
width -= w; |
|
w = 0; |
|
} else { |
|
dst += width; |
|
w -= width; |
|
width = 0; |
|
} |
|
if (!w) { |
|
w = nWidth; |
|
dst -= out.pitch() + w; |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* @brief Blit CL2 sprite, and apply lighting, to the given buffer |
|
* @param out Target buffer |
|
* @param sx Target buffer coordinate |
|
* @param sy Target buffer coordinate |
|
* @param pRLEBytes CL2 pixel stream (run-length encoded) |
|
* @param nDataSize Size of CL2 in bytes |
|
* @param nWidth With of CL2 sprite |
|
* @param pTable Light color table |
|
*/ |
|
static void Cl2BlitLightSafe(CelOutputBuffer out, int sx, int sy, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE *pTable) |
|
{ |
|
int w, spriteWidth; |
|
char width; |
|
BYTE fill; |
|
BYTE *src, *dst; |
|
|
|
src = pRLEBytes; |
|
dst = out.at(sx, sy); |
|
w = nWidth; |
|
spriteWidth = nWidth; |
|
|
|
while (nDataSize) { |
|
width = *src++; |
|
nDataSize--; |
|
if (width < 0) { |
|
width = -width; |
|
if (width > 65) { |
|
width -= 65; |
|
nDataSize--; |
|
fill = pTable[*src++]; |
|
if (dst < out.end() && dst > out.begin()) { |
|
w -= width; |
|
while (width) { |
|
*dst = fill; |
|
dst++; |
|
width--; |
|
} |
|
if (w == 0) { |
|
w = spriteWidth; |
|
dst -= out.pitch() + w; |
|
} |
|
continue; |
|
} |
|
} else { |
|
nDataSize -= width; |
|
if (dst < out.end() && dst > out.begin()) { |
|
w -= width; |
|
while (width) { |
|
*dst = pTable[*src]; |
|
src++; |
|
dst++; |
|
width--; |
|
} |
|
if (w == 0) { |
|
w = spriteWidth; |
|
dst -= out.pitch() + w; |
|
} |
|
continue; |
|
} else { |
|
src += width; |
|
} |
|
} |
|
} |
|
while (width) { |
|
if (width > w) { |
|
dst += w; |
|
width -= w; |
|
w = 0; |
|
} else { |
|
dst += width; |
|
w -= width; |
|
width = 0; |
|
} |
|
if (w == 0) { |
|
w = spriteWidth; |
|
dst -= out.pitch() + w; |
|
} |
|
} |
|
} |
|
} |
|
|
|
void Cl2Draw(const CelOutputBuffer &out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth) |
|
{ |
|
BYTE *pRLEBytes; |
|
int nDataSize; |
|
|
|
assert(pCelBuff != nullptr); |
|
assert(nCel > 0); |
|
|
|
pRLEBytes = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
|
|
Cl2BlitSafe(out, sx, sy, pRLEBytes, nDataSize, nWidth); |
|
} |
|
|
|
void Cl2DrawOutline(CelOutputBuffer out, BYTE col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth) |
|
{ |
|
int nDataSize; |
|
BYTE *pRLEBytes; |
|
|
|
assert(pCelBuff != nullptr); |
|
assert(nCel > 0); |
|
|
|
pRLEBytes = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
|
|
out = out.subregionY(0, out.h() - 1); |
|
Cl2BlitOutlineSafe(out, sx, sy, pRLEBytes, nDataSize, nWidth, col); |
|
} |
|
|
|
void Cl2DrawLightTbl(const CelOutputBuffer &out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, char light) |
|
{ |
|
int nDataSize, idx; |
|
BYTE *pRLEBytes; |
|
|
|
assert(pCelBuff != nullptr); |
|
assert(nCel > 0); |
|
|
|
pRLEBytes = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
|
|
idx = light4flag ? 1024 : 4096; |
|
if (light == 2) |
|
idx += 256; // gray colors |
|
if (light >= 4) |
|
idx += (light - 1) << 8; |
|
|
|
Cl2BlitLightSafe(out, sx, sy, pRLEBytes, nDataSize, nWidth, &pLightTbl[idx]); |
|
} |
|
|
|
void Cl2DrawLight(const CelOutputBuffer &out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth) |
|
{ |
|
int nDataSize; |
|
BYTE *pRLEBytes; |
|
|
|
assert(pCelBuff != nullptr); |
|
assert(nCel > 0); |
|
|
|
pRLEBytes = CelGetFrameClipped(pCelBuff, nCel, &nDataSize); |
|
|
|
if (light_table_index) |
|
Cl2BlitLightSafe(out, sx, sy, pRLEBytes, nDataSize, nWidth, &pLightTbl[light_table_index * 256]); |
|
else |
|
Cl2BlitSafe(out, sx, sy, pRLEBytes, nDataSize, nWidth); |
|
} |
|
|
|
/** |
|
* @brief Fade to black and play a video |
|
* @param pszMovie file path of movie |
|
*/ |
|
void PlayInGameMovie(const char *pszMovie) |
|
{ |
|
PaletteFadeOut(8); |
|
play_movie(pszMovie, false); |
|
ClearScreenBuffer(); |
|
force_redraw = 255; |
|
scrollrt_draw_game_screen(true); |
|
PaletteFadeIn(8); |
|
force_redraw = 255; |
|
} |
|
|
|
} // namespace devilution
|
|
|