# highscore_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: highscore_gui.cpp 15554 2009-02-23 01:51:19Z rubidium $ */
    00002
    00005 #include "highscore.h"
    00006 #include "table/strings.h"
    00007 #include "gfx_func.h"
    00008 #include "table/sprites.h"
    00009 #include "window_gui.h"
    00010 #include "window_func.h"
    00011 #include "network/network.h"
    00012 #include "command_func.h"
    00013 #include "company_func.h"
    00014 #include "company_base.h"
    00015 #include "settings_type.h"
    00016 #include "strings_func.h"
    00017 #include "openttd.h"
    00018
    00019 struct EndGameHighScoreBaseWindow : Window {
    00020   uint32 background_img;
    00021   int8 rank;
    00022
    00023   EndGameHighScoreBaseWindow(const WindowDesc *desc) : Window(desc)
    00024   {
    00025   }
    00026
    00027   /* Always draw a maximized window and within there the centered background */
    00028   void SetupHighScoreEndWindow(uint *x, uint *y)
    00029   {
    00030     /* resize window to "full-screen" */
    00031     this->width = _screen.width;
    00032     this->height = _screen.height;
    00033     this->widget[0].right = this->width - 1;
    00034     this->widget[0].bottom = this->height - 1;
    00035
    00036     this->DrawWidgets();
    00037
    00038     /* Center Highscore/Endscreen background */
    00039     *x = max(0, (_screen.width  / 2) - (640 / 2));
    00040     *y = max(0, (_screen.height / 2) - (480 / 2));
    00041     for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
    00042       DrawSprite(this->background_img + i, PAL_NONE, *x, *y + (i * 50));
    00043     }
    00044   }
    00045
    00046   virtual void OnClick(Point pt, int widget)
    00047   {
    00048     delete this;
    00049   }
    00050 };
    00051
    00053 struct EndGameWindow : EndGameHighScoreBaseWindow {
    00054   EndGameWindow(const WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
    00055   {
    00056     /* Pause in single-player to have a look at the highscore at your own leisure */
    00057     if (!_networking) DoCommandP(0, 1, 0, CMD_PAUSE);
    00058
    00059     this->background_img = SPR_TYCOON_IMG1_BEGIN;
    00060
    00061     if (_local_company != COMPANY_SPECTATOR) {
    00062       const Company *c = GetCompany(_local_company);
    00063       if (c->old_economy[0].performance_history == SCORE_MAX) {
    00064         this->background_img = SPR_TYCOON_IMG2_BEGIN;
    00065       }
    00066     }
    00067
    00068     /* In a network game show the endscores of the custom difficulty 'network' which is the last one
    00069      * as well as generate a TOP5 of that game, and not an all-time top5. */
    00070     if (_networking) {
    00071       this->window_number = lengthof(_highscore_table) - 1;
    00072       this->rank = SaveHighScoreValueNetwork();
    00073     } else {
    00074       /* in single player _local company is always valid */
    00075       const Company *c = GetCompany(_local_company);
    00076       this->window_number = _settings_game.difficulty.diff_level;
    00077       this->rank = SaveHighScoreValue(c);
    00078     }
    00079
    00080     MarkWholeScreenDirty();
    00081   }
    00082
    00083   ~EndGameWindow()
    00084   {
    00085     if (!_networking) DoCommandP(0, 0, 0, CMD_PAUSE); // unpause
    00086     ShowHighscoreTable(this->window_number, this->rank);
    00087   }
    00088
    00089   virtual void OnPaint()
    00090   {
    00091     const Company *c;
    00092     uint x, y;
    00093
    00094     this->SetupHighScoreEndWindow(&x, &y);
    00095
    00096     if (!IsValidCompanyID(_local_company)) return;
    00097
    00098     c = GetCompany(_local_company);
    00099     /* We need to get performance from last year because the image is shown
    00100      * at the start of the new year when these things have already been copied */
    00101     if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
    00102       SetDParam(0, c->index);
    00103       SetDParam(1, c->index);
    00104       SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
    00105       DrawStringMultiCenter(x + (640 / 2), y + 107, STR_021C_OF_ACHIEVES_STATUS, 640);
    00106     } else {
    00107       SetDParam(0, c->index);
    00108       SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
    00109       DrawStringMultiCenter(x + (640 / 2), y + 157, STR_021B_ACHIEVES_STATUS, 640);
    00110     }
    00111   }
    00112 };
    00113
    00114 struct HighScoreWindow : EndGameHighScoreBaseWindow {
    00115   HighScoreWindow(const WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
    00116   {
    00117     /* pause game to show the chart */
    00118     if (!_networking) DoCommandP(0, 1, 0, CMD_PAUSE);
    00119
    00120     /* Close all always on-top windows to get a clean screen */
    00121     if (_game_mode != GM_MENU) HideVitalWindows();
    00122
    00123     MarkWholeScreenDirty();
    00124     this->window_number = difficulty; // show highscore chart for difficulty...
    00125     this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
    00126     this->rank = ranking;
    00127   }
    00128
    00129   ~HighScoreWindow()
    00130   {
    00131     if (_game_mode != GM_MENU) ShowVitalWindows();
    00132
    00133     if (!_networking) DoCommandP(0, 0, 0, CMD_PAUSE); // unpause
    00134   }
    00135
    00136   virtual void OnPaint()
    00137   {
    00138     const HighScore *hs = _highscore_table[this->window_number];
    00139     uint x, y;
    00140
    00141     this->SetupHighScoreEndWindow(&x, &y);
    00142
    00143     SetDParam(0, ORIGINAL_END_YEAR);
    00144     SetDParam(1, this->window_number + STR_6801_EASY);
    00145     DrawStringMultiCenter(x + (640 / 2), y + 62, !_networking ? STR_0211_TOP_COMPANIES_WHO_REACHED : STR_TOP_COMPANIES_NETWORK_GAME, 500);
    00146
    00147     /* Draw Highscore peepz */
    00148     for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
    00149       SetDParam(0, i + 1);
    00150       DrawString(x + 40, y + 140 + (i * 55), STR_0212, TC_BLACK);
    00151
    00152       if (hs[i].company[0] != '\0') {
    00153         TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
    00154
    00155         DoDrawString(hs[i].company, x + 71, y + 140 + (i * 55), colour);
    00156         SetDParam(0, hs[i].title);
    00157         SetDParam(1, hs[i].score);
    00158         DrawString(x + 71, y + 160 + (i * 55), STR_HIGHSCORE_STATS, colour);
    00159       }
    00160     }
    00161   }
    00162 };
    00163
    00164 static const Widget _highscore_widgets[] = {
    00165 {      WWT_PANEL, RESIZE_NONE,  COLOUR_END, 0, 640, 0, 480, 0x0, STR_NULL},
    00166 {   WIDGETS_END},
    00167 };
    00168
    00169 static const WindowDesc _highscore_desc = {
    00170   0, 0, 641, 481, 641, 481,
    00171   WC_HIGHSCORE, WC_NONE,
    00172   0,
    00173   _highscore_widgets,
    00174 };
    00175
    00176 static const WindowDesc _endgame_desc = {
    00177   0, 0, 641, 481, 641, 481,
    00178   WC_ENDSCREEN, WC_NONE,
    00179   0,
    00180   _highscore_widgets,
    00181 };
    00182
    00186 void ShowHighscoreTable(int difficulty, int8 ranking)
    00187 {
    00188   DeleteWindowByClass(WC_HIGHSCORE);
    00189   new HighScoreWindow(&_highscore_desc, difficulty, ranking);
    00190 }
    00191
    00194 void ShowEndGameChart()
    00195 {
    00196   /* Dedicated server doesn't need the highscore window and neither does -v null. */
    00197   if (_network_dedicated || (!_networking && !IsValidCompanyID(_local_company))) return;
    00198
    00199   HideVitalWindows();
    00200   DeleteWindowByClass(WC_ENDSCREEN);
    00201   new EndGameWindow(&_endgame_desc);
    00202 }