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