A game engine for top-down 2D RPG games.
rpg game-engine raylib c99
1#include <keraforge.h> 2#include <raylib.h> 3#include <stdlib.h> 4#include <string.h> 5#include <math.h> 6 7struct _kf_tiles kf_tiles = {0}; 8 9struct kf_world *kf_world_new(u32 width, u32 height, kf_tileid_t fill) 10{ 11 const size_t len = sizeof(kf_tileid_t) * width * height; 12 struct kf_world *world = malloc(sizeof(struct kf_world) + len); 13 world->revision = 0; 14 world->width = width; 15 world->height = height; 16 memset(world->map, fill, len); 17 return world; 18} 19 20size_t kf_world_getsize(struct kf_world *world) 21{ 22 return sizeof(struct kf_world) + sizeof(kf_tileid_t)*world->width*world->height; 23} 24 25kf_tileid_t *kf_world_gettile(struct kf_world *world, u32 x, u32 y) 26{ 27 return &world->map[y*world->width + x]; 28} 29 30void kf_world_drawcolliders(struct kf_world *world, struct kf_actor *player, Camera2D camera) 31{ 32 Rectangle r = { 33 player->pos.x + (player->size.x / 2) + player->sizeoffset.x, 34 player->pos.y + (player->size.y / 2) + player->sizeoffset.y, 35 player->size.x, 36 player->size.y 37 }; 38 39 DrawRectangleLinesEx(r, 1, BLUE); 40 41 const Vector2 start = GetScreenToWorld2D((Vector2){0, 0}, camera); 42 const Vector2 end = GetScreenToWorld2D((Vector2){GetScreenWidth(), GetScreenHeight()}, camera); 43 const u32 sx = fmax(0, floorf(start.x / KF_TILE_SIZE_PX)); 44 const u32 sy = fmax(0, floorf(start.y / KF_TILE_SIZE_PX)); 45 const u32 ex = fmin(world->width, ceilf(end.x / KF_TILE_SIZE_PX)); 46 const u32 ey = fmin(world->height, ceilf(end.y / KF_TILE_SIZE_PX)); 47 const size_t down = world->width - ex + sx - 1; /* number of indexes to add to reach the next tile down */ 48 kf_tileid_t *tile = kf_world_gettile(world, sx, sy); 49 50 /* check if any tiles will collide with the actor's rect */ 51 const f32 trx = sx * KF_TILE_SIZE_PX; 52 Rectangle tr = { 53 trx, 54 sy * KF_TILE_SIZE_PX, 55 KF_TILE_SIZE_PX, 56 KF_TILE_SIZE_PX, 57 }; /* tile rect */ 58 u32 x; 59 60 for (u32 y = sy ; y <= ey ; y++) 61 { 62 for (x = sx ; x <= ex ; x++) 63 { 64 if (kf_tiles.collide[*tile]) 65 DrawRectangleLinesEx(tr, 1, CheckCollisionRecs(r, tr) ? RED : BLACK); 66 67 tile++; /* shift tile pointer to the right */ 68 tr.x += KF_TILE_SIZE_PX; 69 } 70 tile += down; /* shift tile pointer down */ 71 tr.x = trx; 72 tr.y += KF_TILE_SIZE_PX; 73 } 74} 75 76void kf_world_draw(struct kf_world *world, Camera2D camera) 77{ 78 const Vector2 start = GetScreenToWorld2D((Vector2){0, 0}, camera); 79 const Vector2 end = GetScreenToWorld2D((Vector2){GetScreenWidth(), GetScreenHeight()}, camera); 80 const u32 sx = fmax(0, floorf(start.x / KF_TILE_SIZE_PX)); 81 const u32 sy = fmax(0, floorf(start.y / KF_TILE_SIZE_PX)); 82 const u32 ex = fmin(world->width, ceilf(end.x / KF_TILE_SIZE_PX)); 83 const u32 ey = fmin(world->height, ceilf(end.y / KF_TILE_SIZE_PX)); 84 const size_t down = world->width - ex + sx; /* number of indexes to add to reach the next tile down */ 85 kf_tileid_t *tile = kf_world_gettile(world, sx, sy); 86 u32 x; 87 for (u32 y = sy ; y < ey ; y++) 88 { 89 for (x = sx ; x < ex ; x++) 90 { 91 DrawRectangle( 92 (int)x * KF_TILE_SIZE_PX, 93 (int)y * KF_TILE_SIZE_PX, 94 KF_TILE_SIZE_PX, 95 KF_TILE_SIZE_PX, 96 kf_tiles.color[*tile] 97 ); 98 tile++; /* shift tile pointer to the right */ 99 } 100 tile += down; /* shift tile pointer down */ 101 } 102}