Procházet zdrojové kódy

added keyboard input and logs

vlod před 3 roky
rodič
revize
498bd86197

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

@@ -1,6 +1,6 @@
 #include "dllLoader.h"
 #include "pikaConfig.h"
-#include <assert/assert.h>
+#include <logs/assert.h>
 
 #ifdef PIKA_DEVELOPMENT
 
@@ -129,9 +129,9 @@
 
 #endif
 
-	void pika::DllLoader::constructRuntimeContainer(RuntimeContainer &c, const char *name)
-	{
-		constructContainer_(&c.pointer, &c.arena, name);
-	}
+void pika::DllLoader::constructRuntimeContainer(RuntimeContainer &c, const char *name)
+{
+	constructContainer_(&c.pointer, &c.arena, name);
+}
 
 	

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

@@ -6,16 +6,23 @@
 #include <windowSystemm/window.h>
 
 
-#include "assert/assert.h"
+#include "logs/assert.h"
 #include "dllLoader/dllLoader.h"
 #include "pikaImgui/pikaImgui.h"
 
 #include <pikaAllocator/memoryArena.h>
 #include <runtimeContainer/runtimeContainer.h>
 
+#include <logs/log.h>
+
 int main()
 {
 
+#pragma region log
+	pika::LogManager logs;
+	logs.init("logs.txt");
+#pragma endregion
+
 #pragma region load dll
 	std::filesystem::path currentPath = std::filesystem::current_path();
 	pika::DllLoader dllLoader;
@@ -43,17 +50,18 @@ int main()
 	dllLoader.getContainersInfo_(loadedContainers);
 #pragma endregion
 
-
+	logs.log("test");
 
 	RuntimeContainer container;
-	container.arena.allocateStaticMemory(loadedContainers[0]);
+	container.arena.allocateStaticMemory(loadedContainers[0]); //this just allocates the memory
 
-	dllLoader.constructRuntimeContainer(container, "Gameplay");
-	container.pointer->create();
+	dllLoader.constructRuntimeContainer(container, "Gameplay"); //this calls the constructors
+	container.pointer->create();	//this calls create()
 
 	while (!window.shouldClose())
 	{
 
+
 		if (dllLoader.reloadDll())
 		{
 			dllLoader.gameplayReload_(window.context);
@@ -65,7 +73,7 @@ int main()
 		pika::imguiStartFrame(window.context);
 
 		//gameplayUpdate(context);
-		container.pointer->update(window.input);
+		container.pointer->update(window.input, window.deltaTime, window.windowState);
 
 		pika::imguiEndFrame(window.context);
 

+ 93 - 10
Pika/core/pikaRuntime/windowSystemm/callbacks.cpp

@@ -1,34 +1,117 @@
 #include "callbacks.h"
 #include "window.h"
 
-void mouseCallback(GLFWwindow *window, int key, int action, int mods)
-{
 
+void windowFocusCallback(GLFWwindow *window, int focused)
+{
 	auto ptr = glfwGetWindowUserPointer(window);
 	pika::PikaWindow &pikaWindow = *(pika::PikaWindow *)ptr;
 
-	bool state = 0;
+	if (focused)
+	{
+		pikaWindow.windowState.hasFocus = 1;
+	}
+	else
+	{
+		pikaWindow.windowState.hasFocus = 0;
+	}
+}
+
+static void processAButton(pika::Button &b, int action)
+{
 	if (action == GLFW_PRESS)
 	{
-		state = 1;
+		b.setHeld(true);
 	}
 	else if (action == GLFW_RELEASE)
 	{
-		state = 0;
+		b.setHeld(false);
 	}
-	else
+	else if (action == GLFW_REPEAT)
 	{
-		return; //we don't care about any other actions
+		b.setHeld(true);
+		b.setTyped(true);
 	}
 
+};
+
+void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
+{
+	auto ptr = glfwGetWindowUserPointer(window);
+	pika::PikaWindow &pikaWindow = *(pika::PikaWindow *)ptr;
+
+	if(key >= GLFW_KEY_A && key <= GLFW_KEY_Z)
+	{
+		int index = key - GLFW_KEY_A;
+		processAButton(pikaWindow.input.buttons[pika::Button::A + index], action);
+	}else if (key >= GLFW_KEY_0 && key <= GLFW_KEY_9)
+	{
+		int index = key - GLFW_KEY_0;
+		processAButton(pikaWindow.input.buttons[pika::Button::NR0 + index], action);
+	}else
+	{
+
+		if (key == GLFW_KEY_SPACE)
+		{
+			processAButton(pikaWindow.input.buttons[pika::Button::Space], action);
+		}
+		else
+		if (key == GLFW_KEY_ENTER)
+		{
+			processAButton(pikaWindow.input.buttons[pika::Button::Enter], action);
+		}
+		else
+		if (key == GLFW_KEY_ESCAPE)
+		{
+			processAButton(pikaWindow.input.buttons[pika::Button::Escape], action);
+		}
+		else
+		if (key == GLFW_KEY_UP)
+		{
+			processAButton(pikaWindow.input.buttons[pika::Button::Up], action);
+		}
+		else
+		if (key == GLFW_KEY_DOWN)
+		{
+			processAButton(pikaWindow.input.buttons[pika::Button::Down], action);
+		}
+		else
+		if (key == GLFW_KEY_LEFT)
+		{
+			processAButton(pikaWindow.input.buttons[pika::Button::Left], action);
+		}
+		else
+		if (key == GLFW_KEY_RIGHT)
+		{
+			processAButton(pikaWindow.input.buttons[pika::Button::Right], action);
+		}
+		else
+		if (key == GLFW_KEY_LEFT_CONTROL)
+		{
+			processAButton(pikaWindow.input.buttons[pika::Button::LeftCtrl], action);
+		}
+		if (key == GLFW_KEY_TAB)
+		{
+			processAButton(pikaWindow.input.buttons[pika::Button::Tab], action);
+		}
+	}
+
+}
+
+void mouseCallback(GLFWwindow *window, int key, int action, int mods)
+{
+
+	auto ptr = glfwGetWindowUserPointer(window);
+	pika::PikaWindow &pikaWindow = *(pika::PikaWindow *)ptr;
+
 	if (key == GLFW_MOUSE_BUTTON_LEFT)
 	{
-		pikaWindow.input.lMouse.setHeld(state);
+		processAButton(pikaWindow.input.lMouse, action);
 	}
 	else if(key == GLFW_MOUSE_BUTTON_RIGHT)
 	{
-		pikaWindow.input.rMouse.setHeld(state);
+		processAButton(pikaWindow.input.rMouse, action);
 	}
 
 
-}
+}

+ 3 - 1
Pika/core/pikaRuntime/windowSystemm/callbacks.h

@@ -3,4 +3,6 @@
 #include <GLFW/glfw3.h>
 
 
-void mouseCallback(GLFWwindow *window, int key, int action, int mods);
+void mouseCallback(GLFWwindow *window, int key, int action, int mods);
+void windowFocusCallback(GLFWwindow *window, int focused);
+void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);

+ 18 - 1
Pika/core/pikaRuntime/windowSystemm/input.h

@@ -40,18 +40,35 @@ namespace pika
 		//last state of the button (last frame)
 		PIKA_ADD_FLAG(lastState, setLastState, 5);
 
+		enum
+		{
+			A = 0,
+			B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
+			NR0, NR1, NR2, NR3, NR4, NR5, NR6, NR7, NR8, NR9,
+			Space, Enter, Escape,
+			Up, Down, Left, Right,
+			LeftCtrl, Tab,
+			BUTTONS_COUNT, //
+		};
+
 	};
 
 	struct Input
 	{
-
 		Button lMouse = {};
 		Button rMouse = {};
 
+		//mouse position relevant to window
+		int mouseX = 0;
+		int mouseY = 0;
 
+		Button buttons[Button::BUTTONS_COUNT] = {};
 
 	};
 
+	
+
+
 
 
 };

