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 "camera.h"
|
||||||
|
#include "map.h"
|
||||||
|
|
||||||
Position camera_to_camera_position(Camera *cam, Position *pos)
|
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
|
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_
|
#define CAMERA_H_
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -11,4 +12,6 @@ typedef struct {
|
||||||
|
|
||||||
Position camera_to_camera_position(Camera *cam, Position *pos);
|
Position camera_to_camera_position(Camera *cam, Position *pos);
|
||||||
|
|
||||||
|
void camera_follow_position(Camera*, Position*);
|
||||||
|
|
||||||
#endif // CAMERA_H_
|
#endif // CAMERA_H_
|
||||||
|
|
30
src/main.c
30
src/main.c
|
@ -26,8 +26,8 @@ static
|
||||||
bool initSDL()
|
bool initSDL()
|
||||||
{
|
{
|
||||||
int imgFlags = IMG_INIT_PNG;
|
int imgFlags = IMG_INIT_PNG;
|
||||||
//Dimension dim = getScreenDimensions();
|
Dimension dim = getScreenDimensions();
|
||||||
Dimension dim = (Dimension) { 1920, 1080 };
|
//Dimension dim = (Dimension) { 1920, 1080 };
|
||||||
double scale = 1.0;
|
double scale = 1.0;
|
||||||
|
|
||||||
if (dim.height > 1080) {
|
if (dim.height > 1080) {
|
||||||
|
@ -101,22 +101,34 @@ void loadMedia()
|
||||||
gPlayer = player_create(WARRIOR, gRenderer);
|
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
|
static
|
||||||
void run()
|
void run()
|
||||||
{
|
{
|
||||||
SDL_Event event;
|
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
|
|
||||||
while (!quit)
|
while (!quit)
|
||||||
{
|
{
|
||||||
int ticks = SDL_GetTicks();
|
int ticks = SDL_GetTicks();
|
||||||
|
|
||||||
while (SDL_PollEvent(&event) != 0) {
|
quit = handle_events();
|
||||||
if (event.type == SDL_QUIT)
|
|
||||||
quit = true;
|
|
||||||
else
|
|
||||||
gPlayer->handle_event(gPlayer, &event);
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_RenderClear(gRenderer);
|
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) {
|
SDL_Rect draw_box = (SDL_Rect) {
|
||||||
camPos.x,
|
camPos.x,
|
||||||
camPos.y,
|
camPos.y,
|
||||||
64,
|
TILE_DIMENSION,
|
||||||
64
|
TILE_DIMENSION
|
||||||
};
|
};
|
||||||
|
|
||||||
Texture *texture = linkedlist_get(&map->textures, tile->textureIndex);
|
Texture *texture = linkedlist_get(&map->textures, tile->textureIndex);
|
||||||
|
@ -82,22 +82,44 @@ void map_render(Map *map, Camera *cam)
|
||||||
Room *room;
|
Room *room;
|
||||||
Position roomPos = { map->currentRoom.x, map->currentRoom.y };
|
Position roomPos = { map->currentRoom.x, map->currentRoom.y };
|
||||||
Position roomCords = {
|
Position roomCords = {
|
||||||
roomPos.x * MAP_ROOM_WIDTH * 64,
|
roomPos.x * MAP_ROOM_WIDTH * TILE_DIMENSION,
|
||||||
roomPos.y * MAP_ROOM_HEIGHT * 64
|
roomPos.y * MAP_ROOM_HEIGHT * TILE_DIMENSION
|
||||||
};
|
};
|
||||||
|
|
||||||
room = map->rooms[roomPos.x][roomPos.y];
|
room = map->rooms[roomPos.x][roomPos.y];
|
||||||
for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
|
for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
|
||||||
for (j=0; j < MAP_ROOM_WIDTH; ++j) {
|
for (j=0; j < MAP_ROOM_WIDTH; ++j) {
|
||||||
Position tilePos = (Position) {
|
Position tilePos = (Position) {
|
||||||
roomCords.x + j*64,
|
roomCords.x + j*TILE_DIMENSION,
|
||||||
roomCords.y + i*64
|
roomCords.y + i*TILE_DIMENSION
|
||||||
};
|
};
|
||||||
map_tile_render(map, room->tiles[i][j], &tilePos, cam);
|
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
|
static
|
||||||
void map_room_destroy(Room *room)
|
void map_room_destroy(Room *room)
|
||||||
{
|
{
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#define MAP_ROOM_WIDTH 16
|
#define MAP_ROOM_WIDTH 16
|
||||||
#define MAP_V_ROOM_COUNT 10
|
#define MAP_V_ROOM_COUNT 10
|
||||||
#define MAP_H_ROOM_COUNT 10
|
#define MAP_H_ROOM_COUNT 10
|
||||||
|
#define TILE_DIMENSION 64
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned int textureIndex;
|
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_render(Map*, Camera*);
|
||||||
|
|
||||||
|
void map_set_current_room(Map*, Position*);
|
||||||
|
|
||||||
void map_destroy(Map*);
|
void map_destroy(Map*);
|
||||||
|
|
||||||
#endif // MAP_H_
|
#endif // MAP_H_
|
||||||
|
|
Loading…
Reference in New Issue