]> git.sesse.net Git - ccbs/blob - bigscreen/groupscreen.cpp
Add column headings and fix widths.
[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         // find out how wide each column has to be
32         unsigned width[16];
33         for (unsigned i = 0; i < 16; ++i)
34                 width[i] = 0;
35
36         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
37                 width[0] = std::max(width[0], my_draw_text(i->nick, NULL, 0, 0, false, 0, 0, 0));
38
39                 unsigned col = 1;
40                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
41                         if (j->chosen) {
42                                 width[col] = std::max(width[col], my_draw_text(j->song.title, NULL, 0, 0, false, 0, 0, 0) + 
43                                                                   my_draw_text("8888", NULL, 0, 0, false, 0, 0, 0) + 10);
44                         } else {                
45                                 width[col] = std::max(width[col], my_draw_text(j->song.title, NULL, 0, 0, false, 0, 0, 0));
46                                 width[col] = std::max(width[col], my_draw_text("8888", NULL, 0, 0, false, 0, 0, 0));
47                         }
48                 }
49         }       
50
51         // make column headings from the first player's songs
52         unsigned col = 1, x = 40 + width[0];
53         for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
54                 my_draw_text(i->song.title, buf, x, 30, true, 255, 255, 255);
55                 x += width[col] + 20;
56         }
57         
58         // show all the players and the scores
59         unsigned y = 50;
60         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
61                 my_draw_text(i->nick, buf, 20, y, true, 255, 255, 255);
62
63                 unsigned x = 40 + width[0];
64
65                 unsigned col = 1;
66                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
67                         char text[16];
68                         sprintf(text, "%u", j->score);
69                         
70                         if (j->score != -1) {
71                                 my_draw_text(text, buf, x, y, true, 255, 255, 255);
72                         }
73                         x += width[col] + 20;
74                 }
75
76                 y += 20;
77         }
78         
79         valid = true;
80 }
81