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());
42 RotateScreen *rs = new RotateScreen();
45 conn.perform(FetchCurrentTournament(&active_tournament));
46 conn.perform(FetchListOfActiveGroups(&active_groups));
48 if (active_tournament.id == -1) {
49 std::fprintf(stderr, "No active tournament\n");
51 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
53 for (std::vector<SkeletonGroup>::const_iterator i = active_groups.begin(); i != active_groups.end(); ++i) {
54 std::fprintf(stderr, "tourn: %u round: %u parallel: %u num_machines: %u\n",
55 i->tournament, i->round, i->parallel, i->num_machines);
58 for (unsigned j = 0; j < i->num_machines; ++j) {
59 rs->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, j, i->num_machines, i->players_per_machine));
65 conn.perform(FetchAuxilliaryScreens(&aux_screens));
66 for (std::vector<widestring>::const_iterator i = aux_screens.begin(); i != aux_screens.end(); ++i) {
67 if (*i == widestring("top10scores")) {
68 rs->add_screen(new Top10ScoreScreen(conn, active_tournament.id));
71 if (*i == widestring("top5chosen")) {
72 rs->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
79 void main_loop(pqxx::connection &conn)
81 if (active_tournament.id == -1) {
82 // No active tournament, sleep a second or so and exit
83 conn.await_notification(1, 0);
87 if (mainscreen && mainscreen->check_invalidated()) {
88 mainscreen->draw(framebuf, SCREEN_WIDTH, SCREEN_HEIGHT);
89 SDL_LockSurface(screen);
90 for (unsigned y = 0; y < SCREEN_HEIGHT; ++y) {
91 unsigned char *sptr = framebuf + y * SCREEN_WIDTH * 4;
92 unsigned char *dptr = (unsigned char *)screen->pixels + y * screen->pitch;
93 memcpy(dptr, sptr, SCREEN_WIDTH * 4);
95 SDL_UnlockSurface(screen);
97 conn.await_notification(0, 10000);
100 conn.await_notification(0, 200000);
104 int main(int argc, char **argv)
106 SDL_Init(SDL_INIT_VIDEO);
108 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
110 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF);
112 if (screen == NULL) {
113 fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
119 pqxx::connection conn("dbname=ccbs host=www.positivegaming.com user=ccbs password=GeT|>>B_");
120 FlagTrigger tournament_changed(conn, "active_tournament");
121 FlagTrigger rounds_changed(conn, "active_groups");
123 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
124 // (at least currently)
126 tournament_changed.reset_flag();
127 rounds_changed.reset_flag();
132 } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
133 std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
135 } catch (const std::exception &e) {
136 std::fprintf(stderr, "Exception: %s\n", e.what());