BeefySysLib.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. #include "Common.h"
  2. #include "PlatformApp.h"
  3. #include "gfx/RenderDevice.h"
  4. #include "gfx/Texture.h"
  5. #include "gfx/Shader.h"
  6. #include "gfx/DrawLayer.h"
  7. #include "gfx/RenderCmd.h"
  8. #include "gfx/FTFont.h"
  9. #include "img/BFIData.h"
  10. #include "util/Vector.h"
  11. #include "util/PerfTimer.h"
  12. #include "util/TLSingleton.h"
  13. #include "util/MemLogger.h"
  14. #include "img/ImgEffects.h"
  15. #include "util/AllocDebug.h"
  16. #ifdef BF_PLATFORM_WINDOWS
  17. #include "platform/sdl/SdlBFApp.h"
  18. #endif
  19. //#include "third_party/freetype/include/ft2build.h"
  20. //#include FT_FREETYPE_H
  21. //#include "img/PNGData.h"
  22. #define UTF16DECODE_PTR(strPtr) ((strPtr) == NULL ? NULL : UTF16Decode(strPtr).c_str())
  23. USING_NS_BF;
  24. #pragma warning(disable:4996)
  25. static TLSingleton<String> gBeefySys_TLStrReturn;
  26. int gPixelsDrawn = 0;
  27. #ifdef BF_PLATFORM_WINDOWS
  28. static int gLastReqId = 0;
  29. static int BfAllocHook(int nAllocType, void *pvData,
  30. size_t nSize, int nBlockUse, long lRequest,
  31. const unsigned char * szFileName, int nLine)
  32. {
  33. if (gLastReqId == lRequest)
  34. return TRUE;
  35. gLastReqId = lRequest;
  36. if (szFileName == NULL)
  37. return TRUE;
  38. /*char str[1024];
  39. sprintf(str, "Alloc: %d File: %s Line: %d\n", lRequest, szFileName, nLine);
  40. OutputDebugStringA(str);*/
  41. return TRUE;
  42. }
  43. HINSTANCE gDLLInstance = NULL;
  44. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  45. {
  46. //::MessageBoxA(NULL, "C", "D", MB_OK);
  47. #ifdef BF_VC
  48. //_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF );
  49. //_CrtSetBreakAlloc(1437);
  50. //_CrtSetAllocHook(BfAllocHook);
  51. #endif
  52. switch (fdwReason)
  53. {
  54. case DLL_PROCESS_ATTACH:
  55. gDLLInstance = hinstDLL;
  56. case DLL_THREAD_ATTACH:
  57. case DLL_THREAD_DETACH:
  58. case DLL_PROCESS_DETACH:
  59. break;
  60. }
  61. return TRUE;
  62. }
  63. #endif
  64. BF_EXPORT void BF_CALLTYPE BFApp_GetDesktopResolution(int& width, int& height)
  65. {
  66. gBFApp->GetDesktopResolution(width, height);
  67. }
  68. BF_EXPORT void BF_CALLTYPE BFApp_GetWorkspaceRect(int& x, int& y, int& width, int& height)
  69. {
  70. gBFApp->GetWorkspaceRect(x, y, width, height);
  71. }
  72. BF_EXPORT void BF_CALLTYPE BFApp_GetWorkspaceRectFrom(int fromX, int fromY, int fromWidth, int fromHeight, int& outX, int& outY, int& outWidth, int& outHeight)
  73. {
  74. gBFApp->GetWorkspaceRectFrom(fromX, fromY, fromWidth, fromHeight, outX, outY, outWidth, outHeight);
  75. }
  76. static bool gForceSDL = false;
  77. BF_EXPORT void BF_CALLTYPE BFApp_SetOptionString(const char* name, const char* value)
  78. {
  79. if (strcmp(name, "SDL") == 0)
  80. {
  81. gForceSDL = strcmp(value, "1") == 0;
  82. }
  83. }
  84. BF_EXPORT void BF_CALLTYPE BFApp_Create()
  85. {
  86. #ifdef BF_PLATFORM_WINDOWS
  87. if (gForceSDL)
  88. {
  89. new SdlBFApp();
  90. return;
  91. }
  92. #endif
  93. new PlatformBFApp();
  94. }
  95. BF_EXPORT void BF_CALLTYPE BFApp_Delete()
  96. {
  97. delete gBFApp;
  98. gBFApp = NULL;
  99. FTFontManager::ClearCache();
  100. //OutputDebugStrF("Deleting App\n");
  101. #ifdef BF_VC
  102. //_CrtDumpMemoryLeaks();
  103. #endif
  104. }
  105. //void FT_Test()
  106. //{
  107. // FT_Library library; /* handle to library */
  108. // FT_Face face; /* handle to face object */
  109. //
  110. // auto error = FT_Init_FreeType(&library);
  111. // error = FT_New_Face(library, "/temp/SourceCodePro-Regular.ttf", 0, &face);
  112. // if (error == FT_Err_Unknown_File_Format)
  113. // {
  114. //
  115. // }
  116. // else if (error)
  117. // {
  118. //
  119. // }
  120. //
  121. // error = FT_Set_Char_Size(
  122. // face, /* handle to face object */
  123. // 0, /* char_width in 1/64th of points */
  124. // 9 * 64, /* char_height in 1/64th of points */
  125. // 96, /* horizontal device resolution */
  126. // 96); /* vertical device resolution */
  127. //
  128. // String str = ".cHasDebugFlags";
  129. //
  130. // PNGData image;
  131. // image.CreateNew(256, 256);
  132. // for (int i = 0; i < 256 * 256; i++)
  133. // image.mBits[i] = 0xFF000000;
  134. //
  135. // int curX = 0;
  136. // int curY = 0;
  137. //
  138. // for (int i = 0; i < (int)str.length(); i++)
  139. // {
  140. // int glyph_index = FT_Get_Char_Index(face, str[i]);
  141. //
  142. // error = FT_Load_Glyph(
  143. // face, /* handle to face object */
  144. // glyph_index, /* glyph index */
  145. // FT_LOAD_NO_BITMAP); /* load flags, see below */
  146. //
  147. // error = FT_Render_Glyph(face->glyph, /* glyph slot */
  148. // FT_RENDER_MODE_NORMAL); /* render mode */
  149. //
  150. // auto& bitmap = face->glyph->bitmap;
  151. // for (int y = 0; y < (int)bitmap.rows; y++)
  152. // {
  153. // for (int x = 0; x < (int)bitmap.width; x++)
  154. // {
  155. // uint8 val = bitmap.buffer[y * bitmap.pitch + x];
  156. //
  157. // val = (uint8)(pow(val / 255.0f, 0.5556) * 255.0f);
  158. //
  159. // image.mBits[(y + 12 - bitmap.rows) * image.mWidth + x + curX] = 0xFF000000 |
  160. // ((int32)val) | ((int32)val << 8) | ((int32)val << 16);
  161. // }
  162. // }
  163. //
  164. // curX += bitmap.width + 1;
  165. //
  166. // //int w = face->glyph->bitmap.;
  167. //
  168. // //face->glyph->bitmap.buffer
  169. // }
  170. // image.WriteToFile("/temp/fnt.png");
  171. //}
  172. BF_EXPORT void BF_CALLTYPE BFApp_Init()
  173. {
  174. //////////////////////////////////////////////////////////////////////////
  175. //FT_Test();
  176. //////////////////////////////////////////////////////////////////////////
  177. gBFApp->Init();
  178. }
  179. BF_EXPORT void BF_CALLTYPE BFApp_Run()
  180. {
  181. gBFApp->Run();
  182. }
  183. BF_EXPORT void BF_CALLTYPE BFApp_Shutdown()
  184. {
  185. gBFApp->Shutdown();
  186. }
  187. BF_EXPORT void BF_CALLTYPE BFApp_SetDrawEnabled(int enabled)
  188. {
  189. gBFApp->mDrawEnabled = enabled != 0;
  190. }
  191. BF_EXPORT void BF_CALLTYPE BFApp_SetRefreshRate(int rate)
  192. {
  193. gBFApp->mRefreshRate = (float) rate;
  194. }
  195. BF_EXPORT const char* BF_CALLTYPE BFApp_GetInstallDir()
  196. {
  197. return gBFApp->mInstallDir.c_str();
  198. }
  199. BF_EXPORT const char* BF_CALLTYPE BFApp_GetDataDir()
  200. {
  201. return gBFApp->mDataDir.c_str();
  202. }
  203. BF_EXPORT void BF_CALLTYPE BFApp_SetCallbacks(BFApp_UpdateFunc updateFunc, BFApp_UpdateFFunc updateFFunc, BFApp_DrawFunc drawFunc)
  204. {
  205. gBFApp->mUpdateFunc = updateFunc;
  206. gBFApp->mUpdateFFunc = updateFFunc;
  207. gBFApp->mDrawFunc = drawFunc;
  208. //public delegate void UpdateProc();
  209. }
  210. BF_EXPORT BFWindow* BF_CALLTYPE BFApp_CreateWindow(BFWindow* parent, const char* title, int x, int y, int width, int height, int windowFlags)
  211. {
  212. return gBFApp->CreateNewWindow(parent, title, x, y, width, height, windowFlags);
  213. }
  214. BF_EXPORT void BF_CALLTYPE BFApp_RemoveWindow(BFWindow* window)
  215. {
  216. delete window;
  217. }
  218. BF_EXPORT void BF_CALLTYPE BFApp_SetCursor(int cursor)
  219. {
  220. gBFApp->SetCursor(cursor);
  221. }
  222. BF_EXPORT void* BF_CALLTYPE BFApp_GetClipboardData(const char* format, int* size)
  223. {
  224. return gBFApp->GetClipboardData(format, size);
  225. }
  226. BF_EXPORT void BF_CALLTYPE BFApp_ReleaseClipboardData(void* ptr)
  227. {
  228. return gBFApp->ReleaseClipboardData(ptr);
  229. }
  230. BF_EXPORT void BF_CALLTYPE BFApp_SetClipboardData(const char* format, void* ptr, int size, int resetClipboard)
  231. {
  232. return gBFApp->SetClipboardData(format, ptr, size, resetClipboard != 0);
  233. }
  234. BF_EXPORT void BF_CALLTYPE BFApp_CheckMemory()
  235. {
  236. #ifdef BF_PLATFORM_WINDOWS
  237. _CrtCheckMemory();
  238. #endif
  239. }
  240. BF_EXPORT void BF_CALLTYPE BFApp_RehupMouse()
  241. {
  242. }
  243. BF_EXPORT const char* BF_CALLTYPE BFApp_EnumerateInputDevices()
  244. {
  245. String& outString = *gBeefySys_TLStrReturn.Get();
  246. outString = gBFApp->EnumerateInputDevices();
  247. return outString.c_str();
  248. }
  249. BF_EXPORT BFInputDevice* BFApp_CreateInputDevice(const char* guid)
  250. {
  251. return gBFApp->CreateInputDevice(guid);
  252. }
  253. BF_EXPORT BFSoundManager* BF_CALLTYPE BFApp_GetSoundManager()
  254. {
  255. return gBFApp->GetSoundManager();
  256. }
  257. BF_EXPORT intptr BF_CALLTYPE BFApp_GetCriticalThreadId(int idx)
  258. {
  259. return gBFApp->GetCriticalThreadId(idx);
  260. }
  261. ///
  262. BF_EXPORT void BF_CALLTYPE BFWindow_SetCallbacks(BFWindow* window, BFWindow_MovedFunc movedFunc, BFWindow_CloseQueryFunc closeQueryFunc, BFWindow_ClosedFunc closedFunc,
  263. BFWindow_GotFocusFunc gotFocusFunc, BFWindow_LostFocusFunc lostFocusFunc,
  264. BFWindow_KeyCharFunc keyCharFunc, BFWindow_KeyDownFunc keyDownFunc, BFWindow_KeyUpFunc keyUpFunc, BFWindow_HitTestFunc hitTestFunc,
  265. BFWindow_MouseMove mouseMoveFunc, BFWindow_MouseProxyMove mouseProxyMoveFunc,
  266. BFWindow_MouseDown mouseDownFunc, BFWindow_MouseUp mouseUpFunc, BFWindow_MouseWheel mouseWheelFunc, BFWindow_MouseLeave mouseLeaveFunc,
  267. BFWindow_MenuItemSelectedFunc menuItemSelectedFunc, BFWindow_DragDropFileFunc dragDropFileFunc)
  268. {
  269. window->mMovedFunc = movedFunc;
  270. window->mCloseQueryFunc = closeQueryFunc;
  271. window->mClosedFunc = closedFunc;
  272. window->mGotFocusFunc = gotFocusFunc;
  273. window->mLostFocusFunc = lostFocusFunc;
  274. window->mKeyCharFunc = keyCharFunc;
  275. window->mKeyDownFunc = keyDownFunc;
  276. window->mKeyUpFunc = keyUpFunc;
  277. window->mHitTestFunc = hitTestFunc;
  278. window->mMouseMoveFunc = mouseMoveFunc;
  279. window->mMouseProxyMoveFunc = mouseProxyMoveFunc;
  280. window->mMouseDownFunc = mouseDownFunc;
  281. window->mMouseUpFunc = mouseUpFunc;
  282. window->mMouseWheelFunc = mouseWheelFunc;
  283. window->mMouseLeaveFunc = mouseLeaveFunc;
  284. window->mMenuItemSelectedFunc = menuItemSelectedFunc;
  285. window->mDragDropFileFunc = dragDropFileFunc;
  286. }
  287. BF_EXPORT void* BFWindow_GetNativeUnderlying(BFWindow* window)
  288. {
  289. return window->GetUnderlying();
  290. }
  291. BF_EXPORT void BF_CALLTYPE BFWindow_MovedDelegate(BFWindow* window, BFWindow_MovedFunc movedFunc)
  292. {
  293. window->mMovedFunc = movedFunc;
  294. }
  295. BF_EXPORT void BF_CALLTYPE BFWindow_SetTitle(BFWindow* window, const char* title)
  296. {
  297. window->SetTitle(title);
  298. }
  299. BF_EXPORT void BF_CALLTYPE BFWindow_SetMinimumSize(BFWindow* window, int minWidth, int minHeight, bool clientSized)
  300. {
  301. window->SetMinimumSize(minWidth, minHeight, clientSized);
  302. }
  303. BF_EXPORT void BF_CALLTYPE BFWindow_GetPosition(BFWindow* window, int* x, int* y, int* width, int* height, int* clientX, int* clientY, int* clientWidth, int* clientHeight)
  304. {
  305. window->GetPosition(x, y, width, height, clientX, clientY, clientWidth, clientHeight);
  306. }
  307. BF_EXPORT void BF_CALLTYPE BFWindow_GetPlacement(BFWindow* window, int* normX, int* normY, int* normWidth, int* normHeight, int* showKind)
  308. {
  309. window->GetPlacement(normX, normY, normWidth, normHeight, showKind);
  310. }
  311. BF_EXPORT void BF_CALLTYPE BFWindow_Resize(BFWindow* window, int x, int y, int width, int height, int showKind)
  312. {
  313. window->Resize(x, y, width, height, (BFWindow::ShowKind)showKind);
  314. }
  315. BF_EXPORT void BF_CALLTYPE BFWindow_Show(BFWindow* window, BFWindow::ShowKind showKind)
  316. {
  317. window->Show(showKind);
  318. }
  319. BF_EXPORT void BF_CALLTYPE BFWindow_SetForeground(BFWindow* window)
  320. {
  321. window->SetForeground();
  322. }
  323. BF_EXPORT void BF_CALLTYPE BFWindow_SetNonExclusiveMouseCapture(BFWindow* window)
  324. {
  325. window->SetNonExclusiveMouseCapture();
  326. }
  327. BF_EXPORT void BF_CALLTYPE BFWindow_LostFocus(BFWindow* window, BFWindow* newFocus)
  328. {
  329. window->LostFocus(newFocus);
  330. }
  331. BF_EXPORT void BF_CALLTYPE BFWindow_SetAlpha(BFWindow* window, float alpha, uint32 destAlphaSrcMask, int mouseVisible)
  332. {
  333. window->SetAlpha(alpha, destAlphaSrcMask, mouseVisible != 0);
  334. }
  335. BF_EXPORT void BF_CALLTYPE BFWindow_CaptureMouse(BFWindow* window)
  336. {
  337. window->CaptureMouse();
  338. }
  339. BF_EXPORT bool BF_CALLTYPE BFWindow_IsMouseCaptured(BFWindow* window)
  340. {
  341. return window->IsMouseCaptured();
  342. }
  343. BF_EXPORT void BF_CALLTYPE BFWindow_SetMouseVisible(BFWindow* window, bool mouseVisible)
  344. {
  345. window->SetMouseVisible(mouseVisible);
  346. }
  347. BF_EXPORT void BF_CALLTYPE BFWindow_SetClientPosition(BFWindow* window, int x, int y)
  348. {
  349. window->SetClientPosition(x, y);
  350. }
  351. BF_EXPORT BFMenu* BF_CALLTYPE BFWindow_AddMenuItem(BFWindow* window, BFMenu* parent, int insertIdx, const char* text, const char* hotKey, BFSysBitmap* bitmap, int enabled, int checkState, int radioCheck)
  352. {
  353. return window->AddMenuItem(parent, insertIdx, text, hotKey, bitmap, enabled != 0, checkState, radioCheck != 0);
  354. }
  355. BF_EXPORT void BF_CALLTYPE BFWindow_ModifyMenuItem(BFWindow* window, BFMenu* item, const char* text, const char* hotKey, BFSysBitmap* bitmap, int enabled, int checkState, int radioCheck)
  356. {
  357. window->ModifyMenuItem(item, text, hotKey, bitmap, enabled != 0, checkState, radioCheck != 0);
  358. }
  359. BF_EXPORT void BF_CALLTYPE BFWindow_DeleteMenuItem(BFWindow* window, BFMenu* item)
  360. {
  361. window->RemoveMenuItem(item);
  362. delete item;
  363. }
  364. BF_EXPORT void BF_CALLTYPE BFWindow_Close(BFWindow* window, int force)
  365. {
  366. if (force != 0)
  367. gBFApp->RemoveWindow(window);
  368. else
  369. window->TryClose();
  370. }
  371. BF_EXPORT int BF_CALLTYPE BFWindow_GetDPI(BFWindow* window)
  372. {
  373. return window->GetDPI();
  374. }
  375. ///
  376. BF_EXPORT TextureSegment* BF_CALLTYPE Gfx_CreateRenderTarget(int width, int height, int destAlpha)
  377. {
  378. Texture* texture = gBFApp->mRenderDevice->CreateRenderTarget(width, height, destAlpha != 0);
  379. texture->mResetClear = true;
  380. TextureSegment* aTextureSegment = new TextureSegment();
  381. aTextureSegment->InitFromTexture(texture);
  382. return aTextureSegment;
  383. }
  384. BF_EXPORT TextureSegment* BF_CALLTYPE Gfx_CreateDynTexture(int width, int height)
  385. {
  386. Texture* texture = gBFApp->mRenderDevice->CreateDynTexture(width, height);
  387. TextureSegment* aTextureSegment = new TextureSegment();
  388. aTextureSegment->InitFromTexture(texture);
  389. return aTextureSegment;
  390. }
  391. BF_EXPORT TextureSegment* BF_CALLTYPE Gfx_LoadTexture(const char* fileName, int flags)
  392. {
  393. Texture* texture = gBFApp->mRenderDevice->LoadTexture(fileName, flags);
  394. if (texture == NULL)
  395. return NULL;
  396. TextureSegment* aTextureSegment = new TextureSegment();
  397. aTextureSegment->InitFromTexture(texture);
  398. return aTextureSegment;
  399. }
  400. BF_EXPORT void BF_CALLTYPE Gfx_Texture_SetBits(TextureSegment* textureSegment, int destX, int destY, int destWidth, int destHeight, int srcPitch, uint32* bits)
  401. {
  402. textureSegment->SetBits(destX, destY, destWidth, destHeight, srcPitch, bits);
  403. }
  404. BF_EXPORT void BF_CALLTYPE Gfx_Texture_GetBits(TextureSegment* textureSegment, int srcX, int srcY, int srcWidth, int srcHeight, int destPitch, uint32* bits)
  405. {
  406. textureSegment->GetBits(srcX, srcY, srcWidth, srcHeight, destPitch, bits);
  407. }
  408. BF_EXPORT void BF_CALLTYPE Gfx_Texture_Delete(TextureSegment* textureSegment)
  409. {
  410. textureSegment->mTexture->Release();
  411. delete textureSegment;
  412. }
  413. BF_EXPORT int BF_CALLTYPE Gfx_Texture_GetWidth(TextureSegment* textureSegment)
  414. {
  415. return (int) textureSegment->mScaleX;
  416. }
  417. BF_EXPORT int BF_CALLTYPE Gfx_Texture_GetHeight(TextureSegment* textureSegment)
  418. {
  419. return (int) textureSegment->mScaleY;
  420. }
  421. BF_EXPORT void BF_CALLTYPE Gfx_ModifyTextureSegment(TextureSegment* destTextureSegment, TextureSegment* srcTextureSegment, int srcX, int srcY, int srcWidth, int srcHeight)
  422. {
  423. if (destTextureSegment->mTexture != srcTextureSegment->mTexture)
  424. {
  425. destTextureSegment->mTexture->Release();
  426. destTextureSegment->mTexture = srcTextureSegment->mTexture;
  427. destTextureSegment->mTexture->AddRef();
  428. }
  429. Texture* texture = srcTextureSegment->mTexture;
  430. destTextureSegment->mU1 = (srcX / (float) texture->mWidth) + srcTextureSegment->mU1;
  431. destTextureSegment->mV1 = (srcY / (float) texture->mHeight) + srcTextureSegment->mV1;
  432. destTextureSegment->mU2 = ((srcX + srcWidth) / (float) texture->mWidth) + srcTextureSegment->mU1;
  433. destTextureSegment->mV2 = ((srcY + srcHeight) / (float) texture->mHeight) + srcTextureSegment->mV1;
  434. destTextureSegment->mScaleX = (float)abs(srcWidth);
  435. destTextureSegment->mScaleY = (float)abs(srcHeight);
  436. }
  437. int32 FormatI32(const StringView& str)
  438. {
  439. String val = String(str);
  440. if (val.StartsWith('#'))
  441. {
  442. return (int32)strtoll(val.c_str() + 1, NULL, 16);
  443. }
  444. if (val.StartsWith("0x"))
  445. {
  446. return (int32)strtoll(val.c_str() + 2, NULL, 16);
  447. }
  448. return atoi(val.c_str());
  449. }
  450. BF_EXPORT void BF_CALLTYPE Gfx_ApplyEffect(TextureSegment* destTextureSegment, TextureSegment* srcTextureSegment, char* optionsPtr)
  451. {
  452. BaseImageEffect* effect = NULL;
  453. defer({
  454. delete effect;
  455. });
  456. bool needsPremultiply = false;
  457. String options = optionsPtr;
  458. for (auto line : options.Split('\n'))
  459. {
  460. int eqPos = (int)line.IndexOf('=');
  461. if (eqPos != -1)
  462. {
  463. StringView cmd = StringView(line, 0, eqPos);
  464. StringView value = StringView(line, eqPos + 1);
  465. if (cmd == "Effect")
  466. {
  467. if (value == "Stroke")
  468. {
  469. auto strokeEffect = new ImageStrokeEffect();
  470. effect = strokeEffect;
  471. strokeEffect->mPosition = 'OutF';
  472. strokeEffect->mSize = 1;
  473. strokeEffect->mColorFill.mColor = 0xFF000000;
  474. }
  475. }
  476. if (cmd == "Size")
  477. {
  478. int32 size = FormatI32(value);
  479. if (auto strokeEffect = dynamic_cast<ImageStrokeEffect*>(effect))
  480. {
  481. strokeEffect->mSize = size;
  482. }
  483. }
  484. if (cmd == "Color")
  485. {
  486. uint32 color = (uint32)FormatI32(value);
  487. if (auto strokeEffect = dynamic_cast<ImageStrokeEffect*>(effect))
  488. {
  489. strokeEffect->mColorFill.mColor = color;
  490. if ((color & 0x00FFFFFF) != 0)
  491. needsPremultiply = true;
  492. }
  493. }
  494. }
  495. }
  496. if (effect != NULL)
  497. {
  498. ImageData destImageData;
  499. ImageData srcImageData;
  500. RectF srcRect = srcTextureSegment->GetRect();
  501. RectF destRect = destTextureSegment->GetRect();
  502. destTextureSegment->GetImageData(destImageData);
  503. srcImageData.CreateNew(destImageData.mWidth, destImageData.mHeight);
  504. srcTextureSegment->GetImageData(srcImageData,
  505. (int)(destRect.width - srcRect.width) / 2,
  506. (int)(destRect.height - srcRect.height) / 2);
  507. effect->Apply(NULL, &srcImageData, &destImageData);
  508. if (needsPremultiply)
  509. destImageData.PremultiplyAlpha();
  510. destTextureSegment->SetImageData(destImageData);
  511. }
  512. }
  513. BF_EXPORT TextureSegment* BF_CALLTYPE Gfx_CreateTextureSegment(TextureSegment* textureSegment, int srcX, int srcY, int srcWidth, int srcHeight)
  514. {
  515. Texture* texture = textureSegment->mTexture;
  516. texture->AddRef();
  517. TextureSegment* aTextureSegment = new TextureSegment();
  518. aTextureSegment->mTexture = texture;
  519. aTextureSegment->mU1 = (srcX / (float) texture->mWidth) + textureSegment->mU1;
  520. aTextureSegment->mV1 = (srcY / (float) texture->mHeight) + textureSegment->mV1;
  521. aTextureSegment->mU2 = ((srcX + srcWidth) / (float) texture->mWidth) + textureSegment->mU1;
  522. aTextureSegment->mV2 = ((srcY + srcHeight) / (float) texture->mHeight) + textureSegment->mV1;
  523. aTextureSegment->mScaleX = (float)abs(srcWidth);
  524. aTextureSegment->mScaleY = (float)abs(srcHeight);
  525. return aTextureSegment;
  526. }
  527. BF_EXPORT void BF_CALLTYPE Gfx_SetDrawSize(TextureSegment* textureSegment, int width, int height)
  528. {
  529. textureSegment->mScaleX = (float)abs(width);
  530. textureSegment->mScaleY = (float)abs(height);
  531. }
  532. BF_EXPORT void BF_CALLTYPE Gfx_DrawTextureSegment(TextureSegment* textureSegment, float a, float b, float c, float d, float tx, float ty, float z, uint32 color, int pixelSnapping)
  533. {
  534. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  535. drawLayer->SetTexture(0, textureSegment->mTexture);
  536. DefaultVertex3D* v = (DefaultVertex3D*)drawLayer->AllocStrip(4);
  537. if ((pixelSnapping == 1) ||
  538. ((pixelSnapping == 2) && (a == 1.0f) && (b == 0) && (c == 0) && (d == 1.0f)))
  539. {
  540. tx = (float) (int) (tx + 100000) - 100000;
  541. ty = (float) (int) (ty + 100000) - 100000;
  542. }
  543. a *= textureSegment->mScaleX;
  544. b *= textureSegment->mScaleX;
  545. c *= textureSegment->mScaleY;
  546. d *= textureSegment->mScaleY;
  547. v[0].Set(tx, ty, z, textureSegment->mU1, textureSegment->mV1, color);
  548. v[1].Set(tx + a, ty + b, z, textureSegment->mU2, textureSegment->mV1, color);
  549. v[2].Set(tx + c, ty + d, z, textureSegment->mU1, textureSegment->mV2, color);
  550. v[3].Set(tx + (a + c), ty + (b + d), z, textureSegment->mU2, textureSegment->mV2, color);
  551. gPixelsDrawn += (int)((a + b) * (c + d));
  552. }
  553. static TextureSegment* gCurTextureSegment = NULL;
  554. static DefaultVertex3D* gCurAllocVertices = NULL;
  555. BF_EXPORT void BF_CALLTYPE Gfx_AllocTris(TextureSegment* textureSegment, int vtxCount)
  556. {
  557. gCurTextureSegment = textureSegment;
  558. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  559. drawLayer->SetTexture(0, textureSegment->mTexture);
  560. gCurAllocVertices = (DefaultVertex3D*)gBFApp->mRenderDevice->mCurDrawLayer->AllocTris(vtxCount);
  561. }
  562. BF_EXPORT void BF_CALLTYPE Gfx_SetDrawVertex(int idx, float x, float y, float z, float u, float v, uint32 color)
  563. {
  564. gCurAllocVertices[idx].Set(x, y, z,
  565. gCurTextureSegment->mU1 + u * (gCurTextureSegment->mU2 - gCurTextureSegment->mU1),
  566. gCurTextureSegment->mV1 + v * (gCurTextureSegment->mV2 - gCurTextureSegment->mV1), color);
  567. }
  568. BF_EXPORT void BF_CALLTYPE Gfx_CopyDrawVertex(int destIdx, int srcIdx)
  569. {
  570. gCurAllocVertices[destIdx] = gCurAllocVertices[srcIdx];
  571. }
  572. BF_EXPORT void BF_CALLTYPE Gfx_DrawQuads(TextureSegment* textureSegment, DefaultVertex3D* vertices, int vtxCount)
  573. {
  574. /*for (int vtxIdx = 0; vtxIdx < vtxCount; vtxIdx += 4)
  575. {
  576. Vertex3D* v = gBFApp->mRenderDevice->mCurDrawLayer->AllocStrip(textureSegment->mTexture, drawType != 0, 4);
  577. v[0] = vertices[vtxIdx];
  578. v[1] = vertices[vtxIdx + 1];
  579. v[2] = vertices[vtxIdx + 2];
  580. v[3] = vertices[vtxIdx + 3];
  581. }
  582. return;*/
  583. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  584. drawLayer->SetTexture(0, textureSegment->mTexture);
  585. DefaultVertex3D* vtxInPtr = vertices;
  586. int curIdx = 0;
  587. while (curIdx < vtxCount)
  588. {
  589. //int batchSize = std::min(128, vtxCount - curIdx);
  590. int batchSize = std::min(16*1024, vtxCount - curIdx);
  591. uint16 idxOfs;
  592. DefaultVertex3D* vtxPtr;
  593. uint16* idxPtr;
  594. gBFApp->mRenderDevice->mCurDrawLayer->AllocIndexed(batchSize, batchSize * 6 / 4, (void**)&vtxPtr, &idxPtr, &idxOfs);
  595. for (int vtxIdx = 0; vtxIdx < batchSize; vtxIdx += 4)
  596. {
  597. *(vtxPtr++) = *(vtxInPtr++);
  598. *(vtxPtr++) = *(vtxInPtr++);
  599. *(vtxPtr++) = *(vtxInPtr++);
  600. *(vtxPtr++) = *(vtxInPtr++);
  601. *(idxPtr++) = idxOfs;
  602. *(idxPtr++) = idxOfs + 1;
  603. *(idxPtr++) = idxOfs + 2;
  604. *(idxPtr++) = idxOfs + 1;
  605. *(idxPtr++) = idxOfs + 2;
  606. *(idxPtr++) = idxOfs + 3;
  607. /*int curIdxIdx = idxPtr - gBFApp->mRenderDevice->mCurDrawLayer->mDrawBatchList.mTail->mIndices;
  608. BF_ASSERT(curIdxIdx <= gBFApp->mRenderDevice->mCurDrawLayer->mDrawBatchList.mTail->mAllocatedIndices);
  609. int curVtxIdx = vtxPtr - gBFApp->mRenderDevice->mCurDrawLayer->mDrawBatchList.mTail->mVertices;
  610. BF_ASSERT(curVtxIdx <= gBFApp->mRenderDevice->mCurDrawLayer->mDrawBatchList.mTail->mAllocatedVertices);*/
  611. idxOfs += 4;
  612. }
  613. curIdx += batchSize;
  614. }
  615. }
  616. BF_EXPORT void BF_CALLTYPE Gfx_DrawIndexedVertices(int vertexSize, void* vtxData, int vtxCount, uint16* idxData, int idxCount)
  617. {
  618. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  619. uint16 idxOfs;
  620. void* drawBatchVtxPtr;
  621. uint16* drawBatchIdxPtr;
  622. gBFApp->mRenderDevice->mCurDrawLayer->AllocIndexed(vtxCount, idxCount, (void**)&drawBatchVtxPtr, &drawBatchIdxPtr, &idxOfs);
  623. BF_ASSERT(gBFApp->mRenderDevice->mCurDrawLayer->mCurDrawBatch->mVtxSize == vertexSize);
  624. uint16* idxPtr = idxData;
  625. for (int idxIdx = 0; idxIdx < idxCount; idxIdx++)
  626. *(drawBatchIdxPtr++) = *(idxPtr++) + idxOfs;
  627. memcpy(drawBatchVtxPtr, vtxData, vertexSize * vtxCount);
  628. }
  629. BF_EXPORT void BF_CALLTYPE Gfx_DrawIndexedVertices2D(int vertexSize, void* vtxData, int vtxCount, uint16* idxData, int idxCount, float a, float b, float c, float d, float tx, float ty, float z)
  630. {
  631. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  632. uint16 idxOfs;
  633. void* drawBatchVtxPtr;
  634. uint16* drawBatchIdxPtr;
  635. gBFApp->mRenderDevice->mCurDrawLayer->AllocIndexed(vtxCount, idxCount, (void**)&drawBatchVtxPtr, &drawBatchIdxPtr, &idxOfs);
  636. BF_ASSERT(gBFApp->mRenderDevice->mCurDrawLayer->mCurDrawBatch->mVtxSize == vertexSize);
  637. uint16* idxPtr = idxData;
  638. for (int idxIdx = 0; idxIdx < idxCount; idxIdx++)
  639. *(drawBatchIdxPtr++) = *(idxPtr++) + idxOfs;
  640. //memcpy(drawBatchIdxPtr, idxData, sizeof(uint16) * idxCount);
  641. //memcpy(drawBatchVtxPtr, vtxData, vertexSize * vtxCount);
  642. void* vtxPtr = vtxData;
  643. for (int vtxIdx = 0; vtxIdx < vtxCount; vtxIdx++)
  644. {
  645. Vector3* srcPos = (Vector3*)vtxPtr;
  646. Vector3* destPos = (Vector3*)drawBatchVtxPtr;
  647. destPos->mX = srcPos->mX * a + srcPos->mY * c + tx;
  648. destPos->mY = srcPos->mX * b + srcPos->mY * d + ty;
  649. destPos->mZ = srcPos->mZ + z;
  650. memcpy((uint8*)drawBatchVtxPtr + sizeof(Vector3), (uint8*)vtxPtr + sizeof(Vector3), vertexSize - sizeof(Vector3));
  651. drawBatchVtxPtr = (uint8*)drawBatchVtxPtr + vertexSize;
  652. vtxPtr = (uint8*)vtxPtr + vertexSize;
  653. }
  654. }
  655. BF_EXPORT void BF_CALLTYPE Gfx_SetShaderConstantData(int usageIdx, int slotIdx, void* constData, int size)
  656. {
  657. gBFApp->mRenderDevice->mCurDrawLayer->SetShaderConstantData(usageIdx, slotIdx, constData, size);
  658. }
  659. BF_EXPORT void BF_CALLTYPE Gfx_SetShaderConstantDataTyped(int usageIdx, int slotIdx, void* constData, int size, int* typeData, int typeCount)
  660. {
  661. gBFApp->mRenderDevice->mCurDrawLayer->SetShaderConstantDataTyped(usageIdx, slotIdx, constData, size, typeData, typeCount);
  662. }
  663. BF_EXPORT void BF_CALLTYPE Gfx_QueueRenderCmd(RenderCmd* renderCmd)
  664. {
  665. gBFApp->mRenderDevice->mCurDrawLayer->QueueRenderCmd(renderCmd);
  666. }
  667. BF_EXPORT VertexDefinition* BF_CALLTYPE Gfx_CreateVertexDefinition(VertexDefData* elementData, int numElements)
  668. {
  669. return gBFApp->mRenderDevice->CreateVertexDefinition(elementData, numElements);
  670. }
  671. BF_EXPORT void BF_CALLTYPE Gfx_VertexDefinition_Delete(VertexDefinition* vertexDefinition)
  672. {
  673. delete vertexDefinition;
  674. }
  675. BF_EXPORT void BF_CALLTYPE Gfx_CreateRenderState(RenderState* srcRenderState)
  676. {
  677. gBFApp->mRenderDevice->CreateRenderState(srcRenderState);
  678. }
  679. BF_EXPORT void BF_CALLTYPE RenderState_Delete(RenderState* renderState)
  680. {
  681. delete renderState;
  682. }
  683. BF_EXPORT void BF_CALLTYPE RenderState_SetTexWrap(RenderState* renderState, bool texWrap)
  684. {
  685. renderState->SetTexWrap(texWrap);
  686. }
  687. BF_EXPORT void BF_CALLTYPE RenderState_SetWireframe(RenderState* renderState, bool wireframe)
  688. {
  689. renderState->SetWireframe(wireframe);
  690. }
  691. BF_EXPORT void BF_CALLTYPE RenderState_SetClip(RenderState* renderState, float x, float y, float width, float height)
  692. {
  693. BF_ASSERT((width >= 0) && (height >= 0));
  694. renderState->mClipRect.x = x;
  695. renderState->mClipRect.y = y;
  696. renderState->mClipRect.width = width;
  697. renderState->mClipRect.height = height;
  698. if (!renderState->mClipped)
  699. renderState->SetClipped(true);
  700. }
  701. BF_EXPORT void BF_CALLTYPE RenderState_SetShader(RenderState* renderState, Shader* shader)
  702. {
  703. renderState->SetShader(shader);
  704. }
  705. BF_EXPORT void BF_CALLTYPE RenderState_DisableClip(RenderState* renderState)
  706. {
  707. renderState->SetClipped(false);
  708. }
  709. BF_EXPORT void BF_CALLTYPE Gfx_SetTexture_TextureSegment(int textureIdx, TextureSegment* textureSegment)
  710. {
  711. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  712. drawLayer->SetTexture(textureIdx, textureSegment->mTexture);
  713. }
  714. BF_EXPORT void BF_CALLTYPE RenderState_SetDepthFunc(RenderState* renderState, int depthFunc)
  715. {
  716. renderState->SetDepthFunc((DepthFunc)depthFunc);
  717. }
  718. BF_EXPORT void BF_CALLTYPE RenderState_SetDepthWrite(RenderState* renderState, int depthWrite)
  719. {
  720. renderState->SetWriteDepthBuffer(depthWrite != 0);
  721. }
  722. BF_EXPORT void BF_CALLTYPE RenderState_SetTopology(RenderState* renderState, int topology)
  723. {
  724. renderState->SetTopology((Topology3D)topology);
  725. }
  726. BF_EXPORT Shader* BF_CALLTYPE Gfx_LoadShader(const char* fileName, VertexDefinition* vertexDefinition)
  727. {
  728. return gBFApp->mRenderDevice->LoadShader(fileName, vertexDefinition);
  729. }
  730. BF_EXPORT void BF_CALLTYPE Gfx_SetRenderState(RenderState* renderState)
  731. {
  732. BF_ASSERT(renderState->mShader != NULL);
  733. gBFApp->mRenderDevice->SetRenderState(renderState);
  734. }
  735. BF_EXPORT void BF_CALLTYPE Gfx_Shader_Delete(Shader* shader)
  736. {
  737. delete shader;
  738. }
  739. BF_EXPORT ShaderParam* BF_CALLTYPE Gfx_GetShaderParam(Shader* shader, const char* shaderName)
  740. {
  741. return shader->GetShaderParam(shaderName);
  742. }
  743. BF_EXPORT void BF_CALLTYPE BFInput_Destroy(BFInputDevice* inputDevice)
  744. {
  745. delete inputDevice;
  746. }
  747. BF_EXPORT const char* BF_CALLTYPE BFInput_GetState(BFInputDevice* inputDevice)
  748. {
  749. String& outString = *gBeefySys_TLStrReturn.Get();
  750. outString = inputDevice->GetState();
  751. return outString.c_str();
  752. }
  753. BF_EXPORT int BF_CALLTYPE BF_TickCount()
  754. {
  755. return (int) BFTickCount();
  756. }
  757. BF_EXPORT int64 BF_CALLTYPE BF_TickCountMicroFast()
  758. {
  759. return (int) BFGetTickCountMicroFast();
  760. }
  761. BF_EXPORT void BF_CALLTYPE BF_Test()
  762. {
  763. BF_ASSERT(false);
  764. int iArr[] = {2, 3, 4};
  765. for (int i = 0; i < 10; i++)
  766. OutputDebugStrF("Hey %d\n", i);
  767. OutputDebugStrF("Break\n");
  768. for (int i : iArr)
  769. OutputDebugStrF("Hey %d\n", i);
  770. }
  771. BF_EXPORT void* BF_CALLTYPE MemLogger_Create(const char* memName, int size)
  772. {
  773. MemLogger* memLogger = new MemLogger();
  774. if (!memLogger->Create(memName, size))
  775. {
  776. delete memLogger;
  777. return NULL;
  778. }
  779. return memLogger;
  780. }
  781. BF_EXPORT void BF_CALLTYPE MemLogger_Write(MemLogger* memLogger, void* ptr, int size)
  782. {
  783. memLogger->Write(ptr, size);
  784. }
  785. BF_EXPORT void BF_CALLTYPE MemLogger_Delete(MemLogger* memLogger)
  786. {
  787. delete memLogger;
  788. }
  789. BF_EXPORT const char* BF_CALLTYPE MemLogger_Get(const char* memName)
  790. {
  791. MemLogger memLogger;
  792. String& outString = *gBeefySys_TLStrReturn.Get();
  793. outString.Clear();
  794. if (!memLogger.Get(memName, outString))
  795. return NULL;
  796. return outString.c_str();
  797. }