From 83e312894bc4628e82de81307731f36744de645d Mon Sep 17 00:00:00 2001 From: Interfiber Date: Sat, 27 May 2023 13:36:40 -0400 Subject: [PATCH] camera controller --- include/phoenix.hpp | 1 - include/state.hpp | 1 + maps/dm_scale.map | 2 +- src/phoenix.cpp | 16 +++++++++++----- src/texture.cpp | 4 ++-- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/include/phoenix.hpp b/include/phoenix.hpp index 824670d..badf86c 100644 --- a/include/phoenix.hpp +++ b/include/phoenix.hpp @@ -18,5 +18,4 @@ public: private: bool mb_isOpen = false; Map m_currentMap; - SDL_Rect m_viewport; }; diff --git a/include/state.hpp b/include/state.hpp index d725eeb..8ead474 100644 --- a/include/state.hpp +++ b/include/state.hpp @@ -7,6 +7,7 @@ struct GlobalState { SDL_Window *window; SDL_Renderer *renderer; + SDL_Rect camera; PPlayer player; PhoenixAssets assets; int scale = 20; diff --git a/maps/dm_scale.map b/maps/dm_scale.map index 45c8420..527d629 100644 --- a/maps/dm_scale.map +++ b/maps/dm_scale.map @@ -2,7 +2,7 @@ MAPNAME DMSCALE MAPVERS 1 MAPAUTH Interfiber MAPSIZE 10x10 -MAPSCAL 5x5 +MAPSCAL 2x2 MAP TILE WALL TILE FLOOR diff --git a/src/phoenix.cpp b/src/phoenix.cpp index 5488393..59cba55 100644 --- a/src/phoenix.cpp +++ b/src/phoenix.cpp @@ -34,13 +34,14 @@ PhoenixGame::PhoenixGame() { PRunBoostrap(); m_currentMap = PLoadMapFromFile("maps/dm_scale.map"); - m_viewport = {0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2}; + + state.camera.x = 0; + state.camera.y = 0; + state.camera.w = SCREEN_WIDTH; + state.camera.h = SCREEN_HEIGHT; } -void PhoenixGame::present() { - SDL_RenderSetViewport(state.renderer, &m_viewport); - SDL_RenderPresent(state.renderer); -} +void PhoenixGame::present() { SDL_RenderPresent(state.renderer); } void PhoenixGame::render() { @@ -90,6 +91,11 @@ void PhoenixGame::run() { state.player.x += 5; } + state.camera.x = + state.player.x - ((SCREEN_WIDTH / m_currentMap.mapScaleX) / 2); + state.camera.y = + state.player.y - ((SCREEN_HEIGHT / m_currentMap.mapScaleY) / 2); + // Rendering code render(); diff --git a/src/texture.cpp b/src/texture.cpp index 8c45f4e..3325766 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -25,8 +25,8 @@ void PDestroyTexture(PhoenixTexture *texture) { void PRenderTexture(PhoenixTexture *texture, int x, int y) { SDL_Rect dstRect{}; - dstRect.x = x; - dstRect.y = y; + dstRect.x = x - state.camera.x; + dstRect.y = y - state.camera.y; dstRect.w = TEXTURE_WIDTH; dstRect.h = TEXTURE_HEIGHT;