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