Browse Source

refactoring

vlod 3 năm trước cách đây
mục cha
commit
9176a78221

+ 13 - 1
Pika/core/pikaRuntime/README.md

@@ -2,4 +2,16 @@
 The runtime will launch the editor or the game and will provide it all the necessary things (like memory management).
 The runtime will launch the editor or the game and will provide it all the necessary things (like memory management).
 
 
 The runtime is versatile and can be launched with different parameters to provide both for the editor or the game.
 The runtime is versatile and can be launched with different parameters to provide both for the editor or the game.
-The shipping version of the game will lik the runtime with the game logic and disable editor specific things at compile time.
+The shipping version of the game will lik the runtime with the game logic and disable editor specific things at compile time.
+
+
+
+--- The process of loading a container
+
+- The runtime will request the dll all the container names
+- The runtime will allocate memory for the desired container and ask the dll for memory initialization
+- The runtime will call the create function
+- The runtime will call the update function
+- The runtime will call the deinitializate function
+- The runtime will ask the dll to call the destructors for the memory.
+- Checks will be done on both realms even if redundant.

+ 2 - 1
Pika/core/pikaRuntime/dllLoader/containerInformation.h

@@ -1,4 +1,5 @@
 #pragma once
 #pragma once
+#include <string>
 
 
 struct ContainerInformation
 struct ContainerInformation
 {
 {
@@ -8,6 +9,6 @@ struct ContainerInformation
 	const char *containerName):containerStructBaseSize(containerStructBaseSize), containerName(containerName) {};
 	const char *containerName):containerStructBaseSize(containerStructBaseSize), containerName(containerName) {};
 
 
 	size_t containerStructBaseSize = 0;
 	size_t containerStructBaseSize = 0;
-	std::string containerName = 0;
+	std::string containerName = "";
 
 
 };
 };

+ 28 - 28
Pika/core/pikaRuntime/dllLoader/dllLoader.cpp

@@ -11,31 +11,28 @@
 	
 	
 	
 	
 	static HMODULE dllHand;
 	static HMODULE dllHand;
-	
-	
+
 	//todo error reporting with strings
 	//todo error reporting with strings
-	bool pika::loadDll(std::filesystem::path path, 
-		gameplayStart_t **gameplayStart, gameplayUpdate_t **gameplayUpdate, getContainersInfo_t **getContainersInfo
-		,constructContainer_t **constructContainer, destructContainer_t **destructContainer)
+	bool pika::DllLoader::loadDll(std::filesystem::path path)
 	{
 	{
 		path /= "pikaGameplay.dll";
 		path /= "pikaGameplay.dll";
-	
+
 		dllHand = LoadLibraryA(path.string().c_str());
 		dllHand = LoadLibraryA(path.string().c_str());
-	
+
 		if (!dllHand) { return false; }
 		if (!dllHand) { return false; }
-	
-		*gameplayStart = (gameplayStart_t *)GetProcAddress(dllHand, "gameplayStart");
-		*gameplayUpdate = (gameplayUpdate_t *)GetProcAddress(dllHand, "gameplayUpdate");
-		*getContainersInfo = (getContainersInfo_t *)GetProcAddress(dllHand, "getContainersInfo");
-		*constructContainer = (constructContainer_t *)GetProcAddress(dllHand, "constructContainer");
-		*destructContainer = (destructContainer_t *)GetProcAddress(dllHand, "destructContainer");
-	
-		if (!gameplayStart) { return false; }
-		if (!gameplayUpdate) { return false; }
-		if (!getContainersInfo) { return false; }
-		if (!constructContainer) { return false; }
-		if (!destructContainer) { return false; }
-	
+
+		gameplayStart_ = (gameplayStart_t *)GetProcAddress(dllHand, "gameplayStart");
+		gameplayUpdate_ = (gameplayUpdate_t *)GetProcAddress(dllHand, "gameplayUpdate");
+		getContainersInfo_ = (getContainersInfo_t *)GetProcAddress(dllHand, "getContainersInfo");
+		constructContainer_ = (constructContainer_t *)GetProcAddress(dllHand, "constructContainer");
+		destructContainer_ = (destructContainer_t *)GetProcAddress(dllHand, "destructContainer");
+
+		if (!gameplayStart_) { return false; }
+		if (!gameplayUpdate_) { return false; }
+		if (!getContainersInfo_) { return false; }
+		if (!constructContainer_) { return false; }
+		if (!destructContainer_) { return false; }
+
 		return	true;
 		return	true;
 	}
 	}
 	
 	
