First support for in-game weather (rainfall)
[tennix.git] / graphics.c
bloba9d77c75b0a7cd63972307219b2bd616a50a371a
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)
63 void init_graphics() {
64 int i;
65 SDL_Surface* data;
66 SDL_Surface* tmp;
68 #ifndef MACOSX
69 tmp = IMG_Load_RW( SDL_RWFromConstMem( icon, sizeof(icon)), 1);
70 if( tmp != NULL) {
71 SDL_WM_SetIcon( tmp, NULL);
72 SDL_FreeSurface( tmp);
74 #endif
76 rect_update_cache = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
77 rect_update_old = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
78 rect_update_cache_current = 0;
79 rect_update_old_count = 0;
81 images = (Image*)calloc( GR_COUNT, sizeof( Image));
83 for( i=0; i<GR_COUNT; i++) {
84 tmp = IMG_Load_RW( SDL_RWFromConstMem( graphics[i].data, graphics[i].size), 1);
85 if( !tmp) {
86 fprintf( stderr, "Error: %s\n", SDL_GetError());
87 continue;
90 if( GRAPHICS_IS_FONT(i)) {
91 /* Convert to RGB w/ colorkey=black for opacity support */
92 SDL_SetColorKey( tmp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( tmp->format, 0, 0, 0));
93 data = SDL_ConvertSurface( tmp, screen->format, SDL_SRCCOLORKEY | SDL_RLEACCEL);
94 } else {
95 /* Convert to RGBA for alpha channel from PNG */
96 data = SDL_DisplayFormatAlpha( tmp);
98 SDL_FreeSurface( tmp);
100 if( !data) {
101 fprintf( stderr, "Error: %s\n", SDL_GetError());
102 continue;
104 images[i].data = data;
107 buffer = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
108 screen->format->BitsPerPixel,
109 screen->format->Rmask,
110 screen->format->Gmask,
111 screen->format->Bmask,
112 screen->format->Amask);
114 background = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
115 screen->format->BitsPerPixel,
116 screen->format->Rmask,
117 screen->format->Gmask,
118 screen->format->Bmask,
119 screen->format->Amask);
121 if( buffer == NULL) {
122 fprintf( stderr, "Cannot create buffer surface: %s\n", SDL_GetError());
126 void uninit_graphics() {
127 int i;
129 for( i=0; i<GR_COUNT; i++) {
130 SDL_FreeSurface( images[i].data);
133 if( buffer != NULL) {
134 SDL_FreeSurface( buffer);
137 if (background != NULL) {
138 SDL_FreeSurface(background);
141 free(rect_update_cache);
142 free(rect_update_old);
143 free(images);
146 int get_image_width( image_id id) {
147 return images[id].data->w;
150 int get_image_height( image_id id) {
151 return images[id].data->h;
154 int get_sprite_width( image_id id, int items) {
155 return images[id].data->w / items;
158 void show_sprite( image_id id, int pos, int items, int x_offset, int y_offset, int opacity) {
159 SDL_Surface *bitmap;
160 SDL_Rect src, dst;
162 bitmap = images[id].data;
164 if( !bitmap) return;
166 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
168 dst.w = src.w = bitmap->w/items;
169 dst.h = src.h = bitmap->h;
170 src.x = src.w*pos;
171 src.y = 0;
172 dst.x = x_offset;
173 dst.y = y_offset;
175 SDL_BlitSurface( bitmap, &src, screen, &dst);
176 update_rect2(dst);
179 void fill_image( image_id id, int x, int y, int w, int h) {
180 SDL_Surface *bitmap;
181 SDL_Rect src, dst;
182 int dx = 0, dy = 0, cx, cy;
184 if( id >= GR_COUNT) return;
186 bitmap = images[id].data;
187 src.x = src.y = 0;
189 while( dy < h) {
190 src.h = dst.h = cy = (h-dy > bitmap->h)?(bitmap->h):(h-dy);
191 dst.y = y+dy;
193 dx = 0;
194 while( dx < w) {
195 src.w = dst.w = cx = (w-dx > bitmap->w)?(bitmap->w):(w-dx);
196 dst.x = x+dx;
198 SDL_BlitSurface( bitmap, &src, screen, &dst);
199 update_rect2(dst);
201 dx += cx;
204 dy += cy;
208 void line_horiz( int y, Uint8 r, Uint8 g, Uint8 b) {
209 rectangle(0, y, screen->w, 1, r, g, b);
212 void line_vert( int x, Uint8 r, Uint8 g, Uint8 b) {
213 rectangle(x, 0, 1, screen->h, r, g, b);
216 void rectangle( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b) {
217 Uint32 color = SDL_MapRGB( screen->format, r, g, b);
218 SDL_Rect rect;
220 rect.x = x;
221 rect.y = y;
222 rect.w = w;
223 rect.h = h;
225 SDL_FillRect( screen, &rect, color);
226 update_rect(x, y, w, h);
229 void draw_button( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
230 float diff = (pressed?1.0-BUTTON_HIGHLIGHT:1.0+BUTTON_HIGHLIGHT);
231 int border = BUTTON_BORDER;
232 rectangle( x, y, w, h, r*diff, g*diff, b*diff);
233 rectangle( x+border, y+border, w-border, h-border, r/diff, g/diff, b/diff);
234 rectangle( x+border, y+border, w-2*border, h-2*border, r, g, b);
237 void draw_button_text( char* s, int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
238 int font_x, font_y;
239 pressed = pressed?1:0;
240 draw_button( x, y, w, h, r, g, b, pressed);
241 font_x = x + w/2 - font_get_string_width( GR_SMALLISH_FONT, s)/2 + pressed*BUTTON_BORDER;
242 font_y = y + h/2 - get_image_height( GR_SMALLISH_FONT)/2 + pressed*BUTTON_BORDER;
243 font_draw_string( GR_SMALLISH_FONT, s, font_x, font_y, 0, 0);
246 void clear_screen()
248 SDL_Rect rect;
250 rect.x = rect.y = 0;
251 rect.w = WIDTH;
252 rect.h = HEIGHT;
254 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
257 void store_screen()
259 SDL_BlitSurface(screen, NULL, background, NULL);
260 rect_update_old[0].x = rect_update_old[0].y = 0;
261 rect_update_old[0].w = WIDTH;
262 rect_update_old[0].h = HEIGHT;
263 rect_update_old_count = 1;
264 rect_update_cache_current = 0;
267 void reset_screen()
269 int i;
270 SDL_Rect* tmp;
271 SDL_BlitSurface(background, NULL, screen, NULL);
273 for (i=0; i<rect_update_cache_current; i++) {
274 SDL_BlitSurface(background, &rect_update_cache[i], screen, &rect_update_cache[i]);
277 /* Save rects I've just updated for redraw later */
278 tmp = rect_update_cache;
279 rect_update_cache = rect_update_old;
280 rect_update_old = tmp;
281 rect_update_old_count = rect_update_cache_current;
282 rect_update_cache_current = 0;
285 void update_rect(Sint32 x, Sint32 y, Sint32 w, Sint32 h)
287 static int inside = 0;
288 if (inside) return;
289 inside = 1;
290 /*rectangle(x, y, w, h, 50+rand()%200, 50+rand()%200 ,50+rand()%200);*/
292 if ((x >= WIDTH) | (y >= HEIGHT) | (x+w <= 0) | (y+h <= 0) | (!w) | (!h)) {
293 inside = 0;
294 return;
297 if (rect_update_cache_current == RECT_UPDATE_CACHE) {
298 fprintf(stderr, "Overflow\n");
299 rect_update_cache_current = 0;
302 SDL_Rect* u = &(rect_update_cache[rect_update_cache_current]);
304 if (x < 0/* && x+w > 0*/) {
305 w += x;
306 x = 0;
308 if (y < 0/* && y+h > 0*/) {
309 h += y;
310 y = 0;
313 if (x+w >= WIDTH) {
314 w -= (x+w-WIDTH+1);
317 if (y+h >= HEIGHT) {
318 h -= (y+h-HEIGHT+1);
321 if (w==0 || h==0) {
322 inside = 0;
323 return;
326 u->x = x;
327 u->y = y;
328 u->w = w;
329 u->h = h;
331 rect_update_cache_current++;
333 inside = 0;
336 void updatescr()
338 int ticks = SDL_GetTicks();
339 SDL_Rect *r_current = NULL, *end_current = NULL;
340 SDL_Rect *r_old = NULL;
342 static int fading_last_time = 0;
343 int fading_now = is_fading();
345 if (fading_now) {
346 SDL_SetAlpha(buffer, SDL_SRCALPHA | SDL_RLEACCEL, 255-255*(ticks-fading_start)/FADE_DURATION);
347 SDL_BlitSurface(buffer, NULL, screen, NULL);
348 SDL_UpdateRect(screen, 0, 0, 0, 0);
349 } else if (fading_last_time && !fading_now) {
350 SDL_UpdateRect(screen, 0, 0, 0, 0);
351 } else {
352 if (rect_update_old_count == rect_update_cache_current) {
353 /* Merge rects into one single rect list */
354 r_old = rect_update_old;
355 r_current = rect_update_cache;
356 end_current = rect_update_cache + rect_update_cache_current;
357 while (r_current != end_current) {
358 r_old->w = MAX(r_current->x+r_current->w, r_old->x+r_old->w);
359 r_old->h = MAX(r_current->y+r_current->h, r_old->y+r_old->h);
360 r_old->x = MIN(r_current->x, r_old->x);
361 r_old->y = MIN(r_current->y, r_old->y);
362 r_old->w -= r_old->x;
363 r_old->h -= r_old->y;
365 r_current++;
366 r_old++;
368 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
369 } else {
370 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
371 SDL_UpdateRects(screen, rect_update_cache_current, rect_update_cache);
375 reset_screen();
376 fading_last_time = fading_now;
379 void start_fade() {
380 SDL_BlitSurface( screen, NULL, buffer, NULL);
381 fading_start = SDL_GetTicks();
384 int is_fading() {
385 return SDL_GetTicks() < fading_start+FADE_DURATION;
388 int font_get_metrics( image_id id, char ch, int* xp, int* wp) {
389 SDL_Surface *bitmap;
390 int pos, x = -1, w = 0;
391 int search_pos = 0, search_x = 0;
392 Uint8 red, green, blue;
394 if( id >= GR_COUNT) return 0;
396 pos = toupper( ch) - ' '; /* ' ' = first character in font bitmap */
398 bitmap = images[id].data;
400 SDL_LockSurface( bitmap);
401 while( search_x < bitmap->w) {
402 GET_PIXEL_RGB( bitmap, search_x, 0, &red, &green, &blue);
404 /* Increase pos counter if we have a "marker" pixel (255,0,255) */
405 if( red > 250 && green < 10 && blue > 250) {
406 search_pos++;
407 if( search_pos == pos) {
408 x = search_x;
409 } else if( search_pos == pos + 1) {
410 w = search_x - x;
411 break;
415 search_x++;
417 SDL_UnlockSurface( bitmap);
419 if( wp != NULL) (*wp) = w;
420 if( xp != NULL) (*xp) = x;
422 return w;
425 int font_draw_char( image_id id, char ch, int x_offset, int y_offset) {
426 SDL_Surface *bitmap;
427 SDL_Rect src, dst;
428 int x = -1, w = 0;
430 font_get_metrics( id, ch, &x, &w);
431 if( x == -1) return w;
433 bitmap = images[id].data;
435 dst.w = src.w = w;
436 dst.h = src.h = bitmap->h - 1;
437 src.x = x;
438 src.y = 1;
439 dst.x = x_offset;
440 dst.y = y_offset;
442 SDL_BlitSurface( bitmap, &src, screen, &dst);
444 return src.w;
447 void font_draw_string_alpha( image_id id, const char* s, int x_offset, int y_offset, int start, int animation, int opacity) {
448 int y = y_offset;
449 int x = x_offset;
450 int i, additional_x = 0, additional_y = 0;
451 float xw = 0.0, xw_diff;
452 SDL_Surface *bitmap;
454 if( id > GR_COUNT) return;
456 bitmap = images[id].data;
457 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
459 if( animation & ANIMATION_BUNGEE) {
460 xw = (25.0*sinf( start/10.0));
461 xw_diff = 0.0;
462 x -= xw / 2;
465 if( animation & ANIMATION_PENDULUM) {
466 x -= (int)(20.0*sinf( start/20.0));
467 additional_x += 21;
470 for( i=0; i<strlen(s); i++) {
471 if( animation & ANIMATION_WAVE) {
472 y = y_offset + (int)(3.0*sinf( start/10.0 + x/30.0));
474 x += font_draw_char( id, s[i], x, y);
475 if( animation & ANIMATION_BUNGEE) {
476 xw_diff += xw/strlen(s);
477 if( xw_diff > 1.0 || xw_diff < -1.0) {
478 x += (int)(xw_diff);
479 if( xw_diff > 1.0) {
480 xw_diff -= 1.0;
481 } else {
482 xw_diff += 1.0;
487 if (animation & ANIMATION_WAVE) {
488 additional_y += 4;
490 if (animation & ANIMATION_BUNGEE) {
491 additional_x += 26;
493 update_rect(x_offset-additional_x, y_offset-additional_y, x-x_offset+additional_x*2, get_image_height(id)+additional_y*2);
496 int font_get_string_width( image_id id, const char* s) {
497 int w = 0, i;
499 for( i=0; i<strlen(s); i++) {
500 w += font_get_metrics( id, s[i], NULL, NULL);
503 return w;
506 void draw_line_faded( int x1, int y1, int x2, int y2, int r, int g, int b, int r2, int g2, int b2) {
507 float step, dx, dy, x = x1, y = y1;
508 int i;
509 char fade = (r!=r2 || g!=g2 || b!=b2);
511 step = (float)(abs(x2-x1)>abs(y2-y1)?abs(x2-x1):abs(y2-y1));
512 dx = (float)(x2-x1) / step;
513 dy = (float)(y2-y1) / step;
515 SDL_LockSurface( screen);
516 for( i=0; i<step; i++) {
517 x += dx;
518 y += dy;
519 if( x < 0.0 || x >= WIDTH || y < 0.0 || y >= HEIGHT) {
520 continue;
522 if( fade) {
523 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);
524 } else {
525 SET_PIXEL_RGB( screen, (int)x, (int)y, r, g, b);
528 SDL_UnlockSurface( screen);