/src/MenuExperience.cpp
C++ | 138 lines | 79 code | 26 blank | 33 comment | 8 complexity | 16f3727f21d86fe6bad1b34e52eaf718 MD5 | raw file
Possible License(s): GPL-3.0
1/* 2Copyright 2011 Clint Bellanger 3 4This file is part of FLARE. 5 6FLARE is free software: you can redistribute it and/or modify it under the terms 7of the GNU General Public License as published by the Free Software Foundation, 8either version 3 of the License, or (at your option) any later version. 9 10FLARE is distributed in the hope that it will be useful, but WITHOUT ANY 11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12PARTICULAR PURPOSE. See the GNU General Public License for more details. 13 14You should have received a copy of the GNU General Public License along with 15FLARE. If not, see http://www.gnu.org/licenses/ 16*/ 17 18/** 19 * MenuExperience 20 * 21 * Handles the display of the Experience bar on the HUD 22 */ 23 24#include "MenuExperience.h" 25#include "SharedResources.h" 26#include "WidgetLabel.h" 27 28#include <SDL_mixer.h> 29 30#include <string> 31#include <sstream> 32 33using namespace std; 34 35 36MenuExperience::MenuExperience() { 37 loadGraphics(); 38 39 // move these settings to a config file 40 hud_position.x = 0; 41 hud_position.y = 32; 42 hud_position.w = 106; 43 hud_position.h = 26; 44 background_offset.x = 0; 45 background_offset.y = 0; 46 background_size.x = 106; 47 background_size.y = 10; 48 bar_offset.x = 3; 49 bar_offset.y = 3; 50 bar_size.x = 100; 51 bar_size.y = 4; 52 text_offset.x = 2; 53 text_offset.y = 12; 54 text_justify = JUSTIFY_LEFT; 55} 56 57void MenuExperience::loadGraphics() { 58 59 background = IMG_Load(mods->locate("images/menus/menu_xp.png").c_str()); 60 bar = IMG_Load(mods->locate("images/menus/bar_xp.png").c_str()); 61 62 if(!background || !bar) { 63 fprintf(stderr, "Couldn't load image: %s\n", IMG_GetError()); 64 Mix_CloseAudio(); 65 SDL_Quit(); 66 } 67 68 // optimize 69 SDL_Surface *cleanup = background; 70 background = SDL_DisplayFormatAlpha(background); 71 SDL_FreeSurface(cleanup); 72 73 cleanup = bar; 74 bar = SDL_DisplayFormatAlpha(bar); 75 SDL_FreeSurface(cleanup); 76} 77 78/** 79 * Display the XP bar background and current progress. 80 * On mouseover, display progress in text form. 81 */ 82void MenuExperience::render(StatBlock *stats, Point mouse) { 83 84 SDL_Rect src; 85 SDL_Rect dest; 86 int xp_bar_length; 87 88 // don't display anything if max level 89 if (stats->level < 1 || stats->level == MAX_CHARACTER_LEVEL) return; 90 91 // lay down the background image first 92 src.x = 0; 93 src.y = 0; 94 src.w = background_size.x; 95 src.h = background_size.y; 96 dest.x = hud_position.x + background_offset.x; 97 dest.y = hud_position.y + background_offset.y; 98 SDL_BlitSurface(background, &src, screen, &dest); 99 100 // calculate the length of the xp bar 101 // when at a new level, 0% progress 102 src.x = 0; 103 src.y = 0; 104 int required = stats->xp_table[stats->level] - stats->xp_table[stats->level-1]; 105 int current = stats->xp - stats->xp_table[stats->level-1]; 106 xp_bar_length = (current * bar_size.x) / required; 107 src.w = xp_bar_length; 108 src.h = bar_size.y; 109 dest.x = hud_position.x + bar_offset.x; 110 dest.y = hud_position.y + bar_offset.y; 111 112 // draw xp bar 113 SDL_BlitSurface(bar, &src, screen, &dest); 114 115 116 string text_label; 117 118 // if mouseover, draw text 119 if (isWithin(hud_position, mouse)) { 120 121 if (stats->level < MAX_CHARACTER_LEVEL) { 122 text_label = msg->get("XP: %d/%d", stats->xp, stats->xp_table[stats->level]); 123 } 124 else { 125 text_label = msg->get("XP: %d", stats->xp); 126 } 127 128 WidgetLabel label; 129 label.set(hud_position.x + text_offset.x, hud_position.y + text_offset.y, text_justify, VALIGN_TOP, text_label, FONT_WHITE); 130 label.render(); 131 } 132} 133 134MenuExperience::~MenuExperience() { 135 SDL_FreeSurface(background); 136 SDL_FreeSurface(bar); 137} 138