-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture2D.h
More file actions
125 lines (111 loc) · 4.5 KB
/
Copy pathTexture2D.h
File metadata and controls
125 lines (111 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once
#include <cstdint>
// DTEX format flags
#define DTEX_FLAG_HAS_TRANSPARENCY 0x01 // Image has transparent pixels
#define DTEX_FLAG_HAS_ALPHA 0x02 // Image has alpha channel
#define DTEX_FLAG_IS_SPRITE 0x04 // File contains sprite metadata
#define DTEX_FLAG_ALL_OPAQUE 0x08 // All alpha pixels are 255 (skip runtime scan)
/**
* @brief Base texture class for V-Engine
*/
class Texture2D
{
public:
/**
* @brief Texture formats supported by V-Engine
*/
enum class TextureFormat : uint32_t
{
RGB888 = 0, // 3 bytes per pixel
RGBA8888 = 1, // 4 bytes per pixel
RGB565 = 2, // 2 bytes per pixel (for memory constrained scenarios)
RGB565A8 = 3, // 3 bytes per pixel (RGB565 + 8-bit alpha)
ALPHA8 = 4 // 1 byte per pixel (alpha only)
};
/**
* @brief V-Engine Texture Binary Format Header
*
* File structure:
* [Header][pixel_data][metadata]
*/
struct Header
{
char magic[4]; // Magic number "2DTX"
uint32_t version; // Format version (currently 1)
uint32_t width; // Texture width in pixels
uint32_t height; // Texture height in pixels
TextureFormat format; // Pixel format
uint32_t dataSize; // Size of pixel data in bytes
uint32_t metadataSize; // Size of additional metadata (0 if none)
uint32_t flags; // Format flags (DTEX_FLAG_*)
};
uint8_t* data; // Pixel data (includes alpha if format has it) - Runtime only
int32_t width; // Texture width
int32_t height; // Texture height
TextureFormat format; // Pixel format
bool hasTransparency; // Whether the texture has transparent pixels
bool hasAlpha; // Whether the texture has an alpha channel
// Per-row opaque span data for RGB565A8 sprites with alpha.
// For each row: opaqueStart = first fully opaque column, opaqueEnd = last+1 fully opaque column.
// Pixels outside [opaqueStart, opaqueEnd) have alpha and need blending.
// Pixels inside this range are fully opaque and can be written directly (no blend math).
// nullptr if not computed (non-RGB565A8, or fully opaque/transparent sprites).
int16_t* alphaRowSpans; // Packed pairs: [opaqueStart0, opaqueEnd0, opaqueStart1, opaqueEnd1, ...]
#ifdef DEKI_EDITOR
bool allocatedWithBackend; // Editor-only: Track which allocator was used (play mode uses backend, edit mode uses malloc)
#endif
Texture2D();
virtual ~Texture2D();
/**
* @brief Load texture from V-Engine texture format (.vtex)
* @param file_path Path to the .vtex file
* @return Pointer to loaded texture or nullptr on failure
*/
static Texture2D* Load(const char* file_path);
/**
* @brief Get bytes per pixel for a format
* @param format The texture format
* @return Number of bytes per pixel
*/
static uint32_t GetBytesPerPixel(TextureFormat format);
/**
* @brief Get format name as string (for debugging)
* @param format The texture format
* @return Format name
*/
static const char* GetFormatName(TextureFormat format);
/**
* @brief Validate texture header
* @param header Header to validate
* @return true if header is valid
*/
static bool ValidateHeader(const Texture2D::Header& header);
#ifdef DEKI_EDITOR
/**
* @brief Load texture from file and return as RGBA8888 pixel data (for editor)
* @param file_path Path to the .tex file
* @param out_width Output width
* @param out_height Output height
* @param out_has_alpha Output whether texture has alpha
* @return RGBA8888 pixel data (caller owns memory, use delete[]), or nullptr on failure
*/
static uint8_t* LoadAsRGBA(const char* file_path, int32_t& out_width, int32_t& out_height, bool& out_has_alpha);
/**
* @brief Convert texture pixel data to RGBA8888 format
* @param src_data Source pixel data
* @param width Texture width
* @param height Texture height
* @param format Source format
* @return RGBA8888 pixel data (caller owns memory, use delete[]), or nullptr on failure
*/
static uint8_t* ConvertToRGBA(const uint8_t* src_data, int32_t width, int32_t height, TextureFormat format);
#endif
protected:
/**
* @brief Load texture data from memory buffer
* @param header Parsed texture header
* @param data Raw file data after header
* @return true on success
*/
virtual bool LoadFromMemory(const Texture2D::Header& header, const uint8_t* data);
};