Only allocate space for four digits in the score screen -- if somebody actually gets...
[ccbs] / bigscreen / ccbs_bigscreen.cpp
1 #include <cstdio>
2 #include <cstring>
3 #include <iconv.h>
4 #include <unistd.h>
5 #include <pqxx/pqxx>
6 #include <tinyptc.h>
7 #include "flagtrigger.h"
8 #include "widestring.h"
9 #include "fetch_current_tournament.h"
10 #include "fetch_list_of_active_groups.h"
11 #include "fetch_group.h"
12 #include "fonts.h"
13 #include "groupscreen.h"
14
15 Tournament active_tournament;
16 std::vector<SkeletonGroup> active_groups;
17 std::vector<GenericScreen *> screens;
18 unsigned char framebuf[800 * 600 * 4], screenbuf[800 * 600 * 4];
19
20 void init(pqxx::connection &conn)
21 {
22         for (std::vector<GenericScreen *>::const_iterator i = screens.begin(); i != screens.end(); ++i) {
23                 delete *i;
24         }
25         screens.erase(screens.begin(), screens.end());
26         
27         conn.perform(FetchCurrentTournament(&active_tournament));
28         conn.perform(FetchListOfActiveGroups(&active_groups));
29
30         if (active_tournament.id == -1) {
31                 std::fprintf(stderr, "No active tournament\n");
32         } else {
33                 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
34
35                 for (std::vector<SkeletonGroup>::const_iterator i = active_groups.begin(); i != active_groups.end(); ++i) {
36                         std::fprintf(stderr, "tourn: %u  round: %u   parallel: %u\n",
37                                 i->tournament, i->round, i->parallel);
38
39                         screens.push_back(new GroupScreen(conn, i->tournament, i->round, i->parallel));
40                 }
41         }
42 }
43
44 void main_loop(pqxx::connection &conn)
45 {
46         if (active_tournament.id == -1) {
47                 // No active tournament, sleep a second or so and exit
48                 conn.await_notification(1, 0);
49                 return;
50         }
51
52         memset(framebuf, 0, 800*600*4);
53         
54         if (screens.size() > 0) {
55                 if (screens[0]->check_invalidated()) {
56                         screens[0]->draw(screenbuf);
57                 }
58
59                 memcpy(framebuf, screenbuf, 800*600*4);
60         }
61
62         ptc_update(framebuf);
63         conn.await_notification(0, 50000);
64 }
65
66 int main(int argc, char **argv)
67 {
68         ptc_open("CCBS bigscreen", 800, 600);
69         
70         try {
71                 init_freetype();
72                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
73                 FlagTrigger tournament_changed(conn, "active_tournament");
74                 FlagTrigger rounds_changed(conn, "active_groups");
75                 
76                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
77                 // (at least currently)
78                 for ( ;; ) {
79                         tournament_changed.reset_flag();
80                         rounds_changed.reset_flag();
81                         init(conn);
82                         do {
83                                 main_loop(conn);
84                                 conn.get_notifs();
85                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
86                         std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
87                 }
88         } catch (const std::exception &e) {
89                 std::fprintf(stderr, "Exception: %s\n", e.what());
90                 exit(1);
91         }
92         
93         return 0;
94 }