| 1 |
/* |
| 2 |
Copyright (c) 2010-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 |
#include "config.h" |
| 24 |
|
| 25 |
#include <SDL_stdinc.h> |
| 26 |
#include <ft2build.h> // IWYU pragma: keep |
| 27 |
// IWYU pragma: no_include "freetype/config/ftheader.h" |
| 28 |
#ifdef WITH_TRACY |
| 29 |
#include <tracy/Tracy.hpp> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <algorithm> |
| 33 |
#include <climits> |
| 34 |
#include <cstring> |
| 35 |
#include <exception> |
| 36 |
#include <new> |
| 37 |
|
| 38 |
#include "lua.hpp" |
| 39 |
#include "persist_lua.h" |
| 40 |
#include "th_gfx.h" |
| 41 |
#include "th_gfx_font.h" |
| 42 |
#include "th_gfx_sdl.h" |
| 43 |
#include "th_lua.h" |
| 44 |
#include "th_lua_internal.h" |
| 45 |
#include FT_ERRORS_H |
| 46 |
#include FT_TYPES_H |
| 47 |
|
| 48 |
namespace { |
| 49 |
font* luaT_getfont(lua_State* L) { |
| 50 |
font* p = luaT_touserdata_base<font, bitmap_font, freetype_font>( |
| 51 |
L, 1, {"bitmap_font", "freetype_font"}); |
| 52 |
#ifdef DEBUG |
| 53 |
if (p != nullptr && dynamic_cast<bitmap_font*>(p) == nullptr && |
| 54 |
dynamic_cast<freetype_font*>(p) == nullptr) { |
| 55 |
std::fprintf(stderr, "font found which is not freetype or bitmap: %p\n", p); |
| 56 |
} |
| 57 |
#endif |
| 58 |
return p; |
| 59 |
} |
| 60 |
|
| 61 |
int l_palette_new(lua_State* L) { |
| 62 |
size_t iDataLen; |
| 63 |
const uint8_t* pData = luaT_checkfile(L, 2, &iDataLen); |
| 64 |
bool pal8 = lua_toboolean(L, 3); |
| 65 |
|
| 66 |
try { |
| 67 |
luaT_stdnew<palette>(L, luaT_environindex, false, pData, iDataLen, pal8); |
| 68 |
} catch (const std::exception& ex) { |
| 69 |
lua_pushstring(L, ex.what()); |
| 70 |
lua_error(L); |
| 71 |
} |
| 72 |
return 1; |
| 73 |
} |
| 74 |
|
| 75 |
int l_palette_set_entry(lua_State* L) { |
| 76 |
palette* pPalette = luaT_testuserdata<palette>(L); |
| 77 |
lua_pushboolean( |
| 78 |
L, pPalette->set_entry(static_cast<int>(luaL_checkinteger(L, 2)), |
| 79 |
static_cast<uint8_t>(luaL_checkinteger(L, 3)), |
| 80 |
static_cast<uint8_t>(luaL_checkinteger(L, 4)), |
| 81 |
static_cast<uint8_t>(luaL_checkinteger(L, 5))) |
| 82 |
? 1 |
| 83 |
: 0); |
| 84 |
return 1; |
| 85 |
} |
| 86 |
|
| 87 |
int l_rawbitmap_new(lua_State* L) { |
| 88 |
luaT_stdnew<raw_bitmap>(L, luaT_environindex, true); |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
int l_rawbitmap_set_pal(lua_State* L) { |
| 93 |
raw_bitmap* pBitmap = luaT_testuserdata<raw_bitmap>(L); |
| 94 |
palette* pPalette = luaT_testuserdata<palette>(L, 2); |
| 95 |
lua_settop(L, 2); |
| 96 |
|
| 97 |
pBitmap->set_palette(pPalette); |
| 98 |
luaT_setenvfield(L, 1, "palette"); |
| 99 |
return 1; |
| 100 |
} |
| 101 |
|
| 102 |
int l_rawbitmap_load(lua_State* L) { |
| 103 |
ZoneScoped; |
| 104 |
|
| 105 |
raw_bitmap* pBitmap = luaT_testuserdata<raw_bitmap>(L); |
| 106 |
size_t iDataLen; |
| 107 |
const uint8_t* pData = luaT_checkfile(L, 2, &iDataLen); |
| 108 |
int iWidth = static_cast<int>(luaL_checkinteger(L, 3)); |
| 109 |
render_target* pSurface = |
| 110 |
luaT_testuserdata<render_target>(L, 4, luaT_upvalueindex(1), false); |
| 111 |
|
| 112 |
uint32_t flags = 0; |
| 113 |
if (lua_type(L, 5) == LUA_TTABLE) { |
| 114 |
lua_getfield(L, 5, "flags"); |
| 115 |
flags = static_cast<uint32_t>(luaL_optinteger(L, -1, 0)); |
| 116 |
lua_pop(L, 1); |
| 117 |
} |
| 118 |
|
| 119 |
try { |
| 120 |
pBitmap->load_from_th_file(pData, iDataLen, iWidth, pSurface, flags); |
| 121 |
} catch (const std::exception& ex) { |
| 122 |
lua_pushstring(L, ex.what()); |
| 123 |
lua_error(L); |
| 124 |
return 1; |
| 125 |
} |
| 126 |
|
| 127 |
lua_pushboolean(L, 1); |
| 128 |
return 1; |
| 129 |
} |
| 130 |
|
| 131 |
int l_rawbitmap_draw(lua_State* L) { |
| 132 |
ZoneScoped; |
| 133 |
|
| 134 |
raw_bitmap* pBitmap = luaT_testuserdata<raw_bitmap>(L); |
| 135 |
render_target* pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 136 |
|
| 137 |
if (lua_gettop(L) >= 8) { |
| 138 |
pBitmap->draw(pCanvas, static_cast<int>(luaL_checkinteger(L, 3)), |
| 139 |
static_cast<int>(luaL_checkinteger(L, 4)), |
| 140 |
static_cast<int>(luaL_checkinteger(L, 5)), |
| 141 |
static_cast<int>(luaL_checkinteger(L, 6)), |
| 142 |
static_cast<int>(luaL_checkinteger(L, 7)), |
| 143 |
static_cast<int>(luaL_checkinteger(L, 8))); |
| 144 |
} else |
| 145 |
pBitmap->draw(pCanvas, static_cast<int>(luaL_optinteger(L, 3, 0)), |
| 146 |
static_cast<int>(luaL_optinteger(L, 4, 0))); |
| 147 |
|
| 148 |
lua_settop(L, 1); |
| 149 |
return 1; |
| 150 |
} |
| 151 |
|
| 152 |
int l_spritesheet_new(lua_State* L) { |
| 153 |
luaT_stdnew<sprite_sheet>(L, luaT_environindex, true); |
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
int l_spritesheet_set_pal(lua_State* L) { |
| 158 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L); |
| 159 |
palette* pPalette = luaT_testuserdata<palette>(L, 2); |
| 160 |
lua_settop(L, 2); |
| 161 |
|
| 162 |
pSheet->set_palette(pPalette); |
| 163 |
luaT_setenvfield(L, 1, "palette"); |
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
int l_spritesheet_load(lua_State* L) { |
| 168 |
ZoneScoped; |
| 169 |
|
| 170 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L); |
| 171 |
size_t iDataLenTable, iDataLenChunk; |
| 172 |
const uint8_t* pDataTable = luaT_checkfile(L, 2, &iDataLenTable); |
| 173 |
const uint8_t* pDataChunk = luaT_checkfile(L, 3, &iDataLenChunk); |
| 174 |
bool bComplex = lua_toboolean(L, 4) != 0; |
| 175 |
render_target* pSurface = |
| 176 |
luaT_testuserdata<render_target>(L, 5, luaT_upvalueindex(1), false); |
| 177 |
|
| 178 |
if (pSheet->load_from_th_file(pDataTable, iDataLenTable, pDataChunk, |
| 179 |
iDataLenChunk, bComplex, pSurface)) |
| 180 |
lua_pushboolean(L, 1); |
| 181 |
else |
| 182 |
lua_pushboolean(L, 0); |
| 183 |
|
| 184 |
return 1; |
| 185 |
} |
| 186 |
|
| 187 |
int l_spritesheet_count(lua_State* L) { |
| 188 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L); |
| 189 |
|
| 190 |
lua_pushinteger(L, pSheet->get_sprite_count()); |
| 191 |
return 1; |
| 192 |
} |
| 193 |
|
| 194 |
int l_spritesheet_size(lua_State* L) { |
| 195 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L); |
| 196 |
lua_Integer iSprite = luaL_checkinteger(L, 2); // No array adjustment |
| 197 |
if (iSprite < 0 || |
| 198 |
iSprite >= static_cast<lua_Integer>(pSheet->get_sprite_count())) |
| 199 |
return luaL_argerror(L, 2, "Sprite index out of bounds"); |
| 200 |
|
| 201 |
int iWidth; |
| 202 |
int iHeight; |
| 203 |
pSheet->get_sprite_size_unchecked(static_cast<size_t>(iSprite), &iWidth, |
| 204 |
&iHeight); |
| 205 |
|
| 206 |
lua_pushinteger(L, iWidth); |
| 207 |
lua_pushinteger(L, iHeight); |
| 208 |
return 2; |
| 209 |
} |
| 210 |
|
| 211 |
int l_spritesheet_draw(lua_State* L) { |
| 212 |
ZoneScoped; |
| 213 |
|
| 214 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L); |
| 215 |
render_target* pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 216 |
int iSprite = |
| 217 |
static_cast<int>(luaL_checkinteger(L, 3)); // No array adjustment |
| 218 |
|
| 219 |
int x = static_cast<int>(luaL_optinteger(L, 4, 0)); |
| 220 |
int y = static_cast<int>(luaL_optinteger(L, 5, 0)); |
| 221 |
|
| 222 |
int scaleFactor = 1; |
| 223 |
uint32_t flags = 0; |
| 224 |
int arg6type = lua_type(L, 6); |
| 225 |
if (arg6type == LUA_TTABLE) { |
| 226 |
lua_getfield(L, 6, "flags"); |
| 227 |
flags = static_cast<uint32_t>(luaL_optinteger(L, -1, 0)); |
| 228 |
lua_pop(L, 1); |
| 229 |
|
| 230 |
lua_getfield(L, 6, "scaleFactor"); |
| 231 |
scaleFactor = static_cast<int>(luaL_optinteger(L, -1, 1)); |
| 232 |
lua_pop(L, 1); |
| 233 |
} else if (arg6type != LUA_TNONE) { |
| 234 |
luaL_error(L, "Expected table for draw options"); |
| 235 |
} |
| 236 |
|
| 237 |
pSheet->draw_sprite(pCanvas, iSprite, x, y, flags, 0, animation_effect::none, |
| 238 |
scaleFactor); |
| 239 |
|
| 240 |
lua_settop(L, 1); |
| 241 |
return 1; |
| 242 |
} |
| 243 |
|
| 244 |
int l_spritesheet_hittest(lua_State* L) { |
| 245 |
ZoneScoped; |
| 246 |
|
| 247 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L); |
| 248 |
size_t iSprite = luaL_checkinteger(L, 2); |
| 249 |
int iX = static_cast<int>(luaL_checkinteger(L, 3)); |
| 250 |
int iY = static_cast<int>(luaL_checkinteger(L, 4)); |
| 251 |
uint32_t iFlags = static_cast<uint32_t>(luaL_optinteger(L, 5, 0)); |
| 252 |
return pSheet->hit_test_sprite(iSprite, iX, iY, iFlags); |
| 253 |
} |
| 254 |
|
| 255 |
int l_spritesheet_isvisible(lua_State* L) { |
| 256 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L); |
| 257 |
size_t iSprite = luaL_checkinteger(L, 2); |
| 258 |
lua_pushboolean(L, pSheet->is_sprite_visible(iSprite) ? 1 : 0); |
| 259 |
return 1; |
| 260 |
} |
| 261 |
|
| 262 |
int l_font_new(lua_State* L) { |
| 263 |
return luaL_error(L, "Cannot instantiate an interface"); |
| 264 |
} |
| 265 |
|
| 266 |
int l_bitmap_font_new(lua_State* L) { |
| 267 |
luaT_stdnew<bitmap_font>(L, luaT_environindex, true); |
| 268 |
return 1; |
| 269 |
} |
| 270 |
|
| 271 |
int l_bitmap_font_set_spritesheet(lua_State* L) { |
| 272 |
// Note: l_freetype_font_set_spritesheet and l_bitmap_font_set_spritesheet |
| 273 |
// differ in their expected arguments. |
| 274 |
|
| 275 |
bitmap_font* pFont = luaT_testuserdata<bitmap_font>(L); |
| 276 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L, 2); |
| 277 |
int charset_idx = static_cast<int>(luaL_optinteger(L, 3, 1)); |
| 278 |
|
| 279 |
bitmap_font_character_set charset; |
| 280 |
switch (charset_idx) { |
| 281 |
case 1: |
| 282 |
charset = bitmap_font_character_set::cp437; |
| 283 |
break; |
| 284 |
case 2: |
| 285 |
charset = bitmap_font_character_set::mik; |
| 286 |
break; |
| 287 |
default: |
| 288 |
return luaL_argerror(L, 3, "Invalid character set"); |
| 289 |
} |
| 290 |
|
| 291 |
lua_settop(L, 2); |
| 292 |
|
| 293 |
pFont->set_sprite_sheet(pSheet, charset); |
| 294 |
luaT_setenvfield(L, 1, "sprites"); |
| 295 |
return 1; |
| 296 |
} |
| 297 |
|
| 298 |
int l_bitmap_font_get_spritesheet(lua_State* L) { |
| 299 |
luaT_testuserdata<bitmap_font>(L); |
| 300 |
luaT_getenvfield(L, 1, "sprites"); |
| 301 |
return 1; |
| 302 |
} |
| 303 |
|
| 304 |
int l_bitmap_font_set_sep(lua_State* L) { |
| 305 |
bitmap_font* pFont = luaT_testuserdata<bitmap_font>(L); |
| 306 |
|
| 307 |
pFont->set_separation(static_cast<int>(luaL_checkinteger(L, 2)), |
| 308 |
static_cast<int>(luaL_optinteger(L, 3, 0))); |
| 309 |
|
| 310 |
lua_settop(L, 1); |
| 311 |
return 1; |
| 312 |
} |
| 313 |
|
| 314 |
int l_bitmap_font_set_scale(lua_State* L) { |
| 315 |
bitmap_font* pFont = luaT_testuserdata<bitmap_font>(L); |
| 316 |
pFont->set_scale_factor(static_cast<int>(luaL_checkinteger(L, 2))); |
| 317 |
lua_settop(L, 1); |
| 318 |
return 1; |
| 319 |
} |
| 320 |
|
| 321 |
void l_freetype_throw_error_code(lua_State* L, FT_Error e) { |
| 322 |
if (e != FT_Err_Ok) { |
| 323 |
switch (e) { |
| 324 |
#undef __FTERRORS_H__ |
| 325 |
#define FT_ERRORDEF(e, v, s) \ |
| 326 |
case e: \ |
| 327 |
lua_pushliteral(L, s); \ |
| 328 |
break; |
| 329 |
#define FT_ERROR_START_LIST |
| 330 |
#define FT_ERROR_END_LIST |
| 331 |
#include FT_ERRORS_H |
| 332 |
default: |
| 333 |
lua_pushliteral(L, "Unrecognised FreeType2 error"); |
| 334 |
break; |
| 335 |
}; |
| 336 |
lua_error(L); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
int l_freetype_font_new(lua_State* L) { |
| 341 |
freetype_font* pFont = luaT_stdnew<freetype_font>(L, luaT_environindex, true); |
| 342 |
l_freetype_throw_error_code(L, pFont->initialise()); |
| 343 |
return 1; |
| 344 |
} |
| 345 |
|
| 346 |
//! Read the color from the top of the Lua stack. |
| 347 |
//! The color may be either a number representing an ARGB color, or a table |
| 348 |
//! with fields "red", "green", "blue" and "alpha". If alpha is omitted it |
| 349 |
//! defaults to 255 (fully opaque). All other color components default to 0 |
| 350 |
//! If neither a number nor a table is present, the color is left unchanged. |
| 351 |
argb_colour read_color_from_lua(lua_State* L, argb_colour defaultColor) { |
| 352 |
argb_colour color = defaultColor; |
| 353 |
if (lua_istable(L, -1)) { |
| 354 |
lua_getfield(L, -1, "red"); |
| 355 |
lua_getfield(L, -2, "green"); |
| 356 |
lua_getfield(L, -3, "blue"); |
| 357 |
lua_getfield(L, -4, "alpha"); |
| 358 |
|
| 359 |
int red = static_cast<int>(luaL_optinteger(L, -4, 0)); |
| 360 |
int green = static_cast<int>(luaL_optinteger(L, -3, 0)); |
| 361 |
int blue = static_cast<int>(luaL_optinteger(L, -2, 0)); |
| 362 |
int alpha = static_cast<int>(luaL_optinteger(L, -1, 255)); |
| 363 |
color = palette::pack_argb(alpha, red, green, blue); |
| 364 |
lua_pop(L, 4); |
| 365 |
} else if (lua_isnumber(L, -1)) { |
| 366 |
color = static_cast<argb_colour>(lua_tointeger(L, -1)); |
| 367 |
if ((color & 0xFF000000u) == 0) { |
| 368 |
// If no alpha was specified, assume fully opaque. |
| 369 |
color |= 0xFF000000u; |
| 370 |
} |
| 371 |
} |
| 372 |
return color; |
| 373 |
} |
| 374 |
|
| 375 |
//! Set the font options for a freetype font. |
| 376 |
//! |
| 377 |
//! The lua stack contains three arguments: |
| 378 |
//! 1. The freetype_font userdata |
| 379 |
//! 2. A sprite_sheet userdata used to find a default size and colour |
| 380 |
//! 3. A table of options, which may contain: |
| 381 |
//! <dl> |
| 382 |
//! <dt>ttf_color</dt> |
| 383 |
//! <dd>Either a number representing an ARGB colour, or a table |
| 384 |
//! with fields "red", "green", "blue" and "alpha". If alpha is omitted |
| 385 |
//! it defaults to 255 (fully opaque). All other color components default |
| 386 |
//! to 0.</dd> |
| 387 |
//! <dt>ttf_width</dt> |
| 388 |
//! <dd>Desired nominal character width, in pixels. If omitted, the width is |
| 389 |
//! calculated from the sprite sheet.</dd> |
| 390 |
//! <dt>ttf_height</dt> |
| 391 |
//! <dd>Desired nominal character height, in pixels. If omitted, the height |
| 392 |
//! is calculated from the sprite sheet.</dd> |
| 393 |
//! <dt>ttf_shadow</dt> |
| 394 |
//! <dd>A table with fields "offset_x", "offset_y" and "color". The color |
| 395 |
//! field has the same format as ttf_color. If this field is present, the |
| 396 |
//! font will be drawn with a shadow of the specified colour and offset. |
| 397 |
//! color is optional and defaults to opaque black. offset_x and offset_y |
| 398 |
//! are optional and default to 1. If the field is omitted entirely, no |
| 399 |
//! shadow is drawn. Alternately ttf_shadow can be set to true to draw |
| 400 |
//! a shadow with default parameters, or false to disable any shadow. |
| 401 |
//! </dd> |
| 402 |
//! </dl> |
| 403 |
//! @param L Lua stack |
| 404 |
//! @return argument count |
| 405 |
int l_freetype_font_set_font_options(lua_State* L) { |
| 406 |
ZoneScoped; |
| 407 |
|
| 408 |
// Colour 0 falls back to using the average colour of the sprite sheet. |
| 409 |
int width = 0; |
| 410 |
int height = 0; |
| 411 |
argb_colour color = 0; |
| 412 |
|
| 413 |
freetype_font* pFont = luaT_testuserdata<freetype_font>(L); |
| 414 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L, 2); |
| 415 |
|
| 416 |
pFont->match_bitmap_font(pSheet, &color, &width, &height); |
| 417 |
|
| 418 |
// Optional RGB colour parameters |
| 419 |
lua_getfield(L, 3, "ttf_color"); |
| 420 |
color = read_color_from_lua(L, color); |
| 421 |
lua_pop(L, 1); |
| 422 |
|
| 423 |
lua_getfield(L, 3, "ttf_width"); |
| 424 |
if (lua_isnumber(L, -1)) { |
| 425 |
width = static_cast<int>(lua_tointeger(L, -1)); |
| 426 |
} |
| 427 |
lua_pop(L, 1); |
| 428 |
|
| 429 |
lua_getfield(L, 3, "ttf_height"); |
| 430 |
if (lua_isnumber(L, -1)) { |
| 431 |
height = static_cast<int>(lua_tointeger(L, -1)); |
| 432 |
} |
| 433 |
lua_pop(L, 1); |
| 434 |
|
| 435 |
font_shadow_options shadowOptions; |
| 436 |
lua_getfield(L, 3, "ttf_shadow"); |
| 437 |
if (lua_istable(L, -1)) { |
| 438 |
shadowOptions.enabled = true; |
| 439 |
|
| 440 |
lua_getfield(L, -1, "offset_x"); |
| 441 |
shadowOptions.offset_x = static_cast<int>(luaL_optinteger(L, -1, 1)); |
| 442 |
lua_pop(L, 1); |
| 443 |
|
| 444 |
lua_getfield(L, -1, "offset_y"); |
| 445 |
shadowOptions.offset_y = static_cast<int>(luaL_optinteger(L, -1, 1)); |
| 446 |
lua_pop(L, 1); |
| 447 |
|
| 448 |
lua_getfield(L, -1, "color"); |
| 449 |
shadowOptions.color = read_color_from_lua(L, shadowOptions.color); |
| 450 |
lua_pop(L, 1); |
| 451 |
} else if (lua_isboolean(L, -1)) { |
| 452 |
shadowOptions.enabled = lua_toboolean(L, -1); |
| 453 |
} |
| 454 |
lua_pop(L, 1); |
| 455 |
|
| 456 |
lua_getfield(L, -1, "scale_factor"); |
| 457 |
int scale_factor = static_cast<int>(luaL_optinteger(L, -1, 1)); |
| 458 |
width *= scale_factor; |
| 459 |
height *= scale_factor; |
| 460 |
shadowOptions.offset_x *= scale_factor; |
| 461 |
shadowOptions.offset_y *= scale_factor; |
| 462 |
lua_pop(L, 1); |
| 463 |
|
| 464 |
pFont->set_font_color(color); |
| 465 |
pFont->set_shadow_options(shadowOptions); |
| 466 |
l_freetype_throw_error_code(L, |
| 467 |
pFont->set_ideal_character_size(width, height)); |
| 468 |
lua_settop(L, 1); |
| 469 |
return 1; |
| 470 |
} |
| 471 |
|
| 472 |
int l_freetype_font_get_copyright(lua_State* L) { |
| 473 |
lua_pushstring(L, freetype_font::get_copyright_notice()); |
| 474 |
return 1; |
| 475 |
} |
| 476 |
|
| 477 |
int l_freetype_font_set_face(lua_State* L) { |
| 478 |
freetype_font* pFont = luaT_testuserdata<freetype_font>(L); |
| 479 |
size_t iLength; |
| 480 |
const uint8_t* pData = luaT_checkfile(L, 2, &iLength); |
| 481 |
lua_settop(L, 2); |
| 482 |
|
| 483 |
l_freetype_throw_error_code(L, pFont->set_face(pData, iLength)); |
| 484 |
luaT_setenvfield(L, 1, "face"); |
| 485 |
return 1; |
| 486 |
} |
| 487 |
|
| 488 |
int l_freetype_font_clear_cache(lua_State* L) { |
| 489 |
freetype_font* pFont = luaT_testuserdata<freetype_font>(L); |
| 490 |
pFont->clear_cache(); |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
int l_font_get_size(lua_State* L) { |
| 495 |
font* pFont = luaT_getfont(L); |
| 496 |
size_t iMsgLen; |
| 497 |
const char* sMsg = luaT_checkstring(L, 2, &iMsgLen); |
| 498 |
|
| 499 |
ZoneScoped; |
| 500 |
|
| 501 |
int iMaxWidth = INT_MAX; |
| 502 |
if (!lua_isnoneornil(L, 3)) |
| 503 |
iMaxWidth = static_cast<int>(luaL_checkinteger(L, 3)); |
| 504 |
|
| 505 |
text_layout oDrawArea = pFont->get_text_dimensions(sMsg, iMsgLen, iMaxWidth); |
| 506 |
|
| 507 |
lua_pushinteger(L, oDrawArea.end_x); |
| 508 |
lua_pushinteger(L, oDrawArea.end_y); |
| 509 |
lua_pushinteger(L, oDrawArea.row_count); |
| 510 |
|
| 511 |
return 3; |
| 512 |
} |
| 513 |
|
| 514 |
int l_font_draw(lua_State* L) { |
| 515 |
ZoneScoped; |
| 516 |
|
| 517 |
font* pFont = luaT_getfont(L); |
| 518 |
render_target* pCanvas = nullptr; |
| 519 |
if (!lua_isnoneornil(L, 2)) { |
| 520 |
pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 521 |
} |
| 522 |
size_t iMsgLen; |
| 523 |
const char* sMsg = luaT_checkstring(L, 3, &iMsgLen); |
| 524 |
int iX = static_cast<int>(luaL_checkinteger(L, 4)); |
| 525 |
int iY = static_cast<int>(luaL_checkinteger(L, 5)); |
| 526 |
|
| 527 |
text_alignment eAlign = text_alignment::center; |
| 528 |
if (!lua_isnoneornil(L, 8)) { |
| 529 |
const char* sAlign = luaL_checkstring(L, 8); |
| 530 |
if (std::strcmp(sAlign, "right") == 0) { |
| 531 |
eAlign = text_alignment::right; |
| 532 |
} else if (std::strcmp(sAlign, "left") == 0) { |
| 533 |
eAlign = text_alignment::left; |
| 534 |
} else if (std::strcmp(sAlign, "center") == 0 || |
| 535 |
std::strcmp(sAlign, "centre") == 0 || |
| 536 |
std::strcmp(sAlign, "middle") == 0) { |
| 537 |
eAlign = text_alignment::center; |
| 538 |
} else { |
| 539 |
return luaL_error(L, "Invalid alignment: \"%s\"", sAlign); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
text_layout oDrawArea = pFont->get_text_dimensions(sMsg, iMsgLen); |
| 544 |
if (!lua_isnoneornil(L, 7)) { |
| 545 |
int iW = static_cast<int>(luaL_checkinteger(L, 6)); |
| 546 |
int iH = static_cast<int>(luaL_checkinteger(L, 7)); |
| 547 |
if (iW > oDrawArea.end_x && eAlign != text_alignment::left) { |
| 548 |
iX += |
| 549 |
(iW - oDrawArea.end_x) / ((eAlign == text_alignment::center) ? 2 : 1); |
| 550 |
} |
| 551 |
if (iH > oDrawArea.end_y) { |
| 552 |
iY += (iH - oDrawArea.end_y) / 2; |
| 553 |
} |
| 554 |
} |
| 555 |
if (pCanvas != nullptr) { |
| 556 |
pFont->draw_text(pCanvas, sMsg, iMsgLen, iX, iY); |
| 557 |
} |
| 558 |
lua_pushinteger(L, iY + oDrawArea.end_y); |
| 559 |
lua_pushinteger(L, iX + oDrawArea.end_x); |
| 560 |
|
| 561 |
return 2; |
| 562 |
} |
| 563 |
|
| 564 |
int l_font_draw_wrapped(lua_State* L) { |
| 565 |
ZoneScoped; |
| 566 |
|
| 567 |
font* pFont = luaT_getfont(L); |
| 568 |
render_target* pCanvas = nullptr; |
| 569 |
if (!lua_isnoneornil(L, 2)) { |
| 570 |
pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 571 |
} |
| 572 |
size_t iMsgLen; |
| 573 |
const char* sMsg = luaT_checkstring(L, 3, &iMsgLen); |
| 574 |
int iX = static_cast<int>(luaL_checkinteger(L, 4)); |
| 575 |
int iY = static_cast<int>(luaL_checkinteger(L, 5)); |
| 576 |
int iW = static_cast<int>(luaL_checkinteger(L, 6)); |
| 577 |
|
| 578 |
text_alignment eAlign = text_alignment::left; |
| 579 |
if (!lua_isnoneornil(L, 7)) { |
| 580 |
const char* sAlign = luaL_checkstring(L, 7); |
| 581 |
if (std::strcmp(sAlign, "right") == 0) { |
| 582 |
eAlign = text_alignment::right; |
| 583 |
} else if (std::strcmp(sAlign, "left") == 0) { |
| 584 |
eAlign = text_alignment::left; |
| 585 |
} else if (std::strcmp(sAlign, "center") == 0 || |
| 586 |
std::strcmp(sAlign, "centre") == 0 || |
| 587 |
std::strcmp(sAlign, "middle") == 0) { |
| 588 |
eAlign = text_alignment::center; |
| 589 |
} else { |
| 590 |
return luaL_error(L, "Invalid alignment: \"%s\"", sAlign); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
int iMaxRows = INT_MAX; |
| 595 |
if (!lua_isnoneornil(L, 8)) { |
| 596 |
iMaxRows = static_cast<int>(luaL_checkinteger(L, 8)); |
| 597 |
} |
| 598 |
|
| 599 |
int iSkipRows = 0; |
| 600 |
if (!lua_isnoneornil(L, 9)) { |
| 601 |
iSkipRows = static_cast<int>(luaL_checkinteger(L, 9)); |
| 602 |
} |
| 603 |
|
| 604 |
text_layout oDrawArea = pFont->draw_text_wrapped( |
| 605 |
pCanvas, sMsg, iMsgLen, iX, iY, iW, iMaxRows, iSkipRows, eAlign); |
| 606 |
lua_pushinteger(L, oDrawArea.end_y); |
| 607 |
lua_pushinteger(L, oDrawArea.end_x); |
| 608 |
lua_pushinteger(L, oDrawArea.row_count); |
| 609 |
|
| 610 |
return 3; |
| 611 |
} |
| 612 |
|
| 613 |
int l_font_draw_tooltip(lua_State* L) { |
| 614 |
ZoneScoped; |
| 615 |
|
| 616 |
font* pFont = luaT_getfont(L); |
| 617 |
render_target* pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 618 |
size_t iMsgLen; |
| 619 |
const char* sMsg = luaT_checkstring(L, 3, &iMsgLen); |
| 620 |
int iX = static_cast<int>(luaL_checkinteger(L, 4)); |
| 621 |
int iY = static_cast<int>(luaL_checkinteger(L, 5)); |
| 622 |
int iW = static_cast<int>(luaL_optinteger(L, 6, 200)); |
| 623 |
int iScreenWidth = pCanvas->get_width(); |
| 624 |
|
| 625 |
uint32_t iBlack = render_target::map_colour(0x00, 0x00, 0x00); |
| 626 |
uint32_t iWhite = render_target::map_colour(0xFF, 0xFF, 0xFF); |
| 627 |
text_layout oArea = pFont->draw_text_wrapped(nullptr, sMsg, iMsgLen, iX + 2, |
| 628 |
iY + 1, iW - 4, INT_MAX, 0); |
| 629 |
int iLastX = iX + oArea.width + 3; |
| 630 |
int iFirstY = iY - (oArea.end_y - iY) - 1; |
| 631 |
|
| 632 |
int iXOffset = iLastX > iScreenWidth ? iScreenWidth - iLastX : 0; |
| 633 |
int iYOffset = iFirstY < 0 ? -iFirstY : 0; |
| 634 |
|
| 635 |
pCanvas->fill_rect(iBlack, iX + iXOffset, iFirstY + iYOffset, oArea.width + 3, |
| 636 |
oArea.end_y - iY + 2); |
| 637 |
pCanvas->fill_rect(iWhite, iX + iXOffset + 1, iFirstY + 1 + iYOffset, |
| 638 |
oArea.width + 1, oArea.end_y - iY); |
| 639 |
|
| 640 |
pFont->draw_text_wrapped(pCanvas, sMsg, iMsgLen, iX + 2 + iXOffset, |
| 641 |
iFirstY + 1 + iYOffset, iW - 4); |
| 642 |
|
| 643 |
lua_pushinteger(L, oArea.end_y); |
| 644 |
|
| 645 |
return 1; |
| 646 |
} |
| 647 |
|
| 648 |
int l_font_is_bitmap(lua_State* L) { |
| 649 |
font* pFont = luaT_getfont(L); |
| 650 |
lua_pushboolean(L, dynamic_cast<bitmap_font*>(pFont) != nullptr ? 1 : 0); |
| 651 |
return 1; |
| 652 |
} |
| 653 |
|
| 654 |
int l_layers_new(lua_State* L) { |
| 655 |
layers* pLayers = luaT_stdnew<layers>(L, luaT_environindex, false); |
| 656 |
for (int i = 0; i < max_number_of_layers; ++i) pLayers->layer_contents[i] = 0; |
| 657 |
return 1; |
| 658 |
} |
| 659 |
|
| 660 |
int l_layers_get(lua_State* L) { |
| 661 |
layers* pLayers = luaT_testuserdata<layers>(L); |
| 662 |
lua_Integer iLayer = luaL_checkinteger(L, 2); |
| 663 |
if (0 <= iLayer && iLayer < max_number_of_layers) |
| 664 |
lua_pushinteger(L, pLayers->layer_contents[iLayer]); |
| 665 |
else |
| 666 |
lua_pushnil(L); |
| 667 |
return 1; |
| 668 |
} |
| 669 |
|
| 670 |
int l_layers_set(lua_State* L) { |
| 671 |
layers* pLayers = luaT_testuserdata<layers>(L); |
| 672 |
lua_Integer iLayer = luaL_checkinteger(L, 2); |
| 673 |
uint8_t iValue = static_cast<uint8_t>(luaL_checkinteger(L, 3)); |
| 674 |
if (0 <= iLayer && iLayer < max_number_of_layers) { |
| 675 |
pLayers->layer_contents[iLayer] = iValue; |
| 676 |
} |
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
int l_layers_persist(lua_State* L) { |
| 681 |
layers* pLayers = luaT_testuserdata<layers>(L); |
| 682 |
lua_persist_writer* pWriter = (lua_persist_writer*)lua_touserdata(L, 2); |
| 683 |
|
| 684 |
int iNumLayers = max_number_of_layers; |
| 685 |
for (; iNumLayers >= 1; --iNumLayers) { |
| 686 |
if (pLayers->layer_contents[iNumLayers - 1] != 0) break; |
| 687 |
} |
| 688 |
pWriter->write_uint(iNumLayers); |
| 689 |
pWriter->write_byte_stream(pLayers->layer_contents, iNumLayers); |
| 690 |
return 0; |
| 691 |
} |
| 692 |
|
| 693 |
int l_layers_depersist(lua_State* L) { |
| 694 |
void* layers_ud = luaT_testuserdata<layers>(L); |
| 695 |
lua_settop(L, 2); |
| 696 |
lua_insert(L, 1); |
| 697 |
lua_persist_reader* pReader = |
| 698 |
static_cast<lua_persist_reader*>(lua_touserdata(L, 1)); |
| 699 |
|
| 700 |
layers* ls = new (layers_ud) layers(); |
| 701 |
int iNumLayers; |
| 702 |
if (!pReader->read_uint(iNumLayers)) return 0; |
| 703 |
if (iNumLayers > max_number_of_layers) { |
| 704 |
if (!pReader->read_byte_stream(ls->layer_contents, max_number_of_layers)) { |
| 705 |
return 0; |
| 706 |
} |
| 707 |
if (!pReader->read_byte_stream(nullptr, |
| 708 |
iNumLayers - max_number_of_layers)) { |
| 709 |
return 0; |
| 710 |
} |
| 711 |
} else { |
| 712 |
if (!pReader->read_byte_stream(ls->layer_contents, iNumLayers)) return 0; |
| 713 |
} |
| 714 |
return 0; |
| 715 |
} |
| 716 |
|
| 717 |
int l_cursor_new(lua_State* L) { |
| 718 |
luaT_stdnew<cursor>(L, luaT_environindex, false); |
| 719 |
return 1; |
| 720 |
} |
| 721 |
|
| 722 |
int l_cursor_load(lua_State* L) { |
| 723 |
ZoneScoped; |
| 724 |
|
| 725 |
cursor* pCursor = luaT_testuserdata<cursor>(L); |
| 726 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L, 2); |
| 727 |
if (pCursor->create_from_sprite(pSheet, |
| 728 |
static_cast<int>(luaL_checkinteger(L, 3)), |
| 729 |
static_cast<int>(luaL_optinteger(L, 4, 0)), |
| 730 |
static_cast<int>(luaL_optinteger(L, 5, 0)))) { |
| 731 |
lua_settop(L, 1); |
| 732 |
return 1; |
| 733 |
} else { |
| 734 |
lua_pushboolean(L, 0); |
| 735 |
return 1; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
int l_cursor_use(lua_State* L) { |
| 740 |
cursor* pCursor = luaT_testuserdata<cursor>(L); |
| 741 |
render_target* pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 742 |
pCursor->use(pCanvas); |
| 743 |
return 0; |
| 744 |
} |
| 745 |
|
| 746 |
int l_cursor_position(lua_State* L) { |
| 747 |
render_target* pCanvas = |
| 748 |
luaT_testuserdata<render_target>(L, 1, luaT_upvalueindex(1)); |
| 749 |
lua_pushboolean(L, cursor::set_position( |
| 750 |
pCanvas, static_cast<int>(luaL_checkinteger(L, 2)), |
| 751 |
static_cast<int>(luaL_checkinteger(L, 3))) |
| 752 |
? 1 |
| 753 |
: 0); |
| 754 |
return 1; |
| 755 |
} |
| 756 |
|
| 757 |
/** Construct the helper structure for making a #THRenderTarget. */ |
| 758 |
render_target_creation_params l_surface_creation_params(lua_State* L, |
| 759 |
int iArgStart) { |
| 760 |
render_target_creation_params params; |
| 761 |
params.width = static_cast<int>(luaL_checkinteger(L, iArgStart)); |
| 762 |
params.height = static_cast<int>(luaL_checkinteger(L, iArgStart + 1)); |
| 763 |
params.min_width = static_cast<int>(luaL_checkinteger(L, iArgStart + 2)); |
| 764 |
params.min_height = static_cast<int>(luaL_checkinteger(L, iArgStart + 3)); |
| 765 |
|
| 766 |
params.fullscreen = false; |
| 767 |
params.present_immediate = false; |
| 768 |
params.direct_zoom = false; |
| 769 |
|
| 770 |
// Parse string arguments, looking for matching parameter names. |
| 771 |
for (int iArg = iArgStart + 4, iArgCount = lua_gettop(L); iArg <= iArgCount; |
| 772 |
++iArg) { |
| 773 |
const char* sOption = luaL_checkstring(L, iArg); |
| 774 |
if (sOption[0] == 0) continue; |
| 775 |
|
| 776 |
if (std::strcmp(sOption, "fullscreen") == 0) { |
| 777 |
params.fullscreen = true; |
| 778 |
} |
| 779 |
if (std::strcmp(sOption, "present immediate") == 0) { |
| 780 |
params.present_immediate = true; |
| 781 |
} |
| 782 |
if (std::strcmp(sOption, "direct zoom") == 0) { |
| 783 |
params.direct_zoom = true; |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
return params; |
| 788 |
} |
| 789 |
|
| 790 |
int l_surface_new(lua_State* L) { |
| 791 |
lua_remove(L, 1); // Value inserted by __call |
| 792 |
|
| 793 |
render_target_creation_params params = l_surface_creation_params(L, 1); |
| 794 |
try { |
| 795 |
luaT_stdnew<render_target>(L, luaT_environindex, false, params); |
| 796 |
} catch (const std::exception& ex) { |
| 797 |
return luaL_error(L, "Could not create render target: %s", ex.what()); |
| 798 |
} |
| 799 |
|
| 800 |
return 1; |
| 801 |
} |
| 802 |
|
| 803 |
int l_surface_update(lua_State* L) { |
| 804 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 805 |
render_target_creation_params params = l_surface_creation_params(L, 2); |
| 806 |
if (pCanvas->update(params)) { |
| 807 |
lua_pushnil(L); |
| 808 |
return 1; |
| 809 |
} |
| 810 |
|
| 811 |
lua_pushstring(L, pCanvas->get_last_error()); |
| 812 |
return 1; |
| 813 |
} |
| 814 |
|
| 815 |
int l_surface_fill_black(lua_State* L) { |
| 816 |
ZoneScoped; |
| 817 |
|
| 818 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 819 |
lua_settop(L, 1); |
| 820 |
if (pCanvas->fill_black()) return 1; |
| 821 |
lua_pushnil(L); |
| 822 |
lua_pushstring(L, pCanvas->get_last_error()); |
| 823 |
return 2; |
| 824 |
} |
| 825 |
|
| 826 |
int l_surface_fill_colour(lua_State* L) { |
| 827 |
ZoneScoped; |
| 828 |
|
| 829 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 830 |
uint32_t c = read_color_from_lua(L, 0xFF000000); |
| 831 |
lua_settop(L, 2); |
| 832 |
if (pCanvas->fill_colour(c)) return 1; |
| 833 |
lua_pushnil(L); |
| 834 |
lua_pushstring(L, pCanvas->get_last_error()); |
| 835 |
return 2; |
| 836 |
} |
| 837 |
|
| 838 |
int l_surface_start_frame(lua_State* L) { |
| 839 |
ZoneScoped; |
| 840 |
|
| 841 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 842 |
lua_settop(L, 1); |
| 843 |
if (pCanvas->start_frame()) return 1; |
| 844 |
lua_pushnil(L); |
| 845 |
lua_pushstring(L, pCanvas->get_last_error()); |
| 846 |
return 2; |
| 847 |
} |
| 848 |
|
| 849 |
int l_surface_end_frame(lua_State* L) { |
| 850 |
ZoneScoped; |
| 851 |
|
| 852 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 853 |
lua_settop(L, 1); |
| 854 |
if (pCanvas->end_frame()) return 1; |
| 855 |
lua_pushnil(L); |
| 856 |
lua_pushstring(L, pCanvas->get_last_error()); |
| 857 |
return 2; |
| 858 |
} |
| 859 |
|
| 860 |
int l_surface_nonoverlapping(lua_State* L) { |
| 861 |
ZoneScoped; |
| 862 |
|
| 863 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 864 |
if (lua_isnone(L, 2) || lua_toboolean(L, 2) != 0) |
| 865 |
pCanvas->start_nonoverlapping_draws(); |
| 866 |
else |
| 867 |
pCanvas->finish_nonoverlapping_draws(); |
| 868 |
lua_settop(L, 1); |
| 869 |
return 1; |
| 870 |
} |
| 871 |
|
| 872 |
int l_surface_set_blue_filter_active(lua_State* L) { |
| 873 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 874 |
pCanvas->set_blue_filter_active( |
| 875 |
(lua_isnoneornil(L, 2) != 0) ? false : (lua_toboolean(L, 2) != 0)); |
| 876 |
return 1; |
| 877 |
} |
| 878 |
|
| 879 |
int l_surface_map(lua_State* L) { |
| 880 |
ZoneScoped; |
| 881 |
|
| 882 |
lua_pushnumber( |
| 883 |
L, (lua_Number)render_target::map_colour((Uint8)luaL_checkinteger(L, 2), |
| 884 |
(Uint8)luaL_checkinteger(L, 3), |
| 885 |
(Uint8)luaL_checkinteger(L, 4))); |
| 886 |
return 1; |
| 887 |
} |
| 888 |
|
| 889 |
int l_surface_rect(lua_State* L) { |
| 890 |
ZoneScoped; |
| 891 |
|
| 892 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 893 |
if (pCanvas->fill_rect(static_cast<uint32_t>(luaL_checkinteger(L, 2)), |
| 894 |
static_cast<int>(luaL_checkinteger(L, 3)), |
| 895 |
static_cast<int>(luaL_checkinteger(L, 4)), |
| 896 |
static_cast<int>(luaL_checkinteger(L, 5)), |
| 897 |
static_cast<int>(luaL_checkinteger(L, 6)))) { |
| 898 |
lua_settop(L, 1); |
| 899 |
return 1; |
| 900 |
} |
| 901 |
lua_pushnil(L); |
| 902 |
lua_pushstring(L, pCanvas->get_last_error()); |
| 903 |
return 2; |
| 904 |
} |
| 905 |
|
| 906 |
int l_surface_screenshot(lua_State* L) { |
| 907 |
ZoneScoped; |
| 908 |
|
| 909 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 910 |
const char* file_path = luaL_checkstring(L, 2); |
| 911 |
if (pCanvas->take_screenshot(file_path)) { |
| 912 |
lua_settop(L, 1); |
| 913 |
return 1; |
| 914 |
} |
| 915 |
lua_pushnil(L); |
| 916 |
lua_pushstring(L, pCanvas->get_last_error()); |
| 917 |
return 2; |
| 918 |
} |
| 919 |
|
| 920 |
int l_surface_push_clip(lua_State* L) { |
| 921 |
ZoneScoped; |
| 922 |
|
| 923 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 924 |
clip_rect rcClip; |
| 925 |
rcClip.x = static_cast<clip_rect::x_y_type>(luaL_checkinteger(L, 2)); |
| 926 |
rcClip.y = static_cast<clip_rect::x_y_type>(luaL_checkinteger(L, 3)); |
| 927 |
rcClip.w = static_cast<clip_rect::w_h_type>(luaL_checkinteger(L, 4)); |
| 928 |
rcClip.h = static_cast<clip_rect::w_h_type>(luaL_checkinteger(L, 5)); |
| 929 |
pCanvas->push_clip_rect(&rcClip); |
| 930 |
lua_settop(L, 1); |
| 931 |
return 1; |
| 932 |
} |
| 933 |
|
| 934 |
int l_surface_pop_clip(lua_State* L) { |
| 935 |
ZoneScoped; |
| 936 |
|
| 937 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 938 |
pCanvas->pop_clip_rect(); |
| 939 |
lua_settop(L, 1); |
| 940 |
return 1; |
| 941 |
} |
| 942 |
|
| 943 |
int l_surface_get_width(lua_State* L) { |
| 944 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 945 |
lua_pushinteger(L, pCanvas->get_width()); |
| 946 |
return 1; |
| 947 |
} |
| 948 |
|
| 949 |
int l_surface_get_height(lua_State* L) { |
| 950 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 951 |
lua_pushinteger(L, pCanvas->get_height()); |
| 952 |
return 1; |
| 953 |
} |
| 954 |
|
| 955 |
int l_surface_scale(lua_State* L) { |
| 956 |
ZoneScoped; |
| 957 |
|
| 958 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 959 |
scaled_items eToScale = scaled_items::none; |
| 960 |
if (lua_isnoneornil(L, 3)) { |
| 961 |
eToScale = scaled_items::all; |
| 962 |
} else { |
| 963 |
size_t iLength; |
| 964 |
const char* sOption = lua_tolstring(L, 3, &iLength); |
| 965 |
if (sOption && iLength >= 6 && std::memcmp(sOption, "bitmap", 6) == 0) { |
| 966 |
eToScale = scaled_items::bitmaps; |
| 967 |
} else |
| 968 |
luaL_error(L, "Expected \"bitmap\" as 2nd argument"); |
| 969 |
} |
| 970 |
lua_pushboolean(L, pCanvas->set_scale_factor( |
| 971 |
static_cast<float>(luaL_checknumber(L, 2)), eToScale) |
| 972 |
? 1 |
| 973 |
: 0); |
| 974 |
return 1; |
| 975 |
} |
| 976 |
|
| 977 |
int l_surface_set_caption(lua_State* L) { |
| 978 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 979 |
pCanvas->set_caption(luaL_checkstring(L, 2)); |
| 980 |
|
| 981 |
lua_settop(L, 1); |
| 982 |
return 1; |
| 983 |
} |
| 984 |
|
| 985 |
int l_surface_get_renderer_details(lua_State* L) { |
| 986 |
ZoneScoped; |
| 987 |
|
| 988 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 989 |
lua_pushstring(L, pCanvas->get_renderer_details()); |
| 990 |
return 1; |
| 991 |
} |
| 992 |
|
| 993 |
// Lua to THRenderTarget->setWindowGrab |
| 994 |
int l_surface_set_capture_mouse(lua_State* L) { |
| 995 |
ZoneScoped; |
| 996 |
|
| 997 |
render_target* pCanvas = luaT_testuserdata<render_target>(L); |
| 998 |
pCanvas->set_window_grab( |
| 999 |
(lua_isnoneornil(L, 2) != 0) ? false : (lua_toboolean(L, 2) != 0)); |
| 1000 |
return 0; |
| 1001 |
} |
| 1002 |
|
| 1003 |
int l_line_new(lua_State* L) { |
| 1004 |
luaT_stdnew<line_sequence>(L); |
| 1005 |
return 1; |
| 1006 |
} |
| 1007 |
|
| 1008 |
int l_move_to(lua_State* L) { |
| 1009 |
line_sequence* pLine = luaT_testuserdata<line_sequence>(L); |
| 1010 |
pLine->move_to(luaL_optnumber(L, 2, 0), luaL_optnumber(L, 3, 0)); |
| 1011 |
|
| 1012 |
lua_settop(L, 1); |
| 1013 |
return 1; |
| 1014 |
} |
| 1015 |
|
| 1016 |
int l_line_to(lua_State* L) { |
| 1017 |
line_sequence* pLine = luaT_testuserdata<line_sequence>(L); |
| 1018 |
pLine->line_to(luaL_optnumber(L, 2, 0), luaL_optnumber(L, 3, 0)); |
| 1019 |
|
| 1020 |
lua_settop(L, 1); |
| 1021 |
return 1; |
| 1022 |
} |
| 1023 |
|
| 1024 |
int l_set_width(lua_State* L) { |
| 1025 |
line_sequence* pLine = luaT_testuserdata<line_sequence>(L); |
| 1026 |
pLine->set_width(luaL_optnumber(L, 2, 1)); |
| 1027 |
|
| 1028 |
lua_settop(L, 1); |
| 1029 |
return 1; |
| 1030 |
} |
| 1031 |
|
| 1032 |
int l_set_colour(lua_State* L) { |
| 1033 |
line_sequence* pLine = luaT_testuserdata<line_sequence>(L); |
| 1034 |
pLine->set_colour(static_cast<uint8_t>(luaL_optinteger(L, 2, 0)), |
| 1035 |
static_cast<uint8_t>(luaL_optinteger(L, 3, 0)), |
| 1036 |
static_cast<uint8_t>(luaL_optinteger(L, 4, 0)), |
| 1037 |
static_cast<uint8_t>(luaL_optinteger(L, 5, 255))); |
| 1038 |
|
| 1039 |
lua_settop(L, 1); |
| 1040 |
return 1; |
| 1041 |
} |
| 1042 |
|
| 1043 |
int l_line_draw(lua_State* L) { |
| 1044 |
ZoneScoped; |
| 1045 |
|
| 1046 |
line_sequence* pLine = luaT_testuserdata<line_sequence>(L); |
| 1047 |
render_target* pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 1048 |
pLine->draw(pCanvas, static_cast<int>(luaL_optinteger(L, 3, 0)), |
| 1049 |
static_cast<int>(luaL_optinteger(L, 4, 0))); |
| 1050 |
|
| 1051 |
lua_settop(L, 1); |
| 1052 |
return 1; |
| 1053 |
} |
| 1054 |
|
| 1055 |
int l_line_persist(lua_State* L) { |
| 1056 |
line_sequence* pLine = luaT_testuserdata<line_sequence>(L); |
| 1057 |
lua_persist_writer* pWriter = (lua_persist_writer*)lua_touserdata(L, 2); |
| 1058 |
pLine->persist(pWriter); |
| 1059 |
return 0; |
| 1060 |
} |
| 1061 |
|
| 1062 |
int l_line_depersist(lua_State* L) { |
| 1063 |
void* line_ud = luaT_testuserdata<line_sequence>(L); |
| 1064 |
lua_settop(L, 2); |
| 1065 |
lua_insert(L, 1); |
| 1066 |
lua_persist_reader* pReader = |
| 1067 |
static_cast<lua_persist_reader*>(lua_touserdata(L, 1)); |
| 1068 |
line_sequence* line = new (line_ud) line_sequence(); |
| 1069 |
line->depersist(pReader); |
| 1070 |
return 0; |
| 1071 |
} |
| 1072 |
|
| 1073 |
} // namespace |
| 1074 |
|
| 1075 |
void lua_register_gfx(const lua_register_state* pState) { |
| 1076 |
// Palette |
| 1077 |
{ |
| 1078 |
lua_class_binding<palette> lcb(pState, "palette", l_palette_new, |
| 1079 |
lua_metatable::palette); |
| 1080 |
lcb.add_function(l_palette_set_entry, "setEntry"); |
| 1081 |
} |
| 1082 |
|
| 1083 |
// Raw bitmap |
| 1084 |
{ |
| 1085 |
lua_class_binding<raw_bitmap> lcb(pState, "bitmap", l_rawbitmap_new, |
| 1086 |
lua_metatable::bitmap); |
| 1087 |
lcb.add_function(l_rawbitmap_load, "load", lua_metatable::surface); |
| 1088 |
lcb.add_function(l_rawbitmap_set_pal, "setPalette", lua_metatable::palette); |
| 1089 |
lcb.add_function(l_rawbitmap_draw, "draw", lua_metatable::surface); |
| 1090 |
} |
| 1091 |
|
| 1092 |
// Sprite sheet |
| 1093 |
{ |
| 1094 |
lua_class_binding<sprite_sheet> lcb(pState, "sheet", l_spritesheet_new, |
| 1095 |
lua_metatable::sheet); |
| 1096 |
lcb.add_metamethod(l_spritesheet_count, "len"); |
| 1097 |
lcb.add_function(l_spritesheet_load, "load", lua_metatable::surface); |
| 1098 |
lcb.add_function(l_spritesheet_set_pal, "setPalette", |
| 1099 |
lua_metatable::palette); |
| 1100 |
lcb.add_function(l_spritesheet_size, "size"); |
| 1101 |
lcb.add_function(l_spritesheet_draw, "draw", lua_metatable::surface); |
| 1102 |
lcb.add_function(l_spritesheet_hittest, "hitTest"); |
| 1103 |
lcb.add_function(l_spritesheet_isvisible, "isVisible"); |
| 1104 |
} |
| 1105 |
|
| 1106 |
// Font |
| 1107 |
// Also adapt the font proxy meta table (font_proxy_mt) in graphics.lua. |
| 1108 |
{ |
| 1109 |
lua_class_binding<font> lcb(pState, "font", l_font_new, |
| 1110 |
lua_metatable::font); |
| 1111 |
lcb.add_function(l_font_get_size, "sizeOf"); |
| 1112 |
lcb.add_function(l_font_draw, "draw", lua_metatable::surface); |
| 1113 |
lcb.add_function(l_font_draw_wrapped, "drawWrapped", |
| 1114 |
lua_metatable::surface); |
| 1115 |
lcb.add_function(l_font_draw_tooltip, "drawTooltip", |
| 1116 |
lua_metatable::surface); |
| 1117 |
lcb.add_function(l_font_is_bitmap, "isBitmap"); |
| 1118 |
} |
| 1119 |
|
| 1120 |
// BitmapFont |
| 1121 |
{ |
| 1122 |
lua_class_binding<bitmap_font> lcb(pState, "bitmap_font", l_bitmap_font_new, |
| 1123 |
lua_metatable::bitmap_font); |
| 1124 |
lcb.set_superclass(lua_metatable::font); |
| 1125 |
lcb.add_function(l_bitmap_font_set_spritesheet, "setSheet", |
| 1126 |
lua_metatable::sheet); |
| 1127 |
lcb.add_function(l_bitmap_font_get_spritesheet, "getSheet", |
| 1128 |
lua_metatable::sheet); |
| 1129 |
lcb.add_function(l_bitmap_font_set_sep, "setSeparation"); |
| 1130 |
lcb.add_function(l_bitmap_font_set_scale, "setScaleFactor"); |
| 1131 |
} |
| 1132 |
|
| 1133 |
// FreeTypeFont |
| 1134 |
{ |
| 1135 |
lua_class_binding<freetype_font> lcb(pState, "freetype_font", |
| 1136 |
l_freetype_font_new, |
| 1137 |
lua_metatable::freetype_font); |
| 1138 |
lcb.set_superclass(lua_metatable::font); |
| 1139 |
lcb.add_function(l_freetype_font_set_font_options, "setFontOptions", |
| 1140 |
lua_metatable::sheet); |
| 1141 |
lcb.add_function(l_freetype_font_set_face, "setFace"); |
| 1142 |
lcb.add_function(l_freetype_font_get_copyright, "getCopyrightNotice"); |
| 1143 |
lcb.add_function(l_freetype_font_clear_cache, "clearCache"); |
| 1144 |
} |
| 1145 |
|
| 1146 |
// Layers |
| 1147 |
{ |
| 1148 |
lua_class_binding<layers> lcb(pState, "layers", l_layers_new, |
| 1149 |
lua_metatable::layers); |
| 1150 |
lcb.add_metamethod(l_layers_get, "index"); |
| 1151 |
lcb.add_metamethod(l_layers_set, "newindex"); |
| 1152 |
lcb.add_metamethod(l_layers_persist, "persist"); |
| 1153 |
lcb.add_metamethod(l_layers_depersist, "depersist"); |
| 1154 |
} |
| 1155 |
|
| 1156 |
// Cursor |
| 1157 |
{ |
| 1158 |
lua_class_binding<cursor> lcb(pState, "cursor", l_cursor_new, |
| 1159 |
lua_metatable::cursor); |
| 1160 |
lcb.add_function(l_cursor_load, "load", lua_metatable::sheet); |
| 1161 |
lcb.add_function(l_cursor_use, "use", lua_metatable::surface); |
| 1162 |
lcb.add_function(l_cursor_position, "setPosition", lua_metatable::surface); |
| 1163 |
} |
| 1164 |
|
| 1165 |
// Surface |
| 1166 |
{ |
| 1167 |
lua_class_binding<render_target> lcb(pState, "surface", l_surface_new, |
| 1168 |
lua_metatable::surface); |
| 1169 |
lcb.add_function(l_surface_update, "update"); |
| 1170 |
lcb.add_function(l_surface_fill_black, "fillBlack"); |
| 1171 |
lcb.add_function(l_surface_fill_colour, "fillColour"); |
| 1172 |
lcb.add_function(l_surface_start_frame, "startFrame"); |
| 1173 |
lcb.add_function(l_surface_end_frame, "endFrame"); |
| 1174 |
lcb.add_function(l_surface_nonoverlapping, "nonOverlapping"); |
| 1175 |
lcb.add_function(l_surface_map, "mapRGB"); |
| 1176 |
lcb.add_function(l_surface_set_blue_filter_active, "setBlueFilterActive"); |
| 1177 |
lcb.add_function(l_surface_rect, "drawRect"); |
| 1178 |
lcb.add_function(l_surface_push_clip, "pushClip"); |
| 1179 |
lcb.add_function(l_surface_pop_clip, "popClip"); |
| 1180 |
lcb.add_function(l_surface_get_width, "getWidth"); |
| 1181 |
lcb.add_function(l_surface_get_height, "getHeight"); |
| 1182 |
lcb.add_function(l_surface_screenshot, "takeScreenshot"); |
| 1183 |
lcb.add_function(l_surface_scale, "scale"); |
| 1184 |
lcb.add_function(l_surface_set_caption, "setCaption"); |
| 1185 |
lcb.add_function(l_surface_get_renderer_details, "getRendererDetails"); |
| 1186 |
lcb.add_function(l_surface_set_capture_mouse, "setCaptureMouse"); |
| 1187 |
} |
| 1188 |
|
| 1189 |
// Line |
| 1190 |
{ |
| 1191 |
lua_class_binding<line_sequence> lcb(pState, "line", l_line_new, |
| 1192 |
lua_metatable::line); |
| 1193 |
lcb.add_function(l_move_to, "moveTo"); |
| 1194 |
lcb.add_function(l_line_to, "lineTo"); |
| 1195 |
lcb.add_function(l_set_width, "setWidth"); |
| 1196 |
lcb.add_function(l_set_colour, "setColour"); |
| 1197 |
lcb.add_function(l_line_draw, "draw", lua_metatable::surface); |
| 1198 |
lcb.add_metamethod(l_line_persist, "persist"); |
| 1199 |
lcb.add_metamethod(l_line_depersist, "depersist"); |
| 1200 |
} |
| 1201 |
} |