--- /dev/null
+#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
--- /dev/null
+#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