dllMain.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #define PIKA_DECLARE_CONTAINER(x) info.push_back( pika::ContainerInformation(sizeof(x), #x, x::containerInfo()) );
  2. #include "dllMain.h"
  3. #include <gl2d/gl2d.h>
  4. #include <imgui.h>
  5. #include "pikaImgui/pikaImgui.h"
  6. #include <logs/assert.h>
  7. #include "containers/pikaGameplay.h"
  8. #include <containers.h>
  9. #include <memoryArena/memoryArena.h>
  10. #include <globalAllocator/globalAllocator.h>
  11. //todo use a global static array that can be accessed from other cpps and the macro will create an instance of a struct
  12. //that will push that container
  13. PIKA_API void getContainersInfo(std::vector<pika::ContainerInformation> &info)
  14. {
  15. info.clear();
  16. PIKA_ALL_CONTAINERS()
  17. }
  18. #undef PIKA_DECLARE_CONTAINER
  19. //this should not allocate memory
  20. PIKA_API bool constructContainer(Container **c, pika::memory::MemoryArena *arena, const char *name)
  21. {
  22. *c = getContainer(name, arena);
  23. return *c != 0;
  24. }
  25. PIKA_API void destructContainer(Container **c, pika::memory::MemoryArena *arena)
  26. {
  27. //no need to call delete.
  28. (*c)->~Container();
  29. }
  30. PIKA_API void bindAllocator(pika::memory::FreeListAllocator *arena)
  31. {
  32. pika::memory::setGlobalAllocator(arena);
  33. }
  34. PIKA_API void resetAllocator()
  35. {
  36. pika::memory::setGlobalAllocatorToStandard();
  37. }
  38. PIKA_API void dissableAllocators()
  39. {
  40. pika::memory::dissableAllocators();
  41. }
  42. void gl2dErrorFunc(const char *msg, void* userData) //todo
  43. {
  44. pika::LogManager *logManager = (pika::LogManager *)userData;
  45. pika::memory::pushCustomAllocatorsToStandard();
  46. if (logManager)
  47. logManager->log(msg, pika::logError);
  48. pika::memory::popCustomAllocatorsToStandard();
  49. }
  50. //used to initialize libraries
  51. PIKA_API void gameplayStart(pika::PikaContext &pikaContext, pika::LogManager &logs)
  52. {
  53. pika::pikaImgui::setImguiAllocator(pikaContext.imguiAllocator);
  54. pika::initShortcutApi(); //todo
  55. //todo user should have functions to specify this
  56. #pragma region init stuff
  57. #ifdef PIKA_DEVELOPMENT
  58. PIKA_PERMA_ASSERT(gladLoadGL(), "Problem initializing glad from dll");
  59. pika::pikaImgui::setImguiContext(pikaContext);
  60. #endif
  61. gl2d::init();
  62. gl2d::setUserDefinedData(&logs);
  63. gl2d::setErrorFuncCallback(gl2dErrorFunc);
  64. #pragma endregion
  65. }
  66. //this won't be ever called in production so we can remove the code
  67. PIKA_API void gameplayReload(pika::PikaContext &pikaContext, pika::LogManager &logs)
  68. {
  69. #ifdef PIKA_DEVELOPMENT
  70. pika::pikaImgui::setImguiAllocator(pikaContext.imguiAllocator); //todo check if really needed
  71. pika::initShortcutApi();
  72. PIKA_PERMA_ASSERT(gladLoadGL(), "Problem initializing glad from dll");
  73. pika::pikaImgui::setImguiContext(pikaContext);
  74. gl2d::init();
  75. gl2d::setUserDefinedData(&logs);
  76. gl2d::setErrorFuncCallback(gl2dErrorFunc);
  77. #endif
  78. }
  79. #if PIKA_WINDOWS
  80. #ifdef PIKA_DEVELOPMENT
  81. #include <Windows.h>
  82. //https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain
  83. BOOL WINAPI DllMain(
  84. HINSTANCE hinstDLL, // handle to DLL module
  85. DWORD fdwReason, // reason for calling function
  86. LPVOID lpvReserved) // reserved
  87. {
  88. // Perform actions based on the reason for calling.
  89. switch (fdwReason)
  90. {
  91. case DLL_PROCESS_ATTACH:
  92. // Initialize once for each new process.
  93. // Return FALSE to fail DLL load.
  94. break;
  95. case DLL_THREAD_ATTACH:
  96. // Do thread-specific initialization.
  97. break;
  98. case DLL_THREAD_DETACH:
  99. // Do thread-specific cleanup.
  100. break;
  101. case DLL_PROCESS_DETACH:
  102. if (lpvReserved != nullptr)
  103. {
  104. break; // do not do cleanup if process termination scenario
  105. }
  106. //if (old)
  107. //{
  108. // std::cout.rdbuf(old);
  109. //}
  110. // Perform any necessary cleanup.
  111. break;
  112. }
  113. return TRUE; // Successful DLL_PROCESS_ATTACH.
  114. }
  115. #endif
  116. #endif