cepheid

An Aurora 4X clone
Log | Files | Refs | README

commit f7b477f1e73365e610798d13b545331dc3d6c308
parent ba89fc3585bd7cff88251651d79c686f76197942
Author: hhvn <dev@hhvn.uk>
Date:   Fri,  2 Sep 2022 13:05:37 +0100

Include sol within binary

Diffstat:
M.gitignore | 3++-
MMakefile | 5++++-
Mconfig.mk | 3++-
Mdata/Makefile | 5++++-
Adata/dir2c.sh | 48++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.c | 7+++++--
Msrc/main.h | 6++++++
Msrc/pane.c | 2+-
Msrc/save.c | 47+++++++++++++++++++++++++++++++++++++++++++++--
Msrc/ui.c | 8++++++++
Msrc/ui/main.c | 2+-
11 files changed, 126 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,10 +1,11 @@ *.o data/*.h +data/*.c data/sol data/worlds.tsv db/dbtool game -testdb/ +saves/ core.* tags perf.* diff --git a/Makefile b/Makefile @@ -3,7 +3,7 @@ DBDIR = db DBLIB = $(DBDIR)/db.o DBTOOL = $(DBDIR)/dbtool SRCDIR = src -SRC = $(shell find $(SRCDIR) -name "*.c") styles/$(STYLE).c +SRC = $(shell find $(SRCDIR) -name "*.c") styles/$(STYLE).c data/sol.c OBJ = $(SRC:.c=.o) BIN = game RAYLIB = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 @@ -14,6 +14,9 @@ include config.mk all: db data $(BIN) src/data.o: data/icons/* +.c.o: + $(CC) $(CFLAGS) -D"SAVEDIR=\"$(SAVEDIR)\"" -c $< -o $@ + $(OBJ): src/struct.h $(BIN): $(OBJ) $(DBLIB) $(CC) $(LDFLAGS) -o $(BIN) $(OBJ) diff --git a/config.mk b/config.mk @@ -1,2 +1,3 @@ -STYLE = hhvn +STYLE = hhvn CFLAGS = -Wall -g3 -O0 +SAVEDIR = saves diff --git a/data/Makefile b/data/Makefile @@ -2,7 +2,7 @@ FONTS = $(shell find . -name "*.ttf") ICONS = $(shell find icons -name "*.png") HEADERS = $(FONTS:.ttf=.h) $(ICONS:.png=.h) -all: $(HEADERS) sol +all: $(HEADERS) sol.c sol: worlds.tsv worlds-parse.awk rm -rf sol @@ -11,6 +11,9 @@ sol: worlds.tsv worlds-parse.awk printf "x\t0\n" >> sol/index printf "y\t0\n" >> sol/index +sol.c: sol + ./dir2c.sh sol + # Thanks Jonathan worlds.tsv: curl https://planet4589.org/space/gcat/tsv/worlds/worlds.tsv | \ diff --git a/data/dir2c.sh b/data/dir2c.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +p="wdir_" +d="$1" +h="$d.h" +c="$d.c" + +rm -f $h $c + +cat > $c << EOF +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <limits.h> +#include <sys/stat.h> +#include "${h}" + +int +${p}${d}(char *dir) { + char path[PATH_MAX]; + int fd; + + if (mkdir(dir, 0755) == -1) + return -1; +EOF + +for f in $d/*; do + b=$(basename "$f") + v="${p}${d}_$(echo "$b" | tr " ()!'-" 'abcdef')" + + echo "unsigned char $v[] = {" >> $h + xxd -i < $f >> $h + echo "};" >> $h + + cat >> $c << EOF + snprintf(path, PATH_MAX, "%s/$b", dir); + fd = open(path, O_WRONLY|O_CREAT, 0644); + if (fd == -1) + return -1; + write(fd, $v, sizeof($v)); + close(fd); +EOF +done + +cat >> $c << EOF + return 0; +} +EOF diff --git a/src/main.c b/src/main.c @@ -3,7 +3,7 @@ #include <raylib.h> #include "main.h" -#define TESTSAVE "testdb" +#define DEFSAVE "default" Save *save = NULL; @@ -17,7 +17,10 @@ main(void) { ui_init(); data_load(loader); - save_read(loader, TESTSAVE); + + if (!save_exists(DEFSAVE)) + save_create(DEFSAVE); + save_read(loader, DEFSAVE); loading_close(loader); diff --git a/src/main.h b/src/main.h @@ -55,6 +55,7 @@ void ui_deinit(void); void ui_print(int x, int y, Color col, char *format, ...); void ui_title(char *fmt, ...); int ui_textsize(char *text); +float ui_get_scroll(void); int ui_checkbox_size(Checkbox *checkbox); int ui_collides(Geom geom, Vector2 point); int ui_onscreen(Vector2 point); @@ -131,6 +132,11 @@ void body_sort(Body **bodies, size_t n); #define SAVE_READ_STEPS 2 void save_read(Loader *lscr, char *dir); void save_write(void); +int save_exists(char *name); +int save_create(char *name); + +/* ../data/sol.c */ +int wdir_sol(char *dir); /* data.c */ extern Font font; diff --git a/src/pane.c b/src/pane.c @@ -36,7 +36,7 @@ pane_end(void) { } if (m.x >= f->geom->x && m.x <= f->geom->x + f->geom->w && m.y >= f->geom->y && m.y <= f->geom->y + f->geom->h) - f->off -= GetMouseWheelMove() * SCROLL_MULT; + f->off -= ui_get_scroll() * SCROLL_MULT; if (f->off > f->max - f->geom->h) f->off = f->max - f->geom->h; if (f->off < 0) diff --git a/src/save.c b/src/save.c @@ -1,5 +1,9 @@ +#include <stdio.h> #include <stdlib.h> +#include <unistd.h> #include <string.h> +#include <limits.h> +#include <sys/stat.h> #include "main.h" /* Plan for dealing with multiple saves: @@ -20,15 +24,19 @@ save_free(void) { } void -save_read(Loader *lscr, char *dir) { +save_read(Loader *lscr, char *name) { + char dir[PATH_MAX]; char *str; + if (save) save_free(); - if (!dir || !(save = malloc(sizeof(Save)))) + if (!name || !(save = malloc(sizeof(Save)))) return; + snprintf(dir, sizeof(dir), "%s/%s", SAVEDIR, name); + loading_update(lscr, "Initializing DB"); dbdeclare(dir); save->db.dir = nstrdup(dir); @@ -47,3 +55,38 @@ save_write(void) { dbset(save->db.dir, "index", "selsystem", view_main.sys->name); dbwrite(save->db.dir); } + +int +save_exists(char *name) { + char dir[PATH_MAX]; + + snprintf(dir, sizeof(dir), "%s/%s", SAVEDIR, name); + if (access(dir, F_OK) == 0) + return 1; + return 0; +} + +int +save_create(char *name) { + char path[PATH_MAX]; + FILE *f; + + snprintf(path, sizeof(path), "%s/%s", SAVEDIR, name); + if (mkdir(path, 0755) == -1) + return -1; + + snprintf(path, sizeof(path), "%s/%s/Systems", SAVEDIR, name); + if (mkdir(path, 0755) == -1) + return -1; + + snprintf(path, sizeof(path), "%s/%s/Systems/Sol", SAVEDIR, name); + if (wdir_sol(path) == -1) + return -1; + + snprintf(path, sizeof(path), "%s/%s/index", SAVEDIR, name); + if (!(f = fopen(path, "w"))) + return -1; + fprintf(f, "selsystem\tSol\n"); + fclose(f); + return 0; +} diff --git a/src/ui.c b/src/ui.c @@ -117,6 +117,14 @@ ui_textsize(char *text) { return MeasureTextEx(font, text, FONT_SIZE, FONT_SIZE/10).x; } +float +ui_get_scroll(void) { + float ret = GetMouseWheelMove(); + if (IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT)) + ret *= 2; + return ret; +} + int ui_collides(Geom geom, Vector2 point) { switch (geom.type) { diff --git a/src/ui/main.c b/src/ui/main.c @@ -73,7 +73,7 @@ void ui_handle_view_main(int nowsel) { Vector2 mouse = GetMousePosition(); Vector2 delta = GetMouseDelta(); - float wheel = GetMouseWheelMove(); + float wheel = ui_get_scroll(); float diff; Body *furth;