SdlBFApp.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. #include "SdlBFApp.h"
  2. #include "GLRenderDevice.h"
  3. #include "SDL.h"
  4. USING_NS_BF;
  5. ///
  6. #pragma comment(lib, "imm32.lib")
  7. #pragma comment(lib, "version.lib")
  8. SdlBFWindow::SdlBFWindow(BFWindow* parent, const StringImpl& title, int x, int y, int width, int height, int windowFlags)
  9. {
  10. int sdlWindowFlags = 0;
  11. if (windowFlags & BFWINDOW_RESIZABLE)
  12. sdlWindowFlags |= SDL_WINDOW_RESIZABLE;
  13. sdlWindowFlags |= SDL_WINDOW_OPENGL;
  14. #ifdef BF_PLATFORM_FULLSCREEN
  15. sdlWindowFlags |= SDL_WINDOW_FULLSCREEN;
  16. #endif
  17. mSDLWindow = SDL_CreateWindow(title.c_str(), x, y, width, height, sdlWindowFlags);
  18. if (!SDL_GL_CreateContext(mSDLWindow))
  19. {
  20. BF_FATAL(StrFormat("Unable to create OpenGL context: %s", SDL_GetError()).c_str());
  21. SDL_Quit();
  22. exit(2);
  23. }
  24. glEnable(GL_BLEND);
  25. //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  26. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  27. //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  28. #ifndef BF_PLATFORM_OPENGL_ES2
  29. glEnableClientState(GL_INDEX_ARRAY);
  30. #endif
  31. //glEnableClientState(GL_VERTEX_ARRAY);
  32. //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  33. //glEnableClientState(GL_COLOR_ARRAY);
  34. mIsMouseInside = false;
  35. mRenderWindow = new GLRenderWindow((GLRenderDevice*)gBFApp->mRenderDevice, mSDLWindow);
  36. mRenderWindow->mWindow = this;
  37. gBFApp->mRenderDevice->AddRenderWindow(mRenderWindow);
  38. if (parent != NULL)
  39. parent->mChildren.push_back(this);
  40. }
  41. SdlBFWindow::~SdlBFWindow()
  42. {
  43. if (mSDLWindow != NULL)
  44. TryClose();
  45. }
  46. bool SdlBFWindow::TryClose()
  47. {
  48. SdlBFApp* app = (SdlBFApp*)gBFApp;
  49. SdlWindowMap::iterator itr = app->mSdlWindowMap.find(SDL_GetWindowID(mSDLWindow));
  50. app->mSdlWindowMap.erase(itr);
  51. SDL_DestroyWindow(mSDLWindow);
  52. mSDLWindow = NULL;
  53. return true;
  54. }
  55. //LRESULT SdlBFWindow::WindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  56. //{
  57. // SdlBFApp* app = (SdlBFApp*) gBFApp;
  58. //
  59. // switch (Msg)
  60. // {
  61. // case WM_CLOSE:
  62. // {
  63. // if (mCloseQueryFunc(this) != 0)
  64. // gBFApp->RemoveWindow(this);
  65. // return 0;
  66. // }
  67. // break;
  68. // case WM_DESTROY:
  69. // /*if (mFlags & BFWINDOW_QUIT_ON_CLOSE)
  70. // {
  71. // gBFApp->mRunning = false;
  72. // }*/
  73. // mHWnd = NULL;
  74. // break;
  75. // }
  76. //
  77. // LRESULT result = 0;
  78. // bool doResult = false;
  79. //
  80. // if (!app->mInMsgProc)
  81. // {
  82. // app->mInMsgProc = true;
  83. //
  84. // switch (Msg)
  85. // {
  86. //
  87. // case WM_SIZE:
  88. // mRenderWindow->Resized();
  89. // if (mMovedFunc != NULL)
  90. // mMovedFunc(this);
  91. // break;
  92. // case WM_PAINT:
  93. // break;
  94. // case WM_LBUTTONDOWN:
  95. // case WM_RBUTTONDOWN:
  96. // case WM_MBUTTONDOWN:
  97. // case WM_LBUTTONDBLCLK:
  98. // case WM_RBUTTONDBLCLK:
  99. // case WM_LBUTTONUP:
  100. // case WM_RBUTTONUP:
  101. // case WM_MBUTTONUP:
  102. // case WM_MOUSEWHEEL:
  103. // case WM_MOUSEMOVE:
  104. // {
  105. // int x = (short) LOWORD(lParam);
  106. // int y = (short) HIWORD(lParam);
  107. //
  108. // bool releaseCapture = false;
  109. //
  110. // POINT point = {x, y};
  111. // ::ClientToScreen(hWnd, &point);
  112. // HWND windowAtPoint = ::WindowFromPoint(point);
  113. //
  114. // bool isMouseOver = windowAtPoint == hWnd;
  115. //
  116. // if ((!mIsMouseInside) && (isMouseOver))
  117. // {
  118. // TRACKMOUSEEVENT tme;
  119. // tme.cbSize = sizeof(TRACKMOUSEEVENT);
  120. // tme.dwFlags = TME_LEAVE;
  121. // tme.hwndTrack = hWnd;
  122. // TrackMouseEvent(&tme);
  123. // mIsMouseInside = true;
  124. // }
  125. //
  126. // if ((mIsMouseInside) && (!isMouseOver))
  127. // {
  128. // mIsMouseInside = false;
  129. // mMouseLeaveFunc(this);
  130. // }
  131. //
  132. // switch (Msg)
  133. // {
  134. // case WM_LBUTTONDOWN:
  135. // SetCapture(hWnd);
  136. // mMouseDownFunc(this, x, y, 0, 1);
  137. // break;
  138. // case WM_RBUTTONDOWN:
  139. // SetCapture(hWnd);
  140. // mMouseDownFunc(this, x, y, 1, 1);
  141. // break;
  142. // case WM_MBUTTONDOWN:
  143. // SetCapture(hWnd);
  144. // mMouseDownFunc(this, x, y, 2, 1);
  145. // break;
  146. // case WM_LBUTTONDBLCLK:
  147. // SetCapture(hWnd);
  148. // mMouseDownFunc(this, x, y, 0, 2);
  149. // break;
  150. // case WM_RBUTTONDBLCLK:
  151. // SetCapture(hWnd);
  152. // mMouseDownFunc(this, x, y, 1, 2);
  153. // break;
  154. // case WM_MBUTTONDBLCLK:
  155. // SetCapture(hWnd);
  156. // mMouseDownFunc(this, x, y, 2, 2);
  157. // break;
  158. // case WM_LBUTTONUP:
  159. // releaseCapture = true;
  160. // mMouseUpFunc(this, x, y, 0);
  161. // break;
  162. // case WM_RBUTTONUP:
  163. // releaseCapture = true;
  164. // mMouseUpFunc(this, x, y, 1);
  165. // break;
  166. // case WM_MBUTTONUP:
  167. // releaseCapture = true;
  168. // mMouseUpFunc(this, x, y, 2);
  169. // break;
  170. // case WM_MOUSEWHEEL:
  171. // {
  172. // POINT pt = {x, y};
  173. // ScreenToClient(mHWnd, &pt);
  174. //
  175. // int delta = ((int16)HIWORD(wParam)) / 120;
  176. // mMouseWheelFunc(this, pt.x, pt.y, delta);
  177. // }
  178. // break;
  179. // case WM_MOUSEMOVE:
  180. // {
  181. // mMouseMoveFunc(this, x, y);
  182. //
  183. // if ((wParam != 0) && (gBFApp->mWindowList.size() > 1))
  184. // {
  185. // // See if our mouse is down and has entered into another window's space
  186. // POINT point = {x, y};
  187. // ::ClientToScreen(hWnd, &point);
  188. //
  189. // HWND windowAtPoint = ::WindowFromPoint(point);
  190. //
  191. // BFWindowList::iterator itr = gBFApp->mWindowList.begin();
  192. // while (itr != gBFApp->mWindowList.end())
  193. // {
  194. // SdlBFWindow* aWindow = (SdlBFWindow*) *itr;
  195. // if (aWindow != this)
  196. // {
  197. // if (aWindow->mHWnd == windowAtPoint)
  198. // {
  199. // POINT clientPt = point;
  200. // ::ScreenToClient(aWindow->mHWnd, &clientPt);
  201. // aWindow->mMouseProxyMoveFunc(this, clientPt.x, clientPt.y);
  202. // aWindow->mIsMouseInside = true;
  203. // }
  204. // else if (aWindow->mIsMouseInside)
  205. // {
  206. // aWindow->mMouseLeaveFunc(this);
  207. // aWindow->mIsMouseInside = false;
  208. // }
  209. // }
  210. // ++itr;
  211. // }
  212. // }
  213. // }
  214. // break;
  215. // }
  216. //
  217. // if (releaseCapture)
  218. // {
  219. // ReleaseCapture();
  220. //
  221. // BFWindowList::iterator itr = gBFApp->mWindowList.begin();
  222. // while (itr != gBFApp->mWindowList.end())
  223. // {
  224. // SdlBFWindow* aWindow = (SdlBFWindow*) *itr;
  225. // if ((aWindow != this) && (aWindow->mIsMouseInside))
  226. // {
  227. // aWindow->mMouseLeaveFunc(this);
  228. // aWindow->mIsMouseInside = false;
  229. // }
  230. // ++itr;
  231. // }
  232. // }
  233. // }
  234. // break;
  235. //
  236. // case WM_COMMAND:
  237. // {
  238. // SdlBFMenu* aMenu = mMenuIDMap[(uint32)wParam];
  239. // if (aMenu != NULL)
  240. // mMenuItemSelectedFunc(this, aMenu);
  241. // }
  242. // break;
  243. // case WM_INITMENUPOPUP:
  244. // {
  245. // HMENU hMenu = (HMENU) wParam;
  246. // SdlBFMenu* aMenu = mHMenuMap[hMenu];
  247. // if (aMenu != NULL)
  248. // mMenuItemSelectedFunc(this, aMenu);
  249. // }
  250. // break;
  251. //
  252. // case WM_MOUSEACTIVATE:
  253. // if (mFlags & BFWINDOW_NO_MOUSE_ACTIVATE)
  254. // {
  255. // doResult = true;
  256. // result = MA_NOACTIVATE;
  257. // }
  258. // break;
  259. //
  260. // case WM_KILLFOCUS:
  261. // mLostFocusFunc(this);
  262. // break;
  263. // case WM_SETFOCUS:
  264. // mGotFocusFunc(this);
  265. // break;
  266. // case WM_MOUSELEAVE:
  267. // mIsMouseInside = false;
  268. // mMouseLeaveFunc(this);
  269. // break;
  270. //
  271. // case WM_CHAR:
  272. // mKeyCharFunc(this, (WCHAR)wParam);
  273. // break;
  274. // case WM_SYSKEYDOWN:
  275. // case WM_KEYDOWN:
  276. // {
  277. // int keyCode = (int) wParam;
  278. // mIsKeyDown[keyCode] = true;
  279. //
  280. // WinMenuIDMap::iterator itr = mMenuIDMap.begin();
  281. // while (itr != mMenuIDMap.end())
  282. // {
  283. // SdlBFMenu* aMenu = itr->second;
  284. // if ((aMenu->mKeyCode == keyCode) &&
  285. // (aMenu->mKeyShift == mIsKeyDown[VK_SHIFT]) &&
  286. // (aMenu->mKeyCtrl == mIsKeyDown[VK_CONTROL]) &&
  287. // (aMenu->mKeyAlt == mIsKeyDown[VK_MENU]))
  288. // {
  289. // mMenuItemSelectedFunc(this, aMenu);
  290. // doResult = true;
  291. // break;
  292. // }
  293. // ++itr;
  294. // }
  295. // mKeyDownFunc(this, (int) wParam, (lParam & 0x7FFF) != 0);
  296. // }
  297. // break;
  298. // case WM_SYSCHAR:
  299. // {
  300. // int keyCode = toupper((int) wParam);
  301. //
  302. // WinMenuIDMap::iterator itr = mMenuIDMap.begin();
  303. // while (itr != mMenuIDMap.end())
  304. // {
  305. // SdlBFMenu* aMenu = itr->second;
  306. // if ((aMenu->mKeyCode == keyCode) &&
  307. // (aMenu->mKeyShift == mIsKeyDown[VK_SHIFT]) &&
  308. // (aMenu->mKeyCtrl == mIsKeyDown[VK_CONTROL]) &&
  309. // (aMenu->mKeyAlt == mIsKeyDown[VK_MENU]))
  310. // {
  311. // doResult = true;
  312. // break;
  313. // }
  314. // ++itr;
  315. // }
  316. // }
  317. // break;
  318. // case WM_SYSKEYUP:
  319. // case WM_KEYUP:
  320. // {
  321. // int keyCode = (int) wParam;
  322. // if (mIsKeyDown[keyCode])
  323. // {
  324. // mKeyUpFunc(this, (int) wParam);
  325. // mIsKeyDown[keyCode] = false;
  326. // }
  327. // }
  328. // break;
  329. //
  330. // case WM_TIMER:
  331. // if (gBFApp->mSysDialogCnt == 0)
  332. // gBFApp->Process();
  333. // break;
  334. //
  335. // case WM_SETCURSOR:
  336. // gBFApp->PhysSetCursor();
  337. // break;
  338. //
  339. // case WM_MOVING:
  340. // if (mMovedFunc != NULL)
  341. // mMovedFunc(this);
  342. // break;
  343. // case WM_SIZING:
  344. // mRenderWindow->Resized();
  345. // if (mMovedFunc != NULL)
  346. // mMovedFunc(this);
  347. // if (gBFApp->mSysDialogCnt == 0)
  348. // gBFApp->Process();
  349. // break;
  350. // }
  351. //
  352. //
  353. // app->mInMsgProc = false;
  354. // }
  355. //
  356. // if (doResult)
  357. // return result;
  358. //
  359. // return DefWindowProc(hWnd, Msg, wParam, lParam);
  360. //}
  361. static int SDLConvertScanCode(int scanCode)
  362. {
  363. if ((scanCode >= SDL_SCANCODE_A) && (scanCode <= SDL_SCANCODE_Z))
  364. return (scanCode - SDL_SCANCODE_A) + 'A';
  365. if ((scanCode >= SDL_SCANCODE_0) && (scanCode <= SDL_SCANCODE_9))
  366. return (scanCode - SDL_SCANCODE_0) + '0';
  367. switch (scanCode)
  368. {
  369. case SDL_SCANCODE_CANCEL: return 0x03;
  370. case SDL_SCANCODE_AC_BACK: return 0x08;
  371. case SDL_SCANCODE_TAB: return 0x09;
  372. case SDL_SCANCODE_CLEAR: return 0x0C;
  373. case SDL_SCANCODE_RETURN: return 0x0D;
  374. case SDL_SCANCODE_LSHIFT: return 0x10;
  375. case SDL_SCANCODE_RSHIFT: return 0x10;
  376. case SDL_SCANCODE_LCTRL: return 0x11;
  377. case SDL_SCANCODE_RCTRL: return 0x11;
  378. case SDL_SCANCODE_MENU: return 0x12;
  379. case SDL_SCANCODE_PAUSE: return 0x13;
  380. case SDL_SCANCODE_LANG1: return 0x15;
  381. case SDL_SCANCODE_LANG2: return 0x15;
  382. case SDL_SCANCODE_LANG3: return 0x17;
  383. case SDL_SCANCODE_LANG4: return 0x18;
  384. case SDL_SCANCODE_LANG5: return 0x19;
  385. case SDL_SCANCODE_LANG6: return 0x19;
  386. case SDL_SCANCODE_ESCAPE: return 0x1B;
  387. case SDL_SCANCODE_SPACE: return 0x20;
  388. case SDL_SCANCODE_PAGEUP: return 0x21;
  389. case SDL_SCANCODE_PAGEDOWN: return 0x22;
  390. case SDL_SCANCODE_END: return 0x23;
  391. case SDL_SCANCODE_HOME: return 0x24;
  392. case SDL_SCANCODE_LEFT: return 0x25;
  393. case SDL_SCANCODE_UP: return 0x26;
  394. case SDL_SCANCODE_RIGHT: return 0x27;
  395. case SDL_SCANCODE_DOWN: return 0x28;
  396. case SDL_SCANCODE_SELECT: return 0x29;
  397. case SDL_SCANCODE_PRINTSCREEN: return 0x2A;
  398. case SDL_SCANCODE_EXECUTE: return 0x2B;
  399. case SDL_SCANCODE_INSERT: return 0x2D;
  400. case SDL_SCANCODE_DELETE: return 0x2E;
  401. case SDL_SCANCODE_HELP: return 0x2F;
  402. case SDL_SCANCODE_LGUI: return 0x5B;
  403. case SDL_SCANCODE_RGUI: return 0x5C;
  404. case SDL_SCANCODE_KP_0: return 0x60;
  405. case SDL_SCANCODE_KP_1: return 0x61;
  406. case SDL_SCANCODE_KP_2: return 0x62;
  407. case SDL_SCANCODE_KP_3: return 0x63;
  408. case SDL_SCANCODE_KP_4: return 0x64;
  409. case SDL_SCANCODE_KP_5: return 0x65;
  410. case SDL_SCANCODE_KP_6: return 0x66;
  411. case SDL_SCANCODE_KP_7: return 0x67;
  412. case SDL_SCANCODE_KP_8: return 0x68;
  413. case SDL_SCANCODE_KP_9: return 0x69;
  414. case SDL_SCANCODE_KP_MULTIPLY: return 0x6A;
  415. case SDL_SCANCODE_KP_PLUS: return 0x6B;
  416. case SDL_SCANCODE_SEPARATOR: return 0x6C;
  417. case SDL_SCANCODE_KP_MINUS: return 0x6D;
  418. case SDL_SCANCODE_KP_PERIOD: return 0x6E;
  419. case SDL_SCANCODE_KP_DIVIDE: return 0x6F;
  420. case SDL_SCANCODE_F1: return 0x70;
  421. case SDL_SCANCODE_F2: return 0x71;
  422. case SDL_SCANCODE_F3: return 0x72;
  423. case SDL_SCANCODE_F4: return 0x73;
  424. case SDL_SCANCODE_F5: return 0x74;
  425. case SDL_SCANCODE_F6: return 0x75;
  426. case SDL_SCANCODE_F7: return 0x76;
  427. case SDL_SCANCODE_F8: return 0x77;
  428. case SDL_SCANCODE_F9: return 0x78;
  429. case SDL_SCANCODE_F10: return 0x79;
  430. case SDL_SCANCODE_F11: return 0x7A;
  431. case SDL_SCANCODE_F12: return 0x7B;
  432. case SDL_SCANCODE_NUMLOCKCLEAR: return 0x90;
  433. case SDL_SCANCODE_SCROLLLOCK: return 0x91;
  434. case SDL_SCANCODE_GRAVE: return 0xC0;
  435. //case SDL_SCANCODE_COMMAND: return 0xF0;
  436. }
  437. return 0;
  438. }
  439. #ifdef _WIN32
  440. extern HINSTANCE gDLLInstance;
  441. #endif
  442. SdlBFApp::SdlBFApp()
  443. {
  444. //_CrtSetReportHook(SdlBFReportHook);
  445. mRunning = false;
  446. mRenderDevice = NULL;
  447. wchar_t aStr[MAX_PATH];
  448. #ifdef _WIN32
  449. GetModuleFileNameW(gDLLInstance, aStr, MAX_PATH);
  450. #else
  451. GetModuleFileNameW(NULL, aStr, MAX_PATH);
  452. #endif
  453. if (aStr[0] == '!')
  454. {
  455. new SdlBFWindow(NULL, "", 0, 0, 0, 0, 0);
  456. }
  457. mInstallDir = aStr;
  458. int lastSlash = std::max((int)mInstallDir.rfind('\\'), (int)mInstallDir.rfind('/'));
  459. if (lastSlash != -1)
  460. mInstallDir = mInstallDir.substr(0, lastSlash);
  461. //TODO: We're not properly using DataDir vs InstallDir
  462. #if (!defined BFSYSLIB_DYNAMIC) && (defined BF_RESOURCES_REL_DIR)
  463. mInstallDir += "/" + Beefy::UTF8Decode(BF_RESOURCES_REL_DIR);
  464. #endif
  465. mInstallDir += "/";
  466. //OutputDebugStrF(L"DataDir: %s\n", mInstallDir.c_str());
  467. mDataDir = mInstallDir;
  468. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0)
  469. BF_FATAL(StrFormat("Unable to initialize SDL: %s", SDL_GetError()).c_str());
  470. }
  471. SdlBFApp::~SdlBFApp()
  472. {
  473. }
  474. SdlBFWindow* SdlBFApp::GetSdlWindowFromId(uint32 id)
  475. {
  476. SdlWindowMap::iterator itr = mSdlWindowMap.find(id);
  477. if (itr != mSdlWindowMap.end())
  478. return itr->second;
  479. return NULL;
  480. }
  481. void SdlBFApp::Init()
  482. {
  483. mRunning = true;
  484. mInMsgProc = false;
  485. mRenderDevice = new GLRenderDevice();
  486. mRenderDevice->Init(this);
  487. }
  488. void SdlBFApp::Run()
  489. {
  490. while (mRunning)
  491. {
  492. SDL_Event sdlEvent;
  493. while (true)
  494. {
  495. {
  496. //Beefy::DebugTimeGuard suspendTimeGuard(30, "BFApp::Run1");
  497. if (!SDL_PollEvent(&sdlEvent))
  498. break;
  499. }
  500. //Beefy::DebugTimeGuard suspendTimeGuard(30, "BFApp::Run2");
  501. switch (sdlEvent.type)
  502. {
  503. case SDL_QUIT:
  504. //gBFApp->RemoveWindow(sdlEvent.window);
  505. Shutdown();
  506. break;
  507. case SDL_MOUSEBUTTONUP:
  508. {
  509. SdlBFWindow* sdlBFWindow = GetSdlWindowFromId(sdlEvent.button.windowID);
  510. sdlBFWindow->mMouseUpFunc(sdlBFWindow, sdlEvent.button.x, sdlEvent.button.y, sdlEvent.button.button);
  511. }
  512. break;
  513. case SDL_MOUSEBUTTONDOWN:
  514. {
  515. SdlBFWindow* sdlBFWindow = GetSdlWindowFromId(sdlEvent.button.windowID);
  516. sdlBFWindow->mMouseDownFunc(sdlBFWindow, sdlEvent.button.x, sdlEvent.button.y, sdlEvent.button.button, 1);
  517. }
  518. break;
  519. case SDL_MOUSEMOTION:
  520. {
  521. SdlBFWindow* sdlBFWindow = GetSdlWindowFromId(sdlEvent.button.windowID);
  522. sdlBFWindow->mMouseMoveFunc(sdlBFWindow, sdlEvent.button.x, sdlEvent.button.y);
  523. }
  524. break;
  525. case SDL_KEYDOWN:
  526. {
  527. SdlBFWindow* sdlBFWindow = GetSdlWindowFromId(sdlEvent.key.windowID);
  528. sdlBFWindow->mKeyDownFunc(sdlBFWindow, SDLConvertScanCode(sdlEvent.key.keysym.scancode), sdlEvent.key.repeat);
  529. sdlBFWindow->mKeyCharFunc(sdlBFWindow, sdlEvent.key.keysym.sym);
  530. }
  531. break;
  532. case SDL_KEYUP:
  533. {
  534. SdlBFWindow* sdlBFWindow = GetSdlWindowFromId(sdlEvent.key.windowID);
  535. sdlBFWindow->mKeyUpFunc(sdlBFWindow, SDLConvertScanCode(sdlEvent.key.keysym.scancode));
  536. }
  537. break;
  538. }
  539. }
  540. Process();
  541. }
  542. }
  543. extern int gPixelsDrawn;
  544. int gFrameCount = 0;
  545. int gBFDrawBatchCount = 0;
  546. void SdlBFApp::Draw()
  547. {
  548. //Beefy::DebugTimeGuard suspendTimeGuard(30, "SdlBFApp::Draw");
  549. glDisable(GL_SCISSOR_TEST);
  550. glDisable(GL_CULL_FACE);
  551. glDisable(GL_DEPTH_TEST);
  552. glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
  553. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  554. gPixelsDrawn = 0;
  555. gBFDrawBatchCount = 0;
  556. mRenderDevice->FrameStart();
  557. BFApp::Draw();
  558. mRenderDevice->FrameEnd();
  559. gFrameCount++;
  560. //if (gFrameCount % 60 == 0)
  561. //OutputDebugStrF("Pixels: %d Batches: %d\n", gPixelsDrawn / 1000, gBFDrawBatchCount);
  562. }
  563. BFWindow* SdlBFApp::CreateNewWindow(BFWindow* parent, const StringImpl& title, int x, int y, int width, int height, int windowFlags)
  564. {
  565. SdlBFWindow* aWindow = new SdlBFWindow(parent, title, x, y, width, height, windowFlags);
  566. mSdlWindowMap[SDL_GetWindowID(aWindow->mSDLWindow)] = aWindow;
  567. mWindowList.push_back(aWindow);
  568. return aWindow;
  569. }
  570. void SdlBFWindow::GetPosition(int* x, int* y, int* width, int* height, int* clientX, int* clientY, int* clientWidth, int* clientHeight)
  571. {
  572. SDL_GetWindowPosition(mSDLWindow, x, y);
  573. SDL_GetWindowSize(mSDLWindow, width, height);
  574. *clientWidth = *width;
  575. *clientHeight = *height;
  576. }
  577. void SdlBFApp::PhysSetCursor()
  578. {
  579. //
  580. //static HCURSOR cursors [] =
  581. // {
  582. // ::LoadCursor(NULL, IDC_ARROW),
  583. //
  584. // //TODO: mApp->mHandCursor);
  585. // ::LoadCursor(NULL, IDC_ARROW),
  586. // //TODO: mApp->mDraggingCursor);
  587. // ::LoadCursor(NULL, IDC_ARROW),
  588. // ::LoadCursor(NULL, IDC_IBEAM),
  589. //
  590. // ::LoadCursor(NULL, IDC_NO),
  591. // ::LoadCursor(NULL, IDC_SIZEALL),
  592. // ::LoadCursor(NULL, IDC_SIZENESW),
  593. // ::LoadCursor(NULL, IDC_SIZENS),
  594. // ::LoadCursor(NULL, IDC_SIZENWSE),
  595. // ::LoadCursor(NULL, IDC_SIZEWE),
  596. // ::LoadCursor(NULL, IDC_WAIT),
  597. // NULL
  598. // };
  599. //::SetCursor(cursors[mCursor]);
  600. }
  601. void SdlBFWindow::SetClientPosition(int x, int y)
  602. {
  603. SDL_SetWindowPosition(mSDLWindow, x, y);
  604. if (mMovedFunc != NULL)
  605. mMovedFunc(this);
  606. }
  607. void SdlBFWindow::SetAlpha(float alpha, uint32 destAlphaSrcMask, bool isMouseVisible)
  608. {
  609. // Not supported
  610. }
  611. uint32 SdlBFApp::GetClipboardFormat(const StringImpl& format)
  612. {
  613. return CF_TEXT;
  614. }
  615. void* SdlBFApp::GetClipboardData(const StringImpl& format, int* size)
  616. {
  617. return SDL_GetClipboardText();
  618. }
  619. void SdlBFApp::ReleaseClipboardData(void* ptr)
  620. {
  621. SDL_free(ptr);
  622. }
  623. void SdlBFApp::SetClipboardData(const StringImpl& format, const void* ptr, int size, bool resetClipboard)
  624. {
  625. SDL_SetClipboardText((const char*)ptr);
  626. }
  627. BFMenu* SdlBFWindow::AddMenuItem(BFMenu* parent, const wchar_t* text, const wchar_t* hotKey, BFSysBitmap* sysBitmap, bool enabled, int checkState, bool radioCheck)
  628. {
  629. return NULL;
  630. }
  631. void SdlBFWindow::RemoveMenuItem(BFMenu* item)
  632. {
  633. }
  634. BFSysBitmap* SdlBFApp::LoadSysBitmap(const wchar_t* fileName)
  635. {
  636. return NULL;
  637. }
  638. void SdlBFWindow::ModalsRemoved()
  639. {
  640. //::EnableWindow(mHWnd, TRUE);
  641. //::SetFocus(mHWnd);
  642. }
  643. DrawLayer* SdlBFApp::CreateDrawLayer(BFWindow* window)
  644. {
  645. GLDrawLayer* drawLayer = new GLDrawLayer();
  646. if (window != NULL)
  647. {
  648. drawLayer->mRenderWindow = window->mRenderWindow;
  649. window->mRenderWindow->mDrawLayerList.push_back(drawLayer);
  650. }
  651. return drawLayer;
  652. }
  653. void SdlBFApp::GetDesktopResolution(int& width, int& height) override
  654. {
  655. width = 1024;
  656. height = 768;
  657. }
  658. void SdlBFApp::GetWorkspaceRect(int& x, int& y, int& width, int& height) override
  659. {
  660. x = 0;
  661. y = 0;
  662. width = 1024;
  663. height = 768;
  664. }