| 1 |
/* |
| 2 |
Copyright (c) 2009-2013 Peter "Corsix" Cawley and Edvin "Lego3" Linge |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef CORSIX_TH_TH_GFX_SDL_H_ |
| 24 |
#define CORSIX_TH_TH_GFX_SDL_H_ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#include <SDL.h> |
| 29 |
|
| 30 |
#include <array> |
| 31 |
#include <memory> |
| 32 |
#include <stack> |
| 33 |
#include <stdexcept> |
| 34 |
#include <vector> |
| 35 |
|
| 36 |
#include "th_gfx_common.h" |
| 37 |
|
| 38 |
class cursor; |
| 39 |
class line_sequence; |
| 40 |
class lua_persist_reader; |
| 41 |
class lua_persist_writer; |
| 42 |
|
| 43 |
struct clip_rect : public SDL_Rect { |
| 44 |
typedef Sint16 x_y_type; |
| 45 |
typedef Uint16 w_h_type; |
| 46 |
}; |
| 47 |
|
| 48 |
/** Helper structure with parameters to create a #render_target. */ |
| 49 |
struct render_target_creation_params { |
| 50 |
int width; ///< Expected width of the render target. |
| 51 |
int height; ///< Expected height of the render target. |
| 52 |
int bpp; ///< Expected colour depth of the render target. |
| 53 |
bool fullscreen; ///< Run full-screen. |
| 54 |
bool present_immediate; ///< Whether to present immediately to the user |
| 55 |
///< (else wait for Vsync). |
| 56 |
bool direct_zoom; ///< Scale each texture when copying if true, otherwise |
| 57 |
///< render to intermediate texture and scale. |
| 58 |
int min_width; ///< Minimum width of the render target. |
| 59 |
int min_height; ///< Minimum height of the render target. |
| 60 |
}; |
| 61 |
|
| 62 |
enum class scaled_items; |
| 63 |
|
| 64 |
//! 32bpp ARGB colour. See #palette::pack_argb |
| 65 |
typedef uint32_t argb_colour; |
| 66 |
|
| 67 |
//! 8bpp palette class. |
| 68 |
class palette { |
| 69 |
public: // External API |
| 70 |
//! Number of colours in the palette. |
| 71 |
static constexpr int color_count{256}; |
| 72 |
|
| 73 |
//! Create an empty palette. All entries are black and fully opaque. |
| 74 |
palette() = default; |
| 75 |
|
| 76 |
//! Load a 256 index color palette from the supplied data. |
| 77 |
/*! |
| 78 |
@param pData Palette data in RGB or RGBA format. |
| 79 |
@param iDataLength Size of the data. |
| 80 |
@param is8bit Whether the data is in 8bpp (otherwise 6bpp). |
| 81 |
@return Whether loading of the palette succeeded. |
| 82 |
*/ |
| 83 |
palette(const uint8_t* pData, size_t iDataLength, bool is8bit); |
| 84 |
|
| 85 |
//! Set an entry of the palette. |
| 86 |
/*! |
| 87 |
The RGB colour (255, 0, 255) is used as the transparent colour. |
| 88 |
@param iEntry Entry number to change. |
| 89 |
@param iR Amount of red in the new entry. |
| 90 |
@param iG Amount of green in the new entry. |
| 91 |
@param iB Amount of blue in the new entry. |
| 92 |
@return Setting the entry succeeded. |
| 93 |
*/ |
| 94 |
bool set_entry(int iEntry, uint8_t iR, uint8_t iG, uint8_t iB); |
| 95 |
|
| 96 |
public: // Internal (this rendering engine only) API |
| 97 |
//! Convert A, R, G, B values to a 32bpp colour. |
| 98 |
/*! |
| 99 |
@param iA Amount of opacity (0-255). |
| 100 |
@param iR Amount of red (0-255). |
| 101 |
@param iG Amount of green (0-255). |
| 102 |
@param iB Amount of blue (0-255). |
| 103 |
@return 32bpp value representing the provided colour values. |
| 104 |
*/ |
| 105 |
static constexpr argb_colour pack_argb(uint8_t iA, uint8_t iR, uint8_t iG, |
| 106 |
uint8_t iB) { |
| 107 |
return (static_cast<argb_colour>(iR) << 0) | |
| 108 |
(static_cast<argb_colour>(iG) << 8) | |
| 109 |
(static_cast<argb_colour>(iB) << 16) | |
| 110 |
(static_cast<argb_colour>(iA) << 24); |
| 111 |
} |
| 112 |
|
| 113 |
//! Get the red component of a colour. |
| 114 |
/*! |
| 115 |
@param iColour Colour to examine. |
| 116 |
@return The red component intensity of the colour. |
| 117 |
*/ |
| 118 |
static constexpr uint8_t get_red(argb_colour iColour) { |
| 119 |
return static_cast<uint8_t>((iColour >> 0) & 0xFF); |
| 120 |
} |
| 121 |
|
| 122 |
//! Get the green component of a colour. |
| 123 |
/*! |
| 124 |
@param iColour Colour to examine. |
| 125 |
@return The green component intensity of the colour. |
| 126 |
*/ |
| 127 |
static constexpr uint8_t get_green(argb_colour iColour) { |
| 128 |
return static_cast<uint8_t>((iColour >> 8) & 0xFF); |
| 129 |
} |
| 130 |
|
| 131 |
//! Get the blue component of a colour. |
| 132 |
/*! |
| 133 |
@param iColour Colour to examine. |
| 134 |
@return The blue component intensity of the colour. |
| 135 |
*/ |
| 136 |
static constexpr uint8_t get_blue(argb_colour iColour) { |
| 137 |
return static_cast<uint8_t>((iColour >> 16) & 0xFF); |
| 138 |
} |
| 139 |
|
| 140 |
//! Get the opacity component of a colour. |
| 141 |
/*! |
| 142 |
@param iColour Colour to examine. |
| 143 |
@return The opacity of the colour. |
| 144 |
*/ |
| 145 |
static constexpr uint8_t get_alpha(argb_colour iColour) { |
| 146 |
return static_cast<uint8_t>((iColour >> 24) & 0xFF); |
| 147 |
} |
| 148 |
|
| 149 |
//! Get the internal palette data for fast (read-only) access. |
| 150 |
/*! |
| 151 |
@return Table with all 256 colours of the palette. |
| 152 |
*/ |
| 153 |
const std::array<argb_colour, color_count>& get_argb_data() const; |
| 154 |
|
| 155 |
//! Set an entry of the palette. |
| 156 |
/*! |
| 157 |
@param iEntry Entry to modify. |
| 158 |
@param iVal Palette value to set. |
| 159 |
*/ |
| 160 |
inline void set_argb(int iEntry, uint32_t iVal) { |
| 161 |
colour_index_to_argb_map[iEntry] = iVal; |
| 162 |
} |
| 163 |
|
| 164 |
private: |
| 165 |
//! 32bpp palette colours associated with the 8bpp colour index. |
| 166 |
std::array<argb_colour, color_count> colour_index_to_argb_map{}; |
| 167 |
}; |
| 168 |
|
| 169 |
/*! |
| 170 |
Utility class for decoding 32bpp images. |
| 171 |
*/ |
| 172 |
class full_colour_renderer { |
| 173 |
public: |
| 174 |
//! Initialize the renderer for a specific render. |
| 175 |
/*! |
| 176 |
@param iWidth Pixel width of the resulting image |
| 177 |
@param iHeight Pixel height of the resulting image |
| 178 |
*/ |
| 179 |
full_colour_renderer(int iWidth, int iHeight); |
| 180 |
virtual ~full_colour_renderer() = default; |
| 181 |
|
| 182 |
//! Decode a 32bpp image, and push it to the storage backend. |
| 183 |
/*! |
| 184 |
@param pImg Encoded 32bpp image. |
| 185 |
@param pPalette Palette of a legacy sprite. |
| 186 |
@param iSpriteFlags Flags how to render the sprite. |
| 187 |
@return Decoding was successful. |
| 188 |
*/ |
| 189 |
void decode_image(const uint8_t* pImg, const ::palette* pPalette, |
| 190 |
uint32_t iSpriteFlags); |
| 191 |
|
| 192 |
private: |
| 193 |
//! Store a decoded pixel. Use x and y if necessary. |
| 194 |
/*! |
| 195 |
@param pixel Pixel to store. |
| 196 |
*/ |
| 197 |
virtual void store_argb(uint32_t pixel) = 0; |
| 198 |
|
| 199 |
const int width; |
| 200 |
const int height; |
| 201 |
int x{}; |
| 202 |
int y{}; |
| 203 |
|
| 204 |
//! Push a pixel to the storage. |
| 205 |
/*! |
| 206 |
@param iValue Pixel value to store. |
| 207 |
*/ |
| 208 |
inline void push_pixel(uint32_t iValue) { |
| 209 |
if (y < height) { |
| 210 |
store_argb(iValue); |
| 211 |
x++; |
| 212 |
if (x >= width) { |
| 213 |
x = 0; |
| 214 |
y++; |
| 215 |
} |
| 216 |
} else { |
| 217 |
throw std::logic_error("Attempt to push_pixel past the end of the image"); |
| 218 |
} |
| 219 |
} |
| 220 |
}; |
| 221 |
|
| 222 |
class full_colour_storing : public full_colour_renderer { |
| 223 |
public: |
| 224 |
full_colour_storing(uint32_t* pDest, int iWidth, int iHeight); |
| 225 |
|
| 226 |
private: |
| 227 |
void store_argb(uint32_t pixel) override; |
| 228 |
|
| 229 |
//! Pointer to the storage (not owned by this class). |
| 230 |
uint32_t* destination; |
| 231 |
}; |
| 232 |
|
| 233 |
class wx_storing : public full_colour_renderer { |
| 234 |
public: |
| 235 |
wx_storing(uint8_t* pRGBData, uint8_t* pAData, int iWidth, int iHeight); |
| 236 |
|
| 237 |
private: |
| 238 |
void store_argb(uint32_t pixel) override; |
| 239 |
|
| 240 |
//! Pointer to the RGB storage (not owned by this class). |
| 241 |
uint8_t* rgb_data; |
| 242 |
|
| 243 |
//! Pointer to the Alpha channel storage (not owned by this class). |
| 244 |
uint8_t* alpha_data; |
| 245 |
}; |
| 246 |
|
| 247 |
class render_target { |
| 248 |
public: // External API |
| 249 |
explicit render_target(const render_target_creation_params& params); |
| 250 |
render_target(const render_target& other) = delete; |
| 251 |
render_target& operator=(const render_target& other) = delete; |
| 252 |
~render_target(); |
| 253 |
|
| 254 |
//! Encode an RGB triplet for fillRect() |
| 255 |
static constexpr uint32_t map_colour(uint8_t iR, uint8_t iG, uint8_t iB) { |
| 256 |
return palette::pack_argb(0xFF, iR, iG, iB); |
| 257 |
} |
| 258 |
|
| 259 |
//! Update the parameters for the render target |
| 260 |
bool update(const render_target_creation_params& params); |
| 261 |
|
| 262 |
//! Get the reason for the last operation failing |
| 263 |
const char* get_last_error(); |
| 264 |
|
| 265 |
//! Begin rendering a new frame |
| 266 |
bool start_frame(); |
| 267 |
|
| 268 |
//! Finish rendering the current frame and present it |
| 269 |
bool end_frame(); |
| 270 |
|
| 271 |
//! Paint the entire render target black |
| 272 |
bool fill_black(); |
| 273 |
|
| 274 |
//! Paint the entire render target with a solid colour |
| 275 |
bool fill_colour(uint32_t colour); |
| 276 |
|
| 277 |
//! Sets a blue filter on the current surface. |
| 278 |
// Used to add the blue effect when the game is paused. |
| 279 |
void set_blue_filter_active(bool bActivate); |
| 280 |
|
| 281 |
//! Fill a rectangle of the render target with a solid colour |
| 282 |
bool fill_rect(uint32_t iColour, int iX, int iY, int iW, int iH); |
| 283 |
|
| 284 |
//! Sets a minimum size for the render target |
| 285 |
int set_minimum_size(int width, int height); |
| 286 |
|
| 287 |
class scoped_clip { |
| 288 |
public: |
| 289 |
scoped_clip(render_target*, const clip_rect* pRect); |
| 290 |
~scoped_clip(); |
| 291 |
|
| 292 |
private: |
| 293 |
render_target* target{nullptr}; |
| 294 |
}; |
| 295 |
|
| 296 |
//! Push a new clip rectangle. |
| 297 |
void push_clip_rect(const clip_rect* pRect); |
| 298 |
|
| 299 |
//! Restore the previous clip rectangle. |
| 300 |
void pop_clip_rect(); |
| 301 |
|
| 302 |
//! Get the width of the render target (in pixels) |
| 303 |
int get_width() const; |
| 304 |
|
| 305 |
//! Get the height of the render target (in pixels) |
| 306 |
int get_height() const; |
| 307 |
|
| 308 |
//! Enable optimisations for non-overlapping draws |
| 309 |
void start_nonoverlapping_draws(); |
| 310 |
|
| 311 |
//! Disable optimisations for non-overlapping draws |
| 312 |
void finish_nonoverlapping_draws(); |
| 313 |
|
| 314 |
//! Set the cursor to be used |
| 315 |
void set_cursor(cursor* pCursor); |
| 316 |
|
| 317 |
//! Update the cursor position (if the cursor is being simulated) |
| 318 |
void set_cursor_position(int iX, int iY); |
| 319 |
|
| 320 |
//! Take a screenshot and save it as a PNG file. |
| 321 |
bool take_screenshot(const char* file_path) const; |
| 322 |
|
| 323 |
//! Set the amount by which future draw operations are scaled. |
| 324 |
/*! |
| 325 |
@param fScale New scale to use. |
| 326 |
@param eWhatToScale Th kind of items to scale. |
| 327 |
@return Whether the scale could be set. |
| 328 |
*/ |
| 329 |
bool set_scale_factor(double fScale, scaled_items eWhatToScale); |
| 330 |
|
| 331 |
//! Set the window caption |
| 332 |
void set_caption(const char* sCaption); |
| 333 |
|
| 334 |
//! Toggle mouse capture on the window. |
| 335 |
void set_window_grab(bool bActivate); |
| 336 |
|
| 337 |
//! Get any user-displayable information to describe the renderer path used |
| 338 |
const char* get_renderer_details() const; |
| 339 |
|
| 340 |
// If you add any extra methods here which are called from outside the |
| 341 |
// rendering engine, then be sure to at least add dummy implementations |
| 342 |
// to the other rendering engines. |
| 343 |
|
| 344 |
public: // Internal (this rendering engine only) API |
| 345 |
class scoped_buffer { |
| 346 |
public: |
| 347 |
virtual ~scoped_buffer() = default; |
| 348 |
}; |
| 349 |
|
| 350 |
class scoped_target_texture final : public scoped_buffer { |
| 351 |
public: |
| 352 |
scoped_target_texture(render_target* pTarget, int iX, int iY, int iWidth, |
| 353 |
int iHeight, bool bScale); |
| 354 |
scoped_target_texture(scoped_target_texture&) = delete; |
| 355 |
scoped_target_texture& operator=(scoped_target_texture&) = delete; |
| 356 |
~scoped_target_texture() override; |
| 357 |
void offset(SDL_FRect& targetRect) const; |
| 358 |
double scale_factor() const; |
| 359 |
bool is_target() const; |
| 360 |
|
| 361 |
private: |
| 362 |
render_target* target; |
| 363 |
scoped_target_texture* previous_target; |
| 364 |
SDL_Rect rect; |
| 365 |
bool scale; |
| 366 |
SDL_Texture* texture{nullptr}; |
| 367 |
}; |
| 368 |
|
| 369 |
SDL_Renderer* get_renderer() const { return renderer; } |
| 370 |
|
| 371 |
//! Should bitmaps be scaled? |
| 372 |
/*! |
| 373 |
@param [out] pFactor If the function returns \c true, the factor to use |
| 374 |
for scaling (can be \c nullptr if not interested in the value). |
| 375 |
@return Whether bitmaps should be scaled. |
| 376 |
*/ |
| 377 |
bool should_scale_bitmaps(double* pFactor); |
| 378 |
|
| 379 |
SDL_Texture* create_palettized_texture(int iWidth, int iHeight, |
| 380 |
const uint8_t* pPixels, |
| 381 |
const ::palette* pPalette, |
| 382 |
uint32_t iSpriteFlags) const; |
| 383 |
SDL_Texture* create_texture(int iWidth, int iHeight, |
| 384 |
const uint32_t* pPixels) const; |
| 385 |
void draw(SDL_Texture* pTexture, const SDL_Rect* prcSrcRect, |
| 386 |
const SDL_Rect* prcDstRect, int iFlags); |
| 387 |
void draw_line(line_sequence* pLine, int iX, int iY); |
| 388 |
|
| 389 |
//! Begin drawing to an intermediate unscaled texture targeting the given |
| 390 |
//! location and size. The intermediate drawing will be committed once |
| 391 |
//! the returned scoped_buffer is destroyed. |
| 392 |
/*! |
| 393 |
@param iX X-coordinate of left side of drawing rectangle. |
| 394 |
@param iY Y-coordinate of top side of drawing rectangle. |
| 395 |
@param iWidth Width of drawing rectangle. |
| 396 |
@param iHeight Height of drawing rectangle. |
| 397 |
*/ |
| 398 |
std::unique_ptr<scoped_buffer> begin_intermediate_drawing(int iX, int iY, |
| 399 |
int iWidth, |
| 400 |
int iHeight); |
| 401 |
|
| 402 |
private: |
| 403 |
friend class scoped_target_texture; |
| 404 |
|
| 405 |
double draw_scale() const; |
| 406 |
|
| 407 |
void destroy_intermediate_textures(); |
| 408 |
|
| 409 |
SDL_Window* window{nullptr}; |
| 410 |
SDL_Renderer* renderer{nullptr}; |
| 411 |
scoped_target_texture* current_target{nullptr}; |
| 412 |
std::unique_ptr<scoped_target_texture> zoom_buffer; |
| 413 |
SDL_PixelFormat* pixel_format{nullptr}; |
| 414 |
bool blue_filter_active{false}; |
| 415 |
cursor* game_cursor{nullptr}; |
| 416 |
double bitmap_scale_factor{1.0}; ///< Bitmap scale factor. |
| 417 |
double global_scale_factor{1.0}; ///< Global scale factor. |
| 418 |
int width{-1}; |
| 419 |
int height{-1}; |
| 420 |
int cursor_x{}; |
| 421 |
int cursor_y{}; |
| 422 |
|
| 423 |
std::stack<SDL_Rect> clip_rects; ///< Stack of requested clip rects. |
| 424 |
|
| 425 |
// Intermediate textures used in the production of the current frame. |
| 426 |
// These are destroyed after the frame is presented. |
| 427 |
std::vector<SDL_Texture*> intermediate_textures; |
| 428 |
|
| 429 |
bool scale_bitmaps{false}; ///< Whether bitmaps should be scaled. |
| 430 |
bool supports_target_textures{}; |
| 431 |
|
| 432 |
// In SDL2 < 2.0.4 there is an issue with the y coordinates used for |
| 433 |
// ClipRects in opengl and opengles. |
| 434 |
// see: https://bugzilla.libsdl.org/show_bug.cgi?id=2700 |
| 435 |
bool apply_opengl_clip_fix{}; |
| 436 |
bool direct_zoom{}; |
| 437 |
}; |
| 438 |
|
| 439 |
//! Stored image. |
| 440 |
class raw_bitmap { |
| 441 |
public: |
| 442 |
raw_bitmap() = default; |
| 443 |
~raw_bitmap(); |
| 444 |
|
| 445 |
//! Set the palette of the image. |
| 446 |
/*! |
| 447 |
@param pPalette Palette to set for this image. |
| 448 |
*/ |
| 449 |
void set_palette(const ::palette* pPalette); |
| 450 |
|
| 451 |
//! Load the image from the supplied pixel data. |
| 452 |
/*! |
| 453 |
Loader uses the palette supplied before. |
| 454 |
@param pPixelData Image data loaded from a TH file. |
| 455 |
@param iPixelDataLength Size of the loaded image data. |
| 456 |
@param iWidth Width of the image. |
| 457 |
@param pEventualCanvas Canvas to render the image to (eventually). |
| 458 |
@param spriteFlags |
| 459 |
@return Loading was a success. |
| 460 |
*/ |
| 461 |
void load_from_th_file(const uint8_t* pPixelData, size_t iPixelDataLength, |
| 462 |
int iWidth, render_target* pEventualCanvas, |
| 463 |
uint32_t spriteFlags); |
| 464 |
|
| 465 |
//! Draw the image at a given position at the given canvas. |
| 466 |
/*! |
| 467 |
@param pCanvas Canvas to draw at. |
| 468 |
@param iX Destination x position. |
| 469 |
@param iY Destination y position. |
| 470 |
*/ |
| 471 |
void draw(render_target* pCanvas, int iX, int iY); |
| 472 |
|
| 473 |
//! Draw part of the image at a given position at the given canvas. |
| 474 |
/*! |
| 475 |
@param pCanvas Canvas to draw at. |
| 476 |
@param iX Destination x position. |
| 477 |
@param iY Destination y position. |
| 478 |
@param iSrcX X position of the part to display. |
| 479 |
@param iSrcY Y position of the part to display. |
| 480 |
@param iWidth Width of the part to display. |
| 481 |
@param iHeight Height of the part to display. |
| 482 |
*/ |
| 483 |
void draw(render_target* pCanvas, int iX, int iY, int iSrcX, int iSrcY, |
| 484 |
int iWidth, int iHeight); |
| 485 |
|
| 486 |
private: |
| 487 |
//! Image stored in SDL format for quick rendering. |
| 488 |
SDL_Texture* texture{nullptr}; |
| 489 |
|
| 490 |
//! Palette of the image. |
| 491 |
const ::palette* bitmap_palette{nullptr}; |
| 492 |
|
| 493 |
//! Target canvas. |
| 494 |
render_target* target{nullptr}; |
| 495 |
|
| 496 |
//! Width of the stored image. |
| 497 |
int width{}; |
| 498 |
|
| 499 |
//! Height of the stored image. |
| 500 |
int height{}; |
| 501 |
}; |
| 502 |
|
| 503 |
//! Sheet of sprites. |
| 504 |
class sprite_sheet { |
| 505 |
public: // External API |
| 506 |
sprite_sheet() = default; |
| 507 |
~sprite_sheet(); |
| 508 |
|
| 509 |
//! Set the palette to use for the sprites in the sheet. |
| 510 |
/*! |
| 511 |
@param pPalette Palette to use for the sprites at the sheet. |
| 512 |
*/ |
| 513 |
void set_palette(const ::palette* pPalette); |
| 514 |
|
| 515 |
//! Load the sprites from the supplied data (using the palette supplied |
| 516 |
//! earlier). |
| 517 |
/*! |
| 518 |
@param pTableData Start of table data with TH sprite information (see |
| 519 |
th_sprite_properties). |
| 520 |
@param iTableDataLength Length of the table data. |
| 521 |
@param pChunkData Start of image data (chunks). |
| 522 |
@param iChunkDataLength Length of the chunk data. |
| 523 |
@param bComplexChunks Whether the supplied chunks are 'complex'. |
| 524 |
@param pEventualCanvas Canvas to draw at. |
| 525 |
@return Loading succeeded. |
| 526 |
*/ |
| 527 |
bool load_from_th_file(const uint8_t* pTableData, size_t iTableDataLength, |
| 528 |
const uint8_t* pChunkData, size_t iChunkDataLength, |
| 529 |
bool bComplexChunks, render_target* pEventualCanvas); |
| 530 |
|
| 531 |
//! Set the data of a sprite. |
| 532 |
/*! |
| 533 |
@param iSprite Number of the sprite to set. |
| 534 |
@param pData Data of the sprite. |
| 535 |
@param bTakeData Whether the data block may be taken (must be new[] |
| 536 |
then). |
| 537 |
@param iDataLength Length of the data. |
| 538 |
@param iWidth Width of the sprite. |
| 539 |
@param iHeight Height of the sprite. |
| 540 |
@return Setting the sprite succeeded. |
| 541 |
*/ |
| 542 |
bool set_sprite_data(size_t iSprite, const uint8_t* pData, bool bTakeData, |
| 543 |
size_t iDataLength, int iWidth, int iHeight); |
| 544 |
|
| 545 |
//! Supply a new mapped palette to a sprite. |
| 546 |
/*! |
| 547 |
@param iSprite Sprite getting the mapped palette. |
| 548 |
@param pMap The palette map to apply. |
| 549 |
@param iAlt32 What to do for a 32bpp sprite (#thdf_alt32_mask bits). |
| 550 |
*/ |
| 551 |
void set_sprite_alt_palette_map(size_t iSprite, const uint8_t* pMap, |
| 552 |
uint32_t iAlt32); |
| 553 |
|
| 554 |
//! Get the number of sprites at the sheet. |
| 555 |
/*! |
| 556 |
@return The number of sprites available at the sheet. |
| 557 |
*/ |
| 558 |
size_t get_sprite_count() const; |
| 559 |
|
| 560 |
//! Set the number of sprites in the sheet. |
| 561 |
/*! |
| 562 |
@param iCount The desired number of sprites. |
| 563 |
@param pCanvas Canvas to draw at. |
| 564 |
@return Whether the number of sprites could be allocated. |
| 565 |
*/ |
| 566 |
bool set_sprite_count(size_t iCount, render_target* pCanvas); |
| 567 |
|
| 568 |
//! Get size of a sprite. |
| 569 |
/*! |
| 570 |
@param iSprite Sprite to get info from. |
| 571 |
@param pWidth [out] If not nullptr, the sprite width is stored in the |
| 572 |
destination. |
| 573 |
@param pHeight [out] If not nullptr, the sprite height is stored in the |
| 574 |
destination. |
| 575 |
@return Size could be provided for the sprite. |
| 576 |
*/ |
| 577 |
bool get_sprite_size(size_t iSprite, int* pWidth, int* pHeight) const; |
| 578 |
|
| 579 |
//! Get size of a sprite, assuming all input is correctly supplied. |
| 580 |
/*! |
| 581 |
@param iSprite Sprite to get info from. |
| 582 |
@param pWidth [out] The sprite width is stored in the destination. |
| 583 |
@param pHeight [out] The sprite height is stored in the destination. |
| 584 |
*/ |
| 585 |
void get_sprite_size_unchecked(size_t iSprite, int* pWidth, |
| 586 |
int* pHeight) const; |
| 587 |
|
| 588 |
//! Get the best colour to represent the sprite. |
| 589 |
/*! |
| 590 |
@param iSprite Sprite number to analyze. |
| 591 |
@param pColour [out] Resulting colour. |
| 592 |
@return Best colour could be established. |
| 593 |
*/ |
| 594 |
bool get_sprite_average_colour(size_t iSprite, argb_colour* pColour) const; |
| 595 |
|
| 596 |
//! Return whether the given sprite show any pixel when displayed. |
| 597 |
/*! |
| 598 |
@param iSprite Sprite number to analyze. |
| 599 |
@return Whether any pixel of the sprite is visible. |
| 600 |
*/ |
| 601 |
bool is_sprite_visible(size_t iSprite) const; |
| 602 |
|
| 603 |
//! Draw a sprite onto the canvas. |
| 604 |
/*! |
| 605 |
@param pCanvas Canvas to draw on. |
| 606 |
@param iSprite Sprite to draw. |
| 607 |
@param iX X position to draw the sprite. |
| 608 |
@param iY Y position to draw the sprite. |
| 609 |
@param iFlags Flags to apply for drawing. |
| 610 |
@param effect_ticks The number of ticks into the effect animation. |
| 611 |
@param effect The animation effect to apply to the sprite. |
| 612 |
@param scale_factor How much to scale the sprite when drawing. |
| 613 |
*/ |
| 614 |
void draw_sprite(render_target* pCanvas, size_t iSprite, int iX, int iY, |
| 615 |
uint32_t iFlags, size_t effect_ticks = 0u, |
| 616 |
animation_effect effect = animation_effect::none, |
| 617 |
int scale_factor = 1); |
| 618 |
|
| 619 |
//! Test whether a sprite was hit. |
| 620 |
/*! |
| 621 |
@param iSprite Sprite being tested. |
| 622 |
@param iX X position of the point to test relative to the origin of the |
| 623 |
sprite. |
| 624 |
@param iY Y position of the point to test relative to the origin of the |
| 625 |
sprite. |
| 626 |
@param iFlags Draw flags to apply to the sprite before testing. |
| 627 |
@return Whether the sprite covers the give point. |
| 628 |
*/ |
| 629 |
bool hit_test_sprite(size_t iSprite, int iX, int iY, uint32_t iFlags) const; |
| 630 |
|
| 631 |
public: // Internal (this rendering engine only) API |
| 632 |
//! Draw a sprite into wxImage data arrays (for the Map Editor) |
| 633 |
/*! |
| 634 |
@param iSprite Sprite number to draw. |
| 635 |
@param pRGBData Output RGB data array. |
| 636 |
@param pAData Output Alpha channel array. |
| 637 |
*/ |
| 638 |
void wx_draw_sprite(size_t iSprite, uint8_t* pRGBData, uint8_t* pAData); |
| 639 |
|
| 640 |
private: |
| 641 |
friend class cursor; |
| 642 |
|
| 643 |
//! Sprites of the sheet. |
| 644 |
struct sprite { |
| 645 |
//! SDL structure containing the sprite with original palette. |
| 646 |
SDL_Texture* texture; |
| 647 |
|
| 648 |
//! SDL structure containing the sprite with alternative palette. |
| 649 |
SDL_Texture* alt_texture; |
| 650 |
|
| 651 |
//! Data of the sprite. |
| 652 |
const uint8_t* data; |
| 653 |
|
| 654 |
//! Alternative palette (if available). |
| 655 |
const uint8_t* alt_palette_map; |
| 656 |
|
| 657 |
//! Flags how to render the sprite, contains #THDF_Alt32_Mask bits. |
| 658 |
uint32_t sprite_flags; |
| 659 |
|
| 660 |
//! Width of the sprite. |
| 661 |
int width; |
| 662 |
|
| 663 |
//! Height of the sprite. |
| 664 |
int height; |
| 665 |
}* sprites{nullptr}; |
| 666 |
|
| 667 |
//! Original palette. |
| 668 |
const ::palette* palette{nullptr}; |
| 669 |
|
| 670 |
//! Target to render to. |
| 671 |
render_target* target{nullptr}; |
| 672 |
|
| 673 |
//! Number of sprites in the sprite sheet. |
| 674 |
size_t sprite_count{}; |
| 675 |
|
| 676 |
//! Free memory of a single sprite. |
| 677 |
/*! |
| 678 |
@param iNumber Number of the sprite to clear. |
| 679 |
*/ |
| 680 |
void _freeSingleSprite(size_t iNumber); |
| 681 |
|
| 682 |
//! Free the memory used by the sprites. Also releases the SDL bitmaps. |
| 683 |
void _freeSprites(); |
| 684 |
|
| 685 |
//! Construct an alternative version (with its alternative palette map) of |
| 686 |
//! the sprite. |
| 687 |
/*! |
| 688 |
@param pSprite Sprite to change. |
| 689 |
@return SDL texture containing the sprite. |
| 690 |
*/ |
| 691 |
SDL_Texture* _makeAltBitmap(sprite* pSprite); |
| 692 |
}; |
| 693 |
|
| 694 |
class cursor { |
| 695 |
public: |
| 696 |
cursor() = default; |
| 697 |
~cursor(); |
| 698 |
|
| 699 |
bool create_from_sprite(sprite_sheet* pSheet, size_t iSprite, |
| 700 |
int iHotspotX = 0, int iHotspotY = 0); |
| 701 |
|
| 702 |
void use(render_target* pTarget); |
| 703 |
|
| 704 |
static bool set_position(render_target* pTarget, int iX, int iY); |
| 705 |
|
| 706 |
void draw(render_target* pCanvas, int iX, int iY); |
| 707 |
|
| 708 |
private: |
| 709 |
SDL_Surface* bitmap{nullptr}; |
| 710 |
SDL_Cursor* hidden_cursor{nullptr}; |
| 711 |
int hotspot_x{}; |
| 712 |
int hotspot_y{}; |
| 713 |
}; |
| 714 |
|
| 715 |
class line_sequence { |
| 716 |
public: |
| 717 |
line_sequence(); |
| 718 |
|
| 719 |
void move_to(double fX, double fY); |
| 720 |
|
| 721 |
void line_to(double fX, double fY); |
| 722 |
|
| 723 |
void set_width(double lineWidth); |
| 724 |
|
| 725 |
void draw(render_target* pCanvas, int iX, int iY); |
| 726 |
|
| 727 |
void set_colour(uint8_t iR, uint8_t iG, uint8_t iB, uint8_t iA = 255); |
| 728 |
|
| 729 |
void persist(lua_persist_writer* pWriter) const; |
| 730 |
void depersist(lua_persist_reader* pReader); |
| 731 |
|
| 732 |
private: |
| 733 |
friend class render_target; |
| 734 |
|
| 735 |
enum class line_command : uint32_t { move = 0, line = 1 }; |
| 736 |
|
| 737 |
class line_element { |
| 738 |
public: |
| 739 |
line_command type; |
| 740 |
double x, y; |
| 741 |
line_element(line_command type, double x, double y) |
| 742 |
: type(type), x(x), y(y) {} |
| 743 |
}; |
| 744 |
|
| 745 |
std::vector<line_element> line_elements; |
| 746 |
double width{1}; |
| 747 |
uint8_t red{0}, green{0}, blue{0}, alpha{255}; |
| 748 |
}; |
| 749 |
|
| 750 |
#endif // CORSIX_TH_TH_GFX_SDL_H_ |