Browse Source

figured out how to make dynamic container linkage api

vlod 3 years ago
parent
commit
cf77b429c0

+ 7 - 0
Pika/core/pikaRuntime/dllLoader/README.mg

@@ -0,0 +1,7 @@
+# dllLoader
+
+-The loading process
+
+The runtime will ask the dll to hand him the container information.
+That information will be used by the runtime to corectly set each container memory and flags when loading.
+The container itself will be loaded inside the dll realm so the runtime has to corectly know its informations.

+ 13 - 0
Pika/core/pikaRuntime/dllLoader/containerInformation.h

@@ -0,0 +1,13 @@
+#pragma once
+
+struct ContainerInformation
+{
+	ContainerInformation() {};
+	ContainerInformation(
+	size_t containerStructBaseSize,
+	const char *containerName):containerStructBaseSize(containerStructBaseSize), containerName(containerName) {};
+
+	size_t containerStructBaseSize = 0;
+	std::string containerName = 0;
+
+};

+ 21 - 10
Pika/core/pikaRuntime/dllLoader/dllLoader.cpp

@@ -15,7 +15,8 @@
 	
 	
 	//todo error reporting with strings
 	//todo error reporting with strings
 	bool pika::loadDll(std::filesystem::path path, 
 	bool pika::loadDll(std::filesystem::path path, 
-		testStart_t **testStart, testUpdate_t **testUpdate)
+		gameplayStart_t **gameplayStart, gameplayUpdate_t **gameplayUpdate, getContainersInfo_t **getContainersInfo
+		,constructContainer_t **constructContainer, destructContainer_t **destructContainer)
 	{
 	{
 		path /= "pikaGameplay.dll";
 		path /= "pikaGameplay.dll";
 	
 	
@@ -23,11 +24,17 @@
 	
 	
 		if (!dllHand) { return false; }
 		if (!dllHand) { return false; }
 	
 	
-		*testStart = (testStart_t *)GetProcAddress(dllHand, "testStart");
-		*testUpdate = (testUpdate_t *)GetProcAddress(dllHand, "testUpdate");
+		*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 (!testStart) { return false; }
-		if (!testUpdate) { return false; }
+		if (!gameplayStart) { return false; }
+		if (!gameplayUpdate) { return false; }
+		if (!getContainersInfo) { return false; }
+		if (!constructContainer) { return false; }
+		if (!destructContainer) { return false; }
 	
 	
 		return	true;
 		return	true;
 	}
 	}
@@ -40,15 +47,19 @@
 
 
 #elif defined(PIKA_PRODUCTION)
 #elif defined(PIKA_PRODUCTION)
 
 
-	#include <dllMain.h>
+	#include <dll/dllMain.h>
 
 
 	bool pika::loadDll(std::filesystem::path path,
 	bool pika::loadDll(std::filesystem::path path,
-		testStart_t **testStart_, testUpdate_t **testUpdate_)
+		gameplayStart_t **gameplayStart_, gameplayUpdate_t **gameplayUpdate_, getContainersInfo_t **getContainersInfo_
+		,constructContainer_t **constructContainer_, destructContainer_t **destructContainer_)
 	{
 	{
 		
 		
-		*testStart_ = testStart;
-		*testUpdate_ = testUpdate;
-	
+		*gameplayStart_ = gameplayStart;
+		*gameplayUpdate_ = gameplayUpdate;
+		*getContainersInfo_ = getContainersInfo;
+		*constructContainer_ = constructContainer;
+		*destructContainer_ = destructContainer;
+
 		return	true;
 		return	true;
 	}
 	}
 
 

+ 27 - 9
Pika/core/pikaRuntime/dllLoader/dllLoader.h

@@ -1,17 +1,33 @@
 #pragma once
 #pragma once
 #include <filesystem>
 #include <filesystem>
+#include <glad/glad.h> //used to not conflict with glfw
 #include <GLFW/glfw3.h>
 #include <GLFW/glfw3.h>
-#include <imgui.h>
 #include <pikaImgui/pikaImgui.h>
 #include <pikaImgui/pikaImgui.h>
