From 945d6ee4488595f3a8f7180b66b53ff684ab94e4 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Sun, 11 Apr 2004 00:05:35 +0000 Subject: [PATCH] - moved stuff from gamesession to world SVN-Revision: 462 --- src/Makefile.am | 3 +- src/collision.cpp | 38 ++++ src/collision.h | 11 ++ src/gameloop.cpp | 521 +++++------------------------------------------------- src/gameloop.h | 10 +- src/level.cpp | 40 +---- src/level.h | 18 +- src/world.cpp | 134 ++++++++++++-- src/world.h | 23 ++- 9 files changed, 251 insertions(+), 547 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 19e60fe17..bbf335a55 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,7 @@ bin_PROGRAMS = supertux -supertux_SOURCES = badguy.cpp badguy.h bitmask.cpp bitmask.h button.cpp button.h collision.cpp collision.h configfile.cpp configfile.h defines.h gameloop.cpp gameloop.h globals.cpp globals.h high_scores.cpp high_scores.h intro.cpp intro.h level.cpp level.h leveleditor.cpp leveleditor.h lispreader.cpp lispreader.h menu.cpp menu.h particlesystem.cpp particlesystem.h physic.cpp physic.h player.cpp player.h scene.cpp scene.h screen.cpp screen.h setup.cpp setup.h sound.cpp sound.h special.cpp special.h supertux.cpp supertux.h text.cpp text.h texture.cpp texture.h timer.cpp timer.h title.cpp title.h type.cpp type.h world.cpp world.h worldmap.cpp worldmap.h tile.h tile.cpp mousecursor.cpp mousecursor.h +supertux_SOURCES = \ +badguy.cpp badguy.h bitmask.cpp bitmask.h button.cpp button.h collision.cpp collision.h configfile.cpp configfile.h defines.h gameloop.cpp gameloop.h globals.cpp globals.h high_scores.cpp high_scores.h intro.cpp intro.h level.cpp level.h leveleditor.cpp leveleditor.h lispreader.cpp lispreader.h menu.cpp menu.h particlesystem.cpp particlesystem.h physic.cpp physic.h player.cpp player.h scene.cpp scene.h screen.cpp screen.h setup.cpp setup.h sound.cpp sound.h special.cpp special.h supertux.cpp supertux.h text.cpp text.h texture.cpp texture.h timer.cpp timer.h title.cpp title.h type.cpp type.h world.cpp world.h worldmap.cpp worldmap.h tile.h tile.cpp mousecursor.cpp mousecursor.h resources.h resources.cpp # EOF # noinst_HEADERS = diff --git a/src/collision.cpp b/src/collision.cpp index 8335367dd..17164c548 100644 --- a/src/collision.cpp +++ b/src/collision.cpp @@ -14,6 +14,7 @@ #include "collision.h" #include "bitmask.h" #include "scene.h" +#include "tile.h" bool rectcollision(base_type* one, base_type* two) { @@ -279,7 +280,44 @@ void collision_handler() upgrade_collision(&world.upgrades[i], &tux, CO_PLAYER); } } +} + + +Tile* gettile(float x, float y) +{ + return TileManager::instance()->get(GameSession::current()->get_level()->gettileid(x, y)); +} +bool issolid(float x, float y) +{ + Tile* tile = gettile(x,y); + return tile && tile->solid; } +bool isbrick(float x, float y) +{ + Tile* tile = gettile(x,y); + return tile && tile->brick; +} + +bool isice(float x, float y) +{ + Tile* tile = gettile(x,y); + return tile && tile->ice; +} + +bool isfullbox(float x, float y) +{ + Tile* tile = gettile(x,y); + return tile && tile->fullbox; +} + +bool isdistro(float x, float y) +{ + Tile* tile = gettile(x,y); + return tile && tile->distro; +} + +/* EOF */ + diff --git a/src/collision.h b/src/collision.h index 3f048d342..fa165d713 100644 --- a/src/collision.h +++ b/src/collision.h @@ -14,6 +14,8 @@ #include "type.h" +class Tile; + /* Collision objects */ enum { @@ -37,5 +39,14 @@ bool collision_object_map(base_type* object); And calls the collision_handlers, which the collision_objects provide for this case (or not). */ void collision_handler(); +/** Return a pointer to the tile at the given x/y coordinates */ +Tile* gettile(float x, float y); + +// Some little helper function to check for tile properties +bool issolid(float x, float y); +bool isbrick(float x, float y); +bool isice(float x, float y); +bool isfullbox(float x, float y); + #endif /*SUPERTUX_COLLISION_H*/ diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 27235f3ce..e0cbe6a92 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -48,9 +48,6 @@ int game_started = false; /* Local variables: */ - -static texture_type img_waves[3], img_water, img_pole, img_poletop, img_flag[2]; -static texture_type img_cloud[2][4]; static SDL_Event event; static SDLKey key; static char level_subset[100]; @@ -81,16 +78,20 @@ GameSession::GameSession(const std::string& filename) { current_ = this; + world = &::world; + timer_init(&fps_timer, true); timer_init(&frame_timer, true); - current_level.load(filename); + world->load(filename); } GameSession::GameSession(const std::string& subset, int levelnb, int mode) { current_ = this; + world = &::world; + timer_init(&fps_timer, true); timer_init(&frame_timer, true); @@ -100,27 +101,27 @@ GameSession::GameSession(const std::string& subset, int levelnb, int mode) level = levelnb; /* Init the game: */ - world.arrays_free(); + world->arrays_free(); set_defaults(); strcpy(level_subset, subset.c_str()); if (st_gl_mode == ST_GL_LOAD_LEVEL_FILE) { - if (current_level.load(level_subset)) + if (world->load(level_subset)) exit(1); } else { - if(current_level.load(level_subset, level) != 0) + if(world->load(level_subset, level) != 0) exit(1); } - current_level.load_gfx(); + world->get_level()->load_gfx(); loadshared(); - activate_bad_guys(¤t_level); - activate_particle_systems(); - current_level.load_song(); + activate_bad_guys(world->get_level()); + world->activate_particle_systems(); + world->get_level()->load_song(); tux.init(); @@ -147,7 +148,7 @@ GameSession::levelintro(void) sprintf(str, "LEVEL %d", level); text_drawf(&blue_text, str, 0, 200, A_HMIDDLE, A_TOP, 1); - sprintf(str, "%s", current_level.name.c_str()); + sprintf(str, "%s", world->get_level()->name.c_str()); text_drawf(&gold_text, str, 0, 224, A_HMIDDLE, A_TOP, 1); sprintf(str, "TUX x %d", tux.lives); @@ -163,7 +164,7 @@ GameSession::levelintro(void) void GameSession::start_timers() { - timer_start(&time_left,current_level.time_left*1000); + timer_start(&time_left, world->get_level()->time_left*1000); st_pause_ticks_init(); update_time = st_get_ticks(); } @@ -179,30 +180,14 @@ void activate_bad_guys(Level* plevel) } void -GameSession::activate_particle_systems() -{ - if(current_level.particle_system == "clouds") - { - world.particle_systems.push_back(new CloudParticleSystem); - } - else if(current_level.particle_system == "snow") - { - world.particle_systems.push_back(new SnowParticleSystem); - } - else if(current_level.particle_system != "") - { - st_abort("unknown particle system specified in level", ""); - } -} - -void GameSession::process_events() { while (SDL_PollEvent(&event)) { - /* Check for menu-events, if the menu is shown */ - if(show_menu) - menu_event(event); + /* Check for menu-events, if the menu is shown */ + if(show_menu) + menu_event(event); + switch(event.type) { case SDL_QUIT: /* Quit event - quit: */ @@ -369,10 +354,8 @@ GameSession::action() if (tux.is_dead() || next_level) { /* Tux either died, or reached the end of a level! */ - halt_music(); - - + if (next_level) { /* End of a level! */ @@ -385,10 +368,10 @@ GameSession::action() else { level_free_gfx(); - current_level.cleanup(); + world->get_level()->cleanup(); level_free_song(); unloadshared(); - world.arrays_free(); + world->arrays_free(); return(0); } tux.level_begin(); @@ -410,10 +393,10 @@ GameSession::action() save_hs(score); } level_free_gfx(); - current_level.cleanup(); + world->get_level()->cleanup(); level_free_song(); unloadshared(); - world.arrays_free(); + world->arrays_free(); return(0); } /* if (lives < 0) */ } @@ -422,26 +405,26 @@ GameSession::action() tux.level_begin(); set_defaults(); - current_level.cleanup(); + world->get_level()->cleanup(); if (st_gl_mode == ST_GL_LOAD_LEVEL_FILE) { - if(current_level.load(level_subset) != 0) + if(world->get_level()->load(level_subset) != 0) return 0; } else { - if(current_level.load(level_subset,level) != 0) + if(world->get_level()->load(level_subset,level) != 0) return 0; } - world.arrays_free(); - activate_bad_guys(¤t_level); - activate_particle_systems(); + world->arrays_free(); + activate_bad_guys(world->get_level()); + world->activate_particle_systems(); level_free_gfx(); - current_level.load_gfx(); + world->get_level()->load_gfx(); level_free_song(); - current_level.load_song(); + world->get_level()->load_song(); if(st_gl_mode != ST_GL_TEST) levelintro(); start_timers(); @@ -451,11 +434,11 @@ GameSession::action() tux.action(); - world.action(); + world->action(); /* update particle systems */ std::vector::iterator p; - for(p = world.particle_systems.begin(); p != world.particle_systems.end(); ++p) + for(p = world->particle_systems.begin(); p != world->particle_systems.end(); ++p) { (*p)->simulate(frame_ratio); } @@ -469,73 +452,9 @@ GameSession::action() void GameSession::draw() { - int y,x; - /* Draw screen: */ - if(timer_check(&super_bkgd_timer)) - texture_draw(&img_super_bkgd, 0, 0); - else - { - /* Draw the real background */ - if(current_level.bkgd_image[0] != '\0') - { - int s = (int)scroll_x / 30; - texture_draw_part(&img_bkgd,s,0,0,0,img_bkgd.w - s, img_bkgd.h); - texture_draw_part(&img_bkgd,0,0,screen->w - s ,0,s,img_bkgd.h); - } - else - { - clearscreen(current_level.bkgd_red, current_level.bkgd_green, current_level.bkgd_blue); - } - } - - /* Draw particle systems (background) */ - std::vector::iterator p; - for(p = world.particle_systems.begin(); p != world.particle_systems.end(); ++p) - { - (*p)->draw(scroll_x, 0, 0); - } - - /* Draw background: */ - for (y = 0; y < 15; ++y) - { - for (x = 0; x < 21; ++x) - { - drawshape(32*x - fmodf(scroll_x, 32), y * 32, - current_level.bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]); - } - } - - /* Draw interactive tiles: */ - for (y = 0; y < 15; ++y) - { - for (x = 0; x < 21; ++x) - { - drawshape(32*x - fmodf(scroll_x, 32), y * 32, - current_level.ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]); - } - } - world.draw(); - - for (unsigned int i = 0; i < world.broken_bricks.size(); ++i) - broken_brick_draw(&world.broken_bricks[i]); - - /* Draw foreground: */ - for (y = 0; y < 15; ++y) - { - for (x = 0; x < 21; ++x) - { - drawshape(32*x - fmodf(scroll_x, 32), y * 32, - current_level.fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]); - } - } - - /* Draw particle systems (foreground) */ - for(p = world.particle_systems.begin(); p != world.particle_systems.end(); ++p) - { - (*p)->draw(scroll_x, 0, 1); - } + world->draw(); drawstatus(); @@ -743,370 +662,16 @@ GameSession::run() halt_music(); level_free_gfx(); - current_level.cleanup(); + world->get_level()->cleanup(); level_free_song(); unloadshared(); - world.arrays_free(); + world->arrays_free(); game_started = false; return(quit); } -/* Load graphics/sounds shared between all levels: */ -void loadshared() -{ - int i; - - /* Tuxes: */ - texture_load(&smalltux_stand_left, datadir + "/images/shared/smalltux-left-6.png", USE_ALPHA); - texture_load(&smalltux_stand_right, datadir + "/images/shared/smalltux-right-6.png", USE_ALPHA); - - texture_load(&smalltux_jump_left, datadir + "/images/shared/smalltux-jump-left.png", USE_ALPHA); - texture_load(&smalltux_jump_right, datadir + "/images/shared/smalltux-jump-right.png", USE_ALPHA); - - tux_right.resize(8); - texture_load(&tux_right[0], datadir + "/images/shared/smalltux-right-1.png", USE_ALPHA); - texture_load(&tux_right[1], datadir + "/images/shared/smalltux-right-2.png", USE_ALPHA); - texture_load(&tux_right[2], datadir + "/images/shared/smalltux-right-3.png", USE_ALPHA); - texture_load(&tux_right[3], datadir + "/images/shared/smalltux-right-4.png", USE_ALPHA); - texture_load(&tux_right[4], datadir + "/images/shared/smalltux-right-5.png", USE_ALPHA); - texture_load(&tux_right[5], datadir + "/images/shared/smalltux-right-6.png", USE_ALPHA); - texture_load(&tux_right[6], datadir + "/images/shared/smalltux-right-7.png", USE_ALPHA); - texture_load(&tux_right[7], datadir + "/images/shared/smalltux-right-8.png", USE_ALPHA); - - tux_left.resize(8); - texture_load(&tux_left[0], datadir + "/images/shared/smalltux-left-1.png", USE_ALPHA); - texture_load(&tux_left[1], datadir + "/images/shared/smalltux-left-2.png", USE_ALPHA); - texture_load(&tux_left[2], datadir + "/images/shared/smalltux-left-3.png", USE_ALPHA); - texture_load(&tux_left[3], datadir + "/images/shared/smalltux-left-4.png", USE_ALPHA); - texture_load(&tux_left[4], datadir + "/images/shared/smalltux-left-5.png", USE_ALPHA); - texture_load(&tux_left[5], datadir + "/images/shared/smalltux-left-6.png", USE_ALPHA); - texture_load(&tux_left[6], datadir + "/images/shared/smalltux-left-7.png", USE_ALPHA); - texture_load(&tux_left[7], datadir + "/images/shared/smalltux-left-8.png", USE_ALPHA); - - texture_load(&firetux_right[0], datadir + "/images/shared/firetux-right-0.png", USE_ALPHA); - texture_load(&firetux_right[1], datadir + "/images/shared/firetux-right-1.png", USE_ALPHA); - texture_load(&firetux_right[2], datadir + "/images/shared/firetux-right-2.png", USE_ALPHA); - - texture_load(&firetux_left[0], datadir + "/images/shared/firetux-left-0.png", USE_ALPHA); - texture_load(&firetux_left[1], datadir + "/images/shared/firetux-left-1.png", USE_ALPHA); - texture_load(&firetux_left[2], datadir + "/images/shared/firetux-left-2.png", USE_ALPHA); - - - texture_load(&cape_right[0], datadir + "/images/shared/cape-right-0.png", - USE_ALPHA); - - texture_load(&cape_right[1], datadir + "/images/shared/cape-right-1.png", - USE_ALPHA); - - texture_load(&cape_left[0], datadir + "/images/shared/cape-left-0.png", - USE_ALPHA); - - texture_load(&cape_left[1], datadir + "/images/shared/cape-left-1.png", - USE_ALPHA); - - texture_load(&bigtux_right[0], datadir + "/images/shared/bigtux-right-0.png", - USE_ALPHA); - - texture_load(&bigtux_right[1], datadir + "/images/shared/bigtux-right-1.png", - USE_ALPHA); - - texture_load(&bigtux_right[2], datadir + "/images/shared/bigtux-right-2.png", - USE_ALPHA); - - texture_load(&bigtux_right_jump, datadir + "/images/shared/bigtux-right-jump.png", USE_ALPHA); - - texture_load(&bigtux_left[0], datadir + "/images/shared/bigtux-left-0.png", - USE_ALPHA); - - texture_load(&bigtux_left[1], datadir + "/images/shared/bigtux-left-1.png", - USE_ALPHA); - - texture_load(&bigtux_left[2], datadir + "/images/shared/bigtux-left-2.png", - USE_ALPHA); - - texture_load(&bigtux_left_jump, datadir + "/images/shared/bigtux-left-jump.png", USE_ALPHA); - - texture_load(&bigcape_right[0], datadir + "/images/shared/bigcape-right-0.png", - USE_ALPHA); - - texture_load(&bigcape_right[1], datadir + "/images/shared/bigcape-right-1.png", - USE_ALPHA); - - texture_load(&bigcape_left[0], datadir + "/images/shared/bigcape-left-0.png", - USE_ALPHA); - - texture_load(&bigcape_left[1], datadir + "/images/shared/bigcape-left-1.png", - USE_ALPHA); - - texture_load(&bigfiretux_right[0], datadir + "/images/shared/bigfiretux-right-0.png", - USE_ALPHA); - - texture_load(&bigfiretux_right[1], datadir + "/images/shared/bigfiretux-right-1.png", - USE_ALPHA); - - texture_load(&bigfiretux_right[2], datadir + "/images/shared/bigfiretux-right-2.png", - USE_ALPHA); - - texture_load(&bigfiretux_right_jump, datadir + "/images/shared/bigfiretux-right-jump.png", USE_ALPHA); - - texture_load(&bigfiretux_left[0], datadir + "/images/shared/bigfiretux-left-0.png", - USE_ALPHA); - - texture_load(&bigfiretux_left[1], datadir + "/images/shared/bigfiretux-left-1.png", - USE_ALPHA); - - texture_load(&bigfiretux_left[2], datadir + "/images/shared/bigfiretux-left-2.png", - USE_ALPHA); - - texture_load(&bigfiretux_left_jump, datadir + "/images/shared/bigfiretux-left-jump.png", USE_ALPHA); - - texture_load(&bigcape_right[0], datadir + "/images/shared/bigcape-right-0.png", - USE_ALPHA); - - texture_load(&bigcape_right[1], datadir + "/images/shared/bigcape-right-1.png", - USE_ALPHA); - - texture_load(&bigcape_left[0], datadir + "/images/shared/bigcape-left-0.png", - USE_ALPHA); - - texture_load(&bigcape_left[1], datadir + "/images/shared/bigcape-left-1.png", - USE_ALPHA); - - - texture_load(&ducktux_right, datadir + - "/images/shared/ducktux-right.png", - USE_ALPHA); - - texture_load(&ducktux_left, datadir + - "/images/shared/ducktux-left.png", - USE_ALPHA); - - texture_load(&skidtux_right, datadir + - "/images/shared/skidtux-right.png", - USE_ALPHA); - - texture_load(&skidtux_left, datadir + - "/images/shared/skidtux-left.png", - USE_ALPHA); - - texture_load(&duckfiretux_right, datadir + - "/images/shared/duckfiretux-right.png", - USE_ALPHA); - - texture_load(&duckfiretux_left, datadir + - "/images/shared/duckfiretux-left.png", - USE_ALPHA); - - texture_load(&skidfiretux_right, datadir + - "/images/shared/skidfiretux-right.png", - USE_ALPHA); - - texture_load(&skidfiretux_left, datadir + - "/images/shared/skidfiretux-left.png", - USE_ALPHA); - - - /* Boxes: */ - - texture_load(&img_box_full, datadir + "/images/shared/box-full.png", - IGNORE_ALPHA); - texture_load(&img_box_empty, datadir + "/images/shared/box-empty.png", - IGNORE_ALPHA); - - - /* Water: */ - - - texture_load(&img_water, datadir + "/images/shared/water.png", IGNORE_ALPHA); - - texture_load(&img_waves[0], datadir + "/images/shared/waves-0.png", - USE_ALPHA); - - texture_load(&img_waves[1], datadir + "/images/shared/waves-1.png", - USE_ALPHA); - - texture_load(&img_waves[2], datadir + "/images/shared/waves-2.png", - USE_ALPHA); - - - /* Pole: */ - - texture_load(&img_pole, datadir + "/images/shared/pole.png", USE_ALPHA); - texture_load(&img_poletop, datadir + "/images/shared/poletop.png", - USE_ALPHA); - - - /* Flag: */ - - texture_load(&img_flag[0], datadir + "/images/shared/flag-0.png", - USE_ALPHA); - texture_load(&img_flag[1], datadir + "/images/shared/flag-1.png", - USE_ALPHA); - - - /* Cloud: */ - - texture_load(&img_cloud[0][0], datadir + "/images/shared/cloud-00.png", - USE_ALPHA); - - texture_load(&img_cloud[0][1], datadir + "/images/shared/cloud-01.png", - USE_ALPHA); - - texture_load(&img_cloud[0][2], datadir + "/images/shared/cloud-02.png", - USE_ALPHA); - - texture_load(&img_cloud[0][3], datadir + "/images/shared/cloud-03.png", - USE_ALPHA); - - - texture_load(&img_cloud[1][0], datadir + "/images/shared/cloud-10.png", - USE_ALPHA); - - texture_load(&img_cloud[1][1], datadir + "/images/shared/cloud-11.png", - USE_ALPHA); - - texture_load(&img_cloud[1][2], datadir + "/images/shared/cloud-12.png", - USE_ALPHA); - - texture_load(&img_cloud[1][3], datadir + "/images/shared/cloud-13.png", - USE_ALPHA); - - - /* Bad guys: */ - load_badguy_gfx(); - - /* Upgrades: */ - - texture_load(&img_mints, datadir + "/images/shared/mints.png", USE_ALPHA); - texture_load(&img_coffee, datadir + "/images/shared/coffee.png", USE_ALPHA); - - - /* Weapons: */ - - texture_load(&img_bullet, datadir + "/images/shared/bullet.png", USE_ALPHA); - - texture_load(&img_red_glow, datadir + "/images/shared/red-glow.png", - USE_ALPHA); - - - - /* Distros: */ - - texture_load(&img_distro[0], datadir + "/images/shared/distro-0.png", - USE_ALPHA); - - texture_load(&img_distro[1], datadir + "/images/shared/distro-1.png", - USE_ALPHA); - - texture_load(&img_distro[2], datadir + "/images/shared/distro-2.png", - USE_ALPHA); - - texture_load(&img_distro[3], datadir + "/images/shared/distro-3.png", - USE_ALPHA); - - - /* Tux life: */ - - texture_load(&tux_life, datadir + "/images/shared/tux-life.png", - USE_ALPHA); - - /* Herring: */ - - texture_load(&img_golden_herring, datadir + "/images/shared/golden-herring.png", - USE_ALPHA); - - - /* Super background: */ - - texture_load(&img_super_bkgd, datadir + "/images/shared/super-bkgd.png", - IGNORE_ALPHA); - - - /* Sound effects: */ - - /* if (use_sound) // this will introduce SERIOUS bugs here ! because "load_sound" - // initialize sounds[i] with the correct pointer's value: - // NULL or something else. And it will be dangerous to - // play with not-initialized pointers. - // This is also true with if (use_music) - 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(datadir + soundfilenames[i]); - - /* Herring song */ - herring_song = load_song(datadir + "/music/SALCON.MOD"); -} - - -/* Free shared data: */ - -void unloadshared(void) -{ - int i; - - for (i = 0; i < 3; i++) - { - texture_free(&tux_right[i]); - texture_free(&tux_left[i]); - texture_free(&bigtux_right[i]); - texture_free(&bigtux_left[i]); - } - - texture_free(&bigtux_right_jump); - texture_free(&bigtux_left_jump); - - for (i = 0; i < 2; i++) - { - texture_free(&cape_right[i]); - texture_free(&cape_left[i]); - texture_free(&bigcape_right[i]); - texture_free(&bigcape_left[i]); - } - - texture_free(&ducktux_left); - texture_free(&ducktux_right); - - texture_free(&skidtux_left); - texture_free(&skidtux_right); - - free_badguy_gfx(); - - texture_free(&img_box_full); - texture_free(&img_box_empty); - - texture_free(&img_water); - for (i = 0; i < 3; i++) - texture_free(&img_waves[i]); - - texture_free(&img_pole); - texture_free(&img_poletop); - - for (i = 0; i < 2; i++) - texture_free(&img_flag[i]); - - texture_free(&img_mints); - texture_free(&img_coffee); - - for (i = 0; i < 4; i++) - { - texture_free(&img_distro[i]); - texture_free(&img_cloud[0][i]); - texture_free(&img_cloud[1][i]); - } - - texture_free(&img_golden_herring); - - for (i = 0; i < NUM_SOUNDS; i++) - free_chunk(sounds[i]); - - /* free the herring song */ - free_music( herring_song ); -} - - /* Draw a tile on the screen: */ void drawshape(float x, float y, unsigned int c, Uint8 alpha) @@ -1289,16 +854,16 @@ GameSession::loadgame(int slot) fread(&level,sizeof(int),1,fi); set_defaults(); - current_level.cleanup(); - if(current_level.load(level_subset,level) != 0) + world->get_level()->cleanup(); + if(world->get_level()->load(level_subset,level) != 0) exit(1); - world.arrays_free(); - activate_bad_guys(¤t_level); - activate_particle_systems(); + world->arrays_free(); + activate_bad_guys(world->get_level()); + world->activate_particle_systems(); level_free_gfx(); - current_level.load_gfx(); + world->get_level()->load_gfx(); level_free_song(); - current_level.load_song(); + world->get_level()->load_song(); levelintro(); update_time = st_get_ticks(); diff --git a/src/gameloop.h b/src/gameloop.h index d7edeae2f..4806aeba9 100644 --- a/src/gameloop.h +++ b/src/gameloop.h @@ -16,6 +16,7 @@ #include "sound.h" #include "type.h" #include "level.h" +#include "world.h" /* GameLoop modes */ @@ -26,6 +27,8 @@ extern int game_started; +class World; + /** The GameSession class controlls the controll flow of a World, ie. present the menu on specifc keypresses, render and update it while keeping the speed and framerate sane, etc. */ @@ -33,18 +36,20 @@ class GameSession { private: timer_type fps_timer, frame_timer; - Level current_level; + World* world; public: GameSession(); GameSession(const std::string& filename); GameSession(const std::string& subset, int levelnb, int mode); + int run(); void draw(); int action(); void process_events(); - Level* get_level() { return ¤t_level; } + Level* get_level() { return world->get_level(); } + World* get_world() { return world; } void savegame(int slot); void loadgame(int slot); @@ -55,7 +60,6 @@ class GameSession void levelintro(); void start_timers(); - void activate_particle_systems(); }; void activate_bad_guys(Level* plevel); diff --git a/src/level.cpp b/src/level.cpp index a33080f34..6d51c7805 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -228,15 +228,12 @@ Level::init_defaults() } } -/* Load data for this level: */ -/* Returns -1, if the loading of the level failed. */ int Level::load(const char *subset, int level) { char filename[1024]; - /* Load data file: */ - + // Load data file: 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", datadir.c_str(), subset, level); @@ -708,39 +705,4 @@ Level::gettileid(float x, float y) return c; } -Tile* gettile(float x, float y) -{ - return TileManager::instance()->get(GameSession::current()->get_level()->gettileid(x, y)); -} - -bool issolid(float x, float y) -{ - Tile* tile = gettile(x,y); - return tile && tile->solid; -} - -bool isbrick(float x, float y) -{ - Tile* tile = gettile(x,y); - return tile && tile->brick; -} - -bool isice(float x, float y) -{ - Tile* tile = gettile(x,y); - return tile && tile->ice; -} - -bool isfullbox(float x, float y) -{ - Tile* tile = gettile(x,y); - return tile && tile->fullbox; -} - -bool isdistro(float x, float y) -{ - Tile* tile = gettile(x,y); - return tile && tile->distro; -} - /* EOF */ diff --git a/src/level.h b/src/level.h index ad15356f8..682c56925 100644 --- a/src/level.h +++ b/src/level.h @@ -51,10 +51,12 @@ enum TileMapType { }; extern texture_type img_bkgd; + +/* extern texture_type img_bkgd_tile[2][4]; extern texture_type img_solid[4]; extern texture_type img_brick[2]; - +*/ class Level { public: @@ -81,7 +83,12 @@ class Level /** Cleanup the level struct from allocated tile data and such */ void cleanup(); + /** Load data for this level: + Returns -1, if the loading of the level failed. */ int load(const char* subset, int level); + + /** Load data for this level: + Returns -1, if the loading of the level failed. */ int load(const std::string& filename); void load_gfx(); @@ -103,13 +110,4 @@ void level_load_image(texture_type* ptexture, std::string theme, const char * fi void level_free_song(); void level_free_gfx(); -/** Return a pointer to the tile at the given x/y coordinates */ -Tile* gettile(float x, float y); - -// Some little helper function to check for tile properties -bool issolid(float x, float y); -bool isbrick(float x, float y); -bool isice(float x, float y); -bool isfullbox(float x, float y); - #endif /*SUPERTUX_LEVEL_H*/ diff --git a/src/world.cpp b/src/world.cpp index 52337be44..95ff32c96 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -10,6 +10,7 @@ // // +#include #include #include #include "globals.h" @@ -25,7 +26,24 @@ World world; World::World() { - + level = new Level; +} + +World::~World() +{ + delete level; +} + +int +World::load(const char* subset, int level_nr) +{ + return level->load(subset, level_nr); +} + +int +World::load(const std::string& filename) +{ + return level->load(filename); } void @@ -45,9 +63,74 @@ World::arrays_free(void) particle_systems.clear(); } + +void +World::activate_particle_systems() +{ + if (level->particle_system == "clouds") + { + particle_systems.push_back(new CloudParticleSystem); + } + else if (level->particle_system == "snow") + { + particle_systems.push_back(new SnowParticleSystem); + } + else if (level->particle_system != "") + { + st_abort("unknown particle system specified in level", ""); + } +} + void World::draw() { + int y,x; + + /* Draw screen: */ + if(timer_check(&super_bkgd_timer)) + texture_draw(&img_super_bkgd, 0, 0); + else + { + /* Draw the real background */ + if(get_level()->bkgd_image[0] != '\0') + { + int s = (int)scroll_x / 30; + texture_draw_part(&img_bkgd,s,0,0,0,img_bkgd.w - s, img_bkgd.h); + texture_draw_part(&img_bkgd,0,0,screen->w - s ,0,s,img_bkgd.h); + } + else + { + clearscreen(level->bkgd_red, level->bkgd_green, level->bkgd_blue); + } + } + + /* Draw particle systems (background) */ + std::vector::iterator p; + for(p = particle_systems.begin(); p != particle_systems.end(); ++p) + { + (*p)->draw(scroll_x, 0, 0); + } + + /* Draw background: */ + for (y = 0; y < 15; ++y) + { + for (x = 0; x < 21; ++x) + { + drawshape(32*x - fmodf(scroll_x, 32), y * 32, + level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]); + } + } + + /* Draw interactive tiles: */ + for (y = 0; y < 15; ++y) + { + for (x = 0; x < 21; ++x) + { + drawshape(32*x - fmodf(scroll_x, 32), y * 32, + level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]); + } + } + /* (Bouncy bricks): */ for (unsigned int i = 0; i < bouncy_bricks.size(); ++i) bouncy_brick_draw(&bouncy_bricks[i]); @@ -68,6 +151,25 @@ World::draw() for (unsigned int i = 0; i < bouncy_distros.size(); ++i) bouncy_distro_draw(&bouncy_distros[i]); + + for (unsigned int i = 0; i < broken_bricks.size(); ++i) + broken_brick_draw(&broken_bricks[i]); + + /* Draw foreground: */ + for (y = 0; y < 15; ++y) + { + for (x = 0; x < 21; ++x) + { + drawshape(32*x - fmodf(scroll_x, 32), y * 32, + level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]); + } + } + + /* Draw particle systems (foreground) */ + for(p = particle_systems.begin(); p != particle_systems.end(); ++p) + { + (*p)->draw(scroll_x, 0, 1); + } } void @@ -126,20 +228,20 @@ World::add_bouncy_distro(float x, float y) } void -World::add_broken_brick(float x, float y) +World::add_broken_brick(Tile* tile, float x, float y) { - add_broken_brick_piece(x, y, -1, -4); - add_broken_brick_piece(x, y + 16, -1.5, -3); + add_broken_brick_piece(tile, x, y, -1, -4); + add_broken_brick_piece(tile, x, y + 16, -1.5, -3); - add_broken_brick_piece(x + 16, y, 1, -4); - add_broken_brick_piece(x + 16, y + 16, 1.5, -3); + add_broken_brick_piece(tile, x + 16, y, 1, -4); + add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3); } void -World::add_broken_brick_piece(float x, float y, float xm, float ym) +World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym) { broken_brick_type new_broken_brick; - broken_brick_init(&new_broken_brick,x,y,xm,ym); + broken_brick_init(&new_broken_brick, tile, x, y, xm, ym); broken_bricks.push_back(new_broken_brick); } @@ -204,12 +306,15 @@ void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro) pbouncy_distro->base.y); } -void broken_brick_init(broken_brick_type* pbroken_brick, float x, float y, float xm, float ym) +void broken_brick_init(broken_brick_type* pbroken_brick, Tile* tile, + float x, float y, float xm, float ym) { + pbroken_brick->tile = tile; pbroken_brick->base.x = x; pbroken_brick->base.y = y; pbroken_brick->base.xm = xm; pbroken_brick->base.ym = ym; + timer_init(&pbroken_brick->timer, true); timer_start(&pbroken_brick->timer,200); } @@ -235,8 +340,10 @@ void broken_brick_draw(broken_brick_type* pbroken_brick) dest.y = (int)pbroken_brick->base.y; dest.w = 16; dest.h = 16; - - texture_draw_part(&img_brick[0],src.x,src.y,dest.x,dest.y,dest.w,dest.h); + + if (pbroken_brick->tile->images.size() > 0) + texture_draw_part(&pbroken_brick->tile->images[0], + src.x,src.y,dest.x,dest.y,dest.w,dest.h); } void bouncy_brick_init(bouncy_brick_type* pbouncy_brick, float x, float y) @@ -358,8 +465,9 @@ void trybreakbrick(float x, float y, bool small) plevel->change(x, y, TM_IA, tile->next_tile); /* Replace it with broken bits: */ - world.add_broken_brick(((int)(x + 1) / 32) * 32, - (int)(y / 32) * 32); + world.add_broken_brick(tile, + ((int)(x + 1) / 32) * 32, + (int)(y / 32) * 32); /* Get some score: */ play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER); diff --git a/src/world.h b/src/world.h index 0beb81db7..3398b2db6 100644 --- a/src/world.h +++ b/src/world.h @@ -44,9 +44,11 @@ struct broken_brick_type { base_type base; timer_type timer; + Tile* tile; }; -void broken_brick_init(broken_brick_type* pbroken_brick, float x, float y, float xm, float ym); +void broken_brick_init(broken_brick_type* pbroken_brick, Tile* tile, + float x, float y, float xm, float ym); void broken_brick_action(broken_brick_type* pbroken_brick); void broken_brick_draw(broken_brick_type* pbroken_brick); @@ -93,6 +95,7 @@ class World { public: Level* level; + std::vector bouncy_distros; std::vector broken_bricks; std::vector bouncy_bricks; @@ -104,14 +107,28 @@ class World public: World(); + ~World(); + + Level* get_level() { return level; } + void draw(); void action(); void arrays_free(); + /** Load data for this level: + Returns -1, if the loading of the level failed. */ + int load(const char* subset, int level); + + /** Load data for this level: + Returns -1, if the loading of the level failed. */ + int load(const std::string& filename); + + void activate_particle_systems(); + void add_score(float x, float y, int s); void add_bouncy_distro(float x, float y); - void add_broken_brick(float x, float y); - void add_broken_brick_piece(float x, float y, float xm, float ym); + void add_broken_brick(Tile* tile, float x, float y); + void add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym); void add_bouncy_brick(float x, float y); void add_bad_guy(float x, float y, BadGuyKind kind); void add_upgrade(float x, float y, int dir, int kind); -- 2.11.0