Browse Source

working at pika asserts

vlod 3 years ago
parent
commit
22f947665a

+ 6 - 2
Pika/CMakeLists.txt

@@ -42,9 +42,13 @@ file(GLOB_RECURSE PIKA_SOURCES_CORE_STD		CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOUR
 
 
 
 
 
 
-
 add_executable(pikaCore)
 add_executable(pikaCore)
 set_property(TARGET pikaCore PROPERTY CXX_STANDARD 17)
 set_property(TARGET pikaCore PROPERTY CXX_STANDARD 17)
-target_sources(pikaCore PRIVATE "${PIKA_SOURCES_CORE_RUNTIME}" )
+target_sources(pikaCore PRIVATE 
+	"${PIKA_SOURCES_CORE_CONFIG}" "${PIKA_SOURCES_CORE_EDITOR}" "${PIKA_SOURCES_CORE_RUNTIME}" "${PIKA_SOURCES_CORE_STD}")
+target_include_directories(pikaCore PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/coreConfig/")
+target_include_directories(pikaCore PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/pikaEditor/")
+target_include_directories(pikaCore PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/pikaRuntime/")
+target_include_directories(pikaCore PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/core/pikaSTD/")
 target_link_libraries(pikaCore PRIVATE glad glfw gl2d glm stb_image stb_truetype)
 target_link_libraries(pikaCore PRIVATE glad glfw gl2d glm stb_image stb_truetype)
 
 

+ 22 - 0
Pika/core/coreConfig/pikaConfig.h

@@ -0,0 +1,22 @@
+#pragma once
+
+
+#pragma region Platform
+
+
+
+#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__) || defined(__NT__)
+
+#define PIKA_WINDOWS 1
+
+#elif defined(__linux__)
+
+#define PIKA_LINUX 1
+
+#else
+
+#error "Pika supports only windows and linux"
+
+#endif
+
+#pragma endregion

+ 4 - 0
Pika/core/pikaRuntime/pikaMain.cpp

@@ -7,6 +7,8 @@
 #include <gl2d/gl2d.h>
 #include <gl2d/gl2d.h>
 gl2d::Renderer2D renderer;
 gl2d::Renderer2D renderer;
 
 
+#include "assert/assert.h"
+
 int main()
 int main()
 {
 {
 
 
@@ -33,6 +35,8 @@ int main()
 	gl2d::init();
 	gl2d::init();
 	renderer.create();
 	renderer.create();
 
 
+	pika::assert::assertFunctionInternal("abc", __FILE__, __LINE__);
+
 	while (!glfwWindowShouldClose(window))
 	while (!glfwWindowShouldClose(window))
 	{
 	{
 
 

+ 122 - 0
Pika/core/pikaSTD/assert/assert.cpp

@@ -0,0 +1,122 @@
+#include <pikaConfig.h>
+#include "assert.h"
+#include <cstdlib>
+#include <cstdio>
+
+
+namespace pika
+{
+
+	namespace assert
+	{
+
+		void terminate()
+		{
+			std::exit(1);
+		}
+
+
+	#ifdef PIKA_WINDOWS
+
+	#include <Windows.h>
+
+		void assertFunctionInternal(const char *expression, const char *file,
+			int line, const char *comment)
+		{
+			char buffer[1024] = {};
+
+			std::snprintf(buffer, sizeof(buffer),
+				"Assertion failed\n\n"
+
+				"Expression: \n%s\n\n"
+
+				"File: %s\n"
+				"Line: %d\n\n"
+
+				"Comment: \n%s\n\n"
+
+				"Press retry to debug."
+				, expression
+				, file
+				, line
+				, comment
+			);
+
+			int const action = MessageBoxA(0, buffer, "Pika error", MB_TASKMODAL
+				| MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SETFOREGROUND);
+
+			switch (action)
+			{
+				case IDABORT: //Abort the program
+				{
+					terminate();
+					return;
+				}
+				case IDRETRY: //Break execution (debug)
+				{
+				#ifdef _MSC_VER
+					__debugbreak();
+				#endif
+					terminate();
+
+					return;
+				}
+				case IDIGNORE: //Ignore assert
+				{
+					return;
+				}
+			
+			}
+
+		};
+
+
+		void assertFunctionProduction(const char *expression, const char *file,
+			int line, const char *comment)
+		{
+
+			char buffer[1024] = {};
+
+			std::snprintf(buffer, sizeof(buffer),
+				"Assertion failed\n\n"
+
+				"Expression: \n%s\n\n"
+
+				"File: %s\n"
+				"Line: %d\n\n"
+
+				"Comment: \n%s\n\n"
+
+				"Please report this error to the developer."
+				, expression
+				, file
+				, line
+				, comment
+			);
+
+
+			int const action = MessageBoxA(0,
+				buffer, "Pika error", MB_TASKMODAL
+				| MB_ICONHAND | MB_OK | MB_SETFOREGROUND);
+
+			terminate();
+		}
+
+
+	#elif defined(PIKA_LINUX)
+
+
+		void assertFunctionProduction(const char *expression, const char *file,
+			int line, const char *comment)
+		{
+			terminate();
+		}
+
+
+	#endif
+
+
+	}
+
+
+};

+ 32 - 0
Pika/core/pikaSTD/assert/assert.h

@@ -0,0 +1,32 @@
+#pragma once
+#include <pikaConfig.h>
+
+
+
+namespace pika
+{
+
+	namespace assert
+	{
+
+
+		void terminate();
+
+		void assertFunctionInternal(
+			const char *expression, 
+			const char *file,
+			int line, 
+			const char *comment = nullptr);
+
+		void assertFunctionProduction
+		(
+			const char *expression,
+			const char *file,
+			int line,
+			const char *comment = nullptr
+		);
+
+
+	}
+
+}