+#include <dllLoader/containerInformation.h>
+#include <vector>
+#include <baseContainer.h>
+#include <pikaAllocator/memoryArena.h>
 
 
-#define TESTSTART(x) void x(pika::PikaContext pikaContext)
-typedef TESTSTART(testStart_t);
-//extern "C" __declspec(dllexport) TESTPRINT(gameLogic);
-#undef TESTSTART
+#define GAMEPLAYSTART(x) void x(pika::PikaContext pikaContext)
+typedef GAMEPLAYSTART(gameplayStart_t);
+#undef GAMEPLAYSTART
 
 
-#define TESTUPDATE(x) void x(pika::PikaContext pikaContext)
-typedef TESTUPDATE(testUpdate_t);
-#undef TESTUPDATE
+#define GAMEPLAYUPDATE(x) void x(pika::PikaContext pikaContext)
+typedef GAMEPLAYUPDATE(gameplayUpdate_t);
+#undef GAMEPLAYUPDATE
+
+#define GETCONTAINERSINFO(x) void x(std::vector<ContainerInformation> &info)
+typedef GETCONTAINERSINFO(getContainersInfo_t);
+#undef GETCONTAINERSINFO
+
+#define CONSTRUCTCONTAINER(x) void x(Container **c, pika::memory::MemoryArena *arena, const char *name);
+typedef CONSTRUCTCONTAINER(constructContainer_t);
+#undef CONSTRUCTCONTAINER
+
+
+#define DESTRUCTCONTAINER(x) void x(Container **c, pika::memory::MemoryArena *arena);
+typedef DESTRUCTCONTAINER(destructContainer_t);
+#undef DESTRUCTCONTAINER
 
 
 
 
 
 
@@ -19,6 +35,8 @@ namespace pika
 {
 {
 
 
 bool loadDll(std::filesystem::path path, 
 bool loadDll(std::filesystem::path path, 
-	testStart_t** testPrint, testUpdate_t** testUpdate);
+	gameplayStart_t** testPrint, gameplayUpdate_t** testUpdate, 
+	getContainersInfo_t** getContainersInfo, constructContainer_t** constructContainer,
+	destructContainer_t** destructContainer);
 
 
 };
 };

+ 25 - 6
Pika/core/pikaRuntime/pikaMain.cpp

@@ -10,15 +10,21 @@
 #include "dllLoader/dllLoader.h"
 #include "dllLoader/dllLoader.h"
 #include "pikaImgui/pikaImgui.h"
 #include "pikaImgui/pikaImgui.h"
 
 
