Clean up the array copying for the new high scores list. It was way
[attac-man.git] / screen.c
blob0ba9ed059a71c3ba3a5b0bdb0e6f366711603c1e
1 /*
2 Pacman Arena
3 Copyright (C) 2003 Nuno Subtil
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 static const char cvsid[] =
21 "$Id: screen.c,v 1.7 2003/11/30 17:43:55 nsubtil Exp $";
23 #ifdef _WIN32
24 #include <windows.h>
25 #endif
27 #include <SDL.h>
28 #include <GL/gl.h>
30 #include <stdlib.h>
31 #include <assert.h>
33 #include "object.h"
34 #include "gfx.h"
36 #include "screen.h"
38 #define GAMMA_DIR_FADEIN 1
39 #define GAMMA_DIR_FADEOUT 2
41 static struct screen scr;
42 static float cur_gamma, target_gamma;
43 static int gamma_direction;
44 static float last_gamma;
47 initializes the screen
49 void screen_init(int fullscreen, int w, int h, int bpp)
51 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE);
52 /* XXX - startup resolutions */
53 atexit(SDL_Quit);
55 scr.width = w;
56 scr.height = h;
57 scr.bpp = bpp;
58 scr.fullscreen = ( fullscreen ? SDL_FULLSCREEN : 0);
59 scr.surface = NULL;
60 scr.viewports = NULL;
61 scr.cur_viewport = 0;
63 scr.num_viewports = 1;
65 screen_switch_resolution();
67 SDL_WM_SetCaption("Attac-Man", "Attac-Man");
68 SDL_ShowCursor(SDL_DISABLE);
70 printf("%s\n", glGetString(GL_RENDERER));
71 printf("%s\n", glGetString(GL_VENDOR));
75 attempts to setup the screen according to scr
77 void screen_switch_resolution(void)
79 gfx_unload_all();
80 object_release_dlists();
82 scr.surface = SDL_SetVideoMode(scr.width, scr.height, scr.bpp, SDL_OPENGL | scr.fullscreen);
83 if(scr.surface == NULL)
85 printf("screen_switch_resolution: couldn't set mode (%dx%dx%d %s)\n",
86 scr.width, scr.height, scr.bpp, scr.fullscreen ? "fullscreen" : "windowed");
87 SDL_Quit();
88 exit(1);
91 screen_setup_viewports();
93 gfx_reload_all();
94 object_recompile_dlists();
98 sets a new resolution
100 void screen_set_resolution(int w, int h)
102 scr.width = w;
103 scr.height = h;
106 void screen_get_resolution(int *w, int *h)
108 *w = scr.width;
109 *h = scr.height;
113 sets a new bpp
115 void screen_set_bpp(int bpp)
117 scr.bpp = bpp;
121 toggles full-screen mode
123 void screen_toggle_fullscreen(void)
125 if(scr.fullscreen)
126 scr.fullscreen = 0;
127 else
128 scr.fullscreen = SDL_FULLSCREEN;
130 screen_switch_resolution();
134 sets the number of viewports
136 void screen_set_num_viewports(int n)
138 scr.num_viewports = n;
139 screen_setup_viewports();
143 sets up viewports for scr
144 XXX - should handle any number of viewports ?
146 void screen_setup_viewports(void)
148 struct viewport *vp;
150 if(scr.viewports)
151 free(scr.viewports);
153 scr.viewports = malloc(sizeof(struct viewport) * scr.num_viewports);
155 switch(scr.num_viewports)
157 case 1:
158 /* one viewport, whole screen */
159 vp = &scr.viewports[0];
161 vp->ll_x = 0;
162 vp->ll_y = 0;
164 vp->width = scr.width;
165 vp->height = scr.height;
167 break;
169 case 2:
170 /* two viewports, horizontal split */
171 vp = &scr.viewports[0];
172 vp->ll_x = 0;
173 vp->ll_y = scr.height / 2;
174 vp->width = scr.width;
175 vp->height = scr.height / 2;
177 vp = &scr.viewports[1];
178 vp->ll_x = 0;
179 vp->ll_y = 0;
180 vp->width = scr.width;
181 vp->height = scr.height / 2;
183 break;
185 default:
186 printf("screen_setup_viewports: too many viewports (%d)\n", scr.num_viewports);
187 SDL_Quit();
188 exit(1);
190 break;
195 sets the active opengl viewport
197 void screen_set_active_viewport(int vp)
199 assert(vp >= 0 && vp < scr.num_viewports);
200 glViewport(scr.viewports[vp].ll_x, scr.viewports[vp].ll_y,
201 scr.viewports[vp].width, scr.viewports[vp].height);
205 returns a viewport structure
207 struct viewport *screen_get_viewport(int vp)
209 assert(vp >= 0 && vp < scr.num_viewports);
210 return &scr.viewports[vp];
214 fades in for X seconds
216 void screen_set_fadein(float time)
218 cur_gamma = 0.0;
219 target_gamma = time;
220 gamma_direction = GAMMA_DIR_FADEIN;
224 fades out for X seconds
226 void screen_set_fadeout(float time)
228 cur_gamma = 0.0;
229 target_gamma = time;
230 gamma_direction = GAMMA_DIR_FADEOUT;
233 void screen_update_gamma(float delta)
235 float gamma;
237 cur_gamma += delta;
238 if(cur_gamma > target_gamma)
239 cur_gamma = target_gamma;
241 gamma = cur_gamma / target_gamma;
242 if(gamma_direction == GAMMA_DIR_FADEOUT)
243 gamma = 1.0 - gamma;
245 if(last_gamma != gamma)
247 SDL_SetGamma(gamma, gamma, gamma);
248 last_gamma = gamma;
252 void screen_reset_gamma(void)
254 cur_gamma = target_gamma = last_gamma = 1.0;
255 SDL_SetGamma(cur_gamma, cur_gamma, cur_gamma);