File: cachampionshipscreen.cpp

package info (click to toggle)
trophy 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 48,568 kB
  • sloc: cpp: 7,285; sh: 1,032; xml: 277; makefile: 94
file content (205 lines) | stat: -rw-r--r-- 6,839 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "cachampionshipscreen.h"
#include "catrophy.h"
#include "player.h"
#include <functional>
#include <algorithm>

bool RankPredicate::operator()(const Player* const p1, const Player* const p2)
   {
       // In case of level, use alphabetical order
       if (p1->getTotalPoints() == p2->getTotalPoints())
          return (p1->getName() < p2->getName()); 
       else
          return (p1->getTotalPoints() > p2->getTotalPoints());
   }


/** Constructor.
*/
CAChampionshipScreen::CAChampionshipScreen(Player* humanPlayer, const std::vector<Player*> player, const std::vector<std::vector<Player*> > runningPlayer,
                                                                           CL_Surface* background, CL_Surface* button, CL_Surface* buttonEasy, CL_Surface* buttonMedium, CL_Surface* buttonHard, CL_Font* font)
   : CAScreen(),
   m_humanPlayer(humanPlayer),
   m_player(player),
   m_runningPlayers(runningPlayer),
   m_background(background),
   m_font(font),
   m_button(button),
   m_buttonEasy(buttonEasy),
   m_buttonMedium(buttonMedium),
   m_buttonHard(buttonHard),
   m_displayMode(DISPLAY_ADD_POINTS_EASY)
{
    left = 0;
    right = CA_APP->width;
    top = CA_APP->headerHeight + 30;
    bottom = CA_APP->headerHeight + 400;
    barHeight = m_font->get_height() + 6;
    std::sort(m_player.begin(), m_player.end(), RankPredicate());
    title = "C H A M P I O N S H I P";
}

/** Destructor.
*/
CAChampionshipScreen::~CAChampionshipScreen()
{}



/** Runs the screen.
*/
int
CAChampionshipScreen::run() 
{
    //CL_Input::chain_button_release.push_back( this );
    //slot = CL_Input::sig_button_release.connect(thCreateSlot(this, &CAChampionshipScreen::on_button_release));
    //slot = CL_Input::sig_button_press.connect(this, &CAChampionshipScreen::on_button_release);
    slot = CL_Keyboard::sig_key_up().connect(this, &CAChampionshipScreen::on_key_released);

    CA_APP->fadeScreen( true, this );
    done = false;
    cancel = false;

    // Screen loop:
    //
    while( !done ) 
    {
        CA_APP->measureFrameTime( true );

        buildScreen();

        // Play background sound:
        CASoundEffect::playBackgroundMelody();

        CL_Display::flip();   // Copy framebufer to screen
        CL_System::keep_alive();      // VERY VITAL for the system!

        CA_APP->measureFrameTime( false );
    }

    CA_APP->fadeScreen( false, this );
    CA_APP->waitForSilence();

    //CL_Input::chain_button_release.remove( this );

    CL_Keyboard::sig_key_up().disconnect(slot);
    if (m_player[0] == m_humanPlayer)
        return 0;
    else
        return 1;
}

