-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathisEngineSDLWrapper.cpp
1469 lines (1326 loc) · 48.7 KB
/
isEngineSDLWrapper.cpp
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
is::Engine (Infinity Solutions Engine)
Copyright (C) 2018-2024 Is Daouda <isdaouda.n@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "isEngineSDLWrapper.h"
#if defined(IS_ENGINE_SDL_2)
namespace is
{
SDL_Window *IS_ENGINE_SDL_window = NULL;
SDL_Renderer *IS_ENGINE_SDL_renderer = NULL;
SDL_DisplayMode IS_ENGINE_SDL_displayMode;
bool IS_ENGINE_MOBILE_OS(false);
float IS_ENGINE_SDL_screenXScale(1.f);
float IS_ENGINE_SDL_screenYScale(1.f);
short IS_ENGINE_SDL_channel[IS_ENGINE_SDL_CHANNEL_MAX] = {-1};
TouchData IS_ENGINE_SDL_touchData[IS_ENGINE_SDL_TOUCH_ID_COUNT_MAX];
bool IS_ENGINE_SDL_enableFINGERMOTION = true;
short IS_ENGINE_SDL_touchIdLast = 0;
short IS_ENGINE_SDL_touchIdCount = 0;
std::vector<sf::Font*> IS_ENGINE_SDL_AUTO_GENERATE_FONT;
bool SDL2initLib()
{
if (IS_ENGINE_SDL_window == NULL || IS_ENGINE_SDL_renderer == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "init Windows / Renderer : %s\n", SDL_GetError());
return false;
}
if (SDL_GetCurrentDisplayMode( 0, &IS_ENGINE_SDL_displayMode) != 0)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Display Mode : %s\n", SDL_GetError());
}
SDL_SetRenderDrawBlendMode(IS_ENGINE_SDL_renderer, SDL_BLENDMODE_BLEND);
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "init Image : %s\n", IMG_GetError());
return false;
}
if (TTF_Init() < 0)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "init Font : %s\n", TTF_GetError());
return false;
}
// On Android, the use of .wav type music files is not yet supported
#if !defined(__ANDROID__)
int audioFlags = MIX_INIT_OGG;
if ((Mix_Init(audioFlags) & audioFlags) != audioFlags)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "init Mix : %s\n", Mix_GetError());
return false;
}
#endif
const int frequency =
#if defined(IS_ENGINE_HTML_5)
EM_ASM_INT_V({
var context;
try
{
context = new AudioContext();
}
catch(e)
{
context = new webkitAudioContext();
}
return context.sampleRate;
});
// Check if it is mobile platform
int tempBoolToInt = EM_ASM_INT
(
let ua = navigator.userAgent ;
if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) {
return 1;
}
else if (/Mobile|Android|iP(hone|od)|IEMobile|BlackBerry|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(ua)) {
return 1;
}
return 0;
);
IS_ENGINE_MOBILE_OS = (tempBoolToInt == 1) ? true : false;
#else
22050; //44100;
#endif
if (Mix_OpenAudio(frequency, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "init OpenAudio : %s\n", Mix_GetError());
}
Mix_AllocateChannels(IS_ENGINE_SDL_CHANNEL_MAX);
return true;
}
void SDL2freeLib()
{
Mix_CloseAudio();
Mix_Quit();
SDL_DestroyWindow(IS_ENGINE_SDL_window);
is::IS_ENGINE_SDL_window = NULL;
SDL_DestroyRenderer(IS_ENGINE_SDL_renderer);
is::IS_ENGINE_SDL_renderer = NULL;
IMG_Quit();
for (unsigned int _I(0); _I < IS_ENGINE_SDL_AUTO_GENERATE_FONT.size(); _I++)
{
delete IS_ENGINE_SDL_AUTO_GENERATE_FONT[_I];
IS_ENGINE_SDL_AUTO_GENERATE_FONT[_I] = 0;
}
TTF_Quit();
}
}
namespace sf
{
int SoundBuffer::SDL_sndChannel = 0; // Represents the channel of each sound
sf::Color Color::White = sf::Color(255, 255, 255, 255);
sf::Color Color::Black = sf::Color(0, 0, 0, 255);
sf::Color Color::Grey = sf::Color(127, 127, 127, 255);
sf::Color Color::Red = sf::Color(255, 0, 0, 255);
sf::Color Color::Green = sf::Color(0, 255, 0, 255);
sf::Color Color::Blue = sf::Color(0, 0, 255, 255);
sf::Color Color::Yellow = sf::Color(255, 255, 0, 255);
sf::Color Color::Magenta = sf::Color(255, 0, 255, 255);
sf::Color Color::Cyan = sf::Color(0, 255, 255, 255);
sf::Color Color::Transparent = sf::Color(0, 0, 0, 0);
Texture::~Texture()
{
if (m_SDLsurface != NULL)
{
SDL_FreeSurface(m_SDLsurface);
m_SDLsurface = NULL;
}
}
bool Texture::loadSurface(const std::string& filePath)
{
m_filename = filePath;
m_SDLsurface = IMG_Load(m_filename.c_str());
if (m_SDLsurface != NULL)
{
m_size.x = m_SDLsurface->w;
m_size.y = m_SDLsurface->h;
}
else
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Texture : %s\n \"%s\"", SDL_GetError(), m_filename.c_str());
return false;
}
return true;
}
Font::~Font()
{
if (m_SDLfont != NULL)
{
TTF_CloseFont(m_SDLfont);
m_SDLfont = NULL;
}
}
bool Font::loadFont(const std::string& filename)
{
m_filename = filename;
m_SDLfont = TTF_OpenFont(m_filename.c_str(), m_size);
if (m_SDLfont == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Font : %s \"%s\"\n", TTF_GetError(), filename.c_str());
return false;
}
return true;
}
Transformable::Transformable()
{
is::setVector2(m_size, 0.f, 0.f);
is::setVector2(m_scale, 1.f, 1.f);
is::setVector2(m_origin, 0.f, 0.f);
}
Transformable::Transformable(Texture &texture) :
m_texture(&(texture))
{
is::setVector2(m_size, m_texture->getSize().x, m_texture->getSize().y);
is::setVector2(m_scale, 1.f, 1.f);
is::setVector2(m_origin, 0.f, 0.f);
}
void Transformable::setRotation(float angle)
{
m_rotation = static_cast<float>(fmod(angle, 360));
if (m_rotation < 0)
m_rotation += 360.f;
}
void Transformable::setColor(int r, int g, int b, int a)
{
m_color.r = r;
m_color.g = g;
m_color.b = b;
if (a >= 0 && a <= 255) m_color.a = a;
}
const SDL_Color& Transformable::getSDLColor(bool getAlpha)
{
m_SDLcolor.r = m_color.r;
m_SDLcolor.g = m_color.g;
m_SDLcolor.b = m_color.b;
m_SDLcolor.a = ((getAlpha) ? m_color.a : 0);
return m_SDLcolor;
}
SDLTexture::~SDLTexture()
{
if (m_SDLtexture != NULL)
{
SDL_DestroyTexture(m_SDLtexture);
m_SDLtexture = NULL;
}
if (m_SDLoutlineTexture != NULL)
{
SDL_DestroyTexture(m_SDLoutlineTexture);
m_SDLoutlineTexture = NULL;
}
}
void SDLTexture::setTextureRect(IntRect rec)
{
m_textureRec.left = rec.left;
m_textureRec.top = rec.top;
m_textureRec.width = rec.width;
m_textureRec.height = rec.height;
is::setVector2(m_size, m_textureRec.width, m_textureRec.height);
}
void Sprite::setTexture(sf::Texture& texture)
{
m_texture = &texture;
setSDLTexture();
}
void Sprite::setSDLTexture()
{
if (m_texture->getSDLSurface() != NULL)
{
if (m_SDLtexture != NULL)
{
SDL_DestroyTexture(m_SDLtexture);
m_SDLtexture = NULL;
}
m_SDLtexture = SDL_CreateTextureFromSurface(is::IS_ENGINE_SDL_renderer, m_texture->getSDLSurface());
setTextureRect({0, 0, (int)m_texture->getSize().x, (int)m_texture->getSize().y});
}
}
Image::~Image()
{
if (m_texture != nullptr)
{
delete m_texture;
m_texture = nullptr;
}
}
bool Image::loadFromFile(const std::string& filename)
{
if (m_texture != nullptr)
{
delete m_texture;
m_texture = nullptr;
}
m_texture = new Texture(filename);
if (m_texture == nullptr) return false;
if (m_texture->getSDLSurface() != NULL)
{
m_SDLtexture = SDL_CreateTextureFromSurface(is::IS_ENGINE_SDL_renderer, m_texture->getSDLSurface());
setSize(m_texture->getSize().x, m_texture->getSize().y);
}
return true;
}
const Uint8* Image::getPixelsPtr() const
{
int pitch;
void *pixels;
SDL_LockTexture(m_SDLtexture, NULL, &pixels, &pitch);
Uint8 *upixels = (Uint8*) pixels;
memcpy(pixels, upixels, (pitch / 4) * m_texture->getSize().y);
SDL_UnlockTexture(m_SDLtexture);
return upixels;
}
Text::Text(sf::Font& font) :
SDLTexture()
{
m_SDLTextureType = IS_ENGINE_SDL_TEXT;
m_font = &font;
m_characterSize = m_font->getSize();
m_style = m_font->m_SDLFontStyle;
m_outlineFont = m_font;
}
Text::Text(sf::Font& font, const std::string& text) :
SDLTexture()
{
m_SDLTextureType = IS_ENGINE_SDL_TEXT;
m_font = &font;
m_characterSize = m_font->getSize();
m_style = m_font->m_SDLFontStyle;
m_outlineFont = m_font;
setObjectText(text);
}
Text::Text(sf::Font& font, const std::wstring& text):
SDLTexture()
{
m_SDLTextureType = IS_ENGINE_SDL_TEXT;
m_font = &font;
m_characterSize = m_font->getSize();
m_style = m_font->m_SDLFontStyle;
m_outlineFont = m_font;
setObjectText(text);
}
Text::~Text()
{
if (m_SDLtexture != NULL)
{
SDL_DestroyTexture(m_SDLtexture);
m_SDLtexture = NULL;
}
if (m_SDLtext != nullptr)
{
delete m_SDLtext;
m_SDLtext = nullptr;
}
}
void Text::setFont(sf::Font &font)
{
m_font = &font;
m_characterSize = m_font->getSize();
m_style = m_font->m_SDLFontStyle;
if (m_outlineFont == nullptr) m_outlineFont = m_font;
setSDLText();
}
void Text::setString(const std::wstring& text)
{
setObjectText(text);
}
void Text::setString(const wchar_t& text)
{
m_tempWstring = text;
setObjectText(m_tempWstring);
}
void Text::setString(const std::string& text)
{
setObjectText(text);
}
void Text::setString(const char& text)
{
m_tempString = text;
setObjectText(m_tempString);
}
void Text::setColor(int r, int g, int b, int a)
{
if (a >= 0 && a <= 255) m_color.a = a;
if (m_color.r != r || m_color.g != g || m_color.b != b)
{
m_color.r = r;
m_color.g = g;
m_color.b = b;
setSDLText();
}
}
void Text::setOrigin(float x, float y)
{
is::setVector2(m_origin, x, y);
}
void Text::setCharacterSize(int size)
{
if (m_characterSize != size)
{
m_characterSize = size;
setSDLText();
}
}
void Text::setStyle(Uint32 style)
{
if (m_style != style)
{
m_style = style;
setSDLText();
}
}
void Text::setOutlineColor(const Color& color)
{
if (m_outlineColor.r != color.r || m_outlineColor.g != color.g || m_outlineColor.b != color.b ||
m_outlineColor.a != color.a)
{
m_outlineColor = color;
setSDLText();
}
}
void Text::setOutlineThickness(float thickness)
{
if (m_outlineThickness != thickness)
{
m_outlineThickness = thickness;
setSDLText();
}
}
void Text::setObjectText(const std::string& text)
{
if (text != m_string)
{
m_string = text;
if (m_SDLtext != nullptr)
{
delete m_SDLtext;
m_SDLtext = nullptr;
}
m_currentCharSize = text.length() + 1;
m_SDLtext = new char[m_currentCharSize];
strcpy(m_SDLtext, text.c_str());
m_SDLtext[m_currentCharSize - 1] = '\0';
setSDLText();
}
}
void Text::setObjectText(const std::wstring& text)
{
if (text != m_wstring)
{
m_wstring = text;
if (m_SDLtext != nullptr)
{
delete m_SDLtext;
m_SDLtext = nullptr;
}
m_currentCharSize = text.length() + 1;
m_SDLtext = new char[m_currentCharSize];
std::wcstombs(m_SDLtext, text.c_str(), text.size());
m_SDLtext[m_currentCharSize - 1] = L'\0';
setSDLText();
}
}
bool Text::setSDLText()
{
if (m_string == "" && m_wstring == L"") return false;
auto checkFontParam = [this](sf::Font *font, bool normalText)
{
if (m_characterSize != font->getSize() ||
m_style != font->m_SDLFontStyle ||
(!normalText && m_outlineThickness != font->m_SDLoutlineFontSize))
{
bool fontExists(false);
for (unsigned int _I(0); _I < is::IS_ENGINE_SDL_AUTO_GENERATE_FONT.size(); _I++)
{
if (is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[_I]->getFileName() == font->getFileName() &&
is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[_I]->getSize() == m_characterSize &&
is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[_I]->m_SDLFontStyle == m_style &&
is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[_I]->m_SDLoutlineFontSize == m_outlineThickness)
{
if (normalText) m_font = is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[_I];
else m_outlineFont = is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[_I];
fontExists = true;
break;
}
}
if (!fontExists)
{
is::IS_ENGINE_SDL_AUTO_GENERATE_FONT.push_back(new Font(font->getFileName(), m_characterSize));
is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[is::IS_ENGINE_SDL_AUTO_GENERATE_FONT.size() - 1]->m_SDLFontStyle = m_style;
TTF_SetFontStyle(is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[is::IS_ENGINE_SDL_AUTO_GENERATE_FONT.size() - 1]->getSDLFont(), m_style);
if (!normalText)
{
is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[is::IS_ENGINE_SDL_AUTO_GENERATE_FONT.size() - 1]->m_SDLoutlineFontSize = m_outlineThickness;
TTF_SetFontOutline(is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[is::IS_ENGINE_SDL_AUTO_GENERATE_FONT.size() - 1]->getSDLFont(), m_outlineThickness);
m_outlineFont = is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[is::IS_ENGINE_SDL_AUTO_GENERATE_FONT.size() - 1];
}
else m_font = is::IS_ENGINE_SDL_AUTO_GENERATE_FONT[is::IS_ENGINE_SDL_AUTO_GENERATE_FONT.size() - 1];
}
}
};
checkFontParam(m_font, true);
if (m_outlineThickness > 0.f) checkFontParam(m_outlineFont, false);
if (m_SDLtext == nullptr) return false;
m_multiLines = false;
short line(0), maxCaracter(0), caracter(0);
char currentStr[100];
char finalStr[100];
for (size_t i(0); i < strlen(m_SDLtext); i++)
{
if (m_SDLtext[i] != '\n')
{
if (m_SDLtext[i] == '.') currentStr[caracter] = '-';
else if (m_SDLtext[i] == ' ' && m_SDLcontainMultiSpaces) currentStr[caracter] = '_';
else currentStr[caracter] = m_SDLtext[i];
}
caracter++;
if (m_SDLtext[i] == '\n' || (i == strlen(m_SDLtext) - 1 && m_multiLines))
{
currentStr[caracter - 1] = '\0';
if (caracter > maxCaracter)
{
strcpy(finalStr, currentStr);
maxCaracter = caracter;
m_multiLines = true;
}
caracter = 0;
line++;
}
}
int w(0), h(0);
if (line > 0)
{
if (TTF_SizeText((m_outlineThickness == 0) ? m_font->getSDLFont() : m_outlineFont->getSDLFont(), finalStr, &w, &h)) {/* allow to show error */}
w += ((m_characterSize > 30) ? 6 : 0) + m_SDLaddTextRecWSize;
}
auto createTexture = [this, &line, &w, &h](SDL_Surface *surface, bool normalText)
{
surface = NULL;
surface = TTF_RenderText_Blended_Wrapped((normalText) ? m_font->getSDLFont() : m_outlineFont->getSDLFont(),
m_SDLtext,
(normalText) ? getSDLColor(false) : getSDLOutlineColor(),
((line == 0) ? 1280 : w));
if (surface != NULL)
{
SDL_Texture *texture = NULL;
if (normalText)
{
if (m_SDLtexture != NULL)
{
SDL_DestroyTexture(m_SDLtexture);
m_SDLtexture = NULL;
}
m_SDLtexture = SDL_CreateTextureFromSurface(is::IS_ENGINE_SDL_renderer, surface);
texture = m_SDLtexture;
}
else
{
if (m_SDLoutlineTexture != NULL)
{
SDL_DestroyTexture(m_SDLoutlineTexture);
m_SDLoutlineTexture = NULL;
}
m_SDLoutlineTexture = SDL_CreateTextureFromSurface(is::IS_ENGINE_SDL_renderer, surface);
texture = m_SDLoutlineTexture;
}
if (texture != NULL)
{
if (normalText) setSize(surface->w, surface->h);
{
m_SDLoutlineTextureRec.width = surface->w;
m_SDLoutlineTextureRec.height = surface->h;
}
/*if (m_multiLines)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Text: w: %f h: %f \n<%s> line: %d char: %d\n",
m_size.x, m_size.y, finalStr, line, maxCaracter);
}*/
SDL_FreeSurface(surface);
surface = NULL;
}
else
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Texture Text (\"%s\") : %s\n", m_SDLtext, SDL_GetError());
return false;
}
}
else
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Surface Text (\"%s\") : %s\n", m_SDLtext, TTF_GetError());
return false;
}
return true;
};
bool createdSuccessfully = createTexture(m_SDLsurface, true);
if (m_outlineThickness > 0) createdSuccessfully = createTexture(m_SDLsurface, false);
return createdSuccessfully;
}
View::View()
{
setSize(640.f, 480.f);
setCenter(320.f, 240.f);
}
View::View(const Vector2f& center, const Vector2f& size)
{
setSize(size.x, size.y);
setCenter(center.x, center.y);
}
void View::setCenter(float x, float y)
{
is::setVector2(m_center, x, y);
}
void View::setSize(float width, float height)
{
is::setVector2(m_size, width, height);
}
const Vector2f& View::getCenter() const noexcept
{
return m_center;
}
void RectangleShape::draw(View const &view)
{
SDL_Rect rec;
SDL_SetRenderDrawColor(is::IS_ENGINE_SDL_renderer, m_color.r, m_color.g, m_color.b, m_color.a);
rec.x = ((m_position.x - m_origin.x) - (view.getCenter().x - (view.getSize().x / 2.f))) * is::IS_ENGINE_SDL_screenXScale;
rec.y = ((m_position.y - m_origin.y) - (view.getCenter().y - (view.getSize().y / 2.f))) * is::IS_ENGINE_SDL_screenYScale;
rec.w = (m_size.x * std::abs(m_scale.x)) * is::IS_ENGINE_SDL_screenXScale;
rec.h = (m_size.y * std::abs(m_scale.y)) * is::IS_ENGINE_SDL_screenYScale;
SDL_RenderFillRect(is::IS_ENGINE_SDL_renderer, &rec);
if (m_outlineThickness > 0.f)
{
for (int i(0); i < static_cast<int>(m_outlineThickness); i++)
{
SDL_Rect recOutline;
SDL_SetRenderDrawColor(is::IS_ENGINE_SDL_renderer, m_outlineColor.r, m_outlineColor.g, m_outlineColor.b, m_outlineColor.a);
recOutline.x = rec.x + i;
recOutline.y = rec.y + i;
recOutline.w = rec.w - i * 2;
recOutline.h = rec.h - i * 2;
SDL_RenderDrawRect(is::IS_ENGINE_SDL_renderer, &recOutline);
}
}
}
void CircleShape::draw(View const &view)
{
auto drawCircle = [this, &view](sf::Color const &color, int size)
{
SDL_SetRenderDrawColor(is::IS_ENGINE_SDL_renderer, color.r, color.g, color.b, color.a);
for (int w = 0; w < size * 2; w++)
{
for (int h = 0; h < size * 2; h++)
{
int dx = size - w; // horizontal offset
int dy = size - h; // vertical offset
if ((dx * dx + dy * dy) <= (size * size))
{
SDL_RenderDrawPoint(is::IS_ENGINE_SDL_renderer,
((m_position.x - m_origin.x) - (view.getCenter().x - (view.getSize().x / 2.f))) * is::IS_ENGINE_SDL_screenXScale + dx,
((m_position.y - m_origin.y) - (view.getCenter().y - (view.getSize().y / 2.f))) * is::IS_ENGINE_SDL_screenYScale + dy);
}
}
}
};
if (m_outlineThickness > 0.f) drawCircle(m_outlineColor, m_size.x);
drawCircle(m_color, m_size.x - static_cast<int>(m_outlineThickness));
}
bool Keyboard::isKeyPressed(Key key)
{
const Uint8 *state = SDL_GetKeyboardState(NULL);
// These keys have the same ID
if (key == Return) key = Enter;
if (key == Numpad0) key = Num0;
if (key == Numpad1) key = Num1;
if (key == Numpad2) key = Num2;
if (key == Numpad3) key = Num3;
if (key == Numpad4) key = Num4;
if (key == Numpad5) key = Num5;
if (key == Numpad6) key = Num6;
if (key == Numpad7) key = Num7;
if (key == Numpad8) key = Num8;
if (key == Numpad9) key = Num9;
if (key == Numpad9) key = Num9;
if (key == BackSpace) key = Backspace;
if (key == BackSlash) key = Backslash;
if (key == SemiColon) key = Semicolon;
switch (key)
{
case A: if(state[SDL_SCANCODE_A]) return true; break;
case B: if(state[SDL_SCANCODE_B]) return true; break;
case C: if(state[SDL_SCANCODE_C]) return true; break;
case D: if(state[SDL_SCANCODE_D]) return true; break;
case E: if(state[SDL_SCANCODE_E]) return true; break;
case F: if(state[SDL_SCANCODE_F]) return true; break;
case G: if(state[SDL_SCANCODE_G]) return true; break;
case H: if(state[SDL_SCANCODE_H]) return true; break;
case I: if(state[SDL_SCANCODE_I]) return true; break;
case J: if(state[SDL_SCANCODE_J]) return true; break;
case K: if(state[SDL_SCANCODE_K]) return true; break;
case L: if(state[SDL_SCANCODE_L]) return true; break;
case M: if(state[SDL_SCANCODE_M]) return true; break;
case N: if(state[SDL_SCANCODE_N]) return true; break;
case O: if(state[SDL_SCANCODE_O]) return true; break;
case P: if(state[SDL_SCANCODE_P]) return true; break;
case Q: if(state[SDL_SCANCODE_Q]) return true; break;
case R: if(state[SDL_SCANCODE_R]) return true; break;
case S: if(state[SDL_SCANCODE_S]) return true; break;
case T: if(state[SDL_SCANCODE_T]) return true; break;
case U: if(state[SDL_SCANCODE_U]) return true; break;
case V: if(state[SDL_SCANCODE_V]) return true; break;
case W: if(state[SDL_SCANCODE_W]) return true; break;
case X: if(state[SDL_SCANCODE_X]) return true; break;
case Y: if(state[SDL_SCANCODE_Y]) return true; break;
case Z: if(state[SDL_SCANCODE_Z]) return true; break;
case Num0: if(state[SDL_SCANCODE_0]) return true; break;
case Num1: if(state[SDL_SCANCODE_1]) return true; break;
case Num2: if(state[SDL_SCANCODE_2]) return true; break;
case Num3: if(state[SDL_SCANCODE_3]) return true; break;
case Num4: if(state[SDL_SCANCODE_4]) return true; break;
case Num5: if(state[SDL_SCANCODE_5]) return true; break;
case Num6: if(state[SDL_SCANCODE_6]) return true; break;
case Num7: if(state[SDL_SCANCODE_7]) return true; break;
case Num8: if(state[SDL_SCANCODE_8]) return true; break;
case Num9: if(state[SDL_SCANCODE_9]) return true; break;
case Escape: if(state[SDL_SCANCODE_ESCAPE]) return true; break;
case LControl: if(state[SDL_SCANCODE_LCTRL]) return true; break;
case LShift: if(state[SDL_SCANCODE_LSHIFT]) return true; break;
case LAlt: if(state[SDL_SCANCODE_LALT]) return true; break;
//case LSystem: if(state[SDL_SCANCODE_LEFT_SUPER]) return true; break;
case RControl: if(state[SDL_SCANCODE_RCTRL]) return true; break;
case RShift: if(state[SDL_SCANCODE_RSHIFT]) return true; break;
case RAlt: if(state[SDL_SCANCODE_RALT]) return true; break;
//case RSystem: if(state[SDL_SCANCODE_RIGHT_SUPER]) return true; break;
case Menu: if(state[SDL_SCANCODE_MENU]) return true; break;
case LBracket: if(state[SDL_SCANCODE_LEFTBRACKET]) return true; break;
case RBracket: if(state[SDL_SCANCODE_RIGHTBRACKET]) return true; break;
case Semicolon: if(state[SDL_SCANCODE_SEMICOLON]) return true; break;
case Comma: if(state[SDL_SCANCODE_COMMA]) return true; break;
case Period: if(state[SDL_SCANCODE_PERIOD]) return true; break;
case Slash: if(state[SDL_SCANCODE_SLASH]) return true; break;
case Backslash: if(state[SDL_SCANCODE_BACKSLASH]) return true; break;
case Equal: if(state[SDL_SCANCODE_EQUALS]) return true; break;
case Space: if(state[SDL_SCANCODE_SPACE]) return true; break;
case Enter:
// case Return: same value as Enter
if(state[SDL_SCANCODE_RETURN]) return true;
break;
case Backspace: if(state[SDL_SCANCODE_BACKSPACE]) return true; break;
case Tab: if(state[SDL_SCANCODE_TAB]) return true; break;
case PageUp: if(state[SDL_SCANCODE_PAGEUP]) return true; break;
case PageDown: if(state[SDL_SCANCODE_PAGEDOWN]) return true; break;
case End: if(state[SDL_SCANCODE_END]) return true; break;
case Home: if(state[SDL_SCANCODE_HOME]) return true; break;
case Insert: if(state[SDL_SCANCODE_INSERT]) return true; break;
case Delete: if(state[SDL_SCANCODE_DELETE]) return true; break;
//case Add: if(state[SDL_SCANCODE_KP_ADD]) return true; break;
//case Subtract: if(state[SDL_SCANCODE_KP_SUBTRACT]) return true; break;
case Multiply: if(state[SDL_SCANCODE_KP_MULTIPLY]) return true; break;
case Divide: if(state[SDL_SCANCODE_KP_DIVIDE]) return true; break;
case Left: if(state[SDL_SCANCODE_LEFT]) return true; break;
case Right: if(state[SDL_SCANCODE_RIGHT]) return true; break;
case Up: if(state[SDL_SCANCODE_UP]) return true; break;
case Down: if(state[SDL_SCANCODE_DOWN]) return true; break;
/*
case Numpad0: if(state[SDL_SCANCODE_0]) return true; break;
case Numpad1: if(state[SDL_SCANCODE_1]) return true; break;
case Numpad2: if(state[SDL_SCANCODE_2]) return true; break;
case Numpad3: if(state[SDL_SCANCODE_3]) return true; break;
case Numpad4: if(state[SDL_SCANCODE_4]) return true; break;
case Numpad5: if(state[SDL_SCANCODE_5]) return true; break;
case Numpad6: if(state[SDL_SCANCODE_6]) return true; break;
case Numpad7: if(state[SDL_SCANCODE_7]) return true; break;
case Numpad8: if(state[SDL_SCANCODE_8]) return true; break;
case Numpad9: if(state[SDL_SCANCODE_9]) return true; break;
*/
case F1: if(state[SDL_SCANCODE_F1]) return true; break;
case F2: if(state[SDL_SCANCODE_F2]) return true; break;
case F3: if(state[SDL_SCANCODE_F3]) return true; break;
case F4: if(state[SDL_SCANCODE_F4]) return true; break;
case F5: if(state[SDL_SCANCODE_F5]) return true; break;
case F6: if(state[SDL_SCANCODE_F6]) return true; break;
case F7: if(state[SDL_SCANCODE_F7]) return true; break;
case F8: if(state[SDL_SCANCODE_F8]) return true; break;
case F9: if(state[SDL_SCANCODE_F9]) return true; break;
case F10: if(state[SDL_SCANCODE_F10]) return true; break;
case F11: if(state[SDL_SCANCODE_F11]) return true; break;
case F12: if(state[SDL_SCANCODE_F12]) return true; break;
case F13: if(state[SDL_SCANCODE_F13]) return true; break;
case F14: if(state[SDL_SCANCODE_F14]) return true; break;
case F15: if(state[SDL_SCANCODE_F15]) return true; break;
case Pause: if(state[SDL_SCANCODE_PAUSE]) return true; break;
/*
case BackSpace: if(state[SDL_SCANCODE_BACKSPACE]) return true; break;
case BackSlash: if(state[SDL_SCANCODE_BACKSLASH]) return true; break;
case SemiColon: if(state[SDL_SCANCODE_SEMICOLON]) return true; break;
*/
default: return false; break;
}
return false;
}
VideoMode VideoMode::getDesktopMode()
{
VideoMode videoMode;
videoMode.width = is::IS_ENGINE_SDL_displayMode.w;
videoMode.height = is::IS_ENGINE_SDL_displayMode.h;
return videoMode;
}
RenderWindow::~RenderWindow()
{
if (m_SDLiconSurface != NULL)
{
SDL_FreeSurface(m_SDLiconSurface);
m_SDLiconSurface = NULL;
}
}
void RenderWindow::create(VideoMode videoMode, const std::string& title, int style)
{
is::setVector2(m_size, videoMode.width, videoMode.height);
m_view.setSize(videoMode.width, videoMode.height);
m_title = title;
m_style = style;
int w, h;
#if defined(__ANDROID__)
w = is::IS_ENGINE_SDL_displayMode.w;
h = is::IS_ENGINE_SDL_displayMode.h;
#else
SDL_GetWindowSize(is::IS_ENGINE_SDL_window, &w, &h);
#endif
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "init SDL Video : %s\n", SDL_GetError());
is::closeApplication();
}
// Allows to calculate the scale of the screen
is::IS_ENGINE_SDL_screenXScale = w / m_view.getSize().x;
is::IS_ENGINE_SDL_screenYScale = h / m_view.getSize().y;
is::IS_ENGINE_SDL_window = SDL_CreateWindow(m_title.c_str(),
#if !defined(__ANDROID__)
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_size.x, m_size.y, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
#else
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, m_size.x, m_size.y, SDL_WINDOW_OPENGL
#endif
);
is::IS_ENGINE_SDL_renderer = SDL_CreateRenderer(is::IS_ENGINE_SDL_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!is::SDL2initLib()) is::closeApplication();
}
void RenderWindow::setFramerateLimit(int fps)
{
// m_windowFrameLimit = 1000 / fps;
m_windowFrameLimit = (fps * 50) / 60;
}
void RenderWindow::setSize(const Vector2u& size)
{
is::setVector2(m_size, size.x, size.y);
SDL_SetWindowSize(is::IS_ENGINE_SDL_window, size.x, size.y);
}
void RenderWindow::setTitle(const std::string& text)
{
m_title = text;
SDL_SetWindowTitle(is::IS_ENGINE_SDL_window, m_title.c_str());
}
void RenderWindow::setView(const View& view)
{
m_view = view;
// When we change the size of the view we recalculate the scale of the screen
int w, h;
#if defined(__ANDROID__)
w = is::IS_ENGINE_SDL_displayMode.w;
h = is::IS_ENGINE_SDL_displayMode.h;
#else
SDL_GetWindowSize(is::IS_ENGINE_SDL_window, &w, &h);
#endif
is::IS_ENGINE_SDL_screenXScale = w / m_view.getSize().x;
is::IS_ENGINE_SDL_screenYScale = h / m_view.getSize().y;
}
void RenderWindow::setPosition(Vector2i position)
{
SDL_SetWindowPosition(is::IS_ENGINE_SDL_window, position.x, position.y);
}
void RenderWindow::setPosition(int x, int y)
{
SDL_SetWindowPosition(is::IS_ENGINE_SDL_window, x, y);
}
void RenderWindow::setVerticalSyncEnabled(bool vsync)
{
SDL_GL_SetSwapInterval(vsync);
}
void RenderWindow::setIcon(Uint32 width, Uint32 height, const Uint8* pixels)
{
// This part is automatically managed when we define the icon in the "resource.rc" file
}
void RenderWindow::clear(sf::Color const &color)
{
SDL_SetRenderDrawColor(is::IS_ENGINE_SDL_renderer, color.r, color.g, color.b, color.a);
SDL_RenderClear(is::IS_ENGINE_SDL_renderer);
}
void RenderWindow::draw(SDLTexture &obj)
{
if (obj.getSDLTexture() == NULL) return;
SDL_Rect rec;
SDL_Rect recSrc;
SDL_Point point;
// We keep the value of the origin x and y of the object to be drawn.
// This allows to keep the value of the original variables intact during recalculations.
float objOriginX((obj.getScale().x < 0.f /*&& static_cast<int>(obj.getOrigin().y) == 0*/ &&
static_cast<int>(obj.getOrigin().x) == 0) ? obj.getTextureRect().width : obj.getOrigin().x);
float objOriginY((obj.getScale().y < 0.f /*&& static_cast<int>(obj.getOrigin().x) == 0*/ &&
static_cast<int>(obj.getOrigin().y) == 0) ? obj.getTextureRect().height : obj.getOrigin().y);
float xOrigin(objOriginX);
float yOrigin(objOriginY);
// Image angle
float rotation(obj.getRotation());
// Use to stretch the image of the object to make a scale effect
rec.w = obj.getTextureRect().width * std::abs(obj.getScale().x);
rec.h = obj.getTextureRect().height * std::abs(obj.getScale().y);
// Each time the image is scale, the origin of the object is recalculated
if (std::abs(obj.getScale().x) > 1.001f || std::abs(obj.getScale().x) < 0.999f)
xOrigin = rec.w / (obj.getTextureRect().width / objOriginX);
if (std::abs(obj.getScale().y) > 1.001f || std::abs(obj.getScale().y) < 0.999f)
yOrigin = rec.h / (obj.getTextureRect().height / objOriginY);
// We do a corresponding flip according to the sign of the variables x scale and y scale
if (obj.getScale().x < 0.f && obj.getScale().y < 0.f)
{
obj.m_SDLFlip = (SDL_RendererFlip)(SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);
if (static_cast<int>(obj.getOrigin().x) != static_cast<int>(obj.getTextureRect().width / 2) &&
static_cast<int>(obj.getOrigin().x) != 0)
xOrigin += obj.getTextureRect().width;
if (static_cast<int>(obj.getOrigin().y) != static_cast<int>(obj.getTextureRect().height / 2) &&
static_cast<int>(obj.getOrigin().y) != 0)