Fix referee talk image usage; support more ball states
[tennix.git] / graphics.c
blob0d0f708642588ae861b9a99fc132b46539559ded
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>
27 #include <unistd.h>
29 #include "tennix.h"
30 #include "graphics.h"
31 #include "archive.h"
32 #include "sound.h"
34 static Image* images;
36 static SDL_Surface *buffer;
37 static SDL_Surface *background;
39 static SDL_Rect *rect_update_cache;
40 static SDL_Rect *rect_update_old;
41 static int rect_update_cache_current;
42 static int rect_update_old_count;
44 static Uint32 fading_start = 0;
46 static char* graphics[] = {
47 "court.png",
48 "shadow.png",
49 "player-racket.png",
50 "ball.png",
51 "smallish-font.png",
52 "dkc2.png",
53 "referee.png",
54 "ctt_hard.png",
55 "ctt_clay.png",
56 "ctt_grass.png",
57 "sidebar.png",
58 "tennixlogo.png",
59 "btnplay.png",
60 "btnresume.png",
61 "btnquit.png",
62 "stadium.png",
63 "fog.png",
64 "fog2.png",
65 "night.png",
66 "talk.png"
69 void init_graphics() {
70 int i;
71 SDL_Surface* data;
72 SDL_Surface* tmp;
73 TennixArchive *tnxar;
75 rect_update_cache = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
76 rect_update_old = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
77 rect_update_cache_current = 0;
78 rect_update_old_count = 0;
80 images = (Image*)calloc( GR_COUNT, sizeof( Image));
82 buffer = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
83 screen->format->BitsPerPixel,
84 screen->format->Rmask,
85 screen->format->Gmask,
86 screen->format->Bmask,
87 screen->format->Amask);
89 background = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
90 screen->format->BitsPerPixel,
91 screen->format->Rmask,
92 screen->format->Gmask,
93 screen->format->Bmask,
94 screen->format->Amask);
96 if( buffer == NULL) {
97 fprintf( stderr, "Cannot create buffer surface: %s\n", SDL_GetError());
100 tnxar = tnxar_open(ARCHIVE_FILE);
102 rectangle(40, (HEIGHT-40)/2, WIDTH-80, 40, 100, 100, 100);
103 store_screen();
104 updatescr();
106 for( i=0; i<GR_COUNT; i++) {
107 if (tnxar_set_current_filename(tnxar, graphics[i]) != 0) {
108 tmp = IMG_Load_RW(SDL_RWFromMem(tnxar_read_current(tnxar), tnxar_size_current(tnxar)), 1);
109 } else {
110 fprintf(stderr, "Cannot find file: %s\n", graphics[i]);
111 exit(EXIT_FAILURE);
113 if( !tmp) {
114 fprintf( stderr, "Error: %s\n", SDL_GetError());
115 continue;
118 if( GRAPHICS_IS_FONT(i)) {
119 /* Convert to RGB w/ colorkey=black for opacity support */
120 SDL_SetColorKey( tmp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( tmp->format, 0, 0, 0));
121 data = SDL_ConvertSurface( tmp, screen->format, SDL_SRCCOLORKEY | SDL_RLEACCEL);
122 } else {
123 /* Convert to RGBA for alpha channel from PNG */
124 data = SDL_DisplayFormatAlpha( tmp);
126 SDL_FreeSurface( tmp);
128 if( !data) {
129 fprintf( stderr, "Error: %s\n", SDL_GetError());
130 continue;
132 images[i].data = data;
134 rectangle(40, (HEIGHT-40)/2, (WIDTH-80)*i/(GR_COUNT+SOUND_MAX), 40, 100, 250, 100);
135 rectangle(40, (HEIGHT-40)/2+20, (WIDTH-80)*i/(GR_COUNT+SOUND_MAX), 10, 170, 250, 170);
136 updatescr();
138 tnxar_close(tnxar);
141 void uninit_graphics() {
142 int i;
144 for( i=0; i<GR_COUNT; i++) {
145 SDL_FreeSurface( images[i].data);
148 if( buffer != NULL) {
149 SDL_FreeSurface( buffer);
152 if (background != NULL) {
153 SDL_FreeSurface(background);
156 free(rect_update_cache);
157 free(rect_update_old);
158 free(images);
161 int get_image_width( image_id id) {
162 return images[id].data->w;
165 int get_image_height( image_id id) {
166 return images[id].data->h;
169 int get_sprite_width( image_id id, int items) {
170 return images[id].data->w / items;
173 void show_sprite( image_id id, int pos, int items, int x_offset, int y_offset, int opacity) {
174 SDL_Surface *bitmap;
175 SDL_Rect src, dst;
177 bitmap = images[id].data;
179 if( !bitmap) return;
181 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
183 dst.w = src.w = bitmap->w/items;
184 dst.h = src.h = bitmap->h;
185 src.x = src.w*pos;
186 src.y = 0;
187 dst.x = x_offset;
188 dst.y = y_offset;
190 SDL_BlitSurface( bitmap, &src, screen, &dst);
191 update_rect2(dst);
194 void fill_image_offset( image_id id, int x, int y, int w, int h, int offset_x, int offset_y) {
195 SDL_Surface *bitmap;
196 SDL_Rect src, dst;
197 int dx = 0, dy = 0, cx, cy;
199 if( id >= GR_COUNT) return;
201 bitmap = images[id].data;
203 /* Make negative offsets positive */
204 if (offset_x < 0) {
205 offset_x = (offset_x%bitmap->w)+bitmap->w;
207 if (offset_y < 0) {
208 offset_y = (offset_y%bitmap->h)+bitmap->h;
211 src.y = offset_y % bitmap->h;
212 while( dy < h) {
213 src.h = dst.h = cy = (h-dy > bitmap->h-src.y)?(bitmap->h-src.y):(h-dy);
214 dst.y = y+dy;
216 dx = 0;
217 src.x = offset_x % bitmap->w;
218 while( dx < w) {
219 src.w = dst.w = cx = (w-dx > bitmap->w-src.x)?(bitmap->w-src.x):(w-dx);
220 dst.x = x+dx;
222 SDL_BlitSurface( bitmap, &src, screen, &dst);
223 update_rect2(dst);
225 dx += cx;
226 src.x = 0;
229 dy += cy;
230 src.y = 0;
234 void line_horiz( int y, Uint8 r, Uint8 g, Uint8 b) {
235 rectangle(0, y, screen->w, 1, r, g, b);
238 void line_vert( int x, Uint8 r, Uint8 g, Uint8 b) {
239 rectangle(x, 0, 1, screen->h, r, g, b);
242 void rectangle( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b) {
243 Uint32 color = SDL_MapRGB( screen->format, r, g, b);
244 SDL_Rect rect;
246 rect.x = x;
247 rect.y = y;
248 rect.w = w;
249 rect.h = h;
251 SDL_FillRect( screen, &rect, color);
252 update_rect(x, y, w, h);
255 void draw_button( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
256 float diff = (pressed?1.0-BUTTON_HIGHLIGHT:1.0+BUTTON_HIGHLIGHT);
257 int border = BUTTON_BORDER;
258 rectangle( x, y, w, h, r*diff, g*diff, b*diff);
259 rectangle( x+border, y+border, w-border, h-border, r/diff, g/diff, b/diff);
260 rectangle( x+border, y+border, w-2*border, h-2*border, r, g, b);
263 void draw_button_text( char* s, int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
264 int font_x, font_y;
265 pressed = pressed?1:0;
266 draw_button( x, y, w, h, r, g, b, pressed);
267 font_x = x + w/2 - font_get_string_width( GR_SMALLISH_FONT, s)/2 + pressed*BUTTON_BORDER;
268 font_y = y + h/2 - get_image_height( GR_SMALLISH_FONT)/2 + pressed*BUTTON_BORDER;
269 font_draw_string( GR_SMALLISH_FONT, s, font_x, font_y, 0, 0);
272 void clear_screen()
274 SDL_Rect rect;
276 rect.x = rect.y = 0;
277 rect.w = WIDTH;
278 rect.h = HEIGHT;
280 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
283 void store_screen()
285 SDL_BlitSurface(screen, NULL, background, NULL);
286 rect_update_old[0].x = rect_update_old[0].y = 0;
287 rect_update_old[0].w = WIDTH;
288 rect_update_old[0].h = HEIGHT;
289 rect_update_old_count = 1;
290 rect_update_cache_current = 0;
293 void reset_screen()
295 int i;
296 SDL_Rect* tmp;
297 SDL_BlitSurface(background, NULL, screen, NULL);
299 for (i=0; i<rect_update_cache_current; i++) {
300 SDL_BlitSurface(background, &rect_update_cache[i], screen, &rect_update_cache[i]);
303 /* Save rects I've just updated for redraw later */
304 tmp = rect_update_cache;
305 rect_update_cache = rect_update_old;
306 rect_update_old = tmp;
307 rect_update_old_count = rect_update_cache_current;
308 rect_update_cache_current = 0;
311 void update_rect(Sint32 x, Sint32 y, Sint32 w, Sint32 h)
313 static int inside = 0;
314 if (inside) return;
315 inside = 1;
316 /*rectangle(x, y, w, h, 50+rand()%200, 50+rand()%200 ,50+rand()%200);*/
318 if ((x >= WIDTH) | (y >= HEIGHT) | (x+w <= 0) | (y+h <= 0) | (!w) | (!h)) {
319 inside = 0;
320 return;
323 if (rect_update_cache_current == RECT_UPDATE_CACHE) {
324 fprintf(stderr, "Overflow\n");
325 rect_update_cache_current = 0;
328 SDL_Rect* u = &(rect_update_cache[rect_update_cache_current]);
330 if (x < 0/* && x+w > 0*/) {
331 w += x;
332 x = 0;
334 if (y < 0/* && y+h > 0*/) {
335 h += y;
336 y = 0;
339 if (x+w >= WIDTH) {
340 w -= (x+w-WIDTH+1);
343 if (y+h >= HEIGHT) {
344 h -= (y+h-HEIGHT+1);
347 if (w==0 || h==0) {
348 inside = 0;
349 return;
352 u->x = x;
353 u->y = y;
354 u->w = w;
355 u->h = h;
357 rect_update_cache_current++;
359 inside = 0;
362 void updatescr()
364 int ticks = SDL_GetTicks();
365 SDL_Rect *r_current = NULL, *end_current = NULL;
366 SDL_Rect *r_old = NULL;
368 static int fading_last_time = 0;
369 int fading_now = is_fading();
371 if (fading_now) {
372 SDL_SetAlpha(buffer, SDL_SRCALPHA | SDL_RLEACCEL, 255-255*(ticks-fading_start)/FADE_DURATION);
373 SDL_BlitSurface(buffer, NULL, screen, NULL);
374 SDL_UpdateRect(screen, 0, 0, 0, 0);
375 } else if (fading_last_time && !fading_now) {
376 SDL_UpdateRect(screen, 0, 0, 0, 0);
377 } else {
378 if (rect_update_old_count == rect_update_cache_current) {
379 /* Merge rects into one single rect list */
380 r_old = rect_update_old;
381 r_current = rect_update_cache;
382 end_current = rect_update_cache + rect_update_cache_current;
383 while (r_current != end_current) {
384 r_old->w = MAX(r_current->x+r_current->w, r_old->x+r_old->w);
385 r_old->h = MAX(r_current->y+r_current->h, r_old->y+r_old->h);
386 r_old->x = MIN(r_current->x, r_old->x);
387 r_old->y = MIN(r_current->y, r_old->y);
388 r_old->w -= r_old->x;
389 r_old->h -= r_old->y;
391 r_current++;
392 r_old++;
394 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
395 } else {
396 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
397 SDL_UpdateRects(screen, rect_update_cache_current, rect_update_cache);
401 reset_screen();
402 fading_last_time = fading_now;
405 void start_fade() {
406 SDL_BlitSurface( screen, NULL, buffer, NULL);
407 fading_start = SDL_GetTicks();
410 int is_fading() {
411 return SDL_GetTicks() < fading_start+FADE_DURATION;
414 int font_get_metrics(image_id id, unsigned char ch, int* xp, int* wp) {
415 /* Caching of x and width values for faster calculation */
416 static int xcache[0xFF][GRAPHICS_FONT_COUNT],
417 wcache[0xFF][GRAPHICS_FONT_COUNT];
418 static unsigned char cacheflag[0xFF][GRAPHICS_FONT_COUNT];
420 SDL_Surface *bitmap;
421 int pos, x = -1, w = 0;
422 int search_pos = 0, search_x = 0;
423 unsigned char font_index = id-GRAPHICS_FONT_FIRST;
424 Uint8 red, green, blue;
426 if(!GRAPHICS_IS_FONT(id)) return 0;
428 if (cacheflag[ch][font_index]) {
429 /* saved! */
430 if (xp != NULL) *xp = xcache[ch][font_index];
431 if (wp != NULL) *wp = wcache[ch][font_index];
432 return wcache[ch][font_index];
435 pos = toupper( ch) - ' '; /* ' ' = first character in font bitmap */
437 bitmap = images[id].data;
439 SDL_LockSurface( bitmap);
440 while( search_x < bitmap->w) {
441 GET_PIXEL_RGB( bitmap, search_x, 0, &red, &green, &blue);
443 /* Increase pos counter if we have a "marker" pixel (255,0,255) */
444 if( red > 250 && green < 10 && blue > 250) {
445 search_pos++;
446 if( search_pos == pos) {
447 x = search_x;
448 } else if( search_pos == pos + 1) {
449 w = search_x - x;
450 break;
454 search_x++;
456 SDL_UnlockSurface( bitmap);
458 if( wp != NULL) (*wp) = w;
459 if( xp != NULL) (*xp) = x;
461 cacheflag[ch][font_index]++;
462 xcache[ch][font_index] = x;
463 wcache[ch][font_index] = w;
465 return w;
468 int font_draw_char( image_id id, char ch, int x_offset, int y_offset) {
469 SDL_Surface *bitmap;
470 SDL_Rect src, dst;
471 int x = -1, w = 0;
473 font_get_metrics( id, ch, &x, &w);
474 if( x == -1) return w;
476 bitmap = images[id].data;
478 dst.w = src.w = w;
479 dst.h = src.h = bitmap->h - 1;
480 src.x = x;
481 src.y = 1;
482 dst.x = x_offset;
483 dst.y = y_offset;
485 SDL_BlitSurface( bitmap, &src, screen, &dst);
487 return src.w;
490 void font_draw_string_alpha( image_id id, const char* s, int x_offset, int y_offset, int start, int animation, int opacity) {
491 int y = y_offset;
492 int x = x_offset;
493 int i, additional_x = 0, additional_y = 0;
494 float xw = 0.0, xw_diff;
495 SDL_Surface *bitmap;
497 if( id > GR_COUNT) return;
499 bitmap = images[id].data;
500 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
502 if( animation & ANIMATION_BUNGEE) {
503 xw = (25.0*sinf( start/10.0));
504 xw_diff = 0.0;
505 x -= xw / 2;
508 if( animation & ANIMATION_PENDULUM) {
509 x -= (int)(20.0*sinf( start/20.0));
510 additional_x += 21;
513 for( i=0; i<strlen(s); i++) {
514 if( animation & ANIMATION_WAVE) {
515 y = y_offset + (int)(3.0*sinf( start/10.0 + x/30.0));
517 x += font_draw_char( id, s[i], x, y);
518 if( animation & ANIMATION_BUNGEE) {
519 xw_diff += xw/strlen(s);
520 if( xw_diff > 1.0 || xw_diff < -1.0) {
521 x += (int)(xw_diff);
522 if( xw_diff > 1.0) {
523 xw_diff -= 1.0;
524 } else {
525 xw_diff += 1.0;
530 if (animation & ANIMATION_WAVE) {
531 additional_y += 4;
533 if (animation & ANIMATION_BUNGEE) {
534 additional_x += 26;
536 update_rect(x_offset-additional_x, y_offset-additional_y, x-x_offset+additional_x*2, get_image_height(id)+additional_y*2);
539 int font_get_string_width( image_id id, const char* s) {
540 int w = 0, i;
542 for( i=0; i<strlen(s); i++) {
543 w += font_get_metrics( id, s[i], NULL, NULL);
546 return w;
549 void draw_line_faded( int x1, int y1, int x2, int y2, int r, int g, int b, int r2, int g2, int b2) {
550 float step, dx, dy, x = x1, y = y1;
551 int i;
552 char fade = (r!=r2 || g!=g2 || b!=b2);
554 step = (float)(abs(x2-x1)>abs(y2-y1)?abs(x2-x1):abs(y2-y1));
555 dx = (float)(x2-x1) / step;
556 dy = (float)(y2-y1) / step;
558 SDL_LockSurface( screen);
559 for( i=0; i<step; i++) {
560 x += dx;
561 y += dy;
562 if( x < 0.0 || x >= WIDTH || y < 0.0 || y >= HEIGHT) {
563 continue;
565 if( fade) {
566 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);
567 } else {
568 SET_PIXEL_RGB( screen, (int)x, (int)y, r, g, b);
571 SDL_UnlockSurface( screen);