From: Holger Schemel Date: Mon, 16 Nov 1998 18:31:33 +0000 (+0100) Subject: rnd-19981116-1 X-Git-Tag: 1.2.0^2~24 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=c878cb2be6a0bffee850bf4f2dcb1939d5d2cd4f;p=rocksndiamonds.git rnd-19981116-1 --- diff --git a/src/files.c b/src/files.c index ac1b0fa7..c14b9d99 100644 --- a/src/files.c +++ b/src/files.c @@ -12,6 +12,9 @@ ***********************************************************/ #include +#include +#include +#include #include "files.h" #include "tools.h" @@ -105,52 +108,6 @@ static void InitScoreDirectory(char *level_subdir) createDirectory(getScoreDir(level_subdir), "level score"); } -boolean LoadLevelInfo() -{ - int i; - char filename[MAX_FILENAME_LEN]; - char cookie[MAX_FILENAME_LEN]; - FILE *file; - - sprintf(filename, "%s/%s", options.level_directory, LEVDIR_FILENAME); - - if (!(file = fopen(filename, "r"))) - { - Error(ERR_WARN, "cannot read level info '%s'", filename); - return(FALSE); - } - - fscanf(file, "%s\n", cookie); - if (strcmp(cookie, LEVELDIR_COOKIE)) - { - Error(ERR_WARN, "wrong format of level info file"); - fclose(file); - return(FALSE); - } - - num_leveldirs = 0; - leveldir_nr = 0; - for(i=0; inext = NULL; freeSetupFileList(setup_file_list); - if (!first_valid_list_entry) - Error(ERR_WARN, "setup file is empty"); + if (first_valid_list_entry == NULL) + Error(ERR_WARN, "setup/info file '%s' is empty", filename); return first_valid_list_entry; } @@ -875,7 +849,7 @@ static void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list, { if (strcmp(setup_file_list->value, identifier) != 0) { - Error(ERR_WARN, "setup file has wrong version"); + Error(ERR_WARN, "setup/info file has wrong version"); return; } else @@ -886,15 +860,25 @@ static void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list, checkSetupFileListIdentifier(setup_file_list->next, identifier); else { - Error(ERR_WARN, "setup file has no version information"); + Error(ERR_WARN, "setup/info file has no version information"); return; } } +static void setLevelDirInfoToDefaults(struct LevelDirInfo *ldi) +{ + ldi->name = getStringCopy("non-existing"); + ldi->levels = 0; + ldi->sort_priority = 999; /* default: least priority */ + ldi->readonly = TRUE; +} + static void setSetupInfoToDefaults(struct SetupInfo *si) { int i; + si->player_name = getStringCopy(getLoginName()); + si->sound = TRUE; si->sound_loops = FALSE; si->sound_music = FALSE; @@ -908,15 +892,10 @@ static void setSetupInfoToDefaults(struct SetupInfo *si) si->autorecord = FALSE; si->quick_doors = FALSE; - strncpy(si->login_name, getLoginName(), MAX_NAMELEN-1); - si->login_name[MAX_NAMELEN-1] = '\0'; - strncpy(si->alias_name, getLoginName(), MAX_NAMELEN-1); - si->alias_name[MAX_NAMELEN-1] = '\0'; - for (i=0; iinput[i].use_joystick = FALSE; - strcpy(si->input[i].joy.device_name, joystick_device_name[i]); + si->input[i].joy.device_name = getStringCopy(joystick_device_name[i]); si->input[i].joy.xleft = JOYSTICK_XLEFT; si->input[i].joy.xmiddle = JOYSTICK_XMIDDLE; si->input[i].joy.xright = JOYSTICK_XRIGHT; @@ -959,7 +938,9 @@ static void setSetupInfo(int token_nr, char *token_value) break; case TYPE_STRING: - strcpy((char *)setup_value, token_value); + if (*(char **)setup_value != NULL) + free(*(char **)setup_value); + *(char **)setup_value = getStringCopy(token_value); break; default: @@ -1039,6 +1020,85 @@ int getLastPlayedLevelOfLevelSeries(char *level_series_name) return last_level_nr; } +void LoadLevelInfo() +{ + DIR *dir; + struct stat file_status; + char *level_directory = options.level_directory; + char *directory = NULL; + char *filename = NULL; + struct SetupFileList *setup_file_list = NULL; + struct dirent *dir_entry; + int i, num_entries = 0; + + if ((dir = opendir(level_directory)) == NULL) + Error(ERR_EXIT, "cannot read level directory '%s'", level_directory); + + while (num_entries < MAX_LEVDIR_ENTRIES) + { + if ((dir_entry = readdir(dir)) == NULL) /* last directory entry */ + break; + + /* skip entries for current and parent directory */ + if (strcmp(dir_entry->d_name, ".") == 0 || + strcmp(dir_entry->d_name, "..") == 0) + continue; + + /* find out if directory entry is itself a directory */ + directory = getPath2(level_directory, dir_entry->d_name); + if (stat(directory, &file_status) != 0 || /* cannot stat file */ + (file_status.st_mode & S_IFMT) != S_IFDIR) /* not a directory */ + { + free(directory); + continue; + } + + if (strlen(dir_entry->d_name) >= MAX_LEVDIR_FILENAME) + { + Error(ERR_WARN, "filename of level directory '%s' too long -- ignoring", + dir_entry->d_name); + continue; + } + + filename = getPath2(directory, LEVELINFO_FILENAME); + setup_file_list = loadSetupFileList(filename); + + if (setup_file_list) + { + checkSetupFileListIdentifier(setup_file_list, LEVELINFO_COOKIE); + setLevelDirInfoToDefaults(&leveldir[num_entries]); + + ldi = leveldir[num_entries]; + for (i=FIRST_LEVELINFO_TOKEN; i<=LAST_LEVELINFO_TOKEN; i++) + setSetupInfo(i, getTokenValue(setup_file_list, token_info[i].text)); + leveldir[num_entries] = ldi; + + leveldir[num_entries].filename = getStringCopy(dir_entry->d_name); + + freeSetupFileList(setup_file_list); + num_entries++; + } + else + Error(ERR_WARN, "ignoring level directory '%s'", directory); + + free(directory); + free(filename); + } + + if (num_entries == MAX_LEVDIR_ENTRIES) + Error(ERR_WARN, "using %d level directories -- ignoring the rest", + num_entries); + + closedir(dir); + + num_leveldirs = num_entries; + leveldir_nr = 0; + + if (!num_leveldirs) + Error(ERR_EXIT, "cannot find any valid level series in directory '%s'", + level_directory); +} + void LoadSetup() { char filename[MAX_FILENAME_LEN]; @@ -1059,6 +1119,18 @@ void LoadSetup() setup.direct_draw = !setup.double_buffering; freeSetupFileList(setup_file_list); + + /* needed to work around problems with fixed length strings */ + if (strlen(setup.player_name) >= MAX_NAMELEN) + setup.player_name[MAX_NAMELEN - 1] = '\0'; + else if (strlen(setup.player_name) < MAX_NAMELEN - 1) + { + char *new_name = checked_malloc(MAX_NAMELEN); + + strcpy(new_name, setup.player_name); + free(setup.player_name); + setup.player_name = new_name; + } } else Error(ERR_WARN, "using default setup values"); @@ -1117,7 +1189,7 @@ static char *getSetupLine(char *prefix, int token_nr) break; case TYPE_STRING: - strcat(entry, (char *)setup_value); + strcat(entry, *(char **)setup_value); break; default: @@ -1151,11 +1223,11 @@ void SaveSetup() si = setup; for (i=FIRST_GLOBAL_SETUP_TOKEN; i<=LAST_GLOBAL_SETUP_TOKEN; i++) { + fprintf(file, "%s\n", getSetupLine("", i)); + /* just to make things nicer :) */ - if (i == SETUP_TOKEN_ALIAS_NAME) + if (i == SETUP_TOKEN_PLAYER_NAME) fprintf(file, "\n"); - - fprintf(file, "%s\n", getSetupLine("", i)); } /* handle player specific setup values */ @@ -1203,7 +1275,11 @@ void LoadLevelSetup() checkSetupFileListIdentifier(level_setup_list, LEVELSETUP_COOKIE); } else + { + level_setup_list = newSetupFileList(TOKEN_STR_FILE_IDENTIFIER, + LEVELSETUP_COOKIE); Error(ERR_WARN, "using default setup values"); + } } void SaveLevelSetup() diff --git a/src/files.h b/src/files.h index a8dd2774..429fe63f 100644 --- a/src/files.h +++ b/src/files.h @@ -16,8 +16,6 @@ #include "main.h" -boolean LoadLevelInfo(void); - void LoadLevel(int); void SaveLevel(int); @@ -29,6 +27,7 @@ void SaveScore(int); int getLastPlayedLevelOfLevelSeries(char *); +void LoadLevelInfo(void); void LoadSetup(void); void SaveSetup(void); void LoadLevelSetup(void); diff --git a/src/game.c b/src/game.c index 017adc47..13ae62bb 100644 --- a/src/game.c +++ b/src/game.c @@ -572,7 +572,7 @@ boolean NewHiScore() LoadScore(level_nr); - if (!strcmp(setup.alias_name,EMPTY_ALIAS) || + if (!strcmp(setup.player_name, EMPTY_ALIAS) || local_player->score < highscore[MAX_SCORE_ENTRIES-1].Score) return(-1); @@ -588,7 +588,7 @@ boolean NewHiScore() #ifdef ONE_PER_NAME for(l=k;lscore; position = k; break; } #ifdef ONE_PER_NAME - else if (!strcmp(setup.alias_name,highscore[k].Name)) + else if (!strncmp(setup.player_name, highscore[k].Name, MAX_NAMELEN - 1)) break; /* Spieler schon mit besserer Punktzahl in der Liste */ #endif diff --git a/src/image.c b/src/image.c index 675240d0..3cc85e34 100644 --- a/src/image.c +++ b/src/image.c @@ -14,18 +14,6 @@ #include "image.h" #include "misc.h" - -#ifdef DEBUG - -#define DEBUG_TIMING - -#endif - -#ifdef DEBUG_TIMING - long count1, count2; -#endif - - /* extra colors to try allocating in private color maps to minimize flashing */ #define NOFLASH_COLORS 256 @@ -91,36 +79,29 @@ static Pixmap Image_to_Mask(Image *image, Display *display, Window window) return mask_pixmap; } -/* find the best pixmap depth supported by the server for a particular - visual and return that depth */ - -static unsigned int bitsPerPixelAtDepth(Display *display, int screen, - unsigned int depth) +static int bitsPerPixelAtDepth(Display *display, int screen, int depth) { - XPixmapFormatValues *xf; - int nxf, a; + XPixmapFormatValues *pixmap_format; + int i, num_pixmap_formats, bits_per_pixel = -1; - xf = XListPixmapFormats(display, &nxf); - for (a = 0; a < nxf; a++) - { - if (xf[a].depth == depth) - { - int bpp; - bpp = xf[a].bits_per_pixel; - XFree(xf); - return (unsigned int) bpp; - } - } - XFree(xf); + /* get Pixmap formats supported by the X server */ + pixmap_format = XListPixmapFormats(display, &num_pixmap_formats); + + /* find format that matches the given depth */ + for (i=0; idisplay = display; - ximageinfo->screen = screen; ximageinfo->depth = depth; switch (visual->class) @@ -369,16 +349,13 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual, break; default: - Error(ERR_RETURN, "display type not supported"); + Error(ERR_RETURN, "display class not supported"); Error(ERR_EXIT, "DirectColor, TrueColor or PseudoColor display needed"); break; } -#ifdef DEBUG_TIMING - count2 = Counter(); - printf(" CONVERTING IMAGE TO XIMAGE (IMAGE COLORMAP) IN %.2f SECONDS\n", - (float)(count2-count1)/1000.0); - count1 = Counter(); +#if DEBUG_TIMING + debug_print_timestamp(2, " ALLOCATING IMAGE COLORS: "); #endif /* create XImage from internal image structure and convert it to Pixmap */ @@ -443,7 +420,7 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual, } default: - Error(ERR_RETURN, "display type not supported"); + Error(ERR_RETURN, "display class not supported"); Error(ERR_EXIT, "DirectColor, TrueColor or PseudoColor display needed"); break; } @@ -455,11 +432,8 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual, free((byte *)bluevalue); } -#ifdef DEBUG_TIMING - count2 = Counter(); - printf(" CONVERTING IMAGE TO XIMAGE IN %.2f SECONDS\n", - (float)(count2-count1)/1000.0); - count1 = Counter(); +#if DEBUG_TIMING + debug_print_timestamp(2, " CONVERTING IMAGE TO XIMAGE:"); #endif ximageinfo->pixmap = XCreatePixmap(display, window, @@ -524,21 +498,19 @@ int Read_PCX_to_Pixmaps(Display *display, Window window, GC gc, char *filename, XImageInfo *ximageinfo; int screen; Visual *visual; - unsigned int depth; + int depth; -#ifdef DEBUG_TIMING - count1 = Counter(); +#if DEBUG_TIMING + debug_print_timestamp(2, NULL); /* initialize timestamp function */ #endif /* read the graphic file in PCX format to image structure */ if ((image = Read_PCX_to_Image(filename)) == NULL) return PCX_FileInvalid; -#ifdef DEBUG_TIMING - count2 = Counter(); - printf(" LOADING '%s' IN %.2f SECONDS\n", - filename, (float)(count2-count1)/1000.0); - count1 = Counter(); +#if DEBUG_TIMING + printf("%s:\n", filename); + debug_print_timestamp(2, " READING PCX FILE TO IMAGE: "); #endif screen = DefaultScreen(display); @@ -554,21 +526,15 @@ int Read_PCX_to_Pixmaps(Display *display, Window window, GC gc, char *filename, if (ximageinfo->cmap != DefaultColormap(display, screen)) XSetWindowColormap(display, window, ximageinfo->cmap); -#ifdef DEBUG_TIMING - count2 = Counter(); - printf(" CONVERTING IMAGE TO PIXMAP IN %.2f SECONDS\n", - (float)(count2-count1)/1000.0); - count1 = Counter(); +#if DEBUG_TIMING + debug_print_timestamp(2, " CONVERTING IMAGE TO PIXMAP:"); #endif /* create clip mask for the image */ ximageinfo->pixmap_mask = Image_to_Mask(image, display, window); -#ifdef DEBUG_TIMING - count2 = Counter(); - printf(" CONVERTING IMAGE TO MASK IN %.2f SECONDS\n", - (float)(count2-count1)/1000.0); - count1 = Counter(); +#if DEBUG_TIMING + debug_print_timestamp(2, " CONVERTING IMAGE TO MASK: "); #endif *pixmap = ximageinfo->pixmap; diff --git a/src/image.h b/src/image.h index 50335eea..5f96d592 100644 --- a/src/image.h +++ b/src/image.h @@ -23,7 +23,6 @@ typedef unsigned short Intensity; /* RGB intensity for X11 */ typedef struct { Display *display; /* destination display */ - int screen; /* destination screen */ int depth; /* depth of destination drawable */ Pixel index[MAX_COLORS]; /* array of pixel values */ int no; /* number of pixels in the array */ diff --git a/src/init.c b/src/init.c index 37c0a1fe..a8039334 100644 --- a/src/init.c +++ b/src/init.c @@ -24,12 +24,6 @@ #include "network.h" #include "netserv.h" -#ifdef DEBUG - -#define DEBUG_TIMING - -#endif - struct PictureFileInfo { char *picture_filename; @@ -104,9 +98,7 @@ void InitLevelAndPlayerInfo() local_player->connected = TRUE; - if (!LoadLevelInfo()) /* global level info */ - Error(ERR_EXIT, NULL); - + LoadLevelInfo(); /* global level info */ LoadSetup(); /* global setup info */ LoadLevelSetup(); /* info about last played level */ } @@ -123,7 +115,7 @@ void InitNetworkServer() if (!ConnectToServer(options.server_host, options.server_port)) Error(ERR_EXIT, "cannot connect to multiplayer server"); - SendToServer_Nickname(setup.alias_name); + SendToServer_Nickname(setup.player_name); SendToServer_ProtocolVersion(); if (nr_wanted) @@ -502,9 +494,8 @@ void InitGfx() { -1, 0 } }; -#ifdef DEBUG_TIMING - long count1, count2; - count1 = Counter(); +#if DEBUG_TIMING + debug_print_timestamp(0, NULL); /* initialize timestamp function */ #endif LoadGfx(PIX_SMALLFONT,&pic[PIX_SMALLFONT]); @@ -520,12 +511,10 @@ void InitGfx() if (i != PIX_SMALLFONT) LoadGfx(i,&pic[i]); -#ifdef DEBUG_TIMING - count2 = Counter(); - printf("SUMMARY: %.2f SECONDS LOADING TIME\n",(float)(count2-count1)/1000.0); +#if DEBUG_TIMING + debug_print_timestamp(0, "SUMMARY LOADING ALL GRAPHICS:"); #endif - pix[PIX_DB_BACK] = XCreatePixmap(display, window, WIN_XSIZE,WIN_YSIZE, XDefaultDepth(display,screen)); @@ -632,7 +621,7 @@ void LoadGfx(int pos, struct PictureFileInfo *pic) char basefilename[256]; char filename[256]; -#ifdef XPM_INCLUDE_FILE +#ifdef USE_XPM_LIBRARY int xpm_err, xbm_err; unsigned int width,height; int hot_x,hot_y; @@ -644,10 +633,6 @@ void LoadGfx(int pos, struct PictureFileInfo *pic) char *picture_ext = ".pcx"; #endif -#ifdef DEBUG_TIMING - long count1, count2; -#endif - /* Grafik laden */ if (pic->picture_filename) { @@ -660,17 +645,16 @@ void LoadGfx(int pos, struct PictureFileInfo *pic) rest(100); #endif MSDOS -#ifdef DEBUG_TIMING - count1 = Counter(); +#if DEBUG_TIMING + debug_print_timestamp(1, NULL); /* initialize timestamp function */ #endif -#ifdef XPM_INCLUDE_FILE +#ifdef USE_XPM_LIBRARY xpm_att[pos].valuemask = XpmCloseness; xpm_att[pos].closeness = 20000; xpm_err = XpmReadFileToPixmap(display,window,filename, &pix[pos],&shapemask,&xpm_att[pos]); - switch(xpm_err) { case XpmOpenFailed: @@ -685,17 +669,15 @@ void LoadGfx(int pos, struct PictureFileInfo *pic) break; } -#ifdef DEBUG_TIMING - count2 = Counter(); - printf("XPM LOADING %s IN %.2f SECONDS\n", - filename,(float)(count2-count1)/1000.0); +#if DEBUG_TIMING + printf("LOADING XPM FILE %s:", filename); + debug_print_timestamp(1, ""); #endif -#else +#else /* !USE_XPM_LIBRARY */ pcx_err = Read_PCX_to_Pixmaps(display, window, gc, filename, &pix[pos], &clipmask[pos]); - switch(pcx_err) { case PCX_Success: @@ -714,13 +696,12 @@ void LoadGfx(int pos, struct PictureFileInfo *pic) break; } -#ifdef DEBUG_TIMING - count2 = Counter(); - printf("PCX LOADING %s IN %.2f SECONDS\n", - filename,(float)(count2-count1)/1000.0); +#if DEBUG_TIMING + printf("SUMMARY LOADING PCX FILE %s:", filename); + debug_print_timestamp(1, ""); #endif -#endif +#endif /* !USE_XPM_LIBRARY */ if (!pix[pos]) Error(ERR_EXIT, "cannot get graphics for '%s'", pic->picture_filename); @@ -729,21 +710,19 @@ void LoadGfx(int pos, struct PictureFileInfo *pic) /* zugehörige Maske laden (wenn vorhanden) */ if (pic->picture_with_mask) { - -#ifdef XPM_INCLUDE_FILE +#ifdef USE_XPM_LIBRARY sprintf(basefilename, "%s%s", pic->picture_filename, picturemask_ext); DrawInitText(basefilename, 150, FC_YELLOW); sprintf(filename, "%s/%s/%s", options.base_directory, GRAPHICS_DIRECTORY, basefilename); -#ifdef DEBUG_TIMING - count1 = Counter(); +#if DEBUG_TIMING + debug_print_timestamp(1, NULL); /* initialize timestamp function */ #endif xbm_err = XReadBitmapFile(display,window,filename, &width,&height,&clipmask[pos],&hot_x,&hot_y); - switch(xbm_err) { case BitmapSuccess: @@ -759,13 +738,12 @@ void LoadGfx(int pos, struct PictureFileInfo *pic) break; } -#ifdef DEBUG_TIMING - count2 = Counter(); - printf("XBM LOADING %s IN %.2f SECONDS\n", - filename,(float)(count2-count1)/1000.0); +#if DEBUG_TIMING + printf("LOADING XBM FILE %s:", filename); + debug_print_timestamp(1, ""); #endif -#endif +#endif /* USE_XPM_LIBRARY */ if (!clipmask[pos]) Error(ERR_EXIT, "cannot get clipmask for '%s'", pic->picture_filename); @@ -1341,8 +1319,8 @@ void CloseAllAndExit(int exit_value) { if (pix[i]) { -#ifdef XPM_INCLUDE_FILE - if (i #ifdef XPM_INCLUDE_FILE +#define USE_XPM_LIBRARY #include XPM_INCLUDE_FILE #endif #else /* MSDOS */ #include "msdos.h" #endif /* MSDOS */ +#ifdef DEBUG +#define DEBUG_TIMING 0 +#endif + typedef unsigned char boolean; typedef unsigned char byte; @@ -222,7 +227,7 @@ struct OptionInfo struct SetupJoystickInfo { - char device_name[MAX_FILENAME_LEN]; + char *device_name; int xleft, xmiddle, xright; int yupper, ymiddle, ylower; int snap; @@ -248,6 +253,8 @@ struct SetupInputInfo struct SetupInfo { + char *player_name; + boolean sound; boolean sound_loops; boolean sound_music; @@ -262,9 +269,6 @@ struct SetupInfo boolean quick_doors; boolean team_mode; - char login_name[MAX_NAMELEN]; - char alias_name[MAX_NAMELEN]; - struct SetupInputInfo input[MAX_PLAYERS]; }; @@ -335,10 +339,11 @@ struct LevelInfo struct LevelDirInfo { - char filename[MAX_LEVDIR_FILENAME]; - char name[MAX_LEVDIR_NAME]; + char *filename; + char *name; int levels; - int readonly; + int sort_priority; + boolean readonly; }; struct RecordingInfo @@ -369,7 +374,7 @@ extern GC gc, clip_gc[], tile_clip_gc; extern Pixmap pix[]; extern Pixmap clipmask[], tile_clipmask[]; -#ifdef XPM_INCLUDE_FILE +#ifdef USE_XPM_LIBRARY extern XpmAttributes xpm_att[]; #endif @@ -1083,16 +1088,16 @@ extern int num_bg_loops; #ifndef MSDOS #define USERDATA_DIRECTORY ".rocksndiamonds" -#define LEVDIR_FILENAME "ROCKS.levelinfo" -#define SETUP_FILENAME "setup" -#define LEVELSETUP_FILENAME "setup.level" +#define SETUP_FILENAME "setup.conf" +#define LEVELSETUP_FILENAME "levelsetup.conf" +#define LEVELINFO_FILENAME "levelinfo.conf" #define TAPEFILE_EXTENSION "tape" #define SCOREFILE_EXTENSION "score" #else #define USERDATA_DIRECTORY "userdata" -#define LEVDIR_FILENAME "ROCKS.lev" -#define SETUP_FILENAME "setup" -#define LEVELSETUP_FILENAME "setup.lev" +#define SETUP_FILENAME "setup.cnf" +#define LEVELSETUP_FILENAME "lvlsetup.cnf" +#define LEVELINFO_FILENAME "lvlinfo.cnf" #define TAPEFILE_EXTENSION "rec" #define SCOREFILE_EXTENSION "sco" #endif @@ -1119,6 +1124,7 @@ extern int num_bg_loops; #define JOYSTICK_COOKIE "ROCKSNDIAMONDS_JOYSTICK_FILE_VERSION_1.0" #define SETUP_COOKIE "ROCKSNDIAMONDS_SETUP_FILE_VERSION_1.2" #define LEVELSETUP_COOKIE "ROCKSNDIAMONDS_LEVELSETUP_FILE_VERSION_1.2" +#define LEVELINFO_COOKIE "ROCKSNDIAMONDS_LEVELINFO_FILE_VERSION_1.2" #define LEVEL_COOKIE_LEN (strlen(LEVEL_COOKIE)+1) #define SCORE_COOKIE_LEN (strlen(SCORE_COOKIE)+1) #define LEVELDIR_COOKIE_LEN (strlen(LEVELDIR_COOKIE)+1) @@ -1126,6 +1132,7 @@ extern int num_bg_loops; #define JOYSTICK_COOKIE_LEN (strlen(JOYSTICK_COOKIE)+1) #define SETUP_COOKIE_LEN (strlen(SETUP_COOKIE)+1) #define LEVELSETUP_COOKIE_LEN (strlen(LEVELSETUP_COOKIE)+1) +#define LEVELINFO_COOKIE_LEN (strlen(LEVELINFO_COOKIE)+1) #define VERSION_STRING "1.2 preview 1" #define GAMETITLE_STRING "Rocks'n'Diamonds" diff --git a/src/misc.c b/src/misc.c index a7e52dcc..8c88fe29 100644 --- a/src/misc.c +++ b/src/misc.c @@ -210,6 +210,22 @@ char *getHomeDir() return home_dir; } +char *getPath2(char *path1, char *path2) +{ + char *complete_path = checked_malloc(strlen(path1) + strlen(path2) + 2); + + sprintf(complete_path, "%s/%s", path1, path2); + return complete_path; +} + +char *getStringCopy(char *s) +{ + char *s_copy = checked_malloc(strlen(s) + 1); + + strcpy(s_copy, s); + return s_copy; +} + void MarkTileDirty(int x, int y) { int xx = redraw_x1 + x; @@ -363,7 +379,7 @@ void GetOptions(char *argv[]) } } -void Error(int mode, char *format_str, ...) +void Error(int mode, char *format, ...) { FILE *output_stream = stderr; char *process_name = ""; @@ -371,53 +387,17 @@ void Error(int mode, char *format_str, ...) if (mode & ERR_SOUNDSERVER) process_name = " sound server"; - if (format_str) + if (format) { va_list ap; - char *format_ptr; - char *s_value; - int i_value; - double d_value; fprintf(output_stream, "%s%s: ", program_name, process_name); if (mode & ERR_WARN) fprintf(output_stream, "warning: "); - va_start(ap, format_str); /* ap points to first unnamed argument */ - - for(format_ptr=format_str; *format_ptr; format_ptr++) - { - if (*format_ptr != '%') - { - fprintf(output_stream, "%c", *format_ptr); - continue; - } - - switch(*++format_ptr) - { - case 'd': - i_value = va_arg(ap, int); - fprintf(output_stream, "%d", i_value); - break; - - case 'f': - d_value = va_arg(ap, double); - fprintf(output_stream, "%f", d_value); - break; - - case 's': - s_value = va_arg(ap, char *); - fprintf(output_stream, "%s", s_value); - break; - - default: - fprintf(stderr, "\n%s: Error(): invalid format string: %s\n", - program_name, format_str); - CloseAllAndExit(10); - } - } - + va_start(ap, format); + vfprintf(output_stream, format, ap); va_end(ap); fprintf(output_stream, "\n"); @@ -786,3 +766,22 @@ int getJoySymbolFromJoyName(char *name) translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL); return joysymbol; } + +/* the following is only for debugging purpose and normally not used */ +#define DEBUG_NUM_TIMESTAMPS 3 + +void debug_print_timestamp(int counter_nr, char *message) +{ + static long counter[DEBUG_NUM_TIMESTAMPS][2]; + + if (counter_nr >= DEBUG_NUM_TIMESTAMPS) + Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c"); + + counter[counter_nr][0] = Counter(); + + if (message) + printf("%s %.2f seconds\n", message, + (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000); + + counter[counter_nr][1] = Counter(); +} diff --git a/src/misc.h b/src/misc.h index c0109175..65f2966c 100644 --- a/src/misc.h +++ b/src/misc.h @@ -41,6 +41,8 @@ unsigned int RND(unsigned int); unsigned int InitRND(long); char *getLoginName(void); char *getHomeDir(void); +char *getPath2(char *, char *); +char *getStringCopy(char *); void MarkTileDirty(int, int); void GetOptions(char **); void Error(int, char *, ...); @@ -51,5 +53,6 @@ char *getX11KeyNameFromKeySym(KeySym); KeySym getKeySymFromX11KeyName(char *); char *getJoyNameFromJoySymbol(int); int getJoySymbolFromJoyName(char *); +void debug_print_timestamp(int, char *); -#endif +#endif /* MISC_H */ diff --git a/src/network.c b/src/network.c index 890f405f..76b4e66f 100644 --- a/src/network.c +++ b/src/network.c @@ -226,10 +226,11 @@ boolean ConnectToServer(char *host, int port) void SendToServer_Nickname(char *nickname) { static char msgbuf[300]; + int len_nickname = strlen(nickname); buf[1] = OP_NICKNAME; - memcpy(&buf[2], nickname, strlen(nickname)); - sendbuf(2 + strlen(nickname)); + memcpy(&buf[2], nickname, len_nickname); + sendbuf(2 + len_nickname); sprintf(msgbuf, "you set your nick to \"%s\"", nickname); sysmsg(msgbuf); } diff --git a/src/screens.c b/src/screens.c index 139c6d9c..1ff8bd1d 100644 --- a/src/screens.c +++ b/src/screens.c @@ -53,7 +53,7 @@ void DrawMainMenu() ClearWindow(); DrawHeadline(); DrawText(SX + 32, SY + 2*32, name_text, FS_BIG, FC_GREEN); - DrawText(SX + 6*32, SY + 2*32, setup.alias_name, FS_BIG, FC_RED); + DrawText(SX + 6*32, SY + 2*32, setup.player_name, FS_BIG, FC_RED); DrawText(SX + 32, SY + 3*32, "Level:", FS_BIG, FC_GREEN); DrawText(SX + 11*32, SY + 3*32, int2str(level_nr,3), FS_BIG, (level_nr0) { xpos--; - setup.alias_name[xpos] = 0; + setup.player_name[xpos] = 0; DrawGraphic(xpos+6,ypos,GFX_KUGEL_ROT); DrawGraphic(xpos+7,ypos,GFX_LEERRAUM); } else if (key==XK_Return && xpos>0) { - DrawText(SX+6*32,SY+ypos*32,setup.alias_name,FS_BIG,FC_RED); + DrawText(SX+6*32,SY+ypos*32,setup.player_name,FS_BIG,FC_RED); DrawGraphic(xpos+6,ypos,GFX_LEERRAUM); SaveSetup(); @@ -1187,7 +1187,7 @@ static void setJoystickDeviceToNr(char *device_name, int device_nr) device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10); } else - strcpy(device_name, joystick_device_name[device_nr]); + strncpy(device_name, joystick_device_name[device_nr], strlen(device_name)); } static void drawPlayerSetupInputInfo(int player_nr)