BeefySysLib.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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_DrawFunc drawFunc)
  184. {
  185. gBFApp->mUpdateFunc = updateFunc;
  186. gBFApp->mDrawFunc = drawFunc;
  187. //public delegate void UpdateProc();
  188. }
  189. BF_EXPORT BFWindow* BF_CALLTYPE BFApp_CreateWindow(BFWindow* parent, const char* title, int x, int y, int width, int height, int windowFlags)
  190. {
  191. return gBFApp->CreateNewWindow(parent, title, x, y, width, height, windowFlags);
  192. }
  193. BF_EXPORT void BF_CALLTYPE BFApp_RemoveWindow(BFWindow* window)
  194. {
  195. delete window;
  196. }
  197. BF_EXPORT void BF_CALLTYPE BFApp_SetCursor(int cursor)
  198. {
  199. gBFApp->SetCursor(cursor);
  200. }
  201. BF_EXPORT void* BF_CALLTYPE BFApp_GetClipboardData(const char* format, int* size)
  202. {
  203. return gBFApp->GetClipboardData(format, size);
  204. }
  205. BF_EXPORT void BF_CALLTYPE BFApp_ReleaseClipboardData(void* ptr)
  206. {
  207. return gBFApp->ReleaseClipboardData(ptr);
  208. }
  209. BF_EXPORT void BF_CALLTYPE BFApp_SetClipboardData(const char* format, void* ptr, int size, int resetClipboard)
  210. {
  211. return gBFApp->SetClipboardData(format, ptr, size, resetClipboard != 0);
  212. }
  213. BF_EXPORT void BF_CALLTYPE BFApp_CheckMemory()
  214. {
  215. #ifdef BF_PLATFORM_WINDOWS
  216. _CrtCheckMemory();
  217. #endif
  218. }
  219. BF_EXPORT void BF_CALLTYPE BFApp_RehupMouse()
  220. {
  221. }
  222. BF_EXPORT const char* BF_CALLTYPE BFApp_EnumerateInputDevices()
  223. {
  224. String& outString = *gBeefySys_TLStrReturn.Get();
  225. outString = gBFApp->EnumerateInputDevices();
  226. return outString.c_str();
  227. }
  228. BF_EXPORT BFInputDevice* BFApp_CreateInputDevice(const char* guid)
  229. {
  230. return gBFApp->CreateInputDevice(guid);
  231. }
  232. BF_EXPORT BFSoundManager* BF_CALLTYPE BFApp_GetSoundManager()
  233. {
  234. return gBFApp->GetSoundManager();
  235. }
  236. ///
  237. BF_EXPORT void BF_CALLTYPE BFWindow_SetCallbacks(BFWindow* window, BFWindow_MovedFunc movedFunc, BFWindow_CloseQueryFunc closeQueryFunc, BFWindow_ClosedFunc closedFunc,
  238. BFWindow_GotFocusFunc gotFocusFunc, BFWindow_LostFocusFunc lostFocusFunc,
  239. BFWindow_KeyCharFunc keyCharFunc, BFWindow_KeyDownFunc keyDownFunc, BFWindow_KeyUpFunc keyUpFunc, BFWindow_HitTestFunc hitTestFunc,
  240. BFWindow_MouseMove mouseMoveFunc, BFWindow_MouseProxyMove mouseProxyMoveFunc,
  241. BFWindow_MouseDown mouseDownFunc, BFWindow_MouseUp mouseUpFunc, BFWindow_MouseWheel mouseWheelFunc, BFWindow_MouseLeave mouseLeaveFunc,
  242. BFWindow_MenuItemSelectedFunc menuItemSelectedFunc, BFWindow_DragDropFileFunc dragDropFileFunc)
  243. {
  244. window->mMovedFunc = movedFunc;
  245. window->mCloseQueryFunc = closeQueryFunc;
  246. window->mClosedFunc = closedFunc;
  247. window->mGotFocusFunc = gotFocusFunc;
  248. window->mLostFocusFunc = lostFocusFunc;
  249. window->mKeyCharFunc = keyCharFunc;
  250. window->mKeyDownFunc = keyDownFunc;
  251. window->mKeyUpFunc = keyUpFunc;
  252. window->mHitTestFunc = hitTestFunc;
  253. window->mMouseMoveFunc = mouseMoveFunc;
  254. window->mMouseProxyMoveFunc = mouseProxyMoveFunc;
  255. window->mMouseDownFunc = mouseDownFunc;
  256. window->mMouseUpFunc = mouseUpFunc;
  257. window->mMouseWheelFunc = mouseWheelFunc;
  258. window->mMouseLeaveFunc = mouseLeaveFunc;
  259. window->mMenuItemSelectedFunc = menuItemSelectedFunc;
  260. window->mDragDropFileFunc = dragDropFileFunc;
  261. }
  262. BF_EXPORT void* BFWindow_GetNativeUnderlying(BFWindow* window)
  263. {
  264. return window->GetUnderlying();
  265. }
  266. BF_EXPORT void BF_CALLTYPE BFWindow_MovedDelegate(BFWindow* window, BFWindow_MovedFunc movedFunc)
  267. {
  268. window->mMovedFunc = movedFunc;
  269. }
  270. BF_EXPORT void BF_CALLTYPE BFWindow_SetTitle(BFWindow* window, const char* title)
  271. {
  272. window->SetTitle(title);
  273. }
  274. BF_EXPORT void BF_CALLTYPE BFWindow_SetMinimumSize(BFWindow* window, int minWidth, int minHeight, bool clientSized)
  275. {
  276. window->SetMinimumSize(minWidth, minHeight, clientSized);
  277. }
  278. 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)
  279. {
  280. window->GetPosition(x, y, width, height, clientX, clientY, clientWidth, clientHeight);
  281. }
  282. BF_EXPORT void BF_CALLTYPE BFWindow_GetPlacement(BFWindow* window, int* normX, int* normY, int* normWidth, int* normHeight, int* showKind)
  283. {
  284. window->GetPlacement(normX, normY, normWidth, normHeight, showKind);
  285. }
  286. BF_EXPORT void BF_CALLTYPE BFWindow_Resize(BFWindow* window, int x, int y, int width, int height, int showKind)
  287. {
  288. window->Resize(x, y, width, height, showKind);
  289. }
  290. BF_EXPORT void BF_CALLTYPE BFWindow_SetForeground(BFWindow* window)
  291. {
  292. window->SetForeground();
  293. }
  294. BF_EXPORT void BF_CALLTYPE BFWindow_SetNonExclusiveMouseCapture(BFWindow* window)
  295. {
  296. window->SetNonExclusiveMouseCapture();
  297. }
  298. BF_EXPORT void BF_CALLTYPE BFWindow_LostFocus(BFWindow* window, BFWindow* newFocus)
  299. {
  300. window->LostFocus(newFocus);
  301. }
  302. BF_EXPORT void BF_CALLTYPE BFWindow_SetAlpha(BFWindow* window, float alpha, uint32 destAlphaSrcMask, int mouseVisible)
  303. {
  304. window->SetAlpha(alpha, destAlphaSrcMask, mouseVisible != 0);
  305. }
  306. BF_EXPORT void BF_CALLTYPE BFWindow_CaptureMouse(BFWindow* window)
  307. {
  308. window->CaptureMouse();
  309. }
  310. BF_EXPORT bool BF_CALLTYPE BFWindow_IsMouseCaptured(BFWindow* window)
  311. {
  312. return window->IsMouseCaptured();
  313. }
  314. BF_EXPORT void BF_CALLTYPE BFWindow_SetMouseVisible(BFWindow* window, bool mouseVisible)
  315. {
  316. window->SetMouseVisible(mouseVisible);
  317. }
  318. BF_EXPORT void BF_CALLTYPE BFWindow_SetClientPosition(BFWindow* window, int x, int y)
  319. {
  320. window->SetClientPosition(x, y);
  321. }
  322. 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)
  323. {
  324. return window->AddMenuItem(parent, insertIdx, text, hotKey, bitmap, enabled != 0, checkState, radioCheck != 0);
  325. }
  326. 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)
  327. {
  328. window->ModifyMenuItem(item, text, hotKey, bitmap, enabled != 0, checkState, radioCheck != 0);
  329. }
  330. BF_EXPORT void BF_CALLTYPE BFWindow_DeleteMenuItem(BFWindow* window, BFMenu* item)
  331. {
  332. window->RemoveMenuItem(item);
  333. delete item;
  334. }
  335. BF_EXPORT void BF_CALLTYPE BFWindow_Close(BFWindow* window, int force)
  336. {
  337. if (force != 0)
  338. gBFApp->RemoveWindow(window);
  339. else
  340. window->TryClose();
  341. }
  342. BF_EXPORT int BF_CALLTYPE BFWindow_GetDPI(BFWindow* window)
  343. {
  344. return window->GetDPI();
  345. }
  346. ///
  347. BF_EXPORT TextureSegment* BF_CALLTYPE Gfx_CreateRenderTarget(int width, int height, int destAlpha)
  348. {
  349. Texture* texture = gBFApp->mRenderDevice->CreateRenderTarget(width, height, destAlpha != 0);
  350. TextureSegment* aTextureSegment = new TextureSegment();
  351. aTextureSegment->InitFromTexture(texture);
  352. return aTextureSegment;
  353. }
  354. BF_EXPORT TextureSegment* BF_CALLTYPE Gfx_CreateDynTexture(int width, int height)
  355. {
  356. Texture* texture = gBFApp->mRenderDevice->CreateDynTexture(width, height);
  357. TextureSegment* aTextureSegment = new TextureSegment();
  358. aTextureSegment->InitFromTexture(texture);
  359. return aTextureSegment;
  360. }
  361. BF_EXPORT TextureSegment* BF_CALLTYPE Gfx_LoadTexture(const char* fileName, int flags)
  362. {
  363. Texture* texture = gBFApp->mRenderDevice->LoadTexture(fileName, flags);
  364. if (texture == NULL)
  365. return NULL;
  366. TextureSegment* aTextureSegment = new TextureSegment();
  367. aTextureSegment->InitFromTexture(texture);
  368. return aTextureSegment;
  369. }
  370. BF_EXPORT void BF_CALLTYPE Gfx_Texture_SetBits(TextureSegment* textureSegment, int destX, int destY, int destWidth, int destHeight, int srcPitch, uint32* bits)
  371. {
  372. textureSegment->mTexture->SetBits(destX, destY, destWidth, destHeight, srcPitch, bits);
  373. }
  374. BF_EXPORT void BF_CALLTYPE Gfx_Texture_GetBits(TextureSegment* textureSegment, int srcX, int srcY, int srcWidth, int srcHeight, int destPitch, uint32* bits)
  375. {
  376. textureSegment->mTexture->GetBits(srcX, srcY, srcWidth, srcHeight, destPitch, bits);
  377. }
  378. BF_EXPORT void BF_CALLTYPE Gfx_Texture_Delete(TextureSegment* textureSegment)
  379. {
  380. textureSegment->mTexture->Release();
  381. delete textureSegment;
  382. }
  383. BF_EXPORT int BF_CALLTYPE Gfx_Texture_GetWidth(TextureSegment* textureSegment)
  384. {
  385. return (int) textureSegment->mScaleX;
  386. }
  387. BF_EXPORT int BF_CALLTYPE Gfx_Texture_GetHeight(TextureSegment* textureSegment)
  388. {
  389. return (int) textureSegment->mScaleY;
  390. }
  391. BF_EXPORT void BF_CALLTYPE Gfx_ModifyTextureSegment(TextureSegment* destTextureSegment, TextureSegment* srcTextureSegment, int srcX, int srcY, int srcWidth, int srcHeight)
  392. {
  393. if (destTextureSegment->mTexture != srcTextureSegment->mTexture)
  394. {
  395. destTextureSegment->mTexture->Release();
  396. destTextureSegment->mTexture = srcTextureSegment->mTexture;
  397. destTextureSegment->mTexture->AddRef();
  398. }
  399. Texture* texture = srcTextureSegment->mTexture;
  400. destTextureSegment->mU1 = (srcX / (float) texture->mWidth) + srcTextureSegment->mU1;
  401. destTextureSegment->mV1 = (srcY / (float) texture->mHeight) + srcTextureSegment->mV1;
  402. destTextureSegment->mU2 = ((srcX + srcWidth) / (float) texture->mWidth) + srcTextureSegment->mU1;
  403. destTextureSegment->mV2 = ((srcY + srcHeight) / (float) texture->mHeight) + srcTextureSegment->mV1;
  404. destTextureSegment->mScaleX = (float)abs(srcWidth);
  405. destTextureSegment->mScaleY = (float)abs(srcHeight);
  406. }
  407. BF_EXPORT TextureSegment* BF_CALLTYPE Gfx_CreateTextureSegment(TextureSegment* textureSegment, int srcX, int srcY, int srcWidth, int srcHeight)
  408. {
  409. Texture* texture = textureSegment->mTexture;
  410. texture->AddRef();
  411. TextureSegment* aTextureSegment = new TextureSegment();
  412. aTextureSegment->mTexture = texture;
  413. aTextureSegment->mU1 = (srcX / (float) texture->mWidth) + textureSegment->mU1;
  414. aTextureSegment->mV1 = (srcY / (float) texture->mHeight) + textureSegment->mV1;
  415. aTextureSegment->mU2 = ((srcX + srcWidth) / (float) texture->mWidth) + textureSegment->mU1;
  416. aTextureSegment->mV2 = ((srcY + srcHeight) / (float) texture->mHeight) + textureSegment->mV1;
  417. aTextureSegment->mScaleX = (float)abs(srcWidth);
  418. aTextureSegment->mScaleY = (float)abs(srcHeight);
  419. return aTextureSegment;
  420. }
  421. BF_EXPORT void BF_CALLTYPE Gfx_SetDrawSize(TextureSegment* textureSegment, int width, int height)
  422. {
  423. textureSegment->mScaleX = (float)abs(width);
  424. textureSegment->mScaleY = (float)abs(height);
  425. }
  426. 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)
  427. {
  428. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  429. drawLayer->SetTexture(0, textureSegment->mTexture);
  430. DefaultVertex3D* v = (DefaultVertex3D*)drawLayer->AllocStrip(4);
  431. if ((pixelSnapping == 1) ||
  432. ((pixelSnapping == 2) && (a == 1.0f) && (b == 0) && (c == 0) && (d == 1.0f)))
  433. {
  434. tx = (float) (int) (tx + 100000) - 100000;
  435. ty = (float) (int) (ty + 100000) - 100000;
  436. }
  437. a *= textureSegment->mScaleX;
  438. b *= textureSegment->mScaleX;
  439. c *= textureSegment->mScaleY;
  440. d *= textureSegment->mScaleY;
  441. v[0].Set(tx, ty, z, textureSegment->mU1, textureSegment->mV1, color);
  442. v[1].Set(tx + a, ty + b, z, textureSegment->mU2, textureSegment->mV1, color);
  443. v[2].Set(tx + c, ty + d, z, textureSegment->mU1, textureSegment->mV2, color);
  444. v[3].Set(tx + (a + c), ty + (b + d), z, textureSegment->mU2, textureSegment->mV2, color);
  445. gPixelsDrawn += (int)((a + b) * (c + d));
  446. }
  447. static TextureSegment* gCurTextureSegment = NULL;
  448. static DefaultVertex3D* gCurAllocVertices = NULL;
  449. BF_EXPORT void BF_CALLTYPE Gfx_AllocTris(TextureSegment* textureSegment, int vtxCount)
  450. {
  451. gCurTextureSegment = textureSegment;
  452. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  453. drawLayer->SetTexture(0, textureSegment->mTexture);
  454. gCurAllocVertices = (DefaultVertex3D*)gBFApp->mRenderDevice->mCurDrawLayer->AllocTris(vtxCount);
  455. }
  456. BF_EXPORT void BF_CALLTYPE Gfx_SetDrawVertex(int idx, float x, float y, float z, float u, float v, uint32 color)
  457. {
  458. gCurAllocVertices[idx].Set(x, y, z,
  459. gCurTextureSegment->mU1 + u * (gCurTextureSegment->mU2 - gCurTextureSegment->mU1),
  460. gCurTextureSegment->mV1 + v * (gCurTextureSegment->mV2 - gCurTextureSegment->mV1), color);
  461. }
  462. BF_EXPORT void BF_CALLTYPE Gfx_CopyDrawVertex(int destIdx, int srcIdx)
  463. {
  464. gCurAllocVertices[destIdx] = gCurAllocVertices[srcIdx];
  465. }
  466. BF_EXPORT void BF_CALLTYPE Gfx_DrawQuads(TextureSegment* textureSegment, DefaultVertex3D* vertices, int vtxCount)
  467. {
  468. /*for (int vtxIdx = 0; vtxIdx < vtxCount; vtxIdx += 4)
  469. {
  470. Vertex3D* v = gBFApp->mRenderDevice->mCurDrawLayer->AllocStrip(textureSegment->mTexture, drawType != 0, 4);
  471. v[0] = vertices[vtxIdx];
  472. v[1] = vertices[vtxIdx + 1];
  473. v[2] = vertices[vtxIdx + 2];
  474. v[3] = vertices[vtxIdx + 3];
  475. }
  476. return;*/
  477. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  478. drawLayer->SetTexture(0, textureSegment->mTexture);
  479. DefaultVertex3D* vtxInPtr = vertices;
  480. int curIdx = 0;
  481. while (curIdx < vtxCount)
  482. {
  483. //int batchSize = std::min(128, vtxCount - curIdx);
  484. int batchSize = std::min(16*1024, vtxCount - curIdx);
  485. uint16 idxOfs;
  486. DefaultVertex3D* vtxPtr;
  487. uint16* idxPtr;
  488. gBFApp->mRenderDevice->mCurDrawLayer->AllocIndexed(batchSize, batchSize * 6 / 4, (void**)&vtxPtr, &idxPtr, &idxOfs);
  489. for (int vtxIdx = 0; vtxIdx < batchSize; vtxIdx += 4)
  490. {
  491. *(vtxPtr++) = *(vtxInPtr++);
  492. *(vtxPtr++) = *(vtxInPtr++);
  493. *(vtxPtr++) = *(vtxInPtr++);
  494. *(vtxPtr++) = *(vtxInPtr++);
  495. *(idxPtr++) = idxOfs;
  496. *(idxPtr++) = idxOfs + 1;
  497. *(idxPtr++) = idxOfs + 2;
  498. *(idxPtr++) = idxOfs + 1;
  499. *(idxPtr++) = idxOfs + 2;
  500. *(idxPtr++) = idxOfs + 3;
  501. /*int curIdxIdx = idxPtr - gBFApp->mRenderDevice->mCurDrawLayer->mDrawBatchList.mTail->mIndices;
  502. BF_ASSERT(curIdxIdx <= gBFApp->mRenderDevice->mCurDrawLayer->mDrawBatchList.mTail->mAllocatedIndices);
  503. int curVtxIdx = vtxPtr - gBFApp->mRenderDevice->mCurDrawLayer->mDrawBatchList.mTail->mVertices;
  504. BF_ASSERT(curVtxIdx <= gBFApp->mRenderDevice->mCurDrawLayer->mDrawBatchList.mTail->mAllocatedVertices);*/
  505. idxOfs += 4;
  506. }
  507. curIdx += batchSize;
  508. }
  509. }
  510. BF_EXPORT void BF_CALLTYPE Gfx_DrawIndexedVertices(int vertexSize, void* vtxData, int vtxCount, uint16* idxData, int idxCount)
  511. {
  512. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  513. uint16 idxOfs;
  514. void* drawBatchVtxPtr;
  515. uint16* drawBatchIdxPtr;
  516. gBFApp->mRenderDevice->mCurDrawLayer->AllocIndexed(vtxCount, idxCount, (void**)&drawBatchVtxPtr, &drawBatchIdxPtr, &idxOfs);
  517. BF_ASSERT(gBFApp->mRenderDevice->mCurDrawLayer->mCurDrawBatch->mVtxSize == vertexSize);
  518. uint16* idxPtr = idxData;
  519. for (int idxIdx = 0; idxIdx < idxCount; idxIdx++)
  520. *(drawBatchIdxPtr++) = *(idxPtr++) + idxOfs;
  521. memcpy(drawBatchVtxPtr, vtxData, vertexSize * vtxCount);
  522. }
  523. 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)
  524. {
  525. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  526. uint16 idxOfs;
  527. void* drawBatchVtxPtr;
  528. uint16* drawBatchIdxPtr;
  529. gBFApp->mRenderDevice->mCurDrawLayer->AllocIndexed(vtxCount, idxCount, (void**)&drawBatchVtxPtr, &drawBatchIdxPtr, &idxOfs);
  530. BF_ASSERT(gBFApp->mRenderDevice->mCurDrawLayer->mCurDrawBatch->mVtxSize == vertexSize);
  531. uint16* idxPtr = idxData;
  532. for (int idxIdx = 0; idxIdx < idxCount; idxIdx++)
  533. *(drawBatchIdxPtr++) = *(idxPtr++) + idxOfs;
  534. //memcpy(drawBatchIdxPtr, idxData, sizeof(uint16) * idxCount);
  535. //memcpy(drawBatchVtxPtr, vtxData, vertexSize * vtxCount);
  536. void* vtxPtr = vtxData;
  537. for (int vtxIdx = 0; vtxIdx < vtxCount; vtxIdx++)
  538. {
  539. Vector3* srcPos = (Vector3*)vtxPtr;
  540. Vector3* destPos = (Vector3*)drawBatchVtxPtr;
  541. destPos->mX = srcPos->mX * a + srcPos->mY * c + tx;
  542. destPos->mY = srcPos->mX * b + srcPos->mY * d + ty;
  543. destPos->mZ = srcPos->mZ + z;
  544. memcpy((uint8*)drawBatchVtxPtr + sizeof(Vector3), (uint8*)vtxPtr + sizeof(Vector3), vertexSize - sizeof(Vector3));
  545. drawBatchVtxPtr = (uint8*)drawBatchVtxPtr + vertexSize;
  546. vtxPtr = (uint8*)vtxPtr + vertexSize;
  547. }
  548. }
  549. BF_EXPORT void BF_CALLTYPE Gfx_SetShaderConstantData(int usageIdx, int slotIdx, void* constData, int size)
  550. {
  551. gBFApp->mRenderDevice->mCurDrawLayer->SetShaderConstantData(usageIdx, slotIdx, constData, size);
  552. }
  553. BF_EXPORT void BF_CALLTYPE Gfx_SetShaderConstantDataTyped(int usageIdx, int slotIdx, void* constData, int size, int* typeData, int typeCount)
  554. {
  555. gBFApp->mRenderDevice->mCurDrawLayer->SetShaderConstantDataTyped(usageIdx, slotIdx, constData, size, typeData, typeCount);
  556. }
  557. BF_EXPORT void BF_CALLTYPE Gfx_QueueRenderCmd(RenderCmd* renderCmd)
  558. {
  559. gBFApp->mRenderDevice->mCurDrawLayer->QueueRenderCmd(renderCmd);
  560. }
  561. BF_EXPORT VertexDefinition* BF_CALLTYPE Gfx_CreateVertexDefinition(VertexDefData* elementData, int numElements)
  562. {
  563. return gBFApp->mRenderDevice->CreateVertexDefinition(elementData, numElements);
  564. }
  565. BF_EXPORT void BF_CALLTYPE Gfx_VertexDefinition_Delete(VertexDefinition* vertexDefinition)
  566. {
  567. delete vertexDefinition;
  568. }
  569. BF_EXPORT void BF_CALLTYPE Gfx_CreateRenderState(RenderState* srcRenderState)
  570. {
  571. gBFApp->mRenderDevice->CreateRenderState(srcRenderState);
  572. }
  573. BF_EXPORT void BF_CALLTYPE RenderState_Delete(RenderState* renderState)
  574. {
  575. delete renderState;
  576. }
  577. BF_EXPORT void BF_CALLTYPE RenderState_SetTexWrap(RenderState* renderState, bool texWrap)
  578. {
  579. renderState->SetTexWrap(texWrap);
  580. }
  581. BF_EXPORT void BF_CALLTYPE RenderState_SetWireframe(RenderState* renderState, bool wireframe)
  582. {
  583. renderState->SetWireframe(wireframe);
  584. }
  585. BF_EXPORT void BF_CALLTYPE RenderState_SetClip(RenderState* renderState, float x, float y, float width, float height)
  586. {
  587. BF_ASSERT((width >= 0) && (height >= 0));
  588. renderState->mClipRect.mX = x;
  589. renderState->mClipRect.mY = y;
  590. renderState->mClipRect.mWidth = width;
  591. renderState->mClipRect.mHeight = height;
  592. if (!renderState->mClipped)
  593. renderState->SetClipped(true);
  594. }
  595. BF_EXPORT void BF_CALLTYPE RenderState_SetShader(RenderState* renderState, Shader* shader)
  596. {
  597. renderState->SetShader(shader);
  598. }
  599. BF_EXPORT void BF_CALLTYPE RenderState_DisableClip(RenderState* renderState)
  600. {
  601. renderState->SetClipped(false);
  602. }
  603. BF_EXPORT void BF_CALLTYPE Gfx_SetTexture_TextureSegment(int textureIdx, TextureSegment* textureSegment)
  604. {
  605. DrawLayer* drawLayer = gBFApp->mRenderDevice->mCurDrawLayer;
  606. drawLayer->SetTexture(textureIdx, textureSegment->mTexture);
  607. }
  608. BF_EXPORT void BF_CALLTYPE RenderState_SetDepthFunc(RenderState* renderState, int depthFunc)
  609. {
  610. renderState->SetDepthFunc((DepthFunc)depthFunc);
  611. }
  612. BF_EXPORT void BF_CALLTYPE RenderState_SetDepthWrite(RenderState* renderState, int depthWrite)
  613. {
  614. renderState->SetWriteDepthBuffer(depthWrite != 0);
  615. }
  616. BF_EXPORT void BF_CALLTYPE RenderState_SetTopology(RenderState* renderState, int topology)
  617. {
  618. renderState->SetTopology((Topology3D)topology);
  619. }
  620. BF_EXPORT Shader* BF_CALLTYPE Gfx_LoadShader(const char* fileName, VertexDefinition* vertexDefinition)
  621. {
  622. return gBFApp->mRenderDevice->LoadShader(fileName, vertexDefinition);
  623. }
  624. BF_EXPORT void BF_CALLTYPE Gfx_SetRenderState(RenderState* renderState)
  625. {
  626. BF_ASSERT(renderState->mShader != NULL);
  627. gBFApp->mRenderDevice->SetRenderState(renderState);
  628. }
  629. BF_EXPORT void BF_CALLTYPE Gfx_Shader_Delete(Shader* shader)
  630. {
  631. delete shader;
  632. }
  633. BF_EXPORT ShaderParam* BF_CALLTYPE Gfx_GetShaderParam(Shader* shader, const char* shaderName)
  634. {
  635. return shader->GetShaderParam(shaderName);
  636. }
  637. BF_EXPORT void BF_CALLTYPE BFInput_Destroy(BFInputDevice* inputDevice)
  638. {
  639. delete inputDevice;
  640. }
  641. BF_EXPORT const char* BF_CALLTYPE BFInput_GetState(BFInputDevice* inputDevice)
  642. {
  643. String& outString = *gBeefySys_TLStrReturn.Get();
  644. outString = inputDevice->GetState();
  645. return outString.c_str();
  646. }
  647. BF_EXPORT int BF_CALLTYPE BF_TickCount()
  648. {
  649. return (int) BFTickCount();
  650. }
  651. BF_EXPORT int64 BF_CALLTYPE BF_TickCountMicroFast()
  652. {
  653. return (int) BFGetTickCountMicroFast();
  654. }
  655. BF_EXPORT void BF_CALLTYPE BF_Test()
  656. {
  657. BF_ASSERT(false);
  658. int iArr[] = {2, 3, 4};
  659. for (int i = 0; i < 10; i++)
  660. OutputDebugStrF("Hey %d\n", i);
  661. OutputDebugStrF("Break\n");
  662. for (int i : iArr)
  663. OutputDebugStrF("Hey %d\n", i);
  664. }