-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD3D9Object.c
732 lines (623 loc) · 17.6 KB
/
D3D9Object.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include "D3D9Object.h"
// ---------- Debugging -------------
#define __DEBUG_OBJECT__ "D3D9Object"
#include "dbg/dbg.h"
// Factory declaration and static initialization
struct D3D9ObjectFactory {
BbQueue drawObjects;
BbQueue allObjects;
BbQueue spriteToInstanciate;
HANDLE mutex;
int id;
bool initialized;
} d3d9ObjectFactory = {
.drawObjects = bb_queue_local_decl (),
.allObjects = bb_queue_local_decl (),
.spriteToInstanciate = bb_queue_local_decl (),
.mutex = NULL,
.id = 0,
.initialized = false
};
// Private headers
/*
* Description : Add a D3D9Object into the collection of the factory
* D3D9Object *this : An allocated D3D9Object
* Return : void
*/
static void D3D9ObjectFactory_add (D3D9Object *this);
/// ===== D3D9ObjectFactory =====
/*
* Description : Allocate a new D3D9Object
* D3D9ObjectType type : Type of the object to create
* Return : D3D9Object * an allocated D3D9Object
*/
D3D9Object *
D3D9ObjectFactory_createD3D9Object (
D3D9ObjectType type
) {
D3D9Object *this = NULL;
// Check if the factory has been initialized
if (!d3d9ObjectFactory.initialized) {
d3d9ObjectFactory.mutex = CreateMutex (NULL, false, NULL);
d3d9ObjectFactory.initialized = true;
}
// Allocate a new instance of D3D9Object
if ((this = calloc (1, sizeof(D3D9Object))) == NULL)
return NULL;
this->type = type;
this->id = d3d9ObjectFactory.id++;
this->mutex = d3d9ObjectFactory.mutex;
return this;
}
/*
* Description : Add a D3D9Object into the collection of the factory
* D3D9Object *this : An allocated D3D9Object
* Return : void
*/
static void
D3D9ObjectFactory_add (
D3D9Object *this
) {
if (!bb_queue_exists (&d3d9ObjectFactory.drawObjects, this))
bb_queue_add (&d3d9ObjectFactory.drawObjects, this);
if (!bb_queue_exists (&d3d9ObjectFactory.allObjects, this))
bb_queue_add (&d3d9ObjectFactory.allObjects, this);
}
/*
* Description : Return an allocated D3D9Object from the global factory list
* unsigned int id : A D3D9ObjectFactory ID already allocated
* Return : void
*/
D3D9Object *
D3D9ObjectFactory_get (
unsigned int id
) {
foreach_bbqueue_item (&d3d9ObjectFactory.allObjects, D3D9Object *object)
{
if (object->id == id) {
return object;
}
}
warn ("Object ID=%d not found in global list.", id);
return NULL;
}
/*
* Description : Return an allocated D3D9Object from the draw factory list
* unsigned int id : A D3D9ObjectFactory ID already allocated
* Return : void
*/
D3D9Object *
D3D9ObjectFactory_get_draw (
unsigned int id
) {
foreach_bbqueue_item (&d3d9ObjectFactory.drawObjects, D3D9Object *object)
{
if (object->id == id) {
return object;
}
}
warn ("Object ID=%d not found in draw list.", id);
return NULL;
}
/*
* Description : Add an allocated D3D9Object to the factory draw list.
* unsigned int id : A D3D9ObjectFactory ID already allocated
* Return : The D3D9Object hidden, or NULL if not found
*/
D3D9Object *
D3D9ObjectFactory_show (
unsigned int id
) {
D3D9ObjectFactory_lock ();
D3D9Object *object;
if (!(object = D3D9ObjectFactory_get (id))) {
warn ("Cannot show object ID=%d.", id);
return NULL;
}
// Put the object shown in the last position of the allObject list, so
// it keeps a coherent order when hide_all / show_all is called.
bb_queue_put_last (&d3d9ObjectFactory.allObjects, object);
// If already exists, put it to the last position
if (bb_queue_exists (&d3d9ObjectFactory.drawObjects, object)) {
bb_queue_put_last (&d3d9ObjectFactory.drawObjects, object);
}
else {
// Add the object in the draw list
bb_queue_add (&d3d9ObjectFactory.drawObjects, object);
}
D3D9ObjectFactory_release ();
return object;
}
/*
* Description : Add all allocated D3D9Object to the factory draw list.
Don't add it again if it already exists in the list.
* Return : void
*/
void
D3D9ObjectFactory_show_all (
void
) {
D3D9ObjectFactory_lock ();
foreach_bbqueue_item (&d3d9ObjectFactory.allObjects, D3D9Object *object)
{
if (!bb_queue_exists (&d3d9ObjectFactory.drawObjects, object))
bb_queue_add (&d3d9ObjectFactory.drawObjects, object);
}
D3D9ObjectFactory_release ();
}
/*
* Description : Remove an allocated D3D9Object from the factory draw list, so it isn't displayed anymore
* unsigned int id : A D3D9ObjectFactory ID already allocated
* Return : The D3D9Object hidden, or NULL if not found
*/
D3D9Object *
D3D9ObjectFactory_hide (
unsigned int id
) {
D3D9ObjectFactory_lock ();
D3D9Object *object;
if (!(object = D3D9ObjectFactory_get_draw (id))) {
warn ("Cannot hide object ID=%d.", id);
return NULL;
}
// Remove from the draw list
if (!(bb_queue_exists (&d3d9ObjectFactory.drawObjects, object))) {
warn ("Object ID=%d doesn't exist in the draw list. Abort hide.", id);
return NULL;
}
bb_queue_remv (&d3d9ObjectFactory.drawObjects, object);
D3D9ObjectFactory_release ();
return object;
}
/*
* Description : Remove all allocated D3D9Object from the factory draw list, so they aren't displayed anymore
* Return : void
*/
void
D3D9ObjectFactory_hide_all (
void
) {
D3D9ObjectFactory_lock ();
// Remove all D3D9Objects from the drawObjects list
while (bb_queue_get_length (&d3d9ObjectFactory.drawObjects)) {
bb_queue_pop (&d3d9ObjectFactory.drawObjects);
}
D3D9ObjectFactory_release ();
}
/*
* Description : Remove an allocated D3D9Object from all the factory lists
* unsigned int id : A D3D9ObjectFactory ID already allocated
* Return : void
*/
void
D3D9ObjectFactory_delete (
unsigned int id
) {
D3D9ObjectFactory_lock ();
D3D9Object * object;
if (!(object = D3D9ObjectFactory_get (id))) {
warn ("Cannot find the object to delete.");
return;
}
if (bb_queue_exists (&d3d9ObjectFactory.drawObjects, object)) {
bb_queue_remv (&d3d9ObjectFactory.drawObjects, object);
}
bb_queue_remv (&d3d9ObjectFactory.allObjects, object);
D3D9Object_free (object);
D3D9ObjectFactory_release ();
}
/*
* Description : Remove all the allocated D3D9Object from the working factory lists
* Return : void
*/
void
D3D9ObjectFactory_delete_all (
void
) {
D3D9ObjectFactory_lock ();
// Remove all D3D9Objects from the drawObjects list
while (bb_queue_get_length (&d3d9ObjectFactory.drawObjects)) {
bb_queue_pop (&d3d9ObjectFactory.drawObjects);
}
// Remove all D3D9Objects from the global list
while (bb_queue_get_length (&d3d9ObjectFactory.allObjects)) {
D3D9Object *object = bb_queue_pop (&d3d9ObjectFactory.allObjects);
// Free the memory
D3D9Object_free (object);
}
D3D9ObjectFactory_release ();
}
/*
* Description : Return a pointer to the objects list
* Return : BbQueue * A list of D3D9Objects pointer
*/
BbQueue *
D3D9ObjectFactory_get_objects (
void
) {
return &d3d9ObjectFactory.drawObjects;
}
/*
* Description : Lock the mutex shared with all the d3d9objects
* Return : void
*/
void
D3D9ObjectFactory_lock (
void
) {
WaitForSingleObject (d3d9ObjectFactory.mutex, INFINITE);
}
/*
* Description : Release the mutex shared with all the d3d9objects
* Return : void
*/
void
D3D9ObjectFactory_release (
void
) {
ReleaseMutex (d3d9ObjectFactory.mutex);
}
/*
* Description : Get the top level object that is hovered. If no object is hovered, return NULL
* HWND hWindow : The window containing the directX context
* Return : A pointer to the hovered object, or NULL
*/
D3D9Object *
D3D9ObjectFactory_get_hovered_object (
HWND hWindow
) {
int mouseX, mouseY;
get_mouse_pos_in_window (hWindow, &mouseX, &mouseY);
foreach_bbqueue_item_reversed (&d3d9ObjectFactory.drawObjects, D3D9Object *object)
{
switch (object->type)
{
case D3D9_OBJECT_RECTANGLE: {
D3D9ObjectRect *rect = &object->rect;
if (in_bound (mouseX, mouseY, object->x, object->y, object->x + rect->w, object->y + rect->h)) {
return object;
}
} break;
case D3D9_OBJECT_SPRITE: {
D3D9ObjectSprite *sprite = &object->sprite;
if (in_bound (mouseX, mouseY, object->x, object->y, object->x + sprite->w, object->y + sprite->h)) {
return object;
}
} break;
default :
continue;
break;
}
}
return NULL;
}
/// ===== D3D9Object =====
/*
* Description : Move an allocated D3D9Object on the screen
* D3D9Object *this : An allocated D3D9Object
# int x, int y : The new position on the screen
* Return : void
*/
void
D3D9Object_move (
D3D9Object *this,
int x, int y
) {
this->x = x;
this->y = y;
}
/*
* Description : Initialize an allocated D3D9ObjectRect object.
* int x, y : {x, y} position of the rectangle
* int w, h : width and height of the rectangle
* byte r, byte g, byte b : color of the rectangle
* Return : void
*/
bool
D3D9ObjectRect_init (
D3D9Object * this,
int x, int y,
int w, int h,
byte r, byte g, byte b
) {
D3D9ObjectFactory_lock ();
D3D9ObjectRect * rect = &this->rect;
// Fill the structure
this->x = x;
this->y = y;
rect->w = w;
rect->h = h;
rect->r = r;
rect->g = g;
rect->b = b;
dbg ("Rectangle <ID=%d | x=%d | y=%d | w=%d | h=%d | rgb=%02X%02X%02X> has been created.", this->id, x, y, w, h, r, g, b);
D3D9ObjectFactory_add (this);
D3D9ObjectFactory_release ();
return true;
}
/*
* Description : Set attributes of a D3D9ObjectRect
* D3D9ObjectRect *this : An allocated D3D9ObjectRect
* byte r, byte g, byte b : New color of the rectangle
* int w, int h : Width and height of the rectangle
* Return : void
*/
void
D3D9ObjectRect_set (
D3D9ObjectRect *this,
byte r, byte g, byte b,
int w, int h
) {
this->r = r;
this->g = g;
this->b = b;
this->w = w;
this->h = h;
}
/*
* Description : Initialize an allocated D3D9ObjectText object.
* D3D9Object * this : An allocated D3D9Object
* IDirect3DDevice9 * pDevice : An allocated IDirect3DDevice9
* int x, y : {x, y} position of the text
* byte r, byte g, byte b : color of the text
* float opacity : opacity of the text, value between 0.0 and 1.0
* char * string : String of the text
* int fontSize : the size of the font
* char * fontFamily : The name of the family font. If NULL, "Arial" is used.
* Return : void
*/
bool
D3D9ObjectText_init (
D3D9Object * this,
IDirect3DDevice9 * pDevice,
int x, int y,
byte r, byte g, byte b,
float opacity,
char *string,
int fontSize,
char *fontFamily
) {
D3D9ObjectFactory_lock ();
D3D9ObjectText * text = &this->text;
// Default parameters
if (fontFamily == NULL) {
fontFamily = "Arial";
}
// Allocate the font
if ((D3DXCreateFont (
pDevice,
fontSize,
0,
FW_BOLD,
1,
0,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE,
fontFamily,
&text->font
)) != S_OK) {
warn ("Cannot create the font ID=%d.", this->id);
D3D9ObjectFactory_release ();
return false;
}
// Fill the structure
this->x = x;
this->y = y;
text->r = r;
text->g = g;
text->b = b;
text->string = strdup (string);
text->opacity = (opacity * 255 > 255) ? 255 : opacity * 255;
dbg ("Text <ID=%d | string=<%s> | x=%d | y=%d | rgb=%02X%02X%02X | opacity=%d> has been created.", this->id, string, x, y, r, g, b, text->opacity);
D3D9ObjectFactory_add (this);
D3D9ObjectFactory_release ();
return true;
}
/*
* Description : Set new attribute to D3D9ObjectText
* D3D9ObjectText *this : An allocated D3D9ObjectText
* char *string : New string of the text
* byte r, byte g, byte b : New color of the text
* float opacity : opacity of the text
* Return : void
*/
void
D3D9ObjectText_set (
D3D9ObjectText *this,
char *string,
byte r, byte g, byte b,
float opacity
) {
this->string = strdup (string);
this->r = r;
this->g = g;
this->b = b;
this->opacity = (opacity * 255 > 255) ? 255 : opacity * 255;
}
/*
* Description : Initialize an allocated D3D9ObjectSprite object.
* D3D9Object * this : An allocated D3D9Object
* IDirect3DDevice9 * pDevice : An allocated IDirect3DDevice9
* char * filePath : Absolute or relative path of the image (.bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga)
* int x, y : {x, y} position of the sprite
# float opacity : opacity of the image, value between 0.0 and 1.0
* Return : bool True on success, false otherwise
*/
bool
D3D9ObjectSprite_init (
D3D9Object * this,
IDirect3DDevice9 * pDevice,
char *filePath,
int x, int y,
float opacity
) {
D3D9ObjectFactory_lock ();
D3D9ObjectSprite * sprite = &this->sprite;
// Fill the structure
this->x = x;
this->y = y;
sprite->opacity = (opacity * 255 > 255) ? 255 : opacity * 255;
sprite->filePath = strdup (filePath);
dbg ("Sprite <ID=%d | filePath=<%s> | x=%d | y=%d | opacity=%.2f> has been created.", this->id, filePath, x, y, opacity);
// Only DirectX thread can call safely D3DXCreateTextureFromFile and D3DXCreateSprite.
bb_queue_add (&d3d9ObjectFactory.spriteToInstanciate, this);
D3D9ObjectFactory_release ();
while (sprite->status == D3D9_OBJECT_SPRITE_NOT_READY) {
// Active waiting until the DirectX thread initialize the directx objects
Sleep (1);
}
if (sprite->status == D3D9_OBJECT_SPRITE_ERROR) {
return false;
}
return true;
}
/*
* Description : Set new attribute to D3D9ObjectSprite
* D3D9ObjectText *this : An allocated D3D9ObjectSprite
* float opacity : opacity of the sprite
* Return : void
*/
void
D3D9ObjectSprite_set (
D3D9ObjectSprite *this,
float opacity
) {
this->opacity = (opacity * 255 > 255) ? 255 : opacity * 255;
}
/*
* Description : Initialize D3D9ObjectSprite DirectX objects.
* /!\ This function must be called only from the DirectX thread.
* IDirect3DDevice9 * pDevice : An allocated IDirect3DDevice9
* Return : void
*/
void
D3D9ObjectSprite_init_directx (
IDirect3DDevice9 * pDevice
) {
while (bb_queue_get_length (&d3d9ObjectFactory.spriteToInstanciate))
{
D3D9Object * this = bb_queue_pop (&d3d9ObjectFactory.spriteToInstanciate);
D3D9ObjectSprite * sprite = &this->sprite;
// Create the texture
if ((D3DXCreateTextureFromFileEx (
pDevice,
sprite->filePath,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT,
0,
D3DFMT_UNKNOWN,
D3DPOOL_MANAGED,
D3DX_DEFAULT,
D3DX_DEFAULT,
0,
NULL,
NULL,
&sprite->texture)) != D3D_OK) {
warn ("Cannot create the texture <%s>.", sprite->filePath);
sprite->status = D3D9_OBJECT_SPRITE_ERROR;
continue;
}
D3DSURFACE_DESC surfaceDesc;
sprite->texture->lpVtbl->GetLevelDesc (sprite->texture, 0, &surfaceDesc);
sprite->w = surfaceDesc.Width;
sprite->h = surfaceDesc.Height;
// Create the sprite
if ((D3DXCreateSprite (pDevice, &sprite->sprite)) != D3D_OK) {
warn ("Cannot create the sprite.");
sprite->status = D3D9_OBJECT_SPRITE_ERROR;
continue;
}
D3D9ObjectFactory_add (this);
sprite->status = D3D9_OBJECT_SPRITE_READY;
}
}
/// ===== Drawing utilities =====
/*
* Description : Draw a rectangle at a given position / color on the screen
* D3D9ObjectRect *this : An allocated D3D9ObjectRect
* int x, y : {x, y} position of the rectangle
* IDirect3DDevice9 * pDevice : An allocated d3d9 device
*/
void
D3D9ObjectRect_draw (
D3D9ObjectRect *this,
int x, int y,
IDirect3DDevice9 * pDevice
) {
D3DRECT rectPosition = {x, y, x + this->w, y + this->h};
D3DCOLOR color = D3DCOLOR_RGBA (this->r, this->g, this->b, 255);
pDevice->lpVtbl->Clear (pDevice, 1, &rectPosition, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, color, 0, 0);
}
/*
* Description : Draw text at a given position / color on the screen
* D3D9ObjectText *text : An allocated D3D9ObjectText
* int x, y : {x, y} position of the text
* IDirect3DDevice9 * pDevice : An allocated d3d9 device
*/
void
D3D9ObjectText_draw (
D3D9ObjectText *this,
int x, int y,
IDirect3DDevice9 * pDevice
) {
RECT rect;
ID3DXFont *font = this->font;
D3DCOLOR color = D3DCOLOR_RGBA (this->r, this->g, this->b, this->opacity);
SetRect (&rect, x, y, x, y);
font->lpVtbl->DrawText (font, NULL, this->string, -1, &rect, DT_NOCLIP | DT_LEFT, color);
}
/*
* Description : Draw a sprite at a given position on the screen
* D3D9ObjectSprite *spriteObject : An allocated D3D9ObjectSprite.
* int x, y : {x, y} position of the sprite
* Return : void
*/
void
D3D9ObjectSprite_draw (
D3D9ObjectSprite *this,
int x, int y
) {
ID3DXSprite * sprite = this->sprite;
IDirect3DTexture9 * texture = this->texture;
D3DXVECTOR3 position3D = {x, y, 0.0};
D3DCOLOR color = D3DCOLOR_ARGB (this->opacity, 255, 255, 255);
sprite->lpVtbl->Begin (sprite, D3DXSPRITE_ALPHABLEND);
sprite->lpVtbl->Draw (sprite, texture, NULL, NULL, &position3D, color);
sprite->lpVtbl->End (sprite);
}
/*
* Description : Free an allocated D3D9Object
* /!\ The factory MUST BE LOCKED when calling this function.
* D3D9Object *this : An allocated D3D9Object
* Return : void
*/
void
D3D9Object_free (
D3D9Object *this
) {
switch (this->type)
{
case D3D9_OBJECT_RECTANGLE: {
// Nothing to do
} break;
case D3D9_OBJECT_TEXT: {
ID3DXFont *font = this->text.font;
if (font)
font->lpVtbl->Release (font);
} break;
case D3D9_OBJECT_SPRITE: {
ID3DXSprite * sprite = this->sprite.sprite;
IDirect3DTexture9 * texture = this->sprite.texture;
if (texture)
texture->lpVtbl->Release (texture);
if (sprite)
sprite->lpVtbl->Release (sprite);
free (this->sprite.filePath);
} break;
default : warn ("Cannot free completely an unknown type."); break;
}
free (this);
}