initial commit
authorStan_Lewry <stanley.jml@gmail.com>
Fri, 30 Aug 2024 17:49:22 +0000 (18:49 +0100)
committerStan_Lewry <stanley.jml@gmail.com>
Fri, 30 Aug 2024 17:49:22 +0000 (18:49 +0100)
2DEngine.cpp [new file with mode: 0644]
2DEngine.h [new file with mode: 0644]
dllmain.cpp [new file with mode: 0644]
framework.h [new file with mode: 0644]
pch.cpp [new file with mode: 0644]
pch.h [new file with mode: 0644]

diff --git a/2DEngine.cpp b/2DEngine.cpp
new file mode 100644 (file)
index 0000000..2114b6c
--- /dev/null
@@ -0,0 +1,177 @@
+#include "pch.h"\r
+#include "2DEngine.h"\r
+\r
+#include <iostream>\r
+#include <fstream>\r
+\r
+namespace S2DE {\r
+\r
+       bool initRendering(const char* windowTitle, int windowWidth, int windowHeight, SDL_Window** window, SDL_Renderer** renderer) {\r
+\r
+               std::cout << "init rendering" << std::endl;\r
+               bool ret = false;\r
+\r
+               SDL_Init(SDL_INIT_EVERYTHING);\r
+               IMG_Init(IMG_INIT_PNG);\r
+               \r
+               Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);\r
+\r
+               *window = SDL_CreateWindow(windowTitle, SDL_WINDOWPOS_UNDEFINED,\r
+                       SDL_WINDOWPOS_UNDEFINED, windowWidth, windowHeight, SDL_RENDERER_ACCELERATED);\r
+\r
+               if (window) {\r
+\r
+                       //SDL_SetWindowBordered(window, SDL_FALSE); // could be made configurable\r
+\r
+                       *renderer = SDL_CreateRenderer(*window, 0, SDL_RENDERER_ACCELERATED);\r
+\r
+                       if (*renderer) {\r
+                               SDL_SetRenderDrawBlendMode(*renderer, SDL_BLENDMODE_BLEND);\r
+                               SDL_SetRenderDrawColor(*renderer, 0, 0, 0, 255);\r
+                               ret = true;\r
+                       }\r
+                       else {\r
+                               std::cout << "Failed to init renderer" << std::endl;\r
+                       }\r
+               }\r
+               else {\r
+                       std::cout << "Failed to init window" << std::endl;\r
+               }\r
+\r
+               if (*renderer != nullptr && *window != nullptr)\r
+               {\r
+                       std::cout << "SUCCESS" << std::endl;\r
+               }\r
+\r
+               return ret;\r
+       }\r
+\r
+       void quitRendering(SDL_Renderer** renderer, SDL_Window** window) {\r
+               SDL_DestroyRenderer(*renderer);\r
+               SDL_DestroyWindow(*window);\r
+               IMG_Quit();\r
+               SDL_Quit();\r
+       }\r
+\r
+\r
+       SDL_Texture* loadTexture(const char* path, SDL_Renderer** renderer)     {\r
+               SDL_Texture* out;\r
+               SDL_Surface* tempSurf;\r
+\r
+               tempSurf = IMG_Load(path);\r
+\r
+               if (!tempSurf) {\r
+                       std::cout << "Failed to load " << path << std::endl;\r
+               }\r
+               out = SDL_CreateTextureFromSurface(*renderer, tempSurf);\r
+\r
+               SDL_FreeSurface(tempSurf);\r
+\r
+               return out;\r
+       }\r
+\r
+       void renderTexture(SDL_Renderer** renderer, SDL_Texture** texture, SDL_Rect* sRect, SDL_Rect* dRect) {\r
+               SDL_RenderCopy(*renderer, *texture, sRect, dRect);\r
+       }\r
+\r
+       Rect worldToScreenRect(Camera* cam, Vec2<float>* worldPos, float scale, int screenWidth, int screenHeight, int spriteWidth, int spriteHeight) {\r
+\r
+               float screenCenterX = (screenWidth / 2);\r
+               float screenCenterY = (screenHeight / 2);\r
+\r
+               float distX = (cam->x - worldPos->x) * spriteWidth * scale;\r
+               float distY = (cam->y - worldPos->y) * spriteHeight * scale;\r
+\r
+               int screenX = (int)(screenCenterX - distX);\r
+               int screenY = (int)(screenCenterY - distY);\r
+               int screenW = spriteWidth * scale;\r
+               int screenH = spriteHeight * scale;\r
+\r
+               return { screenX, screenY, screenW, screenH };\r
+       }\r
+\r
+       Vec2<int> worldToScreenPoint(Camera* cam, Vec2<float>* worldPos, float scale, int screenWidth, int screenHeight) {\r
+               float screenCenterX = (screenWidth / 2);\r
+               float screenCenterY = (screenHeight / 2);\r
+\r
+               float distX = (cam->x - worldPos->x) * scale;\r
+               float distY = (cam->y - worldPos->y) * scale;\r
+\r
+               int screenX = (int)(screenCenterX - distX);\r
+               int screenY = (int)(screenCenterY - distY);\r
+\r
+               return { screenX, screenY };\r
+       }\r
+\r
+\r
+       json loadJson(const char* path) {\r
+               // TODO: error handling\r
+               json jsonData;\r
+               std::fstream jsonFile(path);\r
+               jsonFile >> jsonData;\r
+\r
+               return jsonData;\r
+       }\r
+\r
+       void updateInputState(InputState* inputState) {\r
+               SDL_Event event;\r
+               while (SDL_PollEvent(&event))\r
+               {\r
+                       switch (event.type)\r
+                       {\r
+                       case SDL_QUIT: inputState->quit = true; break;\r
+                       case SDL_KEYDOWN:\r
+                               switch (event.key.keysym.sym)\r
+                               {\r
+                               case SDLK_UP: inputState->up = true; break;\r
+                               case SDLK_DOWN: inputState->down = true; break;\r
+                               case SDLK_LEFT: inputState->left = true; break;\r
+                               case SDLK_RIGHT: inputState->right = true; break;\r
+                               case SDLK_RETURN: inputState->rtrn = true; break;\r
+                               }\r
+                               break;\r
+                       case SDL_KEYUP:\r
+                               switch (event.key.keysym.sym)\r
+                               {\r
+                               case SDLK_UP: inputState->up = false; break;\r
+                               case SDLK_DOWN: inputState->down = false; break;\r
+                               case SDLK_LEFT: inputState->left = false; break;\r
+                               case SDLK_RIGHT: inputState->right = false; break;\r
+                               case SDLK_RETURN: inputState->rtrn = false; break;\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       void renderCircle(SDL_Renderer** renderer, Vec2<int> center, float radius, SDL_Color colour) {\r
+               const int segments = 20;\r
+               \r
+               SDL_Vertex verts[segments + 1];\r
+               int inds[(segments + 1) * 3];\r
+\r
+               verts[0] = { {(float)center.x, (float)center.y}, colour, {1, 1} };\r
+\r
+               for (int i = 1; i < segments + 1; ++i) {\r
+                       float theta = 2.0f * M_PI * (float)i / (float)segments;\r
+\r
+                       float x = radius * cosf(theta);\r
+                       float y = radius * sinf(theta);\r
+\r
+                       verts[i] = {{center.x + x, center.y + y}, colour, {1, 1}};\r
+               }\r
+\r
+               for (int i = 0; i < segments; ++i) {\r
+                       int indsIdx = i * 3;\r
+                       inds[indsIdx] = 0;\r
+                       inds[indsIdx+1] = i;\r
+                       inds[indsIdx+2] = i+1;\r
+               }\r
+\r
+               inds[segments * 3] = 0;\r
+               inds[segments * 3 + 1] = segments;\r
+               inds[segments * 3 + 2] = 1;\r
+\r
+               SDL_RenderGeometry(*renderer, NULL, verts, segments + 1, inds, (segments + 1) * 3);\r
+       }\r
+}\r
+\r
diff --git a/2DEngine.h b/2DEngine.h
new file mode 100644 (file)
index 0000000..c50a28e
--- /dev/null
@@ -0,0 +1,43 @@
+#pragma once\r
+\r
+#include <SDL.h>\r
+#include <SDL_image.h>\r
+#include <SDL_mixer.h>\r
+#include <nlohmann/json.hpp>\r
+\r
+namespace S2DE {\r
+    \r
+    using json = nlohmann::json;\r
+\r
+    template<typename T>\r
+    struct Vec2\r
+    {\r
+        T x, y;\r
+    };\r
+\r
+    // Rendering\r
+\r
+    typedef Vec2<float> Camera;\r
+\r
+    typedef SDL_Rect Rect;\r
+\r
+    __declspec(dllexport) bool initRendering(const char* windowTitle, int windowWidth, int windowHeight, SDL_Window** window, SDL_Renderer** renderer);\r
+    __declspec(dllexport) SDL_Texture* loadTexture(const char* path, SDL_Renderer** renderer);\r
+    __declspec(dllexport) void quitRendering(SDL_Renderer** renderer, SDL_Window** window);\r
+    __declspec(dllexport) void renderTexture(SDL_Renderer** renderer, SDL_Texture** texture, Rect* sRect, Rect* dRect);\r
+    __declspec(dllexport) Rect worldToScreenRect(Camera* cam, Vec2<float>* worldPos, float scale, int screenWidth, int screenHeight, int spriteWidth, int spriteHeight);\r
+    __declspec(dllexport) Vec2<int> worldToScreenPoint(Camera* cam, Vec2<float>* worldPos, float scale, int screenWidth, int screenHeight);\r
+    __declspec(dllexport) void renderCircle(SDL_Renderer** renderer, Vec2<int> center, float radius, SDL_Color colour);\r
+    // Json\r
+    __declspec(dllexport) json loadJson(const char* path);\r
+\r
+    // Input handling\r
+\r
+    struct InputState {\r
+        bool up, down, left, right;\r
+        bool quit;\r
+        bool rtrn;\r
+    };\r
+\r
+    __declspec(dllexport) void updateInputState(InputState* inputState);\r
+}\r
diff --git a/dllmain.cpp b/dllmain.cpp
new file mode 100644 (file)
index 0000000..123e51e
--- /dev/null
@@ -0,0 +1,2 @@
+// dllmain.cpp : Defines the entry point for the DLL application.\r
+#include "pch.h"
\ No newline at end of file
diff --git a/framework.h b/framework.h
new file mode 100644 (file)
index 0000000..a9744f8
--- /dev/null
@@ -0,0 +1,5 @@
+#pragma once\r
+\r
+#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers\r
+// Windows Header Files\r
+#include <windows.h>\r
diff --git a/pch.cpp b/pch.cpp
new file mode 100644 (file)
index 0000000..91c22df
--- /dev/null
+++ b/pch.cpp
@@ -0,0 +1,5 @@
+// pch.cpp: source file corresponding to the pre-compiled header\r
+\r
+#include "pch.h"\r
+\r
+// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.\r
diff --git a/pch.h b/pch.h
new file mode 100644 (file)
index 0000000..04ff4c2
--- /dev/null
+++ b/pch.h
@@ -0,0 +1,13 @@
+// pch.h: This is a precompiled header file.\r
+// Files listed below are compiled only once, improving build performance for future builds.\r
+// This also affects IntelliSense performance, including code completion and many code browsing features.\r
+// However, files listed here are ALL re-compiled if any one of them is updated between builds.\r
+// Do not add files here that you will be updating frequently as this negates the performance advantage.\r
+\r
+#ifndef PCH_H\r
+#define PCH_H\r
+\r
+// add headers that you want to pre-compile here\r
+#include "framework.h"\r
+\r
+#endif //PCH_H\r