+ 57 - 1
Pika/core/pikaRuntime/windowSystemm/window.cpp

@@ -1,18 +1,24 @@
 #include "window.h"
-#include <assert/assert.h>
+#include <logs/assert.h>
 #include "callbacks.h"
 
 void pika::PikaWindow::create()
 {
 	context.wind = glfwCreateWindow(640, 480, "Pika", NULL, NULL);
 	
+	windowState.hasFocus = true;
+
 	PIKA_PERMA_ASSERT(context.wind, "problem initializing window");
 	glfwMakeContextCurrent(context.wind);
 
 	glfwSetWindowUserPointer(context.wind, this);
 
 	glfwSetMouseButtonCallback(context.wind, mouseCallback);
+	glfwSetWindowFocusCallback(context.wind, windowFocusCallback);
+	glfwSetKeyCallback(context.wind, keyCallback);
+
 
+	timer = std::chrono::high_resolution_clock::now();
 }
 
 bool pika::PikaWindow::shouldClose()
@@ -22,14 +28,58 @@ bool pika::PikaWindow::shouldClose()
 
 void pika::PikaWindow::update()
 {
+#pragma region deltaTime
+	auto end = std::chrono::high_resolution_clock::now();
+	deltaTime = (std::chrono::duration_cast<std::chrono::microseconds>(end - timer)).count() / 1000000.0f;
+	timer = end;
+
+	if (deltaTime > 1.f / 10) { deltaTime = 1.f / 10; }
+#pragma endregion
 
+#pragma region input
+
+	auto processInputBefore = [](pika::Button &b)
+	{
+		b.setTyped(false);
+	};
+
+	processInputBefore(input.lMouse);
+	processInputBefore(input.rMouse);
+
+	for (int i = 0; i < Button::BUTTONS_COUNT; i++)
+	{
+		processInputBefore(input.buttons[i]);
+	}
+
+#pragma endregion
 
 	glfwPollEvents();
 	glfwSwapBuffers(context.wind);
 
+#pragma region window state
+
+	{
+		int w = 0;
+		int h = 0;
+		glfwGetWindowSize(context.wind, &w, &h);
+
+		windowState.w = w;
+		windowState.h = h;
+
+	}
+
+#pragma endregion
+
 
 #pragma region input
 
+	double mouseX = 0;
+	double mouseY = 0;
+	glfwGetCursorPos(context.wind, &mouseX, &mouseY);
+
+	input.mouseX = (int)mouseX;
+	input.mouseY = (int)mouseY;
+
 	auto processInput = [](pika::Button &b)
 	{
 
@@ -58,6 +108,12 @@ void pika::PikaWindow::update()
 	processInput(input.lMouse);
 	processInput(input.rMouse);
 
+	for (int i = 0; i < Button::BUTTONS_COUNT; i++)
+	{
+		processInput(input.buttons[i]);
+	}
+
 
 #pragma endregion
+
 }

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

@@ -3,16 +3,36 @@
 #include <GLFW/glfw3.h>
 #include <pikaContext.h>
 #include "input.h"
+#include <chrono>
 
 namespace pika
 {
 
+	
+	struct WindowState
+	{
+		int w = 0;
+		int h = 0;
+		bool hasFocus = 0;
+
+
+	};
+	
+
 	//this is not intended to have multiple instances in the program
 	struct PikaWindow
 	{
 		pika::PikaContext context = {};
 
+
+		//this is made to be passed to the user code
+		//on live code editing this will be recorded every frame
 		Input input = {};
+		float deltaTime = 0;
+
+
+		WindowState windowState = {};
+
 
 		//this doesn't return error codes because it will do the asserts for you
 		void create();
@@ -21,6 +41,7 @@ namespace pika
 
 		void update();
 
+		std::chrono::steady_clock::time_point timer = {};
 	};
 
 

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

@@ -7,6 +7,6 @@ struct Container
 
 	virtual void create() = 0;
 
-	virtual void update(pika::Input input) = 0;
+	virtual void update(pika::Input input, float deltaTime, pika::WindowState windowState) = 0;
 
 };

+ 0 - 0
Pika/core/pikaSTD/assert/assert.cpp → Pika/core/pikaSTD/logs/assert.cpp


+ 0 - 0
Pika/core/pikaSTD/assert/assert.h → Pika/core/pikaSTD/logs/assert.h


+ 59 - 0
Pika/core/pikaSTD/logs/log.cpp

@@ -0,0 +1,59 @@
+#include "log.h"
+#include <fstream>
+#include <chrono>
+#include <iomanip>
+
+void pika::LogManager::init(std::string name)
+{
+	if (name == "")
+	{
+		internalLogs.reserve(10);
+	}
+
+	this->name = name;
+	bool firstLog = 0;
+	//this might do some other things in the future
+}
+
+void pika::LogManager::log(const char *l, int type)
+{
+
+#ifdef PIKA_DEVELOPMENT
+
+#endif
+
+#ifdef PIKA_PRODUCTION
+
+#endif
+
+	logToFile(l, type);
+}
+
+std::stringstream pika::LogManager::formatLog(const char *l, int type)
+{
+	auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
+	std::stringstream s;
+	s << "#" << std::put_time(std::localtime(&time), "%Y-%m-%d %X") << ": ";
+	s << l << "\n";
+	return s;
+}
+
+void pika::LogManager::logToFile(const char *l, int type)
+{
+	//todo unlickely atribute
+	if (!firstLog)
+	{
+		firstLog = 1;
+		std::ofstream file(name);
+		file.close(); //clear the file content
+	}
+
+	std::ofstream file(name, std::ofstream::app);
+	file << formatLog(l, type).rdbuf();
+	file.close();
+}
+
+void pika::LogManager::logInternally(const char *l, int type)
+{
+	internalLogs.push_back(formatLog(l, type).str());
+}

+ 44 - 0
Pika/core/pikaSTD/logs/log.h

@@ -0,0 +1,44 @@
+#pragma region
+#include <string>
+#include <vector>
+#include <sstream>
+
+namespace pika
+{
+
+
+	struct LogManager
+	{
+
+		//a null name will just log to a internal structure
+		void init(std::string name);
+
+		enum : int
+		{
+			logNormal = 0,
+			logWarning,
+			logError
+		};
+
+		//this will be dependent on the configuration of the project. 
+		void log(const char *l, int type = logNormal);
+		
+
+		std::string name = "";
+		bool firstLog = 0;
+
+		std::vector<std::string> internalLogs;
+
+	private:
+		//used only interally.
+		std::stringstream formatLog(const char *l, int type = logNormal);
+		void logToFile(const char *l, int type = logNormal);
+		void logInternally(const char *l, int type = logNormal);
+
+	};
+
+
+
+
+
+}

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

@@ -10,7 +10,7 @@
 #include <algorithm>
 
 
-#include <assert/assert.h>
+#include <logs/assert.h>
 
 
 namespace pika

+ 1 - 1
Pika/gameplay/containers.cpp

@@ -1,5 +1,5 @@
 #include <containers.h>
-#include <assert/assert.h>
+#include <logs/assert.h>
 
 
 Container *getContainer(const char *name, pika::memory::MemoryArena *memoryArena)

+ 18 - 9
Pika/gameplay/containers/pikaGameplay.h

@@ -17,22 +17,31 @@ struct Gameplay : public Container
 	}
 
 
