Add a main heading to each group.
[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("88888", NULL, 22.0);
50
51         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
52                 width[0] = std::max(width[0], my_draw_text(i->nick, NULL, 18.0));
53
54                 unsigned col = 1;
55                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
56                         if (j->chosen) {
57                                 width[col] = std::max(width[col], my_draw_text(j->song.title, NULL, 12.0) + 
58                                                                   max_num_width + 10);
59                         } else {                
60                                 width[col] = std::max(width[col], my_draw_text(j->song.short_title, NULL, 12.0));
61                                 width[col] = std::max(width[col], max_num_width);
62                         }
63                 }
64         }       
65
66         // make column headings from the first player's songs
67         unsigned col = 1, x = 40 + width[0];
68         for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
69                 if (!i->chosen) {
70                         unsigned this_width = my_draw_text(i->song.short_title, NULL, 12.0);
71                         my_draw_text(i->song.short_title, buf, 12.0, x + width[col] / 2 - this_width / 2, 100);
72                 }
73                 x += width[col] + 20;
74         }
75         
76         // show all the players and the scores
77         unsigned y = 140;
78         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
79                 my_draw_text(i->nick, buf, 18.0, 20, y);
80
81                 unsigned x = 40 + width[0];
82
83                 unsigned col = 1;
84                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
85                         char text[16];
86                         sprintf(text, "%u", j->score);
87         
88                         unsigned this_width = my_draw_text(text, NULL, 22.0);
89                         if (j->chosen) {
90                                 if (j->score != -1) {
91                                         my_draw_text(text, buf, 22.0, x + max_num_width - this_width, y);
92                                 }
93                                 my_draw_text(j->song.title, buf, 12.0, x + max_num_width + 10, y);
94                         } else {
95                                 if (j->score != -1) {
96                                         my_draw_text(text, buf, 22.0, x + width[col] / 2 - this_width / 2, y);
97                                 }
98                         }
99                         x += width[col] + 20;
100                 }
101
102                 y += 40;
103         }
104         
105         valid = true;
106 }
107