From 3e43c88abc23c09ac467b3f47d708a6b08938c66 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Thu, 20 May 2004 14:55:41 +0000 Subject: [PATCH] fixed score display and converted tilemap to a gameobject SVN-Revision: 1278 --- src/Makefile.am | 4 +++- src/display_manager.h | 5 +++-- src/gameobjs.cpp | 4 ++-- src/tilemap.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/tilemap.h | 28 ++++++++++++++++++++++++++++ src/world.cpp | 49 ++++++++----------------------------------------- 6 files changed, 94 insertions(+), 46 deletions(-) create mode 100644 src/tilemap.cpp create mode 100644 src/tilemap.h diff --git a/src/Makefile.am b/src/Makefile.am index b4784a3a6..6583cce08 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -82,6 +82,8 @@ game_object.h \ display_manager.h \ display_manager.cpp \ background.h \ -background.cpp +background.cpp \ +tilemap.h \ +tilemap.cpp # EOF # diff --git a/src/display_manager.h b/src/display_manager.h index cbc47409b..fe4a2c81f 100644 --- a/src/display_manager.h +++ b/src/display_manager.h @@ -13,8 +13,9 @@ enum { LAYER_BACKGROUNDTILES = -100, LAYER_TILES = 0, LAYER_OBJECTS = 100, - LAYER_FOREGROUND0 = 200, - LAYER_FOREGROUND1 = 300 + LAYER_FOREGROUNDTILES = 200, + LAYER_FOREGROUND0 = 300, + LAYER_FOREGROUND1 = 400 }; /** This class holds a list of all things that should be drawn to screen diff --git a/src/gameobjs.cpp b/src/gameobjs.cpp index f85c89da9..f2d589b43 100644 --- a/src/gameobjs.cpp +++ b/src/gameobjs.cpp @@ -119,10 +119,10 @@ FloatingScore::FloatingScore(DisplayManager& displaymanager, const Vector& pos, int score) : position(pos) { - displaymanager.add_drawable(this, LAYER_OBJECTS+1); + displaymanager.add_drawable(this, LAYER_OBJECTS); timer.start(1000); snprintf(str, 10, "%d", score); - position.x += - strlen(str) * 8; + position.x -= strlen(str) * 8; } void diff --git a/src/tilemap.cpp b/src/tilemap.cpp new file mode 100644 index 000000000..ba560c6e2 --- /dev/null +++ b/src/tilemap.cpp @@ -0,0 +1,50 @@ +#include "tilemap.h" +#include "display_manager.h" +#include "level.h" +#include "tile.h" +#include "globals.h" + +TileMap::TileMap(DisplayManager& display_manager, Level* newlevel) + : level(newlevel) +{ + display_manager.add_drawable(this, LAYER_BACKGROUNDTILES); + display_manager.add_drawable(this, LAYER_TILES); + display_manager.add_drawable(this, LAYER_FOREGROUNDTILES); +} + +TileMap::~TileMap() +{ +} + +void +TileMap::action(float ) +{ +} + +void +TileMap::draw(ViewPort& viewport, int layer) +{ + std::vector >* tiles; + switch(layer) { + case LAYER_BACKGROUNDTILES: + tiles = &level->bg_tiles; break; + case LAYER_TILES: + tiles = &level->ia_tiles; break; + case LAYER_FOREGROUNDTILES: + tiles = &level->fg_tiles; break; + default: + assert(!"Wrong layer when drawing tilemap."); + } + + int tsx = int(viewport.get_translation().x / 32); // tilestartindex x + int tsy = int(viewport.get_translation().y / 32); // tilestartindex y + int sx = - (int(viewport.get_translation().x) % 32); + int sy = - (int(viewport.get_translation().y) % 32); + for(int x = sx, tx = tsx; x < screen->w && tx < int((*tiles)[0].size()); + x += 32, ++tx) { + for(int y = sy, ty = tsy; y < screen->h && ty < int(tiles->size()); + y += 32, ++ty) { + Tile::draw(x, y, (*tiles) [ty][tx]); + } + } +} diff --git a/src/tilemap.h b/src/tilemap.h new file mode 100644 index 000000000..682e76d83 --- /dev/null +++ b/src/tilemap.h @@ -0,0 +1,28 @@ +#ifndef __TILEMAP_H__ +#define __TILEMAP_H__ + +#include "game_object.h" +#include "drawable.h" + +class Level; + +/** + * This class is reponsible for drawing the level tiles + */ +class TileMap : public _GameObject, public Drawable +{ +public: + TileMap(DisplayManager& manager, Level* level); + virtual ~TileMap(); + + virtual void action(float elapsed_time); + virtual void draw(ViewPort& viewport, int layer); + virtual std::string type() const + { return "TileMap"; } + +private: + Level* level; +}; + +#endif + diff --git a/src/world.cpp b/src/world.cpp index ed049b03d..16f70af6f 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -36,6 +36,7 @@ #include "viewport.h" #include "display_manager.h" #include "background.h" +#include "tilemap.h" Surface* img_distro[4]; @@ -63,6 +64,9 @@ World::World(const std::string& filename) bg->set_gradient(level->bkgd_top, level->bkgd_bottom); } gameobjects.push_back(bg); + + // add tilemap + gameobjects.push_back(new TileMap(displaymanager, get_level())); activate_objects(); get_level()->load_song(); @@ -93,6 +97,8 @@ World::World(const std::string& subset, int level_nr) bg->set_gradient(level->bkgd_top, level->bkgd_bottom); } gameobjects.push_back(bg); + // add tilemap + gameobjects.push_back(new TileMap(displaymanager, get_level())); get_level()->load_song(); apply_bonuses(); @@ -198,39 +204,10 @@ World::activate_particle_systems() void World::draw() { - int y,x; - - /* Draw the real background */ -#if 0 - drawgradient(level->bkgd_top, level->bkgd_bottom); - if(level->img_bkgd) - level->draw_bg(); -#endif - - /* Draw particle systems (background) */ + /* Draw objects */ displaymanager.get_viewport().set_translation(Vector(scroll_x, scroll_y)); displaymanager.draw(); - /* Draw background: */ - for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y) - { - for (x = 0; x < VISIBLE_TILES_X; ++x) - { - Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32), - level->bg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]); - } - } - - /* Draw interactive tiles: */ - for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y) - { - for (x = 0; x < VISIBLE_TILES_X; ++x) - { - Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32), - level->ia_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]); - } - } - for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i) (*i)->draw(); @@ -244,16 +221,6 @@ World::draw() for (unsigned int i = 0; i < upgrades.size(); ++i) upgrades[i].draw(); - - /* Draw foreground: */ - for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y) - { - for (x = 0; x < VISIBLE_TILES_X; ++x) - { - Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32), - level->fg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]); - } - } } void @@ -275,7 +242,7 @@ World::action(double frame_ratio) for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i) (*i)->action(frame_ratio); - /* update particle systems */ + /* update objects */ for(std::vector<_GameObject*>::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) (*i)->action(frame_ratio); -- 2.11.0