2021-11-30 21:29:18 -06:00
|
|
|
#include "level.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "vec.h"
|
|
|
|
#include "gameobject.h"
|
2022-02-06 10:14:57 -06:00
|
|
|
#include "resources.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
void save_level(char name[MAXNAME])
|
|
|
|
{
|
2022-08-15 23:46:06 -05:00
|
|
|
FILE *lfile = res_open(name, "wb+");
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
if (!lfile) return;
|
|
|
|
|
|
|
|
|
|
|
|
int objs = gameobjects->len;
|
|
|
|
fwrite(&objs, sizeof(objs), 1, lfile);
|
|
|
|
|
|
|
|
for (int i = 0; i < objs; i++) {
|
|
|
|
gameobject_save(vec_get(gameobjects, i), lfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(lfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_level(char name[MAXNAME])
|
|
|
|
{
|
2022-08-15 23:46:06 -05:00
|
|
|
FILE *lfile = fopen(name, "rb");
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
if (!lfile) return;
|
|
|
|
|
|
|
|
|
|
|
|
int objs;
|
|
|
|
fread(&objs, sizeof(objs), 1, lfile);
|
|
|
|
|
|
|
|
vec_clear(gameobjects);
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < objs; i++) {
|
|
|
|
struct mGameObject *go = vec_add(gameobjects, NULL);
|
|
|
|
fread(go, sizeof(struct mGameObject), 1, lfile);
|
2022-08-15 23:46:06 -05:00
|
|
|
go->components = vec_make(1,1);
|
2021-11-30 21:29:18 -06:00
|
|
|
gameobject_init(go, lfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(lfile);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void new_level()
|
|
|
|
{
|
|
|
|
vec_clear(gameobjects);
|
|
|
|
}
|