Monopoly — Portage moderne
0.1.0
Portage moderne C/C++23 du Monopoly de 1999
Loading...
Searching...
No Matches
LegacyAssets.cpp
Go to the documentation of this file.
1
#include "
LegacyAssets.hpp
"
2
3
#include <cstring>
4
#include <filesystem>
5
#include <iostream>
6
7
namespace
monopoly::legacyassets
8
{
9
namespace
10
{
11
SDL_GPUDevice
*
gpuDevice
=
nullptr
;
12
13
Texture2D
background;
14
15
16
bool
loadBMPTexture
(
17
const
std::filesystem::path&
path
,
18
Texture2D
&
destination
)
19
{
20
SDL_Surface
*
source
=
21
SDL_LoadBMP
(
path
.string().c_str());
22
23
if
(
source
==
nullptr
)
24
{
25
std::cerr
26
<<
"Unable to load legacy BMP: "
27
<<
path
.string()
28
<<
" : "
29
<<
SDL_GetError
()
30
<<
'\n'
;
31
32
return
false
;
33
}
34
35
SDL_Surface
* rgba =
36
SDL_ConvertSurface
(
37
source
,
38
SDL_PIXELFORMAT_RGBA32
39
);
40
41
SDL_DestroySurface
(
source
);
42
43
if
(rgba ==
nullptr
)
44
{
45
std::cerr
46
<<
"Unable to convert legacy BMP: "
47
<<
SDL_GetError
()
48
<<
'\n'
;
49
50
return
false
;
51
}
52
53
const
Uint32
width
=
54
static_cast<
Uint32
>
(rgba->w);
55
56
const
Uint32
height
=
57
static_cast<
Uint32
>
(rgba->h);
58
59
const
Uint32
bytesPerPixel
= 4;
60
61
const
Uint32
uploadSize
=
62
width
*
63
height
*
64
bytesPerPixel
;
65
66
67
SDL_GPUTextureCreateInfo
textureInfo
{};
68
69
textureInfo
.type =
70
SDL_GPU_TEXTURETYPE_2D
;
71
72
textureInfo
.format =
73
SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM
;
74
75
textureInfo
.usage =
76
SDL_GPU_TEXTUREUSAGE_SAMPLER
;
77
78
textureInfo
.width =
width
;
79
textureInfo
.height =
height
;
80
81
textureInfo
.layer_count_or_depth = 1;
82
textureInfo
.num_levels = 1;
83
84
textureInfo
.sample_count =
85
SDL_GPU_SAMPLECOUNT_1
;
86
87
88
SDL_GPUTexture
*
texture
=
89
SDL_CreateGPUTexture
(
90
gpuDevice
,
91
&
textureInfo
92
);
93
94
if
(
texture
==
nullptr
)
95
{
96
std::cerr
97
<<
"SDL_CreateGPUTexture failed: "
98
<<
SDL_GetError
()
99
<<
'\n'
;
100
101
SDL_DestroySurface
(rgba);
102
return
false
;
103
}
104
105
106
SDL_GPUTransferBufferCreateInfo
transferInfo
{};
107
108
transferInfo
.usage =
109
SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD
;
110
111
transferInfo
.size =
112
uploadSize
;
113
114
115
SDL_GPUTransferBuffer
*
transfer
=
116
SDL_CreateGPUTransferBuffer
(
117
gpuDevice
,
118
&
transferInfo
119
);
120
121
if
(
transfer
==
nullptr
)
122
{
123
SDL_ReleaseGPUTexture
(
124
gpuDevice
,
125
texture
126
);
127
128
SDL_DestroySurface
(rgba);
129
130
return
false
;
131
}
132
133
134
void
*
mapped
=
135
SDL_MapGPUTransferBuffer
(
136
gpuDevice
,
137
transfer
,
138
false
139
);
140
141
if
(
mapped
==
nullptr
)
142
{
143
SDL_ReleaseGPUTransferBuffer
(
144
gpuDevice
,
145
transfer
146
);
147
148
SDL_ReleaseGPUTexture
(
149
gpuDevice
,
150
texture
151
);
152
153
SDL_DestroySurface
(rgba);
154
155
return
false
;
156
}
157
158
159
auto
*
destinationPixels
=
160
static_cast<
unsigned
char
*
>
(
mapped
);
161
162
const
auto
*
sourcePixels
=
163
static_cast<
const
unsigned
char
*
>
(
164
rgba->pixels
165
);
166
167
const
std::size_t
packedRowSize
=
168
static_cast<
std::size_t
>
(
width
) *
169
bytesPerPixel
;
170
171
for
(
Uint32
y
= 0;
y
<
height
; ++
y
)
172
{
173
std::memcpy(
174
destinationPixels
+
175
static_cast<
std::size_t
>
(
y
) *
176
packedRowSize
,
177
178
sourcePixels
+
179
static_cast<
std::size_t
>
(
y
) *
180
rgba->pitch,
181
182
packedRowSize
183
);
184
}
185
186
187
SDL_UnmapGPUTransferBuffer
(
188
gpuDevice
,
189
transfer
190
);
191
192
SDL_DestroySurface
(rgba);
193
194
195
SDL_GPUCommandBuffer
*
commandBuffer
=
196
SDL_AcquireGPUCommandBuffer
(
197
gpuDevice
198
);
199
200
if
(
commandBuffer
==
nullptr
)
201
{
202
SDL_ReleaseGPUTransferBuffer
(
203
gpuDevice
,
204
transfer
205
);
206
207
SDL_ReleaseGPUTexture
(
208
gpuDevice
,
209
texture
210
);
211
212
return
false
;
213
}
214
215
216
SDL_GPUCopyPass
*
copyPass
=
217
SDL_BeginGPUCopyPass
(
218
commandBuffer
219
);
220
221
if
(
copyPass
==
nullptr
)
222
{
223
SDL_CancelGPUCommandBuffer
(
224
commandBuffer
225
);
226
227
SDL_ReleaseGPUTransferBuffer
(
228
gpuDevice
,
229
transfer
230
);
231
232
SDL_ReleaseGPUTexture
(
233
gpuDevice
,
234
texture
235
);
236
237
return
false
;
238
}
239
240
241
SDL_GPUTextureTransferInfo
sourceInfo
{};
242
243
sourceInfo
.transfer_buffer =
244
transfer
;
245
246
sourceInfo
.offset = 0;
247
248
sourceInfo
.pixels_per_row =
249
width
;
250
251
sourceInfo
.rows_per_layer =
252
height
;
253
254
255
SDL_GPUTextureRegion
destinationRegion
{};
256
257
destinationRegion
.texture =
258
texture
;
259
260
destinationRegion
.mip_level = 0;
261
destinationRegion
.layer = 0;
262
263
destinationRegion
.x = 0;
264
destinationRegion
.y = 0;
265
destinationRegion
.z = 0;
266
267
destinationRegion
.w =
width
;
268
destinationRegion
.h =
height
;
269
destinationRegion
.d = 1;
270
271
272
SDL_UploadToGPUTexture
(
273
copyPass
,
274
&
sourceInfo
,
275
&
destinationRegion
,
276
false
277
);
278
279
SDL_EndGPUCopyPass
(
copyPass
);
280
281
282
if
(!
SDL_SubmitGPUCommandBuffer
(
283
commandBuffer
))
284
{
285
SDL_ReleaseGPUTransferBuffer
(
286
gpuDevice
,
287
transfer
288
);
289
290
SDL_ReleaseGPUTexture
(
291
gpuDevice
,
292
texture
293
);
294
295
return
false
;
296
}
297
298
299
SDL_ReleaseGPUTransferBuffer
(
300
gpuDevice
,
301
transfer
302
);
303
304
305
destination
.texture =
306
texture
;
307
308
destination
.width =
309
width
;
310
311
destination
.height =
312
height
;
313
314
315
SDL_SetGPUTextureName
(
316
gpuDevice
,
317
texture
,
318
"Legacy Monopoly 3D Background"
319
);
320
321
return
true
;
322
}
323
}
324
325
326
bool
initialize
(
SDL_GPUDevice
* device)
327
{
328
gpuDevice
= device;
329
330
if
(
gpuDevice
==
nullptr
)
331
{
332
return
false
;
333
}
334
335
const
char
*
basePath
=
336
SDL_GetBasePath
();
337
338
if
(
basePath
==
nullptr
)
339
{
340
return
false
;
341
}
342
343
const
std::filesystem::path
path
=
344
std::filesystem::path(
basePath
) /
345
"assets"
/
346
"legacy"
/
347
"BackGround.bmp"
;
348
349
350
// display.cpp original :
351
//
352
// #if USA_VERSION
353
// wBmpID = IDB_BACKGROUND;
354
// #endif
355
//
356
// LE_REND3D_SetBackgroundSurface(1, ...)
357
358
return
loadBMPTexture
(
359
path
,
360
background
361
);
362
}
363
364
365
void
shutdown
()
366
{
367
if
(
gpuDevice
!=
nullptr
&&
368
background.texture !=
nullptr
)
369
{
370
SDL_ReleaseGPUTexture
(
371
gpuDevice
,
372
background.texture
373
);
374
}
375
376
background = {};
377
gpuDevice
=
nullptr
;
378
}
379
380
381
const
Texture2D
&
background3D
()
382
{
383
return
background;
384
}
385
}
y
int y
Definition
EscapeConfirmationPlayback.cpp:16
LegacyAssets.hpp
source
std::shared_ptr< const data::HmdTextureImage > source
Definition
MeshGPUResources.cpp:53
texture
SDL_GPUTexture * texture
Definition
MeshGPUResources.cpp:59
height
std::uint32_t height
Definition
MeshGPUResources.cpp:55
width
std::uint32_t width
Definition
MeshGPUResources.cpp:54
index
unsigned index
Definition
ModernIBarSkin.cpp:25
path
std::string path
Definition
StartupResources.cpp:24
monopoly::legacyassets
Definition
LegacyAssets.cpp:8
monopoly::legacyassets::background3D
const Texture2D & background3D()
Definition
LegacyAssets.cpp:381
monopoly::legacyassets::shutdown
void shutdown()
Definition
LegacyAssets.cpp:365
monopoly::legacyassets::initialize
bool initialize(SDL_GPUDevice *device)
Definition
LegacyAssets.cpp:326
monopoly::legacyassets::Texture2D
Definition
LegacyAssets.hpp:9
LegacyAssets.cpp
Generated by
1.9.8