]> git.sesse.net Git - ccbs/blob - bigscreen/ccbs_bigscreen.cpp
In RotateScreen destructor, do not leak subscreen surfaces.
[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 #if !USE_SPLITSCREEN
43         RotateScreen *rs = new RotateScreen();
44         mainscreen = rs;
45 #endif
46
47         conn.perform(FetchCurrentTournament(&active_tournament));
48         conn.perform(FetchListOfActiveGroups(&active_groups));
49
50         if (active_tournament.id == -1) {
51                 std::fprintf(stderr, "No active tournament\n");
52         } else {
53                 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
54
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);
58
59                         // memory leaks here?
60                         for (unsigned j = 0; j < i->num_machines; ++j) {
61 #if USE_SPLITSCREEN
62                                 RotateScreen *rs = new RotateScreen();
63                                 screens.push_back(rs);
64 #endif
65                                 rs->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, j, i->num_machines, i->players_per_machine));
66                         }
67                 }
68         }
69
70         bool show_only_main_screen = (USE_SPLITSCREEN && screens.size() == 1);
71
72         /*
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.
76          */
77         RotateScreen *aux_screen = NULL;
78         if (screens.size() < 4 && !show_only_main_screen) {
79 #if USE_SPLITSCREEN
80                 RotateScreen *rs = new RotateScreen();
81                 screens.push_back(rs);
82 #endif
83                 aux_screen = rs;
84
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));
89                                 continue;
90                         }
91                         if (*i == widestring("top5chosen")) {
92                                 rs->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
93                                 continue;
94                         }
95                 }
96         }
97
98 #if USE_SPLITSCREEN
99         /*
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.
103          */
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);
110         } else {
111                 finished_groups_screen = aux_screen;
112         }
113         if (finished_groups_screen != NULL) {
114                 std::vector<SkeletonGroup> finished_groups;
115                 conn.perform(FetchListOfFinishedGroups(active_tournament.id, &finished_groups));
116
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));
119                 }
120         }
121 #endif
122
123 #if USE_SPLITSCREEN
124         // hack
125         screens.push_back(NULL);
126         screens.push_back(NULL);
127         screens.push_back(NULL);
128         screens.push_back(NULL);
129
130         if (screens[1] == NULL) {
131                 mainscreen = screens[0];
132         } else {
133                 mainscreen = new SplitScreen(screens[0], screens[1], screens[2], screens[3]);
134         }
135 #endif
136 }
137
138 void main_loop(pqxx::connection &conn)
139 {
140         if (active_tournament.id == -1) {
141                 // No active tournament, sleep a second or so and exit
142                 conn.await_notification(1, 0);
143                 return;
144         }
145
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);
151                 } else {
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);
158                         }
159                         SDL_UnlockSurface(screen);
160                 }
161                 SDL_Flip(screen);
162                 conn.await_notification(0, 10000);
163         } else {
164                 SDL_Flip(screen);
165                 conn.await_notification(0, 200000);
166         }
167 }
168
169 int main(int argc, char **argv)
170 {
171         SDL_Init(SDL_INIT_VIDEO);
172 #if USE_FULLSCREEN
173         screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
174         SDL_ShowCursor(SDL_DISABLE);
175 #else
176         screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF);
177 #endif
178         if (screen == NULL) {
179                 fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
180                 exit(1);
181         }
182         
183         try {
184                 init_freetype();
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");
188                 
189                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
190                 // (at least currently)
191                 for ( ;; ) {
192                         tournament_changed.reset_flag();
193                         rounds_changed.reset_flag();
194                         init(conn);
195                         do {
196                                 main_loop(conn);
197                                 conn.get_notifs();
198                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
199                         std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
200                 }
201         } catch (const std::exception &e) {
202                 std::fprintf(stderr, "Exception: %s\n", e.what());
203                 exit(1);
204         }
205
206         SDL_Quit();
207         
208         return 0;
209 }