Let each screen get width and height in as a parameter on draw() instead of hardcodin...
[ccbs] / bigscreen / top5chosenscreen.cpp
1 #include <cstdio>
2 #include <algorithm>
3
4 #include "resolution.h"
5 #include "top5chosenscreen.h"
6 #include "fonts.h"
7
8 Top5ChosenScreen::Top5ChosenScreen(pqxx::connection &conn, unsigned tournament)
9         : conn(conn), tournament(tournament), scores_changed(conn, "scores"), valid(false)
10 {
11 }
12
13 Top5ChosenScreen::~Top5ChosenScreen()
14 {
15 }
16
17 bool Top5ChosenScreen::check_invalidated()
18 {
19         if (!valid)
20                 return true;
21         if (!scores_changed.get_flag())
22                 return false;
23
24         // check that there are indeed changes, otherwise don't bother
25         std::vector<TopChosen> scores;
26         conn.perform(FetchTopChosenSongsForTournament(tournament, 5, &scores));
27
28         for (std::vector<TopChosen>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
29                 if (seen_topchosen.count(*i) == 0) {
30                         return true;
31                 }       
32         }
33         
34         return false;
35 }
36
37 void Top5ChosenScreen::draw(unsigned char *buf, unsigned width, unsigned height)
38 {
39         scores_changed.reset_flag();
40         memset(buf, 0, width * height * 4);
41         set_screen_size(width, height);
42
43         // fetch the top 5 chosen songs
44         std::vector<TopChosen> scores;
45         conn.perform(FetchTopChosenSongsForTournament(tournament, 5, &scores));
46
47         {
48                 unsigned width = my_draw_text("Today's top 5 chosen songs", NULL, 40.0);
49                 my_draw_text("Today's top 5 chosen songs", buf, 40.0, LOGICAL_SCREEN_WIDTH/2 - width/2, 60);
50         }
51
52         // simple headings
53         my_draw_text("Song", buf, 12.0, 70, 100);
54         my_draw_text("Frequency", buf, 12.0, 710, 100);
55         
56         unsigned row = 1, y = 140;
57         for (std::vector<TopChosen>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
58                 char str[16];
59                 unsigned r = 255, g = 255, b = 255;
60
61                 // print new entries in red
62                 if (seen_topchosen.count(*i) == 0 && seen_topchosen.size() > 0) {
63                         g = b = 0;
64                 }
65
66                 std::sprintf(str, "%u", row++);
67                 unsigned width = my_draw_text(str, NULL, 24.0);
68                 my_draw_text(str, buf, 24.0, 30 - width/2, y);
69
70                 if (my_draw_text(i->title, NULL, 24.0) > 610) {
71                         my_draw_text(i->shorttitle, buf, 24.0, 70, y, r, g, b);
72                 } else {
73                         my_draw_text(i->title, buf, 24.0, 70, y, r, g, b);
74                 }
75                 
76                 std::sprintf(str, "%u", i->frequency);
77                 width = my_draw_text(str, NULL, 24.0);
78                 my_draw_text(str, buf, 24.0, 745 - width/2, y, r, g, b);
79
80                 y += 40;
81         }
82         
83         valid = true;
84         
85         seen_topchosen.erase(seen_topchosen.begin(), seen_topchosen.end());
86         std::copy(scores.begin(), scores.end(), std::inserter(seen_topchosen, seen_topchosen.end()));
87 }
88