From 6d2565adf25bf7f99f22f4e1b8d39f5c73ceccea Mon Sep 17 00:00:00 2001 From: Wojciech Graj Date: Tue, 23 Aug 2022 23:56:55 +0200 Subject: [PATCH] Embed resources in binary. Improve makefile. --- Makefile | 70 ++++++++++++++++++++---------------------- src/entity/earth.c | 34 ++++++++++++++------ src/entity/satellite.c | 13 +++++--- src/entity/sky.c | 18 ++++++++--- src/gfx/shader.c | 33 +++++--------------- src/gfx/shader.h | 2 +- src/gfx/texture.c | 6 ++-- src/gfx/texture.h | 2 +- src/ui/perf.c | 2 +- src/ui/toolbar.c | 2 -- src/ui/ui.c | 9 +++--- 11 files changed, 98 insertions(+), 93 deletions(-) diff --git a/Makefile b/Makefile index a9319ee..a273b12 100755 --- a/Makefile +++ b/Makefile @@ -1,14 +1,11 @@ TARGET := orbvis -HAS_PACKAGES := $(shell pkg-config --exists gtk+-3.0 cglm libcurl epoxy ; echo $$?) -ifneq ($(HAS_PACKAGES), 0) - $(error Missing packages) -endif - CC := gcc WARNINGS := -Wall -Wextra -Wpedantic -Wdouble-promotion -Wstrict-prototypes -Wshadow -Wduplicated-cond -Wduplicated-branches -Wjump-misses-init -Wnull-dereference -Wrestrict -Wlogical-op -Walloc-zero -Wformat-security -Wformat-signedness -Winit-self -Wlogical-op -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wswitch-enum -Wundef -Wwrite-strings -Wno-discarded-qualifiers -CFLAGS := -O3 `pkg-config --cflags gtk+-3.0 cglm libcurl epoxy` +CFLAGS := -O3 `pkg-config --cflags gtk+-3.0 cglm libcurl epoxy` -std=c11 LDFLAGS := -lm +COMMANDS := $(CC) pkg-config xxd sed +PACKAGES := gtk+-3.0 cglm libcurl epoxy ifeq ($(MAKECMDGOALS),windows) CFLAGS += -march=core2 -flto @@ -22,6 +19,8 @@ else CFLAGS += -flto LDFLAGS += -flto ifeq ($(MAKECMDGOALS),release) + CFLAGS += -march=core2 + else CFLAGS += -march=native endif endif @@ -30,49 +29,50 @@ endif BUILD_DIR := ./obj SRC_DIRS := ./src LIB_DIRS := ./lib +RES_DIRS := ./res .PHONY: clean -SRC_HASHMAP := lib/hashmap_c/hashmap.c -SRC_STB_IMAGE := lib/stb_image/stb_image.c -SRCS_SGP4 := $(shell find lib/sgp4 -name '*.c') -SRCS_SATCAT := $(shell find lib/satcat -name '*.c') - +LIB_SRCS := $(shell find $(LIB_DIRS) -name '*.c') SRCS := $(shell find $(SRC_DIRS) -name '*.c') +RES := $(shell find $(RES_DIRS) -type f) +RES_SRCS := $(RES:%=$(BUILD_DIR)/%.c) -OBJ_HASHMAP = $(BUILD_DIR)/lib/hashmap_c/hashmap.c.o -OBJ_STB_IMAGE = $(BUILD_DIR)/lib/stb_image/stb_image.c.o -OBJS_SGP4 = $(addprefix $(BUILD_DIR)/,$(addsuffix .o,$(SRCS_SGP4))) -OBJS_SATCAT = $(addprefix $(BUILD_DIR)/,$(addsuffix .o,$(SRCS_SATCAT))) -OBJS := $(OBJ_HASHMAP) $(OBJ_STB_IMAGE) $(OBJS_SGP4) $(OBJS_SATCAT) $(SRCS:%=$(BUILD_DIR)/%.o) - -DEPS := $(OBJS:.o=.d) +OBJS := $(RES_SRCS:$(BUILD_DIR)/%=$(BUILD_DIR)/%.r.o) $(SRCS:%=$(BUILD_DIR)/%.s.o) $(LIB_SRCS:%=$(BUILD_DIR)/%.l.o) INC_DIRS := $(shell find $(SRC_DIRS) $(LIB_DIRS) -type d) INC_FLAGS := $(addprefix -I,$(INC_DIRS)) -$(TARGET): $(OBJS) $(LIBS) +$(TARGET): $(COMMANDS) $(PACKAGES) $(OBJS) $(CC) $(OBJS) -o $@ $(LDFLAGS) -$(OBJ_HASHMAP): - mkdir -p $(BUILD_DIR)/lib/hashmap_c - $(CC) -o $(OBJ_HASHMAP) -std=c99 $(CFLAGS) -Ilib/hashmap_c -c $(SRC_HASHMAP) +$(COMMANDS): + @if ! [ $$(command -v $@) ]; then \ + echo "Missing command: $@"; \ + false; \ + fi + +$(PACKAGES): + @if ! pkg-config --exists $@; then \ + echo "Missing package: $@"; \ + false; \ + fi -$(OBJ_STB_IMAGE): - mkdir -p $(BUILD_DIR)/lib/stb_image - $(CC) -o $(OBJ_STB_IMAGE) -std=c99 $(CFLAGS) -Ilib/stb_image -c $(SRC_STB_IMAGE) +$(BUILD_DIR)/%.c.l.o: %.c + mkdir -p $(@D) + $(CC) -o $@ $(CFLAGS) -I$( $@ -$(BUILD_DIR)/%.c.o: %.c - mkdir -p $(dir $@) - $(CC) $(INC_FLAGS) -std=c11 $(CFLAGS) $(WARNINGS) -c $< -o $@ +%.c.r.o: %.c + mkdir -p $(@D) + $(CC) $(INC_FLAGS) $(CFLAGS) $(WARNINGS) -c $< -o $@ clean: rm -rf $(BUILD_DIR) @@ -81,5 +81,3 @@ clean: windows: $(TARGET) debug: $(TARGET) release: $(TARGET) - --include $(DEPS) diff --git a/src/entity/earth.c b/src/entity/earth.c index 3135f6a..d1c603f 100644 --- a/src/entity/earth.c +++ b/src/entity/earth.c @@ -46,6 +46,22 @@ enum UniformLoc { LOCU_LIGHTING, }; +extern const unsigned char res_shader_earth_vert[]; +extern const unsigned int res_shader_earth_vert_len; +extern const unsigned char res_shader_earth_frag[]; +extern const unsigned int res_shader_earth_frag_len; + +extern const unsigned char res_texture_earth_daymap_jpg[]; +extern const unsigned int res_texture_earth_daymap_jpg_len; +extern const unsigned char res_texture_earth_nightmap_jpg[]; +extern const unsigned int res_texture_earth_nightmap_jpg_len; +extern const unsigned char res_texture_earth_specular_map_jpg[]; +extern const unsigned int res_texture_earth_specular_map_jpg_len; +extern const unsigned char res_texture_earth_clouds_jpg[]; +extern const unsigned int res_texture_earth_clouds_jpg_len; +extern const unsigned char res_texture_sky_gradient_jpg[]; +extern const unsigned int res_texture_sky_gradient_jpg_len; + struct IdxObj obj_earth; struct VAO vao_earth; static struct Model model; @@ -66,16 +82,16 @@ void earth_init(void) idx_obj_init(&obj_earth); model_init(&model, &obj_earth); - shader_init(&shader, "res/shader/earth.vert", "res/shader/earth.frag", 2, (struct ShaderAttr[]){ - { LOCL_APOS, "in_pos" }, - { LOCL_ATEXCOORD, "in_uv" }, - }); + shader_init(&shader, res_shader_earth_vert, res_shader_earth_vert_len, res_shader_earth_frag, res_shader_earth_frag_len, 2, (struct ShaderAttr[]){ + { LOCL_APOS, "in_pos" }, + { LOCL_ATEXCOORD, "in_uv" }, + }); - texture_init_from_image(&texture_day, "res/texture/earth_daymap.jpg", GL_TEXTURE0, GL_TEXTURE_2D); - texture_init_from_image(&texture_night, "res/texture/earth_nightmap.jpg", GL_TEXTURE1, GL_TEXTURE_2D); - texture_init_from_image(&texture_specular, "res/texture/earth_specular_map.jpg", GL_TEXTURE2, GL_TEXTURE_2D); - texture_init_from_image(&texture_clouds, "res/texture/earth_clouds.jpg", GL_TEXTURE3, GL_TEXTURE_2D); - texture_init_from_image(&texture_clouds, "res/texture/sky_gradient.jpg", GL_TEXTURE4, GL_TEXTURE_1D); + texture_init_from_image(&texture_day, res_texture_earth_daymap_jpg, res_texture_earth_daymap_jpg_len, GL_TEXTURE0, GL_TEXTURE_2D); + texture_init_from_image(&texture_night, res_texture_earth_nightmap_jpg, res_texture_earth_nightmap_jpg_len, GL_TEXTURE1, GL_TEXTURE_2D); + texture_init_from_image(&texture_specular, res_texture_earth_specular_map_jpg, res_texture_earth_specular_map_jpg_len, GL_TEXTURE2, GL_TEXTURE_2D); + texture_init_from_image(&texture_clouds, res_texture_earth_clouds_jpg, res_texture_earth_clouds_jpg_len, GL_TEXTURE3, GL_TEXTURE_2D); + texture_init_from_image(&texture_gradient, res_texture_sky_gradient_jpg, res_texture_sky_gradient_jpg_len, GL_TEXTURE4, GL_TEXTURE_1D); vao_attr(&vao_earth, &obj_earth.vbo_verts, 0, 3, GL_FLOAT, sizeof(vec3), 0); vao_attr(&vao_earth, &obj_earth.vbo_uv, 1, 2, GL_FLOAT, sizeof(vec2), 0); diff --git a/src/entity/satellite.c b/src/entity/satellite.c index cb9bed3..467697a 100644 --- a/src/entity/satellite.c +++ b/src/entity/satellite.c @@ -69,6 +69,11 @@ struct OrbitData { gint64 max_delta_ms; }; +extern const unsigned char res_shader_satellite_vert[]; +extern const unsigned int res_shader_satellite_vert_len; +extern const unsigned char res_shader_satellite_frag[]; +extern const unsigned int res_shader_satellite_frag_len; + static struct DLMulti dl_multi; static struct VAO vao_satellites; @@ -145,10 +150,10 @@ void satellite_init(void) vao_attr(&vao_orbits, &vbo_orbits, LOCL_APOS, 3, GL_FLOAT, sizeof(vec3), 0); vao_attr(&vao_orbits, &vbo_orbit_colors, LOCL_COLOR, 3, GL_FLOAT, sizeof(vec3), 0); - shader_init(&shader, "res/shader/satellite.vert", "res/shader/satellite.frag", 2, (struct ShaderAttr[]){ - { LOCL_APOS, "in_pos" }, - { LOCL_COLOR, "in_color" }, - }); + shader_init(&shader, res_shader_satellite_vert, res_shader_satellite_vert_len, res_shader_satellite_frag, res_shader_satellite_frag_len, 2, (struct ShaderAttr[]){ + { LOCL_APOS, "in_pos" }, + { LOCL_COLOR, "in_color" }, + }); alloc_orbit_arrays(); diff --git a/src/entity/sky.c b/src/entity/sky.c index 132b305..3b65d07 100644 --- a/src/entity/sky.c +++ b/src/entity/sky.c @@ -37,6 +37,14 @@ enum UniformLoc { LOCU_TRANSFORM, }; +extern const unsigned char res_shader_sky_vert[]; +extern const unsigned int res_shader_sky_vert_len; +extern const unsigned char res_shader_sky_frag[]; +extern const unsigned int res_shader_sky_frag_len; + +extern const unsigned char res_texture_stars_jpg[]; +extern const unsigned int res_texture_stars_jpg_len; + static struct IdxObj obj; static struct VAO vao; static struct Model model; @@ -53,12 +61,12 @@ void sky_init(void) glm_scale_make(model.scl, (vec3){ 50.f, 50.f, 50.f }); model_transform(&model); - shader_init(&shader, "res/shader/sky.vert", "res/shader/sky.frag", 2, (struct ShaderAttr[]){ - { LOCL_APOS, "in_pos" }, - { LOCL_ATEXCOORD, "in_uv" }, - }); + shader_init(&shader, res_shader_sky_vert, res_shader_sky_vert_len, res_shader_sky_frag, res_shader_sky_frag_len, 2, (struct ShaderAttr[]){ + { LOCL_APOS, "in_pos" }, + { LOCL_ATEXCOORD, "in_uv" }, + }); - texture_init_from_image(&texture, "res/texture/stars.jpg", GL_TEXTURE0, GL_TEXTURE_2D); + texture_init_from_image(&texture, res_texture_stars_jpg, res_texture_stars_jpg_len, GL_TEXTURE0, GL_TEXTURE_2D); vao_attr(&vao, &obj.vbo_verts, 0, 3, GL_FLOAT, sizeof(vec3), 0); vao_attr(&vao, &obj.vbo_uv, 1, 2, GL_FLOAT, sizeof(vec2), 0); diff --git a/src/gfx/shader.c b/src/gfx/shader.c index 1068d35..1a09325 100644 --- a/src/gfx/shader.c +++ b/src/gfx/shader.c @@ -14,48 +14,29 @@ #include "shader.h" -#include - #include "error.h" #include "error_gfx.h" #include "system.h" -static GLint shader_compile(char *path, GLenum type); +static GLint shader_compile(char *str, unsigned int len, GLenum type); -static GLint shader_compile(char *path, GLenum type) +static GLint shader_compile(char *str, unsigned int len, GLenum type) { - FILE *f; - char *buf; - long len; - size_t nmemb_read; - - f = fopen(path, "rb"); - error_check(f, "Error opening shader at [%s].", path); - - len = fsize(f); - error_check(len > 0, "Failed to get size of shader at [%s].", path); - - buf = g_malloc((size_t)len); - nmemb_read = fread(buf, sizeof(char), len, f); - error_check(nmemb_read, "Failed to read shader at [%s].", path); - fclose(f); - GLuint handle = glCreateShader(type); - glShaderSource(handle, 1, (const GLchar *const *)&buf, (const GLint *)&len); + glShaderSource(handle, 1, (const GLchar *const *)&str, (const GLint *)&len); glCompileShader(handle); GLint compiled; glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled); - gfx_error_check(compiled, glGetShaderInfoLog, handle, "Failed to compile shader at [%s].", path); + gfx_error_check(compiled, glGetShaderInfoLog, handle, "Failed to compile shader."); - g_free(buf); return handle; } -void shader_init(struct Shader *shader, char *vs_path, char *fs_path, size_t n_vertex_attr, struct ShaderAttr vertex_attr[]) +void shader_init(struct Shader *shader, char *vs, unsigned int vs_len, char *fs, unsigned int fs_len, size_t n_vertex_attr, struct ShaderAttr vertex_attr[]) { - GLuint vs_handle = shader_compile(vs_path, GL_VERTEX_SHADER); - GLuint fs_handle = shader_compile(fs_path, GL_FRAGMENT_SHADER); + GLuint vs_handle = shader_compile(vs, vs_len, GL_VERTEX_SHADER); + GLuint fs_handle = shader_compile(fs, fs_len, GL_FRAGMENT_SHADER); shader->handle = glCreateProgram(); glAttachShader(shader->handle, vs_handle); diff --git a/src/gfx/shader.h b/src/gfx/shader.h index 3cf257c..52ba5fc 100644 --- a/src/gfx/shader.h +++ b/src/gfx/shader.h @@ -26,7 +26,7 @@ struct Shader { GLuint handle; }; -void shader_init(struct Shader *shader, char *vs_path, char *fs_path, size_t n_vertex_attr, struct ShaderAttr vertex_attr[]); +void shader_init(struct Shader *shader, char *vs, unsigned int vs_len, char *fs, unsigned int fs_len, size_t n_vertex_attr, struct ShaderAttr vertex_attr[]); void shader_deinit(struct Shader *shader); void shader_bind(struct Shader *shader); diff --git a/src/gfx/texture.c b/src/gfx/texture.c index 4b51cc6..250c653 100644 --- a/src/gfx/texture.c +++ b/src/gfx/texture.c @@ -27,12 +27,12 @@ void texture_init(struct Texture *texture, GLenum unit, GLenum type) glBindTexture(type, texture->handle); } -void texture_init_from_image(struct Texture *texture, const char *filename, GLenum unit, GLenum type) +void texture_init_from_image(struct Texture *texture, unsigned char *buffer, unsigned int len, GLenum unit, GLenum type) { int width, height, n_channels; stbi_set_flip_vertically_on_load(TRUE); /* TODO: Move to init */ - unsigned char *data = stbi_load(filename, &width, &height, &n_channels, 0); - error_check(data, "Failed to open texture [%s].", filename); + unsigned char *data = stbi_load_from_memory(buffer, len, &width, &height, &n_channels, 0); + error_check(data, "Failed to open texture."); texture_init(texture, unit, type); diff --git a/src/gfx/texture.h b/src/gfx/texture.h index 7bf6895..f1bdf61 100644 --- a/src/gfx/texture.h +++ b/src/gfx/texture.h @@ -25,7 +25,7 @@ struct Texture { void texture_init(struct Texture *texture, GLenum unit, GLenum type); -void texture_init_from_image(struct Texture *texture, const char *filename, GLenum unit, GLenum type); +void texture_init_from_image(struct Texture *texture, unsigned char *buffer, unsigned int len, GLenum unit, GLenum type); void texture_deinit(struct Texture *texture); diff --git a/src/ui/perf.c b/src/ui/perf.c index d906321..7b4221a 100644 --- a/src/ui/perf.c +++ b/src/ui/perf.c @@ -62,7 +62,7 @@ void perf_tic(void) frame_idx = (frame_idx + 1) % NUM_FRAMETIMES; if (visible) { - gchar *text = g_strdup_printf("%.1f", (NUM_FRAMETIMES * 1000.f) / frametime_total); + gchar *text = g_strdup_printf("%.1f", (NUM_FRAMETIMES * 1000.0) / frametime_total); gtk_label_set_text(fps_lbl, text); g_free(text); } diff --git a/src/ui/toolbar.c b/src/ui/toolbar.c index 87ad551..085f4fc 100644 --- a/src/ui/toolbar.c +++ b/src/ui/toolbar.c @@ -19,8 +19,6 @@ #include "setting.h" #include "system.h" -#include - #define ZOOM_DIST 0.2 static GtkToggleToolButton *play_button; diff --git a/src/ui/ui.c b/src/ui/ui.c index d920ce4..9d9ffc5 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -29,6 +29,9 @@ #include "thread.h" #include "toolbar.h" +extern const unsigned char res_ui_ui_glade[]; +extern const unsigned int res_ui_ui_glade_len; + const gchar *FILENAME_GUI = "res/ui/ui.glade"; GtkWindow *e_window_main; @@ -62,11 +65,7 @@ void ui_init(int argc, char ***argv) { gtk_init(&argc, argv); - GError *err = NULL; - GtkBuilder *builder = gtk_builder_new(); - - gtk_builder_add_from_file(builder, FILENAME_GUI, &err); - error_check(!err, "Failed to open GLADE UI file"); + GtkBuilder *builder = gtk_builder_new_from_string(res_ui_ui_glade, res_ui_ui_glade_len); gtk_builder_add_callback_symbols(builder, "on_window_main_destroy", G_CALLBACK(on_window_main_destroy),