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