| 1 |
/* |
| 2 |
Copyright (c) 2009 Peter "Corsix" Cawley |
| 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_H_ |
| 24 |
#define CORSIX_TH_TH_GFX_H_ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#include <map> |
| 29 |
#include <string> |
| 30 |
#include <string_view> |
| 31 |
#include <utility> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
#include "lua.hpp" |
| 35 |
#include "th.h" |
| 36 |
#include "th_gfx_common.h" |
| 37 |
#include "th_gfx_sdl.h" |
| 38 |
#include "th_lua.h" |
| 39 |
|
| 40 |
class lua_persist_reader; |
| 41 |
class lua_persist_writer; |
| 42 |
class memory_reader; |
| 43 |
struct map_tile; |
| 44 |
|
| 45 |
enum class scaled_items { none, sprite_sheets, bitmaps, all }; |
| 46 |
|
| 47 |
void clip_rect_intersection(clip_rect& rcClip, const clip_rect& rcIntersect); |
| 48 |
|
| 49 |
//! Bitflags for drawing operations |
| 50 |
enum draw_flags : uint32_t { |
| 51 |
/** Sprite drawing flags **/ |
| 52 |
/* Where possible, designed to be the same values used by TH data files */ |
| 53 |
|
| 54 |
//! Draw with the left becoming the right and vice versa |
| 55 |
thdf_flip_horizontal = 1 << 0, |
| 56 |
//! Draw with the top becoming the bottom and vice versa |
| 57 |
thdf_flip_vertical = 1 << 1, |
| 58 |
//! Draw with 50% transparency. If set together with thdf_alpha_75, don't draw |
| 59 |
//! at all. |
| 60 |
thdf_alpha_50 = 1 << 2, |
| 61 |
//! Draw with 75% transparency. If set together with thdf_alpha_50, don't draw |
| 62 |
//! at all. |
| 63 |
thdf_alpha_75 = 1 << 3, |
| 64 |
//! Draw using a remapped palette |
| 65 |
thdf_alt_palette = 1 << 4, |
| 66 |
|
| 67 |
/** How to draw alternative palette in 32bpp. */ |
| 68 |
/* A 3 bit field (bits 5,6,7), currently 2 bits used. */ |
| 69 |
|
| 70 |
//! Lowest bit of the field. |
| 71 |
thdf_alt32_start = 5, |
| 72 |
//! Mask for the 32bpp alternative drawing values. |
| 73 |
thdf_alt32_mask = 0x7 << thdf_alt32_start, |
| 74 |
|
| 75 |
//! Draw the sprite with the normal palette (fallback option). |
| 76 |
thdf_alt32_plain = 0 << thdf_alt32_start, |
| 77 |
//! Draw the sprite in grey scale. |
| 78 |
thdf_alt32_grey_scale = 1 << thdf_alt32_start, |
| 79 |
//! Draw the sprite with red and blue colours swapped. |
| 80 |
thdf_alt32_blue_red_swap = 2 << thdf_alt32_start, |
| 81 |
|
| 82 |
//! Attach to the early sprite list (right-to-left pass) |
| 83 |
/* (should be set prior to attaching to a tile) */ |
| 84 |
thdf_early_list = 1 << 10, |
| 85 |
|
| 86 |
//! Hit-test using bounding-box precision rather than pixel-perfect |
| 87 |
thdf_bound_box_hit_test = 1 << 12, |
| 88 |
//! Apply a cropping operation prior to drawing |
| 89 |
thdf_crop = 1 << 13, |
| 90 |
//! Draw using nearest pixel hinting |
| 91 |
thdf_nearest = 1 << 14, |
| 92 |
}; |
| 93 |
|
| 94 |
// Integer 2D pair. |
| 95 |
struct xy_pair { |
| 96 |
int x{}; ///< Value of the X coordinate. |
| 97 |
int y{}; ///< Value of the Y coordinate. |
| 98 |
}; |
| 99 |
|
| 100 |
/*! |
| 101 |
Base class for a linked list of drawable objects. |
| 102 |
Note that "object" is used as a generic term, not in specific reference to |
| 103 |
game objects (though they are the most common thing in drawing lists). |
| 104 |
*/ |
| 105 |
// TODO: Replace this struct with something cleaner |
| 106 |
struct drawable : public link_list { |
| 107 |
drawable() = default; |
| 108 |
|
| 109 |
//! Draw the object at a specific point on a render target |
| 110 |
/*! |
| 111 |
Can also "draw" the object to the speakers, i.e. play sounds. |
| 112 |
*/ |
| 113 |
virtual void draw_fn(render_target* canvas, const xy_pair& draw_pos) = 0; |
| 114 |
|
| 115 |
//! Perform a hit test against the object |
| 116 |
/*! |
| 117 |
Should return true if when the object is drawn at \p draw_pos on a |
| 118 |
canvas, the point \p obj_pos is within / on the object. |
| 119 |
*/ |
| 120 |
virtual bool hit_test_fn(const xy_pair& draw_pos, const xy_pair& obj_pos) = 0; |
| 121 |
|
| 122 |
/** Returns true if instance is a multiple frame animation. |
| 123 |
Should be overloaded in derived class. |
| 124 |
*/ |
| 125 |
virtual bool is_multiple_frame_animation_fn() = 0; |
| 126 |
|
| 127 |
int get_drawing_layer() { return drawing_layer; } |
| 128 |
void set_drawing_layer(int layer) { drawing_layer = layer; } |
| 129 |
|
| 130 |
//! Drawing flags (zero or more list flags from #draw_flags). |
| 131 |
uint32_t flags{0}; |
| 132 |
|
| 133 |
private: |
| 134 |
int drawing_layer{0}; |
| 135 |
}; |
| 136 |
|
| 137 |
/*! |
| 138 |
Utility class for decoding Theme Hospital "chunked" graphics files. |
| 139 |
Generally used internally by sprite_sheet. |
| 140 |
*/ |
| 141 |
class chunk_renderer { |
| 142 |
public: |
| 143 |
//! Initialise a renderer for a specific size result |
| 144 |
/*! |
| 145 |
@param width Pixel width of the resulting image |
| 146 |
@param height Pixel height of the resulting image |
| 147 |
@param start An iterator to the start writing the resulting image to. |
| 148 |
Must point to a container of at least width * height. |
| 149 |
*/ |
| 150 |
chunk_renderer(int width, int height, std::vector<uint8_t>::iterator start); |
| 151 |
|
| 152 |
//! Convert a stream of chunks into a raw bitmap |
| 153 |
/*! |
| 154 |
@param pData Stream data. |
| 155 |
@param iDataLen Length of \a pData. |
| 156 |
@param bComplex true if pData is a stream of "complex" chunks, false if |
| 157 |
pData is a stream of "simple" chunks. Passing the wrong value will |
| 158 |
usually result in a very visible wrong result. |
| 159 |
*/ |
| 160 |
void decode_chunks(const uint8_t* pData, int iDataLen, bool bComplex); |
| 161 |
|
| 162 |
//! Perform a "copy" chunk (normally called by decodeChunks) |
| 163 |
void chunk_copy(int npixels, const uint8_t* in_data); |
| 164 |
|
| 165 |
//! Perform a "fill" chunk (normally called by decodeChunks) |
| 166 |
void chunk_fill(int npixels, uint8_t value); |
| 167 |
|
| 168 |
//! Perform a "fill to end of line" chunk (normally called by decodeChunks) |
| 169 |
void chunk_fill_to_end_of_line(uint8_t value); |
| 170 |
|
| 171 |
//! Perform a "fill to end of file" chunk (normally called by decodeChunks) |
| 172 |
void chunk_finish(uint8_t value); |
| 173 |
|
| 174 |
private: |
| 175 |
inline bool is_done() const { return ptr == end; } |
| 176 |
inline void fix_n_pixels(int& npixels) const; |
| 177 |
inline void increment_position(int npixels); |
| 178 |
|
| 179 |
std::vector<uint8_t>::iterator ptr, end; |
| 180 |
int x{0}, y{0}; |
| 181 |
int width, height; |
| 182 |
bool skip_eol{false}; |
| 183 |
}; |
| 184 |
|
| 185 |
//! Number of available layers, must be less or equal to 16 as it is stored in |
| 186 |
// a nibble. |
| 187 |
const int max_number_of_layers = 13; |
| 188 |
|
| 189 |
//! Layer information (see animation_manager::draw_frame) |
| 190 |
struct layers { |
| 191 |
uint8_t layer_contents[max_number_of_layers]{}; |
| 192 |
}; |
| 193 |
|
| 194 |
/** Key value for finding an animation. */ |
| 195 |
struct animation_key { |
| 196 |
std::string name; ///< Name of the animations. |
| 197 |
int tile_size; ///< Size of a tile. |
| 198 |
}; |
| 199 |
|
| 200 |
//! Less-than operator for map-sorting. |
| 201 |
/*! |
| 202 |
@param oK First key value. |
| 203 |
@param oL Second key value. |
| 204 |
@return Whether \a oK should be before \a oL. |
| 205 |
*/ |
| 206 |
inline bool operator<(const animation_key& oK, const animation_key& oL) { |
| 207 |
if (oK.tile_size != oL.tile_size) return oK.tile_size < oL.tile_size; |
| 208 |
return oK.name < oL.name; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Start frames of an animation, in each view direction. |
| 213 |
* A negative number indicates there is no animation in that direction. |
| 214 |
*/ |
| 215 |
struct animation_start_frames { |
| 216 |
long north{-1}; ///< Animation start frame for the 'north' view. |
| 217 |
long east{-1}; ///< Animation start frame for the 'east' view. |
| 218 |
long south{-1}; ///< Animation start frame for the 'south' view. |
| 219 |
long west{-1}; ///< Animation start frame for the 'west' view. |
| 220 |
}; |
| 221 |
|
| 222 |
/** Map holding the custom animations. */ |
| 223 |
typedef std::map<animation_key, animation_start_frames> named_animations_map; |
| 224 |
|
| 225 |
/** Insertion data structure. */ |
| 226 |
typedef std::pair<animation_key, animation_start_frames> named_animation_pair; |
| 227 |
|
| 228 |
//! Theme Hospital sprite animation manager |
| 229 |
/*! |
| 230 |
An animation manager takes a sprite sheet and four animation information |
| 231 |
files, and uses them to draw animation frames and provide information about |
| 232 |
the animations. |
| 233 |
*/ |
| 234 |
class animation_manager { |
| 235 |
public: |
| 236 |
animation_manager() = default; |
| 237 |
|
| 238 |
void set_sprite_sheet(sprite_sheet* pSpriteSheet); |
| 239 |
|
| 240 |
//! Load original animations. |
| 241 |
/*! |
| 242 |
set_sprite_sheet() must be called before calling this. |
| 243 |
@param pStartData Animation first frame indices (e.g. VSTART-1.ANI) |
| 244 |
@param iStartDataLength Length of \a pStartData. |
| 245 |
@param pFrameData Frame details (e.g. VFRA-1.ANI) |
| 246 |
@param iFrameDataLength Length of \a pFrameData |
| 247 |
@param pListData Element indices list (e.g. VLIST-1.ANI) |
| 248 |
@param iListDataLength Length of \a pListData |
| 249 |
@param pElementData Element details (e.g. VELE-1.ANI) |
| 250 |
@param iElementDataLength Length of \a pElementData |
| 251 |
@return Loading was successful. |
| 252 |
*/ |
| 253 |
bool load_from_th_file(const uint8_t* pStartData, size_t iStartDataLength, |
| 254 |
const uint8_t* pFrameData, size_t iFrameDataLength, |
| 255 |
const uint8_t* pListData, size_t iListDataLength, |
| 256 |
const uint8_t* pElementData, |
| 257 |
size_t iElementDataLength); |
| 258 |
|
| 259 |
//! Set the video target. |
| 260 |
/*! |
| 261 |
@param pCanvas Video surface to use. |
| 262 |
*/ |
| 263 |
void set_canvas(render_target* pCanvas); |
| 264 |
|
| 265 |
//! Load free animations. |
| 266 |
/*! |
| 267 |
@param pData Start of the loaded data. |
| 268 |
@param iDataLength Length of the loaded data. |
| 269 |
@return Loading was successful. |
| 270 |
*/ |
| 271 |
bool load_custom_animations(const uint8_t* pData, size_t iDataLength); |
| 272 |
|
| 273 |
//! Get the total number of animations |
| 274 |
size_t get_animation_count() const; |
| 275 |
|
| 276 |
//! Get the total number of animation frames |
| 277 |
size_t get_frame_count() const; |
| 278 |
|
| 279 |
//! Get the index of the first frame of an animation |
| 280 |
size_t get_first_frame(size_t iAnimation) const; |
| 281 |
|
| 282 |
//! Get the index of the frame after a given frame |
| 283 |
/*! |
| 284 |
To draw an animation frame by frame, call get_first_frame() to get the |
| 285 |
index of the first frame, and then keep on calling get_next_frame() |
| 286 |
using the most recent return value from get_next_frame() or |
| 287 |
get_first_frame(). |
| 288 |
*/ |
| 289 |
size_t get_next_frame(size_t iFrame) const; |
| 290 |
|
| 291 |
//! Set the palette remap data for an animation |
| 292 |
/*! |
| 293 |
This sets the palette remap data for every single sprite used by the |
| 294 |
given animation. If the animation (or any of its sprites) are drawn |
| 295 |
using the thdf_alt_palette flag, then palette indices will be mapped to |
| 296 |
new palette indices by the 256 byte array pMap. This is typically used |
| 297 |
to draw things in different colours or in greyscale. |
| 298 |
*/ |
| 299 |
void set_animation_alt_palette_map(size_t iAnimation, const uint8_t* pMap, |
| 300 |
uint32_t iAlt32); |
| 301 |
|
| 302 |
//! Draw an animation frame |
| 303 |
/*! |
| 304 |
@param pCanvas The render target to draw onto. |
| 305 |
@param iFrame The frame index to draw (should be in range [0, |
| 306 |
getFrameCount() - 1]) |
| 307 |
@param oLayers Information to decide what to draw on each layer. |
| 308 |
An animation is comprised of up to thirteen layers, numbered 0 |
| 309 |
through 12. Some animations will have different options for what to |
| 310 |
render on each layer. For example, patient animations generally |
| 311 |
have the different options on layer 1 as different clothes, so if |
| 312 |
layer 1 is set to the value 0, they may have their default clothes, |
| 313 |
and if set to the value 2 or 4 or 6, they may have other clothes. |
| 314 |
Play with the AnimView tool for a better understanding of layers, |
| 315 |
though note that while it can draw more than one option on each |
| 316 |
layer, this class can only draw a single option for each layer. |
| 317 |
@param iX The screen position to use as the animation X origin. |
| 318 |
@param iY The screen position to use as the animation Y origin. |
| 319 |
@param iFlags Zero or more THDrawFlags flags. |
| 320 |
@param patient_effect The animation effect to apply to the patient. |
| 321 |
@param patient_effect_offset The number of ticks to offset the effect |
| 322 |
animation by. |
| 323 |
@param scale_factor |
| 324 |
*/ |
| 325 |
void draw_frame(render_target* pCanvas, size_t iFrame, |
| 326 |
const ::layers& oLayers, int iX, int iY, uint32_t iFlags, |
| 327 |
animation_effect patient_effect = animation_effect::none, |
| 328 |
size_t patient_effect_offset = 0, int scale_factor = 1) const; |
| 329 |
|
| 330 |
void get_frame_extent(size_t iFrame, const ::layers& oLayers, int* pMinX, |
| 331 |
int* pMaxX, int* pMinY, int* pMaxY, |
| 332 |
uint32_t iFlags) const; |
| 333 |
size_t get_frame_sound(size_t iFrame); |
| 334 |
|
| 335 |
bool hit_test(size_t iFrame, const ::layers& oLayers, int iX, int iY, |
| 336 |
uint32_t iFlags, int iTestX, int iTestY) const; |
| 337 |
|
| 338 |
bool set_frame_primary_marker(size_t iFrame, int iX, int iY); |
| 339 |
bool set_frame_secondary_marker(size_t iFrame, int iX, int iY); |
| 340 |
bool get_frame_primary_marker(size_t iFrame, int* pX, int* pY); |
| 341 |
bool get_frame_secondary_marker(size_t iFrame, int* pX, int* pY); |
| 342 |
|
| 343 |
//! Retrieve a custom animation by name and tile size. |
| 344 |
/*! |
| 345 |
@param sName Name of the animation. |
| 346 |
@param iTilesize Tile size of the animation. |
| 347 |
@return A set starting frames for the queried animation. |
| 348 |
*/ |
| 349 |
const animation_start_frames& get_named_animations(std::string_view sName, |
| 350 |
int iTilesize) const; |
| 351 |
|
| 352 |
//! Notified every world tick to allow tracking rate of game time passage. |
| 353 |
void tick(); |
| 354 |
|
| 355 |
private: |
| 356 |
struct frame { |
| 357 |
size_t list_index; ///< First entry in #element_list (pointing to an |
| 358 |
///< element) for this frame. |
| 359 |
size_t next_frame; ///< Number of the next frame. |
| 360 |
unsigned int sound; ///< Sound to play, if non-zero. |
| 361 |
unsigned int flags; ///< Flags of the frame. Bit 0=start of animation. |
| 362 |
|
| 363 |
// Bounding rectangle is with all layers / options enabled - used as a |
| 364 |
// quick test prior to a full pixel perfect test. |
| 365 |
int bounding_left; ///< Left edge of the bounding rectangle of this |
| 366 |
///< frame. |
| 367 |
int bounding_right; ///< Right edge of the bounding rectangle of this |
| 368 |
///< frame. |
| 369 |
int bounding_top; ///< Top edge of the bounding rectangle of this |
| 370 |
///< frame. |
| 371 |
int bounding_bottom; ///< Bottom edge of the bounding rectangle of this |
| 372 |
///< frame. |
| 373 |
|
| 374 |
// Markers are used to know where humanoids are on an frame. The |
| 375 |
// positions are pixels offsets from the centre of the frame's base |
| 376 |
// tile to the centre of the humanoid's feet. |
| 377 |
int primary_marker_x; ///< X position of the first center of a humanoids |
| 378 |
///< feet. |
| 379 |
int primary_marker_y; ///< Y position of the first center of a humanoids |
| 380 |
///< feet. |
| 381 |
int secondary_marker_x; ///< X position of the second center of a |
| 382 |
///< humanoids feet. |
| 383 |
int secondary_marker_y; ///< Y position of the second center of a |
| 384 |
///< humanoids feet. |
| 385 |
}; |
| 386 |
|
| 387 |
struct element { |
| 388 |
size_t sprite; ///< Sprite number of the sprite sheet to display. |
| 389 |
uint32_t flags; ///< Flags of the sprite. |
| 390 |
///< bit 0=flip vertically, bit 1=flip horizontally, |
| 391 |
///< bit 2=draw 50% alpha, bit 3=draw 75% alpha. |
| 392 |
int x; ///< X offset of the sprite. |
| 393 |
int y; ///< Y offset of the sprite. |
| 394 |
uint8_t layer; ///< Layer class [0..max_number_of_layers). |
| 395 |
uint8_t layer_id; ///< Value of the layer class to match. |
| 396 |
|
| 397 |
sprite_sheet* element_sprite_sheet; ///< Sprite sheet to use for this |
| 398 |
///< element. Not owned. |
| 399 |
}; |
| 400 |
|
| 401 |
std::vector<size_t> first_frames; ///< First frame number of an animation. |
| 402 |
std::vector<frame> frames; ///< The loaded frames. |
| 403 |
std::vector<uint16_t> element_list; ///< List of elements for a frame. |
| 404 |
std::vector<element> elements; ///< Sprite Elements. |
| 405 |
std::vector<sprite_sheet> |
| 406 |
custom_sheets; ///< Sprite sheets with custom graphics. |
| 407 |
named_animations_map named_animations; ///< Collected named animations. |
| 408 |
|
| 409 |
sprite_sheet* sheet{nullptr}; ///< Sprite sheet to use. Not owned. |
| 410 |
render_target* canvas{nullptr}; ///< Video surface to use. Not owned. |
| 411 |
|
| 412 |
size_t animation_count{0}; ///< Number of animations. |
| 413 |
size_t frame_count{0}; ///< Number of frames. |
| 414 |
size_t element_list_count{0}; ///< Number of list elements. |
| 415 |
size_t element_count{0}; ///< Number of sprite elements. |
| 416 |
|
| 417 |
//! Number of game ticks, used for global animation timing. |
| 418 |
size_t game_ticks{0}; |
| 419 |
|
| 420 |
//! Compute the bounding box of the frame. |
| 421 |
/*! |
| 422 |
@param oFrame Frame to inspect/set. |
| 423 |
*/ |
| 424 |
void set_bounding_box(frame& oFrame); |
| 425 |
|
| 426 |
//! Load sprite elements from the input. |
| 427 |
/*! |
| 428 |
@param [inout] input Data to read. |
| 429 |
@param pSpriteSheet Sprite sheet to use. |
| 430 |
@param iNumElements Number of elements to read. |
| 431 |
@param [inout] iLoadedElements Number of loaded elements so far. |
| 432 |
@param iElementStart Offset of the first element. |
| 433 |
@param iElementCount Number of elements to load. |
| 434 |
@return Index of the first loaded element in #elements. Negative value |
| 435 |
means failure. |
| 436 |
*/ |
| 437 |
size_t load_elements(memory_reader& input, sprite_sheet* pSpriteSheet, |
| 438 |
size_t iNumElements, size_t& iLoadedElements, |
| 439 |
size_t iElementStart, size_t iElementCount); |
| 440 |
|
| 441 |
//! Construct a list element for every element, and a 0xFFFF at the end. |
| 442 |
/*! |
| 443 |
@param iFirstElement Index of the first element in #elements. |
| 444 |
@param iNumElements Number of elements to add. |
| 445 |
@param [inout] iLoadedListElements Number of created list elements so |
| 446 |
far. |
| 447 |
@param iListStart Offset of the first created list element. |
| 448 |
@param iListCount Expected number of list elements to create. |
| 449 |
@return Index of the list elements, or a negative value to indicate |
| 450 |
failure. |
| 451 |
*/ |
| 452 |
size_t make_list_elements(size_t iFirstElement, size_t iNumElements, |
| 453 |
size_t& iLoadedListElements, size_t iListStart, |
| 454 |
size_t iListCount); |
| 455 |
|
| 456 |
//! Fix the flags of the first frame, and set the next frame of the last |
| 457 |
//! frame back to the first frame. |
| 458 |
/*! |
| 459 |
@param iFirst First frame of the animation, or 0xFFFFFFFFu. |
| 460 |
@param iLength Number of frames in the animation. |
| 461 |
*/ |
| 462 |
void fix_next_frame(uint32_t iFirst, size_t iLength); |
| 463 |
}; |
| 464 |
|
| 465 |
class animation_base : public drawable { |
| 466 |
public: |
| 467 |
animation_base(); |
| 468 |
|
| 469 |
void remove_from_tile(); |
| 470 |
void attach_to_tile(const xy_pair& tile_pos, map_tile* node, int layer); |
| 471 |
|
| 472 |
uint32_t get_flags() const { return flags; } |
| 473 |
const xy_pair& get_pixel_offset() const { return pixel_offset; } |
| 474 |
const xy_pair& get_tile() const { return tile; } |
| 475 |
|
| 476 |
void set_flags(uint32_t iFlags) { flags = iFlags; } |
| 477 |
void set_tile(const xy_pair& tile_pos) { tile = tile_pos; } |
| 478 |
void set_pixel_offset(int x, int y) { |
| 479 |
pixel_offset.x = x; |
| 480 |
pixel_offset.y = y; |
| 481 |
} |
| 482 |
void set_layer(int iLayer, int iId); |
| 483 |
void set_layers_from(const animation_base* pSrc) { layers = pSrc->layers; } |
| 484 |
void set_scale_factor(int s) { scale_factor = s; } |
| 485 |
|
| 486 |
protected: |
| 487 |
//! Tile containing the animation. A negative x or y means it is not active. |
| 488 |
xy_pair tile{-1, -1}; |
| 489 |
|
| 490 |
//! Offset in pixels relative to the center of the animation tile. |
| 491 |
xy_pair pixel_offset{0, 0}; |
| 492 |
|
| 493 |
::layers layers{}; |
| 494 |
|
| 495 |
//! Scale factor for the animation. |
| 496 |
/** |
| 497 |
* For both animation and sprite_render_list scale_factor is currently only |
| 498 |
* implemented for drawing. Support for hit_test should be implemented if |
| 499 |
* a need to hit_test on a scaled sprite_render_list or animation arises. |
| 500 |
* |
| 501 |
* In all cases scale_factor is not persisted, and expected to be set |
| 502 |
* according to the current game options before calling draw. |
| 503 |
*/ |
| 504 |
int scale_factor{1}; |
| 505 |
}; |
| 506 |
|
| 507 |
//! The kind of animation. |
| 508 |
enum class animation_kind { primary_child, secondary_child, normal, morph }; |
| 509 |
|
| 510 |
class animation : public animation_base { |
| 511 |
public: |
| 512 |
animation(); |
| 513 |
|
| 514 |
void set_parent(animation* parent_anim, bool use_primary); |
| 515 |
|
| 516 |
void tick(); |
| 517 |
void draw(render_target* canvas, const xy_pair& draw_pos); |
| 518 |
void draw_morph(render_target* canvas, const xy_pair& draw_pos); |
| 519 |
void draw_child(render_target* canvas, const xy_pair& draw_pos, |
| 520 |
bool use_primary); |
| 521 |
|
| 522 |
void draw_fn(render_target* canvas, const xy_pair& draw_pos) override { |
| 523 |
switch (anim_kind) { |
| 524 |
case animation_kind::normal: |
| 525 |
draw(canvas, draw_pos); |
| 526 |
return; |
| 527 |
case animation_kind::primary_child: |
| 528 |
draw_child(canvas, draw_pos, true); |
| 529 |
return; |
| 530 |
case animation_kind::secondary_child: |
| 531 |
draw_child(canvas, draw_pos, false); |
| 532 |
return; |
| 533 |
case animation_kind::morph: |
| 534 |
draw_morph(canvas, draw_pos); |
| 535 |
return; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
bool hit_test(const xy_pair& draw_pos, const xy_pair& obj_pos); |
| 540 |
bool hit_test_morph(const xy_pair& draw_pos, const xy_pair& obj_pos); |
| 541 |
bool hit_test_child(const xy_pair& draw_pos, const xy_pair& obj_pos); |
| 542 |
|
| 543 |
bool hit_test_fn(const xy_pair& draw_pos, const xy_pair& obj_pos) override { |
| 544 |
switch (anim_kind) { |
| 545 |
case animation_kind::normal: |
| 546 |
return hit_test(draw_pos, obj_pos); |
| 547 |
case animation_kind::primary_child: |
| 548 |
case animation_kind::secondary_child: |
| 549 |
return hit_test_child(draw_pos, obj_pos); |
| 550 |
case animation_kind::morph: |
| 551 |
return hit_test_morph(draw_pos, obj_pos); |
| 552 |
} |
| 553 |
return false; |
| 554 |
} |
| 555 |
|
| 556 |
bool is_multiple_frame_animation_fn() override { |
| 557 |
size_t anim = get_animation(); |
| 558 |
size_t first_frame = get_animation_manager()->get_first_frame(anim); |
| 559 |
size_t next_frame = get_animation_manager()->get_next_frame(first_frame); |
| 560 |
return next_frame != first_frame; |
| 561 |
} |
| 562 |
|
| 563 |
link_list* get_previous() { return prev; } |
| 564 |
size_t get_animation() const { return animation_index; } |
| 565 |
bool get_primary_marker(int* pX, int* pY); |
| 566 |
bool get_secondary_marker(int* pX, int* pY); |
| 567 |
size_t get_frame() const { return frame_index; } |
| 568 |
int get_crop_column() const { return crop_column; } |
| 569 |
|
| 570 |
void set_animation(animation_manager* mgr, size_t anim); |
| 571 |
void set_morph_target(animation* target, int duration = 1); |
| 572 |
void set_frame(size_t new_frame); |
| 573 |
|
| 574 |
void set_speed(int x, int y) { |
| 575 |
speed.x = x; |
| 576 |
speed.y = y; |
| 577 |
} |
| 578 |
void set_crop_column(int column) { crop_column = column; } |
| 579 |
|
| 580 |
void persist(lua_persist_writer* writer) const; |
| 581 |
void depersist(lua_persist_reader* reader); |
| 582 |
|
| 583 |
void set_patient_effect(animation_effect patient_effect); |
| 584 |
void set_animation_kind(animation_kind anim_kind); |
| 585 |
animation_kind get_animation_kind() { return anim_kind; } |
| 586 |
|
| 587 |
animation_manager* get_animation_manager() { return manager; } |
| 588 |
|
| 589 |
private: |
| 590 |
animation_manager* manager{nullptr}; |
| 591 |
animation* morph_target{nullptr}; |
| 592 |
size_t animation_index{}; ///< Animation number. |
| 593 |
size_t frame_index{}; ///< Frame number. |
| 594 |
union { |
| 595 |
xy_pair speed{}; ///< Speed in pixels per tick. |
| 596 |
|
| 597 |
//! Some animations are tied to the primary or secondary marker of another |
| 598 |
//! animation and hence have a parent rather than a speed. |
| 599 |
animation* parent; |
| 600 |
}; |
| 601 |
|
| 602 |
size_t sound_to_play{}; |
| 603 |
int crop_column{}; |
| 604 |
animation_kind anim_kind{animation_kind::normal}; |
| 605 |
animation_effect patient_effect{animation_effect::none}; |
| 606 |
//! Number of game_ticks to offset animation by so they aren't all |
| 607 |
//! running in sync. |
| 608 |
size_t patient_effect_offset; |
| 609 |
}; |
| 610 |
|
| 611 |
class sprite_render_list : public animation_base { |
| 612 |
public: |
| 613 |
void tick(); |
| 614 |
|
| 615 |
void set_sheet(sprite_sheet* new_sheet) { sheet = new_sheet; } |
| 616 |
void set_speed(int x_speed, int y_speed) { |
| 617 |
dx_per_tick = x_speed; |
| 618 |
dy_per_tick = y_speed; |
| 619 |
} |
| 620 |
void set_lifetime(int life_time); |
| 621 |
void set_use_intermediate_buffer(); |
| 622 |
void append_sprite(size_t spr_num, int xpos, int ypos); |
| 623 |
bool is_dead() const { return lifetime == 0; } |
| 624 |
|
| 625 |
void persist(lua_persist_writer* writer) const; |
| 626 |
void depersist(lua_persist_reader* reader); |
| 627 |
|
| 628 |
void draw(render_target* canvas, const xy_pair& draw_pos); |
| 629 |
|
| 630 |
void draw_fn(render_target* canvas, const xy_pair& draw_pos) override { |
| 631 |
draw(canvas, draw_pos); |
| 632 |
} |
| 633 |
|
| 634 |
bool hit_test(const xy_pair& draw_pos, const xy_pair& obj_pos); |
| 635 |
|
| 636 |
bool hit_test_fn(const xy_pair& draw_pos, const xy_pair& obj_pos) override { |
| 637 |
return hit_test(draw_pos, obj_pos); |
| 638 |
} |
| 639 |
|
| 640 |
bool is_multiple_frame_animation_fn() override { return false; } |
| 641 |
|
| 642 |
private: |
| 643 |
struct sprite { |
| 644 |
size_t index; |
| 645 |
int x; |
| 646 |
int y; |
| 647 |
}; |
| 648 |
|
| 649 |
sprite_sheet* sheet{nullptr}; |
| 650 |
std::vector<sprite> sprites{}; |
| 651 |
|
| 652 |
//! Amount to change x per tick |
| 653 |
int dx_per_tick{0}; |
| 654 |
//! Amount to change y per tick |
| 655 |
int dy_per_tick{0}; |
| 656 |
//! Number of ticks until reports as dead (-1 = never dies) |
| 657 |
int lifetime{-1}; |
| 658 |
//! Whether to draw to an intermediate buffer. This is used to preserve text |
| 659 |
//! rendering quality when scaling. Not persisted. |
| 660 |
bool use_intermediate_buffer{false}; |
| 661 |
}; |
| 662 |
|
| 663 |
// Find the appropriate subclass of animation_base for a given |
| 664 |
// userdata object. |
| 665 |
// |
| 666 |
// th_lua_internal.h defines our c++ binding class metatables: |
| 667 |
// metatable: { __index = method_table, ... } |
| 668 |
// method_table: { ... } |
| 669 |
// method_table metatable: { __class_name = class_name } |
| 670 |
inline animation_base* luaT_toanimationbase(lua_State* L, int idx) { |
| 671 |
return luaT_touserdata_base<animation_base, animation, sprite_render_list>( |
| 672 |
L, idx, {"animation", "spriteList"}); |
| 673 |
} |
| 674 |
|
| 675 |
#endif // CORSIX_TH_TH_GFX_H_ |