polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit addcea9881d5d29c905a7af5a7a2a3953a3925bb
parent 69e6213e73924e4061b72cf343572a082dd7588a
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 22 Sep 2021 09:14:53 -0700

misc wasm related changes

Diffstat:
MMakefile | 2+-
Mdefault.nix | 3++-
Mmain.c | 4++--
Msrc/file.c | 4++--
Msrc/file.h | 2+-
Msrc/ply.c | 2+-
Msrc/shader.c | 2+-
Msrc/texture.c | 20+++++++++++++-------
Msrc/util.c | 4----
Msrc/util.h | 4+++-
10 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/Makefile b/Makefile @@ -17,7 +17,7 @@ BASE_CFLAGS = $(DEFS) -O2 -g -I src -Wall -Werror -Wextra -std=c99 \ $(shell pkg-config --cflags sdl2 gl) CFLAGS = $(BASE_CFLAGS) -EM_CFLAGS = $(BASE_CFLAGS) -s STANDALONE_WASM +EM_CFLAGS = $(BASE_CFLAGS) -s STANDALONE_WASM -mbulk-memory LDFLAGS = $(shell pkg-config --libs sdl2 gl) -lm SRC=src diff --git a/default.nix b/default.nix @@ -4,6 +4,7 @@ let pkgs = nixpkgs.pkgs; stdenv = pkgs.stdenv; + lib = nixpkgs.lib; in stdenv.mkDerivation rec { name = "polyadvent"; @@ -24,7 +25,7 @@ stdenv.mkDerivation rec { export LD_LIBRARY_PATH=${pkgs.libglvnd}/lib; ''; - meta = with stdenv.lib; { + meta = with lib; { description = "Procedural low poly fun"; homepage = "https://jb55.com/polyadvent"; maintainers = with maintainers; [ jb55 ]; diff --git a/main.c b/main.c @@ -84,8 +84,8 @@ int main(void) double new_time = hires_time_in_seconds(); double frame_time = new_time - last; game.dt = frame_time; - if (game.frame % 24 == 0) - printf("frame time %f\n", frame_time * 1000); + if (game.frame % 60 == 0) + printf("%f fps\n", 1000.0 / (frame_time*1000) ); update(&game); diff --git a/src/file.c b/src/file.c @@ -15,9 +15,9 @@ time_t file_mtime(const char *filename) { return stats.st_mtime; } -char *file_contents(const char *filename, size_t *length) { +unsigned char *file_contents(const char *filename, size_t *length) { FILE *f = fopen(filename, "r"); - char *buffer; + unsigned char *buffer; if (!f) { fprintf(stderr, "Unable to open %s for reading\n", filename); diff --git a/src/file.h b/src/file.h @@ -4,6 +4,6 @@ #include <time.h> time_t file_mtime(const char *filename); -char *file_contents(const char *filename, size_t *length); +unsigned char *file_contents(const char *filename, size_t *length); #endif /* POLYADVEMT_FILE_H */ diff --git a/src/ply.c b/src/ply.c @@ -129,7 +129,7 @@ int parse_ply_with_mkgeom(const char *filename, struct mdl_geometry *mdlgeom) struct make_geometry *mkgeom = &mdlgeom->mkgeom; enum ply_state state = PLY_MAGIC; - const char *data = file_contents(filename, &len); + const char *data = (const char*)file_contents(filename, &len); const char *p = data; float vert[3], norm[3], min[3]={0}, max[3]={0}; diff --git a/src/shader.c b/src/shader.c @@ -27,7 +27,7 @@ static char *cached_file_contents(const char *filename) { } } - return file_contents(filename, &len); + return (char*)file_contents(filename, &len); } static char *strsep(char **stringp, const char *delim) { diff --git a/src/texture.c b/src/texture.c @@ -4,24 +4,30 @@ #include "gl.h" #include "stb_image.h" #include "util.h" - - +#include "file.h" #include "texture.h" #include "common.h" u32 create_cubemap(const char *faces[6]) { u32 tid; int width, height, n_channels; + u8 *data, *img_data; + size_t data_len; + glGenTextures(1, &tid); glBindTexture(GL_TEXTURE_CUBE_MAP, tid); for (u32 i = 0; i < 6; i++) { const char *texture = faces[i]; - u8 *data = stbi_load(texture, &width, &height, &n_channels, 0); - if (data) { - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, - height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); - stbi_image_free(data); + data = file_contents(texture, &data_len); + img_data = stbi_load_from_memory(data, data_len, &width, &height, + &n_channels, 0); + + if (img_data) { + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, + height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data); + stbi_image_free(img_data); + free(data); } else { printf("cubemap texture failed to load: %s\n", texture); diff --git a/src/util.c b/src/util.c @@ -26,10 +26,6 @@ double clamp(double a, double mina, double maxa) { } -double rand_0to1() { - return (double) rand() / RAND_MAX; -} - void look_at(vec3 *eye, vec3 *target, vec3 *up, mat4 *dest) { float z[3], x[3], y[3]; diff --git a/src/util.h b/src/util.h @@ -38,6 +38,8 @@ void look_at(vec3 *eye, vec3 *target, vec3 *up, mat4 *dest); int clampi(int a, int mina, int maxa); double clamp(double a, double mina, double maxa); -double rand_0to1(); +static inline double rand_0to1() { + return (double) rand() / RAND_MAX; +} #endif /* PA_UTIL_H */