Clean up the my_draw_text() interface considerably, and let us set sizes per-call.
[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         unsigned max_num_width = my_draw_text("88888", NULL, 12.0);
37
38         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
39                 width[0] = std::max(width[0], my_draw_text(i->nick, NULL, 12.0));
40
41                 unsigned col = 1;
42                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
43                         if (j->chosen) {
44                                 width[col] = std::max(width[col], my_draw_text(j->song.title, NULL, 12.0) + 
45                                                                   max_num_width + 10);
46                         } else {                
47                                 width[col] = std::max(width[col], my_draw_text(j->song.title, NULL, 12.0));
48                                 width[col] = std::max(width[col], max_num_width);
49                         }
50                 }
51         }       
52
53         // make column headings from the first player's songs
54         unsigned col = 1, x = 40 + width[0];
55         for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
56                 if (!i->chosen)
57                         my_draw_text(i->song.title, buf, 12.0, x, 30);
58                 x += width[col] + 20;
59         }
60         
61         // show all the players and the scores
62         unsigned y = 50;
63         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
64                 my_draw_text(i->nick, buf, 12.0, 20, y);
65
66                 unsigned x = 40 + width[0];
67
68                 unsigned col = 1;
69                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
70                         char text[16];
71                         sprintf(text, "%u", j->score);
72         
73                         unsigned this_width = my_draw_text(text, NULL, 12.0);
74                         if (j->chosen) {
75                                 if (j->score != -1) {
76                                         my_draw_text(text, buf, 12.0, x + max_num_width - this_width, y);
77                                 }
78                                 my_draw_text(j->song.title, buf, 12.0, x + max_num_width + 10, y);
79                         } else {
80                                 if (j->score != -1) {
81                                         my_draw_text(text, buf, 12.0, x + width[col] / 2 - this_width / 2, y);
82                                 }
83                         }
84                         x += width[col] + 20;
85                 }
86
87                 y += 20;
88         }
89         
90         valid = true;
91 }
92