From 0b376a4998949b4bbe2367aefc6284f0f04c5bfb Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Sun, 3 Dec 2017 11:09:57 +0100 Subject: [PATCH] Camera and map following --- src/camera.c | 19 +++++++++++++++++++ src/camera.h | 3 +++ src/main.c | 30 +++++++++++++++++++++--------- src/map.c | 34 ++++++++++++++++++++++++++++------ src/map.h | 3 +++ 5 files changed, 74 insertions(+), 15 deletions(-) diff --git a/src/camera.c b/src/camera.c index 3f7fc21..ea077ce 100644 --- a/src/camera.c +++ b/src/camera.c @@ -1,4 +1,5 @@ #include "camera.h" +#include "map.h" Position camera_to_camera_position(Camera *cam, Position *pos) { @@ -7,3 +8,21 @@ Position camera_to_camera_position(Camera *cam, Position *pos) pos->y - cam->pos.y }; } + +void camera_follow_position(Camera *cam, Position *pos) +{ + unsigned int room_width, room_height; + + room_width = MAP_ROOM_WIDTH * TILE_DIMENSION; + room_height = MAP_ROOM_HEIGHT * TILE_DIMENSION; + + if (pos->x <= 0) + cam->pos.x = 0; + else + cam->pos.x = pos->x - (pos->x % room_width); + + if (pos->y <= 0) + cam->pos.y = 0; + else + cam->pos.y = pos->y - (pos->y % room_height); +} diff --git a/src/camera.h b/src/camera.h index dbf04ef..99e1247 100644 --- a/src/camera.h +++ b/src/camera.h @@ -2,6 +2,7 @@ #define CAMERA_H_ #include + #include "position.h" typedef struct { @@ -11,4 +12,6 @@ typedef struct { Position camera_to_camera_position(Camera *cam, Position *pos); +void camera_follow_position(Camera*, Position*); + #endif // CAMERA_H_ diff --git a/src/main.c b/src/main.c index 7a5bf70..fce230f 100644 --- a/src/main.c +++ b/src/main.c @@ -26,8 +26,8 @@ static bool initSDL() { int imgFlags = IMG_INIT_PNG; - //Dimension dim = getScreenDimensions(); - Dimension dim = (Dimension) { 1920, 1080 }; + Dimension dim = getScreenDimensions(); + //Dimension dim = (Dimension) { 1920, 1080 }; double scale = 1.0; if (dim.height > 1080) { @@ -101,22 +101,34 @@ void loadMedia() gPlayer = player_create(WARRIOR, gRenderer); } +static +bool handle_events() +{ + static SDL_Event event; + bool quit = false; + + while (SDL_PollEvent(&event) != 0) { + if (event.type == SDL_QUIT) { + quit = true; + } else { + gPlayer->handle_event(gPlayer, &event); + camera_follow_position(&gCamera, &gPlayer->pos); + map_set_current_room(gMap, &gPlayer->pos); + } + } + return quit; +} + static void run() { - SDL_Event event; bool quit = false; while (!quit) { int ticks = SDL_GetTicks(); - while (SDL_PollEvent(&event) != 0) { - if (event.type == SDL_QUIT) - quit = true; - else - gPlayer->handle_event(gPlayer, &event); - } + quit = handle_events(); SDL_RenderClear(gRenderer); diff --git a/src/map.c b/src/map.c index db2cd7b..cbd63b0 100644 --- a/src/map.c +++ b/src/map.c @@ -62,8 +62,8 @@ void map_tile_render(Map *map, MapTile *tile, Position *pos, Camera *cam) SDL_Rect draw_box = (SDL_Rect) { camPos.x, camPos.y, - 64, - 64 + TILE_DIMENSION, + TILE_DIMENSION }; Texture *texture = linkedlist_get(&map->textures, tile->textureIndex); @@ -82,22 +82,44 @@ void map_render(Map *map, Camera *cam) Room *room; Position roomPos = { map->currentRoom.x, map->currentRoom.y }; Position roomCords = { - roomPos.x * MAP_ROOM_WIDTH * 64, - roomPos.y * MAP_ROOM_HEIGHT * 64 + roomPos.x * MAP_ROOM_WIDTH * TILE_DIMENSION, + roomPos.y * MAP_ROOM_HEIGHT * TILE_DIMENSION }; room = map->rooms[roomPos.x][roomPos.y]; for (i=0; i < MAP_ROOM_HEIGHT; ++i) { for (j=0; j < MAP_ROOM_WIDTH; ++j) { Position tilePos = (Position) { - roomCords.x + j*64, - roomCords.y + i*64 + roomCords.x + j*TILE_DIMENSION, + roomCords.y + i*TILE_DIMENSION }; map_tile_render(map, room->tiles[i][j], &tilePos, cam); } } } +void map_set_current_room(Map *map, Position *pos) +{ + unsigned int room_width, room_height; + + room_width = MAP_ROOM_WIDTH * TILE_DIMENSION; + room_height = MAP_ROOM_HEIGHT * TILE_DIMENSION; + + if (pos->x <= 0) { + map->currentRoom.x = 0; + } else { + unsigned int room_cord_x = pos->x - (pos->x % room_width); + map->currentRoom.x = room_cord_x / room_width; + } + + if (pos->y <= 0) { + map->currentRoom.y = 0; + } else { + unsigned int room_cord_y = pos->y - (pos->y % room_height); + map->currentRoom.y = room_cord_y / room_height; + } +} + static void map_room_destroy(Room *room) { diff --git a/src/map.h b/src/map.h index 3f59117..eacfb9c 100644 --- a/src/map.h +++ b/src/map.h @@ -12,6 +12,7 @@ #define MAP_ROOM_WIDTH 16 #define MAP_V_ROOM_COUNT 10 #define MAP_H_ROOM_COUNT 10 +#define TILE_DIMENSION 64 typedef struct { unsigned int textureIndex; @@ -37,6 +38,8 @@ void map_add_tile(Map *map, Position *room_pos, Position *tile_pos, MapTile*); void map_render(Map*, Camera*); +void map_set_current_room(Map*, Position*); + void map_destroy(Map*); #endif // MAP_H_