Bundled resources in bin (using GTK gresources)

This commit is contained in:
2024-03-25 04:03:33 +01:00
parent ae4ebd8e2b
commit 9593deceed
5 changed files with 108 additions and 21 deletions

View File

@@ -1,32 +1,60 @@
PROJECT_NAME = tiny-kyoukai
BUILD_DIR = build
SOURCE_DIR = src
HEADER_DIR = $(SOURCE_DIR)/includes
#------------------------------------------------------------------------------
# SETUP
#------------------------------------------------------------------------------
CC = gcc
CFLAGS = -std=c11 -g
LDFLAGS = -g
LIBS = gtk4 x11 gtk4-x11
LDLIBS = $(shell if [ -n "$(LIBS)" ]; then pkg-config -libs $(LIBS); fi)
INCLUDES = $(shell if [ -n "$(LIBS)" ]; then pkg-config -cflags $(LIBS); fi) -Isrc/includes
INCLUDES = $(shell if [ -n "$(LIBS)" ]; then pkg-config -cflags $(LIBS); fi) -I$(HEADER_DIR)
SRC = $(shell find src/ -type f -name '*.c') main.c
SRC = $(shell find $(SOURCE_DIR)/ -type f -name '*.c') main.c
NOM = $(basename $(notdir $(SRC)))
OBJ = $(addprefix $(BUILD_DIR)/,$(addsuffix .o, $(NOM)))
DEP = $(addprefix $(BUILD_DIR)/,$(addsuffix .d, $(NOM)))
# Variables for GResources (GTK)
RES_DIR = ressources
RES_PREFIX = kyou
RCC = glib-compile-resources
RCC_OPTS = --c-name $(RES_PREFIX)
RES_SRC = $(shell find $(RES_DIR)/ -type f -name '*.gresource.xml')
RES_NOM = $(basename $(notdir $(RES_SRC)))
RES = $(addprefix $(SOURCE_DIR)/,$(addsuffix .c, $(RES_NOM)))
RES_INC = $(addprefix $(HEADER_DIR)/,$(addsuffix .h, $(RES_NOM)))
RES_OBJ = $(addprefix $(BUILD_DIR)/,$(addsuffix .o, $(RES_NOM)))
RES_DEP = $(addprefix $(BUILD_DIR)/,$(addsuffix .dep, $(RES_NOM)))
# Add GResources objects to general prerequisites
OBJ := $(RES_OBJ) $(OBJ)
DEP += $(addprefix $(BUILD_DIR)/,$(addsuffix .d, $(RES_NOM)))
# Note: '.d' and '.dep' are different, as CC -MMD would overwrite the resource
# dependencies generated by glib-compile-resources, and '.d' concerns the
# '.o' objects made from '.gresource.c', not deps for the latter.
#------------------------------------------------------------------------------
# MAIN BUILD RULES
#------------------------------------------------------------------------------
# Main rule
all: init build
all: init build
# General build
build: $(PROJECT_NAME)
# Build all the .o files in src dir
$(BUILD_DIR)/%.o: src/%.c src/includes/%.h Makefile
$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c Makefile
$(CC) $(CFLAGS) -MMD -MP -c $< $(INCLUDES) -o $(BUILD_DIR)/$(notdir $@)
# Build all the .o files in base dir
@@ -44,6 +72,42 @@ kyoutest: $(BUILD_DIR)/helloworld.o
# Dependancies generated by GCC (for headers)
-include $(DEP)
#------------------------------------------------------------------------------
# GRESOURCES
#------------------------------------------------------------------------------
# Build all GResources from .gresource.xml files in res dir
$(SOURCE_DIR)/%.gresource.c: $(RES_DIR)/%.gresource.xml $(HEADER_DIR)/%.gresource.h Makefile
$(eval DEPFILE = $(BUILD_DIR)/$(subst xml,dep,$(notdir $<)))
printf "$(SOURCE_DIR)/$$(basename $<): " | sed 's/xml/c/g' > $(DEPFILE)
$(RCC) --generate-dependencies $< --sourcedir=$(RES_DIR) | tr "\n" " " >> $(DEPFILE)
$(RCC) $< --generate-source $(RCC_OPTS) --target=$@ --sourcedir=$(RES_DIR)
# Generate header files from .gresource.xml files in res dir
$(HEADER_DIR)/%.gresource.h: $(RES_DIR)/%.gresource.xml Makefile
$(RCC) $< --generate-header $(RCC_OPTS) --target=$@ --sourcedir=$(RES_DIR)
# Dependencies generated above
-include $(RES_DEP)
# Utility to pre-generate resource headers
pregen-res: $(RES_INC)
test-res:
@echo RCC: $(RCC)
@echo RCC_OPTS: $(RCC_OPTS)
@echo RES_DIR: $(RES_DIR)
@echo RES_SRC: $(RES_SRC)
@echo RES: $(RES)
@echo RES_OBJ: $(RES_OBJ)
@echo RES_DEP: $(RES_DEP)
@echo RES_INC: $(RES_INC)
# Keep intermediate C files ! (remove with 'clear')
.PRECIOUS: $(RES_INC) $(RES)
#------------------------------------------------------------------------------
# SILLY RULES
#------------------------------------------------------------------------------
@@ -60,11 +124,7 @@ SUSSYBAKA:
rebuild: clean build
dev: build run
init: $(BUILD_DIR) clear
$(BUILD_DIR):
init: clear
mkdir -p $(BUILD_DIR)
test:
@@ -73,6 +133,8 @@ test:
@echo LDFLAGS: $(LDFLAGS)
@echo LIBS: $(LIBS)
@echo LDLIBS: $(LDLIBS)
@echo SOURCE_DIR: $(SOURCE_DIR)
@echo HEADER_DIR: $(HEADER_DIR)
@echo OBJ: $(OBJ)
@echo SRC: $(SRC)
@echo DEP: $(DEP)
@@ -82,12 +144,12 @@ run: $(PROJECT_NAME)
./$(PROJECT_NAME)
clean:
@rm -if $(OBJ) $(DEP)
@rm -if $(OBJ) $(DEP) $(RES) $(RES_DEP)
# @rm -rif *.o *.gch
# @rm -rif $(BUILD_DIR)/*.o $(BUILD_DIR)/*.gch
clear: clean
@rm $(PROJECT_NAME)
@rm $(PROJECT_NAME) $(RES_INC)
.PHONY: always init SUSSYBAKA test clear clean
.PHONY: always init SUSSYBAKA test test-res clear clean run pregen-res

