Scarab
A thin, game-agnostic 2D engine host built on the sunlight tile-map engine
Loading...
Searching...
No Matches
spritehandle.h
Go to the documentation of this file.
1/*
2 * Copyright (c) since 2021 by PopolonY2k and Leidson Campos A. Ferreira
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 */
20
21#ifndef __SPRITEHANDLE_H__
22#define __SPRITEHANDLE_H__
23
24#include <cstdint>
25
26
27namespace Scarab {
28 namespace Engine {
29
37 typedef uint32_t SpriteHandle;
38
40
41 inline SpriteHandle MakeSpriteHandle( uint32_t nIndex, uint16_t nGeneration ) {
42
43 return ( ( ( SpriteHandle ) nGeneration ) << 16 ) | ( ( nIndex + 1 ) & 0xFFFF );
44 }
45
46 inline int32_t SpriteHandleIndex( SpriteHandle handle ) {
47
48 return ( int32_t ) ( handle & 0xFFFF ) - 1;
49 }
50
51 inline uint16_t SpriteHandleGeneration( SpriteHandle handle ) {
52
53 return ( uint16_t ) ( handle >> 16 );
54 }
55
56 /*
57 * A SpriteHandle is stashed inside a Collider's opaque void* data (via
58 * SetPtrData/GetPtrData) so a collision callback can identify which
59 * sprite collided without exposing raw C++ pointers to Lua. The upper
60 * 32 bits of the packed value below are set to a fixed tag no real
61 * 64-bit heap pointer will ever carry - originally so callbacks could
62 * tell a packed handle apart from a legacy Caravellius stSpriteData*
63 * stored in that same slot during the Lua migration (that legacy path
64 * is fully gone now, Phase 9, but the tag remains a cheap defensive
65 * check against any future non-handle value ending up there).
66 */
67 const uintptr_t SPRITE_HANDLE_PTR_TAG = 0xDEADBEEF00000000ULL;
68
69 inline void* PackSpriteHandleAsPtr( SpriteHandle handle ) {
70
71 return ( void * ) ( SPRITE_HANDLE_PTR_TAG | ( uintptr_t ) handle );
72 }
73
74 inline bool IsPackedSpriteHandle( void *pPtr ) {
75
76 return ( ( ( uintptr_t ) pPtr ) & 0xFFFFFFFF00000000ULL ) == SPRITE_HANDLE_PTR_TAG;
77 }
78
80
81 return ( SpriteHandle ) ( ( uintptr_t ) pPtr & 0xFFFFFFFFULL );
82 }
83 }
84}
85
86#endif /* __SPRITEHANDLE_H__ */
int32_t SpriteHandleIndex(SpriteHandle handle)
Definition spritehandle.h:46
SpriteHandle MakeSpriteHandle(uint32_t nIndex, uint16_t nGeneration)
Definition spritehandle.h:41
bool IsPackedSpriteHandle(void *pPtr)
Definition spritehandle.h:74
SpriteHandle UnpackSpriteHandleFromPtr(void *pPtr)
Definition spritehandle.h:79
void * PackSpriteHandleAsPtr(SpriteHandle handle)
Definition spritehandle.h:69
const SpriteHandle INVALID_SPRITE_HANDLE
Definition spritehandle.h:39
uint16_t SpriteHandleGeneration(SpriteHandle handle)
Definition spritehandle.h:51
uint32_t SpriteHandle
Opaque handle to a sprite pool slot. Packs a slot index (low 16 bits, 1-based so 0 stays reserved for...
Definition spritehandle.h:37
const uintptr_t SPRITE_HANDLE_PTR_TAG
Definition spritehandle.h:67
Definition irendererprovider.h:32