Only allocate space for four digits in the score screen -- if somebody actually gets...
[ccbs] / bigscreen / groupscreen.cpp
1 #include <algorithm>
2
3 #include "groupscreen.h"
4 #include "fetch_group.h"
5 #include "fonts.h"
6
7 GroupScreen::GroupScreen(pqxx::connection &conn, unsigned tournament, unsigned round, unsigned parallel)
8         : tournament(tournament), round(round), parallel(parallel), scores_changed(conn, "scores"), conn(conn), valid(false)
9 {
10 }
11
12 GroupScreen::~GroupScreen()
13 {
14 }
15
16 bool GroupScreen::check_invalidated()
17 {
18         // we might want to do this slightly more sophisticated later, but for now this will do
19         return !valid || scores_changed.get_flag();
20 }
21
22 void GroupScreen::draw(unsigned char *buf)
23 {
24         scores_changed.reset_flag();
25
26         Group group;
27         conn.perform(FetchGroup(tournament, round, parallel, &group));
28
29         memset(buf, 0, 800 * 600 * 4);
30
31         // main heading
32         char heading[64];
33         if (parallel == 0) {
34                 sprintf(heading, "Round %u", round);
35         } else {
36                 sprintf(heading, "Round %u, Group %u", round, parallel);
37         }
38
39         {
40                 unsigned width = my_draw_text(heading, NULL, 48.0);
41                 my_draw_text(heading, buf, 48.0, 800/2 - width/2, 60);
42         }
43         
44         // find out how wide each column has to be
45         unsigned width[16];
46         for (unsigned i = 0; i < 16; ++i)
47                 width[i] = 0;
48
49         unsigned max_num_width = my_draw_text("8888", NULL, 22.0);
50         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
51                 unsigned col = 1;
52                 width[0] = std::max(width[0], my_draw_text(i->nick, NULL, 18.0));
53
54                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
55                         if (j->chosen) {
56                                 width[col] = std::max(width[col], my_draw_text(j->song.title, NULL, 12.0) + 
57                                                                   max_num_width + 10);
58                         } else {                
59                                 width[col] = std::max(width[col], my_draw_text(j->song.short_title, NULL, 12.0));
60                                 width[col] = std::max(width[col], max_num_width);
61                         }
62                 }
63         }
64
65         unsigned num_scores = group.players[0].scores.size();
66         
67         width[num_scores + 1] = std::max(my_draw_text("Total", NULL, 12.0), max_num_width);
68         width[num_scores + 2] = my_draw_text("Rank", NULL, 12.0);
69
70         // make column headings from the first player's songs
71         unsigned col = 1;
72         unsigned x = 40 + width[0];
73         for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
74                 if (!i->chosen) {
75                         unsigned this_width = my_draw_text(i->song.short_title, NULL, 12.0);
76                         my_draw_text(i->song.short_title, buf, 12.0, x + width[col] / 2 - this_width / 2, 100);
77                 }
78                 x += width[col] + 20;
79         }
80
81         my_draw_text("Total", buf, 12.0, x + width[num_scores + 1] / 2 - my_draw_text("Total", NULL, 12.0) / 2, 100);
82         x += width[num_scores + 1] + 20;
83         my_draw_text("Rank", buf, 12.0, x + width[num_scores + 2] / 2 - my_draw_text("Rank", NULL, 12.0) / 2, 100);
84         
85         // show all the players and the scores
86         unsigned y = 140;
87         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
88                 my_draw_text(i->nick, buf, 18.0, 20, y);
89
90                 unsigned x = 40 + width[0];
91
92                 unsigned col = 1;
93                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
94                         char text[16];
95                         sprintf(text, "%u", j->score);
96         
97                         unsigned this_width = my_draw_text(text, NULL, 22.0);
98                         if (j->chosen) {
99                                 if (j->score != -1) {
100                                         my_draw_text(text, buf, 22.0, x + max_num_width - this_width, y);
101                                 }
102                                 my_draw_text(j->song.title, buf, 12.0, x + max_num_width + 10, y);
103                         } else {
104                                 if (j->score != -1) {
105                                         my_draw_text(text, buf, 22.0, x + width[col] / 2 - this_width / 2, y);
106                                 }
107                         }
108                         x += width[col] + 20;
109                 }
110
111                 y += 40;
112         }
113         
114         valid = true;
115 }
116