-	void update(pika::Input input)
+	void update(pika::Input input, float deltaTime, pika::WindowState windowState)
 	{
+
+
 		gl2d::enableNecessaryGLFeatures();
-		renderer.updateWindowMetrics(640, 480);
+		renderer.updateWindowMetrics(windowState.w, windowState.h);
 
-		renderer.renderRectangle({10,10, 100, 100}, Colors_Magenta);
+		renderer.renderRectangle({10, 10, 100, 100}, Colors_Magenta);
 
-		if (input.lMouse.pressed())
-		{
-			std::cout << "pressed\n";
-		}
-		if (input.lMouse.released())
+		//if (input.lMouse.pressed())
+		//{
+		//	std::cout << "pressed\n";
+		//}
+		//if (input.lMouse.released())
+		//{
+		//	std::cout << "released\n";
+		//}
+
+		if (input.lMouse.typed())
 		{
-			std::cout << "released\n";
+			std::cout << "typed\n";
 		}
 
+		//std::cout << state.deltaTime << "\n";
+
 		renderer.flush();
 
 		//ImGui::SetAllocatorFunctions(userMalloc, userFree);

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

@@ -5,7 +5,7 @@
 #include <imgui.h>
 
 #include "pikaImgui/pikaImgui.h"
-#include <assert/assert.h>
+#include <logs/assert.h>
 
 #include "containers/pikaGameplay.h"
 #include <containers.h>