From 25fb1c6fb564779797345e379908d25248efde13 Mon Sep 17 00:00:00 2001 From: David Griffith Date: Sun, 1 Dec 2013 01:05:10 -0800 Subject: [PATCH] First stab at getting sound working. AIFF-only for now. Doesn't work. --- Makefile | 7 +++-- src/curses/ux_blorb.c | 49 +++++++++++++++++++++--------- src/curses/ux_blorb.h | 15 +++++++++- src/curses/ux_init.c | 64 +++++++++++++++++++++++----------------- src/curses/ux_resource.c | 59 ++++++++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 44 deletions(-) create mode 100644 src/curses/ux_resource.c diff --git a/Makefile b/Makefile index be71a52..d11f102 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ CC = gcc # Define your optimization flags. Most compilers understand -O and -O2, # Standard (note: Solaris on UltraSparc using gcc 2.8.x might not like this.) # -OPTS = -O2 +OPTS = -O2 -g # Pentium with gcc 2.7.0 or better #OPTS = -O2 -fomit-frame-pointer -malign-functions=2 -malign-loops=2 \ @@ -147,7 +147,8 @@ CURSES_OBJECT = $(CURSES_DIR)/ux_init.o \ $(CURSES_DIR)/ux_screen.o \ $(CURSES_DIR)/ux_text.o \ $(CURSES_DIR)/ux_blorb.o \ - $(CURSES_DIR)/ux_audio.o + $(CURSES_DIR)/ux_audio.o \ + $(CURSES_DIR)/ux_resource.o # $(CURSES_DIR)/ux_audio_none.o \ # $(CURSES_DIR)/ux_audio_oss.o @@ -192,6 +193,8 @@ CURSES_DEFS = $(OPT_DEFS) $(COLOR_DEFS) $(SOUND_DEFS) $(SOUNDCARD) \ FLAGS = $(OPTS) $(CURSES_DEFS) $(INCL) +SOUND_LIB = -lao -ldl -lm + $(NAME): $(NAME)-curses curses: $(NAME)-curses $(NAME)-curses: $(COMMON_TARGET) $(CURSES_TARGET) $(BLORB_TARGET) diff --git a/src/curses/ux_blorb.c b/src/curses/ux_blorb.c index b25649a..671da51 100644 --- a/src/curses/ux_blorb.c +++ b/src/curses/ux_blorb.c @@ -71,7 +71,7 @@ static int isblorb(FILE *fp) /* - * ux_init_blorb + * ux_blorb_init * * Check if we're opening a Blorb file directly. If not, check * to see if there's a seperate Blorb file that looks like it goes @@ -79,9 +79,9 @@ static int isblorb(FILE *fp) * other, make a Blorb map. If we opened a Blorb file directly, that * means that our executable is in that file and therefore we will look * for a ZCOD chunk and record its location so os_load_story() can find it. - * + * Make sure the Blorb file is opened and with the file pointer blorb_fp. */ -bb_err_t ux_init_blorb(char *filename) +bb_err_t ux_blorb_init(char *filename) { FILE *fp; char *p; @@ -91,16 +91,20 @@ bb_err_t ux_init_blorb(char *filename) bb_err_t blorb_err; +/* + blorb_map = NULL; + blorb_res = NULL; +*/ + if ((fp = fopen(filename, "rb")) == NULL) return bb_err_Read; - printf("filename: %s\n", filename); - /* Is this really a Blorb file? If not, maybe we're loading a naked * zcode file and our resources are in a seperate blorb file. */ if (isblorb(fp)) { /* Now we know to look */ u_setup.exec_in_blorb = 1; /* for zcode in the blorb */ + blorb_fp = fopen(filename, "rb"); } else { len1 = strlen(filename) + strlen(EXT_BLORB); len2 = strlen(filename) + strlen(EXT_BLORBLONG); @@ -112,18 +116,27 @@ bb_err_t ux_init_blorb(char *filename) strncat(mystring, EXT_BLORB, len1 * sizeof(char)); + /* Done monkeying with the initial file. */ + fclose(fp); + fp = NULL; + + /* Check if foo.blb is there. */ - if ((fp = fopen(mystring, "rb")) == NULL) { + if ((blorb_fp = fopen(mystring, "rb")) == NULL) { p = rindex(mystring, '.'); *p = '\0'; strncat(mystring, EXT_BLORBLONG, len2 * sizeof(char)); - fp = fopen(mystring, "rb"); + blorb_fp = fopen(mystring, "rb"); + printf("Got a blorb_fp\n"); } - if (fp == NULL || !isblorb(fp)) /* No matching blorbs found. */ + + if (blorb_fp == NULL || !isblorb(fp)) /* No matching blorbs found. */ return bb_err_NoBlorb; - u_setup.exec_in_blorb = 0; /* Using naked zcode with */ - } /* resources in seperate blorb. */ + /* At this point we know that we're using a naked zcode file */ + /* with resources in a seperate Blorb file. */ + u_setup.use_blorb = 1; + } /* Create a Blorb map from this file. * This will fail if the file is not a valid Blorb file. @@ -139,17 +152,25 @@ bb_err_t ux_init_blorb(char *filename) if (u_setup.exec_in_blorb) { blorb_err = bb_load_chunk_by_type(blorb_map, bb_method_FilePos, &blorb_res, bb_make_id('Z','C','O','D'), 0); - } - - if (blorb_err == bb_err_None) { u_setup.exec_in_blorb = 1; - u_setup.use_blorb = 1; } fclose(fp); return blorb_err; } +/* + * ux_stop_blorb + * + * Basically just close the Blorb file. + * + */ +void ux_stop_blorb(void) +{ + if (blorb_fp != NULL) + fclose(blorb_fp); + blorb_fp = NULL; +} char *findchunk(char *data, char *string, int length) { diff --git a/src/curses/ux_blorb.h b/src/curses/ux_blorb.h index 1c03a41..713ea9d 100644 --- a/src/curses/ux_blorb.h +++ b/src/curses/ux_blorb.h @@ -17,16 +17,29 @@ typedef struct blorb_data_struct { } blorb_data_t; */ +/* + * The bb_result_t struct lacks a few members that would make things a + * bit easier. The myresource struct takes encapsulates the bb_result_t + * struct and adds a type member and a filepointer. I would like to + * convince Andrew Plotkin to make a change in the reference Blorb code + * to add these members. + * + */ typedef struct { bb_result_t bbres; ulong type; - FILE *file; + FILE *fp; } myresource; //bb_err_t blorb_err; bb_map_t *blorb_map; bb_result_t blorb_res; +FILE *blorb_fp; + + +int sf_getresource( int num, int ispic, int method, myresource * res); +void sf_freeresource( myresource *res); /* uint32 *findchunk(uint32 *data, char *chunkID, int length); */ char *findchunk(char *pstart, char *fourcc, int n); diff --git a/src/curses/ux_init.c b/src/curses/ux_init.c index a7ff359..ab52389 100644 --- a/src/curses/ux_init.c +++ b/src/curses/ux_init.c @@ -86,17 +86,18 @@ void os_fatal (const char *s, ...) { if (u_setup.curses_active) { - /* Solaris 2.6's cc complains if the below cast is missing */ - os_display_string((zchar *)"\n\n"); - os_beep(BEEP_HIGH); - os_set_text_style(BOLDFACE_STYLE); - os_display_string((zchar *)"Fatal error: "); - os_set_text_style(0); - os_display_string((zchar *)s); - os_display_string((zchar *)"\n"); - new_line(); - os_reset_screen(); - exit(1); + /* Solaris 2.6's cc complains if the below cast is missing */ + os_display_string((zchar *)"\n\n"); + os_beep(BEEP_HIGH); + os_set_text_style(BOLDFACE_STYLE); + os_display_string((zchar *)"Fatal error: "); + os_set_text_style(0); + os_display_string((zchar *)s); + os_display_string((zchar *)"\n"); + new_line(); + os_reset_screen(); + ux_stop_blorb(); + exit(1); } fputs ("\nFatal error: ", stderr); @@ -145,8 +146,8 @@ void os_process_arguments (int argc, char *argv[]) #ifndef WIN32 if ((getuid() == 0) || (geteuid() == 0)) { - printf("I won't run as root!\n"); - exit(1); + printf("I won't run as root!\n"); + exit(1); } #endif @@ -157,8 +158,8 @@ void os_process_arguments (int argc, char *argv[]) #endif if ((home = getenv(HOMEDIR)) == NULL) { - printf("Hard drive on fire!\n"); - exit(1); + printf("Hard drive on fire!\n"); + exit(1); } /* @@ -309,15 +310,6 @@ void os_process_arguments (int argc, char *argv[]) printf("f_setup.story_path %s\n", f_setup.story_path); */ - switch (c = ux_init_blorb(f_setup.story_file)) { - case bb_err_Format: - printf("Blorb file loaded, but unable to build map.\n\n"); - break; - case bb_err_NotFound: - printf("Blorb file loaded, but lacks executable chunk.\n\n"); - break; - } - }/* os_process_arguments */ @@ -536,12 +528,30 @@ FILE *os_load_story(void) { FILE *fp; -printf("Loading %s\n", f_setup.story_file); + switch (ux_blorb_init(f_setup.story_file)) { + case bb_err_NoBlorb: + printf("No blorb file found.\n\n"); + break; + case bb_err_Format: + printf("Blorb file loaded, but unable to build map.\n\n"); + break; + case bb_err_NotFound: + printf("Blorb file loaded, but lacks executable chunk.\n\n"); + break; + case bb_err_None: + printf("No blorb errors.\n\n"); + break; + } - fp = fopen(f_setup.story_file, "rb"); + ux_initsound(); +// os_start_sample(3, 8, 1, 0); +// exit(1); + printf("Loading %s\n", f_setup.story_file); + + fp = fopen(f_setup.story_file, "rb"); - /* Did we build a valid blorb map? */ + /* Is this a Blorb file containing Zcode? */ if (u_setup.exec_in_blorb) fseek(fp, blorb_res.data.startpos, SEEK_SET); diff --git a/src/curses/ux_resource.c b/src/curses/ux_resource.c new file mode 100644 index 0000000..33245a4 --- /dev/null +++ b/src/curses/ux_resource.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include "ux_frotz.h" +#include "ux_blorb.h" + +/* + * ux_getresource + * + * Load a bb_result_t struct (encapsulated within myresource struct) + * with information about a resource. We need two other facts about the + * chunk to do something intelligent later: a filepointer and type + * (gotten from the bb_map_t struct). I'll have to talk to Zarf about + * possibly modifying the bb_result_t struct in the reference version of + * the Blorb library. + * + */ +int ux_getresource( int num, int ispic, int method, myresource * res) +{ + int st; + ulong usage; + + res->bbres.data.ptr = NULL; + res->fp = NULL; + + if (!blorb_map) return bb_err_NotFound; + + if (ispic) + usage = bb_ID_Pict; + else + usage = bb_ID_Snd; + + st = bb_load_resource(blorb_map, method, (bb_result_t *) res, usage, num); + + if (st == bb_err_None) { + res->type = blorb_map->chunks[res->bbres.chunknum].type; + if (method == bb_method_FilePos) + res->fp = blorb_fp; + } + return st; +} + +/* + * ux_freeresource + * + * Destroys a myresource struct and returns the memory to the heap. + * + */ + +int ux_freeresource(myresource *res) +{ + if (res == NULL) + return; + + if (blorb_map != NULL) + return bb_unload_chunk(blorb_map, res->bbres.chunknum); +} -- 2.34.1