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"
17 #include "groupscreen.h"
18 #include "top10scorescreen.h"
19 #include "top5chosenscreen.h"
20 #include "splitscreen.h"
21 #include "rotatescreen.h"
23 SDL_Surface *screen = NULL;
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;
32 void init(pqxx::connection &conn)
34 std::vector<widestring> aux_screens;
36 if (screens.size() == 0 || mainscreen != screens[0])
39 for (std::vector<GenericScreen *>::const_iterator i = screens.begin(); i != screens.end(); ++i) {
42 screens.erase(screens.begin(), screens.end());
45 RotateScreen *rs = new RotateScreen();
49 conn.perform(FetchCurrentTournament(&active_tournament));
50 conn.perform(FetchListOfActiveGroups(&active_groups));
52 if (active_tournament.id == -1) {
53 std::fprintf(stderr, "No active tournament\n");
55 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
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);
62 for (unsigned j = 0; j < i->num_machines; ++j) {
64 RotateScreen *rs = new RotateScreen();
65 screens.push_back(rs);
67 rs->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, j, i->num_machines, i->players_per_machine));
72 bool show_only_main_screen = (USE_SPLITSCREEN && screens.size() == 1);
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.
79 RotateScreen *aux_screen = NULL;
80 if (screens.size() < 4 && !show_only_main_screen) {
82 RotateScreen *rs = new RotateScreen();
83 screens.push_back(rs);
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));
93 if (*i == widestring("top5chosen")) {
94 rs->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
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.
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);
113 finished_groups_screen = aux_screen;
115 if (finished_groups_screen != NULL) {
116 std::vector<SkeletonGroup> finished_groups;
117 conn.perform(FetchListOfFinishedGroups(active_tournament.id, &finished_groups));
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));
127 screens.push_back(NULL);
128 screens.push_back(NULL);
129 screens.push_back(NULL);
130 screens.push_back(NULL);
132 if (screens[1] == NULL) {
133 mainscreen = screens[0];
135 mainscreen = new SplitScreen(screens[0], screens[1], screens[2], screens[3]);
140 void main_loop(pqxx::connection &conn)
142 if (active_tournament.id == -1) {
143 // No active tournament, sleep a second or so and exit
144 conn.await_notification(1, 0);
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);
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);
161 SDL_UnlockSurface(screen);
164 conn.await_notification(0, 10000);
167 conn.await_notification(0, 200000);
174 while (SDL_PollEvent(&event)) {
175 if (event.type == SDL_QUIT) {
176 quit_requested = true;
178 if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE) {
179 quit_requested = true;
184 int main(int argc, char **argv)
186 SDL_Init(SDL_INIT_VIDEO);
188 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
189 SDL_ShowCursor(SDL_DISABLE);
191 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF);
193 if (screen == NULL) {
194 fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
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");
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();
215 } while (!tournament_changed.get_flag() && !rounds_changed.get_flag() && !quit_requested);
217 if (quit_requested) {
218 fprintf(stderr, "Quitting...\n");
220 fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
223 } catch (const std::exception &e) {
224 std::fprintf(stderr, "Exception: %s\n", e.what());