From 5d1290054358cb4560f54bcd6840121a69360bf9 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Thu, 13 Apr 2006 14:06:22 +0000 Subject: [PATCH] fix menu width, didn't committ the tileset fix, miscelaneous small fixes SVN-Revision: 3325 --- data/images/tiles.strf | 3 +- data/script/default.nut | 2 +- src/console.cpp | 12 +++--- src/gui/menu.cpp | 52 +++++++++++++------------ src/gui/menu.hpp | 4 +- src/mainloop.cpp | 3 +- src/object/block.cpp | 4 +- src/object/coin.cpp | 2 +- src/object/oneup.cpp | 2 +- src/object/player.cpp | 24 ++++++++++++ src/object/player.hpp | 3 ++ src/object/powerup.cpp | 2 +- src/player_status.cpp | 16 +++----- src/player_status.hpp | 3 +- src/scripting/functions.cpp | 5 +++ src/scripting/functions.hpp | 6 +++ src/scripting/player.hpp | 6 --- src/scripting/wrapper.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++ 18 files changed, 183 insertions(+), 59 deletions(-) diff --git a/data/images/tiles.strf b/data/images/tiles.strf index b31abba73..843ee1115 100644 --- a/data/images/tiles.strf +++ b/data/images/tiles.strf @@ -93,9 +93,8 @@ (name "Icebridge") (tiles 0 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551) ) -(tile + (tile (id 0) - (editor-images "objects/bonus_block/bonus-fire_flower.png") ) (tile (id 1) diff --git a/data/script/default.nut b/data/script/default.nut index 6629aaef3..f3543af00 100644 --- a/data/script/default.nut +++ b/data/script/default.nut @@ -6,7 +6,7 @@ function end_level() wait(6); Effect.fade_out(2); wait(2); - Level.finish(); + Level.finish(true); } function levelflip() diff --git a/src/console.cpp b/src/console.cpp index edef8b5a5..2867c9ed4 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -295,11 +295,13 @@ Console::draw(DrawingContext& context) if (height == 0) return; + int layer = LAYER_GUI + 1; + context.push_transform(); context.set_alpha(alpha); - context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 - background->get_width() + backgroundOffset, height - background->get_height()), LAYER_FOREGROUND1+1); - context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 + backgroundOffset, height - background->get_height()), LAYER_FOREGROUND1+1); - context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), LAYER_FOREGROUND1+1); + context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 - background->get_width() + backgroundOffset, height - background->get_height()), layer); + context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 + backgroundOffset, height - background->get_height()), layer); + context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), layer); backgroundOffset+=10; if (backgroundOffset > (int)background->get_width()) backgroundOffset -= (int)background->get_width(); @@ -308,7 +310,7 @@ Console::draw(DrawingContext& context) if (focused) { lineNo++; float py = height-4-1*9; - context.draw_text(font.get(), "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1); + context.draw_text(font.get(), "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, layer); } int skipLines = -offset; @@ -317,7 +319,7 @@ Console::draw(DrawingContext& context) lineNo++; float py = height-4-lineNo*9; if (py < -9) break; - context.draw_text(font.get(), *i, Vector(4, py), LEFT_ALLIGN, LAYER_FOREGROUND1+1); + context.draw_text(font.get(), *i, Vector(4, py), LEFT_ALLIGN, layer); } context.pop_transform(); diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index 4e28ff27e..e524894c8 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -488,8 +488,8 @@ Menu::menu_action(MenuItem* ) void Menu::draw_item(DrawingContext& context, int index) { - int menu_height = get_height(); - int menu_width = get_width(); + float menu_height = get_height(); + float menu_width = get_width(); MenuItem& pitem = *(items[index]); @@ -667,29 +667,33 @@ Menu::draw_item(DrawingContext& context, int index) } } -int Menu::get_width() const +float Menu::get_width() const +{ + /* The width of the menu has to be more than the width of the text + with the most characters */ + float menu_width = 0; + for(unsigned int i = 0; i < items.size(); ++i) { - /* The width of the menu has to be more than the width of the text - with the most characters */ - int menu_width = 0; - for(unsigned int i = 0; i < items.size(); ++i) - { - int w = items[i]->text.size() + items[i]->input.size() + 1; - if(w > menu_width) - { - menu_width = w; - if( items[i]->kind == MN_TOGGLE) - menu_width += 2; - } - } - - return (menu_width * 16 + 24); + Font* font = default_font; + if(items[i]->kind == MN_LABEL) + font = label_font; + + float w = font->get_text_width(items[i]->text) + + label_font->get_text_width(items[i]->input) + 16; + if(items[i]->kind == MN_TOGGLE) + w += 32; + + if(w > menu_width) + menu_width = w; } + + return menu_width + 24; +} -int Menu::get_height() const - { - return items.size() * 24; - } +float Menu::get_height() const +{ + return items.size() * 24; +} /* Draw the current menu. */ void @@ -699,8 +703,8 @@ Menu::draw(DrawingContext& context) MouseCursor::current()->draw(context); } - int menu_height = get_height(); - int menu_width = get_width(); + float menu_height = get_height(); + float menu_width = get_width(); /* Draw a transparent background */ context.draw_filled_rect( diff --git a/src/gui/menu.hpp b/src/gui/menu.hpp index 9b6308857..ae6a837c8 100644 --- a/src/gui/menu.hpp +++ b/src/gui/menu.hpp @@ -186,8 +186,8 @@ public: protected: void additem(MenuItem* pmenu_item); - int get_width() const; - int get_height() const; + float get_width() const; + float get_height() const; private: void check_controlfield_change_event(const SDL_Event& event); diff --git a/src/mainloop.cpp b/src/mainloop.cpp index e65f8c9f2..e3021cfbf 100644 --- a/src/mainloop.cpp +++ b/src/mainloop.cpp @@ -64,8 +64,7 @@ MainLoop::push_screen(Screen* screen) void MainLoop::exit_screen() { - if (screen_stack.size() < 1) - { + if (screen_stack.size() < 1) { quit(); return; } diff --git a/src/object/block.cpp b/src/object/block.cpp index 82f310488..6e988b106 100644 --- a/src/object/block.cpp +++ b/src/object/block.cpp @@ -214,7 +214,7 @@ BonusBlock::try_open() switch(contents) { case CONTENT_COIN: Sector::current()->add_object(new BouncyCoin(get_pos())); - player.get_status()->incCoins(); + player.get_status()->add_coins(1); break; case CONTENT_FIREGROW: @@ -300,7 +300,7 @@ Brick::try_break(bool playerhit) if(coin_counter > 0) { sector->add_object(new BouncyCoin(get_pos())); coin_counter--; - player.get_status()->incCoins(); + player.get_status()->add_coins(1); if(coin_counter == 0) sprite->set_action("empty"); start_bounce(); diff --git a/src/object/coin.cpp b/src/object/coin.cpp index 8d969e1a9..d3e5a5689 100644 --- a/src/object/coin.cpp +++ b/src/object/coin.cpp @@ -66,7 +66,7 @@ Coin::draw(DrawingContext& context) void Coin::collect() { - Sector::current()->player->get_status()->incCoins(); + Sector::current()->player->get_status()->add_coins(1); Sector::current()->add_object(new BouncyCoin(get_pos())); global_stats.add_points(COINS_COLLECTED_STAT, 1); remove_me(); diff --git a/src/object/oneup.cpp b/src/object/oneup.cpp index 25d6b5e25..ddbf70fe8 100644 --- a/src/object/oneup.cpp +++ b/src/object/oneup.cpp @@ -61,7 +61,7 @@ OneUp::collision(GameObject& other, const CollisionHit& ) { Player* player = dynamic_cast (&other); if(player) { - player->get_status()->incLives(); + player->get_status()->add_coins(100); remove_me(); return ABORT_MOVE; } diff --git a/src/object/player.cpp b/src/object/player.cpp index f453f50ae..d7e380f12 100644 --- a/src/object/player.cpp +++ b/src/object/player.cpp @@ -561,6 +561,30 @@ Player::handle_input() } void +Player::add_coins(int count) +{ + player_status->add_coins(count); +} + +void +Player::set_bonus(const std::string& bonustype) +{ + if(bonustype == "grow") + set_bonus(GROWUP_BONUS); + else if(bonustype == "fireflower") + set_bonus(FIRE_BONUS); + else if(bonustype == "iceflower") + set_bonus(ICE_BONUS); + else if(bonustype == "none") + set_bonus(NO_BONUS); + + + std::ostringstream msg; + msg << "Unknown bonus type " << bonustype; + throw std::runtime_error(msg.str()); +} + +void Player::set_bonus(BonusType type, bool animate) { if(player_status->bonus >= type) diff --git a/src/object/player.hpp b/src/object/player.hpp index d7326cd0d..d16f536e0 100644 --- a/src/object/player.hpp +++ b/src/object/player.hpp @@ -160,6 +160,9 @@ public: void kill(HurtMode mode); void check_bounds(Camera* camera); void move(const Vector& vector); + + virtual void set_bonus(const std::string& bonus); + virtual void add_coins(int count); void set_bonus(BonusType type, bool animate = false); PlayerStatus* get_status() { diff --git a/src/object/powerup.cpp b/src/object/powerup.cpp index 70306034a..ad0104633 100644 --- a/src/object/powerup.cpp +++ b/src/object/powerup.cpp @@ -84,7 +84,7 @@ PowerUp::collision(GameObject& other, const CollisionHit& hit) } else if (sprite_name == "images/powerups/star/star.sprite") { player->make_invincible(); } else if (sprite_name == "images/powerups/1up/1up.sprite") { - player->get_status()->incLives(); + player->get_status()->add_coins(100); } return ABORT_MOVE; } diff --git a/src/player_status.cpp b/src/player_status.cpp index 414a24dbf..109b772c2 100644 --- a/src/player_status.cpp +++ b/src/player_status.cpp @@ -72,17 +72,13 @@ void PlayerStatus::reset() } void -PlayerStatus::incLives() +PlayerStatus::add_coins(int count) { - player_status->coins = std::min(player_status->coins+100, MAX_COINS); - sound_manager->play("sounds/lifeup.wav"); -} - -void -PlayerStatus::incCoins() -{ - coins++; - sound_manager->play("sounds/coin.wav"); + coins = std::min(coins + count, MAX_COINS); + if(count > 100) + sound_manager->play("sounds/lifeup.wav"); + else + sound_manager->play("sounds/coin.wav"); } void diff --git a/src/player_status.hpp b/src/player_status.hpp index 99af85b1f..0660d6897 100644 --- a/src/player_status.hpp +++ b/src/player_status.hpp @@ -45,8 +45,7 @@ public: PlayerStatus(); ~PlayerStatus(); void reset(); - void incLives(); - void incCoins(); + void add_coins(int count); void set_keys(int new_key); void write(lisp::Writer& writer); diff --git a/src/scripting/functions.cpp b/src/scripting/functions.cpp index 4856d36c1..aa08d282c 100644 --- a/src/scripting/functions.cpp +++ b/src/scripting/functions.cpp @@ -63,6 +63,11 @@ void wait_for_screenswitch(HSQUIRRELVM vm) script_manager->set_wakeup_event(vm, ScriptManager::SCREEN_SWITCHED); } +void exit_screen() +{ + main_loop->exit_screen(); +} + std::string translate(const std::string& text) { return dictionary_manager.get_dictionary().translate(text); diff --git a/src/scripting/functions.hpp b/src/scripting/functions.hpp index baf827d41..83b03a036 100644 --- a/src/scripting/functions.hpp +++ b/src/scripting/functions.hpp @@ -68,6 +68,12 @@ void wait(HSQUIRRELVM vm, float seconds) __suspend; void wait_for_screenswitch(HSQUIRRELVM vm) __suspend; /** + * Exits the currently running screen (force exit from worldmap or scrolling + * text for example) + */ +void exit_screen(); + +/** * Translate a text into the users language (by looking it up in the .po * files) */ diff --git a/src/scripting/player.hpp b/src/scripting/player.hpp index 579652463..033eb1934 100644 --- a/src/scripting/player.hpp +++ b/src/scripting/player.hpp @@ -12,21 +12,15 @@ public: {} #endif -#if 0 /** * Set tux bonus. * This can be "grow", "fireflower" or "iceflower" at the moment */ virtual void set_bonus(const std::string& bonus) = 0; /** - * Give tux another life - */ - virtual void add_life() = 0; - /** * Give tux more coins */ virtual void add_coins(int count) = 0; -#endif /** * Make tux invicible for a short amount of time */ diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index 996842631..dd4502650 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -999,6 +999,62 @@ static int Player_release_hook(SQUserPointer ptr, int ) return 0; } +static int Player_set_bonus_wrapper(HSQUIRRELVM vm) +{ + Scripting::Player* _this; + if(SQ_FAILED(sq_getinstanceup(vm, 1, reinterpret_cast (&_this), 0))) { + sq_throwerror(vm, _SC("'set_bonus' called without instance")); + return SQ_ERROR; + } + const char* arg0; + if(SQ_FAILED(sq_getstring(vm, 2, &arg0))) { + sq_throwerror(vm, _SC("Argument 1 not a string")); + return SQ_ERROR; + } + + try { + _this->set_bonus(arg0); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'set_bonus'")); + return SQ_ERROR; + } + +} + +static int Player_add_coins_wrapper(HSQUIRRELVM vm) +{ + Scripting::Player* _this; + if(SQ_FAILED(sq_getinstanceup(vm, 1, reinterpret_cast (&_this), 0))) { + sq_throwerror(vm, _SC("'add_coins' called without instance")); + return SQ_ERROR; + } + int arg0; + if(SQ_FAILED(sq_getinteger(vm, 2, &arg0))) { + sq_throwerror(vm, _SC("Argument 1 not an integer")); + return SQ_ERROR; + } + + try { + _this->add_coins(arg0); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'add_coins'")); + return SQ_ERROR; + } + +} + static int Player_make_invincible_wrapper(HSQUIRRELVM vm) { Scripting::Player* _this; @@ -1537,6 +1593,25 @@ static int wait_for_screenswitch_wrapper(HSQUIRRELVM vm) } +static int exit_screen_wrapper(HSQUIRRELVM vm) +{ + (void) vm; + + try { + Scripting::exit_screen(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'exit_screen'")); + return SQ_ERROR; + } + +} + static int translate_wrapper(HSQUIRRELVM vm) { const char* arg0; @@ -1961,6 +2036,12 @@ void register_supertux_wrapper(HSQUIRRELVM v) throw SquirrelError(v, "Couldn't register function 'wait_for_screenswitch'"); } + sq_pushstring(v, "exit_screen", -1); + sq_newclosure(v, &exit_screen_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'exit_screen'"); + } + sq_pushstring(v, "translate", -1); sq_newclosure(v, &translate_wrapper, 0); if(SQ_FAILED(sq_createslot(v, -3))) { @@ -2256,6 +2337,18 @@ void register_supertux_wrapper(HSQUIRRELVM v) msg << "Couldn't create new class 'Player'"; throw SquirrelError(v, msg.str()); } + sq_pushstring(v, "set_bonus", -1); + sq_newclosure(v, &Player_set_bonus_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'set_bonus'"); + } + + sq_pushstring(v, "add_coins", -1); + sq_newclosure(v, &Player_add_coins_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'add_coins'"); + } + sq_pushstring(v, "make_invincible", -1); sq_newclosure(v, &Player_make_invincible_wrapper, 0); if(SQ_FAILED(sq_createslot(v, -3))) { -- 2.11.0