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];
30 void init(pqxx::connection &conn)
32 std::vector<widestring> aux_screens;
34 if (screens.size() == 0 || mainscreen != screens[0])
37 for (std::vector<GenericScreen *>::const_iterator i = screens.begin(); i != screens.end(); ++i) {
40 screens.erase(screens.begin(), screens.end());
43 RotateScreen *rs = new RotateScreen();
47 conn.perform(FetchCurrentTournament(&active_tournament));
48 conn.perform(FetchListOfActiveGroups(&active_groups));
50 if (active_tournament.id == -1) {
51 std::fprintf(stderr, "No active tournament\n");
53 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
55 for (std::vector<SkeletonGroup>::const_iterator i = active_groups.begin(); i != active_groups.end(); ++i) {
56 std::fprintf(stderr, "tourn: %u round: %u parallel: %u num_machines: %u\n",
57 i->tournament, i->round, i->parallel, i->num_machines);
60 for (unsigned j = 0; j < i->num_machines; ++j) {
62 RotateScreen *rs = new RotateScreen();
63 screens.push_back(rs);
65 rs->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, j, i->num_machines, i->players_per_machine));
70 bool show_only_main_screen = (USE_SPLITSCREEN && screens.size() == 1);
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.
77 RotateScreen *aux_screen = NULL;
78 if (screens.size() < 4 && !show_only_main_screen) {
80 RotateScreen *rs = new RotateScreen();
81 screens.push_back(rs);
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));
91 if (*i == widestring("top5chosen")) {
92 rs->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
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.
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);
111 finished_groups_screen = aux_screen;
113 if (finished_groups_screen != NULL) {
114 std::vector<SkeletonGroup> finished_groups;
115 conn.perform(FetchListOfFinishedGroups(active_tournament.id, &finished_groups));
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));
125 screens.push_back(NULL);
126 screens.push_back(NULL);
127 screens.push_back(NULL);
128 screens.push_back(NULL);
130 if (screens[1] == NULL) {
131 mainscreen = screens[0];
133 mainscreen = new SplitScreen(screens[0], screens[1], screens[2], screens[3]);
138 void main_loop(pqxx::connection &conn)
140 if (active_tournament.id == -1) {
141 // No active tournament, sleep a second or so and exit
142 conn.await_notification(1, 0);
146 if (mainscreen && mainscreen->check_invalidated()) {
147 if (screen->pitch == SCREEN_WIDTH * 4) {
148 SDL_LockSurface(screen);
149 mainscreen->draw((unsigned char *)screen->pixels, SCREEN_WIDTH, SCREEN_HEIGHT);
150 SDL_UnlockSurface(screen);
152 mainscreen->draw(framebuf, SCREEN_WIDTH, SCREEN_HEIGHT);
153 SDL_LockSurface(screen);
154 for (unsigned y = 0; y < SCREEN_HEIGHT; ++y) {
155 unsigned char *sptr = framebuf + y * SCREEN_WIDTH * 4;
156 unsigned char *dptr = (unsigned char *)screen->pixels + y * screen->pitch;
157 memcpy(dptr, sptr, SCREEN_WIDTH * 4);
159 SDL_UnlockSurface(screen);
162 conn.await_notification(0, 10000);
165 conn.await_notification(0, 200000);
169 int main(int argc, char **argv)
171 SDL_Init(SDL_INIT_VIDEO);
173 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
174 SDL_ShowCursor(SDL_DISABLE);
176 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF);
178 if (screen == NULL) {
179 fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
185 pqxx::connection conn("dbname=ccbs host=www.positivegaming.com user=ccbs password=GeT|>>B_");
186 FlagTrigger tournament_changed(conn, "active_tournament");
187 FlagTrigger rounds_changed(conn, "active_groups");
189 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
190 // (at least currently)
192 tournament_changed.reset_flag();
193 rounds_changed.reset_flag();
198 } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
199 std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
201 } catch (const std::exception &e) {
202 std::fprintf(stderr, "Exception: %s\n", e.what());