35
main.c
View File

@@ -27,19 +27,32 @@
#include "version.h"
#include "types.h"
/*
*------------------------------------------------------------------------------
* Definitions
*------------------------------------------------------------------------------
*/
#define KYOU_ICON_PATH "ressources/"
#define KYOU_ICON_NAME TINYKYOU_NAME
#define KYOU_PATH "kyoukai_peek.png"
/* Bundle resources for portable build */
#define KYOU_PORTABLE
#ifdef KYOU_PORTABLE
#include "tiny-kyoukai.gresource.h"
#endif
/* Files and resources */
#define KYOU_ICON_PATH "ressources/"
#define KYOU_ICON_NAME TINYKYOU_NAME
#define KYOU_IMAGE_PATH "ressources/kyoukai_peek.png"
#define KYOU_RESOURCES "/dev/chenco/" TINYKYOU_NAME
#define KYOU_IMAGE_RESOURCE KYOU_RESOURCES "/kyoukai_peek.png"
/* Window properties */
#define KYOU_WIN_WIDTH 250
#define KYOU_WIN_HEIGHT 120
/* General options */
typedef struct s_kyou_options {
char* szKyouPath;
bool bAllowResize;
@@ -188,8 +201,11 @@ static void activate(GtkApplication *app, gpointer user_data) {
/* Load icon theme (kyoukai icon) */
icon_theme = gtk_icon_theme_get_for_display(gdk_display_get_default());
#ifndef KYOU_PORTABLE
gtk_icon_theme_add_search_path(icon_theme, KYOU_ICON_PATH );
#else
gtk_icon_theme_add_resource_path(icon_theme, KYOU_RESOURCES );
#endif
if ( !gtk_icon_theme_has_icon(icon_theme, KYOU_ICON_NAME) ) {
printf("WARN: Icon not found\n");
}
@@ -203,7 +219,11 @@ static void activate(GtkApplication *app, gpointer user_data) {
gtk_window_set_default_size(GTK_WINDOW(window), KYOU_WIN_WIDTH, KYOU_WIN_HEIGHT);
/* Load Kyoukai */
#ifndef KYOU_PORTABLE
kyou = gtk_picture_new_for_filename( opt->szKyouPath );
#else
kyou = gtk_picture_new_for_resource( KYOU_IMAGE_RESOURCE );
#endif
gtk_picture_set_can_shrink(GTK_PICTURE(kyou), FALSE);
gtk_picture_set_content_fit(GTK_PICTURE(kyou), GTK_CONTENT_FIT_CONTAIN);
@@ -229,8 +249,7 @@ static void activate(GtkApplication *app, gpointer user_data) {
/* Make window background transparent */
GtkCssProvider *provider = gtk_css_provider_new();
char* css = "window {background: transparent;}";
gtk_css_provider_load_from_string(provider, css);
gtk_css_provider_load_from_string(provider, "window {background: transparent;}");
gtk_style_context_add_provider_for_display(gdk_display_get_default(),GTK_STYLE_PROVIDER(provider),GTK_STYLE_PROVIDER_PRIORITY_USER);
/* Hide window frame and title bar */
@@ -258,7 +277,7 @@ int main(int argc, char **argv) {
int status;
t_kyou_options options = {
.szKyouPath = KYOU_PATH,
.szKyouPath = KYOU_IMAGE_PATH,
.bDrawFrame = false
};
@@ -272,7 +291,7 @@ int main(int argc, char **argv) {
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
printf( "\nO ancient darkness\n\t...primordial flame, what is it that lies\n\t...within your eyes?\nO bright light that" \
printf( "\nÔ ancient darkness,\n\t...primordial flame, what is it that lies\n\t...within your eyes?\nÔ bright light that" \
" splits the Heavens,\n\trumbling tremors that shake the Earth, what pulse beats\n\t...within your ears?\nMan" \
" walks adrift, a vessel of the Earth.\nI dance for thee, O God of Lightning!\nAh...Ryoku Sui..." \
"\n\tLet blood boil over!\n" );

View File

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View File

@@ -0,0 +1,6 @@
<gresources>
<gresource prefix="/dev/chenco/tiny-kyoukai">
<file>kyoukai_peek.png</file>
<file alias="icons/48x48/apps/tiny-kyoukai.png">hicolor/48x48/apps/tiny-kyoukai.png</file>
</gresource>
</gresources>

View File

@@ -1,4 +1,4 @@
#define DEBUG
// #define DEBUG
extern char* sTestString;