]> git.sesse.net Git - ccbs/blob - bigscreen/top10scorescreen.cpp
Parametrize color scheme into a separate header file.
[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         unsigned char *ptr = buf;
49         for (unsigned i = 0; i < width * height; ++i) {
50                 *ptr++ = BACKGROUND_BLUE;
51                 *ptr++ = BACKGROUND_GREEN;
52                 *ptr++ = BACKGROUND_RED;
53                 *ptr++ = 0;
54         }
55         set_screen_size(width, height);
56
57         // fetch the top 10 scores
58         std::vector<TopScore> scores;
59         conn.perform(FetchTopScoresForTournament(tournament, 10, &scores));
60
61         {
62                 unsigned width = my_draw_text("Today's top 10 scores", NULL, 40.0);
63                 my_draw_text("Today's top 10 scores", buf, 40.0, LOGICAL_SCREEN_WIDTH/2 - width/2, 60, MAIN_HEADING_RED, MAIN_HEADING_GREEN, MAIN_HEADING_BLUE);
64         }
65
66         // simple headings
67         my_draw_text("Player", buf, 12.0, PLAYER_X, 100, COLUMN_HEADING_RED, COLUMN_HEADING_GREEN, COLUMN_HEADING_BLUE);
68         my_draw_text("Song", buf, 12.0, SONG_X, 100, COLUMN_HEADING_RED, COLUMN_HEADING_GREEN, COLUMN_HEADING_BLUE);
69         width = my_draw_text("Score", NULL, 12.0);
70         my_draw_text("Score", buf, 12.0, SCORE_X - width/2, 100, COLUMN_HEADING_RED, COLUMN_HEADING_GREEN, COLUMN_HEADING_BLUE);
71         
72         unsigned row = 1, y = 140;
73         for (std::vector<TopScore>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
74                 char str[16];
75                 unsigned r = DATA_RED, g = DATA_GREEN, b = DATA_BLUE, rh = ROW_HEADING_RED, gh = ROW_HEADING_GREEN, bh = ROW_HEADING_BLUE;
76
77                 // print new entries in red
78                 if (seen_topscore.count(*i) == 0 && seen_topscore.size() > 0) {
79                         r = rh = FRESH_DATA_RED;
80                         g = gh = FRESH_DATA_GREEN;
81                         b = bh = FRESH_DATA_BLUE;
82                 }
83
84                 std::sprintf(str, "%u", row++);
85                 unsigned width = my_draw_text(str, NULL, 24.0);
86                 my_draw_text(str, buf, 24.0, RANK_X - width/2, y, rh, gh, bh);
87
88                 my_draw_text(i->nick, buf, 24.0, PLAYER_X, y, r, g, b);
89
90                 if (my_draw_text(i->title, NULL, 24.0) > SONG_MAX_WIDTH &&
91                     !i->shorttitle.empty()) {
92                         my_draw_text(i->shorttitle, buf, 24.0, SONG_X, y, r, g, b);
93                 } else {
94                         my_draw_text(i->title, buf, 24.0, SONG_X, y, r, g, b);
95                 }
96                 
97                 std::sprintf(str, "%u", i->score);
98                 width = my_draw_text(str, NULL, 24.0);
99                 my_draw_text(str, buf, 24.0, SCORE_X - width/2, y, r, g, b);
100
101                 y += 40;
102         }
103         
104         valid = true;
105         
106         seen_topscore.erase(seen_topscore.begin(), seen_topscore.end());
107         std::copy(scores.begin(), scores.end(), std::inserter(seen_topscore, seen_topscore.end()));
108 }
109
110 int Top10ScoreScreen::get_priority()
111 {
112         return 5;
113 }