Add assets loading from a file
Changelog: - Add assets_loader functions from a file - Update scene_test to use loader functionscene_man
parent
57c9eb0216
commit
0f485d89f2
12
scene_test.c
12
scene_test.c
|
@ -1,5 +1,6 @@
|
|||
#include "mempool.h"
|
||||
#include "scene_impl.h"
|
||||
#include "assets_loader.h"
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -23,16 +24,7 @@ int main(void)
|
|||
init_memory_pools();
|
||||
|
||||
init_assets(&engine.assets);
|
||||
Texture2D* tex = add_texture(&engine.assets, "plr_tex", "res/bunny_stand.png");
|
||||
Sprite_t* spr = add_sprite(&engine.assets, "plr_stand", tex);
|
||||
spr->origin = (Vector2){0, 0};
|
||||
spr->frame_size = (Vector2){32, 64};
|
||||
|
||||
spr = add_sprite(&engine.assets, "plr_run", tex);
|
||||
spr->frame_count = 1;
|
||||
spr->origin = (Vector2){0, 0};
|
||||
spr->frame_size = (Vector2){32, 64};
|
||||
spr->speed = 30;
|
||||
load_from_infofile("res/assets.info", &engine.assets);
|
||||
|
||||
LevelScene_t scene;
|
||||
scene.scene.engine = &engine;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
add_subdirectory(engine)
|
||||
add_library(lib_scenes STATIC
|
||||
assets_loader.c
|
||||
player_ent.c
|
||||
items_ent.c
|
||||
editor_scene.c
|
||||
|
|
|
@ -0,0 +1,125 @@
|
|||
#include "assets_loader.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef enum AssetInfoType
|
||||
{
|
||||
TEXTURE_INFO,
|
||||
SPRITE_INFO,
|
||||
INVALID_INFO
|
||||
}AssetInfoType_t;
|
||||
|
||||
typedef struct SpriteInfo
|
||||
{
|
||||
char* tex;
|
||||
Vector2 origin;
|
||||
Vector2 frame_size;
|
||||
int speed;
|
||||
int frame_count;
|
||||
}SpriteInfo_t;
|
||||
|
||||
static bool parse_sprite_info(char* sprite_info_str, SpriteInfo_t* spr_info)
|
||||
{
|
||||
char* tex_name = strtok(sprite_info_str, ",");
|
||||
if (tex_name == NULL) return false;
|
||||
spr_info->tex = tex_name;
|
||||
char* spr_data = tex_name + strlen(tex_name) + 1;
|
||||
int data_count = sscanf(
|
||||
spr_data, "%f,%f,%f,%f,%d,%d",
|
||||
&spr_info->origin.x, &spr_info->origin.y,
|
||||
&spr_info->frame_size.x, &spr_info->frame_size.y,
|
||||
&spr_info->frame_count, &spr_info->speed
|
||||
);
|
||||
return data_count == 6;
|
||||
}
|
||||
|
||||
bool load_from_infofile(const char* file, Assets_t* assets)
|
||||
{
|
||||
FILE* in_file = fopen(file, "r");
|
||||
if (in_file == NULL)
|
||||
{
|
||||
printf("Unable to open file %s\n", file);
|
||||
return false;
|
||||
}
|
||||
|
||||
static char buffer[256];
|
||||
char* tmp;
|
||||
char tmp2[32];
|
||||
size_t line_num = 0;
|
||||
AssetInfoType_t info_type = INVALID_INFO;
|
||||
|
||||
while (true)
|
||||
{
|
||||
tmp = fgets(buffer, 256, in_file);
|
||||
if (tmp == NULL) break;
|
||||
tmp[strcspn(tmp, "\r\n")] = '\0';
|
||||
|
||||
if (tmp[0] == '-')
|
||||
{
|
||||
tmp++;
|
||||
if (strcmp(tmp, "Texture") == 0)
|
||||
{
|
||||
info_type = TEXTURE_INFO;
|
||||
}
|
||||
else if (strcmp(tmp, "Sprite") == 0)
|
||||
{
|
||||
info_type = SPRITE_INFO;
|
||||
}
|
||||
else
|
||||
{
|
||||
info_type = INVALID_INFO;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char* name = strtok(buffer, ":");
|
||||
char* info_str = strtok(NULL, ":");
|
||||
if (name == NULL || info_str == NULL) continue;
|
||||
|
||||
while(*name == ' ' || *name == '\t') name++;
|
||||
while(*info_str == ' ' || *info_str == '\t') info_str++;
|
||||
switch(info_type)
|
||||
{
|
||||
case TEXTURE_INFO:
|
||||
{
|
||||
if (add_texture(assets, name, info_str) == NULL)
|
||||
{
|
||||
printf("Unable to add texture at line %lu\n", line_num);
|
||||
break;
|
||||
}
|
||||
printf("Added texture %s as %s\n", info_str, name);
|
||||
//strcpy(tmp2, name);
|
||||
}
|
||||
break;
|
||||
case SPRITE_INFO:
|
||||
{
|
||||
SpriteInfo_t spr_info = {0};
|
||||
if (!parse_sprite_info(info_str, &spr_info))
|
||||
{
|
||||
printf("Unable to parse info for sprite at line %lu\n", line_num);
|
||||
break;
|
||||
}
|
||||
//printf("Compare %s,%s = %d\n", tmp2, spr_info.tex, strcmp(tmp2, spr_info.tex));
|
||||
Texture2D* tex = get_texture(assets, spr_info.tex);
|
||||
if (tex == NULL)
|
||||
{
|
||||
printf("Unable to get texture info %s for sprite %s\n", spr_info.tex, name);
|
||||
break;
|
||||
}
|
||||
printf("Added Sprite %s from texture %s\n", name, spr_info.tex);
|
||||
Sprite_t* spr = add_sprite(assets, name, tex);
|
||||
spr->origin = spr_info.origin;
|
||||
spr->frame_size = spr_info.frame_size;
|
||||
spr->frame_count = spr_info.frame_count;
|
||||
spr->speed = spr_info.speed;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
line_num++;
|
||||
}
|
||||
fclose(in_file);
|
||||
return true;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
#ifndef __ASSETS_LOADER_H
|
||||
#define __ASSETS_LOADER_H
|
||||
#include "assets.h"
|
||||
|
||||
bool load_from_infofile(const char* file, Assets_t* assets);
|
||||
|
||||
#endif // __ASSETS_LOADER_H
|
Loading…
Reference in New Issue