Asset loading with metadesk

main
Cameron Murphy Reikes 2 years ago
parent e3186e68f9
commit 68bd1d5d68

@ -0,0 +1,4 @@
@image merchant:
{
filepath = "merchant.png",
}

@ -2,7 +2,7 @@
@REM https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category?view=msvc-170 @REM https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category?view=msvc-170
call shadergen.bat || goto :error call run_codegen.bat || goto :error
cl /DDEVTOOLS /Igen /Ithirdparty /W3 /Zi /WX main.c || goto :error cl /DDEVTOOLS /Igen /Ithirdparty /W3 /Zi /WX main.c || goto :error
goto :EOF goto :EOF

@ -0,0 +1,83 @@
#include <stdio.h>
#define assert(cond, explanation) { if(!cond) { printf("Codegen assertion %s failed: %.*s\n", #cond, MD_S8VArg(explanation)); exit(1); } }
#pragma warning(disable : 4996) // nonsense about fopen being insecure
#pragma warning(push)
#pragma warning(disable : 4244) // loss of data warning
#pragma warning(disable : 4101) // unreferenced local variable
#include "md.h"
#include "md.c"
#pragma warning(pop)
MD_String8 OUTPUT_FOLDER = MD_S8LitComp("gen"); // no trailing slash
MD_String8 ASSETS_FOLDER = MD_S8LitComp("assets");
#define log(...) { printf("Codegen: "); printf(__VA_ARGS__); }
void dump(MD_ParseResult parse) {
// Iterate through each top-level node
for(MD_EachNode(node, parse.node->first_child))
{
printf("/ %.*s\n", MD_S8VArg(node->string));
// Print the name of each of the node's tags
for(MD_EachNode(tag, node->first_tag))
{
printf("|-- Tag %.*s\n", MD_S8VArg(tag->string));
}
// Print the name of each of the node's children
for(MD_EachNode(child, node->first_child))
{
printf("|-- Child %.*s\n", MD_S8VArg(child->string));
}
}
}
int main(int argc, char **argv) {
MD_Arena *cg_arena = MD_ArenaAlloc();
assert(cg_arena, MD_S8Lit("Memory"));
// I hope to God MD_String8's are null terminated...
MD_String8 writeto = MD_S8Fmt(cg_arena, "%.*s/assets.gen.c", MD_S8VArg(OUTPUT_FOLDER));
log("Writing to %.*s\n", MD_S8VArg(writeto));
FILE *output = fopen(writeto.str, "w");
MD_ParseResult parse = MD_ParseWholeFile(cg_arena, MD_S8Lit("assets.mdesk"));
//dump(parse);
MD_String8List declarations_list = {0};
MD_String8List load_list = {0};
for(MD_EachNode(node, parse.node->first_child)) {
if(MD_S8Match(node->first_tag->string, MD_S8Lit("image"), 0)) {
MD_String8 variable_name = MD_S8Fmt(cg_arena, "image_%.*s", MD_S8VArg(node->string));
log("New image variable %.*s\n", MD_S8VArg(variable_name));
MD_String8 filepath = {0};
for(MD_EachNode(child, node->first_child)) {
if(MD_S8Match(child->string, MD_S8Lit("filepath"), 0)) {
filepath = child->next->next->string;
}
}
filepath = MD_S8Fmt(cg_arena, "%.*s/%.*s", MD_S8VArg(ASSETS_FOLDER), MD_S8VArg(filepath));
assert(filepath.str != 0, MD_S8Fmt(cg_arena, "No filepath specified for image '%.*s'", MD_S8VArg(node->string)));
FILE *asset_file = fopen(filepath.str, "r");
assert(asset_file, MD_S8Fmt(cg_arena, "Could not open filepath %.*s for asset '%.*s'", MD_S8VArg(filepath), MD_S8VArg(node->string)));
fclose(asset_file);
MD_S8ListPush(cg_arena, &declarations_list, MD_S8Fmt(cg_arena, "sg_image %.*s = {0};\n", MD_S8VArg(variable_name)));
MD_S8ListPush(cg_arena, &load_list, MD_S8Fmt(cg_arena, "%.*s = load_image(\"%.*s\");\n", MD_S8VArg(variable_name), MD_S8VArg(filepath)));
}
}
MD_StringJoin join = MD_ZERO_STRUCT;
MD_String8 declarations = MD_S8ListJoin(cg_arena, declarations_list, &join);
MD_String8 loads = MD_S8ListJoin(cg_arena, load_list, &join);
fprintf(output, "%.*s\nvoid load_assets() {\n%.*s\n}", MD_S8VArg(declarations), MD_S8VArg(loads));
fclose(output);
return 0;
}

