2
0

BeefySysLib.cpp 25 KB

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