@@ -49,20 +46,23 @@
 
 
 	#include <dll/dllMain.h>
 	#include <dll/dllMain.h>
 
 
-	bool pika::loadDll(std::filesystem::path path,
-		gameplayStart_t **gameplayStart_, gameplayUpdate_t **gameplayUpdate_, getContainersInfo_t **getContainersInfo_
-		,constructContainer_t **constructContainer_, destructContainer_t **destructContainer_)
+	bool pika::DllLoader::loadDll(std::filesystem::path path)
 	{
 	{
 		
 		
-		*gameplayStart_ = gameplayStart;
-		*gameplayUpdate_ = gameplayUpdate;
-		*getContainersInfo_ = getContainersInfo;
-		*constructContainer_ = constructContainer;
-		*destructContainer_ = destructContainer;
+		gameplayStart_ = gameplayStart;
+		gameplayUpdate_ = gameplayUpdate;
+		getContainersInfo_ = getContainersInfo;
+		constructContainer_ = constructContainer;
+		destructContainer_ = destructContainer;
 
 
 		return	true;
 		return	true;
 	}
 	}
 
 
+	
 
 
 #endif
 #endif
 
 
+	void pika::DllLoader::constructRuntimeContainer(RuntimeContainer &c, const char *name)
+	{
+		constructContainer_(&c.pointer, &c.arena, name);
+	}

+ 15 - 4
Pika/core/pikaRuntime/dllLoader/dllLoader.h

@@ -7,6 +7,7 @@
 #include <vector>
 #include <vector>
 #include <baseContainer.h>
 #include <baseContainer.h>
 #include <pikaAllocator/memoryArena.h>
 #include <pikaAllocator/memoryArena.h>
+#include <runtimeContainer/runtimeContainer.h>
 
 
 #define GAMEPLAYSTART(x) void x(pika::PikaContext pikaContext)
 #define GAMEPLAYSTART(x) void x(pika::PikaContext pikaContext)
 typedef GAMEPLAYSTART(gameplayStart_t);
 typedef GAMEPLAYSTART(gameplayStart_t);
@@ -34,9 +35,19 @@ typedef DESTRUCTCONTAINER(destructContainer_t);
 namespace pika
 namespace pika
 {
 {
 
 
-bool loadDll(std::filesystem::path path, 
-	gameplayStart_t** testPrint, gameplayUpdate_t** testUpdate, 
-	getContainersInfo_t** getContainersInfo, constructContainer_t** constructContainer,
-	destructContainer_t** destructContainer);
+//this will have only one instance open at a time for now
+struct DllLoader
+{
+	gameplayStart_t *gameplayStart_ = {};
+	gameplayUpdate_t *gameplayUpdate_ = {};
+	getContainersInfo_t *getContainersInfo_ = {};
+	constructContainer_t *constructContainer_ = {};
+	destructContainer_t *destructContainer_ = {};
+
+	bool loadDll(std::filesystem::path path);
+	void constructRuntimeContainer(RuntimeContainer &c, const char *name);
+
+};
+
 
 
 };
 };

+ 31 - 39
Pika/core/pikaRuntime/pikaMain.cpp

@@ -3,7 +3,7 @@
 #include <filesystem>
 #include <filesystem>
 
 
 #include <glad/glad.h>
 #include <glad/glad.h>
-#include <GLFW/glfw3.h>
+#include <windowSystemm/window.h>
 
 
 
 
 #include "assert/assert.h"
 #include "assert/assert.h"
