Added maps and a camera
This commit is contained in:
parent
1714c5d732
commit
23abb0f48c
|
@ -9,16 +9,18 @@ include_directories(linkedlist)
|
|||
add_definitions("-Wall")
|
||||
|
||||
# PROGRAMS:
|
||||
add_executable(rpgfight
|
||||
add_executable(breakhack
|
||||
src/main
|
||||
src/texture
|
||||
src/screenresolution
|
||||
src/sprite
|
||||
src/util
|
||||
src/player
|
||||
src/map
|
||||
src/camera
|
||||
)
|
||||
|
||||
target_link_libraries(rpgfight
|
||||
target_link_libraries(breakhack
|
||||
linkedlist
|
||||
-lSDL2
|
||||
-lSDL2_image
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include "linkedlist.h"
|
||||
|
||||
static
|
||||
void *linkedlist_malloc(size_t size)
|
||||
void *linkedlist_malloc(unsigned int size)
|
||||
{
|
||||
void *ptr;
|
||||
ptr = malloc(size);
|
||||
|
@ -28,14 +28,14 @@ LinkedList* linkedlist_create()
|
|||
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;
|
||||
for (i = 0; i < size; ++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();
|
||||
|
@ -59,7 +59,7 @@ void* linkedlist_pop(LinkedList **head)
|
|||
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) {
|
||||
*head = linkedlist_node_create();
|
||||
|
|
|
@ -9,11 +9,11 @@ typedef struct Node LinkedList;
|
|||
|
||||
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_append(LinkedList **head, void *value, size_t size);
|
||||
void linkedlist_append(LinkedList **head, void *value, unsigned int size);
|
||||
|
||||
void* linkedlist_poplast(LinkedList **head);
|
||||
|
||||
|
|
|
@ -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
|
||||
};
|
||||
}
|
|
@ -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_
|
14
src/main.c
14
src/main.c
|
@ -7,6 +7,8 @@
|
|||
#include "player.h"
|
||||
#include "screenresolution.h"
|
||||
#include "dimension.h"
|
||||
#include "camera.h"
|
||||
#include "map.h"
|
||||
|
||||
#define SCREEN_WIDTH 1024
|
||||
#define SCREEN_HEIGHT 768
|
||||
|
@ -15,6 +17,9 @@ static SDL_Window *gWindow = NULL;
|
|||
static SDL_Renderer *gRenderer = NULL;
|
||||
static Sprite *gPlayer = NULL;
|
||||
static LinkedList *gSpriteList = NULL;
|
||||
static Map *gMap = NULL;
|
||||
|
||||
static Camera gCamera;
|
||||
|
||||
static
|
||||
bool initSDL()
|
||||
|
@ -71,6 +76,7 @@ static
|
|||
bool initGame()
|
||||
{
|
||||
gSpriteList = linkedlist_create();
|
||||
gMap = map_create(gRenderer);
|
||||
return gSpriteList == NULL;
|
||||
}
|
||||
|
||||
|
@ -80,6 +86,10 @@ bool init()
|
|||
bool result = true;
|
||||
result = result && initSDL();
|
||||
result = result && initGame();
|
||||
if (result) {
|
||||
gCamera.pos = (Position) { 0, 0 };
|
||||
gCamera.renderer = gRenderer;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -108,7 +118,8 @@ void run()
|
|||
|
||||
SDL_RenderClear(gRenderer);
|
||||
|
||||
sprite_render(gPlayer, gRenderer);
|
||||
map_render(gMap, &gCamera);
|
||||
sprite_render(gPlayer, &gCamera);
|
||||
|
||||
SDL_RenderPresent(gRenderer);
|
||||
|
||||
|
@ -122,6 +133,7 @@ static
|
|||
void close()
|
||||
{
|
||||
sprite_destroy(gPlayer);
|
||||
map_destroy(gMap);
|
||||
SDL_DestroyWindow(gWindow);
|
||||
gWindow = NULL;
|
||||
IMG_Quit();
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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_
|
|
@ -27,16 +27,17 @@ void sprite_load_texture(Sprite *sprite, char *path, SDL_Renderer *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) {
|
||||
s->pos.x,
|
||||
s->pos.y,
|
||||
cameraPos.x,
|
||||
cameraPos.y,
|
||||
s->texture->dim.width,
|
||||
s->texture->dim.height
|
||||
};
|
||||
|
||||
SDL_RenderCopy(renderer,
|
||||
SDL_RenderCopy(cam->renderer,
|
||||
s->texture->texture,
|
||||
&s->texture->clip,
|
||||
&draw_box);
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "texture.h"
|
||||
#include "position.h"
|
||||
#include "camera.h"
|
||||
|
||||
typedef struct Sprite_t{
|
||||
Texture *texture;
|
||||
|
@ -16,7 +17,7 @@ Sprite* sprite_create();
|
|||
|
||||
void sprite_load_texture(Sprite *, char *path, SDL_Renderer *);
|
||||
|
||||
void sprite_render(Sprite*, SDL_Renderer*);
|
||||
void sprite_render(Sprite*, Camera*);
|
||||
|
||||
void sprite_destroy(Sprite *);
|
||||
|
||||
|
|
Loading…
Reference in New Issue