Isle
Loading...
Searching...
No Matches
texture.cpp
Go to the documentation of this file.
1#include "impl.h"
2
3using namespace TglImpl;
4
6
7inline TglD3DRMIMAGE* TextureGetImage(IDirect3DRMTexture* pTexture)
8{
9 return reinterpret_cast<TglD3DRMIMAGE*>(pTexture->GetAppData());
10}
11
12// Forward declare to satisfy order check
13void TextureDestroyCallback(IDirect3DRMObject* pObject, void* pArg);
14
15// FUNCTION: LEGO1 0x100a12a0
16Result TextureImpl::SetImage(IDirect3DRMTexture* pSelf, TglD3DRMIMAGE* pImage)
17{
18 void* appData;
19 Result result;
20
21 appData = pImage;
22
23 // This is here because in the original code they asserted
24 // on the return value being NULL.
25 TextureGetImage(pSelf);
26
27 result = ResultVal(pSelf->SetAppData((LPD3DRM_APPDATA) appData));
28 if (Succeeded(result) && pImage) {
29 result = ResultVal(pSelf->AddDestroyCallback(TextureDestroyCallback, NULL));
30 if (!Succeeded(result)) {
31 pSelf->SetAppData(0);
32 }
33 }
34 return result;
35}
36
37// FUNCTION: LEGO1 0x100a1300
38void TextureDestroyCallback(IDirect3DRMObject* pObject, void* pArg)
39{
40 TglD3DRMIMAGE* pImage = reinterpret_cast<TglD3DRMIMAGE*>(pObject->GetAppData());
41 delete pImage;
42 pObject->SetAppData(0);
43}
44
45// FUNCTION: LEGO1 0x100a1330
47 int width,
48 int height,
49 int depth,
50 void* pBuffer,
51 int useBuffer,
52 int paletteSize,
53 PaletteEntry* pEntries
54)
55{
56 m_image.aspectx = 1;
57 m_image.aspecty = 1;
58 m_image.width = 0;
59 m_image.height = 0;
60 m_image.depth = 0;
61 m_image.rgb = 0;
65 m_image.red_mask = 0xFF;
66 m_image.green_mask = 0xFF;
67 m_image.blue_mask = 0xFF;
68 m_image.alpha_mask = 0xFF;
72 if (pBuffer != NULL) {
73 CreateBuffer(width, height, depth, pBuffer, useBuffer);
74 }
75 if (pEntries != NULL) {
76 InitializePalette(paletteSize, pEntries);
77 }
78}
79
80// FUNCTION: LEGO1 0x100a13b0
82{
84 delete[] ((char*) m_image.buffer1);
85 }
86 delete m_image.palette;
87}
88
89inline static int IsPowerOfTwo(int v)
90{
91 int m = 0;
92
93 while (v > 2 && m == 0) {
94 m = v % 2;
95 v /= 2;
96 }
97
98 return v == 2 && m == 0;
99}
100
101// FUNCTION: LEGO1 0x100a13e0
102Result TglD3DRMIMAGE::CreateBuffer(int width, int height, int depth, void* pBuffer, int useBuffer)
103{
104 if (!(IsPowerOfTwo(width) && IsPowerOfTwo(height) && width % 4 == 0)) {
105 return Error;
106 }
107
108 m_image.width = width;
109 m_image.height = height;
110 m_image.depth = depth;
111 m_image.bytes_per_line = width;
112
114 delete[] ((char*) m_image.buffer1);
116 }
117
118 if (useBuffer) {
120 m_image.buffer1 = (char*) pBuffer;
121 }
122 else {
123 m_image.buffer1 = new char[width * height];
124 memcpy(m_image.buffer1, pBuffer, width * height);
126 }
127
128 return Success;
129}
130
131// FUNCTION: LEGO1 0x100a1510
132Result TglD3DRMIMAGE::FillRowsOfTexture(int y, int height, char* pContent)
133{
134 // The purpose is clearly this but I can't get the assembly to line up.
135 memcpy((char*) m_image.buffer1 + (y * m_image.bytes_per_line), pContent, height * m_image.bytes_per_line);
136 return Success;
137}
138
139// FUNCTION: LEGO1 0x100a1550
141{
142 // This function is a 100% match if the PaletteEntry class is copied
143 // into into the TglD3DRMIMAGE class instead of being a global struct.
144 if (m_image.palette_size != paletteSize) {
145 if (m_image.palette != NULL) {
146 delete m_image.palette;
149 }
150 if (paletteSize > 0) {
151 m_image.palette = new D3DRMPALETTEENTRY[paletteSize];
152 m_image.palette_size = paletteSize;
153 }
154 }
155 if (paletteSize > 0) {
156 for (int i = 0; i < paletteSize; i++) {
157 m_image.palette[i].red = pEntries[i].m_red;
158 m_image.palette[i].green = pEntries[i].m_green;
159 m_image.palette[i].blue = pEntries[i].m_blue;
161 }
162 }
163 return Success;
164}
165
166// FUNCTION: LEGO1 0x100a3c10
167Result TextureImpl::SetTexels(int width, int height, int bitsPerTexel, void* pTexels)
168{
169 TglD3DRMIMAGE* image = TextureGetImage(m_data);
170 Result result = image->CreateBuffer(width, height, bitsPerTexel, pTexels, TRUE);
171 if (Succeeded(result)) {
172 result = ResultVal(m_data->Changed(TRUE, FALSE));
173 }
174 return result;
175}
176
177// FUNCTION: LEGO1 0x100a3c60
178void TextureImpl::FillRowsOfTexture(int y, int height, void* pBuffer)
179{
180 TglD3DRMIMAGE* image = TextureGetImage(m_data);
181 image->FillRowsOfTexture(y, height, (char*) pBuffer);
182}
183
184// FUNCTION: LEGO1 0x100a3c90
185Result TextureImpl::Changed(int texelsChanged, int paletteChanged)
186{
187 return ResultVal(m_data->Changed(texelsChanged, paletteChanged));
188}
189
190// FUNCTION: LEGO1 0x100a3cc0
192 int* width,
193 int* height,
194 int* depth,
195 void** pBuffer,
196 int* paletteSize,
197 PaletteEntry** pEntries
198)
199{
200 // Something really doesn't match here, not sure what's up.
201 TglD3DRMIMAGE* image = TextureGetImage(m_data);
202 *width = image->m_image.width;
203 *height = image->m_image.height;
204 *depth = image->m_image.depth;
205 *pBuffer = image->m_image.buffer1;
206 *paletteSize = image->m_image.palette_size;
207 for (int i = 0; i < image->m_image.palette_size; i++) {
208 pEntries[i]->m_red = image->m_image.palette[i].red;
209 pEntries[i]->m_green = image->m_image.palette[i].green;
210 pEntries[i]->m_blue = image->m_image.palette[i].blue;
211 }
212 return Success;
213}
214
215// FUNCTION: LEGO1 0x100a3d40
217{
218 // Not 100% confident this is supposed to directly be forwarding arguments,
219 // but it probably is given FillRowsOfTexture matches doing that.
220 TglD3DRMIMAGE* image = TextureGetImage(m_data);
221 image->InitializePalette(entryCount, pEntries);
222 m_data->Changed(FALSE, TRUE);
223 return Success;
224}
225
226// FUNCTION: LEGO1 0x100a3d70
227// FUNCTION: BETA10 0x1016c760
229{
230 return reinterpret_cast<void*>(&m_data);
231}
static Result SetImage(IDirect3DRMTexture *pSelf, TglD3DRMIMAGE *pImage)
Copies palette/texel data from TglD3DRMIMAGE to Direct3DRMTexture [AI].
Definition: texture.cpp:16
void FillRowsOfTexture(int y, int height, void *pBuffer) override
Fills rows of the texture [AI].
Definition: texture.cpp:178
void * ImplementationDataPtr() override
Returns implementation pointer for texture [AI].
Definition: texture.cpp:228
Result GetBufferAndPalette(int *pWidth, int *pHeight, int *pDepth, void **ppBuffer, int *ppPaletteSize, PaletteEntry **ppPalette) override
Returns current buffer and palette [AI].
Definition: texture.cpp:191
Result SetPalette(int entryCount, PaletteEntry *entries) override
Sets the palette entries for the texture [AI].
Definition: texture.cpp:216
Result Changed(int texelsChanged, int paletteChanged) override
Notifies that texels or palette have changed [AI].
Definition: texture.cpp:185
Result SetTexels(int width, int height, int bitsPerTexel, void *pTexels) override
Sets texel data for the texture [AI].
Definition: texture.cpp:167
[AI] Simple wrapper for D3DRMIMAGE; manages pixel buffer and palette for Direct3DRM textures [AI]
Definition: impl.h:1207
TglD3DRMIMAGE(int width, int height, int depth, void *pBuffer, int useBuffer, int paletteSize, PaletteEntry *pEntries)
Constructs an image buffer for use with Direct3DRM textures [AI].
Definition: texture.cpp:46
void Destroy()
Destroys any buffers/palette managed by the image [AI].
Definition: texture.cpp:81
D3DRMIMAGE m_image
[AI] Underlying D3DRM image struct [AI]
Definition: impl.h:1260
Result FillRowsOfTexture(int y, int height, char *content)
Writes pixel rows into texture [AI].
Definition: texture.cpp:132
Result InitializePalette(int paletteSize, PaletteEntry *pEntries)
Initializes palette for image [AI].
Definition: texture.cpp:140
int m_texelsAllocatedByClient
[AI] Flag for texel memory ownership (by client) [AI]
Definition: impl.h:1261
Result CreateBuffer(int width, int height, int depth, void *pBuffer, int useBuffer)
Creates or reallocates image buffer as necessary [AI].
Definition: texture.cpp:102
#define TRUE
Definition: d3drmdef.h:28
#define FALSE
Definition: d3drmdef.h:27
@ D3DRMPALETTE_READONLY
Definition: d3drmdef.h:118
#define DECOMP_SIZE_ASSERT(T, S)
Definition: decomp.h:19
LPVOID LPD3DRM_APPDATA
[AI] Application data type varies by DirectX version [AI]
Definition: impl.h:10
#define NULL
[AI] Null pointer value (C/C++ semantics).
Definition: legotypes.h:26
[AI] Forward declaration for Direct3DRMTexture interface [AI]
Definition: impl.h:23
Result ResultVal(HRESULT result)
Returns a Tgl Result value for HRESULT [AI].
Definition: impl.h:35
int Succeeded(Result result)
[AI] Returns whether a Tgl::Result indicates success.
Definition: tgl.h:136
Result
[AI] Result type used throughout the Tgl API to report operation success or failure.
Definition: tgl.h:126
@ Success
[AI] Operation succeeded. [AI]
Definition: tgl.h:128
@ Error
[AI] Operation failed. [AI]
Definition: tgl.h:127
[AI] Represents a single color entry in a palette.
Definition: tgl.h:86
unsigned char m_blue
[AI] Blue component. [AI]
Definition: tgl.h:89
unsigned char m_green
[AI] Green component. [AI]
Definition: tgl.h:88
unsigned char m_red
[AI] Red component. [AI]
Definition: tgl.h:87
unsigned long red_mask
Definition: d3drmdef.h:143
unsigned long blue_mask
Definition: d3drmdef.h:145
char * buffer2
Definition: d3drmdef.h:140
unsigned long alpha_mask
Definition: d3drmdef.h:146
D3DRMPALETTEENTRY * palette
Definition: d3drmdef.h:156
char * buffer1
Definition: d3drmdef.h:139
int aspectx
Definition: d3drmdef.h:131
int height
Definition: d3drmdef.h:130
int palette_size
Definition: d3drmdef.h:155
int aspecty
Definition: d3drmdef.h:131
int bytes_per_line
Definition: d3drmdef.h:136
unsigned long green_mask
Definition: d3drmdef.h:144
unsigned char blue
Definition: d3drmdef.h:125
unsigned char green
Definition: d3drmdef.h:124
unsigned char red
Definition: d3drmdef.h:123
unsigned char flags
Definition: d3drmdef.h:126
TglD3DRMIMAGE * TextureGetImage(IDirect3DRMTexture *pTexture)
Definition: texture.cpp:7
void TextureDestroyCallback(IDirect3DRMObject *pObject, void *pArg)
Definition: texture.cpp:38