+#include <pikaAllocator/memoryArena.h>
+
 int main()
 int main()
 {
 {
 	std::filesystem::path currentPath = std::filesystem::current_path();
 	std::filesystem::path currentPath = std::filesystem::current_path();
 	
 	
 	
 	
-	testStart_t *testStart = {};
-	testUpdate_t *testUpdate = {};
-
-	PIKA_PERMA_ASSERT(pika::loadDll(currentPath, &testStart, &testUpdate), "Couldn't load dll");
+	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");
 	
 	
 	PIKA_PERMA_ASSERT(glfwInit(), "Problem initializing glfw");
 	PIKA_PERMA_ASSERT(glfwInit(), "Problem initializing glfw");
 
 
@@ -40,7 +46,19 @@ int main()
 
 
 	context.glfwMakeContextCurrentPtr = glfwMakeContextCurrent;
 	context.glfwMakeContextCurrentPtr = glfwMakeContextCurrent;
 
 
-	testStart(context);
+	std::vector<ContainerInformation> loadedContainers;
+	//todo validate stuff
+	getContainersInfo(loadedContainers);
+	pika::memory::MemoryArena arena = {};
+
+	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))
 	while (!glfwWindowShouldClose(context.wind))
 	{
 	{
@@ -49,7 +67,8 @@ int main()
 
 
 		pika::imguiStartFrame(context);
 		pika::imguiStartFrame(context);
 
 
-		testUpdate(context);
+		//gameplayUpdate(context);
+		gameCode->update();
 
 
 		pika::imguiEndFrame(context);
 		pika::imguiEndFrame(context);
 
 

+ 7 - 0
Pika/core/pikaRuntime/runtimeContainer/README.md

@@ -0,0 +1,7 @@
+# runtimeContainer
+
+The runtime can potentially have multiple containers oppened but will defenetly have one running as the game.
+The game container could be changed. It is the responsability of the runtime to manage the state of the containers.
+The runtime is responsible to check if a container name is valid.
+The dll will just crash if it wasn't able to load the container.
+

+ 3 - 0
Pika/core/pikaRuntime/runtimeContainer/runtimeContainer.cpp

@@ -0,0 +1,3 @@
+#include <runtimeContainer/runtimeContainer.h>
+
+

+ 12 - 0
Pika/core/pikaRuntime/runtimeContainer/runtimeContainer.h

@@ -0,0 +1,12 @@
+#pragma once
+#include <pikaConfig.h>
+#include <string>
+
+struct RuntimeContainer
+{
+	std::string baseContainerName = {};
+	std::string name = {};
+	
+
+
+};

+ 12 - 0
Pika/core/pikaSTD/baseContainer.h

@@ -0,0 +1,12 @@
+#pragma once
+
+
+
+struct Container
+{
+
+	virtual void create() = 0;
+
+	virtual void update() = 0;
+
+};

+ 31 - 0
Pika/core/pikaSTD/pikaAllocator/memoryArena.h

@@ -0,0 +1,31 @@
+#pragma once
+
+
+namespace pika
+{
+namespace memory
+{
+
+
+	struct MemoryBlock
+	{
+		size_t size = 0;
+		void *block = 0;
+	};
+
+
+	struct MemoryArena
+	{
+
+		//this is used to allocate the static memory of the container (struct member data)
+		MemoryBlock containerStructMemory = {};
+
+
+	};
+
+
+
+
+};
+};
+

+ 4 - 0
Pika/core/pikaSTD/pikaImgui/README.md

@@ -0,0 +1,4 @@
+#pikaImgui
+
+Imgui is one of the few libraries that is part of the core pika engine. However it can be removed in a production build using
+a macro.

+ 4 - 3
Pika/core/pikaSTD/pikaImgui/pikaImgui.cpp

@@ -1,5 +1,6 @@
 #include <glad/glad.h>
 #include <glad/glad.h>
 #include <pikaImgui/pikaImgui.h>
 #include <pikaImgui/pikaImgui.h>
+#include <GLFW/glfw3.h>
 
 
 ImGuiContext *pika::initImgui(PikaContext pikaContext)
 ImGuiContext *pika::initImgui(PikaContext pikaContext)
 {
 {
@@ -29,14 +30,14 @@ ImGuiContext *pika::initImgui(PikaContext pikaContext)
 	return context;
 	return context;
 }
 }
 
 
-void pika::setContext(PikaContext pikaContext)
+void pika::setImguiContext(PikaContext pikaContext)
 {
 {
 	ImGui::SetCurrentContext(pikaContext.ImGuiContext);
 	ImGui::SetCurrentContext(pikaContext.ImGuiContext);
 }
 }
 
 
 void pika::imguiStartFrame(PikaContext pikaContext)
 void pika::imguiStartFrame(PikaContext pikaContext)
 {
 {
-	setContext(pikaContext);
+	setImguiContext(pikaContext);
 	ImGui_ImplOpenGL3_NewFrame();
 	ImGui_ImplOpenGL3_NewFrame();
 	ImGui_ImplGlfw_NewFrame();
 	ImGui_ImplGlfw_NewFrame();
 	ImGui::NewFrame();
 	ImGui::NewFrame();
@@ -46,7 +47,7 @@ void pika::imguiStartFrame(PikaContext pikaContext)
 
 
 void pika::imguiEndFrame(PikaContext pikaContext)
 void pika::imguiEndFrame(PikaContext pikaContext)
 {
 {
-	setContext(pikaContext);
+	setImguiContext(pikaContext);
 	ImGui::Render();
 	ImGui::Render();
 	int display_w = 0, display_h = 0;
 	int display_w = 0, display_h = 0;
 	glfwGetFramebufferSize(pikaContext.wind, &display_w, &display_h);
 	glfwGetFramebufferSize(pikaContext.wind, &display_w, &display_h);

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

@@ -7,7 +7,6 @@
 #include "backends/imgui_impl_opengl3.h"
 #include "backends/imgui_impl_opengl3.h"
 #include "imguiThemes.h"
 #include "imguiThemes.h"
 
 
-#include <GLFW/glfw3.h>
 
 
 #include <pikaContext.h>
 #include <pikaContext.h>
 
 
@@ -16,7 +15,7 @@ namespace pika
 
 
 
 
 	ImGuiContext *initImgui(PikaContext pikaContext);
 	ImGuiContext *initImgui(PikaContext pikaContext);
-	void setContext(PikaContext pikaContext);
+	void setImguiContext(PikaContext pikaContext);
 	void imguiStartFrame(PikaContext pikaContext);
 	void imguiStartFrame(PikaContext pikaContext);
 	void imguiEndFrame(PikaContext pikaContext);
 	void imguiEndFrame(PikaContext pikaContext);
 
 

+ 20 - 0
Pika/gameplay/containers.cpp

@@ -0,0 +1,20 @@
+#include <containers.h>
+#include <assert/assert.h>
+
+
+Container *getContainer(const char *name, pika::memory::MemoryArena *memoryArena)
+{
+
+	if (std::strcmp(name, "Gameplay") == 0)
+	{
+		PIKA_PERMA_ASSERT(sizeof(Gameplay) == memoryArena->containerStructMemory.size, "invalid memory size for static data");
+
+		return new(memoryArena->containerStructMemory.block)  Gameplay();
+	}
+	else
+	{
+		PIKA_PERMA_ASSERT(0, (std::string("invalid container name: ") + name).c_str());
+		return nullptr;
+	}
+
+}

+ 14 - 0
Pika/gameplay/containers.h

@@ -0,0 +1,14 @@
+#pragma once
+
+#include <pikaConfig.h>
+#include <baseContainer.h>
+//this is used to declare containers
+
+
+
+
+#include "containers/pikaGameplay.h"
+#include <pikaAllocator/memoryArena.h>
+
+
+Container *getContainer(const char* name, pika::memory::MemoryArena *memoryArena);

+ 4 - 0
Pika/gameplay/containers/README.md

@@ -0,0 +1,4 @@
+#containers
+
+A container is a isolated and memory managed
+unit of code that can be used for gameplay or for writing pluggins to the engine.

+ 1 - 0
Pika/gameplay/containers/pikaGameplay.cpp

@@ -0,0 +1 @@
+#include "pikaGameplay.h"

+ 33 - 0
Pika/gameplay/containers/pikaGameplay.h

@@ -0,0 +1,33 @@
+#pragma once
+
+#include <iostream>
+#include <gl2d/gl2d.h>
+#include <imgui.h>
+#include <baseContainer.h>
+
+struct Gameplay : public Container
+{
+
+	gl2d::Renderer2D renderer;
+
+	void create()
+	{
+		renderer.create();
+
+	}
+
+
+	void update()
+	{
+		gl2d::enableNecessaryGLFeatures();
+		renderer.updateWindowMetrics(640, 480);
+		renderer.renderRectangle({10,10, 100, 100}, Colors_Magenta);
+		renderer.flush();
+
+		//ImGui::SetAllocatorFunctions(userMalloc, userFree);
+
+		ImGui::Begin("test");
+		ImGui::End();
+	}
+
+};

+ 4 - 0
Pika/gameplay/dll/README.md

@@ -0,0 +1,4 @@
+#dll
+
+Here is the logic that will merge the runtime to the containers. On development mode all containers will live inside a dll.
+On release mode this functions will be compiled directly into the final game.

+ 59 - 0
Pika/gameplay/dll/dllMain.cpp

@@ -0,0 +1,59 @@
+#include "dllMain.h"
+
+#include <gl2d/gl2d.h>
+
+#include <imgui.h>
+
+#include "pikaImgui/pikaImgui.h"
+#include <assert/assert.h>
+
+#include "containers/pikaGameplay.h"
+#include <containers.h>
+
+#include <pikaAllocator/memoryArena.h>
+
+
+#define PIKA_MAKE_CONTAINER_INFO(x) ContainerInformation(sizeof(x), #x)
+
+PIKA_API void getContainersInfo(std::vector<ContainerInformation> &info)
+{
+	info.clear();
+	info.push_back(PIKA_MAKE_CONTAINER_INFO(Gameplay));
+}
+
+PIKA_API void constructContainer(Container **c, pika::memory::MemoryArena *arena, const char *name)
+{
+	*c = getContainer(name, arena);
+	PIKA_PERMA_ASSERT(*c, "coultn't create container(probably invalid name)");
+	return;
+
+
+}
+
+PIKA_API void destructContainer(Container **c, pika::memory::MemoryArena *arena)
+{
+	//placement delete here
+}
+
+
+
+PIKA_API void gameplayStart(pika::PikaContext pikaContext)
+{
+	//todo user should have functions to specify this
+#pragma region init stuff
+#ifdef PIKA_DEVELOPMENT
+	PIKA_PERMA_ASSERT(gladLoadGL(), "Problem initializing glad from dll");
+	pika::setImguiContext(pikaContext);
+#endif
+
+	gl2d::init();
+
+#pragma endregion
+}
+
+//todo: can remove
+PIKA_API void gameplayUpdate(pika::PikaContext pikaContext)
+{
+	
+
+}

+ 15 - 0
Pika/gameplay/dll/dllMain.h

@@ -0,0 +1,15 @@
+#pragma once
+#include <pikaConfig.h>
+#include <imgui.h>
+#include <pikaImgui/pikaImgui.h>
+#include <containers.h>
+#include <dllLoader/containerInformation.h>
+#include <vector>
+#include <dllLoader/dllLoader.h>
+#include <pikaAllocator/memoryArena.h>	
+
+PIKA_API void gameplayStart(pika::PikaContext pikaContext);
+PIKA_API void gameplayUpdate(pika::PikaContext pikaContext);
+PIKA_API void getContainersInfo(std::vector<ContainerInformation> &info);
+PIKA_API void constructContainer(Container **c, pika::memory::MemoryArena *arena, const char *name);
+PIKA_API void destructContainer(Container **c, pika::memory::MemoryArena *arena);

+ 0 - 40
Pika/gameplay/dllMain.cpp

@@ -1,40 +0,0 @@
-#include <iostream>
-#include <glad/glad.h>
-#include "dllMain.h"
-#include <imgui.h>
-#include "pikaImgui/pikaImgui.h"
-#include <assert/assert.h>
-
-#include <gl2d/gl2d.h>
-gl2d::Renderer2D renderer;
-
-
-PIKA_API void testStart(pika::PikaContext pikaContext)
-{
-	
-
-	PIKA_PERMA_ASSERT(gladLoadGL(), "Problem initializing glad from dll");
-
-
-	gl2d::init();
-	renderer.create();
-	pika::setContext(pikaContext);
-
-}
-
-
-PIKA_API void testUpdate(pika::PikaContext pikaContext)
-{
-	gl2d::enableNecessaryGLFeatures();
-	renderer.updateWindowMetrics(640, 480);
-	renderer.renderRectangle({10,10, 100, 100}, Colors_Magenta);
-	renderer.flush();
-
-
-//	ImGui::SetAllocatorFunctions(userMalloc, userFree);
-
-
-	ImGui::Begin("test");
-	ImGui::End();
-
-}

+ 0 - 8
Pika/gameplay/dllMain.h

@@ -1,8 +0,0 @@
-#pragma once
-#include <pikaConfig.h>
-#include <GLFW/glfw3.h>
-#include <imgui.h>
-#include <pikaImgui/pikaImgui.h>
-
-PIKA_API void testStart(pika::PikaContext pikaContext);
-PIKA_API void testUpdate(pika::PikaContext pikaContext);

+ 2 - 1
Pika/pluggins/README.md

@@ -1,3 +1,4 @@
 # pluggins
 # pluggins
 Here the user can write engine pluggins 
 Here the user can write engine pluggins 
-(they are not very different to writing gameplay code, but here there will be some basic pluggins that are usefull to any game like a .png viewer a .txt viewer or a .obj viewer)
+(they are not different to writing gameplay code, this folder is just for organization purposes,
+ here will be some basic pluggins that are usefull to any game like a .png viewer a .txt viewer or a .obj viewer)