You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

867 lines
30 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "gui_sdl.h"
  2. #include "core/coreWidget.h"
  3. #include "../alg/generic/md5.h"
  4. #include "joystickWrapper.h"
  5. #define LOG( X ) m_pLog->Log( "SDL_GUI", this, X )
  6. #define LOG_ERROR( X ) m_pLog->Log( "SDL_GUI", this, _LOG_ERROR, X )
  7. //Thread Logging
  8. #define SLOG( X ) mThis->m_pLog->Log( "SDL_GUI_S", mThis, X )
  9. #define SLOG_ERROR( X ) mThis->m_pLog->Log( "SDL_GUI_S", mThis, _LOG_ERROR, X )
  10. //https://www.parallelrealities.co.uk/tutorials/
  11. extern "C" {
  12. FILE* __cdecl __iob_func(void) {
  13. FILE _iob[] = { *stdin, *stdout, *stderr };
  14. return _iob;
  15. }
  16. }
  17. static void SDL_LoadWindowIcon(SDL_Window* window){
  18. //https://gist.github.com/noct/9884320
  19. #if defined(_WIN32)
  20. const unsigned int mask_r = 0x00ff0000;
  21. const unsigned int mask_g = 0x0000ff00;
  22. const unsigned int mask_b = 0x000000ff;
  23. const unsigned int mask_a = 0xff000000;
  24. const int res_id = 107;
  25. const int size = 32;
  26. const int bpp = 32;
  27. HICON icon = (HICON)LoadImage(
  28. GetModuleHandle(NULL),
  29. MAKEINTRESOURCE(res_id),
  30. IMAGE_ICON,
  31. size, size,
  32. LR_SHARED
  33. );
  34. if (icon) {
  35. ICONINFO ici;
  36. if (GetIconInfo(icon, &ici)) {
  37. HDC dc = CreateCompatibleDC(NULL);
  38. if (dc) {
  39. SDL_Surface* surface = SDL_CreateRGBSurface(0, size, size, bpp, mask_r, mask_g, mask_b, mask_a);
  40. if (surface) {
  41. BITMAPINFO bmi;
  42. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  43. bmi.bmiHeader.biWidth = size;
  44. bmi.bmiHeader.biHeight = -size;
  45. bmi.bmiHeader.biPlanes = 1;
  46. bmi.bmiHeader.biBitCount = bpp;
  47. bmi.bmiHeader.biCompression = BI_RGB;
  48. bmi.bmiHeader.biSizeImage = 0;
  49. SelectObject(dc, ici.hbmColor);
  50. GetDIBits(dc, ici.hbmColor, 0, size, surface->pixels, &bmi, DIB_RGB_COLORS);
  51. SDL_SetWindowIcon(window, surface);
  52. SDL_FreeSurface(surface);
  53. }
  54. DeleteDC(dc);
  55. }
  56. DeleteObject(ici.hbmColor);
  57. DeleteObject(ici.hbmMask);
  58. }
  59. DestroyIcon(icon);
  60. }
  61. #endif
  62. }
  63. classGUI_SDL * classGUI_SDL::m_pInstance = NULL;
  64. classGUI_SDL * classGUI_SDL::getInstance(void){
  65. if (m_pInstance == NULL){
  66. m_pInstance = new classGUI_SDL();
  67. };
  68. return m_pInstance;
  69. };
  70. classGUI_SDL::classGUI_SDL(): basicThread(){
  71. m_pLog = basicLogCollection::getInstance();
  72. m_pWindow = NULL;
  73. m_pJoystickWraper = NULL;
  74. m_pRenderer = NULL;
  75. m_iCursorPosX = 100;
  76. m_iCursorPosY = 100;
  77. m_nWidth = SCREEN_WIDTH;
  78. m_nHeight = SCREEN_HEIGHT;
  79. m_nBackgroundMode = 1;
  80. m_bWindowSystemOk = false;
  81. m_bInitOk = false;
  82. m_bUseAltBackground = false;
  83. m_pMainLayout = NULL;
  84. m_pProtector = NULL;
  85. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(NULL, (CSTR)"GUI");
  86. m_pBackgroundTexture = NULL;
  87. m_nBackgroundTextureWidth = 0;
  88. m_nBackgroundTextureHeight = 0;
  89. memset(&m_sBackgroundSolidColor, 0, sizeof(SDL_Color));
  90. memset(m_aBackgroundGradientColor, 0, sizeof(uint32_t) * 4);
  91. m_pAltBackgroundTexture = NULL;
  92. m_nAltBackgroundTextureWidth = 0;
  93. m_nAltBackgroundTextureHeight = 0;
  94. memset(&m_sAltBackgroundSolidColor, 0, sizeof(SDL_Color));
  95. memset(m_aAltBackgroundGradientColor, 0, sizeof(uint32_t) * 4);
  96. if (m_pSettings != NULL) {
  97. config_setting_t * pSetting;
  98. pSetting = config_setting_get_member(m_pSettings, "FULL_SCREEN_MODE");
  99. if (pSetting == NULL) {
  100. pSetting = config_setting_add(m_pSettings, "FULL_SCREEN_MODE", CONFIG_TYPE_INT);
  101. m_nWindowMode = 0;
  102. if (pSetting != NULL) {
  103. config_setting_set_int(pSetting, m_nWindowMode);
  104. basicSettingsProvider::getInstance()->compromise();
  105. };
  106. } else {
  107. m_nWindowMode = config_setting_get_int(pSetting);
  108. };
  109. if (m_nWindowMode < 2) { // 0 - Windowed, 1 - custom sized FS 2 - FS with desktop size
  110. pSetting = config_setting_get_member(m_pSettings, "WIDTH");
  111. if (pSetting == NULL) {
  112. pSetting = config_setting_add(m_pSettings, "WIDTH", CONFIG_TYPE_INT);
  113. if (pSetting != NULL) {
  114. config_setting_set_int(pSetting, m_nWidth);
  115. basicSettingsProvider::getInstance()->compromise();
  116. };
  117. } else {
  118. m_nWidth = config_setting_get_int(pSetting);
  119. };
  120. pSetting = config_setting_get_member(m_pSettings, "HEIGHT");
  121. if (pSetting == NULL) {
  122. pSetting = config_setting_add(m_pSettings, "HEIGHT", CONFIG_TYPE_INT);
  123. if (pSetting != NULL) {
  124. config_setting_set_int(pSetting, m_nHeight);
  125. basicSettingsProvider::getInstance()->compromise();
  126. };
  127. } else {
  128. m_nHeight = config_setting_get_int(pSetting);
  129. };
  130. };
  131. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_MODE");
  132. if (pSetting == NULL) {
  133. pSetting = config_setting_add(m_pSettings, "BACKGROUND_MODE", CONFIG_TYPE_INT);
  134. m_nBackgroundMode = 0;
  135. if (pSetting != NULL) {
  136. config_setting_set_int(pSetting, m_nBackgroundMode);
  137. basicSettingsProvider::getInstance()->compromise();
  138. };
  139. } else {
  140. m_nBackgroundMode = config_setting_get_int(pSetting);
  141. };
  142. switch (m_nBackgroundMode) {
  143. case 0: {
  144. uint32_t tVal;
  145. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_SOLID_COLOR");
  146. if (pSetting == NULL) {
  147. pSetting = config_setting_add(m_pSettings, "BACKGROUND_SOLID_COLOR", CONFIG_TYPE_INT);
  148. tVal = 0x3483ae; // Not set Alpha channel!!
  149. if (pSetting != NULL) {
  150. config_setting_set_int(pSetting, tVal);
  151. basicSettingsProvider::getInstance()->compromise();
  152. };
  153. } else {
  154. tVal = config_setting_get_int(pSetting);
  155. };
  156. m_sBackgroundSolidColor.r = (tVal >> 16 ) & 0xFF;
  157. m_sBackgroundSolidColor.g = (tVal >> 8) & 0xFF;
  158. m_sBackgroundSolidColor.b = (tVal >> 0) & 0xFF;
  159. m_sBackgroundSolidColor.a = 0xFF;
  160. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_ALT_SOLID_COLOR");
  161. if (pSetting == NULL) {
  162. pSetting = config_setting_add(m_pSettings, "BACKGROUND_ALT_SOLID_COLOR", CONFIG_TYPE_INT);
  163. tVal = 0x3483ae; // Not set Alpha channel!!
  164. if (pSetting != NULL) {
  165. config_setting_set_int(pSetting, tVal);
  166. basicSettingsProvider::getInstance()->compromise();
  167. };
  168. } else {
  169. tVal = config_setting_get_int(pSetting);
  170. };
  171. m_sAltBackgroundSolidColor.r = (tVal >> 16) & 0xFF;
  172. m_sAltBackgroundSolidColor.g = (tVal >> 8) & 0xFF;
  173. m_sAltBackgroundSolidColor.b = (tVal >> 0) & 0xFF;
  174. m_sAltBackgroundSolidColor.a = 0xFF;
  175. break;
  176. };
  177. case 1: {
  178. uint32_t tVal;
  179. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_GRADIENT_LT_COLOR");
  180. if (pSetting == NULL) {
  181. pSetting = config_setting_add(m_pSettings, "BACKGROUND_GRADIENT_LT_COLOR", CONFIG_TYPE_INT);
  182. tVal = 0x3483ae; // Not set Alpha channel!!
  183. if (pSetting != NULL) {
  184. config_setting_set_int(pSetting, tVal);
  185. basicSettingsProvider::getInstance()->compromise();
  186. };
  187. } else {
  188. tVal = config_setting_get_int(pSetting);
  189. };
  190. m_aBackgroundGradientColor[0] = tVal << 8 | 0xFF;
  191. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_GRADIENT_RT_COLOR");
  192. if (pSetting == NULL) {
  193. pSetting = config_setting_add(m_pSettings, "BACKGROUND_GRADIENT_RT_COLOR", CONFIG_TYPE_INT);
  194. tVal = 0x3483ae; // Not set Alpha channel!!
  195. if (pSetting != NULL) {
  196. config_setting_set_int(pSetting, tVal);
  197. basicSettingsProvider::getInstance()->compromise();
  198. };
  199. } else {
  200. tVal = config_setting_get_int(pSetting);
  201. };
  202. m_aBackgroundGradientColor[1] = tVal << 8 | 0xFF;
  203. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_GRADIENT_LB_COLOR");
  204. if (pSetting == NULL) {
  205. pSetting = config_setting_add(m_pSettings, "BACKGROUND_GRADIENT_LB_COLOR", CONFIG_TYPE_INT);
  206. tVal = 0x0; // Not set Alpha channel!!
  207. if (pSetting != NULL) {
  208. config_setting_set_int(pSetting, tVal);
  209. basicSettingsProvider::getInstance()->compromise();
  210. };
  211. } else {
  212. tVal = config_setting_get_int(pSetting);
  213. };
  214. m_aBackgroundGradientColor[2] = tVal << 8 | 0xFF;
  215. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_GRADIENT_RB_COLOR");
  216. if (pSetting == NULL) {
  217. pSetting = config_setting_add(m_pSettings, "BACKGROUND_GRADIENT_RB_COLOR", CONFIG_TYPE_INT);
  218. tVal = 0x0; // Not set Alpha channel!!
  219. if (pSetting != NULL) {
  220. config_setting_set_int(pSetting, tVal);
  221. basicSettingsProvider::getInstance()->compromise();
  222. };
  223. } else {
  224. tVal = config_setting_get_int(pSetting);
  225. };
  226. m_aBackgroundGradientColor[3] = tVal << 8 | 0xFF;
  227. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_ALT_GRADIENT_LT_COLOR");
  228. if (pSetting == NULL) {
  229. pSetting = config_setting_add(m_pSettings, "BACKGROUND_ALT_GRADIENT_LT_COLOR", CONFIG_TYPE_INT);
  230. tVal = 0x3483ae; // Not set Alpha channel!!
  231. if (pSetting != NULL) {
  232. config_setting_set_int(pSetting, tVal);
  233. basicSettingsProvider::getInstance()->compromise();
  234. };
  235. } else {
  236. tVal = config_setting_get_int(pSetting);
  237. };
  238. m_aAltBackgroundGradientColor[0] = tVal << 8 | 0xFF;
  239. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_ALT_GRADIENT_RT_COLOR");
  240. if (pSetting == NULL) {
  241. pSetting = config_setting_add(m_pSettings, "BACKGROUND_ALT_GRADIENT_RT_COLOR", CONFIG_TYPE_INT);
  242. tVal = 0x3483ae; // Not set Alpha channel!!
  243. if (pSetting != NULL) {
  244. config_setting_set_int(pSetting, tVal);
  245. basicSettingsProvider::getInstance()->compromise();
  246. };
  247. } else {
  248. tVal = config_setting_get_int(pSetting);
  249. };
  250. m_aAltBackgroundGradientColor[1] = tVal << 8 | 0xFF;
  251. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_ALT_GRADIENT_LB_COLOR");
  252. if (pSetting == NULL) {
  253. pSetting = config_setting_add(m_pSettings, "BACKGROUND_ALT_GRADIENT_LB_COLOR", CONFIG_TYPE_INT);
  254. tVal = 0x0; // Not set Alpha channel!!
  255. if (pSetting != NULL) {
  256. config_setting_set_int(pSetting, tVal);
  257. basicSettingsProvider::getInstance()->compromise();
  258. };
  259. } else {
  260. tVal = config_setting_get_int(pSetting);
  261. };
  262. m_aAltBackgroundGradientColor[2] = tVal << 8 | 0xFF;
  263. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_ALT_GRADIENT_RB_COLOR");
  264. if (pSetting == NULL) {
  265. pSetting = config_setting_add(m_pSettings, "BACKGROUND_ALT_GRADIENT_RB_COLOR", CONFIG_TYPE_INT);
  266. tVal = 0x0; // Not set Alpha channel!!
  267. if (pSetting != NULL) {
  268. config_setting_set_int(pSetting, tVal);
  269. basicSettingsProvider::getInstance()->compromise();
  270. };
  271. } else {
  272. tVal = config_setting_get_int(pSetting);
  273. };
  274. m_aAltBackgroundGradientColor[3] = tVal << 8 | 0xFF;
  275. break;
  276. };
  277. default: {
  278. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_PICTURE");
  279. if (pSetting == NULL) {
  280. sprintf(m_pBackgroundFileName, "335566.jpg");
  281. pSetting = config_setting_add(m_pSettings, "BACKGROUND_PICTURE", CONFIG_TYPE_STRING);
  282. if (pSetting != NULL) {
  283. config_setting_set_string(pSetting, m_pBackgroundFileName);
  284. basicSettingsProvider::getInstance()->compromise();
  285. };
  286. } else {
  287. sprintf(m_pBackgroundFileName,"%s",config_setting_get_string(pSetting));
  288. };
  289. pSetting = config_setting_get_member(m_pSettings, "BACKGROUND_ALT_PICTURE");
  290. if (pSetting == NULL) {
  291. sprintf(m_pAltBackgroundFileName, "335566.jpg");
  292. pSetting = config_setting_add(m_pSettings, "BACKGROUND_ALT_PICTURE", CONFIG_TYPE_STRING);
  293. if (pSetting != NULL) {
  294. config_setting_set_string(pSetting, m_pAltBackgroundFileName);
  295. basicSettingsProvider::getInstance()->compromise();
  296. };
  297. } else {
  298. sprintf(m_pAltBackgroundFileName, "%s", config_setting_get_string(pSetting));
  299. };
  300. break;
  301. };
  302. };
  303. basicSettingsProvider::getInstance()->validate();
  304. };
  305. m_bInitOk = basicThread::init();
  306. while ( ( m_bWindowSystemOk == false ) && ( m_bThreadStatus < TH_ENDED ) ) SLEEP(1);
  307. };
  308. classGUI_SDL::~classGUI_SDL(){
  309. m_bWindowSystemOk = false;
  310. m_bInitOk = false;
  311. if( m_pJoystickWraper != NULL)delete(m_pJoystickWraper);
  312. if( m_pProtector != NULL)delete(m_pProtector);
  313. if( m_pRenderer!=NULL){
  314. SDL_DestroyRenderer(m_pRenderer);
  315. };
  316. //if (m_pScreenSurface != NULL){
  317. // //Deallocate surface
  318. // SDL_FreeSurface(m_pScreenSurface);
  319. // m_pScreenSurface = NULL;
  320. //};
  321. if ( m_pWindow!= NULL ){
  322. //Destroy window
  323. SDL_DestroyWindow(m_pWindow);
  324. m_pWindow = NULL;
  325. };
  326. //Quit SDL subsystems
  327. SDL_Quit();
  328. };
  329. //https://www.facebook.com/missing.person.3158
  330. //https://www.facebook.com/guga.wereteli.90
  331. //https://www.facebook.com/mentul.ohmentul
  332. //https://www.facebook.com/profile.php?id=100001772172980
  333. //https://www.facebook.com/egagua
  334. /* Collect Confirmation Codes
  335. Contact your friends and ask them for their help.
  336. 1. Call three of these friends or talk to them in person.
  337. Raphael LlamadoChantebis SamkaroMentul OhmentulEstrada Irish PotchEvgeny Gagua
  338. 2. Ask them to visit :
  339. https ://www.facebook.com/recover
  340. 3. Each friend will give you a code.Enter them below :
  341. */
  342. void classGUI_SDL::m_fThreadProc(void){
  343. if (m_pProtector == NULL) m_pProtector = new basicMutex();
  344. if (m_pProtector == NULL) {
  345. SDL_Log("No Mutex Created! SDL Error: %s", SDL_GetError());
  346. m_bInitOk = false;
  347. m_bThreadStatus = TH_ENDED;
  348. return;
  349. };
  350. if (SDL_Init(SDL_INIT_VIDEO| SDL_INIT_AUDIO| SDL_INIT_JOYSTICK| SDL_INIT_HAPTIC| SDL_INIT_GAMECONTROLLER| SDL_INIT_EVENTS) != 0) {
  351. SDL_Log("SDL_Init Error: %s", SDL_GetError());
  352. m_bInitOk = false;
  353. m_bThreadStatus = TH_ENDED;
  354. return;
  355. };
  356. int display_count = 0, display_index = 0, mode_index = 0;
  357. SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
  358. if ((display_count = SDL_GetNumVideoDisplays()) < 1) {
  359. SDL_Log("SDL_GetNumVideoDisplays returned: %i, %s", display_count, SDL_GetError());
  360. };
  361. SDL_Log("SDL_GetNumVideoDisplays returned: %i", display_count);
  362. //if (SDL_GetDisplayMode(display_index, mode_index, &mode) != 0) {
  363. // SDL_Log("SDL_GetDisplayMode failed: %s", SDL_GetError());
  364. // m_bInitOk = false;
  365. // m_bThreadStatus = TH_ENDED;
  366. // return;
  367. //};
  368. //SDL_Log("SDL_GetDisplayMode(0, 0, &mode):\t\t%i bpp\t%i x %i", SDL_BITSPERPIXEL(mode.format), mode.w, mode.h);
  369. if ((IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG | IMG_INIT_TIF) & (IMG_INIT_PNG | IMG_INIT_JPG | IMG_INIT_TIF))!= (IMG_INIT_PNG | IMG_INIT_JPG | IMG_INIT_TIF)){
  370. SDL_Log("SDL_image could not initialize! SDL_image Error: %s", IMG_GetError());
  371. m_bInitOk = false;
  372. m_bThreadStatus = TH_ENDED;
  373. return;
  374. };
  375. //Initialize SDL_ttf
  376. if( TTF_Init() == -1 ){
  377. SDL_Log("SDL_ttf could not initialize! SDL_ttf Error: %s", TTF_GetError());
  378. m_bInitOk = false;
  379. m_bThreadStatus = TH_ENDED;
  380. return;
  381. } else {
  382. };
  383. m_bInitOk = true;
  384. // Set linear blending (haven't tried this with bilinear...)
  385. SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");//2?
  386. switch (m_nWindowMode) {
  387. case 0: {
  388. if (SDL_CreateWindowAndRenderer(m_nWidth, m_nHeight, SDL_WINDOW_RESIZABLE, &m_pWindow, &m_pRenderer)) {
  389. SDL_Log("Window could not be created! SDL_Error: %s", SDL_GetError());
  390. m_bThreadStatus = TH_ENDED;
  391. m_bWindowSystemOk = false;
  392. return;
  393. } else {
  394. SDL_LoadWindowIcon(m_pWindow);
  395. SDL_GetRendererOutputSize(m_pRenderer, &m_nWidth, &m_nHeight);
  396. m_bThreadStatus = TH_WORKING;
  397. m_bWindowSystemOk = true;
  398. };
  399. break;
  400. };
  401. case 1: {
  402. if (SDL_CreateWindowAndRenderer(m_nWidth, m_nHeight, SDL_WINDOW_FULLSCREEN, &m_pWindow, &m_pRenderer)) {
  403. SDL_Log("Window could not be created! SDL_Error: %s", SDL_GetError());
  404. m_bThreadStatus = TH_ENDED;
  405. m_bWindowSystemOk = false;
  406. return;
  407. } else {
  408. m_bThreadStatus = TH_WORKING;
  409. m_bWindowSystemOk = true;
  410. };
  411. break;
  412. };
  413. default: {
  414. m_nWidth = 0;
  415. m_nHeight = 0;
  416. if (SDL_CreateWindowAndRenderer(m_nWidth, m_nHeight, SDL_WINDOW_FULLSCREEN_DESKTOP, &m_pWindow, &m_pRenderer)) {
  417. SDL_Log("Window could not be created! SDL_Error: %s", SDL_GetError());
  418. m_bThreadStatus = TH_ENDED;
  419. m_bWindowSystemOk = false;
  420. return;
  421. } else {
  422. SDL_LoadWindowIcon(m_pWindow);
  423. SDL_GetRendererOutputSize(m_pRenderer, &m_nWidth, &m_nHeight);
  424. m_bThreadStatus = TH_WORKING;
  425. m_bWindowSystemOk = true;
  426. };
  427. break;
  428. };
  429. };
  430. // Set linear blending (haven't tried this with bilinear...)
  431. //SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
  432. // Create a 4x4 texture to serve as the source for our gradient.
  433. uint32_t* bgpixels;
  434. SDL_Texture* background = SDL_CreateTexture(m_pRenderer, SDL_PIXELFORMAT_RGBA8888,
  435. SDL_TEXTUREACCESS_STREAMING, 4, 4);
  436. uint32_t* altbgpixels;
  437. SDL_Texture* altbackground = SDL_CreateTexture(m_pRenderer, SDL_PIXELFORMAT_RGBA8888,
  438. SDL_TEXTUREACCESS_STREAMING, 4, 4);
  439. // Set up the gradient colors.
  440. // Each 2x2 quadrant of the texture has a separate color:
  441. // AABB
  442. // AABB
  443. // CCDD
  444. // CCDD
  445. int i;
  446. SDL_LockTexture(background, NULL, (void**)(&bgpixels), &i);
  447. bgpixels[0] = m_aBackgroundGradientColor[0]; bgpixels[1] = m_aBackgroundGradientColor[0]; bgpixels[2] = m_aBackgroundGradientColor[1]; bgpixels[3] = m_aBackgroundGradientColor[1];
  448. bgpixels[4] = m_aBackgroundGradientColor[0]; bgpixels[5] = m_aBackgroundGradientColor[0]; bgpixels[6] = m_aBackgroundGradientColor[1]; bgpixels[7] = m_aBackgroundGradientColor[1];
  449. bgpixels[8] = m_aBackgroundGradientColor[2]; bgpixels[9] = m_aBackgroundGradientColor[2];bgpixels[10] = m_aBackgroundGradientColor[3];bgpixels[11] = m_aBackgroundGradientColor[3];
  450. bgpixels[12] = m_aBackgroundGradientColor[2];bgpixels[13] = m_aBackgroundGradientColor[2];bgpixels[14] = m_aBackgroundGradientColor[3];bgpixels[15] = m_aBackgroundGradientColor[3];
  451. SDL_UnlockTexture(background);
  452. SDL_LockTexture(altbackground, NULL, (void**)(&altbgpixels), &i);
  453. altbgpixels[0] = m_aAltBackgroundGradientColor[0]; altbgpixels[1] = m_aAltBackgroundGradientColor[0]; altbgpixels[2] = m_aAltBackgroundGradientColor[1]; altbgpixels[3] = m_aAltBackgroundGradientColor[1];
  454. altbgpixels[4] = m_aAltBackgroundGradientColor[0]; altbgpixels[5] = m_aAltBackgroundGradientColor[0]; altbgpixels[6] = m_aAltBackgroundGradientColor[1]; altbgpixels[7] = m_aAltBackgroundGradientColor[1];
  455. altbgpixels[8] = m_aAltBackgroundGradientColor[2]; altbgpixels[9] = m_aAltBackgroundGradientColor[2];altbgpixels[10] = m_aAltBackgroundGradientColor[3];altbgpixels[11] = m_aAltBackgroundGradientColor[3];
  456. altbgpixels[12] = m_aAltBackgroundGradientColor[2];altbgpixels[13] = m_aAltBackgroundGradientColor[2];altbgpixels[14] = m_aAltBackgroundGradientColor[3];altbgpixels[15] = m_aAltBackgroundGradientColor[3];
  457. SDL_UnlockTexture(altbackground);
  458. if (m_nBackgroundMode > 1) {
  459. SDL_Surface* tSurface;
  460. tSurface = IMG_Load((CSTR)m_pBackgroundFileName);
  461. if (tSurface != NULL) {
  462. m_pBackgroundTexture = SDL_CreateTextureFromSurface(m_pRenderer, tSurface);
  463. if (m_pBackgroundTexture != NULL) {
  464. m_nBackgroundTextureWidth = tSurface->w;
  465. m_nBackgroundTextureHeight = tSurface->h;
  466. };
  467. SDL_FreeSurface(tSurface);
  468. };
  469. tSurface = IMG_Load((CSTR)m_pAltBackgroundFileName);
  470. if (tSurface != NULL) {
  471. m_pAltBackgroundTexture = SDL_CreateTextureFromSurface(m_pRenderer, tSurface);
  472. if (m_pAltBackgroundTexture != NULL) {
  473. m_nAltBackgroundTextureWidth = tSurface->w;
  474. m_nAltBackgroundTextureHeight = tSurface->h;
  475. };
  476. SDL_FreeSurface(tSurface);
  477. };
  478. };
  479. while( m_bThreadStatus < TH_ENDED ){
  480. //Event handler
  481. SDL_Event mEvent;
  482. //Handle events on queue
  483. if( SDL_PollEvent(&mEvent) != 0 ){
  484. m_pProtector->lock();
  485. if (m_vGraphicElements.size()){
  486. for (vectorGEL::iterator index = m_vGraphicElements.begin(); index < m_vGraphicElements.end(); index++) (*index)->dispatch(&mEvent);
  487. };
  488. m_pProtector->unlock();
  489. // Global Window Event handler
  490. switch (mEvent.type) {
  491. case SDL_QUIT:
  492. m_bThreadStatus = TH_ENDED;
  493. break;
  494. case SDL_KEYDOWN:
  495. doKeyDown(&mEvent.key);
  496. break;
  497. case SDL_KEYUP:
  498. doKeyUp(&mEvent.key);
  499. break;
  500. case SDL_JOYDEVICEADDED:
  501. if (m_pJoystickWraper != NULL ) {
  502. m_pJoystickWraper->setController(SDL_JoystickOpen(0));
  503. };
  504. break;
  505. case SDL_JOYDEVICEREMOVED:
  506. if (m_pJoystickWraper != NULL) {
  507. m_pJoystickWraper->setController(NULL);
  508. };
  509. break;
  510. default:
  511. break;
  512. };
  513. };
  514. //if (SDL_LockMutex(m_pProtector) == 0)
  515. {
  516. m_pProtector->lock();
  517. switch (m_nBackgroundMode) {
  518. case 0: { // Solid Color
  519. if (m_bUseAltBackground) {
  520. SDL_SetRenderDrawColor(m_pRenderer, m_sAltBackgroundSolidColor.r, m_sAltBackgroundSolidColor.g, m_sAltBackgroundSolidColor.b, m_sAltBackgroundSolidColor.a);
  521. } else {
  522. SDL_SetRenderDrawColor(m_pRenderer, m_sBackgroundSolidColor.r, m_sBackgroundSolidColor.g, m_sBackgroundSolidColor.b, m_sBackgroundSolidColor.a);
  523. };
  524. SDL_RenderClear(m_pRenderer);
  525. break;
  526. };
  527. case 1: { // Gradient
  528. // Blit it into place with the renderer.
  529. SDL_Rect OP = { 1, 1, 2, 2 };
  530. //SDL_Rect OP = { 0, 0, 4, 4 };
  531. if (m_bUseAltBackground) {
  532. SDL_RenderCopy(m_pRenderer, altbackground, &OP, NULL);
  533. } else {
  534. SDL_RenderCopy(m_pRenderer, background, &OP, NULL);
  535. };
  536. break;
  537. };
  538. case 2: { // Picture scaled
  539. if (m_bUseAltBackground) {
  540. SDL_RenderCopy(m_pRenderer, m_pAltBackgroundTexture, NULL, NULL);
  541. } else {
  542. SDL_RenderCopy(m_pRenderer, m_pBackgroundTexture, NULL, NULL);
  543. };
  544. break;
  545. };
  546. case 3: { // Picture tail
  547. break;
  548. };
  549. case 4: { // Picture Centered
  550. SDL_Rect SrcRect, DstRect;
  551. if (m_bUseAltBackground) {
  552. bool A = (m_nWidth > m_nAltBackgroundTextureWidth);
  553. bool B = (m_nHeight > m_nAltBackgroundTextureHeight);
  554. if (A && B) {
  555. SrcRect.x = SrcRect.y = 0;
  556. DstRect.w = SrcRect.w = m_nAltBackgroundTextureWidth;
  557. DstRect.h = SrcRect.h = m_nAltBackgroundTextureHeight;
  558. DstRect.x = (m_nWidth - m_nAltBackgroundTextureWidth) >> 1;
  559. DstRect.y = (m_nHeight - m_nAltBackgroundTextureHeight) >> 1;
  560. } else if (A) {
  561. SrcRect.x = DstRect.y = 0;
  562. DstRect.w = SrcRect.w = m_nAltBackgroundTextureWidth;
  563. DstRect.h = SrcRect.h = m_nHeight;
  564. DstRect.x = (m_nWidth - m_nAltBackgroundTextureWidth) >> 1;
  565. SrcRect.y = (m_nAltBackgroundTextureHeight - m_nHeight) >> 1;
  566. } else if (B) {
  567. DstRect.x = SrcRect.y = 0;
  568. DstRect.w = SrcRect.w = m_nWidth;
  569. DstRect.h = SrcRect.h = m_nAltBackgroundTextureHeight;
  570. SrcRect.x = (m_nAltBackgroundTextureWidth - m_nWidth) >> 1;
  571. DstRect.y = (m_nHeight - m_nAltBackgroundTextureHeight) >> 1;
  572. } else {
  573. DstRect.x = DstRect.y = 0;
  574. DstRect.w = SrcRect.w = m_nWidth;
  575. DstRect.h = SrcRect.h = m_nHeight;
  576. SrcRect.x = (m_nAltBackgroundTextureWidth - m_nWidth) >> 1;
  577. SrcRect.y = (m_nAltBackgroundTextureHeight - m_nHeight) >> 1;
  578. };
  579. SDL_RenderCopy(m_pRenderer, m_pAltBackgroundTexture, &SrcRect, &DstRect);
  580. } else {
  581. bool A = (m_nWidth > m_nBackgroundTextureWidth);
  582. bool B = (m_nHeight > m_nBackgroundTextureHeight);
  583. if (A && B) {
  584. SrcRect.x = SrcRect.y = 0;
  585. DstRect.w = SrcRect.w = m_nBackgroundTextureWidth;
  586. DstRect.h = SrcRect.h = m_nBackgroundTextureHeight;
  587. DstRect.x = (m_nWidth - m_nBackgroundTextureWidth) >> 1;
  588. DstRect.y = (m_nHeight - m_nBackgroundTextureHeight) >> 1;
  589. } else if (A) {
  590. SrcRect.x = DstRect.y = 0;
  591. DstRect.w = SrcRect.w = m_nBackgroundTextureWidth;
  592. DstRect.h = SrcRect.h = m_nHeight;
  593. DstRect.x = (m_nWidth - m_nBackgroundTextureWidth) >> 1;
  594. SrcRect.y = (m_nBackgroundTextureHeight - m_nHeight) >> 1;
  595. } else if (B) {
  596. DstRect.x = SrcRect.y = 0;
  597. DstRect.w = SrcRect.w = m_nWidth;
  598. DstRect.h = SrcRect.h = m_nBackgroundTextureHeight;
  599. SrcRect.x = (m_nBackgroundTextureWidth - m_nWidth) >> 1;
  600. DstRect.y = (m_nHeight - m_nBackgroundTextureHeight) >> 1;
  601. } else {
  602. DstRect.x = DstRect.y = 0;
  603. DstRect.w = SrcRect.w = m_nWidth;
  604. DstRect.h = SrcRect.h = m_nHeight;
  605. SrcRect.x = (m_nBackgroundTextureWidth - m_nWidth) >> 1;
  606. SrcRect.y = (m_nBackgroundTextureHeight - m_nHeight) >> 1;
  607. };
  608. SDL_RenderCopy(m_pRenderer, m_pBackgroundTexture, &SrcRect, &DstRect);
  609. };
  610. break;
  611. };
  612. };
  613. if (m_vGraphicElements.size()) {
  614. for (vectorGEL::iterator index = m_vGraphicElements.begin(); index < m_vGraphicElements.end(); index++) {
  615. if ((*index)->m_bInitComplete) {
  616. (*index)->render(m_pRenderer);
  617. }
  618. else {
  619. if ((*index)->create())(*index)->render(m_pRenderer);
  620. };
  621. };
  622. };
  623. SDL_RenderPresent(m_pRenderer);
  624. m_pProtector->unlock();
  625. SLEEP(2);
  626. };// else SDL_Log("Failure to lock Mutex! SDL_Error: %s", SDL_GetError());
  627. };
  628. };
  629. //FOXCONN PVB120G12H-P01
  630. //AVC MD413 BA13733B12H
  631. void classGUI_SDL::registerGEL(sdlGraphicElement * tPtr){
  632. while( !m_bWindowSystemOk ) SLEEP(1);
  633. if( tPtr != NULL ){
  634. m_pProtector->lock();
  635. m_vGraphicElements.push_back(tPtr);
  636. m_pProtector->unlock();
  637. };
  638. };
  639. void classGUI_SDL::removeGEL(sdlGraphicElement* tPtr){
  640. if (tPtr != NULL){
  641. m_pProtector->lock();
  642. for (vectorGEL::iterator index = m_vGraphicElements.begin(); index < m_vGraphicElements.end(); index++){
  643. if ((*index) == tPtr){
  644. (*index)->setVisibility(false);
  645. m_vGraphicElements.erase(index);
  646. break;
  647. };
  648. };
  649. m_pProtector->unlock();
  650. };
  651. };
  652. bool classGUI_SDL::setFullScreen( bool status ){
  653. if (m_pWindow == NULL) return false;
  654. if (status){
  655. SDL_SetWindowFullscreen(m_pWindow, SDL_WINDOW_FULLSCREEN );
  656. } else {
  657. SDL_SetWindowFullscreen(m_pWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  658. };
  659. return true;
  660. };
  661. TEXTURE_HEAP_ITEM* classGUI_SDL::loadTexture(char* filename) {
  662. char *md5;
  663. md5 = MD5String(filename);
  664. if ( m_vTextureHeap.size() ) {
  665. for (int i = 0; i < m_vTextureHeap.size(); i++) {
  666. if (strncmp(md5, m_vTextureHeap.at(i)->key, 32) == 0) {
  667. SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "%s Found in Heap [%s]", filename, md5 );
  668. return m_vTextureHeap.at(i);
  669. };
  670. };
  671. };
  672. SDL_Surface * tSurface;
  673. TEXTURE_HEAP_ITEM * tVal;
  674. tSurface = IMG_Load(filename);
  675. if (tSurface != NULL) {
  676. tVal = (TEXTURE_HEAP_ITEM*)malloc(sizeof(TEXTURE_HEAP_ITEM));
  677. if (tVal != NULL) {
  678. memset(tVal, 0, sizeof(TEXTURE_HEAP_ITEM));
  679. SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
  680. tVal->texture = SDL_CreateTextureFromSurface(m_pRenderer, tSurface);
  681. if (tVal->texture != NULL) {
  682. memcpy(tVal->key, md5, 32);
  683. tVal->geopmetry.w = tSurface->w;
  684. tVal->geopmetry.h = tSurface->h;
  685. m_vTextureHeap.push_back(tVal);
  686. return tVal;
  687. } else {
  688. free(tVal);
  689. };
  690. SDL_FreeSurface(tSurface);
  691. };
  692. }
  693. return NULL;
  694. };
  695. TEXTURE_HEAP_ITEM* classGUI_SDL::createTexture(int width, int height){
  696. TEXTURE_HEAP_ITEM* tVal = NULL;
  697. tVal = (TEXTURE_HEAP_ITEM*)malloc(sizeof(TEXTURE_HEAP_ITEM));
  698. if (tVal != NULL) {
  699. memset(tVal, 0, sizeof(TEXTURE_HEAP_ITEM));
  700. tVal->geopmetry.w = width;
  701. tVal->geopmetry.h = height;
  702. tVal->texture = SDL_CreateTexture(m_pRenderer,0, SDL_TEXTUREACCESS_TARGET, width, height);
  703. if (tVal->texture == NULL) {
  704. free(tVal);
  705. tVal = NULL;
  706. };
  707. };
  708. return tVal;
  709. };
  710. TEXTURE_HEAP_ITEM* classGUI_SDL::createYUV420PTexture(int width, int height, bool streaming) {
  711. TEXTURE_HEAP_ITEM* tVal = NULL;
  712. tVal = (TEXTURE_HEAP_ITEM*)malloc(sizeof(TEXTURE_HEAP_ITEM));
  713. if (tVal != NULL) {
  714. memset(tVal, 0, sizeof(TEXTURE_HEAP_ITEM));
  715. tVal->geopmetry.w = width;
  716. tVal->geopmetry.h = height;
  717. if (streaming) {
  718. tVal->texture = SDL_CreateTexture(m_pRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING | SDL_TEXTUREACCESS_TARGET, width, height);
  719. } else {
  720. tVal->texture = SDL_CreateTexture(m_pRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_TARGET, width, height);
  721. };
  722. if (tVal->texture == NULL) {
  723. free(tVal);
  724. tVal = NULL;
  725. };
  726. };
  727. return tVal;
  728. };
  729. SDL_Surface* classGUI_SDL::loadSurface(CSTR path) {
  730. //The final optimized image
  731. SDL_Surface* optimizedSurface = NULL;
  732. //Load image at specified path
  733. SDL_Surface* loadedSurface = IMG_Load(path);
  734. if (loadedSurface != NULL) {
  735. //Convert surface to screen format
  736. optimizedSurface = SDL_ConvertSurface(loadedSurface, SDL_GetWindowSurface(m_pWindow)->format, 0);
  737. if (optimizedSurface == NULL) {
  738. SDL_Log("Unable to optimize image %s! SDL Error: %s", path, SDL_GetError());
  739. };
  740. //Get rid of old loaded surface
  741. SDL_FreeSurface(loadedSurface);
  742. };
  743. return optimizedSurface;
  744. };
  745. void classGUI_SDL::doKeyDown(SDL_KeyboardEvent* event) {
  746. if (event->repeat == 0) {
  747. if (event->keysym.scancode == SDL_SCANCODE_LCTRL){
  748. LOG("Left CTRL pressed");
  749. }
  750. if (event->keysym.scancode == SDL_SCANCODE_UP) {
  751. LOG("Key Up pressed");
  752. //app.up = 1;
  753. };
  754. if (event->keysym.scancode == SDL_SCANCODE_DOWN) {
  755. LOG("Key Down pressed");
  756. //app.down = 1;
  757. };
  758. if (event->keysym.scancode == SDL_SCANCODE_LEFT) {
  759. LOG("Key Left pressed");
  760. //app.left = 1;
  761. };
  762. if (event->keysym.scancode == SDL_SCANCODE_RIGHT) {
  763. LOG("Key Right pressed");
  764. //app.right = 1;
  765. };
  766. };
  767. };
  768. void classGUI_SDL::doKeyUp(SDL_KeyboardEvent* event) {
  769. if (event->repeat == 0) {
  770. if (event->keysym.scancode == SDL_SCANCODE_ESCAPE) {
  771. LOG("Key Escape released");
  772. m_bThreadStatus = TH_ENDED;
  773. }
  774. if (event->keysym.scancode == SDL_SCANCODE_HOME) {
  775. LOG("Key Home released");
  776. //app.up = 0;
  777. //app.down = 0;
  778. //app.left = 0;
  779. //app.right = 0;
  780. m_iCursorPosY = 120;
  781. m_iCursorPosX = 120;
  782. };
  783. if (event->keysym.scancode == SDL_SCANCODE_LCTRL){
  784. LOG("Left CTRL released");
  785. //app.fire = 0;
  786. }
  787. if (event->keysym.scancode == SDL_SCANCODE_UP) {
  788. LOG("Key Up released");
  789. //app.up = 0;
  790. };
  791. if (event->keysym.scancode == SDL_SCANCODE_DOWN) {
  792. LOG("Key Down released");
  793. //app.down = 0;
  794. };
  795. if (event->keysym.scancode == SDL_SCANCODE_LEFT) {
  796. LOG("Key Left released");
  797. //app.left = 0;
  798. };
  799. if (event->keysym.scancode == SDL_SCANCODE_RIGHT) {
  800. LOG("Key Right released");
  801. //app.right = 0;
  802. };
  803. };
  804. };
  805. void classGUI_SDL::registerJoystickWraper(sdlJoystickWrapper* Ptr) {
  806. m_pJoystickWraper = Ptr;
  807. if (m_pJoystickWraper != NULL) {
  808. if (SDL_NumJoysticks() < 1) {
  809. SDL_Log("SDL Warning: No joysticks connected!");
  810. } else {
  811. m_pJoystickWraper->setController(SDL_JoystickOpen(0));
  812. };
  813. };
  814. };
  815. void classGUI_SDL::setLayout(sdlCoreLayout* ll) {
  816. if (ll == NULL)return;
  817. if (m_pMainLayout == NULL) {
  818. m_pMainLayout = ll;
  819. } else {
  820. };
  821. };
  822. #undef LOG
  823. #undef LOG_ERROR
  824. #undef SLOG
  825. #undef SLOG_ERROR