Added maps and a camera

This commit is contained in:
Linus Probert 2017-12-01 16:03:19 +01:00
parent 1714c5d732
commit 23abb0f48c
10 changed files with 178 additions and 14 deletions

View File

@ -9,16 +9,18 @@ include_directories(linkedlist)
add_definitions("-Wall") add_definitions("-Wall")
# PROGRAMS: # PROGRAMS:
add_executable(rpgfight add_executable(breakhack
src/main src/main
src/texture src/texture
src/screenresolution src/screenresolution
src/sprite src/sprite
src/util src/util
src/player src/player
src/map
src/camera
) )
target_link_libraries(rpgfight target_link_libraries(breakhack
linkedlist linkedlist
-lSDL2 -lSDL2
-lSDL2_image -lSDL2_image

View File

@ -3,7 +3,7 @@
#include "linkedlist.h" #include "linkedlist.h"
static static
void *linkedlist_malloc(size_t size) void *linkedlist_malloc(unsigned int size)
{ {
void *ptr; void *ptr;
ptr = malloc(size); ptr = malloc(size);
@ -28,14 +28,14 @@ LinkedList* linkedlist_create()
return NULL; return NULL;
} }
static void copy_data(void *dest, void *src, size_t size) static void copy_data(void *dest, void *src, unsigned int size)
{ {
int i; int i;
for (i = 0; i < size; ++i) for (i = 0; i < size; ++i)
*(char*)(dest + i) = *(char*)(src + i); *(char*)(dest + i) = *(char*)(src + i);
} }
void linkedlist_push(LinkedList **head, void *value, size_t size) void linkedlist_push(LinkedList **head, void *value, unsigned int size)
{ {
LinkedList *node = linkedlist_node_create(); LinkedList *node = linkedlist_node_create();
@ -59,7 +59,7 @@ void* linkedlist_pop(LinkedList **head)
return data; return data;
} }
void linkedlist_append(LinkedList **head, void *value, size_t size) void linkedlist_append(LinkedList **head, void *value, unsigned int size)
{ {
if (*head == NULL) { if (*head == NULL) {
*head = linkedlist_node_create(); *head = linkedlist_node_create();

View File

@ -9,11 +9,11 @@ typedef struct Node LinkedList;
LinkedList* linkedlist_create(); LinkedList* linkedlist_create();
void linkedlist_push(LinkedList **head, void *value, size_t size); void linkedlist_push(LinkedList **head, void *value, unsigned int size);
void* linkedlist_pop(LinkedList **head); void* linkedlist_pop(LinkedList **head);
void linkedlist_append(LinkedList **head, void *value, size_t size); void linkedlist_append(LinkedList **head, void *value, unsigned int size);
void* linkedlist_poplast(LinkedList **head); void* linkedlist_poplast(LinkedList **head);

9
src/camera.c Normal file
View File

@ -0,0 +1,9 @@
#include "camera.h"
Position camera_to_camera_position(Camera *cam, Position *pos)
{
return (Position) {
pos->x - cam->pos.x,
pos->y - cam->pos.y
};
}

14
src/camera.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef CAMERA_H_
#define CAMERA_H_
#include <SDL2/SDL.h>
#include "position.h"
typedef struct {
Position pos;
SDL_Renderer *renderer;
} Camera;
Position camera_to_camera_position(Camera *cam, Position *pos);
#endif // CAMERA_H_

View File

@ -7,6 +7,8 @@
#include "player.h" #include "player.h"
#include "screenresolution.h" #include "screenresolution.h"
#include "dimension.h" #include "dimension.h"
#include "camera.h"
#include "map.h"
#define SCREEN_WIDTH 1024 #define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768 #define SCREEN_HEIGHT 768
@ -15,6 +17,9 @@ static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL; static SDL_Renderer *gRenderer = NULL;
static Sprite *gPlayer = NULL; static Sprite *gPlayer = NULL;
static LinkedList *gSpriteList = NULL; static LinkedList *gSpriteList = NULL;
static Map *gMap = NULL;
static Camera gCamera;
static static
bool initSDL() bool initSDL()
@ -71,6 +76,7 @@ static
bool initGame() bool initGame()
{ {
gSpriteList = linkedlist_create(); gSpriteList = linkedlist_create();
gMap = map_create(gRenderer);
return gSpriteList == NULL; return gSpriteList == NULL;
} }
@ -80,6 +86,10 @@ bool init()
bool result = true; bool result = true;
result = result && initSDL(); result = result && initSDL();
result = result && initGame(); result = result && initGame();
if (result) {
gCamera.pos = (Position) { 0, 0 };
gCamera.renderer = gRenderer;
}
return result; return result;
} }
@ -108,7 +118,8 @@ void run()
SDL_RenderClear(gRenderer); SDL_RenderClear(gRenderer);
sprite_render(gPlayer, gRenderer); map_render(gMap, &gCamera);
sprite_render(gPlayer, &gCamera);
SDL_RenderPresent(gRenderer); SDL_RenderPresent(gRenderer);
@ -122,6 +133,7 @@ static
void close() void close()
{ {
sprite_destroy(gPlayer); sprite_destroy(gPlayer);
map_destroy(gMap);
SDL_DestroyWindow(gWindow); SDL_DestroyWindow(gWindow);
gWindow = NULL; gWindow = NULL;
IMG_Quit(); IMG_Quit();

93
src/map.c Normal file
View File

@ -0,0 +1,93 @@
#include "map.h"
#include "util.h"
static
Sprite* create_default_tile(SDL_Renderer *renderer)
{
Sprite *s = sprite_create();
sprite_load_texture(s, "assets/Objects/Floor.png", renderer);
s->texture->clip = (SDL_Rect) { 16, 54, 16, 16 };
s->texture->dim = (Dimension) { 64, 64 };
return s;
}
static
Room* create_room()
{
int i, j;
Room *room;
room = ec_malloc(sizeof(Room));
for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
for (j=0; j < MAP_ROOM_WIDTH; ++j) {
room->tiles[i][j] = NULL;
}
}
return room;
}
Map* map_create(SDL_Renderer *renderer)
{
int i, j;
Map *map = ec_malloc(sizeof(Map));
map->currentRoom = (Position) { 0, 0 };
map->level = 1;
map->defaultTile = create_default_tile(renderer);
for (i=0; i < MAP_V_ROOM_COUNT; ++i) {
for (j=0; j < MAP_H_ROOM_COUNT; ++j) {
map->rooms[i][j] = create_room();
}
}
return map;
}
void map_render(Map *map, Camera *cam)
{
int i, j;
Room *room;
Position roomPos = { map->currentRoom.x, map->currentRoom.y };
Position roomCords = {
roomPos.x * MAP_ROOM_WIDTH * 64,
roomPos.y * MAP_ROOM_HEIGHT * 64
};
Sprite *dTile = map->defaultTile;
room = map->rooms[roomPos.x][roomPos.y];
for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
for (j=0; j < MAP_ROOM_WIDTH; ++j) {
if (room->tiles[i][j] == NULL) {
dTile->pos.x = roomCords.x + (64*j);
dTile->pos.y = roomCords.y + (64*i);
sprite_render(dTile, cam);
}
}
}
}
static
void map_room_destroy(Room *room)
{
int i, j;
for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
for (j=0; j < MAP_ROOM_WIDTH; ++j) {
if (room->tiles[i][j]) {
sprite_destroy(room->tiles[i][j]);
}
}
}
free(room);
}
void map_destroy(Map *map)
{
int i, j;
for (i=0; i < MAP_V_ROOM_COUNT; ++i) {
for (j=0; j < MAP_H_ROOM_COUNT; ++j) {
map_room_destroy(map->rooms[i][j]);
}
}
free(map);
}

32
src/map.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef MAP_H_
#define MAP_H_
#include <SDL2/SDL.h>
#include <linkedlist.h>
#include "sprite.h"
#include "camera.h"
#define MAP_ROOM_HEIGHT 12
#define MAP_ROOM_WIDTH 16
#define MAP_V_ROOM_COUNT 10
#define MAP_H_ROOM_COUNT 10
typedef struct {
Sprite* tiles[MAP_ROOM_HEIGHT][MAP_ROOM_WIDTH];
} Room;
typedef struct {
Room* rooms[MAP_V_ROOM_COUNT][MAP_H_ROOM_COUNT];
Position currentRoom;
Sprite* defaultTile;
int level;
} Map;
Map* map_create(SDL_Renderer *renderer);
void map_render(Map*, Camera*);
void map_destroy(Map*);
#endif // MAP_H_

View File

@ -27,16 +27,17 @@ void sprite_load_texture(Sprite *sprite, char *path, SDL_Renderer *renderer)
sprite->texture = texture_create(path, renderer); sprite->texture = texture_create(path, renderer);
} }
void sprite_render(Sprite *s, SDL_Renderer *renderer) void sprite_render(Sprite *s, Camera *cam)
{ {
Position cameraPos = camera_to_camera_position(cam, &s->pos);
SDL_Rect draw_box = (SDL_Rect) { SDL_Rect draw_box = (SDL_Rect) {
s->pos.x, cameraPos.x,
s->pos.y, cameraPos.y,
s->texture->dim.width, s->texture->dim.width,
s->texture->dim.height s->texture->dim.height
}; };
SDL_RenderCopy(renderer, SDL_RenderCopy(cam->renderer,
s->texture->texture, s->texture->texture,
&s->texture->clip, &s->texture->clip,
&draw_box); &draw_box);

View File

@ -5,6 +5,7 @@
#include "texture.h" #include "texture.h"
#include "position.h" #include "position.h"
#include "camera.h"
typedef struct Sprite_t{ typedef struct Sprite_t{
Texture *texture; Texture *texture;
@ -16,7 +17,7 @@ Sprite* sprite_create();
void sprite_load_texture(Sprite *, char *path, SDL_Renderer *); void sprite_load_texture(Sprite *, char *path, SDL_Renderer *);
void sprite_render(Sprite*, SDL_Renderer*); void sprite_render(Sprite*, Camera*);
void sprite_destroy(Sprite *); void sprite_destroy(Sprite *);