Make sure FPS limit in the main menu is always correct
[tennix.git] / graphics.c
blobb8936640ad2d234973953ad3852c838dedd26621
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008 Thomas Perl <thp@perli.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 **/
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
28 #include "tennix.h"
29 #include "graphics.h"
31 static Image* images;
33 static SDL_Surface *buffer;
34 static SDL_Surface *background;
36 static SDL_Rect *rect_update_cache;
37 static SDL_Rect *rect_update_old;
38 static int rect_update_cache_current;
39 static int rect_update_old_count;
41 static Uint32 fading_start = 0;
43 #include "data/graphics_data.c"
44 static const ResourceData graphics[] = {
45 RESOURCE(court),
46 RESOURCE(shadow),
47 RESOURCE(player_racket),
48 RESOURCE(ball),
49 RESOURCE(smallish_font),
50 RESOURCE(dkc2),
51 RESOURCE(referee),
52 RESOURCE(ctt_hard),
53 RESOURCE(ctt_clay),
54 RESOURCE(ctt_grass),
55 RESOURCE(sidebar),
56 RESOURCE(tennixlogo),
57 RESOURCE(btnplay),
58 RESOURCE(btnresume),
59 RESOURCE(btnquit),
60 RESOURCE(stadium),
61 RESOURCE(fog),
62 RESOURCE(fog2),
63 RESOURCE(night)
66 void init_graphics() {
67 int i;
68 SDL_Surface* data;
69 SDL_Surface* tmp;
71 #ifndef MACOSX
72 tmp = IMG_Load_RW( SDL_RWFromConstMem( icon, sizeof(icon)), 1);
73 if( tmp != NULL) {
74 SDL_WM_SetIcon( tmp, NULL);
75 SDL_FreeSurface( tmp);
77 #endif
79 rect_update_cache = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
80 rect_update_old = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
81 rect_update_cache_current = 0;
82 rect_update_old_count = 0;
84 images = (Image*)calloc( GR_COUNT, sizeof( Image));
86 for( i=0; i<GR_COUNT; i++) {
87 tmp = IMG_Load_RW( SDL_RWFromConstMem( graphics[i].data, graphics[i].size), 1);
88 if( !tmp) {
89 fprintf( stderr, "Error: %s\n", SDL_GetError());
90 continue;
93 if( GRAPHICS_IS_FONT(i)) {
94 /* Convert to RGB w/ colorkey=black for opacity support */
95 SDL_SetColorKey( tmp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( tmp->format, 0, 0, 0));
96 data = SDL_ConvertSurface( tmp, screen->format, SDL_SRCCOLORKEY | SDL_RLEACCEL);
97 } else {
98 /* Convert to RGBA for alpha channel from PNG */
99 data = SDL_DisplayFormatAlpha( tmp);
101 SDL_FreeSurface( tmp);
103 if( !data) {
104 fprintf( stderr, "Error: %s\n", SDL_GetError());
105 continue;
107 images[i].data = data;
110 buffer = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
111 screen->format->BitsPerPixel,
112 screen->format->Rmask,
113 screen->format->Gmask,
114 screen->format->Bmask,
115 screen->format->Amask);
117 background = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
118 screen->format->BitsPerPixel,
119 screen->format->Rmask,
120 screen->format->Gmask,
121 screen->format->Bmask,
122 screen->format->Amask);
124 if( buffer == NULL) {
125 fprintf( stderr, "Cannot create buffer surface: %s\n", SDL_GetError());
129 void uninit_graphics() {
130 int i;
132 for( i=0; i<GR_COUNT; i++) {
133 SDL_FreeSurface( images[i].data);
136 if( buffer != NULL) {
137 SDL_FreeSurface( buffer);
140 if (background != NULL) {
141 SDL_FreeSurface(background);
144 free(rect_update_cache);
145 free(rect_update_old);
146 free(images);
149 int get_image_width( image_id id) {
150 return images[id].data->w;
153 int get_image_height( image_id id) {
154 return images[id].data->h;
157 int get_sprite_width( image_id id, int items) {
158 return images[id].data->w / items;
161 void show_sprite( image_id id, int pos, int items, int x_offset, int y_offset, int opacity) {
162 SDL_Surface *bitmap;
163 SDL_Rect src, dst;
165 bitmap = images[id].data;
167 if( !bitmap) return;
169 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
171 dst.w = src.w = bitmap->w/items;
172 dst.h = src.h = bitmap->h;
173 src.x = src.w*pos;
174 src.y = 0;
175 dst.x = x_offset;
176 dst.y = y_offset;
178 SDL_BlitSurface( bitmap, &src, screen, &dst);
179 update_rect2(dst);
182 void fill_image_offset( image_id id, int x, int y, int w, int h, int offset_x, int offset_y) {
183 SDL_Surface *bitmap;
184 SDL_Rect src, dst;
185 int dx = 0, dy = 0, cx, cy;
187 if( id >= GR_COUNT) return;
189 bitmap = images[id].data;
191 /* Make negative offsets positive */
192 if (offset_x < 0) {
193 offset_x = (offset_x%bitmap->w)+bitmap->w;
195 if (offset_y < 0) {
196 offset_y = (offset_y%bitmap->h)+bitmap->h;
199 src.y = offset_y % bitmap->h;
200 while( dy < h) {
201 src.h = dst.h = cy = (h-dy > bitmap->h-src.y)?(bitmap->h-src.y):(h-dy);
202 dst.y = y+dy;
204 dx = 0;
205 src.x = offset_x % bitmap->w;
206 while( dx < w) {
207 src.w = dst.w = cx = (w-dx > bitmap->w-src.x)?(bitmap->w-src.x):(w-dx);
208 dst.x = x+dx;
210 SDL_BlitSurface( bitmap, &src, screen, &dst);
211 update_rect2(dst);
213 dx += cx;
214 src.x = 0;
217 dy += cy;
218 src.y = 0;
222 void line_horiz( int y, Uint8 r, Uint8 g, Uint8 b) {
223 rectangle(0, y, screen->w, 1, r, g, b);
226 void line_vert( int x, Uint8 r, Uint8 g, Uint8 b) {
227 rectangle(x, 0, 1, screen->h, r, g, b);
230 void rectangle( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b) {
231 Uint32 color = SDL_MapRGB( screen->format, r, g, b);
232 SDL_Rect rect;
234 rect.x = x;
235 rect.y = y;
236 rect.w = w;
237 rect.h = h;
239 SDL_FillRect( screen, &rect, color);
240 update_rect(x, y, w, h);
243 void draw_button( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
244 float diff = (pressed?1.0-BUTTON_HIGHLIGHT:1.0+BUTTON_HIGHLIGHT);
245 int border = BUTTON_BORDER;
246 rectangle( x, y, w, h, r*diff, g*diff, b*diff);
247 rectangle( x+border, y+border, w-border, h-border, r/diff, g/diff, b/diff);
248 rectangle( x+border, y+border, w-2*border, h-2*border, r, g, b);
251 void draw_button_text( char* s, int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
252 int font_x, font_y;
253 pressed = pressed?1:0;
254 draw_button( x, y, w, h, r, g, b, pressed);
255 font_x = x + w/2 - font_get_string_width( GR_SMALLISH_FONT, s)/2 + pressed*BUTTON_BORDER;
256 font_y = y + h/2 - get_image_height( GR_SMALLISH_FONT)/2 + pressed*BUTTON_BORDER;
257 font_draw_string( GR_SMALLISH_FONT, s, font_x, font_y, 0, 0);
260 void clear_screen()
262 SDL_Rect rect;
264 rect.x = rect.y = 0;
265 rect.w = WIDTH;
266 rect.h = HEIGHT;
268 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
271 void store_screen()
273 SDL_BlitSurface(screen, NULL, background, NULL);
274 rect_update_old[0].x = rect_update_old[0].y = 0;
275 rect_update_old[0].w = WIDTH;
276 rect_update_old[0].h = HEIGHT;
277 rect_update_old_count = 1;
278 rect_update_cache_current = 0;
281 void reset_screen()
283 int i;
284 SDL_Rect* tmp;
285 SDL_BlitSurface(background, NULL, screen, NULL);
287 for (i=0; i<rect_update_cache_current; i++) {
288 SDL_BlitSurface(background, &rect_update_cache[i], screen, &rect_update_cache[i]);
291 /* Save rects I've just updated for redraw later */
292 tmp = rect_update_cache;
293 rect_update_cache = rect_update_old;
294 rect_update_old = tmp;
295 rect_update_old_count = rect_update_cache_current;
296 rect_update_cache_current = 0;
299 void update_rect(Sint32 x, Sint32 y, Sint32 w, Sint32 h)
301 static int inside = 0;
302 if (inside) return;
303 inside = 1;
304 /*rectangle(x, y, w, h, 50+rand()%200, 50+rand()%200 ,50+rand()%200);*/
306 if ((x >= WIDTH) | (y >= HEIGHT) | (x+w <= 0) | (y+h <= 0) | (!w) | (!h)) {
307 inside = 0;
308 return;
311 if (rect_update_cache_current == RECT_UPDATE_CACHE) {
312 fprintf(stderr, "Overflow\n");
313 rect_update_cache_current = 0;
316 SDL_Rect* u = &(rect_update_cache[rect_update_cache_current]);
318 if (x < 0/* && x+w > 0*/) {
319 w += x;
320 x = 0;
322 if (y < 0/* && y+h > 0*/) {
323 h += y;
324 y = 0;
327 if (x+w >= WIDTH) {
328 w -= (x+w-WIDTH+1);
331 if (y+h >= HEIGHT) {
332 h -= (y+h-HEIGHT+1);
335 if (w==0 || h==0) {
336 inside = 0;
337 return;
340 u->x = x;
341 u->y = y;
342 u->w = w;
343 u->h = h;
345 rect_update_cache_current++;
347 inside = 0;
350 void updatescr()
352 int ticks = SDL_GetTicks();
353 SDL_Rect *r_current = NULL, *end_current = NULL;
354 SDL_Rect *r_old = NULL;
356 static int fading_last_time = 0;
357 int fading_now = is_fading();
359 if (fading_now) {
360 SDL_SetAlpha(buffer, SDL_SRCALPHA | SDL_RLEACCEL, 255-255*(ticks-fading_start)/FADE_DURATION);
361 SDL_BlitSurface(buffer, NULL, screen, NULL);
362 SDL_UpdateRect(screen, 0, 0, 0, 0);
363 } else if (fading_last_time && !fading_now) {
364 SDL_UpdateRect(screen, 0, 0, 0, 0);
365 } else {
366 if (rect_update_old_count == rect_update_cache_current) {
367 /* Merge rects into one single rect list */
368 r_old = rect_update_old;
369 r_current = rect_update_cache;
370 end_current = rect_update_cache + rect_update_cache_current;
371 while (r_current != end_current) {
372 r_old->w = MAX(r_current->x+r_current->w, r_old->x+r_old->w);
373 r_old->h = MAX(r_current->y+r_current->h, r_old->y+r_old->h);
374 r_old->x = MIN(r_current->x, r_old->x);
375 r_old->y = MIN(r_current->y, r_old->y);
376 r_old->w -= r_old->x;
377 r_old->h -= r_old->y;
379 r_current++;
380 r_old++;
382 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
383 } else {
384 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
385 SDL_UpdateRects(screen, rect_update_cache_current, rect_update_cache);
389 reset_screen();
390 fading_last_time = fading_now;
393 void start_fade() {
394 SDL_BlitSurface( screen, NULL, buffer, NULL);
395 fading_start = SDL_GetTicks();
398 int is_fading() {
399 return SDL_GetTicks() < fading_start+FADE_DURATION;
402 int font_get_metrics( image_id id, char ch, int* xp, int* wp) {
403 SDL_Surface *bitmap;
404 int pos, x = -1, w = 0;
405 int search_pos = 0, search_x = 0;
406 Uint8 red, green, blue;
408 if( id >= GR_COUNT) return 0;
410 pos = toupper( ch) - ' '; /* ' ' = first character in font bitmap */
412 bitmap = images[id].data;
414 SDL_LockSurface( bitmap);
415 while( search_x < bitmap->w) {
416 GET_PIXEL_RGB( bitmap, search_x, 0, &red, &green, &blue);
418 /* Increase pos counter if we have a "marker" pixel (255,0,255) */
419 if( red > 250 && green < 10 && blue > 250) {
420 search_pos++;
421 if( search_pos == pos) {
422 x = search_x;
423 } else if( search_pos == pos + 1) {
424 w = search_x - x;
425 break;
429 search_x++;
431 SDL_UnlockSurface( bitmap);
433 if( wp != NULL) (*wp) = w;
434 if( xp != NULL) (*xp) = x;
436 return w;
439 int font_draw_char( image_id id, char ch, int x_offset, int y_offset) {
440 SDL_Surface *bitmap;
441 SDL_Rect src, dst;
442 int x = -1, w = 0;
444 font_get_metrics( id, ch, &x, &w);
445 if( x == -1) return w;
447 bitmap = images[id].data;
449 dst.w = src.w = w;
450 dst.h = src.h = bitmap->h - 1;
451 src.x = x;
452 src.y = 1;
453 dst.x = x_offset;
454 dst.y = y_offset;
456 SDL_BlitSurface( bitmap, &src, screen, &dst);
458 return src.w;
461 void font_draw_string_alpha( image_id id, const char* s, int x_offset, int y_offset, int start, int animation, int opacity) {
462 int y = y_offset;
463 int x = x_offset;
464 int i, additional_x = 0, additional_y = 0;
465 float xw = 0.0, xw_diff;
466 SDL_Surface *bitmap;
468 if( id > GR_COUNT) return;
470 bitmap = images[id].data;
471 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
473 if( animation & ANIMATION_BUNGEE) {
474 xw = (25.0*sinf( start/10.0));
475 xw_diff = 0.0;
476 x -= xw / 2;
479 if( animation & ANIMATION_PENDULUM) {
480 x -= (int)(20.0*sinf( start/20.0));
481 additional_x += 21;
484 for( i=0; i<strlen(s); i++) {
485 if( animation & ANIMATION_WAVE) {
486 y = y_offset + (int)(3.0*sinf( start/10.0 + x/30.0));
488 x += font_draw_char( id, s[i], x, y);
489 if( animation & ANIMATION_BUNGEE) {
490 xw_diff += xw/strlen(s);
491 if( xw_diff > 1.0 || xw_diff < -1.0) {
492 x += (int)(xw_diff);
493 if( xw_diff > 1.0) {
494 xw_diff -= 1.0;
495 } else {
496 xw_diff += 1.0;
501 if (animation & ANIMATION_WAVE) {
502 additional_y += 4;
504 if (animation & ANIMATION_BUNGEE) {
505 additional_x += 26;
507 update_rect(x_offset-additional_x, y_offset-additional_y, x-x_offset+additional_x*2, get_image_height(id)+additional_y*2);
510 int font_get_string_width( image_id id, const char* s) {
511 int w = 0, i;
513 for( i=0; i<strlen(s); i++) {
514 w += font_get_metrics( id, s[i], NULL, NULL);
517 return w;
520 void draw_line_faded( int x1, int y1, int x2, int y2, int r, int g, int b, int r2, int g2, int b2) {
521 float step, dx, dy, x = x1, y = y1;
522 int i;
523 char fade = (r!=r2 || g!=g2 || b!=b2);
525 step = (float)(abs(x2-x1)>abs(y2-y1)?abs(x2-x1):abs(y2-y1));
526 dx = (float)(x2-x1) / step;
527 dy = (float)(y2-y1) / step;
529 SDL_LockSurface( screen);
530 for( i=0; i<step; i++) {
531 x += dx;
532 y += dy;
533 if( x < 0.0 || x >= WIDTH || y < 0.0 || y >= HEIGHT) {
534 continue;
536 if( fade) {
537 SET_PIXEL_RGB( screen, (int)x, (int)y, (r*(step-i)+r2*i)/step, (g*(step-i)+g2*i)/step, (b*(step-i)+b2*i)/step);
538 } else {
539 SET_PIXEL_RGB( screen, (int)x, (int)y, r, g, b);
542 SDL_UnlockSurface( screen);