/** Builds the screen.
*/
void
CAChampionshipScreen::buildScreen() 
{
    // Background:
    //
    m_background->draw ( CL_Rect(0, 0, CA_APP->width, CA_APP->height) );
    displayTitle();

    CL_Display::fill_rect( CL_Rect(left, top, right, bottom), CL_Color(0, 0, 0, 64) );
    
    const int rankWidth = 30;
    const int rankLeft = 0;

    const int nameWidth = 200;
    const int nameLeft = 50;

    const int addPtWidth = 30;
    const int addPtLeft = 260;

    const int totalPtWidth = 50;
    const int totalPtLeft = 300;

    const int caWidth = (totalPtLeft + totalPtWidth)*2;
    const int middleMargin = 30;
   

    const int caHeight = (barHeight * m_player.size())/2;
    const int marginTop = (top+bottom)/2 - caHeight/2;

    for (unsigned int rank=0; rank < m_player.size(); rank++)
    {
        std::ostringstream ossRankStr, ossTotalPoints, ossRacePoints;
        ossRankStr << rank + 1 << ".";
        ossTotalPoints << m_player[rank]->getTotalPoints();
        int marginLeft = (right+left)/2 + middleMargin/2;
        int upPos = rank - m_player.size()/2; 
        if (rank < m_player.size()/2 )
        {
            marginLeft -= caWidth/2 + middleMargin;
            upPos = rank;
        }
        // Buttons:
        //
        m_button->draw ( CL_Rect(marginLeft+rankLeft, marginTop+barHeight*upPos, marginLeft+rankLeft+rankWidth, marginTop+(barHeight*(upPos+1))) );
        m_button->draw ( CL_Rect(marginLeft+nameLeft, marginTop+barHeight*upPos, marginLeft+nameLeft+nameWidth, marginTop+(barHeight*(upPos+1))) );
        if (m_displayMode != DISPLAY_CHAMPIONSHIP)
        {
            for (int i=0; i<= int(m_displayMode); i++)
            {
                std::vector<Player*>::const_iterator it = std::find(m_runningPlayers[i].begin(), m_runningPlayers[i].end(), m_player[rank]);
                if (it != m_runningPlayers[i].end() && m_player[rank]->getRacePoints() != 0)
                {
                    CL_Surface* buttonToUse = (i==0 ? m_buttonEasy : (i==1)? m_buttonMedium : m_buttonHard); // Choose the button color
                    buttonToUse->draw ( CL_Rect(marginLeft+addPtLeft, marginTop+barHeight*upPos, marginLeft+addPtLeft+addPtWidth, marginTop+(barHeight*(upPos+1))) );
                    ossRacePoints << "+" << m_player[rank]->getRacePoints();
                }
            }
        }
        m_button->draw ( CL_Rect(marginLeft+totalPtLeft, marginTop+barHeight*upPos, marginLeft+totalPtLeft+totalPtWidth,  marginTop+(barHeight*(upPos+1))) );

        // Texts:
        //
        const int textPosX = marginLeft;
        const int textPosY = marginTop + 4 + barHeight*upPos;
        m_font->set_alignment(origin_top_center, 0, 0);
        m_font->draw( textPosX+rankLeft+rankWidth/2, textPosY,  ossRankStr.str());
        m_font->draw( textPosX+nameLeft+nameWidth/2, textPosY, m_player[rank]->getName() );
        m_font->draw( textPosX+addPtLeft+addPtWidth/2, textPosY, ossRacePoints.str() );
        m_font->draw( textPosX+totalPtLeft+totalPtWidth/2, textPosY, ossTotalPoints.str() );
    }
}

/** Called on key release.
*/
void
CAChampionshipScreen::on_key_released (const CL_InputEvent &key) 
{
    switch( key.id ) 
    {
        // Activate:
        //
    case CL_KEY_ENTER:
    case CL_KEY_SPACE:
    case CL_KEY_ESCAPE:
        if (m_displayMode != DISPLAY_CHAMPIONSHIP)
        {
            int displayInt = int(m_displayMode) + 1;
            m_displayMode = DisplayMode(displayInt);
            if (m_displayMode == DISPLAY_CHAMPIONSHIP)
            {
                for (unsigned int pl=0; pl < m_player.size(); pl++)
                {
                    m_player[pl]->addMoney( m_player[pl]->getRaceMoney() );
                    m_player[pl]->setTotalPoints( m_player[pl]->getTotalPoints() + m_player[pl]->getRacePoints() );
                    m_player[pl]->resetForRace(0, NULL); // Player doesn't belong to a race for now    
                }
                // players points have changed so we sort the player list again
                std::sort(m_player.begin(), m_player.end(), RankPredicate());
            }
        }
        else
        {
            done = true;
        }
        break;

    default:
        break;
    }
}

// EOF