From: Holger Schemel Date: Sat, 12 Apr 2003 23:45:55 +0000 (+0200) Subject: rnd-20030413-1-src X-Git-Tag: 3.0.0^2~103 X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=commitdiff_plain;h=e1fdeb57afb3db322b3757d8105c66ba33cadca4 rnd-20030413-1-src --- diff --git a/src/conftime.h b/src/conftime.h index 59e3eb37..dbcf875c 100644 --- a/src/conftime.h +++ b/src/conftime.h @@ -1 +1 @@ -#define COMPILE_DATE_STRING "[2003-04-12 22:10]" +#define COMPILE_DATE_STRING "[2003-04-13 01:35]" diff --git a/src/editor.c b/src/editor.c index d93c67e0..d5c66bb9 100644 --- a/src/editor.c +++ b/src/editor.c @@ -3596,10 +3596,181 @@ static void DrawPropertiesMain() } } +char *getElementDescriptionFilename(int element) +{ + char *docs_dir = options.docs_directory; + char *elements_subdir = "elements"; + static char *filename = NULL; + char basename[MAX_FILENAME_LEN]; + + if (filename != NULL) + free(filename); + + /* 1st try: look for element description file for exactly this element */ + sprintf(basename, "%s.txt", element_info[element].token_name); + filename = getPath3(docs_dir, elements_subdir, basename); + if (fileExists(filename)) + return filename; + + free(filename); + + /* 2nd try: look for element description file for this element's class */ + sprintf(basename, "%s.txt", element_info[element].class_name); + filename = getPath3(docs_dir, elements_subdir, basename); + if (fileExists(filename)) + return filename; + + return NULL; +} + +static int PrintElementDescriptionFromFile(char *filename) +{ + int font_nr = FONT_TEXT_2; + int font_width = getFontWidth(font_nr); + int font_height = getFontHeight(font_nr); + int pad_x = ED_SETTINGS_XPOS; + int pad_y = 5 * TILEY; + int sx = SX + pad_x; + int sy = SY + pad_y; + int max_chars_per_line = (SXSIZE - 2 * pad_x) / font_width; + int max_lines_per_screen = (SYSIZE - pad_y) / font_height - 1; + int screen_line_nr = 0; + char line[MAX_LINE_LEN]; + char buffer[max_chars_per_line + 1]; + int buffer_len; + FILE *file; + + if (filename == NULL) + return 0; + + if (!(file = fopen(filename, MODE_READ))) + return 0; + + buffer[0] = '\0'; + buffer_len = 0; + + while(!feof(file)) + { + char *line_ptr, *word_ptr; + boolean last_line_was_empty = TRUE; + + /* read next line of input file */ + if (!fgets(line, MAX_LINE_LEN, file)) + break; + + /* skip comments (lines directly beginning with '#') */ + if (line[0] == '#') + continue; + + /* cut trailing newline from input line */ + for (line_ptr = line; *line_ptr; line_ptr++) + { + if (*line_ptr == '\n' || *line_ptr == '\r') + { + *line_ptr = '\0'; + break; + } + } + + if (strlen(line) == 0) /* special case: force empty line */ + strcpy(line, "\n"); + + word_ptr = line; + + while (*word_ptr) + { + boolean print_buffer = FALSE; + int word_len; + + /* skip leading whitespaces */ + while (*word_ptr == ' ' || *word_ptr == '\t') + word_ptr++; + + line_ptr = word_ptr; + word_len = 0; + + /* look for end of next word */ + while (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != '\0') + { + line_ptr++; + word_len++; + } + + if (word_len == 0) + { + continue; + } + else if (*word_ptr == '\n') /* special case: force empty line */ + { + if (buffer_len == 0) + word_ptr++; + + /* prevent printing of multiple empty lines */ + if (buffer_len > 0 || !last_line_was_empty) + print_buffer = TRUE; + } + else if (word_len < max_chars_per_line - buffer_len) + { + /* word fits into text buffer -- add word */ + + if (buffer_len > 0) + buffer[buffer_len++] = ' '; + + strncpy(&buffer[buffer_len], word_ptr, word_len); + buffer_len += word_len; + buffer[buffer_len] = '\0'; + word_ptr += word_len; + } + else if (buffer_len > 0) + { + /* not enough space left for word in text buffer -- print buffer */ + + print_buffer = TRUE; + } + else + { + /* word does not fit at all into empty text buffer -- cut word */ + + strncpy(buffer, word_ptr, max_chars_per_line); + buffer[max_chars_per_line] = '\0'; + word_ptr += max_chars_per_line; + print_buffer = TRUE; + } + + if (print_buffer) + { + DrawText(sx, sy + screen_line_nr * font_height, buffer, FONT_TEXT_2); + + last_line_was_empty = (buffer_len == 0); + + buffer[0] = '\0'; + buffer_len = 0; + print_buffer = FALSE; + + if (++screen_line_nr >= max_lines_per_screen) + return screen_line_nr; /* currently too much text gets cut */ + } + } + } + + if (buffer_len > 0) + { + DrawText(sx, sy + screen_line_nr * font_height, buffer, FONT_TEXT_2); + screen_line_nr++; + } + + fclose(file); + + return screen_line_nr; +} + static void DrawPropertiesInfo() { - DrawText(SX + ED_SETTINGS_XPOS, SY + 5 * TILEY, "No description available.", - FONT_TEXT_1); + char *filename = getElementDescriptionFilename(properties_element); + + if (PrintElementDescriptionFromFile(filename) == 0) + DrawText(SX + ED_SETTINGS_XPOS, SY + 5 * TILEY, + "No description available.", FONT_TEXT_1); } static void DrawPropertiesAdvanced() diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 69a69516..efb56bba 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -573,6 +573,7 @@ void GetOptions(char *argv[]) options.graphics_directory = RO_BASE_PATH "/" GRAPHICS_DIRECTORY; options.sounds_directory = RO_BASE_PATH "/" SOUNDS_DIRECTORY; options.music_directory = RO_BASE_PATH "/" MUSIC_DIRECTORY; + options.docs_directory = RO_BASE_PATH "/" DOCS_DIRECTORY; options.execute_command = NULL; options.serveronly = FALSE; options.network = FALSE; @@ -1500,9 +1501,18 @@ void dumpList(ListNode *node_first) /* ------------------------------------------------------------------------- */ -/* functions for checking filenames */ +/* functions for checking files and filenames */ /* ------------------------------------------------------------------------- */ +boolean fileExists(char *filename) +{ +#if 0 + printf("checking file '%s'\n", filename); +#endif + + return (access(filename, F_OK) == 0); +} + boolean FileIsGraphic(char *filename) { if (strlen(filename) > 4 && diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 3b9d945b..cec93e97 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -151,6 +151,7 @@ void deleteNodeFromList(ListNode **, char *, void (*function)(void *)); ListNode *getNodeFromKey(ListNode *, char *); int getNumNodes(ListNode *); +boolean fileExists(char *); boolean FileIsGraphic(char *); boolean FileIsSound(char *); boolean FileIsMusic(char *); diff --git a/src/libgame/setup.c b/src/libgame/setup.c index 68dd7434..0996d10d 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -437,15 +437,6 @@ static char *getCorrectedImageBasename(char *basename) return basename_corrected; } -static boolean fileExists(char *filename) -{ -#if 0 - printf("checking file '%s'\n", filename); -#endif - - return (access(filename, F_OK) == 0); -} - char *getCustomImageFilename(char *basename) { static char *filename = NULL; @@ -1124,7 +1115,6 @@ struct SetupFileList *loadSetupFileList(char *filename) char *token, *value, *line_ptr; struct SetupFileList *setup_file_list = newSetupFileList("", ""); struct SetupFileList *first_valid_list_entry; - FILE *file; if (!(file = fopen(filename, MODE_READ))) diff --git a/src/libgame/system.h b/src/libgame/system.h index 8126eb04..930cc113 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -193,6 +193,7 @@ #define LEVELS_DIRECTORY "levels" #define TAPES_DIRECTORY "tapes" #define SCORES_DIRECTORY "scores" +#define DOCS_DIRECTORY "docs" #if !defined(PLATFORM_MSDOS) #define GRAPHICS_SUBDIR "gfx_classic" @@ -279,6 +280,7 @@ struct OptionInfo char *graphics_directory; char *sounds_directory; char *music_directory; + char *docs_directory; char *execute_command; boolean serveronly;