camera controller

This commit is contained in:
Hunter 2023-05-27 13:36:40 -04:00
parent a1f8e26d9a
commit 83e312894b
5 changed files with 15 additions and 9 deletions

View file

@ -18,5 +18,4 @@ public:
private:
bool mb_isOpen = false;
Map m_currentMap;
SDL_Rect m_viewport;
};

View file

@ -7,6 +7,7 @@
struct GlobalState {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Rect camera;
PPlayer player;
PhoenixAssets assets;
int scale = 20;

View file

@ -2,7 +2,7 @@ MAPNAME DMSCALE
MAPVERS 1
MAPAUTH Interfiber
MAPSIZE 10x10
MAPSCAL 5x5
MAPSCAL 2x2
MAP
TILE WALL
TILE FLOOR

View file

@ -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();

View file

@ -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;