Browse Source

added delta time pause and gl2d controller

meemknight 2 years ago
parent
commit
61db221b37

+ 18 - 18
Pika/CMakeLists.txt

@@ -101,23 +101,23 @@ target_link_libraries(pikaGameplay PRIVATE glad glfw gl2d gl3d glui glm stb_imag
 
 
 #pikaProduction ###########################################
-add_executable(pikaProduction)
-
-target_compile_definitions(pikaProduction PUBLIC PIKA_PRODUCTION)
-set_property(TARGET pikaProduction PROPERTY CXX_STANDARD 17)
-
-target_sources(pikaProduction PRIVATE 
-	"${PIKA_SOURCES_CORE_CONFIG}" "${PIKA_SOURCES_CORE_EDITOR}" "${PIKA_SOURCES_PLUGGINS}"
-	"${PIKA_SOURCES_CORE_RUNTIME}" "${PIKA_SOURCES_CORE_STD}" "${PIKA_SOURCES_GAMEPLAY}" "${PIKA_SOURCES_CORE_SHARED_RUNTIME}")
-target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/coreConfig/")
-target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/pikaEditor/")
-target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/pikaRuntime/")
-target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/pikaSTD/")
-target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/gameplay/")
-target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/sharedRuntime/")
-target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/pluggins/")
-
-target_link_libraries(pikaProduction PRIVATE glad glfw gl2d gl3d glui glm stb_image stb_truetype imgui safeSave profilerLib box2d)
+#add_executable(pikaProduction)
+#
+#target_compile_definitions(pikaProduction PUBLIC PIKA_PRODUCTION)
+#set_property(TARGET pikaProduction PROPERTY CXX_STANDARD 17)
+#
+#target_sources(pikaProduction PRIVATE 
+#	"${PIKA_SOURCES_CORE_CONFIG}" "${PIKA_SOURCES_CORE_EDITOR}" "${PIKA_SOURCES_PLUGGINS}"
+#	"${PIKA_SOURCES_CORE_RUNTIME}" "${PIKA_SOURCES_CORE_STD}" "${PIKA_SOURCES_GAMEPLAY}" "${PIKA_SOURCES_CORE_SHARED_RUNTIME}")
+#target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/coreConfig/")
+#target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/pikaEditor/")
+#target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/pikaRuntime/")
+#target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/pikaSTD/")
+#target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/gameplay/")
+#target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/sharedRuntime/")
+#target_include_directories(pikaProduction PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/pluggins/")
+#
+#target_link_libraries(pikaProduction PRIVATE glad glfw gl2d gl3d glui glm stb_image stb_truetype imgui safeSave profilerLib box2d)
 
 
 
@@ -128,7 +128,7 @@ target_link_libraries(pikaProduction PRIVATE glad glfw gl2d gl3d glui glm stb_im
 if(MSVC)
 	
 	#todo maybe try to bring back console in production in the future
-	 set_target_properties(pikaProduction PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
+	 #set_target_properties(pikaProduction PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
 	 set_target_properties(pikaCore PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
 
 endif()

+ 7 - 1
Pika/core/pikaEditor/containersWindow/containersWindow.cpp

@@ -391,6 +391,13 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 
 					#pragma endregion
 
+						ImGui::Separator();
+						ImGui::Checkbox("Pause", &c.deltaTimePaused);
+						ImGui::SameLine();
+						ImGui::DragFloat("Simulation speed", &c.simulationSpeed, 0.01, 0, 100);
+
+						ImGui::Separator();
+
 						if(!stopped)
 						{
 
@@ -398,7 +405,6 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 
 							ImGui::Text("Time (ms): %f", c.currentMs);
 
-							ImGui::Separator();
 
 							if (ImGui::Button("Calculate memory usage"))
 							{

+ 2 - 0
Pika/core/pikaRuntime/containerManager/containerManager.cpp

@@ -434,6 +434,8 @@ void pika::ContainerManager::update(pika::LoadedDll &loadedDll, pika::PikaWindow
 
 				}
 
+				windowInput.deltaTime *= c.second.simulationSpeed;
+				windowInput.deltaTime *= !c.second.deltaTimePaused;
 
 				c.second.requestedContainerInfo.mainAllocator = &c.second.allocator; //reset this
 				c.second.requestedContainerInfo.bonusAllocators = &c.second.bonusAllocators;

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

@@ -41,6 +41,8 @@ struct RuntimeContainer
 	size_t availableMemory = 0;
 	size_t biggestBlock = 0;
 	int freeBlocks = 0;
+	float simulationSpeed = 1;
+	bool deltaTimePaused = 0;
 
 	bool lastFrameFocus = 0;
 
@@ -48,6 +50,8 @@ struct RuntimeContainer
 	bool andInputWithWindowHasFocus = 0;
 	bool andInputWithWindowHasFocusLastFrame = 0;
 
+	
+
 	struct FLAGS
 	{
 		enum

+ 30 - 0
Pika/core/pikaSTD/engineLibraresSupport/engineGL2DSupport.cpp

@@ -83,3 +83,33 @@
 
 	return f;
 }
+
+void pika::gl2d::cameraController(::gl2d::Camera &c, Input &input, float speed)
+{
+	if (input.buttons[pika::Button::W].held())
+	{
+		c.position.y -= speed * input.deltaTime;
+	}
+	if (input.buttons[pika::Button::S].held())
+	{
+		c.position.y += speed * input.deltaTime;
+	}
+	if (input.buttons[pika::Button::A].held())
+	{
+		c.position.x -= speed * input.deltaTime;
+	}
+	if (input.buttons[pika::Button::D].held())
+	{
+		c.position.x += speed * input.deltaTime;
+	}
+	if (input.buttons[pika::Button::Q].held())
+	{
+		c.zoom -= 1 * input.deltaTime;
+	}
+	if (input.buttons[pika::Button::E].held())
+	{
+		c.zoom += 1 * input.deltaTime;
+	}
+
+	c.zoom = glm::clamp(c.zoom, 0.0001f, 1000.f);
+}

+ 2 - 0
Pika/core/pikaSTD/engineLibraresSupport/engineGL2DSupport.h

@@ -19,6 +19,8 @@ namespace pika
 
 		::gl2d::Font loadFont(const char *path, RequestedContainerInfo &info);
 
+		//speed without delta time
+		void cameraController(::gl2d::Camera &c, Input &input, float speed);
 
 	}
 

+ 109 - 1
Pika/gameplay/containers/hollowknight/hollowknight.h

@@ -60,6 +60,7 @@ struct Holloknight: public Container
 			dynamicBody->CreateFixture(&boxFixtureDef);
 		}
 
+		//remove
 		void updateMetrics(b2World &world) 
 		{
 			auto pos = dynamicBody->GetPosition();
@@ -78,7 +79,9 @@ struct Holloknight: public Container
 
 	Block floor;
 	Block blocks[10];
+	b2Body *currentBodySelected = 0;
 
+	glm::vec2 draggedStart = {};
 
 	bool create(RequestedContainerInfo &requestedInfo, pika::StaticString<256> commandLineArgument)
 	{
@@ -105,8 +108,15 @@ struct Holloknight: public Container
 	bool update(pika::Input input, pika::WindowState windowState,
 		RequestedContainerInfo &requestedInfo)
 	{
+	#pragma region clear stuff
 		glClear(GL_COLOR_BUFFER_BIT);
 		renderer.updateWindowMetrics(windowState.windowW, windowState.windowH);
+	#pragma endregion
+
+	#pragma region input
+		::pika::gl2d::cameraController(renderer.currentCamera, input, 200);
+	#pragma endregion
+
 		int32 velocityIterations = 6;
 		int32 positionIterations = 2;
 		world.Step(input.deltaTime, velocityIterations, positionIterations);
@@ -118,7 +128,6 @@ struct Holloknight: public Container
 			blocks[i].updateMetrics(world);
 		}
 
-
 		floor.render(renderer, Colors_White);
 
 		for (int i = 0; i < 10; i++)
@@ -126,6 +135,105 @@ struct Holloknight: public Container
 			blocks[i].render(renderer, Colors_Orange);
 		}
 
+		glm::vec2 mouseWorldpos(input.mouseX, input.mouseY);
+		{
+			auto viewRect = renderer.getViewRect();
+
+			glm::vec2 mousePosNormalized = mouseWorldpos / glm::vec2(windowState.frameBufferW, windowState.frameBufferH);
+			
+			mouseWorldpos = glm::vec2(viewRect) + mousePosNormalized * glm::vec2(viewRect.z, viewRect.w);
+		}
+
+	#pragma region body render
+
+		for (b2Body *b = world.GetBodyList(); b; b = b->GetNext())
+		{
+
+			glm::vec4 color = Colors_Red;
+			float thickness = 1;
+
+			if (currentBodySelected == b) 
+			{
+				color = Colors_Blue;
+				thickness = 2;
+			}
+
+			auto centerOfMass = b->GetWorldCenter();
+			auto angleDegrees = glm::degrees(b->GetAngle());
+			auto shapePos = b->GetTransform();
+
+			renderer.renderRectangle({centerOfMass.x - thickness,centerOfMass.y - thickness,2* thickness,2* thickness}, 
+				color, {}, angleDegrees);
+
+			for (b2Fixture *f = b->GetFixtureList(); f; f = f->GetNext()) 
+			{
+				auto shape = f->GetShape();
+
+				if (shape->TestPoint(shapePos, {mouseWorldpos.x,mouseWorldpos.y}) && input.lMouse.held())
+				{
+					currentBodySelected = b;
+				}
+
+				//the shape doesn't know anything about position so we have to move it from 0 0 
+				if (shape->GetType() == b2Shape::Type::e_polygon) 
+				{
+					b2PolygonShape *poligon = dynamic_cast<b2PolygonShape*>(shape);
+				
+					if (poligon)
+					{
+						for (int i = 0; i < poligon->m_count; i++)
+						{
+							int j = (i + 1) % poligon->m_count;
+
+							glm::vec2 p1 = {poligon->m_vertices[i].x,poligon->m_vertices[i].y};
+							glm::vec2 p2 = {poligon->m_vertices[j].x,poligon->m_vertices[j].y};
+
+							p1 += glm::vec2{centerOfMass.x, centerOfMass.y};
+							p2 += glm::vec2{centerOfMass.x, centerOfMass.y};
+
+							p1 = gl2d::rotateAroundPoint(p1, {centerOfMass.x,-centerOfMass.y}, angleDegrees);
+							p2 = gl2d::rotateAroundPoint(p2, {centerOfMass.x,-centerOfMass.y}, angleDegrees);
+
+							renderer.renderLine(p1, p2, color, thickness*2);
+						}
+
+					}
+
+				}
+			
+			}
+
+
+		}
+
+	#pragma endregion
+
+	#pragma region drag
+
+		{
+
+			if (input.rMouse.pressed())
+			{
+				draggedStart = glm::vec2(input.mouseX, input.mouseY);
+			}
+
+			if (input.rMouse.released())
+			{
+				glm::vec2 dragEnd(input.mouseX, input.mouseY);
+
+				glm::vec2 movement = dragEnd - draggedStart;
+
+				if (currentBodySelected)
+				{
+					currentBodySelected->SetLinearVelocity({movement.x,movement.y});
+				}
+			}
+			
+		}
+
+
+	#pragma endregion
+
 
 		renderer.flush();
 

+ 12 - 7
Pika/resources/logs.txt

@@ -1,7 +1,12 @@
-#2023-07-11 23:36:25: Created container: Holloknight
-#2023-07-11 23:37:20: Destroyed continer: Holloknight #1
-#2023-07-11 23:37:28[warning]: Couldn't reloaded dll
-#2023-07-11 23:37:30[warning]: Couldn't reloaded dll
-#2023-07-11 23:37:32: Reloaded dll
-#2023-07-11 23:38:29: Created container: Holloknight
-#2023-07-11 23:39:03: Destroyed continer: Holloknight #2
+#2023-07-12 12:14:18: Created container: Holloknight
+#2023-07-12 12:18:41[error]: Killed container because its static container info
+has changed: Holloknight #1
+#2023-07-12 12:18:41: Force terminated continer: Holloknight #1
+#2023-07-12 12:18:42: Reloaded dll
+#2023-07-12 12:18:47: Created container: Holloknight
+#2023-07-12 12:25:47[error]: Killed container because its static container info
+has changed: Holloknight #2
+#2023-07-12 12:25:47: Force terminated continer: Holloknight #2
+#2023-07-12 12:25:47: Reloaded dll
+#2023-07-12 12:25:52: Created container: Holloknight
+#2023-07-12 12:27:11: Reloaded dll