BeefySysLib.cpp 21 KB

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