BeefySysLib.cpp 24 KB

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