1 /**
2 * @file engine.h
3 *
4 * of basic engine helper functions:
5 * - Sprite blitting
6 * - Drawing
7 * - Angle calculation
8 * - RNG
9 * - Memory allocation
10 * - File loading
11 * - Video playback
12 */
13 #ifndef __ENGINE_H__
14 #define __ENGINE_H__
15
16 #include <algorithm>
17 #include <cstddef>
18
19 #include <SDL.h>
20
21 #ifdef USE_SDL1
22 #include "sdl2_to_1_2_backports.h"
23 #endif
24
25 #include "../types.h"
26
27 DEVILUTION_BEGIN_NAMESPACE
28
CelGetFrameStart(BYTE * pCelBuff,int nCel)29 inline BYTE *CelGetFrameStart(BYTE *pCelBuff, int nCel)
30 {
31 DWORD *pFrameTable;
32
33 pFrameTable = (DWORD *)pCelBuff;
34
35 return pCelBuff + SwapLE32(pFrameTable[nCel]);
36 }
37
38 #define LOAD_LE32(b) (((DWORD)(b)[3] << 24) | ((DWORD)(b)[2] << 16) | ((DWORD)(b)[1] << 8) | (DWORD)(b)[0])
39 #define LOAD_BE32(b) (((DWORD)(b)[0] << 24) | ((DWORD)(b)[1] << 16) | ((DWORD)(b)[2] << 8) | (DWORD)(b)[3])
CelGetFrame(BYTE * pCelBuff,int nCel,int * nDataSize)40 inline BYTE *CelGetFrame(BYTE *pCelBuff, int nCel, int *nDataSize)
41 {
42 DWORD nCellStart;
43
44 nCellStart = LOAD_LE32(&pCelBuff[nCel * 4]);
45 *nDataSize = LOAD_LE32(&pCelBuff[(nCel + 1) * 4]) - nCellStart;
46 return pCelBuff + nCellStart;
47 }
48
CelGetFrameClipped(BYTE * pCelBuff,int nCel,int * nDataSize)49 inline BYTE *CelGetFrameClipped(BYTE *pCelBuff, int nCel, int *nDataSize)
50 {
51 DWORD nDataStart;
52 BYTE *pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize);
53
54 nDataStart = pRLEBytes[1] << 8 | pRLEBytes[0];
55 *nDataSize -= nDataStart;
56
57 return pRLEBytes + nDataStart;
58 }
59
60 struct CelOutputBuffer {
61 // 8-bit palletized surface.
62 SDL_Surface *surface;
63 SDL_Rect region;
64
CelOutputBufferCelOutputBuffer65 CelOutputBuffer()
66 : surface(NULL)
67 , region(SDL_Rect { 0, 0, 0, 0 })
68 {
69 }
70
CelOutputBufferCelOutputBuffer71 explicit CelOutputBuffer(SDL_Surface *surface)
72 : surface(surface)
73 , region(SDL_Rect { 0, 0, (Uint16)surface->w, (Uint16)surface->h })
74 {
75 }
76
CelOutputBufferCelOutputBuffer77 CelOutputBuffer(SDL_Surface *surface, SDL_Rect region)
78 : surface(surface)
79 , region(region)
80 {
81 }
82
CelOutputBufferCelOutputBuffer83 CelOutputBuffer(const CelOutputBuffer &other)
84 : surface(other.surface)
85 , region(other.region)
86 {
87 }
88
89 void operator=(const CelOutputBuffer &other)
90 {
91 surface = other.surface;
92 region = other.region;
93 }
94
95 /**
96 * @brief Allocate a buffer that owns its underlying data.
97 */
AllocCelOutputBuffer98 static CelOutputBuffer Alloc(std::size_t width, std::size_t height)
99 {
100 return CelOutputBuffer(SDL_CreateRGBSurfaceWithFormat(0, width, height, 8, SDL_PIXELFORMAT_INDEX8));
101 }
102
103 /**
104 * @brief Free the underlying data.
105 *
106 * Only use this if the buffer owns its data.
107 */
FreeCelOutputBuffer108 void Free()
109 {
110 SDL_FreeSurface(this->surface);
111 this->surface = NULL;
112 }
113
wCelOutputBuffer114 int w() const
115 {
116 return region.w;
117 }
hCelOutputBuffer118 int h() const
119 {
120 return region.h;
121 }
122
atCelOutputBuffer123 BYTE *at(int x, int y) const
124 {
125 return static_cast<BYTE *>(surface->pixels) + region.x + x + surface->pitch * (region.y + y);
126 }
127
beginCelOutputBuffer128 BYTE *begin() const
129 {
130 return at(0, 0);
131 }
endCelOutputBuffer132 BYTE *end() const
133 {
134 return at(0, region.h);
135 }
136
137 /**
138 * @brief Line width of the raw underlying byte buffer.
139 * May be wider than its logical width (for power-of-2 alignment).
140 */
pitchCelOutputBuffer141 int pitch()
142 {
143 return surface->pitch;
144 }
145
in_boundsCelOutputBuffer146 bool in_bounds(Sint16 x, Sint16 y) const
147 {
148 return x >= 0 && y >= 0 && x < region.w && y < region.h;
149 }
150
151 /**
152 * @brief Returns a subregion of the given buffer.
153 */
subregionCelOutputBuffer154 CelOutputBuffer subregion(Sint16 x, Sint16 y, Uint16 w, Uint16 h) const
155 {
156 // In SDL1 SDL_Rect x and y are Sint16. Cast explicitly to avoid a compiler warning.
157 using CoordType = decltype(SDL_Rect {}.x);
158 return CelOutputBuffer(
159 surface,
160 SDL_Rect {
161 static_cast<CoordType>(region.x + x),
162 static_cast<CoordType>(region.y + y),
163 w, h });
164 }
165
166 /**
167 * @brief Returns a buffer that starts at `y` of height `h`.
168 */
subregionYCelOutputBuffer169 CelOutputBuffer subregionY(Sint16 y, Sint16 h) const
170 {
171 SDL_Rect subregion = region;
172 subregion.y += y;
173 subregion.h = h;
174 return CelOutputBuffer(surface, subregion);
175 }
176 };
177
178 /**
179 * @brief Blit CEL sprite to the back buffer at the given coordinates
180 * @param out Target buffer
181 * @param sx Target buffer coordinate
182 * @param sy Target buffer coordinate
183 * @param pCelBuff Cel data
184 * @param nCel CEL frame number
185 * @param nWidth Width of sprite
186 */
187 void CelDrawTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth);
188
189 /**
190 * @briefBlit CEL sprite to the given buffer, does not perform bounds-checking.
191 * @param out Target buffer
192 * @param x Cordinate in the target buffer
193 * @param y Cordinate in the target buffer
194 * @param pCelBuff Cel data
195 * @param nCel CEL frame number
196 * @param nWidth Width of cel
197 */
198 void CelDrawUnsafeTo(CelOutputBuffer out, int x, int y, BYTE *pCelBuff, int nCel, int nWidth);
199
200 /**
201 * @brief Same as CelDrawTo but with the option to skip parts of the top and bottom of the sprite
202 * @param out Target buffer
203 * @param sx Target buffer coordinate
204 * @param sy Target buffer coordinate
205 * @param pCelBuff Cel data
206 * @param nCel CEL frame number
207 * @param nWidth Width of sprite
208 */
209 void CelClippedDrawTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth);
210
211 /**
212 * @brief Blit CEL sprite, and apply lighting, to the back buffer at the given coordinates
213 * @param out Target buffer
214 * @param sx Target buffer coordinate
215 * @param sy Target buffer coordinate
216 * @param pCelBuff Cel data
217 * @param nCel CEL frame number
218 * @param nWidth Width of sprite
219 */
220 void CelDrawLightTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, BYTE *tbl);
221
222 /**
223 * @brief Same as CelDrawLightTo but with the option to skip parts of the top and bottom of the sprite
224 * @param out Target buffer
225 * @param sx Target buffer coordinate
226 * @param sy Target buffer coordinate
227 * @param pCelBuff Cel data
228 * @param nCel CEL frame number
229 * @param nWidth Width of sprite
230 */
231 void CelClippedDrawLightTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth);
232
233 /**
234 * @brief Same as CelBlitLightTransSafeTo
235 * @param out Target buffer
236 * @param sx Target buffer coordinate
237 * @param sy Target buffer coordinate
238 * @param pCelBuff Cel data
239 * @param nCel CEL frame number
240 * @param nWidth Width of sprite
241 */
242 void CelClippedBlitLightTransTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth);
243
244 /**
245 * @brief Blit CEL sprite, and apply lighting, to the back buffer at the given coordinates, translated to a red hue
246 * @param out Target buffer
247 * @param sx Target buffer coordinate
248 * @param sy Target buffer coordinate
249 * @param pCelBuff Cel data
250 * @param nCel CEL frame number
251 * @param nWidth Width of sprite
252 * @param light Light shade to use
253 */
254 void CelDrawLightRedTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, char light);
255
256 /**
257 * @brief Blit CEL sprite to the given buffer, checks for drawing outside the buffer.
258 * @param out Target buffer
259 * @param sx Target buffer coordinate
260 * @param sy Target buffer coordinate
261 * @param pRLEBytes CEL pixel stream (run-length encoded)
262 * @param nDataSize Size of CEL in bytes
263 * @param nWidth Width of sprite
264 */
265 void CelBlitSafeTo(CelOutputBuffer out, int sx, int sy, BYTE *pRLEBytes, int nDataSize, int nWidth);
266
267 /**
268 * @brief Same as CelClippedDrawTo but checks for drawing outside the buffer
269 * @param out Target buffer
270 * @param sx Target buffer coordinate
271 * @param sy Target buffer coordinate
272 * @param pCelBuff Cel data
273 * @param nCel CEL frame number
274 * @param nWidth Width of sprite
275 */
276 void CelClippedDrawSafeTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth);
277
278 /**
279 * @brief Blit CEL sprite, and apply lighting, to the given buffer, checks for drawing outside the buffer
280 * @param out Target buffer
281 * @param sx Target buffer coordinate
282 * @param sy Target buffer coordinate
283 * @param pRLEBytes CEL pixel stream (run-length encoded)
284 * @param nDataSize Size of CEL in bytes
285 * @param nWidth Width of sprite
286 * @param tbl Palette translation table
287 */
288 void CelBlitLightSafeTo(CelOutputBuffer out, int sx, int sy, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE *tbl);
289
290 /**
291 * @brief Same as CelBlitLightSafeTo but with stippled transparancy applied
292 * @param out Target buffer
293 * @param sx Target buffer coordinate
294 * @param sy Target buffer coordinate
295 * @param pRLEBytes CEL pixel stream (run-length encoded)
296 * @param nDataSize Size of CEL in bytes
297 * @param nWidth Width of sprite
298 */
299 void CelBlitLightTransSafeTo(CelOutputBuffer out, int sx, int sy, BYTE *pRLEBytes, int nDataSize, int nWidth);
300
301 /**
302 * @brief Same as CelDrawLightRedTo but checks for drawing outside the buffer
303 * @param out Target buffer
304 * @param sx Target buffer coordinate
305 * @param sy Target buffer coordinate
306 * @param pCelBuff Cel data
307 * @param nCel CEL frame number
308 * @param nWidth Width of cel
309 * @param light Light shade to use
310 */
311 void CelDrawLightRedSafeTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, char light);
312
313 /**
314 * @brief Blit a solid colder shape one pixel larger then the given sprite shape, to the target buffer at the given coordianates
315 * @param out Target buffer
316 * @param col Color index from current palette
317 * @param sx Target buffer coordinate
318 * @param sy Target buffer coordinate
319 * @param pCelBuff CEL buffer
320 * @param nCel CEL frame number
321 * @param nWidth Width of sprite
322 * @param skipColorIndexZero If true, color in index 0 will be treated as transparent (these are typically used for shadows in sprites)
323 */
324 void CelBlitOutlineTo(CelOutputBuffer out, BYTE col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, bool skipColorIndexZero = true);
325
326 /**
327 * @brief Set the value of a single pixel in the back buffer, checks bounds
328 * @param out Target buffer
329 * @param sx Target buffer coordinate
330 * @param sy Target buffer coordinate
331 * @param col Color index from current palette
332 */
333 void SetPixel(CelOutputBuffer out, int sx, int sy, BYTE col);
334
335 /**
336 * @brief Blit CL2 sprite, to the back buffer at the given coordianates
337 * @param out Output buffer
338 * @param sx Output buffer coordinate
339 * @param sy Output buffer coordinate
340 * @param pCelBuff CL2 buffer
341 * @param nCel CL2 frame number
342 * @param nWidth Width of sprite
343 */
344 void Cl2Draw(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth);
345
346 /**
347 * @brief Blit a solid colder shape one pixel larger then the given sprite shape, to the given buffer at the given coordianates
348 * @param col Color index from current palette
349 * @param out Output buffer
350 * @param sx Output buffer coordinate
351 * @param sy Output buffer coordinate
352 * @param pCelBuff CL2 buffer
353 * @param nCel CL2 frame number
354 * @param nWidth Width of sprite
355 */
356 void Cl2DrawOutline(CelOutputBuffer out, BYTE col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth);
357
358 /**
359 * @brief Blit CL2 sprite, and apply a given lighting, to the given buffer at the given coordianates
360 * @param out Output buffer
361 * @param sx Output buffer coordinate
362 * @param sy Output buffer coordinate
363 * @param pCelBuff CL2 buffer
364 * @param nCel CL2 frame number
365 * @param nWidth Width of sprite
366 * @param light Light shade to use
367 */
368 void Cl2DrawLightTbl(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, char light);
369
370 /**
371 * @brief Blit CL2 sprite, and apply lighting, to the given buffer at the given coordinates
372 * @param out Output buffer
373 * @param sx Output buffer coordinate
374 * @param sy Output buffer coordinate
375 * @param pCelBuff CL2 buffer
376 * @param nCel CL2 frame number
377 * @param nWidth Width of sprite
378 */
379 void Cl2DrawLight(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth);
380
381 /**
382 * @brief Draw a line in the target buffer
383 * @param out Target buffer
384 * @param x0 Back buffer coordinate
385 * @param y0 Back buffer coordinate
386 * @param x1 Back buffer coordinate
387 * @param y1 Back buffer coordinate
388 * @param color_index Color index from current palette
389 */
390 void DrawLineTo(CelOutputBuffer out, int x0, int y0, int x1, int y1, BYTE color_index);
391
392 /**
393 * Draws a half-transparent rectangle by blacking out odd pixels on odd lines,
394 * even pixels on even lines.
395 *
396 * @brief Render a transparent black rectangle
397 * @param out Target buffer
398 * @param sx Screen coordinate
399 * @param sy Screen coordinate
400 * @param width Rectangle width
401 * @param height Rectangle height
402 */
403 void DrawHalfTransparentRectTo(CelOutputBuffer out, int sx, int sy, int width, int height);
404
405 /**
406 * @brief Calculate the best fit direction between two points
407 * @param x1 Tile coordinate
408 * @param y1 Tile coordinate
409 * @param x2 Tile coordinate
410 * @param y2 Tile coordinate
411 * @return A value from the direction enum
412 */
413 direction GetDirection(int x1, int y1, int x2, int y2);
414
415 void SetRndSeed(Sint32 s);
416 Sint32 AdvanceRndSeed();
417 Sint32 GetRndSeed();
418 Sint32 random_(BYTE idx, Sint32 v);
419 BYTE *DiabloAllocPtr(DWORD dwBytes);
420 void mem_free_dbg(void *p);
421 BYTE *LoadFileInMem(const char *pszName, DWORD *pdwFileLen);
422 DWORD LoadFileWithMem(const char *pszName, BYTE *p);
423 void Cl2ApplyTrans(BYTE *p, BYTE *ttbl, int nCel);
424 void PlayInGameMovie(const char *pszMovie);
425
426 DEVILUTION_END_NAMESPACE
427
428 #endif /* __ENGINE_H__ */
429