BeefySysLib.cpp 24 KB

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