瀏覽代碼

working at adding input

vlod 3 年之前
父節點
當前提交
4a33f48eaa

+ 1 - 1
Pika/core/pikaRuntime/pikaMain.cpp

@@ -65,7 +65,7 @@ int main()
 		pika::imguiStartFrame(window.context);
 
 		//gameplayUpdate(context);
-		container.pointer->update();
+		container.pointer->update(window.input);
 
 		pika::imguiEndFrame(window.context);
 

+ 34 - 0
Pika/core/pikaRuntime/windowSystemm/callbacks.cpp

@@ -0,0 +1,34 @@
+#include "callbacks.h"
+#include "window.h"
+
+void mouseCallback(GLFWwindow *window, int key, int action, int mods)
+{
+
+	auto ptr = glfwGetWindowUserPointer(window);
+	pika::PikaWindow &pikaWindow = *(pika::PikaWindow *)ptr;
+
+	bool state = 0;
+	if (action == GLFW_PRESS)
+	{
+		state = 1;
+	}
+	else if (action == GLFW_RELEASE)
+	{
+		state = 0;
+	}
+	else
+	{
+		return; //we don't care about any other actions
+	}
+
+	if (key == GLFW_MOUSE_BUTTON_LEFT)
+	{
+		pikaWindow.input.lMouse.setHeld(state);
+	}
+	else if(key == GLFW_MOUSE_BUTTON_RIGHT)
+	{
+		pikaWindow.input.rMouse.setHeld(state);
+	}
+
+
+}

+ 6 - 0
Pika/core/pikaRuntime/windowSystemm/callbacks.h

@@ -0,0 +1,6 @@
+#pragma once
+#include <glad/glad.h> //so we don't have conflicts
+#include <GLFW/glfw3.h>
+
+
+void mouseCallback(GLFWwindow *window, int key, int action, int mods);

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

@@ -1,14 +1,52 @@
 #pragma once
 #include <cstdint>
 
+
+#define PIKA_ADD_FLAG(NAME, SETNAME, VALUE)	\
+	bool NAME() {return (flags & ((std::uint32_t)1<<VALUE)); } \
+	void SETNAME(bool s) { \
+		if (s) { flags = flags | ((std::uint32_t)1 << VALUE); }	\
+			else { flags = flags & ~((std::uint32_t)1 << VALUE); }	\
+		}
+	
+
 namespace pika
 {
 
+
 	struct Button
 	{
+		//internal use only
 		float timer = 0;
+
+		//internal use only
 		std::uint32_t flags = 0;
 
+		//true in the first frame the key is pressed
+		PIKA_ADD_FLAG(pressed, setPressed, 0);
+
+		//true while the key is held
+		PIKA_ADD_FLAG(held, setHeld, 1);
+	
+		//true in the frame the key is released
+		PIKA_ADD_FLAG(released, setReleased, 2);
+
+		//true in the first frame is pressed then after a pause true every few milliseconds
+		PIKA_ADD_FLAG(typed, setTyped, 3);
+
+		//true if the key is double pressed (true only for one frame, 3 presses would yield only one frame of this being true)
+		PIKA_ADD_FLAG(doublePressed, setDoublePressed, 4);
+
+		//last state of the button (last frame)
+		PIKA_ADD_FLAG(lastState, setLastState, 5);
+
+	};
+
+	struct Input
+	{
+
+		Button lMouse = {};
+		Button rMouse = {};
 
 
 
@@ -16,6 +54,8 @@ namespace pika
 
 
 
+};
+
 
 
-};
+#undef PIKA_ADD_FLAG

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

@@ -1,5 +1,6 @@
 #include "window.h"
 #include <assert/assert.h>
+#include "callbacks.h"
 
 void pika::PikaWindow::create()
 {
@@ -7,6 +8,11 @@ void pika::PikaWindow::create()
 	
 	PIKA_PERMA_ASSERT(context.wind, "problem initializing window");
 	glfwMakeContextCurrent(context.wind);
+
+	glfwSetWindowUserPointer(context.wind, this);
+
+	glfwSetMouseButtonCallback(context.wind, mouseCallback);
+
 }
 
 bool pika::PikaWindow::shouldClose()
@@ -16,6 +22,42 @@ bool pika::PikaWindow::shouldClose()
 
 void pika::PikaWindow::update()
 {
+
+
 	glfwPollEvents();
 	glfwSwapBuffers(context.wind);
+
+
+#pragma region input
+
+	auto processInput = [](pika::Button &b)
+	{
+
+		if (!b.lastState() && b.held())
+		{
+			b.setPressed(true);
+		}
+		else
+		{
+			b.setPressed(false);
+		}
+
+		if (b.lastState() && !b.held())
+		{
+			b.setReleased(true);
+		}
+		else
+		{
+			b.setReleased(false);
+		}
+
+		b.setLastState(b.held());
+
+	};
+
+	processInput(input.lMouse);
+	processInput(input.rMouse);
+
+
+#pragma endregion
 }

+ 4 - 1
Pika/core/pikaRuntime/windowSystemm/window.h

@@ -1,7 +1,8 @@
 #pragma once
+#include <glad/glad.h> //so we don't have conflicts
 #include <GLFW/glfw3.h>
 #include <pikaContext.h>
-
+#include "input.h"
 
 namespace pika
 {
@@ -11,6 +12,8 @@ namespace pika
 	{
 		pika::PikaContext context = {};
 
+		Input input = {};
+
 		//this doesn't return error codes because it will do the asserts for you
 		void create();
 

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

@@ -1,5 +1,5 @@
 #pragma once
-
+#include <windowSystemm/window.h>
 
 
 struct Container
@@ -7,6 +7,6 @@ struct Container
 
 	virtual void create() = 0;
 
-	virtual void update() = 0;
+	virtual void update(pika::Input input) = 0;
 
 };

+ 12 - 1
Pika/gameplay/containers/pikaGameplay.h

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