forked from MSDehghan/SBDL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SBDL.h
646 lines (571 loc) · 16.7 KB
/
SBDL.h
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
/**
* SBDL: Sadegh & Borjian Directmedia Layer!
*/
#include <string>
#if defined(_WIN32) || defined(_WIN64) // Windows
#pragma once
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"
#elif defined(__linux__) // linux
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_ttf.h"
#include "SDL2/SDL_mixer.h"
#endif
#undef main
/**
* represent a Sound
* */
using Sound = Mix_Chunk;
/**
* represent a Music
* */
using Music = Mix_Music;
/**
* represent a Font
* */
using Font = TTF_Font;
/**
* Texture living on the graphics card that can be used for drawing.
*/
struct Texture {
/**
* underneath texture which is loaded with SDL
* don't use it directly in your code
*/
SDL_Texture *underneathTexture = nullptr;
/**
* width of this Texture
* */
int width;
/**
* height of this Texture
* */
int height;
};
namespace SBDL {
/**
* don't import this namespace
* this namespace used for handle underneath SDL functions for you
*/
namespace Core {
/**
* current state of SDL
*/
bool running = true;
/**
* SDL keyboard state array size
*/
int keystate_size = -1;
/**
* SDL current keyboard state
*/
const Uint8 *keystate = nullptr;
/**
* SDL last keyboard state
*/
Uint8 *old_keystate = nullptr;
/**
* SDL current event
*/
SDL_Event event;
/**
* SDL windows
*/
SDL_Window *window = nullptr;
/**
* SDL renderer
*/
SDL_Renderer *renderer = nullptr;
/**
* create texture with given features
* @param path path of texture
* @param changeColor true if given color must be replaced with transparent color
* @param r red color
* @param g green color
* @param b blue color
* @param alpha transparency level
* @return texture which is created
*/
Texture loadTextureUnderneath(const std::string &path, bool changeColor, Uint8 r, Uint8 g, Uint8 b,
Uint8 alpha = 255) {
// Check existence of image
SDL_Surface *pic = IMG_Load(path.c_str());
if (pic == nullptr) {
const std::string message = "Missing Image file: " + path;
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SBDL load image error", message.c_str(), nullptr);
exit(1);
}
if (changeColor)
SDL_SetColorKey(pic, SDL_TRUE, SDL_MapRGB(pic->format, r, g, b));
if (alpha != 255)
SDL_SetSurfaceAlphaMod(pic, alpha);
Texture newTexture;
newTexture.underneathTexture = SDL_CreateTextureFromSurface(renderer, pic);
newTexture.width = pic->w;
newTexture.height = pic->h;
SDL_SetTextureBlendMode(newTexture.underneathTexture, SDL_BLENDMODE_BLEND);
SDL_FreeSurface(pic);
return newTexture;
}
}
/**
* comparator of SDL_Rect
*/
bool operator==(const SDL_Rect &x, const SDL_Rect &y) {
return x.x == y.x && x.y == y.y && x.h == y.h && x.w == y.w;
}
/**
* a structure which give useful information about mouse state
* updateEvents must be call before using this structure
*/
struct Mouse {
/**
* x position of Mouse
*/
int x;
/**
* y position of Mouse
*/
int y;
/**
* left button state
*/
bool left;
/**
* right button state
*/
bool right;
/**
* middle button state
*/
bool middle;
/**
* state of Mouse < SDL_PRESSED or SDL_RELEASED >
*/
Uint8 state;
/**
* button clicks
*/
Uint8 clicks;
/**
* for SDL backward compatibility
*/
Uint8 button;
/**
* Check if Mouse clicked with given conditions.
* @param button button to check <SDL_BUTTON_LEFT,SDL_BUTTON_RIGHT,SDL_BUTTON_MIDDLE>
* @param clicks number of clicks
* @param state sate of mouse <SDL_PRESSED,SDL_RELEASED>
*/
bool clicked(Uint8 button = SDL_BUTTON_LEFT, Uint8 clicks = 1, Uint8 state = SDL_PRESSED) {
return this->button == button && this->clicks == clicks && this->state == state;
}
} Mouse;
/**
* check state of program
* @return state of SDL
*/
bool isRunning() {
return Core::running;
}
/**
* stop SBDL and SBDL::isRunning will return false later
*/
void stop() {
Core::running = false;
}
/**
* indicate whether a key with specific scanCode was pressed
* @param scanCode specific code for each keyboard button (https://wiki.libsdl.org/SDL_Scancode)
* @return true if specific keyboard button was pressed
*/
bool keyPressed(SDL_Scancode scanCode) {
return !Core::old_keystate[scanCode] && Core::keystate[scanCode];
}
/**
* indicate whether a key with specific scanCode was released
* @param scanCode specific code for each keyboard button (https://wiki.libsdl.org/SDL_Scancode)
* @return true if specific keyboard button was released
*/
bool keyReleased(SDL_Scancode scanCode) {
return Core::old_keystate[scanCode] && !Core::keystate[scanCode];
}
/**
* indicate whether a key with specific scanCode is hold
* @param scanCode specific code for each keyboard button (https://wiki.libsdl.org/SDL_Scancode)
* @return true if specific keyboard button is hold
*/
bool keyHeld(SDL_Scancode scanCode) {
return Core::old_keystate[scanCode] && Core::keystate[scanCode];
}
/**
* initialize SDL and show a simple empty window for drawing texture on it
* before start using SDL functions and types, first initialize engine
* @param windowsTitle title of window
* @param windowsWidth width of window
* @param windowsHeight height of window
* @param r red color of default background
* @param g green color of default background
* @param b blue color of default background
*/
void InitEngine(const std::string &windowsTitle, int windowsWidth, int windowsHeight,
Uint8 r = 255, Uint8 g = 255, Uint8 b = 255) {
atexit(SDL_Quit); // set a SDL_Quit as exit function
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SBDL initialization", "SBDL initialize video engine error",
nullptr);
exit(1);
}
SDL_CreateWindowAndRenderer(windowsWidth, windowsHeight, SDL_WINDOW_SHOWN, &Core::window, &Core::renderer);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); // make the scaled rendering look smoother
SDL_RenderSetLogicalSize(Core::renderer, windowsWidth, windowsHeight);
SDL_SetRenderDrawColor(Core::renderer, r, g, b, 255);
SDL_SetRenderDrawBlendMode(Core::renderer, SDL_BLENDMODE_BLEND);
SDL_SetWindowTitle(Core::window, windowsTitle.c_str());
// inilialize SDL_mixer, exit if fail
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SBDL initialization", "SBDL initialize audio engine error",
nullptr);
exit(1);
}
// setup audio mode
Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 640);
// setup text system
TTF_Init();
}
/**
* update state of keyboard buttons (release or push) and mouse
* if ESCAPE pressed, application will stop
* call this function in a loop after initialize engine for get updated state all times
*/
void updateEvents() {
// update keyboard state
if (Core::keystate_size == -1) {
Core::keystate = SDL_GetKeyboardState(&Core::keystate_size);
Core::old_keystate = new Uint8[Core::keystate_size];
for (int i = 0; i < Core::keystate_size; i++)
Core::old_keystate[i] = 0;
}
else {
for (int i = 0; i < Core::keystate_size; i++)
Core::old_keystate[i] = Core::keystate[i];
Core::keystate = SDL_GetKeyboardState(&Core::keystate_size);
}
SDL_PumpEvents();
// close app if ESCAPE pressed
if (keyPressed(SDL_SCANCODE_ESCAPE)) {
Core::running = false;
}
// reset event handler state for check it again
Core::event = {};
// returns true if there is an event in the queue, but will not remove it
if (!SDL_PollEvent(nullptr)) {
Mouse.left = Mouse.middle = Mouse.right = false;
Mouse.button = 0;
return;
}
while (SDL_PollEvent(&Core::event)) { // loop until there is a new event for handling
if (Core::event.type == SDL_MOUSEBUTTONDOWN || Core::event.type == SDL_MOUSEBUTTONUP) {
// update state of Mouse structure if it was changed
switch (Core::event.button.button) {
case 1:
Mouse.left = true;
Mouse.right = Mouse.middle = false;
break;
case 2:
Mouse.middle = true;
Mouse.right = Mouse.left = false;
break;
case 3:
Mouse.right = true;
Mouse.left = Mouse.middle = false;
break;
default:
Mouse.left = Mouse.middle = Mouse.right = false;
}
Mouse.state = Core::event.button.state;
Mouse.button = Core::event.button.button;
Mouse.clicks = Core::event.button.clicks;
}
// update position of mouse if it was changed
if (Core::event.type == SDL_MOUSEMOTION) {
Mouse.x = Core::event.motion.x;
Mouse.y = Core::event.motion.y;
}
if (Core::event.type == SDL_QUIT) {
Core::running = false;
}
}
}
/**
* get Milliseconds since program was started.
*/
unsigned int getTime() {
return SDL_GetTicks();
}
/**
* clear the current rendering target
*/
void clearRenderScreen() {
SDL_RenderClear(Core::renderer);
}
/**
* update the screen and apply all changes
*/
void updateRenderScreen() {
SDL_RenderPresent(Core::renderer);
}
/**
* wait a few milliseconds before continue process of application
* @param frameRate set the dalay (milisecond)
*/
void delay(Uint32 frameRate) {
SDL_Delay(frameRate);
}
/**
* load the font from a file
* @param path path of the font file to load
* @param size size of font
* @return font which is loaded
*/
Font *loadFont(const std::string &path, int size) {
Font *font;
font = TTF_OpenFont(path.c_str(), size);
if (font == nullptr) {
const std::string message = "Unable to load: " + path;
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SBDL load font error", message.c_str(), nullptr);
exit(1);
}
return font;
}
/**
* load the texture from a file on disk
* @param path path of the image file to load
* @param alpha transparency level
* @return texture which is loaded
*/
Texture loadTexture(const std::string &path, Uint8 alpha = 255) {
return Core::loadTextureUnderneath(path, false, 0, 0, 0, alpha);
}
/**
* load the texture from a file on disk and replace transparency of image with specific color
* @param path path of the image file to load
* @param r red color
* @param g green color
* @param b blue color
* @param alpha transparency level
* @return texture which is loaded
*/
Texture loadTexture(const std::string &path, Uint8 r, Uint8 g, Uint8 b, Uint8 alpha = 255) {
return Core::loadTextureUnderneath(path, true, r, g, b, alpha);
}
/**
* play sound
* multiple sound can play concurrently
* @param sound sound which is loaded before
* @param count frequency of sound (-1 to play all time)
* @see loadSound
*/
void playSound(Sound *sound, int count) {
if (count != 0)
Mix_PlayChannel(-1, sound, (count > 0) ? count - 1 : -1);
}
/**
* play music
* only one music file can play
* @param music music which is loaded before
* @param count frequency of msuic (-1 to play all time)
* @see loadMusic
*/
void playMusic(Music *music, int count) {
Mix_PlayMusic(music, count);
}
/**
* load sound from a file in disk (use .wav)
* @param path path of the sound file to load
* @return sound which is loaded
*/
Sound *loadSound(const std::string &path) {
Sound *sound;
sound = Mix_LoadWAV(path.c_str());
if (sound == nullptr) {
const std::string message = "Unable to load: " + path;
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SBDL load sound error", message.c_str(), nullptr);
exit(1);
}
return sound;
}
/**
* load music from a file in disk (use .ogg or .wav)
* @param path path of the music file to load
* @return music which is loaded
*/
Music *loadMusic(const std::string &path) {
Music *music;
music = Mix_LoadMUS(path.c_str());
if (music == nullptr) {
const std::string message = "Unable to load: " + path;
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SBDL load music error", message.c_str(), nullptr);
exit(1);
}
return music;
}
/**
* stop music
*/
void stopMusic() {
Mix_HaltMusic();
}
/**
* stop all sounds
*/
void stopAllSounds() {
Mix_HaltChannel(-1);
}
/**
* free memory which is used for load sound from file
* @param sound Sound which you want to destroy
*/
void freeSound(Sound *sound) {
Mix_FreeChunk(sound);
}
/**
* free memory which is used for load music from file
* @param music Music which you want to destroy
*/
void freeMusic(Music *music) {
Mix_FreeMusic(music);
}
/**
* free memory which is used for texture
* After call this function, texture is not usable anymore and any using has undefined behavior
* @param texture Texture which you want to destroy
*/
void freeTexture(Texture &texture) {
SDL_DestroyTexture(texture.underneathTexture);
texture.underneathTexture = nullptr;
texture.width = 0;
texture.height = 0;
}
/**
* texture showed in render screen in position destRect with angle and flip
* @param texture the source texture
* @param angle an angle in degrees that indicates the rotation that will be applied to texture, rotating it in a clockwise direction around center of texture
* @param destRect custom rect to draw texture
* @param flip flipping actions performed on the texture (SDL_FLIP_NONE or SDL_FLIP_HORIZONTAL or SDL_FLIP_VERTICAL)
*/
void showTexture(const Texture &texture, double angle, const SDL_Rect &destRect,
SDL_RendererFlip flip = SDL_FLIP_NONE) {
SDL_RenderCopyEx(Core::renderer, texture.underneathTexture, nullptr, &destRect, angle, nullptr,
flip);
}
/**
* texture showed in render screen in position texture.rect with angle and flip
* @param texture the source texture
* @param x position x
* @param y position y
* @param angle an angle in degrees that indicates the rotation that will be applied to texture, rotating it
in a clockwise direction around center of texture
* @param flip flipping actions performed on the texture (SDL_FLIP_NONE or SDL_FLIP_HORIZONTAL or SDL_FLIP_VERTICAL)
*/
void showTexture(const Texture &texture, int x, int y, double angle, SDL_RendererFlip flip = SDL_FLIP_NONE) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = texture.width;
rect.h = texture.height;
showTexture(texture, angle, rect, flip);
}
/**
* texture showed in render screen in position destRect
* @param texture the source texture
* @param destRect custom rect to draw texture
*/
void showTexture(const Texture &texture, const SDL_Rect &destRect) {
SDL_RenderCopy(Core::renderer, texture.underneathTexture, nullptr, &destRect);
}
/**
* texture showed in render screen in position texture.rect
* @param texture the source texture
* @param x position x
* @param y position y
*/
void showTexture(const Texture &texture, int x, int y) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = texture.width;
rect.h = texture.height;
showTexture(texture, rect);
}
/**
* create a texture from a font for a special string with specific color whcih can be drawed in render window
* @param font font which is loaded
* @param text text that convert to texture
* @param r red color
* @param g green color
* @param b blue color
* @return texture which created with that font and text
*/
Texture createFontTexture(Font *font, const std::string &text, Uint8 r, Uint8 g, Uint8 b) {
SDL_Color color;
color.r = r;
color.g = g;
color.b = b;
SDL_Surface *temp = TTF_RenderText_Solid(font, text.c_str(), color);
Texture newTexture;
newTexture.underneathTexture = SDL_CreateTextureFromSurface(Core::renderer, temp);
newTexture.width = temp->w;
newTexture.height = temp->h;
SDL_FreeSurface(temp);
return newTexture;
}
/**
* check intersection of two SDL_Rect
* @param firstRect first rectangle
* @param secondRect second rectangle
* @return true if has intersection
*/
bool hasIntersectionRect(const SDL_Rect &firstRect, const SDL_Rect &secondRect) {
return SDL_HasIntersection(&firstRect, &secondRect) == SDL_TRUE;
}
/**
* Draw rectangle on renderer screen.
* @param rect rectangle position
* @param r red color
* @param g green color
* @param b blue color
* @param alpha transparency
*/
void drawRectangle(const SDL_Rect &rect, Uint8 r, Uint8 g, Uint8 b, Uint8 alpha = 255) {
Uint8 defaults[4];
SDL_GetRenderDrawColor(Core::renderer, &defaults[0], &defaults[1], &defaults[2], &defaults[3]);
SDL_SetRenderDrawColor(Core::renderer, r, g, b, alpha);
SDL_RenderFillRect(Core::renderer, &rect);
SDL_SetRenderDrawColor(Core::renderer, defaults[0], defaults[1], defaults[2], defaults[3]);
}
/**
* Check if a point is inside a Rect
* @param x
* @param y
* @param rect the rectangle to check with
* @return true if point is inside the rectangle
*/
bool pointInRect(int x, int y, const SDL_Rect &rect) {
SDL_Point point;
point.x = x;
point.y = y;
return SDL_PointInRect(&point, &rect) == SDL_TRUE;
}
/**
* Check if mouse is inside a rect
* @param rect check when mouse is inside this rectangle
* @return true if mouse is inside the rectangle
*/
bool mouseInRect(const SDL_Rect &rect) {
return pointInRect(Mouse.x, Mouse.y, rect);
}
}