/src/MenuExperience.cpp

http://github.com/clintbellanger/flare · C++ · 138 lines · 79 code · 26 blank · 33 comment · 8 complexity · 16f3727f21d86fe6bad1b34e52eaf718 MD5 · raw file

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