]> git.sesse.net Git - ccbs/blob - bigscreen/ccbs_bigscreen.cpp
d09b1c4d36f1fae35d363aa0fe7bd01ae1ef34aa
[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 "groupscreen.h"
17 #include "top10scorescreen.h"
18 #include "top5chosenscreen.h"
19 #include "splitscreen.h"
20 #include "rotatescreen.h"
21
22 SDL_Surface *screen = NULL;
23
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
30 void init(pqxx::connection &conn)
31 {
32         std::vector<widestring> aux_screens;
33         
34         if (screens.size() == 0 || mainscreen != screens[0])
35                 delete mainscreen;
36         
37         for (std::vector<GenericScreen *>::const_iterator i = screens.begin(); i != screens.end(); ++i) {
38                 delete *i;
39         }
40         screens.erase(screens.begin(), screens.end());
41
42         RotateScreen *rs = new RotateScreen();
43         mainscreen = rs;
44         
45         conn.perform(FetchCurrentTournament(&active_tournament));
46         conn.perform(FetchListOfActiveGroups(&active_groups));
47
48         if (active_tournament.id == -1) {
49                 std::fprintf(stderr, "No active tournament\n");
50         } else {
51                 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
52
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);
56
57                         // memory leaks here?
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));
60                         }
61                 }
62         }
63
64         {
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));
69                                 continue;
70                         }
71                         if (*i == widestring("top5chosen")) {
72                                 rs->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
73                                 continue;
74                         }
75                 }
76         }
77 }
78
79 void main_loop(pqxx::connection &conn)
80 {
81         if (active_tournament.id == -1) {
82                 // No active tournament, sleep a second or so and exit
83                 conn.await_notification(1, 0);
84                 return;
85         }
86
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);
94                 }
95                 SDL_UnlockSurface(screen);
96                 SDL_Flip(screen);
97                 conn.await_notification(0, 10000);
98         } else {
99                 SDL_Flip(screen);
100                 conn.await_notification(0, 200000);
101         }
102 }
103
104 int main(int argc, char **argv)
105 {
106         SDL_Init(SDL_INIT_VIDEO);
107 #if USE_FULLSCREEN
108         screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
109         SDL_ShowCursor(SDL_DISABLE);
110 #else
111         screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF);
112 #endif
113         if (screen == NULL) {
114                 fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
115                 exit(1);
116         }
117         
118         try {
119                 init_freetype();
120                 pqxx::connection conn("dbname=ccbs host=www.positivegaming.com user=ccbs password=GeT|>>B_");
121                 FlagTrigger tournament_changed(conn, "active_tournament");
122                 FlagTrigger rounds_changed(conn, "active_groups");
123                 
124                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
125                 // (at least currently)
126                 for ( ;; ) {
127                         tournament_changed.reset_flag();
128                         rounds_changed.reset_flag();
129                         init(conn);
130                         do {
131                                 main_loop(conn);
132                                 conn.get_notifs();
133                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
134                         std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
135                 }
136         } catch (const std::exception &e) {
137                 std::fprintf(stderr, "Exception: %s\n", e.what());
138                 exit(1);
139         }
140
141         SDL_Quit();
142         
143         return 0;
144 }