From 57fbded58117c85dcade4cfe3dbf4bf17af3e63c Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Fri, 21 May 2004 00:22:57 +0000 Subject: [PATCH] eliminated global scroll_x and scroll_y variables SVN-Revision: 1292 --- src/badguy.cpp | 10 ++++++++++ src/gameloop.cpp | 9 ++++++--- src/level.cpp | 19 ------------------- src/level.h | 3 --- src/leveleditor.cpp | 7 +++---- src/player.cpp | 20 +++++++++++++------- src/player.h | 2 +- src/scene.cpp | 3 --- src/scene.h | 1 - src/special.cpp | 16 +++++++++++++--- src/title.cpp | 3 --- src/world.cpp | 29 ++++++++++++++++------------- src/world.h | 6 ++++-- 13 files changed, 66 insertions(+), 62 deletions(-) diff --git a/src/badguy.cpp b/src/badguy.cpp index 1f626a658..2cb0bcba9 100644 --- a/src/badguy.cpp +++ b/src/badguy.cpp @@ -35,6 +35,7 @@ #include "gameloop.h" #include "display_manager.h" #include "lispwriter.h" +#include "viewport.h" Sprite* img_mriceblock_flat_left; Sprite* img_mriceblock_flat_right; @@ -315,6 +316,9 @@ BadGuy::action_mriceblock(double elapsed_time) check_horizontal_bump(); if(mode == KICK && changed != dir) { + float scroll_x = World::current()->displaymanager + .get_viewport().get_translation().x; + /* handle stereo sound (number 10 should be tweaked...)*/ if (base.x < scroll_x + screen->w/2 - 10) play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER); @@ -514,6 +518,9 @@ BadGuy::action_bomb(double elapsed_time) dying = DYING_NOT; // now the bomb hurts timer.start(EXPLODETIME); + float scroll_x = World::current()->displaymanager + .get_viewport().get_translation().x; + /* play explosion sound */ // FIXME: is the stereo all right? maybe we should use player cordinates... if (base.x < scroll_x + screen->w/2 - 10) play_sound(sounds[SND_EXPLODE], SOUND_LEFT_SPEAKER); @@ -746,6 +753,9 @@ BadGuy::action_snowball(double elapsed_time) void BadGuy::action(float elapsed_time) { + float scroll_x = World::current()->displaymanager + .get_viewport().get_translation().x; + // Remove if it's far off the screen: if (base.x < scroll_x - OFFSCREEN_DISTANCE) { diff --git a/src/gameloop.cpp b/src/gameloop.cpp index b5f331519..766223d7f 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -53,6 +53,7 @@ #include "tile.h" #include "particlesystem.h" #include "resources.h" +#include "background.h" #include "music_manager.h" GameSession* GameSession::current_ = 0; @@ -145,8 +146,9 @@ GameSession::levelintro(void) music_manager->halt_music(); char str[60]; - - get_level()->draw_bg(); + + ViewPort dummy; + world->background->draw(dummy, LAYER_BACKGROUND0); sprintf(str, "%s", world->get_level()->name.c_str()); gold_text->drawf(str, 0, 220, A_HMIDDLE, A_TOP, 1); @@ -717,7 +719,8 @@ GameSession::drawresultscreen(void) { char str[80]; - get_level()->draw_bg(); + ViewPort dummy; + world->background->draw(dummy, LAYER_BACKGROUND0); blue_text->drawf("Result:", 0, 200, A_HMIDDLE, A_TOP, 1); diff --git a/src/level.cpp b/src/level.cpp index 4bdf8854f..66da47787 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -613,25 +613,6 @@ Level::change(float x, float y, int tm, unsigned int c) } } -void Level::draw_bg() -{ - if(img_bkgd) - { - // Tile background horizontally - int sx = (int)((float)scroll_x * ((float)bkgd_speed/100.0f)) % img_bkgd->w; - int sy = (int)((float)scroll_y * ((float)bkgd_speed/100.0f)) % img_bkgd->h; - for (int x = 0; (x-1)*img_bkgd->w <= screen->w; x++) - for (int y = 0; (y-1)*img_bkgd->h <= screen->h; y++) - img_bkgd->draw_part(x == 0 ? sx : 0, y == 0 ? sy : 0, - x == 0 ? 0 : (img_bkgd->w * x) - sx, y == 0 ? 0 : (img_bkgd->h * y) - sy, - x == 0 ? img_bkgd->w - sx : img_bkgd->w, y == 0 ? img_bkgd->h - sy : img_bkgd->h); - } - else - { - drawgradient(bkgd_top, bkgd_bottom); - } -} - void Level::load_song() { diff --git a/src/level.h b/src/level.h index 28eb0b489..1d2b67829 100644 --- a/src/level.h +++ b/src/level.h @@ -140,9 +140,6 @@ class Level /** Resize the level to a new width/height */ void resize(int new_width, int new_height); - /* Draw background */ - void draw_bg(); - /** Return the id of the tile at position x/y */ unsigned int gettileid(float x, float y) const; /** returns the id of the tile at position x,y diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index f72328991..ed06a5dc8 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -42,6 +42,7 @@ #include "tile.h" #include "resources.h" #include "music_manager.h" +#include "background.h" #include "display_manager.h" /* definitions to aid development */ @@ -524,8 +525,6 @@ void le_init_menus() int le_init() { - - level_subsets = dsubdirs("/levels", "level1.stl"); le_level_subset = new LevelSubset; @@ -535,7 +534,6 @@ int le_init() active_tm = TM_IA; le_show_grid = true; show_selections = true; - scroll_x = 0; done = 0; le_frame = 0; /* support for frames in some tiles, like waves and bad guys */ @@ -944,7 +942,8 @@ void le_drawlevel() Uint8 a; /* Draw the real background */ - le_world->get_level()->draw_bg(); + le_world->background->draw(le_world->displaymanager.get_viewport(), + LAYER_BACKGROUND0); if(le_current.IsTile()) { diff --git a/src/player.cpp b/src/player.cpp index d90666084..2cfdaff17 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -669,7 +669,8 @@ Player::draw(ViewPort& viewport, int layer) } if (debug_mode) - fillrect(base.x - scroll_x, base.y - scroll_y, + fillrect(base.x - viewport.get_translation().x, + base.y - viewport.get_translation().y, base.width, base.height, 75,75,75, 150); } @@ -828,6 +829,10 @@ Player::is_dying() bool Player::is_dead() { + float scroll_x = + World::current()->displaymanager.get_viewport().get_translation().x; + float scroll_y = + World::current()->displaymanager.get_viewport().get_translation().y; if(base.y > screen->h + scroll_y || base.y > World::current()->get_level()->height*32 || base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // can happen in auto-scrolling return true; @@ -845,7 +850,8 @@ Player::remove_powerups() } void -Player::check_bounds(bool back_scrolling, bool hor_autoscroll) +Player::check_bounds(ViewPort& viewport, + bool back_scrolling, bool hor_autoscroll) { /* Keep tux in bounds: */ if (base.x < 0) @@ -860,17 +866,17 @@ Player::check_bounds(bool back_scrolling, bool hor_autoscroll) kill(KILL); } - if(base.x < scroll_x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled - base.x = scroll_x; + if(base.x < viewport.get_translation().x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled + base.x = viewport.get_translation().x; if(hor_autoscroll) { - if(base.x == scroll_x) + if(base.x == viewport.get_translation().x) if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32))) kill(KILL); - if(base.x + base.width > scroll_x + screen->w) - base.x = scroll_x + screen->w - base.width; + if(base.x + base.width > viewport.get_translation().x + screen->w) + base.x = viewport.get_translation().x + screen->w - base.width; } } diff --git a/src/player.h b/src/player.h index d1e5b4af7..362fbc621 100644 --- a/src/player.h +++ b/src/player.h @@ -158,7 +158,7 @@ public: void is_dying(); bool is_dead(); void player_remove_powerups(); - void check_bounds(bool back_scrolling, bool hor_autoscroll); + void check_bounds(ViewPort& viewport, bool back_scrolling, bool hor_autoscroll); bool on_ground(); bool under_solid(); void grow(); diff --git a/src/scene.cpp b/src/scene.cpp index ccdce0bb4..0ee4d1c29 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -68,9 +68,6 @@ PlayerStatus::BonusType string_to_bonus(const std::string& str) return PlayerStatus::NO_BONUS; } -// FIXME: Move this into a view class -float scroll_x, scroll_y; - unsigned int global_frame_counter; // EOF // diff --git a/src/scene.h b/src/scene.h index d2fa82f85..a4f7215f2 100644 --- a/src/scene.h +++ b/src/scene.h @@ -46,7 +46,6 @@ PlayerStatus::BonusType string_to_bonus(const std::string& str); extern PlayerStatus player_status; -extern float scroll_x, scroll_y; extern unsigned int global_frame_counter; #endif /*SUPERTUX_SCENE_H*/ diff --git a/src/special.cpp b/src/special.cpp index 8ba35ccac..9c4be7b3d 100644 --- a/src/special.cpp +++ b/src/special.cpp @@ -95,9 +95,14 @@ Bullet::action(float elapsed_time) else if(physic.get_velocity_y() < -9) physic.set_velocity_y(-9); + float scroll_x = + World::current()->displaymanager.get_viewport().get_translation().x; + float scroll_y = + World::current()->displaymanager.get_viewport().get_translation().y; if (base.x < scroll_x || base.x > scroll_x + screen->w || - base.y > screen->h || + base.y < scroll_y || + base.y > scroll_y + screen->h || issolid(base.x + 4, base.y + 2) || issolid(base.x, base.y + 2) || life_count <= 0) @@ -181,11 +186,16 @@ Upgrade::action(float elapsed_time) } /* Away from the screen? Kill it! */ - if(base.x < scroll_x - OFFSCREEN_DISTANCE) { + float scroll_x = + World::current()->displaymanager.get_viewport().get_translation().x; + float scroll_y = + World::current()->displaymanager.get_viewport().get_translation().y; + if(base.x < scroll_x - OFFSCREEN_DISTANCE + || base.y < scroll_y - OFFSCREEN_DISTANCE) { remove_me(); return; } - if(base.y > screen->h) { + if(base.y > scroll_y + screen->h) { remove_me(); return; } diff --git a/src/title.cpp b/src/title.cpp index 9639caf2f..a2fa61923 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -192,7 +192,6 @@ void draw_demo(GameSession* session, double frame_ratio) if(plevel->width * 32 - 320 < tux->base.x) { tux->level_begin(); - scroll_x = 0; } tux->can_jump = true; @@ -339,8 +338,6 @@ void title(void) else if (process_load_game_menu()) { // FIXME: shouldn't be needed if GameSession doesn't relay on global variables - // reset tux - scroll_x = 0; //titletux.level_begin(); update_time = st_get_ticks(); } diff --git a/src/world.cpp b/src/world.cpp index 6bdc33a72..97edbeb31 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -59,13 +59,13 @@ World::World(const std::string& filename) get_level()->load_gfx(); // add background activate_particle_systems(); - Background* bg = new Background(displaymanager); + background = new Background(displaymanager); if(level->img_bkgd) { - bg->set_image(level->img_bkgd, level->bkgd_speed); + background->set_image(level->img_bkgd, level->bkgd_speed); } else { - bg->set_gradient(level->bkgd_top, level->bkgd_bottom); + background->set_gradient(level->bkgd_top, level->bkgd_bottom); } - gameobjects.push_back(bg); + gameobjects.push_back(background); // add tilemap gameobjects.push_back(new TileMap(displaymanager, get_level())); @@ -92,13 +92,13 @@ World::World(const std::string& subset, int level_nr) get_level()->load_gfx(); activate_particle_systems(); - Background* bg = new Background(displaymanager); + background = new Background(displaymanager); if(level->img_bkgd) { - bg->set_image(level->img_bkgd, level->bkgd_speed); + background->set_image(level->img_bkgd, level->bkgd_speed); } else { - bg->set_gradient(level->bkgd_top, level->bkgd_bottom); + background->set_gradient(level->bkgd_top, level->bkgd_bottom); } - gameobjects.push_back(bg); + gameobjects.push_back(background); // add tilemap gameobjects.push_back(new TileMap(displaymanager, get_level())); get_level()->load_song(); @@ -140,9 +140,6 @@ World::~World() void World::set_defaults() { - // Set defaults: - scroll_x = 0; - player_status.score_multiplier = 1; counting_distros = false; @@ -213,7 +210,6 @@ void World::draw() { /* Draw objects */ - displaymanager.get_viewport().set_translation(Vector(scroll_x, scroll_y)); displaymanager.draw(); } @@ -226,7 +222,8 @@ World::action(float elapsed_time) for(size_t i = 0; i < gameobjects.size(); ++i) gameobjects[i]->action(elapsed_time); - tux->check_bounds(level->back_scrolling, (bool)level->hor_autoscroll_speed); + tux->check_bounds(displaymanager.get_viewport(), + level->back_scrolling, (bool)level->hor_autoscroll_speed); scrolling(elapsed_time); /* Handle all possible collisions. */ @@ -284,6 +281,9 @@ World::action(float elapsed_time) /* This functions takes cares of the scrolling */ void World::scrolling(float elapsed_time) { + float scroll_x = displaymanager.get_viewport().get_translation().x; + float scroll_y = displaymanager.get_viewport().get_translation().y; + /* Y-axis scrolling */ float tux_pos_y = tux->base.y + (tux->base.height/2); @@ -308,6 +308,7 @@ void World::scrolling(float elapsed_time) if(level->hor_autoscroll_speed) { scroll_x += level->hor_autoscroll_speed * elapsed_time; + displaymanager.get_viewport().set_translation(Vector(scroll_x, scroll_y)); return; } @@ -379,6 +380,8 @@ void World::scrolling(float elapsed_time) scroll_x = level->width * 32 - screen->w; if(scroll_x < 0) scroll_x = 0; + + displaymanager.get_viewport().set_translation(Vector(scroll_x, scroll_y)); } void diff --git a/src/world.h b/src/world.h index 04e00c614..2ae7e0ba6 100644 --- a/src/world.h +++ b/src/world.h @@ -33,6 +33,7 @@ #include "display_manager.h" class Level; +class Background; /** The World class holds a level and all the game objects (badguys, bouncy distros, etc) that are needed to run a game. */ @@ -54,6 +55,7 @@ private: static World* current_; public: + Background* background; BadGuys bad_guys; std::vector upgrades; @@ -79,8 +81,8 @@ public: void set_defaults(); void draw(); - void action(double frame_ratio); - void scrolling(double frame_ratio); // camera scrolling + void action(float elapsed_time); + void scrolling(float elapsed_time); // camera scrolling void play_music(int musictype); int get_music_type(); -- 2.11.0