baseContainer.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #pragma once
  2. #include <windowSystemm/window.h>
  3. #include <iostream>
  4. #include <pikaOptional.h>
  5. #include <string>
  6. #include <pikaAllocator/freeListAllocator.h>
  7. #include <staticVector.h>
  8. #include <pikaGL/frameBuffer.h>
  9. #include <pikaConsoleManager/pikaConsoleWindow.h>
  10. #include <globalAllocator/globalAllocator.h>
  11. #include <fstream>
  12. #include <staticString.h>
  13. #include <vector>
  14. #include <stringManipulation/stringManipulation.h>
  15. #define READENTIREFILE(x) bool x(const char* name, void* buffer, size_t size)
  16. typedef READENTIREFILE(readEntireFile_t);
  17. #undef READENTIREFILE
  18. #define GETFILESIZE(x) bool x(const char* name, size_t &size)
  19. typedef GETFILESIZE(getFileSize_t);
  20. #undef GETFILESIZE
  21. static constexpr size_t MaxAllocatorsCount = 128;
  22. struct CreateContainerInfo
  23. {
  24. pika::StaticString<257> containerName = {};
  25. pika::StaticString<257> cmdArgs = {};
  26. };
  27. //this is passed by the engine. You should not modify the data
  28. //this is also used by the engine to give you acces to some io functions
  29. struct RequestedContainerInfo
  30. {
  31. pika::memory::FreeListAllocator *mainAllocator = {};
  32. pika::StaticVector<pika::memory::FreeListAllocator, MaxAllocatorsCount> *bonusAllocators = {};
  33. //readEntireFile_t *readEntireFilePointer = {};
  34. //getFileSize_t *getFileSizePointer = {};
  35. pika::GL::PikaFramebuffer requestedFBO = {};
  36. int requestedImguiIds = 0;
  37. int imguiTotalRequestedIds = 0;
  38. pika::ConsoleWindow *consoleWindow = nullptr;
  39. //todo add logs here
  40. //todo probably remove
  41. void setMousePositionRelevantToWindow(int x, int y)
  42. {
  43. if (internal.mainWindow)
  44. {
  45. internal.setCursorPosFunc(internal.window, x, y);
  46. }
  47. else
  48. {
  49. int mainX = 0;
  50. int mainY = 0;
  51. internal.getWindowPosFunc(internal.window, &mainX, &mainY);
  52. //internal.setCursorPosFunc(internal.window, x + internal.windowPosX - mainX, y + internal.windowPosY - mainY);
  53. internal.setCursorPosFunc(internal.window, x + internal.windowPosX - mainX + 8, y + internal.windowPosY - mainY + 28);
  54. }
  55. };
  56. void setFpsCursor()
  57. {
  58. PIKA_DEVELOPMENT_ONLY_ASSERT(internal.setInputModeFunc, "missing setInputModeFunc func");
  59. internal.setInputModeFunc(internal.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  60. }
  61. void setNormalCursor()
  62. {
  63. PIKA_DEVELOPMENT_ONLY_ASSERT(internal.setInputModeFunc, "missing setInputModeFunc func");
  64. internal.setInputModeFunc(internal.window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  65. }
  66. struct
  67. {
  68. int windowPosX = 0;
  69. int windowPosY = 0;
  70. GLFWwindow *window = 0;
  71. bool mainWindow = 0;
  72. decltype(glfwSetCursorPos) *setCursorPosFunc = nullptr;
  73. decltype(glfwGetWindowPos) *getWindowPosFunc = nullptr;
  74. decltype(glfwSetInputMode) *setInputModeFunc = nullptr;
  75. std::vector<CreateContainerInfo> *containersToCreate;
  76. }internal;
  77. //resets global allocator!
  78. void createContainer(std::string containerName, std::string cmdArgs = "")
  79. {
  80. PIKA_DEVELOPMENT_ONLY_ASSERT(internal.containersToCreate, "missing containersToCreate pointer");
  81. CreateContainerInfo info;
  82. if (containerName.size() >= info.containerName.MAX_SIZE-1) { return; }
  83. if (cmdArgs.size() >= info.cmdArgs.MAX_SIZE-1) { return; }
  84. info.containerName = {containerName.c_str()};
  85. info.cmdArgs = {cmdArgs.c_str()};
  86. pika::memory::setGlobalAllocatorToStandard();
  87. internal.containersToCreate->push_back(info);
  88. pika::memory::setGlobalAllocator(mainAllocator);
  89. }
  90. //returns true if succeded (can return false if console is disabeled)
  91. bool consoleWrite(const char* c)
  92. {
  93. //do not allocate memory here!
  94. //console window is from core realm
  95. if (!consoleWindow) { return false; }
  96. consoleWindow->write(c);
  97. return true;
  98. }
  99. bool writeEntireFile(const char *name, const std::string &content)
  100. {
  101. std::ofstream f(name);
  102. if (!f.is_open()) { return 0; }
  103. f.write(content.c_str(), content.size());
  104. f.close();
  105. return 1;
  106. }
  107. bool writeEntireFileBinary(const char *name, void *data, size_t s)
  108. {
  109. std::ofstream f(name, std::ios::binary | std::ios::out);
  110. if (!f.is_open()) { return 0; }
  111. f.write((char*)data, s);
  112. f.close();
  113. return 1;
  114. }
  115. bool readEntireFileBinary(const char *name, std::vector<char> &data)
  116. {
  117. size_t s = 0;
  118. data.clear();
  119. if (!getFileSizeBinary(name, s)) { return 0; }
  120. data.reserve(s);
  121. return readEntireFileBinary(name, data.data(), s);
  122. }
  123. bool readEntireFile(const char *name, std::string &data)
  124. {
  125. size_t s = 0;
  126. data.clear();
  127. if (!getFileSize(name, s)) { return 0; }
  128. data.resize(s);
  129. return readEntireFile(name, data.data(), s);
  130. }
  131. bool readEntireFileBinary(const char *name, void *buffer, size_t size)
  132. {
  133. //PIKA_DEVELOPMENT_ONLY_ASSERT(readEntireFilePointer, "read entire file pointer not assigned");
  134. bool success = true;
  135. pika::memory::setGlobalAllocatorToStandard();
  136. {
  137. std::ifstream f(name, std::ios::binary);
  138. if (!f.is_open())
  139. {
  140. success = false;
  141. }
  142. else
  143. {
  144. f.read((char*)buffer, size);
  145. f.close();
  146. }
  147. }
  148. pika::memory::setGlobalAllocator(mainAllocator);
  149. return success;
  150. }
  151. bool readEntireFile(const char *name, void *buffer, size_t size)
  152. {
  153. //PIKA_DEVELOPMENT_ONLY_ASSERT(readEntireFilePointer, "read entire file pointer not assigned");
  154. bool success = true;
  155. pika::memory::setGlobalAllocatorToStandard();
  156. {
  157. std::ifstream f(name);
  158. if (!f.is_open())
  159. {
  160. success = false;
  161. }
  162. else
  163. {
  164. f.read((char *)buffer, size);
  165. f.close();
  166. }
  167. }
  168. pika::memory::setGlobalAllocator(mainAllocator);
  169. return success;
  170. }
  171. bool getFileSizeBinary(const char *name, size_t &size)
  172. {
  173. //PIKA_DEVELOPMENT_ONLY_ASSERT(getFileSizePointer, "get file size pointer not assigned");
  174. bool success = true;
  175. size = 0;
  176. //todo push pop allocator or use that pointer thing (and don't forget to only use explicit allocators calls or sthing)
  177. pika::memory::setGlobalAllocatorToStandard();
  178. {
  179. std::ifstream f(name, std::ifstream::ate | std::ifstream::binary);
  180. if (!f.is_open())
  181. {
  182. success = false;
  183. }
  184. else
  185. {
  186. size = f.tellg();
  187. f.close();
  188. }
  189. }
  190. pika::memory::setGlobalAllocator(mainAllocator);
  191. return size;
  192. }
  193. bool getFileSize(const char *name, size_t &size)
  194. {
  195. bool success = true;
  196. size = 0;
  197. pika::memory::setGlobalAllocatorToStandard();
  198. {
  199. std::ifstream f(name, std::ifstream::ate);
  200. if (!f.is_open())
  201. {
  202. success = false;
  203. }
  204. else
  205. {
  206. size = f.tellg();
  207. f.close();
  208. }
  209. }
  210. pika::memory::setGlobalAllocator(mainAllocator);
  211. return success;
  212. }
  213. };
  214. struct ContainerStaticInfo
  215. {
  216. //this is the main heap allocator memory size
  217. size_t defaultHeapMemorySize = 0;
  218. //this will use the global allocator. you won't be able to use input recording or snapshots, and the
  219. //memory leak protection won't be possible.
  220. bool useDefaultAllocator = 0;
  221. pika::StaticVector<size_t, MaxAllocatorsCount> bonusAllocators = {};
  222. //add file extensions here so that the engine knows that your container can open them.
  223. pika::StaticVector<pika::StaticString<16>, 16> extensionsSuported = {};
  224. //the engine will create a new window for your container and give you the fbo to bind to
  225. //in release that fbo will just be the default framebuffer
  226. bool requestImguiFbo = 0;
  227. unsigned int requestImguiIds = 0;
  228. bool andInputWithWindowHasFocus = 1;
  229. bool andInputWithWindowHasFocusLastFrame = 1;
  230. bool _internalNotImplemented = 0;
  231. bool operator==(const ContainerStaticInfo &other)
  232. {
  233. if (this == &other) { return true; }
  234. return
  235. this->defaultHeapMemorySize == other.defaultHeapMemorySize &&
  236. this->bonusAllocators == other.bonusAllocators &&
  237. this->_internalNotImplemented == other._internalNotImplemented &&
  238. this->requestImguiFbo == other.requestImguiFbo &&
  239. this->requestImguiIds == other.requestImguiIds &&
  240. this->useDefaultAllocator == other.useDefaultAllocator &&
  241. this->andInputWithWindowHasFocus == other.andInputWithWindowHasFocus &&
  242. this->andInputWithWindowHasFocusLastFrame == other.andInputWithWindowHasFocusLastFrame;
  243. ;
  244. }
  245. bool operator!=(const ContainerStaticInfo &other)
  246. {
  247. return !(*this == other);
  248. }
  249. };
  250. struct Container
  251. {
  252. //this is used to give to the engine basic information about your container.
  253. //this function should be pure
  254. //this function should not allocate memory
  255. //this should not be dependent on anything that is called on create or library initialization
  256. static ContainerStaticInfo containerInfo() { ContainerStaticInfo c; c._internalNotImplemented = true; return c; };
  257. virtual bool create(RequestedContainerInfo &requestedInfo, pika::StaticString<256> commandLineArgument) = 0;
  258. virtual bool update(
  259. pika::Input input,
  260. pika::WindowState windowState,
  261. RequestedContainerInfo &requestedInfo) = 0;
  262. virtual void destruct() {};
  263. virtual ~Container() {};
  264. };