@@ -11,71 +11,63 @@
 #include "pikaImgui/pikaImgui.h"
 #include "pikaImgui/pikaImgui.h"
 
 
 #include <pikaAllocator/memoryArena.h>
 #include <pikaAllocator/memoryArena.h>
+#include <runtimeContainer/runtimeContainer.h>
 
 
 int main()
 int main()
 {
 {
+
+#pragma region load dll
 	std::filesystem::path currentPath = std::filesystem::current_path();
 	std::filesystem::path currentPath = std::filesystem::current_path();
+	pika::DllLoader dllLoader;
+	PIKA_PERMA_ASSERT(dllLoader.loadDll(currentPath), "Couldn't load dll");
+#pragma endregion
 	
 	
-	
-	gameplayStart_t *gameplayStart = {};
-	gameplayUpdate_t *gameplayUpdate = {};
-	getContainersInfo_t *getContainersInfo = {};
-	constructContainer_t *constructContainer = {};
-	destructContainer_t *destrContainer = {};
-
-	PIKA_PERMA_ASSERT(pika::loadDll(currentPath, &gameplayStart, &gameplayUpdate, &getContainersInfo,
-		&constructContainer, &destrContainer), "Couldn't load dll");
-	
+#pragma region init window opengl imgui and context
 	PIKA_PERMA_ASSERT(glfwInit(), "Problem initializing glfw");
 	PIKA_PERMA_ASSERT(glfwInit(), "Problem initializing glfw");
-
 	//glfwSetErrorCallback(error_callback); todo
 	//glfwSetErrorCallback(error_callback); todo
-
-	pika::PikaContext context = {};
-
-	context.wind = glfwCreateWindow(640, 480, "Pika", NULL, NULL);
-	if (!context.wind)
-	{
-		std::cout << "problem initializing window";
-	}
-
-	glfwMakeContextCurrent(context.wind);
+	pika::PikaWindow window = {};
+	window.create();
 
 
 	PIKA_PERMA_ASSERT(gladLoadGL(), "Problem initializing glad");
 	PIKA_PERMA_ASSERT(gladLoadGL(), "Problem initializing glad");
 
 
-	context.ImGuiContext = pika::initImgui(context);
+	pika::initImgui(window.context);
+
+	window.context.glfwMakeContextCurrentPtr = glfwMakeContextCurrent;
+#pragma endregion
 
 
-	context.glfwMakeContextCurrentPtr = glfwMakeContextCurrent;
+#pragma region init dll reaml
+	dllLoader.gameplayStart_(window.context);
 
 
 	std::vector<ContainerInformation> loadedContainers;
 	std::vector<ContainerInformation> loadedContainers;
 	//todo validate stuff
 	//todo validate stuff
-	getContainersInfo(loadedContainers);
-	pika::memory::MemoryArena arena = {};
+	dllLoader.getContainersInfo_(loadedContainers);
+#pragma endregion
+
+
 
 
-	arena.containerStructMemory.size = loadedContainers[0].containerStructBaseSize;
-	arena.containerStructMemory.block = malloc(loadedContainers[0].containerStructBaseSize);
 
 
-	gameplayStart(context);
 
 
-	Container *gameCode = 0;
-	constructContainer(&gameCode, &arena, "Gameplay");
-	gameCode->create();
 
 
-	while (!glfwWindowShouldClose(context.wind))
+	RuntimeContainer container;
+	container.arena.allocateStaticMemory(loadedContainers[0]);
+
+	dllLoader.constructRuntimeContainer(container, "Gameplay");
+	container.pointer->create();
+
+	while (!window.shouldClose())
 	{
 	{
 
 
 		glClear(GL_COLOR_BUFFER_BIT);
 		glClear(GL_COLOR_BUFFER_BIT);
 
 
-		pika::imguiStartFrame(context);
+		pika::imguiStartFrame(window.context);
 
 
 		//gameplayUpdate(context);
 		//gameplayUpdate(context);
-		gameCode->update();
-
-		pika::imguiEndFrame(context);
+		container.pointer->update();
 
 
+		pika::imguiEndFrame(window.context);
 
 
 
 
-		glfwPollEvents();
-		glfwSwapBuffers(context.wind);
+		window.update();
 	}
 	}
 
 
 
 

+ 5 - 3
Pika/core/pikaRuntime/runtimeContainer/runtimeContainer.h

@@ -1,12 +1,14 @@
 #pragma once
 #pragma once
 #include <pikaConfig.h>
 #include <pikaConfig.h>
 #include <string>
 #include <string>
+#include <baseContainer.h>
+#include <pikaAllocator/memoryArena.h>
 
 
 struct RuntimeContainer
 struct RuntimeContainer
 {
 {
 	std::string baseContainerName = {};
 	std::string baseContainerName = {};
-	std::string name = {};
-	
-
+	//std::string name = {};
+	Container *pointer = {};
 
 
+	pika::memory::MemoryArena arena = {};
 };
 };

+ 21 - 0
Pika/core/pikaRuntime/windowSystemm/window.cpp

@@ -0,0 +1,21 @@
+#include "window.h"
+#include <assert/assert.h>
+
+void pika::PikaWindow::create()
+{
+	context.wind = glfwCreateWindow(640, 480, "Pika", NULL, NULL);
+	
+	PIKA_PERMA_ASSERT(context.wind, "problem initializing window");
+	glfwMakeContextCurrent(context.wind);
+}
+
+bool pika::PikaWindow::shouldClose()
+{
+	return glfwWindowShouldClose(context.wind);
+}
+
+void pika::PikaWindow::update()
+{
+	glfwPollEvents();
+	glfwSwapBuffers(context.wind);
+}

+ 25 - 0
Pika/core/pikaRuntime/windowSystemm/window.h

@@ -0,0 +1,25 @@
+#pragma once
+#include <GLFW/glfw3.h>
+#include <pikaContext.h>
+
+
+namespace pika
+{
+
+	//this is not intended to have multiple instances in the program
+	struct PikaWindow
+	{
+		pika::PikaContext context = {};
+
+		//this doesn't return error codes because it will do the asserts for you
+		void create();
+
+		bool shouldClose();
+
+		void update();
+
+	};
+
+
+
+}

+ 2 - 1
Pika/core/pikaSTD/assert/assert.cpp

@@ -12,9 +12,10 @@ namespace pika
 	namespace assert
 	namespace assert
 	{
 	{
 
 
-		void terminate(...)
+		inline void terminate(...)
 		{
 		{
 			std::exit(1);
 			std::exit(1);
+			__assume(0); //optimize code after the exit
 		}
 		}
 
 
 
 

+ 1 - 1
Pika/core/pikaSTD/assert/assert.h

@@ -9,7 +9,7 @@ namespace pika
 	{
 	{
 
 
 		//arguments don't do anything here
 		//arguments don't do anything here
-		void terminate(...);
+		inline void terminate(...);
 
 
 		void assertFunctionDevelopment(
 		void assertFunctionDevelopment(
 			const char *expression, 
 			const char *expression, 

+ 1 - 1
Pika/core/pikaSTD/pikaAllocator/freeListAllocator.cpp

@@ -16,7 +16,7 @@
 namespace pika
 namespace pika
 {
 {
 
 
-namespace allocator
+namespace memory
 {
 {
 
 
 	const uint64_t GUARD_VALUE = 0xff'ff'ff'ff'ff'ff'ff'ff;
 	const uint64_t GUARD_VALUE = 0xff'ff'ff'ff'ff'ff'ff'ff;

+ 1 - 2
Pika/core/pikaSTD/pikaAllocator/freeListAllocator.h

@@ -12,11 +12,10 @@
 namespace pika
 namespace pika
 {
 {
 
 
-namespace allocator
+namespace memory
 {
 {
 
 
 
 
-
 	#ifdef PIKA_WINDOWS
 	#ifdef PIKA_WINDOWS
 	
 	
 		struct FreeListAllocatorMutex
 		struct FreeListAllocatorMutex

+ 17 - 0
Pika/core/pikaSTD/pikaAllocator/memoryArena.cpp

@@ -0,0 +1,17 @@
+#include <pikaAllocator/memoryArena.h>
+#include <malloc.h>
+
+
+void pika::memory::MemoryArena::allocateStaticMemory(const ContainerInformation &containerInfo)
+{
+	containerStructMemory.size = containerInfo.containerStructBaseSize;
+	containerStructMemory.block = malloc(containerInfo.containerStructBaseSize);
+}
+
+void pika::memory::MemoryArena::dealocateStaticMemory()
+{
+	containerStructMemory.size = 0;
+	free(containerStructMemory.block);
+	containerStructMemory.block = 0;
+}
+

+ 4 - 2
Pika/core/pikaSTD/pikaAllocator/memoryArena.h

@@ -1,5 +1,5 @@
 #pragma once
 #pragma once
-
+#include <dllLoader/containerInformation.h>
 
 
 namespace pika
 namespace pika
 {
 {
@@ -20,7 +20,9 @@ namespace memory
 		//this is used to allocate the static memory of the container (struct member data)
 		//this is used to allocate the static memory of the container (struct member data)
 		MemoryBlock containerStructMemory = {};
 		MemoryBlock containerStructMemory = {};
 
 
-
+		//todo this will request an allocator probably in the future
+		void allocateStaticMemory(const ContainerInformation &containerInfo);
+		void dealocateStaticMemory();
 	};
 	};
 
 
 
 

+ 2 - 2
Pika/core/pikaSTD/pikaImgui/pikaImgui.cpp

@@ -2,7 +2,7 @@
 #include <pikaImgui/pikaImgui.h>
 #include <pikaImgui/pikaImgui.h>
 #include <GLFW/glfw3.h>
 #include <GLFW/glfw3.h>
 
 
-ImGuiContext *pika::initImgui(PikaContext pikaContext)
+void pika::initImgui(PikaContext &pikaContext)
 {
 {
 	auto context = ImGui::CreateContext();
 	auto context = ImGui::CreateContext();
 	//ImGui::StyleColorsDark();
 	//ImGui::StyleColorsDark();
@@ -27,7 +27,7 @@ ImGuiContext *pika::initImgui(PikaContext pikaContext)
 	ImGui_ImplGlfw_InitForOpenGL(pikaContext.wind, true);
 	ImGui_ImplGlfw_InitForOpenGL(pikaContext.wind, true);
 	ImGui_ImplOpenGL3_Init("#version 330");
 	ImGui_ImplOpenGL3_Init("#version 330");
 
 
-	return context;
+	pikaContext.ImGuiContext = context;
 }
 }
 
 
 void pika::setImguiContext(PikaContext pikaContext)
 void pika::setImguiContext(PikaContext pikaContext)

+ 1 - 1
Pika/core/pikaSTD/pikaImgui/pikaImgui.h

@@ -14,7 +14,7 @@ namespace pika
 {
 {
 
 
 
 
-	ImGuiContext *initImgui(PikaContext pikaContext);
+	void initImgui(PikaContext &pikaContext);
 	void setImguiContext(PikaContext pikaContext);
 	void setImguiContext(PikaContext pikaContext);
 	void imguiStartFrame(PikaContext pikaContext);
 	void imguiStartFrame(PikaContext pikaContext);
 	void imguiEndFrame(PikaContext pikaContext);
 	void imguiEndFrame(PikaContext pikaContext);

+ 1 - 1
Pika/gameplay/dll/dllMain.cpp

@@ -36,7 +36,7 @@ PIKA_API void destructContainer(Container **c, pika::memory::MemoryArena *arena)
 }
 }
 
 
 
 
-
+//used to initialize libraries 
 PIKA_API void gameplayStart(pika::PikaContext pikaContext)
 PIKA_API void gameplayStart(pika::PikaContext pikaContext)
 {
 {
 	//todo user should have functions to specify this
 	//todo user should have functions to specify this