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"
16 #include "groupscreen.h"
17 #include "top10scorescreen.h"
18 #include "top5chosenscreen.h"
19 #include "splitscreen.h"
20 #include "rotatescreen.h"
22 SDL_Surface *screen = NULL;
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;
31 void init(pqxx::connection &conn)
33 std::vector<widestring> aux_screens;
35 if (screens.size() == 0 || mainscreen != screens[0])
38 for (std::vector<GenericScreen *>::const_iterator i = screens.begin(); i != screens.end(); ++i) {
41 screens.erase(screens.begin(), screens.end());
44 RotateScreen *rs = new RotateScreen();
48 conn.perform(FetchCurrentTournament(&active_tournament));
49 conn.perform(FetchListOfActiveGroups(&active_groups));
51 if (active_tournament.id == -1) {
52 std::fprintf(stderr, "No active tournament\n");
54 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
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);
61 for (unsigned j = 0; j < i->num_machines; ++j) {
63 RotateScreen *rs = new RotateScreen();
64 screens.push_back(rs);
66 rs->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, j, i->num_machines, i->players_per_machine));
71 bool show_only_main_screen = (USE_SPLITSCREEN && screens.size() == 1);
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.
78 RotateScreen *aux_screen = NULL;
79 if (screens.size() < 4 && !show_only_main_screen) {
81 RotateScreen *rs = new RotateScreen();
82 screens.push_back(rs);
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));
92 if (*i == widestring("top5chosen")) {
93 rs->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
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.
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);
112 finished_groups_screen = aux_screen;
114 if (finished_groups_screen != NULL) {
115 std::vector<SkeletonGroup> finished_groups;
116 conn.perform(FetchListOfFinishedGroups(active_tournament.id, &finished_groups));
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));
126 screens.push_back(NULL);
127 screens.push_back(NULL);
128 screens.push_back(NULL);
129 screens.push_back(NULL);
131 if (screens[1] == NULL) {
132 mainscreen = screens[0];
134 mainscreen = new SplitScreen(screens[0], screens[1], screens[2], screens[3]);
139 void main_loop(pqxx::connection &conn)
141 if (active_tournament.id == -1) {
142 // No active tournament, sleep a second or so and exit
143 conn.await_notification(1, 0);
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);
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);
160 SDL_UnlockSurface(screen);
163 conn.await_notification(0, 10000);
166 conn.await_notification(0, 200000);
173 while (SDL_PollEvent(&event)) {
174 if (event.type == SDL_QUIT) {
175 quit_requested = true;
177 if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE) {
178 quit_requested = true;
183 int main(int argc, char **argv)
185 SDL_Init(SDL_INIT_VIDEO);
187 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
188 SDL_ShowCursor(SDL_DISABLE);
190 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF);
192 if (screen == NULL) {
193 fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
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");
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();
213 } while (!tournament_changed.get_flag() && !rounds_changed.get_flag() && !quit_requested);
215 if (quit_requested) {
216 fprintf(stderr, "Quitting...\n");
218 fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
221 } catch (const std::exception &e) {
222 std::fprintf(stderr, "Exception: %s\n", e.what());