Fix sound panning for ground hit
[tennix.git] / graphics.c
blob4e7bbfef0e26ba077768ff136abb087129d7b944
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
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>
28 #include <assert.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <assert.h>
33 #include "tennix.h"
34 #include "graphics.h"
35 #include "archive.h"
36 #include "sound.h"
38 #include "SDL_rotozoom.h"
40 static Image* images;
42 static Font* fonts;
43 FreeList* font_freelist = NULL;
45 static FontDescription font_desc[] = {
46 /**
47 * This is the list of fonts to be loaded. Be sure to
48 * use NULL as filename when using the same .ttf file
49 * the second time in a row - this saves memory in the
50 * loader function.
51 **/
52 { "dustismo.ttf", 13, TTF_STYLE_NORMAL },
53 { NULL, 20, TTF_STYLE_NORMAL },
54 { "itwasntme.ttf", 22, TTF_STYLE_NORMAL },
55 { NULL, 28, TTF_STYLE_NORMAL }
58 static SDL_Surface *buffer;
59 static SDL_Surface *background;
61 static SDL_Rect *rect_update_cache;
62 static SDL_Rect *rect_update_old;
63 static int rect_update_cache_current;
64 static int rect_update_old_count;
66 Uint32 fading_start = 0;
68 static const char* graphics[] = {
69 "court.png",
70 "shadow.png",
71 "player-racket.png",
72 "ball.png",
73 "referee.png",
74 "ctt_hard.png",
75 "ctt_clay.png",
76 "ctt_grass.png",
77 "sidebar.png",
78 "tennixlogo.png",
79 "btnplay.png",
80 "btnresume.png",
81 "btnquit.png",
82 "stadium.png",
83 "fog.png",
84 "fog2.png",
85 "night.png",
86 "talk.png",
87 "cursor.png",
88 "worldmap.png",
89 "loc_margaret_court_arena.png",
90 "loc_stade_roland_garros.png",
91 "loc_court_no_1.png",
92 "loc_arthur_ashe_stadium.png",
93 #ifdef NONFREE_LOCATIONS
94 "loc_training_camp.png",
95 "loc_austrian_open.png",
96 "loc_olympic_green_tennis.png"
97 #endif
100 void init_graphics() {
101 int i;
102 char *d;
103 SDL_Surface* data;
104 SDL_Surface* tmp;
105 TennixArchive *tnxar;
106 struct SDL_RWops* rw;
107 char *font_data = NULL;
109 if (TTF_Init() == -1) {
110 fprintf(stderr, "Cannot init TTF: %s\n", TTF_GetError());
111 exit(EXIT_FAILURE);
114 rect_update_cache = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
115 rect_update_old = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
116 rect_update_cache_current = 0;
117 rect_update_old_count = 0;
119 images = (Image*)calloc( GR_COUNT, sizeof( Image));
120 fonts = (Font*)calloc(FONT_COUNT, sizeof(Font));
122 buffer = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
123 screen->format->BitsPerPixel,
124 screen->format->Rmask,
125 screen->format->Gmask,
126 screen->format->Bmask,
127 screen->format->Amask);
129 background = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
130 screen->format->BitsPerPixel,
131 screen->format->Rmask,
132 screen->format->Gmask,
133 screen->format->Bmask,
134 screen->format->Amask);
136 if( buffer == NULL) {
137 fprintf( stderr, "Cannot create buffer surface: %s\n", SDL_GetError());
140 tnxar = tnxar_open(ARCHIVE_FILE);
141 if (tnxar == NULL) {
142 /* not found in cwd - try installed... */
143 tnxar = tnxar_open(ARCHIVE_FILE_INSTALLED);
144 assert(tnxar != NULL);
147 font_freelist = freelist_create();
149 /* Load fonts from resource file */
150 for (i=0; i<FONT_COUNT; i++) {
151 if (font_desc[i].filename != NULL) {
152 assert(tnxar_set_current_filename(tnxar, font_desc[i].filename) != 0);
153 font_data = tnxar_read_current(tnxar);
154 freelist_append(font_freelist, font_data);
156 assert(font_data != NULL);
157 rw = SDL_RWFromMem(font_data, tnxar_size_current(tnxar));
158 fonts[i].data = TTF_OpenFontRW(rw, 1, font_desc[i].size);
159 assert(fonts[i].data != NULL);
160 TTF_SetFontStyle(fonts[i].data, font_desc[i].style);
163 draw_button(40, (HEIGHT-40)/2, WIDTH-80, 40, 100, 100, 100, 1);
164 store_screen();
165 updatescr();
167 for( i=0; i<GR_COUNT; i++) {
168 if (tnxar_set_current_filename(tnxar, graphics[i]) != 0) {
169 d = tnxar_read_current(tnxar);
170 tmp = IMG_Load_RW(SDL_RWFromMem(d, tnxar_size_current(tnxar)), 1);
171 free(d);
172 } else {
173 fprintf(stderr, "Cannot find file: %s\n", graphics[i]);
174 exit(EXIT_FAILURE);
176 if( !tmp) {
177 fprintf( stderr, "Error: %s\n", SDL_GetError());
178 continue;
181 /* Convert to RGBA in the display format */
182 data = SDL_DisplayFormatAlpha(tmp);
183 SDL_FreeSurface(tmp);
185 if( !data) {
186 fprintf( stderr, "Error: %s\n", SDL_GetError());
187 continue;
189 images[i].data = data;
191 draw_button(40, (HEIGHT-40)/2, (WIDTH-80)*i/(GR_COUNT+SOUND_MAX), 40, 100, 250, 100, 0);
192 rectangle(40+BUTTON_BORDER*2, (HEIGHT-40)/2+20+BUTTON_BORDER, (WIDTH-80)*i/(GR_COUNT+SOUND_MAX)-BUTTON_BORDER*2, 10, 170, 250, 170);
193 updatescr();
195 tnxar_close(tnxar);
198 void uninit_graphics() {
199 int i;
201 for( i=0; i<GR_COUNT; i++) {
202 SDL_FreeSurface( images[i].data);
205 if( buffer != NULL) {
206 SDL_FreeSurface( buffer);
209 if (background != NULL) {
210 SDL_FreeSurface(background);
213 free(rect_update_cache);
214 free(rect_update_old);
215 free(images);
217 for (i=0; i<FONT_COUNT; i++) {
218 TTF_CloseFont(fonts[i].data);
219 fonts[i].data = NULL;
221 free(fonts);
223 freelist_free_all(font_freelist);
225 TTF_Quit();
229 FreeList* freelist_create()
231 FreeList* list = (FreeList*)malloc(sizeof(FreeList));
232 assert(list != NULL);
234 list->head = NULL;
236 return list;
239 void freelist_append(FreeList* list, char* data)
241 FreeListItem* new_item = (FreeListItem*)malloc(sizeof(FreeListItem));
243 assert(list != NULL);
244 assert(data != NULL);
246 new_item->data = data;
248 /* Insert item at the beginning of list */
249 new_item->next = list->head;
250 list->head = new_item;
253 void freelist_free_all(FreeList* list)
255 FreeListItem* next;
257 assert(list != NULL);
259 while (list->head != NULL) {
260 /* Remove one item from the head of the list */
261 next = list->head->next;
262 free(list->head->data);
263 free(list->head);
264 list->head = next;
267 free(list);
271 int get_image_width( image_id id) {
272 return images[id].data->w;
275 int get_image_height( image_id id) {
276 return images[id].data->h;
279 int get_sprite_width( image_id id, int items) {
280 return images[id].data->w / items;
283 void show_sprite( image_id id, int pos, int items, int x_offset, int y_offset, int opacity) {
284 SDL_Surface *bitmap;
285 SDL_Rect src, dst;
287 bitmap = images[id].data;
289 if( !bitmap) return;
291 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
293 dst.w = src.w = bitmap->w/items;
294 dst.h = src.h = bitmap->h;
295 src.x = src.w*pos;
296 src.y = 0;
297 dst.x = x_offset;
298 dst.y = y_offset;
300 SDL_BlitSurface( bitmap, &src, screen, &dst);
301 update_rect2(dst);
304 void show_image_rotozoom(image_id id, int x, int y, float rotate, float zoom)
306 SDL_Surface* tmp;
307 SDL_Rect src, dst;
309 assert(id < GR_COUNT);
311 tmp = rotozoomSurface(images[id].data, rotate, zoom, SMOOTHING_OFF);
313 dst.w = src.w = tmp->w;
314 dst.h = src.h = tmp->h;
315 src.x = 0;
316 src.y = 0;
317 dst.x = x - src.w/2;
318 dst.y = y - src.h/2;
320 SDL_BlitSurface(tmp, &src, screen, &dst);
321 update_rect2(dst);
322 SDL_FreeSurface(tmp);
325 void fill_image_offset( image_id id, int x, int y, int w, int h, int offset_x, int offset_y) {
326 SDL_Surface *bitmap;
327 SDL_Rect src, dst;
328 int dx = 0, dy = 0, cx, cy;
330 if( id >= GR_COUNT) return;
332 bitmap = images[id].data;
334 /* Make negative offsets positive */
335 if (offset_x < 0) {
336 offset_x = (offset_x%bitmap->w)+bitmap->w;
338 if (offset_y < 0) {
339 offset_y = (offset_y%bitmap->h)+bitmap->h;
342 src.y = offset_y % bitmap->h;
343 while( dy < h) {
344 src.h = dst.h = cy = (h-dy > bitmap->h-src.y)?(bitmap->h-src.y):(h-dy);
345 dst.y = y+dy;
347 dx = 0;
348 src.x = offset_x % bitmap->w;
349 while( dx < w) {
350 src.w = dst.w = cx = (w-dx > bitmap->w-src.x)?(bitmap->w-src.x):(w-dx);
351 dst.x = x+dx;
353 SDL_BlitSurface( bitmap, &src, screen, &dst);
354 update_rect2(dst);
356 dx += cx;
357 src.x = 0;
360 dy += cy;
361 src.y = 0;
365 void line_horiz( int y, Uint8 r, Uint8 g, Uint8 b) {
366 rectangle(0, y, screen->w, 1, r, g, b);
369 void line_vert( int x, Uint8 r, Uint8 g, Uint8 b) {
370 rectangle(x, 0, 1, screen->h, r, g, b);
373 void rectangle( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b) {
374 Uint32 color = SDL_MapRGB( screen->format, r, g, b);
375 SDL_Rect rect;
377 rect.x = x;
378 rect.y = y;
379 rect.w = w;
380 rect.h = h;
382 SDL_FillRect( screen, &rect, color);
383 update_rect(x, y, w, h);
386 void rectangle_alpha(int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, Uint8 opacity)
388 Uint32 color = SDL_MapRGB(screen->format, r, g, b);
389 SDL_Surface *buf;
390 SDL_Rect rect, rect2;
392 buf = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
393 screen->format->BitsPerPixel,
394 screen->format->Rmask,
395 screen->format->Gmask,
396 screen->format->Bmask,
397 screen->format->Amask);
399 rect.x = rect.y = 0;
400 rect2.x = x;
401 rect2.y = y;
402 rect.w = rect2.w = w;
403 rect.h = rect2.h = h;
405 SDL_FillRect(buf, &rect, color);
406 SDL_SetAlpha(buf, SDL_RLEACCEL | SDL_SRCALPHA, opacity);
407 SDL_BlitSurface(buf, &rect, screen, &rect2);
408 update_rect(x, y, w, h);
410 SDL_FreeSurface(buf);
413 void draw_button( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
414 float diff = (pressed?1.0-BUTTON_HIGHLIGHT:1.0+BUTTON_HIGHLIGHT);
415 rectangle(x, y, w, h, MIN(r*diff, 255), MIN(g*diff, 255), MIN(b*diff, 255));
416 rectangle(x+BUTTON_BORDER, y+BUTTON_BORDER, w-BUTTON_BORDER, h-BUTTON_BORDER, MIN(r/diff, 255), MIN(g/diff, 255), MIN(b/diff, 255));
417 rectangle(x+BUTTON_BORDER, y+BUTTON_BORDER, w-2*BUTTON_BORDER, h-2*BUTTON_BORDER, r, g, b);
420 void draw_button_text(const char* s, int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
421 int font_x, font_y;
422 float factor = 0.3;
423 pressed = pressed?1:0;
424 draw_button( x, y, w, h, r, g, b, pressed);
425 font_x = x + w/2 - font_get_string_width(FONT_SMALL, s)/2 + pressed*BUTTON_BORDER;
426 font_y = y + h/2 - font_get_height(FONT_SMALL)/2 + pressed*BUTTON_BORDER;
427 font_draw_string_color(FONT_SMALL, s, font_x, font_y, (Uint8)(r*factor), (Uint8)(g*factor), (Uint8)(b*factor));
430 void clear_screen()
432 SDL_Rect rect;
434 rect.x = rect.y = 0;
435 rect.w = WIDTH;
436 rect.h = HEIGHT;
438 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
441 void store_screen()
443 SDL_BlitSurface(screen, NULL, background, NULL);
444 rect_update_old[0].x = rect_update_old[0].y = 0;
445 rect_update_old[0].w = WIDTH;
446 rect_update_old[0].h = HEIGHT;
447 rect_update_old_count = 1;
448 rect_update_cache_current = 0;
451 void reset_screen()
453 int i;
454 SDL_Rect* tmp;
455 SDL_BlitSurface(background, NULL, screen, NULL);
457 for (i=0; i<rect_update_cache_current; i++) {
458 SDL_BlitSurface(background, &rect_update_cache[i], screen, &rect_update_cache[i]);
461 /* Save rects I've just updated for redraw later */
462 tmp = rect_update_cache;
463 rect_update_cache = rect_update_old;
464 rect_update_old = tmp;
465 rect_update_old_count = rect_update_cache_current;
466 rect_update_cache_current = 0;
469 void update_rect(Sint32 x, Sint32 y, Sint32 w, Sint32 h)
471 SDL_Rect* u = NULL;
472 static int inside = 0;
473 if (inside) return;
474 inside = 1;
476 #ifdef DRAW_UPDATE_RECTANGLE
477 rectangle(x, y, w, h, 50+rand()%200, 50+rand()%200 ,50+rand()%200);
478 #endif
480 if ((x >= WIDTH) | (y >= HEIGHT) | (x+w <= 0) | (y+h <= 0) | (!w) | (!h)) {
481 inside = 0;
482 return;
485 if (rect_update_cache_current == RECT_UPDATE_CACHE) {
486 fprintf(stderr, "Overflow\n");
487 rect_update_cache_current = 0;
490 u = &(rect_update_cache[rect_update_cache_current]);
492 if (x < 0/* && x+w > 0*/) {
493 w += x;
494 x = 0;
496 if (y < 0/* && y+h > 0*/) {
497 h += y;
498 y = 0;
501 if (x+w >= WIDTH) {
502 w -= (x+w-WIDTH+1);
505 if (y+h >= HEIGHT) {
506 h -= (y+h-HEIGHT+1);
509 if (w==0 || h==0) {
510 inside = 0;
511 return;
514 u->x = x;
515 u->y = y;
516 u->w = w;
517 u->h = h;
519 rect_update_cache_current++;
521 inside = 0;
524 void updatescr()
526 Uint32 ticks = SDL_GetTicks();
527 SDL_Rect *r_current = NULL, *end_current = NULL;
528 SDL_Rect *r_old = NULL;
530 static int fading_last_time = 0;
531 unsigned char fading_now = ticks < fading_start+FADE_DURATION;
533 if (fading_now) {
534 SDL_SetAlpha(buffer, SDL_SRCALPHA | SDL_RLEACCEL, 255-255*(ticks-fading_start)/FADE_DURATION);
535 SDL_BlitSurface(buffer, NULL, screen, NULL);
536 SDL_UpdateRect(screen, 0, 0, 0, 0);
537 } else if (fading_last_time && !fading_now) {
538 SDL_UpdateRect(screen, 0, 0, 0, 0);
539 } else {
540 if (rect_update_old_count == rect_update_cache_current) {
541 /* Merge rects into one single rect list */
542 r_old = rect_update_old;
543 r_current = rect_update_cache;
544 end_current = rect_update_cache + rect_update_cache_current;
545 while (r_current != end_current) {
546 r_old->w = MAX(r_current->x+r_current->w, r_old->x+r_old->w);
547 r_old->h = MAX(r_current->y+r_current->h, r_old->y+r_old->h);
548 r_old->x = MIN(r_current->x, r_old->x);
549 r_old->y = MIN(r_current->y, r_old->y);
550 r_old->w -= r_old->x;
551 r_old->h -= r_old->y;
553 r_current++;
554 r_old++;
556 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
557 } else {
558 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
559 SDL_UpdateRects(screen, rect_update_cache_current, rect_update_cache);
563 reset_screen();
564 fading_last_time = fading_now;
567 void start_fade() {
568 SDL_BlitSurface( screen, NULL, buffer, NULL);
569 fading_start = SDL_GetTicks();
572 void font_draw_string_color(font_id id, const char* s, int x_offset, int y_offset, Uint8 r, Uint8 g, Uint8 b) {
573 SDL_Surface *text;
574 SDL_Rect rect, rect2;
575 SDL_Color color;
577 color.r = r;
578 color.g = g;
579 color.b = b;
581 assert(id < FONT_COUNT);
583 text = TTF_RenderText_Blended(fonts[id].data, s, color);
584 if (text != NULL) {
585 rect.w = rect2.w = text->w;
586 rect.h = rect2.h = text->h;
587 rect.x = rect.y = 0;
588 rect2.x = x_offset;
589 rect2.y = y_offset;
590 SDL_SetAlpha(text, SDL_SRCALPHA | SDL_RLEACCEL, 255);
591 SDL_BlitSurface(text, &rect, screen, &rect2);
592 SDL_FreeSurface(text);
595 update_rect(x_offset, y_offset, rect.w, rect.h);
598 int font_get_string_width(font_id id, const char* s) {
599 int w, h;
601 assert(id < FONT_COUNT);
603 if (TTF_SizeText(fonts[id].data, s, &w, &h) != 0) {
604 return 0;
607 return w;
610 int font_get_height(font_id id) {
611 assert(id < FONT_COUNT);
612 return TTF_FontHeight(fonts[id].data);
615 void draw_line_faded( int x1, int y1, int x2, int y2, int r, int g, int b, int r2, int g2, int b2) {
616 float step, dx, dy, x = x1, y = y1;
617 int i;
618 char fade = (r!=r2 || g!=g2 || b!=b2);
620 step = (float)(abs(x2-x1)>abs(y2-y1)?abs(x2-x1):abs(y2-y1));
621 dx = (float)(x2-x1) / step;
622 dy = (float)(y2-y1) / step;
624 SDL_LockSurface( screen);
625 for( i=0; i<step; i++) {
626 x += dx;
627 y += dy;
628 if( x < 0.0 || x >= WIDTH || y < 0.0 || y >= HEIGHT) {
629 continue;
631 if( fade) {
632 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);
633 } else {
634 SET_PIXEL_RGB( screen, (int)x, (int)y, r, g, b);
637 SDL_UnlockSurface( screen);