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.

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