From 9179c4aa0235365318633cc0426ca41a60b55a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Le=C5=9Bniak?= Date: Sun, 7 Nov 2021 10:53:45 +0100 Subject: [PATCH] Zadanie 5 WIP --- 2dgk_5/2dgk_5.sln | 31 ++ 2dgk_5/2dgk_5/2dgk_5.vcxproj | 165 +++++++ 2dgk_5/2dgk_5/2dgk_5.vcxproj.filters | 25 + 2dgk_5/2dgk_5/level.txt | 31 ++ 2dgk_5/2dgk_5/main.cpp | 709 +++++++++++++++++++++++++++ 2dgk_5/2dgk_5/textures/00.bmp | 3 + 2dgk_5/2dgk_5/textures/01.bmp | 3 + 2dgk_5/2dgk_5/textures/02.bmp | 3 + 2dgk_5/2dgk_5/textures/03.bmp | 3 + 2dgk_5/2dgk_5/textures/player.bmp | 3 + 2dgk_5/2dgk_5/textures/player2.bmp | 3 + 2dgk_5/2dgk_5/vcpkg.json | 8 + 12 files changed, 987 insertions(+) create mode 100644 2dgk_5/2dgk_5.sln create mode 100644 2dgk_5/2dgk_5/2dgk_5.vcxproj create mode 100644 2dgk_5/2dgk_5/2dgk_5.vcxproj.filters create mode 100644 2dgk_5/2dgk_5/level.txt create mode 100644 2dgk_5/2dgk_5/main.cpp create mode 100644 2dgk_5/2dgk_5/textures/00.bmp create mode 100644 2dgk_5/2dgk_5/textures/01.bmp create mode 100644 2dgk_5/2dgk_5/textures/02.bmp create mode 100644 2dgk_5/2dgk_5/textures/03.bmp create mode 100644 2dgk_5/2dgk_5/textures/player.bmp create mode 100644 2dgk_5/2dgk_5/textures/player2.bmp create mode 100644 2dgk_5/2dgk_5/vcpkg.json diff --git a/2dgk_5/2dgk_5.sln b/2dgk_5/2dgk_5.sln new file mode 100644 index 0000000..e3c76c9 --- /dev/null +++ b/2dgk_5/2dgk_5.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31729.503 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{48610DB6-1D7A-4667-B1C7-5A023FAEB4DE}") = "2dgk_5", "2dgk_5\2dgk_5.vcxproj", "{F0B2E58A-0401-4039-BAA7-19120983CB67}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F0B2E58A-0401-4039-BAA7-19120983CB67}.Debug|x64.ActiveCfg = Debug|x64 + {F0B2E58A-0401-4039-BAA7-19120983CB67}.Debug|x64.Build.0 = Debug|x64 + {F0B2E58A-0401-4039-BAA7-19120983CB67}.Debug|x86.ActiveCfg = Debug|Win32 + {F0B2E58A-0401-4039-BAA7-19120983CB67}.Debug|x86.Build.0 = Debug|Win32 + {F0B2E58A-0401-4039-BAA7-19120983CB67}.Release|x64.ActiveCfg = Release|x64 + {F0B2E58A-0401-4039-BAA7-19120983CB67}.Release|x64.Build.0 = Release|x64 + {F0B2E58A-0401-4039-BAA7-19120983CB67}.Release|x86.ActiveCfg = Release|Win32 + {F0B2E58A-0401-4039-BAA7-19120983CB67}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {958FDA17-FFCC-4AA3-932E-C8CF2B78EBD3} + EndGlobalSection +EndGlobal diff --git a/2dgk_5/2dgk_5/2dgk_5.vcxproj b/2dgk_5/2dgk_5/2dgk_5.vcxproj new file mode 100644 index 0000000..083a127 --- /dev/null +++ b/2dgk_5/2dgk_5/2dgk_5.vcxproj @@ -0,0 +1,165 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {f0b2e58a-0401-4039-baa7-19120983cb67} + My2dgk3 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + true + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x86-windows\x86-windows\include\SDL2 + + + Console + true + vcpkg_installed\x86-windows\x86-windows\debug\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2maind.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x86-windows\x86-windows\include\SDL2 + + + Console + true + true + true + vcpkg_installed\x86-windows\x86-windows\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2main.lib;%(AdditionalDependencies) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x64-windows\x64-windows\include\SDL2 + + + Console + true + vcpkg_installed\x64-windows\x64-windows\debug\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2maind.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x64-windows\x64-windows\include\SDL2 + + + Console + true + true + true + vcpkg_installed\x64-windows\x64-windows\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2main.lib;%(AdditionalDependencies) + + + + + + + + + + + + \ No newline at end of file diff --git a/2dgk_5/2dgk_5/2dgk_5.vcxproj.filters b/2dgk_5/2dgk_5/2dgk_5.vcxproj.filters new file mode 100644 index 0000000..a71858a --- /dev/null +++ b/2dgk_5/2dgk_5/2dgk_5.vcxproj.filters @@ -0,0 +1,25 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + + \ No newline at end of file diff --git a/2dgk_5/2dgk_5/level.txt b/2dgk_5/2dgk_5/level.txt new file mode 100644 index 0000000..41ca7c7 --- /dev/null +++ b/2dgk_5/2dgk_5/level.txt @@ -0,0 +1,31 @@ +/------------\/------------\ +|............||............| +|./--\./---\.||./---\./--\.| +|#| |.| |.||.| |.| |#| +|.`--'.`---'.`'.`---'.`--'.| +|..........................| +|./--\./\./------\./\./--\.| +|.`--'.||.`--\/--'.||.`--'.| +|......||....||....||......| +`----\.|`--\ || /--'|./----' + |.|/--' `' `--\|.| + |.|| ||.| + |.|| /--==--\ ||.| +-----'.`' | | `'.`----- + . | | . +-----\./\ | | /\./----- + |.|| `------' ||.| + |.|| ||.| + |.|| /------\ ||.| +/----'.`' `--\/--' `'.`----\ +|............||............| +|./--\./---\.||./---\./--\.| +|.`-\|.`---'.`'.`---'.|/-'.| +|#..||....... .......||..#| +`-\.||./\./------\./\.||./-' +/-'.`'.||.`--\/--'.||.`'.`-\ +|......||....||....||......| +|./----'`--\.||./--'`----\.| +|.`--------'.`'.`--------'.| +|..........................| +`--------------------------' \ No newline at end of file diff --git a/2dgk_5/2dgk_5/main.cpp b/2dgk_5/2dgk_5/main.cpp new file mode 100644 index 0000000..9787e32 --- /dev/null +++ b/2dgk_5/2dgk_5/main.cpp @@ -0,0 +1,709 @@ +//Using SDL and standard IO +#include "SDL.h" +#include +#include +#include +#include +#include + +template +constexpr const T& clamp(const T& x) +{ + return std::max(std::min(1.f, x), 0.f); +} + +template +constexpr const T& lerp(const T& a, const T& b, const float& t) +{ + return a + (b - a) * t; +} + +//Analog joystick dead zone +const int JOYSTICK_DEAD_ZONE = 8000; +//Screen dimension constants +const int WINDOW_DEAD_ZONE = 100; +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; +const int TILE_WIDTH = 32; +const int TILE_HEIGHT = 32; +const float SPEED = 0.16f; +const float SMOOTH = 0.4f; + +enum TILE_TYPE { + DEFAULT = 0, + HORIZONTAL_WALL = 1, + VERTICAL_WALL = 2, + DOT = 3 +}; + +class Vector2D { +public: + + Vector2D(float x, float y) : m_x(x), m_y(y) {} + float getX() { return m_x; } + float getY() { return m_y; } + + void setX(float x) { m_x = x; } + void setY(float y) { m_y = y; } + + float length() { return sqrt(m_x * m_x + m_y * m_y); } + + Vector2D operator+(const Vector2D& v2) const { + return Vector2D(m_x + v2.m_x, m_y + v2.m_y); + } + + friend Vector2D& operator+=(Vector2D& v1, const Vector2D& v2) { + v1.m_x += v2.m_x; + v1.m_y += v2.m_y; + return v1; + } + + Vector2D operator*(float scalar) { + return Vector2D(m_x * scalar, m_y * scalar); + } + + Vector2D& operator*=(float scalar) { + m_x *= scalar; + m_y *= scalar; + return *this; + } + + Vector2D operator-(const Vector2D& v2) const { + return Vector2D(m_x - v2.m_x, m_y - v2.m_y); + } + friend Vector2D& operator-=(Vector2D& v1, const Vector2D& v2) { + v1.m_x -= v2.m_x; + v1.m_y -= v2.m_y; + return v1; + } + + Vector2D operator/(float scalar) { + return Vector2D(m_x / scalar, m_y / scalar); + } + Vector2D& operator/=(float scalar) { + m_x /= scalar; + m_y /= scalar; + return *this; + } + + void normalize() { + float l = length(); + if (l > 0) { + (*this) *= 1 / l; + } + } + +private: + + float m_x; + float m_y; +}; + +class KTile { +public: + //Initializes position and type + KTile(int x, int y, TILE_TYPE tileType); + + //Shows the tile + void render(SDL_Rect& camera, float scale = 1.f); + + //Get the tile type + int getType(); + + SDL_Rect getBox(); + +private: + //The attributes of the tile + SDL_Rect mBox; + + //The tile type + TILE_TYPE mType; +}; + +int KTile::getType() +{ + return mType; +} + +SDL_Rect KTile::getBox() +{ + return mBox; +} + +//Texture wrapper class +class KTexture +{ +public: + //Initializes variables + KTexture(); + + //Deallocates memory + ~KTexture(); + + //Loads image at specified path + bool loadFromFile(std::string path); + + //Deallocates texture + void free(); + + //Renders texture at given point + void render(int x, int y, SDL_Rect* clip = NULL, float scale = 1.f); + + //Gets image dimensions + int getWidth(); + int getHeight(); + +private: + //The actual hardware texture + SDL_Texture* mTexture; + + //Image dimensions + int mWidth; + int mHeight; +}; + +SDL_Window* gWindow = NULL; + +SDL_Renderer* gRenderer = NULL; + +KTexture gTileTextures[4]; +KTexture gPlayerTexture; +KTexture gPlayerTexture2; +std::vector gTiles; + +//Game Controller 1 handler +SDL_Joystick* gGameController = NULL; + +int gMapHeight = -1; +int gMapWidth = -1; + +bool init(); + +bool loadMedia(); + +void close(); +SDL_Texture* loadTexture(std::string path); + +KTexture::KTexture() +{ + //Initialize + mTexture = NULL; + mWidth = 0; + mHeight = 0; +} + +KTexture::~KTexture() +{ + //Deallocate + free(); +} + +bool KTexture::loadFromFile(std::string path) +{ + //Get rid of preexisting texture + free(); + //The final texture + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == NULL) + { + printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Color key image + SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0xFF, 0, 0)); + //Create texture from surface pixels + newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface); + if (newTexture == NULL) + { + printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Get image dimensions + mWidth = loadedSurface->w; + mHeight = loadedSurface->h; + } + + //Get rid of old loaded surface + SDL_FreeSurface(loadedSurface); + } + + //Return success + mTexture = newTexture; + return mTexture != NULL; +} + +void KTexture::free() +{ + //Free texture if it exists + if (mTexture != NULL) + { + SDL_DestroyTexture(mTexture); + mTexture = NULL; + mWidth = 0; + mHeight = 0; + } +} + +void KTexture::render(int x, int y, SDL_Rect* clip, float scale) +{ + //Set rendering space and render to screen + SDL_Rect renderQuad = { x * scale, y * scale, mWidth * scale, mHeight * scale }; + + //Set clip rendering dimensions + if (clip != NULL) + { + renderQuad.w = clip->w; + renderQuad.h = clip->h; + } + + //Render to screen + SDL_RenderCopy(gRenderer, mTexture, clip, &renderQuad); +} + +int KTexture::getWidth() +{ + return mWidth; +} + +int KTexture::getHeight() +{ + return mHeight; +} + +bool init() +{ + //Initialization flag + bool success = true; + + //Initialize SDL + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) + { + printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Set texture filtering to linear + if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) + { + printf("Warning: Linear texture filtering not enabled!"); + } + + //Check for joysticks + if (SDL_NumJoysticks() < 1) + { + printf("Warning: No joysticks connected!\n"); + } + else + { + //Load joystick + gGameController = SDL_JoystickOpen(0); + if (gGameController == NULL) + { + printf("Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError()); + } + } + + //Create window + gWindow = SDL_CreateWindow("2DGK - Zadanie 5", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (gWindow == NULL) + { + printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Create renderer for window + gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); + if (gRenderer == NULL) + { + printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Initialize renderer color + SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF); + } + } + } + + return success; +} + +bool loadMedia() +{ + //Loading success flag + bool success = true; + + if (!gPlayerTexture.loadFromFile("textures/player.bmp")) + { + printf("Failed to load player texture image!\n"); + success = false; + } + if (!gPlayerTexture2.loadFromFile("textures/player2.bmp")) + { + printf("Failed to load player texture image!\n"); + success = false; + } + + for (int i = TILE_TYPE::DEFAULT; i <= TILE_TYPE::DOT; ++i) + { + if (!gTileTextures[i].loadFromFile("textures/0" + std::to_string(i) + ".bmp")) + { + printf("Failed to load 0%d texture image!\n", i); + success = false; + } + } + + gTiles.clear(); + std::ifstream levelFile; + levelFile.open("level.txt"); + if (levelFile.fail()) + { + printf("Failed to load level.txt!\n"); + success = false; + } + else + { + int y = 0; + std::string line; + while (std::getline(levelFile, line)) { + if (gMapWidth < (int)(line.length() * TILE_WIDTH)) + gMapWidth = line.length() * TILE_WIDTH; + for (int i = 0; i < line.length(); ++i) { + TILE_TYPE type = TILE_TYPE::DEFAULT; + switch (line[i]) + { + case '|': + type = TILE_TYPE::VERTICAL_WALL; + break; + case '-': + type = TILE_TYPE::HORIZONTAL_WALL; + break; + case '.': + type = TILE_TYPE::DOT; + break; + } + gTiles.push_back(KTile(i * TILE_WIDTH, y * TILE_HEIGHT, type)); + } + ++y; + } + gMapHeight = y * TILE_HEIGHT; + levelFile.close(); + } + + return success; +} + +SDL_Texture* loadTexture(std::string path) +{ + //The final optimized image + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == NULL) + { + printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Create texture from surface pixels + newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface); + if (newTexture == NULL) + { + printf("Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + + //Get rid of old loaded surface + SDL_FreeSurface(loadedSurface); + } + + return newTexture; +} + +void close() +{ + //Free loaded images + for (int i = 0; i <= TILE_TYPE::DOT; ++i) + gTileTextures[i].free(); + + gPlayerTexture.free(); + gPlayerTexture2.free(); + + //Close game controller + SDL_JoystickClose(gGameController); + gGameController = NULL; + + //Destroy window + SDL_DestroyRenderer(gRenderer); + SDL_DestroyWindow(gWindow); + gWindow = NULL; + gRenderer = NULL; + + //Quit SDL subsystems + SDL_Quit(); +} + +int main(int argc, char* args[]) +{ + //Start up SDL and create window + if (!init()) + { + printf("Failed to initialize!\n"); + } + else + { + //Load media + if (!loadMedia()) + { + printf("Failed to load media!\n"); + } + else + { + //Main loop flag + bool quit = false; + + //Event handler + SDL_Event e; + + SDL_Rect viewport; + viewport.x = SCREEN_WIDTH / 2; + viewport.y = SCREEN_HEIGHT / 2; + viewport.w = SCREEN_WIDTH; + viewport.h = SCREEN_HEIGHT; + + Vector2D playerOnePosition(0, 0); + Vector2D playerOneInput(0, 0); + Vector2D playerOneVelocity(0, 0); + + Vector2D playerTwoPosition(0, 0); + Vector2D playerTwoInput(0, 0); + Vector2D playerTwoVelocity(0, 0); + + //Normalized direction + int xDir = 0; + int yDir = 0; + + uint32_t time, previous_time; + time = previous_time = SDL_GetTicks(); + printf("\n"); + + //While application is running + while (!quit) + { + previous_time = time; + time = SDL_GetTicks(); + playerOneInput.setX(0); + playerOneInput.setY(0); + + //Handle events on queue + while (SDL_PollEvent(&e) != 0) + { + //User requests quit + if (e.type == SDL_QUIT) + { + quit = true; + } + else if (e.type == SDL_JOYAXISMOTION) + { + //Motion on controller 0 + if (e.jaxis.which == 0) + { + //X axis motion + if (e.jaxis.axis == 0) + { + //Left of dead zone + if (e.jaxis.value < -JOYSTICK_DEAD_ZONE) + { + playerTwoInput.setX(e.jaxis.value); + } + //Right of dead zone + else if (e.jaxis.value > JOYSTICK_DEAD_ZONE) + { + playerTwoInput.setX(e.jaxis.value); + } + else + { + playerTwoInput.setX(0); + } + } + //Y axis motion + else if (e.jaxis.axis == 1) + { + //Below of dead zone + if (e.jaxis.value < -JOYSTICK_DEAD_ZONE) + { + playerTwoInput.setY(e.jaxis.value); + } + //Above of dead zone + else if (e.jaxis.value > JOYSTICK_DEAD_ZONE) + { + playerTwoInput.setY(e.jaxis.value); + } + else + { + playerTwoInput.setY(0); + } + } + } + } + + } + + const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL); + if (currentKeyStates[SDL_SCANCODE_W]) + { + playerOneInput.setY(-1); + } + if (currentKeyStates[SDL_SCANCODE_S]) + { + playerOneInput.setY(1); + } + if (currentKeyStates[SDL_SCANCODE_A]) + { + playerOneInput.setX(-1); + } + if (currentKeyStates[SDL_SCANCODE_D]) + { + playerOneInput.setX(1); + } + + playerOneInput.normalize(); + playerOneVelocity = playerOneInput * SPEED * (1 - SMOOTH) + playerOneVelocity * SMOOTH; + + playerOnePosition += playerOneVelocity; + + bool zoom = true; + if (currentKeyStates[SDL_SCANCODE_F]) + { + zoom = true; + } + + if (!zoom) { + // keep player one near player two + if (playerOnePosition.getX() < playerTwoPosition.getX() - SCREEN_WIDTH + WINDOW_DEAD_ZONE) playerOnePosition.setX(playerTwoPosition.getX() - SCREEN_WIDTH + WINDOW_DEAD_ZONE); + if (playerOnePosition.getX() > playerTwoPosition.getX() + SCREEN_WIDTH - WINDOW_DEAD_ZONE) playerOnePosition.setX(playerTwoPosition.getX() + SCREEN_WIDTH - WINDOW_DEAD_ZONE); + if (playerOnePosition.getY() < playerTwoPosition.getY() - SCREEN_HEIGHT + WINDOW_DEAD_ZONE) playerOnePosition.setY(playerTwoPosition.getY() - SCREEN_HEIGHT + WINDOW_DEAD_ZONE); + if (playerOnePosition.getY() > playerTwoPosition.getY() + SCREEN_HEIGHT - WINDOW_DEAD_ZONE) playerOnePosition.setY(playerTwoPosition.getY() + SCREEN_HEIGHT - WINDOW_DEAD_ZONE); + } + // keep player one in map + if (playerOnePosition.getX() < 0) playerOnePosition.setX(0); + if (playerOnePosition.getX() > gMapWidth - gPlayerTexture.getWidth()) playerOnePosition.setX(gMapWidth - gPlayerTexture.getWidth()); + if (playerOnePosition.getY() < 0) playerOnePosition.setY(0); + if (playerOnePosition.getY() > gMapHeight - gPlayerTexture.getHeight()) playerOnePosition.setY(gMapHeight - gPlayerTexture.getHeight()); + + playerTwoInput.normalize(); + //printf("\r \rX:%f, Y:%f", playerTwoInput.getX(), playerTwoInput.getY()); + playerTwoVelocity = playerTwoInput * SPEED * (1 - SMOOTH) + playerTwoVelocity * SMOOTH; + + playerTwoPosition += playerTwoVelocity; + + if (!zoom) { + if (playerTwoPosition.getX() < playerOnePosition.getX() - SCREEN_WIDTH + WINDOW_DEAD_ZONE) playerTwoPosition.setX(playerOnePosition.getX() - SCREEN_WIDTH + WINDOW_DEAD_ZONE); + if (playerTwoPosition.getX() > playerOnePosition.getX() + SCREEN_WIDTH - WINDOW_DEAD_ZONE) playerTwoPosition.setX(playerOnePosition.getX() + SCREEN_WIDTH - WINDOW_DEAD_ZONE); + if (playerTwoPosition.getY() < playerOnePosition.getY() - SCREEN_HEIGHT + WINDOW_DEAD_ZONE) playerTwoPosition.setY(playerOnePosition.getY() - SCREEN_HEIGHT + WINDOW_DEAD_ZONE); + if (playerTwoPosition.getY() > playerOnePosition.getY() + SCREEN_HEIGHT - WINDOW_DEAD_ZONE) playerTwoPosition.setY(playerOnePosition.getY() + SCREEN_HEIGHT - WINDOW_DEAD_ZONE); + } + + if (playerTwoPosition.getX() < 0) playerTwoPosition.setX(0); + if (playerTwoPosition.getX() > gMapWidth - gPlayerTexture.getWidth()) playerTwoPosition.setX(gMapWidth - gPlayerTexture.getWidth()); + if (playerTwoPosition.getY() < 0) playerTwoPosition.setY(0); + if (playerTwoPosition.getY() > gMapHeight - gPlayerTexture.getHeight()) playerTwoPosition.setY(gMapHeight - gPlayerTexture.getHeight()); + + + float scale = 1.f; + if (zoom) + { + float xDist = (std::abs(playerTwoPosition.getX() - playerOnePosition.getX()) - (SCREEN_WIDTH - WINDOW_DEAD_ZONE)); + float yDist = (std::abs(playerTwoPosition.getY() - playerOnePosition.getY()) - (SCREEN_HEIGHT - WINDOW_DEAD_ZONE)); + float xMaxDist = (gMapWidth - gPlayerTexture.getWidth() - (SCREEN_WIDTH - WINDOW_DEAD_ZONE)); + float t1 = xDist / xMaxDist; + float t2 = yDist / (gMapHeight - gPlayerTexture.getHeight() - (SCREEN_HEIGHT - WINDOW_DEAD_ZONE)); + scale = lerp(scale, std::max(SCREEN_WIDTH * 1.0f / gMapWidth, SCREEN_HEIGHT * 1.0f / gMapHeight), + std::max( + std::max(t1, 0.f), + std::max(t2, 0.f) + )); + printf("\rscale: %f", scale); + } + if (currentKeyStates[SDL_SCANCODE_E]) + { + scale = std::max(SCREEN_WIDTH * 1.0f / gMapWidth, SCREEN_HEIGHT * 1.0f / gMapHeight); + } + Vector2D focusPoint = (playerOnePosition + playerTwoPosition + Vector2D(gPlayerTexture.getWidth(), gPlayerTexture.getHeight())) * scale / 2; + if (currentKeyStates[SDL_SCANCODE_Q]) + { + focusPoint = Vector2D(gMapWidth / 2, gMapHeight / 2); + } + + //viewport.x += clamp((time - previous_time) * .05f) * (focusPoint.getX() - SCREEN_WIDTH / 2 - viewport.x); + viewport.x = lerp(viewport.x, int(focusPoint.getX() - SCREEN_WIDTH / 2), (time - previous_time) * .05f); + if ((focusPoint.getX() - SCREEN_WIDTH / 2) - viewport.x <= 0.005f) + { + viewport.x = (focusPoint.getX() - SCREEN_WIDTH / 2); + } + viewport.y += clamp((time - previous_time) * .05f) * (focusPoint.getY() - SCREEN_HEIGHT / 2 - viewport.y); + if ((focusPoint.getY() - SCREEN_HEIGHT / 2) - viewport.y <= 0.005f) + { + viewport.y = (focusPoint.getY() - SCREEN_HEIGHT / 2); + + } + if (viewport.x < 0) + viewport.x = 0; + if (viewport.y < 0) + viewport.y = 0; + if (viewport.x > (gMapWidth - viewport.w) * scale) + viewport.x = (gMapWidth - viewport.w) * scale; + if (viewport.y > (gMapHeight - viewport.h) * scale) + viewport.y = (gMapHeight - viewport.h) * scale; + + + //Clear screen + SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF); + SDL_RenderClear(gRenderer); + + + + for (auto& tile : gTiles) + tile.render(viewport, scale); + + gPlayerTexture.render(playerOnePosition.getX() - viewport.x, playerOnePosition.getY() - viewport.y, NULL, scale); + gPlayerTexture2.render(playerTwoPosition.getX() - viewport.x, playerTwoPosition.getY() - viewport.y, NULL, scale); + + //Update screen + SDL_RenderPresent(gRenderer); + } + } + } + + //Free resources and close SDL + close(); + + return 0; +} + +KTile::KTile(int x, int y, TILE_TYPE tileType) +{ + //Get the offsets + mBox.x = x; + mBox.y = y; + + mBox.w = TILE_WIDTH; + mBox.h = TILE_HEIGHT; + + //Get the tile type + mType = tileType; + +} + +void KTile::render(SDL_Rect& camera, float scale) +{ + gTileTextures[mType].render(mBox.x - camera.x, mBox.y - camera.y, NULL, scale); +} \ No newline at end of file diff --git a/2dgk_5/2dgk_5/textures/00.bmp b/2dgk_5/2dgk_5/textures/00.bmp new file mode 100644 index 0000000..34f892e --- /dev/null +++ b/2dgk_5/2dgk_5/textures/00.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f46f203f44733ef973f3231b899951e6ab581739a57f3a3bab008ae5fe379711 +size 3126 diff --git a/2dgk_5/2dgk_5/textures/01.bmp b/2dgk_5/2dgk_5/textures/01.bmp new file mode 100644 index 0000000..563e1e1 --- /dev/null +++ b/2dgk_5/2dgk_5/textures/01.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:77efffc116983d8bc47f1866823f050823e035590f8bea75a050de01e052f0d5 +size 3126 diff --git a/2dgk_5/2dgk_5/textures/02.bmp b/2dgk_5/2dgk_5/textures/02.bmp new file mode 100644 index 0000000..87bdec3 --- /dev/null +++ b/2dgk_5/2dgk_5/textures/02.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:744ed424d4794efdabecdfc0b8618d53de57f20b7229b12a0d1db40e1ecd9b30 +size 3126 diff --git a/2dgk_5/2dgk_5/textures/03.bmp b/2dgk_5/2dgk_5/textures/03.bmp new file mode 100644 index 0000000..adf47f9 --- /dev/null +++ b/2dgk_5/2dgk_5/textures/03.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5ca36925a5793836bd24dbf79fbaa05f153d542cd11d2237bf69d51cea0ee13d +size 3126 diff --git a/2dgk_5/2dgk_5/textures/player.bmp b/2dgk_5/2dgk_5/textures/player.bmp new file mode 100644 index 0000000..ab193d5 --- /dev/null +++ b/2dgk_5/2dgk_5/textures/player.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:71ab1fbb649b03c37d1aa219be3d1221ee6a4e6dd7c141bccf29a862de1cdd9a +size 1878 diff --git a/2dgk_5/2dgk_5/textures/player2.bmp b/2dgk_5/2dgk_5/textures/player2.bmp new file mode 100644 index 0000000..d2f36d6 --- /dev/null +++ b/2dgk_5/2dgk_5/textures/player2.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:27e386557e6a6f069961b3abac29452491c283ec782bac480d4170b5b1ca6682 +size 1878 diff --git a/2dgk_5/2dgk_5/vcpkg.json b/2dgk_5/2dgk_5/vcpkg.json new file mode 100644 index 0000000..3017dca --- /dev/null +++ b/2dgk_5/2dgk_5/vcpkg.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json", + "name": "dgk-5", + "version": "0.1.0", + "dependencies": [ + "sdl2" + ] +} \ No newline at end of file