Camera and map following
This commit is contained in:
parent
7362c2370f
commit
0b376a4998
19
src/camera.c
19
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);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define CAMERA_H_
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#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_
|
||||
|
|
30
src/main.c
30
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);
|
||||
|
||||
|
|
34
src/map.c
34
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)
|
||||
{
|
||||
|
|
|
@ -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_
|
||||
|
|
Loading…
Reference in New Issue