]> git.sesse.net Git - ccbs/blob - bigscreen/top10scorescreen.cpp
Center some headings a bit better.
[ccbs] / bigscreen / top10scorescreen.cpp
1 #include <cstdio>
2 #include <algorithm>
3
4 #include "resolution.h"
5 #include "top10scorescreen.h"
6 #include "fonts.h"
7
8 #define RANK_X 30
9 #define PLAYER_X 70
10 #define SONG_X 370
11 #define SCORE_X (LOGICAL_SCREEN_WIDTH - 72)
12 #define SONG_MAX_WIDTH (LOGICAL_SCREEN_WIDTH - 480)
13
14 Top10ScoreScreen::Top10ScoreScreen(pqxx::connection &conn, unsigned tournament)
15         : conn(conn), tournament(tournament), scores_changed(conn, "scores"), valid(false)
16 {
17 }
18
19 Top10ScoreScreen::~Top10ScoreScreen()
20 {
21 }
22
23 bool Top10ScoreScreen::check_invalidated()
24 {
25         if (!valid)
26                 return true;
27         if (!scores_changed.get_flag())
28                 return false;
29         scores_changed.reset_flag();
30
31         // check that there are indeed changes, otherwise don't bother
32         std::vector<TopScore> scores;
33         conn.perform(FetchTopScoresForTournament(tournament, 10, &scores));
34         
35         for (std::vector<TopScore>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
36                 if (seen_topscore.count(*i) == 0) {
37                         valid = false;
38                         return true;
39                 }
40         }
41
42         return false;
43 }
44
45 void Top10ScoreScreen::draw(unsigned char *buf, unsigned width, unsigned height)
46 {
47         scores_changed.reset_flag();
48         memset(buf, 0, width * height * 4);
49         set_screen_size(width, height);
50
51         // fetch the top 10 scores
52         std::vector<TopScore> scores;
53         conn.perform(FetchTopScoresForTournament(tournament, 10, &scores));
54
55         {
56                 unsigned width = my_draw_text("Today's top 10 scores", NULL, 40.0);
57                 my_draw_text("Today's top 10 scores", buf, 40.0, LOGICAL_SCREEN_WIDTH/2 - width/2, 60);
58         }
59
60         // simple headings
61         my_draw_text("Player", buf, 12.0, PLAYER_X, 100);
62         my_draw_text("Song", buf, 12.0, SONG_X, 100);
63         width = my_draw_text("Score", NULL, 12.0);
64         my_draw_text("Score", buf, 12.0, SCORE_X - width/2, 100);
65         
66         unsigned row = 1, y = 140;
67         for (std::vector<TopScore>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
68                 char str[16];
69                 unsigned r = 255, g = 255, b = 255;
70
71                 // print new entries in red
72                 if (seen_topscore.count(*i) == 0 && seen_topscore.size() > 0) {
73                         g = b = 0;
74                 }
75
76                 std::sprintf(str, "%u", row++);
77                 unsigned width = my_draw_text(str, NULL, 24.0);
78                 my_draw_text(str, buf, 24.0, RANK_X - width/2, y);
79
80                 my_draw_text(i->nick, buf, 24.0, PLAYER_X, y, r, g, b);
81
82                 if (my_draw_text(i->title, NULL, 24.0) > SONG_MAX_WIDTH &&
83                     !i->shorttitle.empty()) {
84                         my_draw_text(i->shorttitle, buf, 24.0, SONG_X, y, r, g, b);
85                 } else {
86                         my_draw_text(i->title, buf, 24.0, SONG_X, y, r, g, b);
87                 }
88                 
89                 std::sprintf(str, "%u", i->score);
90                 width = my_draw_text(str, NULL, 24.0);
91                 my_draw_text(str, buf, 24.0, SCORE_X - width/2, y, r, g, b);
92
93                 y += 40;
94         }
95         
96         valid = true;
97         
98         seen_topscore.erase(seen_topscore.begin(), seen_topscore.end());
99         std::copy(scores.begin(), scores.end(), std::inserter(seen_topscore, seen_topscore.end()));
100 }
101
102 int Top10ScoreScreen::get_priority()
103 {
104         return 5;
105 }