From b76c9452394e9c8a11a6451bb2c14e86668b679c Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Sun, 21 Mar 2004 15:41:08 +0000 Subject: [PATCH] - replaced a few char* with std::string - load datafiles from 'datadir' instead of DATA_PREFIX SVN-Revision: 294 --- src/gameloop.cpp | 2 +- src/high_scores.cpp | 2 +- src/level.cpp | 29 ++++++++++++----------------- src/leveleditor.cpp | 2 +- src/lispreader.cpp | 8 ++++---- src/lispreader.h | 2 +- src/setup.cpp | 39 ++++++++++++++++++++------------------- src/sound.cpp | 36 ++++++++++++++++++------------------ src/text.cpp | 2 +- src/text.h | 2 +- src/title.cpp | 10 +++++----- src/worldmap.cpp | 15 ++++++++------- 12 files changed, 73 insertions(+), 76 deletions(-) diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 58f959302..859690478 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -1190,7 +1190,7 @@ void loadshared(void) Send a mail to me: neoneurone@users.sf.net, if you have another opinion. :) */ for (i = 0; i < NUM_SOUNDS; i++) - sounds[i] = load_sound(soundfilenames[i]); + sounds[i] = load_sound(datadir + soundfilenames[i]); /* Herring song */ herring_song = load_song(datadir + "/music/SALCON.MOD"); diff --git a/src/high_scores.cpp b/src/high_scores.cpp index d1b4d94ec..0fc59ddad 100644 --- a/src/high_scores.cpp +++ b/src/high_scores.cpp @@ -118,7 +118,7 @@ void save_hs(int score) FILE * fi; - texture_load(&bkgd, DATA_PREFIX "/images/highscore/highscore.png", IGNORE_ALPHA); + texture_load(&bkgd, datadir + "/images/highscore/highscore.png", IGNORE_ALPHA); hs_score = score; diff --git a/src/level.cpp b/src/level.cpp index 953f87c19..d8c20859e 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -87,7 +87,7 @@ void st_subset::load(char *subset) snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset); if(!faccessible(filename)) - snprintf(filename, 1024, "%s/levels/%s/info", DATA_PREFIX, subset); + snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset); if(faccessible(filename)) { fi = fopen(filename, "r"); @@ -126,7 +126,7 @@ void st_subset::load(char *subset) } else { - snprintf(filename, 1024, "%s/images/status/level-subset-info.png", DATA_PREFIX); + snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str()); texture_load(&image,filename,IGNORE_ALPHA); } } @@ -137,7 +137,7 @@ void st_subset::load(char *subset) snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i); if(!faccessible(filename)) { - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", DATA_PREFIX, subset,i); + snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i); if(!faccessible(filename)) break; } @@ -156,7 +156,7 @@ void st_subset::save() fcreatedir(filename.c_str()); filename = string(st_dir) + "/levels/" + name + "/info"; if(!fwriteable(filename.c_str())) - filename = string(DATA_PREFIX) + "/levels/" + name + "/info"; + filename = datadir + "/levels/" + name + "/info"; if(fwriteable(filename.c_str())) { fi = fopen(filename.c_str(), "w"); @@ -224,7 +224,7 @@ int level_load(st_level* plevel,const char *subset, int level) snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level); if(!faccessible(filename)) - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", DATA_PREFIX, subset, level); + snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level); return level_load(plevel, filename); } @@ -322,7 +322,7 @@ void level_save(st_level* plevel,const char * subset, int level) fcreatedir(str); snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level); if(!fwriteable(filename)) - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", DATA_PREFIX, subset, level); + snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level); fi = fopen(filename, "w"); if (fi == NULL) @@ -403,7 +403,7 @@ void level_load_gfx(st_level *plevel) char fname[1024]; snprintf(fname, 1024, "%s/background/%s", st_dir, plevel->bkgd_image.c_str()); if(!faccessible(fname)) - snprintf(fname, 1024, "%s/images/background/%s", DATA_PREFIX, plevel->bkgd_image.c_str()); + snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), plevel->bkgd_image.c_str()); texture_load(&img_bkgd, fname, IGNORE_ALPHA); } else @@ -441,7 +441,7 @@ void level_load_image(texture_type* ptexture, string theme,const char * file, i snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file); if(!faccessible(fname)) - snprintf(fname, 1024, "%s/images/themes/%s/%s", DATA_PREFIX, theme.c_str(), file); + snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file); texture_load(ptexture, fname, use_alpha); } @@ -475,18 +475,13 @@ void level_load_song(st_level* plevel) char * song_path; char * song_subtitle; - song_path = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) + - strlen(plevel->song_title.c_str()) + 8)); - sprintf(song_path, "%s/music/%s", DATA_PREFIX, plevel->song_title.c_str()); - level_song = load_song(song_path); - free(song_path); - + level_song = load_song(datadir + "/music/" + plevel->song_title); - song_path = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) + - strlen(plevel->song_title.c_str()) + 8 + 5)); + song_path = (char *) malloc(sizeof(char) * datadir.length() + + strlen(plevel->song_title.c_str()) + 8 + 5); song_subtitle = strdup(plevel->song_title.c_str()); strcpy(strstr(song_subtitle, "."), "\0"); - sprintf(song_path, "%s/music/%s-fast%s", DATA_PREFIX, song_subtitle, strstr(plevel->song_title.c_str(), ".")); + sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), song_subtitle, strstr(plevel->song_title.c_str(), ".")); level_song_fast = load_song(song_path); free(song_subtitle); free(song_path); diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index a7c5b8b7e..c96e436c1 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -383,7 +383,7 @@ int le_init() le_mouse_pressed[LEFT] = NO; le_mouse_pressed[RIGHT] = NO; - texture_load(&le_selection,DATA_PREFIX "/images/leveleditor/select.png", USE_ALPHA); + texture_load(&le_selection, datadir + "/images/leveleditor/select.png", USE_ALPHA); /* Load buttons */ button_load(&le_save_level_bt,"/images/icons/save.png","Save level", SDLK_F6,screen->w-64,32); diff --git a/src/lispreader.cpp b/src/lispreader.cpp index 855c8ca5f..c51cae9c8 100644 --- a/src/lispreader.cpp +++ b/src/lispreader.cpp @@ -1269,13 +1269,13 @@ bool has_suffix(const char* data, const char* suffix) } } -lisp_object_t* lisp_read_from_file(const char* filename) +lisp_object_t* lisp_read_from_file(const std::string& filename) { lisp_stream_t stream; - if (has_suffix(filename, ".gz")) + if (has_suffix(filename.c_str(), ".gz")) { - return lisp_read_from_gzfile(filename); + return lisp_read_from_gzfile(filename.c_str()); #if 0 lisp_object_t* obj = 0; gzFile in = gzopen(filename, "r"); @@ -1292,7 +1292,7 @@ lisp_object_t* lisp_read_from_file(const char* filename) else { lisp_object_t* obj = 0; - FILE* in = fopen(filename, "r"); + FILE* in = fopen(filename.c_str(), "r"); if (in) { diff --git a/src/lispreader.h b/src/lispreader.h index 5dde3b1aa..ad2fbbe69 100644 --- a/src/lispreader.h +++ b/src/lispreader.h @@ -115,7 +115,7 @@ lisp_stream_t* lisp_stream_init_any (lisp_stream_t *stream, void *data, void (*unget_char) (char c, void *data)); lisp_object_t* lisp_read (lisp_stream_t *in); -lisp_object_t* lisp_read_from_file(const char* filename); +lisp_object_t* lisp_read_from_file(const std::string& filename); void lisp_free (lisp_object_t *obj); lisp_object_t* lisp_read_from_string (const char *buf); diff --git a/src/setup.cpp b/src/setup.cpp index dc79fd069..bc9d10c6d 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -80,7 +80,7 @@ int fcreatedir(const char* relative_dir) snprintf(path, 1024, "%s/%s/", st_dir, relative_dir); if(mkdir(path,0755) != 0) { - snprintf(path, 1024, "%s/%s/", DATA_PREFIX, relative_dir); + snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir); if(mkdir(path,0755) != 0) { return NO; @@ -133,7 +133,7 @@ string_list_type dsubdirs(const char *rel_path,const char* expected_file) closedir(dirStructP); } - sprintf(path,"%s/%s",DATA_PREFIX,rel_path); + sprintf(path,"%s/%s",datadir.c_str(),rel_path); if((dirStructP = opendir(path)) != NULL) { while((direntp = readdir(dirStructP)) != NULL) @@ -204,7 +204,7 @@ string_list_type dfiles(const char *rel_path, const char* glob, const char* ex closedir(dirStructP); } - sprintf(path,"%s/%s",DATA_PREFIX,rel_path); + sprintf(path,"%s/%s",datadir.c_str(),rel_path); if((dirStructP = opendir(path)) != NULL) { while((direntp = readdir(dirStructP)) != NULL) @@ -293,6 +293,7 @@ void st_directory_setup(void) if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0) { puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX); + datadir = DATA_PREFIX; } else { @@ -487,21 +488,21 @@ void st_general_setup(void) /* Load global images: */ - text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18); - text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18); - text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18); - text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18); - text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18); - text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9); - text_load(&white_big_text,DATA_PREFIX "/images/status/letters-white-big.png", TEXT_TEXT, 20,23); - text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32); + text_load(&black_text, datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18); + text_load(&gold_text,datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18); + text_load(&blue_text,datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18); + text_load(&red_text,datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18); + text_load(&white_text,datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18); + text_load(&white_small_text,datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9); + text_load(&white_big_text,datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,23); + text_load(&yellow_nums,datadir + "/images/status/numbers.png", TEXT_NUM, 32,32); /* Load GUI/menu images: */ - texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA); - texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA); - texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA); - texture_load(&arrow_left, DATA_PREFIX "/images/icons/left.png", USE_ALPHA); - texture_load(&arrow_right, DATA_PREFIX "/images/icons/right.png", USE_ALPHA); + texture_load(&checkbox, datadir + "/images/status/checkbox.png", USE_ALPHA); + texture_load(&checkbox_checked, datadir + "/images/status/checkbox-checked.png", USE_ALPHA); + texture_load(&back, datadir + "/images/status/back.png", USE_ALPHA); + texture_load(&arrow_left, datadir + "/images/icons/left.png", USE_ALPHA); + texture_load(&arrow_right, datadir + "/images/icons/right.png", USE_ALPHA); } @@ -809,13 +810,13 @@ void seticon(void) /* Load icon into a surface: */ - icon = IMG_Load(DATA_PREFIX "/images/icon.png"); + icon = IMG_Load((datadir + "/images/icon.png").c_str()); if (icon == NULL) { fprintf(stderr, - "\nError: I could not load the icon image: %s\n" + "\nError: I could not load the icon image: %s%s\n" "The Simple DirectMedia error that occured was:\n" - "%s\n\n", DATA_PREFIX "/images/icon.png", SDL_GetError()); + "%s\n\n", datadir.c_str(), "/images/icon.png", SDL_GetError()); exit(1); } diff --git a/src/sound.cpp b/src/sound.cpp index 1cf63f9cf..9863eaf67 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -22,24 +22,24 @@ int audio_device; /* != 0: available and initialized */ int current_music; char * soundfilenames[NUM_SOUNDS] = { - DATA_PREFIX "/sounds/jump.wav", - DATA_PREFIX "/sounds/bigjump.wav", - DATA_PREFIX "/sounds/skid.wav", - DATA_PREFIX "/sounds/distro.wav", - DATA_PREFIX "/sounds/herring.wav", - DATA_PREFIX "/sounds/brick.wav", - DATA_PREFIX "/sounds/hurt.wav", - DATA_PREFIX "/sounds/squish.wav", - DATA_PREFIX "/sounds/fall.wav", - DATA_PREFIX "/sounds/ricochet.wav", - DATA_PREFIX "/sounds/bump-upgrade.wav", - DATA_PREFIX "/sounds/upgrade.wav", - DATA_PREFIX "/sounds/excellent.wav", - DATA_PREFIX "/sounds/coffee.wav", - DATA_PREFIX "/sounds/shoot.wav", - DATA_PREFIX "/sounds/lifeup.wav", - DATA_PREFIX "/sounds/stomp.wav", - DATA_PREFIX "/sounds/kick.wav" + "/sounds/jump.wav", + "/sounds/bigjump.wav", + "/sounds/skid.wav", + "/sounds/distro.wav", + "/sounds/herring.wav", + "/sounds/brick.wav", + "/sounds/hurt.wav", + "/sounds/squish.wav", + "/sounds/fall.wav", + "/sounds/ricochet.wav", + "/sounds/bump-upgrade.wav", + "/sounds/upgrade.wav", + "/sounds/excellent.wav", + "/sounds/coffee.wav", + "/sounds/shoot.wav", + "/sounds/lifeup.wav", + "/sounds/stomp.wav", + "/sounds/kick.wav" }; diff --git a/src/text.cpp b/src/text.cpp index c17d12908..9b3dee20b 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -17,7 +17,7 @@ #include "screen.h" #include "text.h" -void text_load(text_type* ptext,const char* file, int kind, int w, int h) +void text_load(text_type* ptext, const std::string& file, int kind, int w, int h) { int mx, my; SDL_Surface *conv; diff --git a/src/text.h b/src/text.h index b09004b9f..383e25f95 100644 --- a/src/text.h +++ b/src/text.h @@ -42,7 +42,7 @@ enum { A_NONE }; -void text_load(text_type* ptext, const char* file, int kind, int w, int h); +void text_load(text_type* ptext, const std::string& file, int kind, int w, int h); void text_draw(text_type* ptext, const char* text, int x, int y, int shadowsize, int update); void text_draw_chars(text_type* ptext, texture_type* pchars, const char* text, int x, int y, int update); void text_drawf(text_type* ptext, const char* text, int x, int y, int halign, int valign, int shadowsize, int update); diff --git a/src/title.cpp b/src/title.cpp index 34af37351..517c0047d 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -81,10 +81,10 @@ int title(void) /* Load images: */ - texture_load(&bkg_title,DATA_PREFIX "/images/title/title.png", IGNORE_ALPHA); - texture_load(&anim1,DATA_PREFIX "/images/title/title-anim2.png", IGNORE_ALPHA); - texture_load(&anim2,DATA_PREFIX "/images/title/title-anim1.png", IGNORE_ALPHA); - texture_load(&img_choose_subset,DATA_PREFIX "/images/status/choose-level-subset.png", USE_ALPHA); + texture_load(&bkg_title,datadir + "/images/title/title.png", IGNORE_ALPHA); + texture_load(&anim1,datadir + "/images/title/title-anim2.png", IGNORE_ALPHA); + texture_load(&anim2,datadir + "/images/title/title-anim1.png", IGNORE_ALPHA); + texture_load(&img_choose_subset,datadir + "/images/status/choose-level-subset.png", USE_ALPHA); /* --- Main title loop: --- */ @@ -292,7 +292,7 @@ void display_credits() string_list_type names; char filename[1024]; string_list_init(&names); - sprintf(filename,"%s/CREDITS",DATA_PREFIX); + sprintf(filename,"%s/CREDITS", datadir.c_str()); if((fi = fopen(filename,"r")) != NULL) { while(fgets(temp, sizeof(temp), fi) != NULL) diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 9cd8ba569..f71f28e52 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -20,6 +20,7 @@ #include #include #include +#include "globals.h" #include "texture.h" #include "screen.h" #include "lispreader.h" @@ -32,7 +33,7 @@ TileManager* TileManager::instance_ = 0; TileManager::TileManager() { - lisp_object_t* root_obj = lisp_read_from_file(DATA_PREFIX "images/worldmap/antarctica.scm"); + lisp_object_t* root_obj = lisp_read_from_file(datadir + "images/worldmap/antarctica.scm"); assert(root_obj); @@ -66,7 +67,7 @@ TileManager::TileManager() reader.read_string("image", &filename); texture_load(&tile->sprite, - const_cast((std::string(DATA_PREFIX "/images/worldmap/") + filename).c_str()), + datadir + "/images/worldmap/" + filename, USE_ALPHA); if (id >= int(tiles.size())) @@ -98,7 +99,7 @@ TileManager::get(int i) Tux::Tux(WorldMap* worldmap_) : worldmap(worldmap_) { - texture_load(&sprite, DATA_PREFIX "/images/worldmap/tux.png", USE_ALPHA); + texture_load(&sprite, datadir + "/images/worldmap/tux.png", USE_ALPHA); offset = 0; moving = false; tile_pos.x = 0; @@ -196,7 +197,7 @@ WorldMap::WorldMap() width = 20; height = 15; - texture_load(&level_sprite, DATA_PREFIX "/images/worldmap/levelmarker.png", USE_ALPHA); + texture_load(&level_sprite, datadir + "/images/worldmap/levelmarker.png", USE_ALPHA); input_direction = NONE; enter_level = false; @@ -216,7 +217,7 @@ WorldMap::~WorldMap() void WorldMap::load_map() { - lisp_object_t* root_obj = lisp_read_from_file(DATA_PREFIX "levels/default/worldmap.scm"); + lisp_object_t* root_obj = lisp_read_from_file(datadir + "levels/default/worldmap.scm"); assert(root_obj); if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0) @@ -385,7 +386,7 @@ WorldMap::update() { std::cout << "Enter the current level: " << i->name << std::endl;; halt_music(); - gameloop(const_cast((DATA_PREFIX "levels/default/" + i->name).c_str()), + gameloop(const_cast((datadir + "levels/default/" + i->name).c_str()), 1, ST_GL_LOAD_LEVEL_FILE); play_music(song, 1); break; @@ -433,7 +434,7 @@ WorldMap::display() { quit = false; - song = load_song(const_cast((DATA_PREFIX "/music/" + music).c_str())); + song = load_song(datadir + "/music/" + music); play_music(song, 1); while(!quit) { -- 2.11.0