From 752e593b55da5471d9de4e5db043f251fcec7c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Le=C5=9Bniak?= Date: Tue, 9 Nov 2021 13:26:24 +0100 Subject: [PATCH] Zadanie (5-6) 1 completed --- 2dgk_5/2dgk_5/2dgk_5.vcxproj | 16 +- 2dgk_5/2dgk_5/2dgk_5.vcxproj.filters | 40 +- 2dgk_5/2dgk_5/Constants.h | 17 + 2dgk_5/2dgk_5/GamePad.h | 8 + 2dgk_5/2dgk_5/KGame.cpp | 359 ++++++++++++++ 2dgk_5/2dgk_5/KGame.h | 42 ++ 2dgk_5/2dgk_5/KInput.cpp | 153 ++++++ 2dgk_5/2dgk_5/KInput.h | 38 ++ 2dgk_5/2dgk_5/KTexture.cpp | 121 +++++ 2dgk_5/2dgk_5/KTexture.h | 43 ++ 2dgk_5/2dgk_5/KTile.cpp | 33 ++ 2dgk_5/2dgk_5/KTile.h | 36 ++ 2dgk_5/2dgk_5/KVector2d.h | 61 +++ 2dgk_5/2dgk_5/Utils.h | 30 ++ 2dgk_5/2dgk_5/main.cpp | 707 +-------------------------- 2dgk_5/readme.txt | 7 + 16 files changed, 1005 insertions(+), 706 deletions(-) create mode 100644 2dgk_5/2dgk_5/Constants.h create mode 100644 2dgk_5/2dgk_5/GamePad.h create mode 100644 2dgk_5/2dgk_5/KGame.cpp create mode 100644 2dgk_5/2dgk_5/KGame.h create mode 100644 2dgk_5/2dgk_5/KInput.cpp create mode 100644 2dgk_5/2dgk_5/KInput.h create mode 100644 2dgk_5/2dgk_5/KTexture.cpp create mode 100644 2dgk_5/2dgk_5/KTexture.h create mode 100644 2dgk_5/2dgk_5/KTile.cpp create mode 100644 2dgk_5/2dgk_5/KTile.h create mode 100644 2dgk_5/2dgk_5/KVector2d.h create mode 100644 2dgk_5/2dgk_5/Utils.h create mode 100644 2dgk_5/readme.txt diff --git a/2dgk_5/2dgk_5/2dgk_5.vcxproj b/2dgk_5/2dgk_5/2dgk_5.vcxproj index 083a127..bced92b 100644 --- a/2dgk_5/2dgk_5/2dgk_5.vcxproj +++ b/2dgk_5/2dgk_5/2dgk_5.vcxproj @@ -154,11 +154,25 @@ - + + + + + + + + + + + + + + + diff --git a/2dgk_5/2dgk_5/2dgk_5.vcxproj.filters b/2dgk_5/2dgk_5/2dgk_5.vcxproj.filters index a71858a..e06774f 100644 --- a/2dgk_5/2dgk_5/2dgk_5.vcxproj.filters +++ b/2dgk_5/2dgk_5/2dgk_5.vcxproj.filters @@ -15,11 +15,49 @@ - + + Source Files + + + Source Files + + + Source Files + + + Source Files + + Source Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + \ No newline at end of file diff --git a/2dgk_5/2dgk_5/Constants.h b/2dgk_5/2dgk_5/Constants.h new file mode 100644 index 0000000..e23a090 --- /dev/null +++ b/2dgk_5/2dgk_5/Constants.h @@ -0,0 +1,17 @@ +#pragma once +namespace KapitanGame { + namespace Constants { + constexpr const char* WINDOW_TITLE = "2DGK - Zadanie 5"; + //Analog joystick dead zone + constexpr int JOYSTICK_DEAD_ZONE = 8000; + //Screen dimension constants + constexpr int WINDOW_DEAD_ZONE = 100; + constexpr int SCREEN_WIDTH = 640; + constexpr int SCREEN_HEIGHT = 480; + constexpr float SCREEN_RATIO = static_cast(SCREEN_WIDTH) / SCREEN_HEIGHT; + constexpr int TILE_WIDTH = 32; + constexpr int TILE_HEIGHT = 32; + constexpr float SPEED = 0.16f; + constexpr float SMOOTH = 0.4f; + } +} diff --git a/2dgk_5/2dgk_5/GamePad.h b/2dgk_5/2dgk_5/GamePad.h new file mode 100644 index 0000000..4d8990f --- /dev/null +++ b/2dgk_5/2dgk_5/GamePad.h @@ -0,0 +1,8 @@ +#pragma once +#include +namespace KapitanGame { + struct GamePad { + bool Buttons[SDL_CONTROLLER_BUTTON_MAX]; + int Axis[SDL_CONTROLLER_AXIS_MAX]; + }; +} diff --git a/2dgk_5/2dgk_5/KGame.cpp b/2dgk_5/2dgk_5/KGame.cpp new file mode 100644 index 0000000..8aec477 --- /dev/null +++ b/2dgk_5/2dgk_5/KGame.cpp @@ -0,0 +1,359 @@ +#include "KGame.h" +#include "SDL.h" +#include +#include +#include +#include +#include + +#include "Utils.h" +#include "Constants.h" +#include "KTexture.h" +#include "KTile.h" +#include "KVector2d.h" + +namespace KapitanGame { + KGame::KGame() { + //Initialize SDL + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + throw std::runtime_error(Utils::StringFormat("SDL could not initialize! SDL_Error: %s", SDL_GetError())); + } + //Set texture filtering to linear + if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) + { + printf("Warning: Linear texture filtering not enabled!\n"); + } + + Input.Init(); + + //Create window + Window = SDL_CreateWindow(Constants::WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Constants::SCREEN_WIDTH, Constants::SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (Window == nullptr) + { + throw std::runtime_error(Utils::StringFormat("Window could not be created! SDL_Error: %s", SDL_GetError())); + } + //Create renderer for window + Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED); + if (Renderer == nullptr) + { + throw std::runtime_error(Utils::StringFormat("Renderer could not be created! SDL Error: %s", SDL_GetError())); + } + //Initialize renderer color + SDL_SetRenderDrawColor(Renderer, 0, 0xFF, 0, 0xFF); + } + + KGame::~KGame() { + //Free loaded images + for (int i = 0; i <= static_cast(TileType::Dot); ++i) + TileTextures[i].Free(); + + PlayerTexture.Free(); + PlayerTexture2.Free(); + + Input.Free(); + + //Destroy window + SDL_DestroyRenderer(Renderer); + SDL_DestroyWindow(Window); + Window = nullptr; + Renderer = nullptr; + + //Quit SDL subsystems + SDL_Quit(); + } + + bool KGame::LoadMedia() + { + //Loading success flag + bool success = true; + + if (!PlayerTexture.LoadFromFile("textures/player.bmp", Renderer)) + { + printf("Failed to load player texture image!\n"); + success = false; + } + if (!PlayerTexture2.LoadFromFile("textures/player2.bmp", Renderer)) + { + printf("Failed to load player texture image!\n"); + success = false; + } + + for (int i = static_cast(TileType::Default); i <= static_cast(TileType::Dot); ++i) + { + if (!TileTextures[i].LoadFromFile("textures/0" + std::to_string(i) + ".bmp", Renderer)) + { + printf("Failed to load 0%d texture image!\n", i); + success = false; + } + } + + Tiles.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 (MapWidth < static_cast(line.length() * Constants::TILE_WIDTH)) + MapWidth = static_cast(line.length()) * Constants::TILE_WIDTH; + for (auto i = 0ull; i < line.length(); ++i) { + auto type = TileType::Default; + switch (line[i]) + { + case '|': + type = TileType::VerticalWall; + break; + case '-': + type = TileType::HorizontalWall; + break; + case '.': + type = TileType::Dot; + break; + default:; + } + Tiles.emplace_back(i * Constants::TILE_WIDTH, y * Constants::TILE_HEIGHT, type); + } + ++y; + } + MapHeight = y * Constants::TILE_HEIGHT; + levelFile.close(); + } + + return success; + } + + SDL_Texture* KGame::LoadTexture(const std::string& path) const { + //The final optimized image + SDL_Texture* newTexture = nullptr; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == nullptr) + { + printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Create texture from surface pixels + newTexture = SDL_CreateTextureFromSurface(Renderer, loadedSurface); + if (newTexture == nullptr) + { + 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; + } + + int KGame::Run(int argc, char* args[]) + { + //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 = Constants::SCREEN_WIDTH / 2; + viewport.y = Constants::SCREEN_HEIGHT / 2; + viewport.w = Constants::SCREEN_WIDTH; + viewport.h = Constants::SCREEN_HEIGHT; + + KVector2D playerOnePosition(0, 0); + KVector2D playerOneInput(0, 0); + KVector2D playerOneVelocity(0, 0); + + KVector2D playerTwoPosition(0, 0); + KVector2D playerTwoInput(0, 0); + KVector2D playerTwoVelocity(0, 0); + + SDL_Rect playerAreaModeGauntlet; + playerAreaModeGauntlet.x = 0; + playerAreaModeGauntlet.y = 0; + playerAreaModeGauntlet.w = Constants::SCREEN_WIDTH - Constants::WINDOW_DEAD_ZONE; + playerAreaModeGauntlet.h = Constants::SCREEN_HEIGHT - Constants::WINDOW_DEAD_ZONE; + + + SDL_Rect playerAreaModeZoom; + playerAreaModeZoom.x = 0; + playerAreaModeZoom.y = 0; + playerAreaModeZoom.w = (MapWidth <= MapHeight ? MapWidth : static_cast(static_cast(MapHeight) * Constants::SCREEN_RATIO)) - Constants::WINDOW_DEAD_ZONE; + playerAreaModeZoom.h = (MapHeight <= MapWidth ? MapHeight : static_cast(static_cast(MapWidth) / Constants::SCREEN_RATIO)) - Constants::WINDOW_DEAD_ZONE; + + float scale = 1.f; + + //Normalized direction + int xDir = 0; + int yDir = 0; + + uint32_t time, previousTime; + time = previousTime = SDL_GetTicks(); + printf("\n"); + bool zoom = false; + + //While application is running + while (!quit) + { + previousTime = time; + time = SDL_GetTicks(); + playerOneInput.X = 0; + playerOneInput.Y = 0; + + Input.HandleInputPreEvents(); + + //Handle events on queue + while (SDL_PollEvent(&e)) + { + //User requests quit + if (e.type == SDL_QUIT) + { + quit = true; + continue; + } + Input.HandleEvent(e); + } + Input.HandleInputPostEvents(); + + playerTwoInput.X = Input.GetControllerAxis(Controllers::Controller1, SDL_CONTROLLER_AXIS_LEFTX); + playerTwoInput.Y = Input.GetControllerAxis(Controllers::Controller1, SDL_CONTROLLER_AXIS_LEFTY); + + if (Input.IsKeyboardButtonHeld(SDL_SCANCODE_W)) + { + playerOneInput.Y = -1; + } + if (Input.IsKeyboardButtonHeld(SDL_SCANCODE_S)) + { + playerOneInput.Y = 1; + } + if (Input.IsKeyboardButtonHeld(SDL_SCANCODE_A)) + { + playerOneInput.X = -1; + } + if (Input.IsKeyboardButtonHeld(SDL_SCANCODE_D)) + { + playerOneInput.X = 1; + } + + playerOneInput.Normalize(); + playerOneVelocity = playerOneInput * Constants::SPEED * (1 - Constants::SMOOTH) + playerOneVelocity * Constants::SMOOTH; + + playerOnePosition += playerOneVelocity; + + if (Input.IsKeyboardButtonPressed(SDL_SCANCODE_F)) + { + zoom = !zoom; + } + + const auto& playerArea = zoom ? playerAreaModeZoom : playerAreaModeGauntlet; + + + // keep player one near player two + if (playerOnePosition.X < playerTwoPosition.X - playerArea.w) playerOnePosition.X = playerTwoPosition.X - playerArea.w; + if (playerOnePosition.X > playerTwoPosition.X + playerArea.w) playerOnePosition.X = playerTwoPosition.X + playerArea.w; + if (playerOnePosition.Y < playerTwoPosition.Y - playerArea.h) playerOnePosition.Y = playerTwoPosition.Y - playerArea.h; + if (playerOnePosition.Y > playerTwoPosition.Y + playerArea.h) playerOnePosition.Y = playerTwoPosition.Y + playerArea.h; + + // keep player one in map + if (playerOnePosition.X < 0) playerOnePosition.X = 0; + if (playerOnePosition.X > MapWidth - PlayerTexture.GetWidth()) playerOnePosition.X = (MapWidth - PlayerTexture.GetWidth()); + if (playerOnePosition.Y < 0) playerOnePosition.Y = (0); + if (playerOnePosition.Y > MapHeight - PlayerTexture.GetHeight()) playerOnePosition.Y = (MapHeight - PlayerTexture.GetHeight()); + + //playerTwoInput.Normalize(); + //printf("\r \rX:%f, Y:%f", playerTwoInput.x, playerTwoInput.y); + playerTwoVelocity = playerTwoInput * Constants::SPEED * (1 - Constants::SMOOTH) + playerTwoVelocity * Constants::SMOOTH; + + playerTwoPosition += playerTwoVelocity; + + + if (playerTwoPosition.X < playerOnePosition.X - static_cast(playerArea.w)) playerTwoPosition.X = playerOnePosition.X - playerArea.w; + if (playerTwoPosition.X > playerOnePosition.X + playerArea.w) playerTwoPosition.X = playerOnePosition.X + playerArea.w; + if (playerTwoPosition.Y < playerOnePosition.Y - playerArea.h) playerTwoPosition.Y = playerOnePosition.Y - playerArea.h; + if (playerTwoPosition.Y > playerOnePosition.Y + playerArea.h) playerTwoPosition.Y = playerOnePosition.Y + playerArea.h; + + if (playerTwoPosition.X < 0) playerTwoPosition.X = 0; + if (playerTwoPosition.X > MapWidth - PlayerTexture.GetWidth()) playerTwoPosition.X = MapWidth - PlayerTexture.GetWidth(); + if (playerTwoPosition.Y < 0) playerTwoPosition.Y = 0; + if (playerTwoPosition.Y > MapHeight - PlayerTexture.GetHeight()) playerTwoPosition.Y = MapHeight - PlayerTexture.GetHeight(); + + + scale = 1.f; + if (zoom) + { + float xDist = std::abs(playerTwoPosition.X - playerOnePosition.X) - playerAreaModeGauntlet.w; + float yDist = std::abs(playerTwoPosition.Y - playerOnePosition.Y) - playerAreaModeGauntlet.h; + float xMaxDist = playerAreaModeZoom.w - playerAreaModeGauntlet.w; + float yMaxDist = playerAreaModeZoom.h - playerAreaModeGauntlet.h; + float t1 = Utils::Clamp(xDist / xMaxDist); + float t2 = Utils::Clamp(yDist / yMaxDist); + float t = std::max(std::max(t1, 0.f), std::max(t2, 0.f)); + scale = Utils::Lerp(scale, std::max(Constants::SCREEN_WIDTH * 1.0f / MapWidth, Constants::SCREEN_HEIGHT * 1.0f / MapHeight), t); + //printf("\rscale: %f, t1: %f, t2: %f, t: %f", scale, t1, t2, t); + } + + KVector2D focusPoint = (playerOnePosition + playerTwoPosition + KVector2D(PlayerTexture.GetWidth(), PlayerTexture.GetHeight())+KVector2D(Constants::WINDOW_DEAD_ZONE, Constants::WINDOW_DEAD_ZONE)/2)*scale/2; + if (Input.IsKeyboardButtonHeld(SDL_SCANCODE_Q)) + { + focusPoint = KVector2D(MapWidth / 2.f, MapHeight / 2.f); + } + + //viewport.x += clamp((time - previous_time) * .05f) * (focusPoint.x - SCREEN_WIDTH / 2 - viewport.x); + viewport.x = Utils::Lerp(viewport.x, static_cast(focusPoint.X - Constants::SCREEN_WIDTH / 2.f), static_cast(time - previousTime) * .05f); + if ((focusPoint.X - Constants::SCREEN_WIDTH / 2.f) - static_cast(viewport.x) <= 0.005f) + { + viewport.x = static_cast(focusPoint.X - Constants::SCREEN_WIDTH / 2.f)/scale; + } + viewport.y = Utils::Lerp(viewport.y, static_cast(focusPoint.Y - Constants::SCREEN_HEIGHT / 2.f), static_cast(time - previousTime) * .05f); + if (focusPoint.Y - static_cast(Constants::SCREEN_HEIGHT) / 2.0f - static_cast(viewport.y) <= 0.005f) + { + viewport.y = static_cast(focusPoint.Y - Constants::SCREEN_HEIGHT / 2.f) / scale; + } + if (viewport.x < 0) + viewport.x = 0; + if (viewport.y < 0) + viewport.y = 0; + if (viewport.x > MapWidth - static_cast(static_cast(viewport.w) / scale)) + viewport.x = MapWidth - static_cast(static_cast(viewport.w) / scale); + if (viewport.y > MapHeight - static_cast(static_cast(viewport.h) / scale)) + viewport.y = MapHeight - static_cast(static_cast(viewport.h) / scale); + + //printf("\rp1(%f, %f), p2(%f, %f), viewport:(%d, %d, %d, %d), map(%d, %d), focus(%f, %f)", playerOnePosition.X * scale, playerOnePosition.Y * scale, playerTwoPosition.X * scale, playerTwoPosition.Y * scale, static_cast(viewport.x * scale), static_cast(viewport.y * scale), static_cast(static_cast(viewport.w) / scale), static_cast(static_cast(viewport.h) / scale), MapWidth, MapHeight, focusPoint.X, focusPoint.Y); + + + //Clear screen + SDL_SetRenderDrawColor(Renderer, 0, 0xFF, 0, 0xFF); + SDL_RenderClear(Renderer); + + + + for (auto& tile : Tiles) + tile.Render(Renderer, TileTextures, viewport, scale); + + PlayerTexture.Render(Renderer, playerOnePosition.X - viewport.x, playerOnePosition.Y - viewport.y, nullptr, scale); + PlayerTexture2.Render(Renderer, playerTwoPosition.X - viewport.x, playerTwoPosition.Y - viewport.y, nullptr, scale); + + //Update screen + SDL_RenderPresent(Renderer); + } + } + + return 0; + } +} \ No newline at end of file diff --git a/2dgk_5/2dgk_5/KGame.h b/2dgk_5/2dgk_5/KGame.h new file mode 100644 index 0000000..f3126d9 --- /dev/null +++ b/2dgk_5/2dgk_5/KGame.h @@ -0,0 +1,42 @@ +#pragma once +#include "KTexture.h" +#include +#include + +#include "KInput.h" + + +namespace KapitanGame { + class KTile; + + class KGame + { + public: + KGame(); + ~KGame(); + KGame(const KGame& other) = delete; + KGame(KGame&& other) noexcept = delete; + KGame& operator=(const KGame& other) = delete; + KGame& operator=(KGame&& other) noexcept = delete; + int Run(int argc, char* args[]); + private: + SDL_Window* Window = nullptr; + + SDL_Renderer* Renderer = nullptr; + + KTexture TileTextures[4]; + KTexture PlayerTexture; + KTexture PlayerTexture2; + std::vector Tiles; + + int MapHeight = -1; + int MapWidth = -1; + KInput Input; + + bool LoadMedia(); + + SDL_Texture* LoadTexture(const std::string& path) const; + }; +} + + diff --git a/2dgk_5/2dgk_5/KInput.cpp b/2dgk_5/2dgk_5/KInput.cpp new file mode 100644 index 0000000..c071cfe --- /dev/null +++ b/2dgk_5/2dgk_5/KInput.cpp @@ -0,0 +1,153 @@ +#include "KInput.h" +#include "Constants.h" + +#include + +namespace KapitanGame { + KInput::KInput() { + Initialized = false; + } + + void KInput::Init() { + if (Initialized) return; + if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1) + SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER); + + const int nJoysticks = SDL_NumJoysticks(); + GamepadsCount = 0; + + // Count how many controllers there are + for (int i = 0; i < nJoysticks; i++) + if (SDL_IsGameController(i)) + GamepadsCount++; + + // If we have some controllers attached + if (GamepadsCount > 0) + { + for (int i = 0; i < GamepadsCount; i++) + { + // Open the controller and add it to our list + SDL_GameController* pad = SDL_GameControllerOpen(i); + if (SDL_GameControllerGetAttached(pad) == 1) + ConnectedControllers.push_back(pad); + else + printf("SDL_GetError() = %s\n", SDL_GetError()); + } + SDL_GameControllerEventState(SDL_ENABLE); + } + + // Vectors are empty to begin with, this sets their size + ControllerInputs.resize(GamepadsCount); + LastControllerInputs.resize(GamepadsCount); + + // Set the status of the controllers to "nothing is happening" + for (int i = 0; i < GamepadsCount; i++) { + for (int a = 0; a < SDL_CONTROLLER_AXIS_MAX; a++) { + ControllerInputs[i].Axis[a] = 0; + LastControllerInputs[i].Axis[a] = 0; + } + for (int b = 0; b < SDL_CONTROLLER_BUTTON_MAX; b++) { + ControllerInputs[i].Buttons[b] = false; + LastControllerInputs[i].Buttons[b] = false; + } + } + Initialized = true; + } + + void KInput::Free() { + for (const auto& pad : ConnectedControllers) { + SDL_GameControllerClose(pad); + } + ConnectedControllers.clear(); + } + + void KInput::HandleInputPreEvents() { + for (int i = 0; i < GamepadsCount; i++) { + for (int a = 0; a < SDL_CONTROLLER_AXIS_MAX; a++) { + LastControllerInputs[i].Axis[a] = ControllerInputs[i].Axis[a]; + } + for (int b = 0; b < SDL_CONTROLLER_BUTTON_MAX; b++) { + LastControllerInputs[i].Buttons[b] = ControllerInputs[i].Buttons[b]; + } + } + LastKeyboardInputs = KeyboardInputs; + } + void KInput::HandleInputPostEvents() { + int size = 0; + const Uint8* currentKeyStates = SDL_GetKeyboardState(&size); + KeyboardInputs = std::vector(currentKeyStates, currentKeyStates + size); + } + void KInput::HandleEvent(const SDL_Event& event) { + switch (event.type) { + // This happens when a device is added + // A future improvement is to cope with new controllers being plugged in + // when the game is running + case SDL_CONTROLLERDEVICEADDED: + printf("DEVICEADDED cdevice.which = %d\n", event.cdevice.which); + break; + + // If a controller button is pressed + case SDL_CONTROLLERBUTTONDOWN: + // Cycle through the controllers + for (int i = 0; i < GamepadsCount; i++) { + // Looking for the button that was pressed + if (event.cbutton.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(ConnectedControllers[i]))) { + // So the relevant state can be updated + ControllerInputs[i].Buttons[event.cbutton.button] = true; + } + } + break; + + // Do the same for releasing a button + case SDL_CONTROLLERBUTTONUP: + for (int i = 0; i < GamepadsCount; i++) { + if (event.cbutton.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(ConnectedControllers[i]))) { + ControllerInputs[i].Buttons[event.cbutton.button] = false; + } + } + break; + + // And something similar for axis motion + case SDL_CONTROLLERAXISMOTION: + for (int i = 0; i < GamepadsCount; i++) { + if (event.cbutton.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(ConnectedControllers[i]))) { + ControllerInputs[i].Axis[event.caxis.axis] = event.caxis.value; + } + } + break; + } + } + bool KInput::IsControllerButtonPressed(const Controllers controllerId, const SDL_GameControllerButton button) { + if (controllerId < Controllers::Controller1 || controllerId > static_cast(GamepadsCount - 1)) return false; + + return ControllerInputs[static_cast(controllerId)].Buttons[button] && !LastControllerInputs[static_cast(controllerId)].Buttons[button]; + } + + bool KInput::IsControllerButtonHeld(const Controllers controllerId, const SDL_GameControllerButton button) { + if (controllerId < Controllers::Controller1 || controllerId > static_cast(GamepadsCount - 1)) return false; + + return ControllerInputs[static_cast(controllerId)].Buttons[button] && LastControllerInputs[static_cast(controllerId)].Buttons[button]; + } + + float KInput::GetControllerAxis(const Controllers controllerId, const SDL_GameControllerAxis axis) { + if (controllerId static_cast(GamepadsCount - 1)) return 0.0; + + const int value = ControllerInputs[static_cast(controllerId)].Axis[axis]; + if (std::abs(value) < Constants::JOYSTICK_DEAD_ZONE) return 0.0; + + return static_cast(value) / 32768.0f; + } + + bool KInput::IsKeyboardButtonPressed(const Uint8 scanCode) { + if (scanCode > KeyboardInputs.size() || scanCode > LastKeyboardInputs.size()) + return false; + return KeyboardInputs[scanCode] && KeyboardInputs[scanCode] != LastKeyboardInputs[scanCode]; + } + + bool KInput::IsKeyboardButtonHeld(const Uint8 scanCode) { + if (scanCode > KeyboardInputs.size()) + return false; + return KeyboardInputs[scanCode]; + } +} + diff --git a/2dgk_5/2dgk_5/KInput.h b/2dgk_5/2dgk_5/KInput.h new file mode 100644 index 0000000..0542139 --- /dev/null +++ b/2dgk_5/2dgk_5/KInput.h @@ -0,0 +1,38 @@ +#pragma once +#include +#include + +#include "GamePad.h" + +namespace KapitanGame { + enum class Controllers : int { + Controller1, + Controller2, + Controller3, + Controller4 + }; + + class KInput { + std::vector ConnectedControllers; + std::vector ControllerInputs; + std::vector LastControllerInputs; + std::vector KeyboardInputs; + std::vector LastKeyboardInputs; + int GamepadsCount; + bool Initialized; + public: + KInput(); + void Init(); + void Free(); + void HandleInputPreEvents(); + void HandleInputPostEvents(); + void HandleEvent(const SDL_Event& event); + bool IsControllerButtonPressed(Controllers controllerId, SDL_GameControllerButton button); + bool IsControllerButtonHeld(Controllers controllerId, SDL_GameControllerButton button); + float GetControllerAxis(Controllers controllerId, SDL_GameControllerAxis axis); + bool IsKeyboardButtonHeld(Uint8 scanCode); + bool IsKeyboardButtonPressed(Uint8 scanCode); + }; +} + + diff --git a/2dgk_5/2dgk_5/KTexture.cpp b/2dgk_5/2dgk_5/KTexture.cpp new file mode 100644 index 0000000..e659cb6 --- /dev/null +++ b/2dgk_5/2dgk_5/KTexture.cpp @@ -0,0 +1,121 @@ +#include "KTexture.h" + +namespace KapitanGame { + KTexture::KTexture() + { + //Initialize + Texture = nullptr; + Width = 0; + Height = 0; + } + + KTexture::KTexture(KTexture&& other) noexcept + : Texture(other.Texture), + Width(other.Width), + Height(other.Height) { + } + + KTexture& KTexture::operator=(const KTexture& other) { + if (this == &other) + return *this; + Texture = other.Texture; + Width = other.Width; + Height = other.Height; + return *this; + } + + KTexture& KTexture::operator=(KTexture&& other) noexcept { + if (this == &other) + return *this; + Texture = other.Texture; + Width = other.Width; + Height = other.Height; + return *this; + } + + KTexture::~KTexture() + { + //Deallocate + Free(); + + } + bool KTexture::LoadFromFile(const std::string& path, SDL_Renderer* renderer) + { + //Get rid of preexisting texture + Free(); + //The final texture + SDL_Texture* newTexture = nullptr; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == nullptr) + { + 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(renderer, loadedSurface); + if (newTexture == nullptr) + { + printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Get image dimensions + Width = loadedSurface->w; + Height = loadedSurface->h; + } + + //Get rid of old loaded surface + SDL_FreeSurface(loadedSurface); + } + + //Return success + Texture = newTexture; + return Texture != nullptr; + } + + void KTexture::Free() + { + //Free texture if it exists + if (Texture != nullptr) + { + SDL_DestroyTexture(Texture); + Texture = nullptr; + Width = 0; + Height = 0; + } + } + + void KTexture::Render(SDL_Renderer* renderer, const int x, const int y, SDL_Rect* clip, const float scale) const + { + //Set rendering space and render to screen + SDL_Rect renderQuad = { + static_cast(static_cast(x) * scale), static_cast(static_cast(y) * scale), + static_cast(static_cast(Width) * scale), static_cast(static_cast(Height) * scale) + }; + + //Set clip rendering dimensions + if (clip != nullptr) + { + renderQuad.w = clip->w; + renderQuad.h = clip->h; + } + + //Render to screen + SDL_RenderCopy(renderer, Texture, clip, &renderQuad); + } + + int KTexture::GetWidth() const + { + return Width; + } + + int KTexture::GetHeight() const + { + return Height; + } +} \ No newline at end of file diff --git a/2dgk_5/2dgk_5/KTexture.h b/2dgk_5/2dgk_5/KTexture.h new file mode 100644 index 0000000..85aef92 --- /dev/null +++ b/2dgk_5/2dgk_5/KTexture.h @@ -0,0 +1,43 @@ +#pragma once +#include +#include + +namespace KapitanGame { + //Texture wrapper class + class KTexture { + public: + KTexture(); + + ~KTexture(); + + KTexture(const KTexture& other) = default; + + KTexture(KTexture&& other) noexcept; + + KTexture& operator=(const KTexture& other); + + KTexture& operator=(KTexture&& other) noexcept; + + //Loads image at specified path + bool LoadFromFile(const std::string& path, SDL_Renderer* renderer); + + //Deallocates texture + void Free(); + + //Renders texture at given point + void Render(SDL_Renderer* renderer, int x, int y, SDL_Rect* clip = nullptr, float scale = 1.f) const; + + //Gets image dimensions + int GetWidth() const; + int GetHeight() const; + + private: + //The actual hardware texture + SDL_Texture* Texture; + + //Image dimensions + int Width; + int Height; + }; + +} diff --git a/2dgk_5/2dgk_5/KTile.cpp b/2dgk_5/2dgk_5/KTile.cpp new file mode 100644 index 0000000..9836c73 --- /dev/null +++ b/2dgk_5/2dgk_5/KTile.cpp @@ -0,0 +1,33 @@ +#include "KTile.h" + +#include + +#include "Constants.h" +#include "KTexture.h" + +namespace KapitanGame { + + int KTile::GetType() const { + return static_cast(Type); + } + + SDL_Rect KTile::GetBox() const { + return Box; + } + + KTile::KTile(const int x, const int y, const TileType tileType) { + //Get the offsets + Box.x = x; + Box.y = y; + + Box.w = Constants::TILE_WIDTH; + Box.h = Constants::TILE_HEIGHT; + + //Get the tile type + Type = tileType; + } + + void KTile::Render(SDL_Renderer* renderer, KTexture tileTextures[], const SDL_Rect& camera, const float scale) const { + tileTextures[GetType()].Render(renderer, Box.x - camera.x, Box.y - camera.y, nullptr, scale); + } +} diff --git a/2dgk_5/2dgk_5/KTile.h b/2dgk_5/2dgk_5/KTile.h new file mode 100644 index 0000000..0f60ea3 --- /dev/null +++ b/2dgk_5/2dgk_5/KTile.h @@ -0,0 +1,36 @@ +#pragma once +#include +#include +#include + +namespace KapitanGame +{ + class KTexture; + + enum class TileType : std::uint32_t { + Default = 0, + HorizontalWall = 1, + VerticalWall = 2, + Dot = 3 + }; + class KTile { + public: + //Initializes position and type + KTile(int x, int y, TileType tileType); + + //Shows the tile + void Render(SDL_Renderer* renderer, KTexture tileTextures[], const SDL_Rect& camera, float scale = 1.f) const; + + //Get the tile type + int GetType() const; + + SDL_Rect GetBox() const; + + private: + //The attributes of the tile + SDL_Rect Box{}; + + //The tile type + TileType Type; + }; +} diff --git a/2dgk_5/2dgk_5/KVector2d.h b/2dgk_5/2dgk_5/KVector2d.h new file mode 100644 index 0000000..339b01a --- /dev/null +++ b/2dgk_5/2dgk_5/KVector2d.h @@ -0,0 +1,61 @@ +#pragma once +#include + +namespace KapitanGame { + struct KVector2D { + float X; + float Y; + + KVector2D(const float x, const float y) : X(x), Y(y) {} + + float Length() const { return sqrt(X * X + Y * Y); } + + KVector2D operator+(const KVector2D& v2) const { + return { X + v2.X, Y + v2.Y }; + } + + friend KVector2D& operator+=(KVector2D& v1, const KVector2D& v2) { + v1.X += v2.X; + v1.Y += v2.Y; + return v1; + } + + KVector2D operator*(const float scalar) const + { + return { X * scalar, Y * scalar }; + } + + KVector2D& operator*=(const float scalar) { + X *= scalar; + Y *= scalar; + return *this; + } + + KVector2D operator-(const KVector2D& v2) const { + return { X - v2.X, Y - v2.Y }; + } + friend KVector2D& operator-=(KVector2D& v1, const KVector2D& v2) { + v1.X -= v2.X; + v1.Y -= v2.Y; + return v1; + } + + KVector2D operator/(const float scalar) const + { + return { X / scalar, Y / scalar }; + } + KVector2D& operator/=(const float scalar) { + X /= scalar; + Y /= scalar; + return *this; + } + + void Normalize() { + const float l = Length(); + if (l > 0) { + (*this) *= 1 / l; + } + } + }; +} + diff --git a/2dgk_5/2dgk_5/Utils.h b/2dgk_5/2dgk_5/Utils.h new file mode 100644 index 0000000..959b653 --- /dev/null +++ b/2dgk_5/2dgk_5/Utils.h @@ -0,0 +1,30 @@ +#pragma once +#include +#include +#include +namespace KapitanGame { + namespace Utils { + + template + std::string StringFormat(const std::string& format, Args ... args) + { + const int sizeS = std::snprintf(nullptr, 0, format.c_str(), args ...) + 1; // Extra space for '\0' + if (sizeS <= 0) { throw std::runtime_error("Error during formatting."); } + const auto size = static_cast(sizeS); + const auto buf = std::make_unique(size); + std::snprintf(buf.get(), size, format.c_str(), args ...); + return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside + } + 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; + } + } +} \ No newline at end of file diff --git a/2dgk_5/2dgk_5/main.cpp b/2dgk_5/2dgk_5/main.cpp index 9787e32..2944d8f 100644 --- a/2dgk_5/2dgk_5/main.cpp +++ b/2dgk_5/2dgk_5/main.cpp @@ -1,709 +1,8 @@ -//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(); -} +#include "KGame.h" 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; + KapitanGame::KGame game; - //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; + return game.Run(argc, args); } - -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/readme.txt b/2dgk_5/readme.txt new file mode 100644 index 0000000..74e0672 --- /dev/null +++ b/2dgk_5/readme.txt @@ -0,0 +1,7 @@ +IDE: Microsoft Visual Studio 2019 +Kompilator: Microsoft Visual C++ +Biblioteki: SDL2 + +Przetestowane pady: DualSense, DualShock 3 (ze sterownikiem/wrapperem XInputowym ScpToolkit), telefon Android (ze sterownikiem/wrapperem XInputowym HandyGamePad). + +Uwaga: Do pobrania bibliotek projekt wykorzystuje vcpkg, menedżer bibliotek C++, w trybie Manifest. Należy go pobrać i skonfigurować zgodnie z instrukcją: https://github.com/microsoft/vcpkg#getting-started