From 4a1320c5852a6e3ac07915723adff31a7dd705f3 Mon Sep 17 00:00:00 2001 From: Ben Lynn Date: Fri, 6 Aug 2004 13:01:45 -0700 Subject: [PATCH] Sun Jun 27 10:41:49 PDT 2004 Minor Makefile cleanup. Fri Aug 6 13:01:45 PDT 2004 Implemented suggestions from Denis Klykvin: now loads config file from $HOME/.netwalk/ and if it doesn't exist, a default one will be created. Updated outdated README. [netwalk-0.4.5.tgz] --- Makefile | 41 +++++++++----- NEWS | 12 ++++ README | 10 ++-- config | 4 -- config.c | 129 ++++++++++++++++++++++++++++++++++++++++++ config.h | 18 ++++++ linux/config_file.c | 61 ++++++++++++++++++++ main.c | 159 +++------------------------------------------------- util.h | 14 +++++ win32/config_file.c | 33 +++++++++++ 10 files changed, 307 insertions(+), 174 deletions(-) delete mode 100644 config create mode 100644 config.c create mode 100644 config.h create mode 100644 linux/config_file.c create mode 100644 util.h create mode 100644 win32/config_file.c diff --git a/Makefile b/Makefile index 532ee1b..64a0900 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ -VERSION=0.4.4 -ALLFILES = *.[ch] Makefile LICENSE README NEWS config helmetr.ttf +VERSION=0.4.5 +ALLFILES = *.[ch] Makefile LICENSE README NEWS linux/*.[ch] win32/*.[ch] helmetr.ttf PROJNAME = netwalk -ifdef WIN32 +OS ?= linux +ifeq ("$(OS)", "win32") CC = i586-mingw32msvc-gcc -EFLAGS=-boost -O2 -I /home/ben/cross/SDL/include/SDL CFLAGS=-O2 -Wall -I /home/ben/cross/SDL/include/SDL -mwindows SDL_LIBS=-L /home/ben/cross/SDL/lib -lmingw32 -lSDLmain -lSDL LIBS = $(SDL_LIBS) -lSDL_ttf @@ -12,6 +12,8 @@ CC = gcc CFLAGS=-Wall -O2 -fomit-frame-pointer `sdl-config --cflags` SDL_LIBS=`sdl-config --libs` LIBS = $(SDL_LIBS) -lSDL_ttf +INSTALL = /usr/bin/install +PREFIX = /usr endif .PHONY: target clean dist @@ -21,32 +23,45 @@ target : version.h $(PROJNAME) version.h : ./Makefile echo '#define VERSION_STRING "'$(VERSION)'"' > version.h -$(PROJNAME) : main.c game.c colour.c widget.c +config_file.c : $(OS)/config_file.c + ln -s $^ $@ + +$(PROJNAME) : main.c game.c colour.c widget.c config.c config_file.c $(CC) -o $@ $^ $(CFLAGS) $(LIBS) DISTNAME=$(PROJNAME)-$(VERSION) -dist: $(ALLFILES) - -rm version.h +clean : + -rm $(PROJNAME) *.o version.h config_file.c + +dist: $(ALLFILES) clean -rm -rf $(DISTNAME) mkdir $(DISTNAME) cp -rl --parents $(ALLFILES) $(DISTNAME) tar chfz $(DISTNAME).tgz $(DISTNAME) -rm -rf $(DISTNAME) -ifdef WIN32 -bindist : $(PROJNAME) +ifeq ("$(OS)", "win32") +zip : target -rm -rf $(DISTNAME) mkdir $(DISTNAME) cp -l LICENSE $(DISTNAME) cp -l $(PROJNAME) $(DISTNAME)/$(PROJNAME).exe cp -l *.ttf $(DISTNAME) - cp -l config $(DISTNAME) + #cp -l config $(DISTNAME) cp -l /home/ben/cross/SDL/lib/SDL.dll $(DISTNAME) cp -l /home/ben/cross/SDL/lib/SDL_ttf.dll $(DISTNAME) zip $(DISTNAME)-win.zip $(DISTNAME)/* -rm -rf $(DISTNAME) -endif +else -clean : - -rm $(PROJNAME) *.o version.h +install : netwalk + $(INSTALL) -m 755 netwalk $(PREFIX)/bin + $(INSTALL) -d $(PREFIX)/share/$(PROJNAME) + $(INSTALL) -m 644 helmetr.ttf $(PREFIX)/share/$(PROJNAME)/ + +uninstall : clean + -rm -f $(PREFIX)/bin/$(PROJNAME) + -rm -rf $(PREFIX)/share/$(PROJNAME) + +endif diff --git a/NEWS b/NEWS index 33e3f66..d1fd7c6 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,15 @@ +Fri Aug 6 13:01:45 PDT 2004 + Implemented suggestions from Denis Klykvin: now loads config file + from $HOME/.netwalk/ and if it doesn't exist, a default one will be + created. + + Updated outdated README. + + [netwalk-0.4.5.tgz] + +Sun Jun 27 10:41:49 PDT 2004 + Minor Makefile cleanup. + Tue Jun 22 23:13:04 PDT 2004 Interface is better: shading of widgets, current menu item is highlighted, menu item is activated on mouse button up, rather than a second click. diff --git a/README b/README index bcf7cac..d96deb5 100644 --- a/README +++ b/README @@ -1,10 +1,10 @@ -README for NetWalk v0.4.2 +README for NetWalk v0.4.5 Ben Lynn INTRODUCTION NetWalk is a puzzle game where the object is to connect every terminal to the -main server. (These are represented by coloured boxes, so I guess you could +main server. (These are represented by coloured boxes, so you could also imagine them to be water mains, electricity cables, phone lines, etc.) In this version, not only must every terminal be connected, but every piece of @@ -15,15 +15,13 @@ USAGE Click on a square to rotate its contents. A left click performs an anticlockwise rotation and a right click performs a clockwise rotation. -You can select presets, or make a custom game in the options window. - There is one shortcut key: F2 starts a new game. -Edit the "config" file to configure the game and add presets. +Edit the ".netwalk/config" file to configure the game. COMPILATION -Type "make" to compile the project. +Type "make" to compile. LICENSE diff --git a/config b/config deleted file mode 100644 index 9421b4f..0000000 --- a/config +++ /dev/null @@ -1,4 +0,0 @@ -font helmetr.ttf -fontsize 11 -hiscores hiscores.txt -showmoves 0 diff --git a/config.c b/config.c new file mode 100644 index 0000000..86ed851 --- /dev/null +++ b/config.c @@ -0,0 +1,129 @@ +#include "config.h" +#include "util.h" + +static void parse_option(config_ptr config, char *s1, char *s2) +{ + if (!strcmp(s1, "showmoves")) { + config->showmoves = atoi(s2); + } + if (!strcmp(s1, "fontsize")) { + config->fontsize = atoi(s2); + } + if (!strcmp(s1, "font")) { + config->fontname = clonestr(s2); + } + if (!strcmp(s1, "hiscores")) { + config->hsfile = clonestr(s2); + } +} + +static int is_whitespace(char c) +{ + if (strchr(" \t\r\n", c)) return -1; + return 0; +} + +static void skip_whitespace(FILE *fp) +{ + for (;;) { + int c; + c = getc(fp); + if (feof(fp)) return; + if (!is_whitespace(c)) { + ungetc(c, fp); + break; + } + } +} + +/* +static void read_word(char *s, FILE *fp) +{ + int i = 0; + + skip_whitespace(fp); + if (feof(fp)) return; + + for (;;) { + int c; + c = getc(fp); + if (feof(fp)) return; + if (is_whitespace(c)) { + ungetc(c, fp); + break; + } + s[i] = c; + i++; + if (i >= 128 - 1) break; + } + s[i] = 0; +} +*/ + +static void read_line(char *s, FILE *fp) +{ + int i = 0; + + for (;;) { + int c; + c = getc(fp); + if (feof(fp)) return; + if (c == '\r') { + //safest thing to do? + continue; + } + if (c == '\n') { + ungetc(c, fp); + break; + } + s[i] = c; + i++; + if (i >= 1024 - 1) break; + } + s[i] = 0; +} + +void config_load(config_ptr config) +{ + FILE *fp; + + fp = config_get_fp(); + + for(;;) { + int i; + char s1[1024], *s2; + + skip_whitespace(fp); + if (feof(fp)) { + break; + } + read_line(s1, fp); + if (feof(fp)) { + break; + } + + i = 0; + for(;;) { + if (!s1[i]) { + s2 = &s1[i]; + break; + } + if (is_whitespace(s1[i])) { + s1[i] = 0; + i++; + for(;;) { + if (!s1[i] || !is_whitespace(s1[i])) { + s2 = &s1[i]; + break; + } + } + break; + } + i++; + } + + parse_option(config, s1, s2); + } + + fclose(fp); +} diff --git a/config.h b/config.h new file mode 100644 index 0000000..38cf784 --- /dev/null +++ b/config.h @@ -0,0 +1,18 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include + +struct config_s { + char *fontname; + char *hsfile; + int fontsize; + int showmoves; +}; +typedef struct config_s *config_ptr; +typedef struct config_s config_t[1]; + +void config_load(config_ptr config); +FILE *config_get_fp(); + +#endif //CONFIG_H diff --git a/linux/config_file.c b/linux/config_file.c new file mode 100644 index 0000000..590ff94 --- /dev/null +++ b/linux/config_file.c @@ -0,0 +1,61 @@ +#include "config.h" +#include +#include +#include +#include +#include + +#define NETWALK_SHARE_DIR "." +//#define NETWALK_SHARE_DIR "/usr/share/netwalk" + +FILE *config_get_fp() +{ + FILE *fp; + + char *home_dir = getenv("HOME"); + char *netwalk_dir; + char *config_file; + struct stat stbuf; + + if (!home_dir) { + fprintf(stderr, "$HOME is not set\n"); + exit(1); + } + + netwalk_dir = malloc(strlen(home_dir) + 100); + config_file = malloc(strlen(home_dir) + 100); + + strcpy(netwalk_dir, home_dir); + strcat(netwalk_dir,"/.netwalk"); + + if (stat(netwalk_dir, &stbuf)) { + if (mkdir(netwalk_dir, 0755)) { + fprintf(stderr, "Can't stat nor mkdir %s\n", netwalk_dir); + exit(1); + } + } + + strcpy(config_file, netwalk_dir); + strcat(config_file, "/config"); + + if (stat(config_file, &stbuf)) { + fprintf(stderr, "Creating config file at %s\n", config_file); + fp = fopen(config_file, "w"); + if (!fp) { + fprintf(stderr, "Can't create %s\n", config_file); + exit(1); + } + fprintf(fp, "font %s/helmetr.ttf\n", NETWALK_SHARE_DIR); + fprintf(fp, "fontsize 11\n"); + fprintf(fp, "hiscores %s/hiscores.txt\n", netwalk_dir); + fprintf(fp, "showmoves 0\n"); + fclose(fp); + } + + fp = fopen(config_file, "r"); + + free(netwalk_dir); + free(config_file); + + return fp; +} diff --git a/main.c b/main.c index 0606b7e..7eca62a 100644 --- a/main.c +++ b/main.c @@ -42,7 +42,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "colour.h" #include "game.h" -char *config_file = "config"; +#include "config.h" +#include "util.h" static int interrupted = 0; @@ -77,15 +78,6 @@ enum { }; char shifttable[256]; -int show_moves_flag = 0; - -struct config_s { - char *fontname; - char *hsfile; - int fontsize; -}; -typedef struct config_s *config_ptr; -typedef struct config_s config_t[1]; struct hsentry_s { char *name; @@ -107,10 +99,11 @@ typedef struct gameparm_s gameparm_t[1]; gameparm_t gp; +static config_t config; + SDL_Surface *screen; TTF_Font *font; int lastmousex, lastmousey; -config_t config; char *player_name; int game_won; int state; @@ -215,13 +208,6 @@ label_t l_en1; textbox_t tb_en1; button_t b_en1; -char *clonestr(char *s) -{ - char *res = malloc(sizeof(char) * strlen(s) + 1); - strcpy(res, s); - return res; -} - SDL_Surface *font_render(char *s, int c) { return TTF_RenderText_Solid(font, s, rgbtable[c]); @@ -1426,136 +1412,7 @@ void window_init(window_ptr w) w->widget.computexy = window_computexy; } -void parse_option(char *s1, char *s2) -{ - if (!strcmp(s1, "showmoves")) { - show_moves_flag = atoi(s2); - } - if (!strcmp(s1, "fontsize")) { - config->fontsize = atoi(s2); - } - if (!strcmp(s1, "font")) { - config->fontname = clonestr(s2); - } - if (!strcmp(s1, "hiscores")) { - config->hsfile = clonestr(s2); - } -} - -int is_whitespace(char c) -{ - if (strchr(" \t\r\n", c)) return -1; - return 0; -} - -void skip_whitespace(FILE *fp) -{ - for (;;) { - int c; - c = getc(fp); - if (feof(fp)) return; - if (!is_whitespace(c)) { - ungetc(c, fp); - break; - } - } -} - -void read_word(char *s, FILE *fp) -{ - int i = 0; - - skip_whitespace(fp); - if (feof(fp)) return; - - for (;;) { - int c; - c = getc(fp); - if (feof(fp)) return; - if (is_whitespace(c)) { - ungetc(c, fp); - break; - } - s[i] = c; - i++; - if (i >= 128 - 1) break; - } - s[i] = 0; -} - -void read_line(char *s, FILE *fp) -{ - int i = 0; - - for (;;) { - int c; - c = getc(fp); - if (feof(fp)) return; - if (c == '\r') { - //safest thing to do? - continue; - } - if (c == '\n') { - ungetc(c, fp); - break; - } - s[i] = c; - i++; - if (i >= 1024 - 1) break; - } - s[i] = 0; -} - -void read_config() -{ - FILE *fp; - - fp = fopen(config_file, "r"); - if (!fp) { - fprintf(stderr,"Can't open config file %s\n", config_file); - exit(1); - } - - for(;;) { - int i; - char s1[1024], *s2; - - skip_whitespace(fp); - if (feof(fp)) { - break; - } - read_line(s1, fp); - if (feof(fp)) { - break; - } - - i = 0; - for(;;) { - if (!s1[i]) { - s2 = &s1[i]; - break; - } - if (is_whitespace(s1[i])) { - s1[i] = 0; - i++; - for(;;) { - if (!s1[i] || !is_whitespace(s1[i])) { - s2 = &s1[i]; - break; - } - } - break; - } - i++; - } - - parse_option(s1, s2); - } - - fclose(fp); -} - -void add_shiftstring(char *s1, char *s2) +static void add_shiftstring(char *s1, char *s2) { int i; @@ -1588,7 +1445,7 @@ int main(int argc, char *argv[]) add_shiftstring("[]\\;',./`", "{}|:\"<>?~"); } - read_config(); + config_load(config); read_hstable(); init(); @@ -1596,7 +1453,7 @@ int main(int argc, char *argv[]) font = TTF_OpenFont(config->fontname, config->fontsize); if (!font) { - fprintf(stderr, "error loading font\n"); + fprintf(stderr, "error loading font %s\n", config->fontname); exit(1); } @@ -1639,7 +1496,7 @@ int main(int argc, char *argv[]) //setup moves and time label_init(l_moves); - if (show_moves_flag) { + if (config->showmoves) { window_add_widget(root, l_moves); } diff --git a/util.h b/util.h new file mode 100644 index 0000000..644375a --- /dev/null +++ b/util.h @@ -0,0 +1,14 @@ +#ifndef UTIL_H +#define UTIL_H + +#include +#include + +static inline char *clonestr(char *s) +{ + char *res = malloc(sizeof(char) * strlen(s) + 1); + strcpy(res, s); + return res; +} + +#endif //UTIL_H diff --git a/win32/config_file.c b/win32/config_file.c new file mode 100644 index 0000000..361cf88 --- /dev/null +++ b/win32/config_file.c @@ -0,0 +1,33 @@ +#include +#include "config.h" + +#define DEFAULT_WINFONT "C:\\WINDOWS\\FONTS\\ARIAL.TTF" + +char *config_file = "config"; + +FILE *config_get_fp() +{ + FILE *fp; + + fp = fopen(config_file, "r"); + if (!fp) { + fp = fopen(config_file, "w"); + if (!fp) { + fprintf(stderr, "Can't open or create config file\n"); + exit(1); + } + fprintf(fp, "font %s\n", DEFAULT_WINFONT); + fprintf(fp, "fontsize 11\n"); + fprintf(fp, "hiscores hiscores.txt\n"); + fprintf(fp, "showmoves 0\n"); + fclose(fp); + + fp = fopen(config_file, "r"); + if (!fp) { + fprintf(stderr,"Can't open config file %s\n", config_file); + exit(1); + } + } + + return fp; +} -- 2.11.4.GIT