replaced tilemap with world

pull/2/head
Sara 2023-05-05 23:20:07 +02:00
parent 851714810c
commit 465551e981
4 changed files with 97 additions and 123 deletions

View File

@ -1,82 +0,0 @@
#include "tilemap.h"
#include "corelib/layers.h"
#include "stdio.h"
#include "string.h"
#include "signal.h"
#include "stdio.h"
void draw_tilemap(const tilemap_t* tilemap, const tileset_t* tileset) {
sprite_t sprite;
for(int y=0;y<tilemap->height;++y) {
for(int x=0;x<tilemap->width;++x) {
size_t index = get_tile(tilemap, x, y);
if(index == 0) continue;
const tile_t tile = tileset->set[index-1];
sprite = tile.sprite;
sprite.depth = RLAYER_TILEMAP;
sprite.x = x; sprite.y = y;
draw_sprite(&sprite);
}
}
}
int save_tilemap(const tilemap_t* map, const char* file) {
FILE* fs = fopen(file, "w");
if(fs == NULL) {
fprintf(stderr, "ERROR: Failed to open file %s\n", file);
return 1;
}
fprintf(fs, "%d,%d,", map->width, map->height);
for(int i = 0; i < map->width * map->height; ++i)
fprintf(fs,"%zu,", map->tiles[i]);
fclose(fs);
return 0;
}
int load_tilemap(tilemap_t* map, const char* file) {
FILE* fs = fopen(file, "r");
if(fs == NULL) {
fprintf(stderr, "ERROR: Failed to open file %s\n", file);
return 1;
}
char buf[99];
char* writer = buf;
int n = 0;
size_t* tile = map->tiles;
int entries=0;
while(1) {
n = fgetc(fs);
const char c = (char)n;
switch(n) {
case ' ':
case '\n':
case '\t':
break; // ignore
case EOF:
case ',':
*writer = '\0';
if(entries == 0) {
map->width = atoi(buf);
} else if(entries == 1) {
map->height = atoi(buf);
} else {
if(entries-2 >= TILEMAP_MAX_SIZE) goto end_while;
*tile = (size_t)atoi(buf);
++tile;
}
writer = buf;
++entries;
if(n == EOF)
goto end_while;
else
break;
default:
*writer = c;
++writer;
break;
}
}
end_while:
fclose(fs);
return 0;
}

View File

@ -1,41 +0,0 @@
#ifndef _tilemap_h
#define _tilemap_h
#include "corelib/render.h"
#define TILEMAP_MAX_SIZE 16384
#define TILESET_MAX_SIZE 255
typedef struct tile_t {
sprite_t sprite;
short walkable;
} tile_t;
typedef struct tileset_t {
tile_t* set;
size_t size;
} tileset_t;
typedef struct tilemap_t {
size_t tiles[TILEMAP_MAX_SIZE];
int width, height;
float x, y;
} tilemap_t;
static inline int get_tilemap_index(const tilemap_t* tilemap, int x, int y) {
return x + y * tilemap->width;
}
static inline size_t get_tile(const tilemap_t* tilemap, int x, int y) {
return *(tilemap->tiles + get_tilemap_index(tilemap, x, y));
}
static inline void set_tile(tilemap_t* tilemap, int x, int y, size_t value) {
*(tilemap->tiles + get_tilemap_index(tilemap, x, y)) = value;
}
extern void draw_tilemap(const tilemap_t* tilemap, const tileset_t* tileset);
/*load csv tilemap from file*/
extern int load_tilemap(tilemap_t* tilemap, const char* file);
/*write csv tilemap to file*/
extern int save_tilemap(const tilemap_t* tilemap, const char* file);
#endif /* _tilemap_h */

60
src/world.c Normal file
View File

@ -0,0 +1,60 @@
#include "world.h"
object_t g_objects[WORLD_NUM_OBJECTS];
void world_clear() {
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
g_objects[i].active = 0;
g_objects[i].enabled = 0;
}
}
void object_draw_sprite(object_t* object) {
draw_sprite(&object->sprite);
}
object_t* _find_free_object() {
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
if(g_objects[i].active == 0) {
return g_objects + i;
}
}
return NULL;
}
object_t* make_object() {
object_t* o = _find_free_object();
o->active = 1;
o->enabled = 1;
o->evt_draw = &object_draw_sprite;
o->evt_update = NULL;
memset(&o->sprite, 0, sizeof(sprite_t));
return o;
}
object_t* instantiate_object(const object_t *original) {
object_t* obj = make_object();
*obj = *original;
obj->active = 1;
return obj;
}
void update_objects() {
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
if(g_objects[i].active == 1
&& g_objects[i].enabled == 1
&& g_objects[i].evt_update != NULL) {
g_objects[i].evt_update(g_objects + i);
}
}
}
void draw_objects() {
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
if(g_objects[i].active == 1
&& g_objects[i].enabled == 1
&& g_objects[i].evt_draw != NULL) {
g_objects[i].evt_draw(g_objects + i);
}
}
}

37
src/world.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef _world_h
#define _world_h
#include "render.h"
#include <stdint.h>
#define WORLD_NUM_OBJECTS 255
typedef struct object_t object_t;
typedef void(*tick_fn)(struct object_t*);
typedef void(*draw_fn)(struct object_t*);
struct object_t {
sprite_t sprite;
int active; // 1 if this object is in use and should not be overriden
int enabled; // 1 if this object's events should be triggered
uintptr_t timer; // free to use for whatever
tick_fn evt_update;
draw_fn evt_draw;
};
extern object_t g_objects[WORLD_NUM_OBJECTS];
void world_clear();
object_t* make_object();
object_t* instantiate_object(const object_t* original);
void object_draw_sprite(object_t* object);
void update_objects();
void draw_objects();
#endif /* _world_h */