From 2532885046fbdd21024386aa868bba7dc94895bb Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Thu, 6 Sep 2007 00:58:29 +0200 Subject: [PATCH] Resources in binary support, button drawing functions Added support for converting data files to c source, which will then be included and compiled into the binary. This makes Tennix run without the "data" folder, and the game resources (sounds and data) cannot simply be edited for the binary distribution. Added first cut of button drawing routines which can be useful for menu navigation later on. (including text) Fix space character in Smallish font. --- data/data2csrc.c | 34 ++++++++++++++++++++++++++++++++++ data/makefile | 20 ++++++++++++++++++++ data/smallish-font.png | Bin 687 -> 686 bytes graphics.c | 45 +++++++++++++++++++++++++++++---------------- graphics.h | 4 ++++ makefile | 13 ++++++++++--- sound.c | 20 +++++++++----------- sound.h | 2 +- tennix.c | 3 --- tennix.h | 14 +++++++------- 10 files changed, 114 insertions(+), 41 deletions(-) create mode 100644 data/data2csrc.c create mode 100644 data/makefile rewrite data/smallish-font.png (98%) diff --git a/data/data2csrc.c b/data/data2csrc.c new file mode 100644 index 0000000..a705212 --- /dev/null +++ b/data/data2csrc.c @@ -0,0 +1,34 @@ +/*** + * data2csrc.c -- Read data and output c source to stdout + * + * Copyright (c) 2007 Thomas Perl + * Website: http://thpinfo.com/ + * + * License: This code is in the public domain. + ***/ + +#include +#include + +int main( int argc, char** argv) { + FILE *fp; + int c, x = 0; + + if( argc != 3) { + fprintf( stderr, "Usage: %s /path/to/infile.dat name_of_variable\n", argv[0]); + exit(1); + } + + fp = fopen( argv[1], "rb"); + if( fp == NULL) { fprintf( stderr, "Cannot open file: %s\n", argv[1]); exit(1); } + printf( "const char %s[] = {", argv[2]); + while( (c = fgetc( fp)) != EOF) { + if( x > 0) putchar(','); + if( x++ % 20 == 0) putchar('\n'); + printf( "%d", c); + } + printf( "\n};\n"); + fclose( fp); + return 0; +} + diff --git a/data/makefile b/data/makefile new file mode 100644 index 0000000..bcd47ad --- /dev/null +++ b/data/makefile @@ -0,0 +1,20 @@ + +GRAPHICS=$(wildcard *.png) +SOUNDS=$(wildcard *.wav) + +all: graphics_data.c sounds_data.c + +data2csrc: data2csrc.o + +graphics_data.c: data2csrc $(GRAPHICS) + (for i in $(GRAPHICS); do base=$${i%%.png}; ./data2csrc $$i $${base/-/_}; done)>graphics_data.c + +sounds_data.c: data2csrc $(SOUNDS) + (for i in $(SOUNDS); do base=$${i%%.wav}; ./data2csrc $$i $${base/-/_}; done)>sounds_data.c + +clean: + rm -f data2csrc *.o graphics_data.c sounds_data.c + +.PHONY: clean all +.DEFAULT: all + diff --git a/data/smallish-font.png b/data/smallish-font.png dissimilarity index 98% index 7f2a5f22f8b5399936b56fe20267713176af6eec..7380bb610a4461836b213cdcb3474651716a48fa 100644 GIT binary patch delta 650 zcwPaD0(Jeb1+E1liBL{Q4GJ0x0000DNk~Le0003_0000A2m$~A09214@R1=F2><{9 z008+zyMK{DAAbo24=oc`=}34000J&aL_t(&-tAf2Zo@DLjjR6uFMAJ}EXw8h*v46= zhKDIdHel|sQfoad zL?K;o$ra<<;5Fn-)~hD0YXf7B;7OrT2Du|Tqc8y4NlUQdEkT#Bi(G*sKHeVP zS3mz3+*9rENbfjn%CgCP0|QH%o2;8fcAxV_MU1a$O!whhWV8bU%@p74Sib8IcN@l0 z38Ryv-G7|)XV5JIrPRi!NKJ3PK|!Ctu7c}5P`Qd^Xd$sn$8LFqCi4k5ALhGQvFF+61k!d^+;lwpKQTrz8Mj% z$+MJ&?IBHWGrH}Hv9rp|8aWo=5|uTp8CRI0k$*l*u@`h_-8h#Eri(+%X7iNys!-y& zL{;t8oiSRyRptH4gyatA*UVAZ%w3X8R8OKaKu&i(kv?E=O3=7@zDX4wUHULt%my02 z-*LA{Yyhe3I729 z0RN!9r<0LDAAblR6eKp@nDwjx00J*bL_t(&-tAf2jsqbG#isxN%kD!bhUMl_(Vb0) zhsF{G- zA);C<5tUMEt?ieRfD`{LIC7oxbsa{QQx5i+!U$=9X@4v{-sS5dQ?5Tmb3*xv_7X${ z()5;GF}@96h!1IJnlP%4hdF{D3XO7*GQ2$k6F}Rk2{ybYX!13YD^M5+{WLAzAB6nk z@Be~(s{O5tK%in?B$0JsS59-2QM1VI^Et0%;S~(&HB5_)_B%oAgts=D@A}2f_Ha}} zZ|7+5#((`e=nVtp(B^K-DI;8KkeBftj^4P$&bQcJdVbb2nwNdS*wBnP!7i7e@!>=o z1+9TN>>DY5Rv@)*O)&%#JR9&`9}r9z4J5?%VBWeb)t~ero-1Av?g`4XkuLAZ8j(y} zI!i~f8Mvi&{?@H;?4vx7dj1q}@rsz%j48~}xPP9d*aN*-H@x}d(cy^FNj&AgB9w40 zUNL)l^FynyqL`mdKjz?lW=1+QQ*ka{_3-uta+*&M>BIA;yc~DPH>sJU4?0Nl(t(D* z-Iy;#oi5$1;nS(kdmWvwHfDM=8#Y09=s`Q~w7uirwt%(St-j<_Bpixels + x * surface->format->BytesPerPixel + y * surface->pitch))) @@ -69,6 +71,8 @@ void show_image( unsigned int, int, int, int); void line_horiz( int y, Uint8 r, Uint8 g, Uint8 b); void line_vert( int x, Uint8 r, Uint8 g, Uint8 b); void rectangle( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b); +void draw_button( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed); +void draw_button_text( char* s, int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed); void introimage( unsigned int); void clearscr(); void updatescr(); diff --git a/makefile b/makefile index 1d327ac..93ed598 100644 --- a/makefile +++ b/makefile @@ -45,7 +45,7 @@ OBJ = tennix.o game.o graphics.o input.o sound.o WIN32LIBS = *.dll OSXAPP = Tennix.app -DATAFILES = README README.* data/*.png data/*.wav +DATAFILES = README README.* ifeq ($(TARGET),cocoa) DATAFILES += data/Tennix.icns endif @@ -55,9 +55,15 @@ tennix: $(OBJ) test -f tennix.exe && upx tennix.exe || true tennix.o: tennix.c tennix.h game.h graphics.h input.h sound.h -graphics.o: graphics.c graphics.h tennix.h input.h +graphics.o: graphics.c graphics.h tennix.h input.h data/graphics_data.c game.o: game.c game.h graphics.h tennix.h sound.h -sound.o: sound.c sound.h tennix.h +sound.o: sound.c sound.h tennix.h data/sounds_data.c + +data/graphics_data.c: + make -C data graphics_data.c + +data/sounds_data.c: + make -C data sounds_data.c release-osx: tennix mkdir -p $(OSXAPP)/Contents/{MacOS,/Resources} @@ -76,6 +82,7 @@ release-bin: tennix clean: rm -f *.o tennix{,.exe} rm -rf $(OSXAPP) + make -C data clean distclean: clean rm -f tennix-$(RELEASE).zip tennix-$(RELEASE)-bin.tar.gz diff --git a/sound.c b/sound.c index 86385a4..2b695e6 100644 --- a/sound.c +++ b/sound.c @@ -26,6 +26,8 @@ static char datadir[MAXPATHLEN]; +#include "data/sounds_data.c" + void init_sound( const char *data_dir) { strcpy( datadir, data_dir); @@ -34,19 +36,15 @@ void init_sound( const char *data_dir) { } } -void play_sample( const char* filename, unsigned char channel, int loop) { +void play_sample( ResourceData sound, unsigned char channel, int loop) { static Mix_Chunk* chunks[CH_MAX] = { 0 }; - char fn[MAXPATHLEN]; - - strcpy( fn, datadir); - strcat( fn, filename); if( chunks[channel] != NULL) { Mix_FreeChunk( chunks[channel]); chunks[channel] = NULL; } - chunks[channel] = Mix_LoadWAV( fn); + chunks[channel] = Mix_LoadWAV_RW( SDL_RWFromConstMem( sound.data, sound.size), 1); if( chunks[channel] != NULL) { Mix_PlayChannel( channel, chunks[channel], loop); } else { @@ -59,21 +57,21 @@ void fade_out( unsigned char channel) { } void sound_ground() { - const char* sounds[] = { "ground1.wav", "ground2.wav" }; + ResourceData sounds[] = { RESOURCE(ground1), RESOURCE(ground2) }; play_sample( sounds[rand()%2], CH_GROUND, 0); } void sound_racket() { - const char* sounds[] = { "racket1.wav", "racket2.wav" }; + ResourceData sounds[] = { RESOURCE(racket1), RESOURCE(racket2) }; play_sample( sounds[rand()%2], CH_RACKET, 0); } void sound_audience() { - play_sample( "audience.wav", CH_AUDIENCE, -1); + play_sample( RESOURCE(audience), CH_AUDIENCE, -1); } void sound_applause() { - play_sample( "applause.wav", CH_APPLAUSE, 0); + play_sample( RESOURCE(applause), CH_APPLAUSE, 0); } void sound_applause_stop() { @@ -81,6 +79,6 @@ void sound_applause_stop() { } void sound_out() { - play_sample( "out.wav", CH_OUT, 0); + play_sample( RESOURCE(out), CH_OUT, 0); } diff --git a/sound.h b/sound.h index 59e60db..df6a401 100644 --- a/sound.h +++ b/sound.h @@ -38,7 +38,7 @@ enum { }; void init_sound( const char *); -void play_sample( const char*, unsigned char, int); +void play_sample( ResourceData sound, unsigned char channel, int loop); void fade_out( unsigned char); void sound_ground(); diff --git a/tennix.c b/tennix.c index f71924b..472645a 100644 --- a/tennix.c +++ b/tennix.c @@ -88,9 +88,6 @@ int main( int argc, char** argv) { SDL_ShowCursor( SDL_DISABLE); SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, 1); - strcpy( datadir, argv[0]); - strcpy( (char*)(datadir+strlen( dirname( datadir))), DATADIR); - init_sound( datadir); init_graphics( datadir); diff --git a/tennix.h b/tennix.h index 7fe5007..a55186b 100644 --- a/tennix.h +++ b/tennix.h @@ -32,13 +32,6 @@ #define COPYRIGHT "Copyright 2003, 2007 Thomas Perl" #define URL "http://icculus.org/tennix/" - -#define DATADIR "/data/" -#ifdef MACOSX -#undef DATADIR -#define DATADIR "/../Resources/" -#endif - #define WIDTH 640 #define HEIGHT 480 @@ -59,6 +52,13 @@ extern SDL_Surface *screen; typedef struct { + const char* data; + unsigned int size; +} ResourceData; + +#define RESOURCE(x) ((ResourceData){ x, sizeof( x) }) + +typedef struct { int x; int y; float current_x; -- 2.11.4.GIT