]> git.sesse.net Git - ccbs/blob - bigscreen/ccbs_bigscreen.cpp
e9a30110c5eb449ed67ca0a067f2d6dac4e4eb68
[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 <SDL.h>
7
8 #include "flagtrigger.h"
9 #include "widestring.h"
10 #include "fetch_current_tournament.h"
11 #include "fetch_list_of_active_groups.h"
12 #include "fetch_list_of_finished_groups.h"
13 #include "fetch_group.h"
14 #include "fetch_auxilliary_screens.h"
15 #include "fonts.h"
16 #include "groupscreen.h"
17 #include "top10scorescreen.h"
18 #include "top5chosenscreen.h"
19 #include "splitscreen.h"
20 #include "rotatescreen.h"
21
22 SDL_Surface *screen = NULL;
23
24 Tournament active_tournament;
25 std::vector<SkeletonGroup> active_groups;
26 std::vector<GenericScreen *> screens;
27 GenericScreen *mainscreen = NULL;
28 unsigned char framebuf[SCREEN_WIDTH * SCREEN_HEIGHT * 4], screenbuf[SCREEN_WIDTH * SCREEN_HEIGHT * 4];
29
30 void init(pqxx::connection &conn)
31 {
32         std::vector<widestring> aux_screens;
33         
34         if (screens.size() == 0 || mainscreen != screens[0])
35                 delete mainscreen;
36         
37         for (std::vector<GenericScreen *>::const_iterator i = screens.begin(); i != screens.end(); ++i) {
38                 delete *i;
39         }
40         screens.erase(screens.begin(), screens.end());
41
42         bool show_only_main_screen = (USE_SPLITSCREEN && screens.size() == 1);
43
44 #if !USE_SPLITSCREEN
45         RotateScreen *rs = new RotateScreen();
46         mainscreen = rs;
47 #endif
48
49         conn.perform(FetchCurrentTournament(&active_tournament));
50         conn.perform(FetchListOfActiveGroups(&active_groups));
51
52         if (active_tournament.id == -1) {
53                 std::fprintf(stderr, "No active tournament\n");
54         } else {
55                 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
56
57                 for (std::vector<SkeletonGroup>::const_iterator i = active_groups.begin(); i != active_groups.end(); ++i) {
58                         std::fprintf(stderr, "tourn: %u  round: %u  parallel: %u  num_machines: %u\n",
59                                 i->tournament, i->round, i->parallel, i->num_machines);
60
61                         // memory leaks here?
62                         for (unsigned j = 0; j < i->num_machines; ++j) {
63 #if USE_SPLITSCREEN
64                                 RotateScreen *rs = new RotateScreen();
65                                 screens.push_back(rs);
66 #endif
67                                 rs->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, j, i->num_machines, i->players_per_machine));
68                         }
69                 }
70         }
71
72         /*
73          * Show auxilliary screens except if we have too many already,
74          * or if we're in the special split-screen end-tournament mode,
75          * where there if only one.
76          */
77         RotateScreen *aux_screen = NULL;
78         if (screens.size() < 4 && !show_only_main_screen) {
79 #if USE_SPLITSCREEN
80                 RotateScreen *rs = new RotateScreen();
81                 screens.push_back(rs);
82 #endif
83                 aux_screen = rs;
84
85                 conn.perform(FetchAuxilliaryScreens(&aux_screens));
86                 for (std::vector<widestring>::const_iterator i = aux_screens.begin(); i != aux_screens.end(); ++i) {
87                         if (*i == widestring("top10scores")) {
88                                 rs->add_screen(new Top10ScoreScreen(conn, active_tournament.id));
89                                 continue;
90                         }
91                         if (*i == widestring("top5chosen")) {
92                                 rs->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
93                                 continue;
94                         }
95                 }
96         }
97
98 #if USE_SPLITSCREEN
99         /*
100          * If we still have room, make yet another rotational screen with
101          * results from previous groups -- otherwise tack them onto the end
102          * of the auxilliary screens.
103          */
104         RotateScreen *finished_groups_screen;
105         if (show_only_main_screen) {
106                 finished_groups_screen = NULL;
107         } else if (screens.size() < 4) {
108                 finished_groups_screen = new RotateScreen();
109                 screens.push_back(finished_groups_screen);
110         } else {
111                 finished_groups_screen = aux_screen;
112         }
113         if (finished_groups_screen != NULL) {
114                 std::vector<SkeletonGroup> finished_groups;
115                 conn.perform(FetchListOfFinishedGroups(active_tournament.id, &finished_groups));
116
117                 for (std::vector<SkeletonGroup>::const_iterator i = finished_groups.begin(); i != finished_groups.end(); ++i) {
118                         finished_groups_screen->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, 0, 1, 1));
119                 }
120         }
121 #endif
122
123 #if USE_SPLITSCREEN
124         // hack
125         screens.push_back(NULL);
126         screens.push_back(NULL);
127         screens.push_back(NULL);
128         screens.push_back(NULL);
129
130         if (screens[1] == NULL) {
131                 mainscreen = screens[0];
132         } else {
133                 mainscreen = new SplitScreen(screens[0], screens[1], screens[2], screens[3]);
134         }
135 #endif
136 }
137
138 void main_loop(pqxx::connection &conn)
139 {
140         if (active_tournament.id == -1) {
141                 // No active tournament, sleep a second or so and exit
142                 conn.await_notification(1, 0);
143                 return;
144         }
145
146         if (mainscreen && mainscreen->check_invalidated()) {
147                 mainscreen->draw(framebuf, SCREEN_WIDTH, SCREEN_HEIGHT);
148                 SDL_LockSurface(screen);
149                 for (unsigned y = 0; y < SCREEN_HEIGHT; ++y) {
150                         unsigned char *sptr = framebuf + y * SCREEN_WIDTH * 4;
151                         unsigned char *dptr = (unsigned char *)screen->pixels + y * screen->pitch;
152                         memcpy(dptr, sptr, SCREEN_WIDTH * 4);
153                 }
154                 SDL_UnlockSurface(screen);
155                 SDL_Flip(screen);
156                 conn.await_notification(0, 10000);
157         } else {
158                 SDL_Flip(screen);
159                 conn.await_notification(0, 200000);
160         }
161 }
162
163 int main(int argc, char **argv)
164 {
165         SDL_Init(SDL_INIT_VIDEO);
166 #if USE_FULLSCREEN
167         screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
168         SDL_ShowCursor(SDL_DISABLE);
169 #else
170         screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF);
171 #endif
172         if (screen == NULL) {
173                 fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
174                 exit(1);
175         }
176         
177         try {
178                 init_freetype();
179                 pqxx::connection conn("dbname=ccbs host=www.positivegaming.com user=ccbs password=GeT|>>B_");
180                 FlagTrigger tournament_changed(conn, "active_tournament");
181                 FlagTrigger rounds_changed(conn, "active_groups");
182                 
183                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
184                 // (at least currently)
185                 for ( ;; ) {
186                         tournament_changed.reset_flag();
187                         rounds_changed.reset_flag();
188                         init(conn);
189                         do {
190                                 main_loop(conn);
191                                 conn.get_notifs();
192                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
193                         std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
194                 }
195         } catch (const std::exception &e) {
196                 std::fprintf(stderr, "Exception: %s\n", e.what());
197                 exit(1);
198         }
199
200         SDL_Quit();
201         
202         return 0;
203 }