Don't show column headings for chosen songs, as they are not the same everywhere...
[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                 if (!i->chosen)
55                         my_draw_text(i->song.title, buf, x, 30, true, 255, 255, 255);
56                 x += width[col] + 20;
57         }
58         
59         // show all the players and the scores
60         unsigned y = 50;
61         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
62                 my_draw_text(i->nick, buf, 20, y, true, 255, 255, 255);
63
64                 unsigned x = 40 + width[0];
65
66                 unsigned col = 1;
67                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
68                         char text[16];
69                         sprintf(text, "%u", j->score);
70                         
71                         if (j->score != -1) {
72                                 my_draw_text(text, buf, x, y, true, 255, 255, 255);
73                         }
74                         x += width[col] + 20;
75                 }
76
77                 y += 20;
78         }
79         
80         valid = true;
81 }
82