@ -6,41 +6,23 @@
#include "sokol_gfx.h" #include "sokol_gfx.h"
#include "sokol_time.h" #include "sokol_time.h"
#include "sokol_glue.h" #include "sokol_glue.h"
#include "quad-sapp.glsl.h"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
#include "HandMadeMath.h" #include "HandMadeMath.h"
#include <math.h> #include <math.h>
// so can be grep'd and removed sg_image load_image(const char *path) {
#define dbgprint(...) { printf("Debug | %s:%d | ", __FILE__, __LINE__); printf(__VA_ARGS__); } sg_image to_return = {0};
static struct {
sg_pass_action pass_action;
sg_pipeline pip;
sg_bindings bind;
} state;
sg_image merchant_tilesheet = {0};
void init(void) {
stm_setup();
sg_setup(&(sg_desc){
.context = sapp_sgcontext()
});
// load the image
int png_width, png_height, num_channels; int png_width, png_height, num_channels;
const int desired_channels = 4; const int desired_channels = 4;
stbi_uc* pixels = stbi_load( stbi_uc* pixels = stbi_load(
"assets/merchant.png", path,
&png_width, &png_height, &png_width, &png_height,
&num_channels, 0); &num_channels, 0);
assert(pixels); assert(pixels);
{ to_return = sg_make_image(&(sg_image_desc){
merchant_tilesheet = sg_make_image(&(sg_image_desc){
.width = png_width, .width = png_width,
.height = png_height, .height = png_height,
.pixel_format = SG_PIXELFORMAT_RGBA8, .pixel_format = SG_PIXELFORMAT_RGBA8,
@ -52,8 +34,29 @@ void init(void) {
} }
}); });
stbi_image_free(pixels); stbi_image_free(pixels);
return to_return;
} }
#include "quad-sapp.glsl.h"
#include "assets.gen.c"
// so can be grep'd and removed
#define dbgprint(...) { printf("Debug | %s:%d | ", __FILE__, __LINE__); printf(__VA_ARGS__); }
static struct {
sg_pass_action pass_action;
sg_pipeline pip;
sg_bindings bind;
} state;
void init(void) {
stm_setup();
sg_setup(&(sg_desc){
.context = sapp_sgcontext()
});
load_assets();
state.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ state.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.usage = SG_USAGE_STREAM, .usage = SG_USAGE_STREAM,
//.data = SG_RANGE(vertices), //.data = SG_RANGE(vertices),
@ -187,7 +190,7 @@ void frame(void) {
int index = (int)floor(time/0.3); int index = (int)floor(time/0.3);
sg_image_info info = sg_query_image_info(merchant_tilesheet); sg_image_info info = sg_query_image_info(image_merchant);
int cell_size = 110; int cell_size = 110;
assert(info.width % cell_size == 0); assert(info.width % cell_size == 0);
@ -195,7 +198,7 @@ void frame(void) {
region.upper_left = HMM_V2( (float)((index % (info.width/cell_size)) * cell_size), 0.0); region.upper_left = HMM_V2( (float)((index % (info.width/cell_size)) * cell_size), 0.0);
region.lower_right = HMM_V2(region.upper_left.X + (float)cell_size, (float)cell_size); region.lower_right = HMM_V2(region.upper_left.X + (float)cell_size, (float)cell_size);
draw_quad_all_parameters(points, merchant_tilesheet, region, col(1.0, 1.0, 1.0, 1.0)); draw_quad_all_parameters(points, image_merchant, region, col(1.0, 1.0, 1.0, 1.0));
sg_end_pass(); sg_end_pass();
sg_commit(); sg_commit();

@ -1,8 +1,14 @@
@echo off @echo off
rmdir /S /q gen rmdir /S /q gen
mkdir gen mkdir gen
@REM shaders
thirdparty\sokol-shdc.exe --input quad.glsl --output gen\quad-sapp.glsl.h --slang glsl330:hlsl5:metal_macos || goto :error thirdparty\sokol-shdc.exe --input quad.glsl --output gen\quad-sapp.glsl.h --slang glsl330:hlsl5:metal_macos || goto :error
@REM metadesk codegen
cl /Ithirdparty /W3 /Zi /WX codegen.c || goto :error
codegen || goto :error
goto :EOF goto :EOF
:error :error

4410
thirdparty/md.c vendored

File diff suppressed because it is too large Load Diff

1248
thirdparty/md.h vendored

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save