Tennix 1.1 "Classic Championship Tour 2011" released
[tennix.git] / graphics.cc
blob49937a847cdb9e8e25c7b99a17ee15ecc1d76099
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.hh"
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", 15, 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 "input_gamepad.png",
90 "input_keyboard_arrows.png",
91 "input_keyboard_ol.png",
92 "input_keyboard_ws.png",
93 "input_maemo_dpad.png",
94 "input_mouse.png",
95 "input_touchscreen.png",
96 "input_ai.png",
97 "back.png",
98 "forward.png",
99 "loc_margaret_court_arena.png",
100 "loc_stade_roland_garros.png",
101 "loc_court_no_1.png",
102 "loc_arthur_ashe_stadium.png",
103 #ifdef NONFREE_LOCATIONS
104 "loc_training_camp.png",
105 "loc_austrian_open.png",
106 "loc_olympic_green_tennis.png"
107 #endif
110 void init_graphics(TennixArchive& tnxar) {
111 int i;
112 char *d;
113 SDL_Surface* data;
114 SDL_Surface* tmp;
115 struct SDL_RWops* rw;
116 const char* font_data = NULL;
118 if (TTF_Init() == -1) {
119 fprintf(stderr, "Cannot init TTF: %s\n", TTF_GetError());
120 exit(EXIT_FAILURE);
123 rect_update_cache = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
124 rect_update_old = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
125 rect_update_cache_current = 0;
126 rect_update_old_count = 0;
128 images = (Image*)calloc( GR_COUNT, sizeof( Image));
129 fonts = (Font*)calloc(FONT_COUNT, sizeof(Font));
131 buffer = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
132 screen->format->BitsPerPixel,
133 screen->format->Rmask,
134 screen->format->Gmask,
135 screen->format->Bmask,
136 screen->format->Amask);
138 background = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
139 screen->format->BitsPerPixel,
140 screen->format->Rmask,
141 screen->format->Gmask,
142 screen->format->Bmask,
143 screen->format->Amask);
145 if( buffer == NULL) {
146 fprintf( stderr, "Cannot create buffer surface: %s\n", SDL_GetError());
149 font_freelist = freelist_create();
151 /* Load fonts from resource file */
152 for (i=0; i<FONT_COUNT; i++) {
153 if (font_desc[i].filename != NULL) {
154 assert(tnxar.setItemFilename(font_desc[i].filename) != 0);
155 font_data = tnxar.getItemBytes();
156 freelist_append(font_freelist, font_data);
158 assert(font_data != NULL);
159 rw = SDL_RWFromMem((void*)font_data, tnxar.getItemSize());
160 fonts[i].data = TTF_OpenFontRW(rw, 1, font_desc[i].size);
161 if (fonts[i].data == NULL) {
162 fprintf(stderr, "TTF error: %s\n", TTF_GetError());
163 assert(fonts[i].data != NULL);
165 TTF_SetFontStyle(fonts[i].data, font_desc[i].style);
168 draw_button(40, (HEIGHT-40)/2, WIDTH-80, 40, 100, 100, 100, 1);
169 store_screen();
170 updatescr();
172 for( i=0; i<GR_COUNT; i++) {
173 if (tnxar.setItemFilename(graphics[i]) != 0 ) {
174 d = tnxar.getItemBytes();
175 tmp = IMG_Load_RW(SDL_RWFromMem(d, tnxar.getItemSize()), 1);
176 free(d);
177 } else {
178 fprintf(stderr, "Cannot find file: %s\n", graphics[i]);
179 exit(EXIT_FAILURE);
181 if( !tmp) {
182 fprintf( stderr, "Error: %s\n", SDL_GetError());
183 continue;
186 /* Convert to RGBA in the display format */
187 data = SDL_DisplayFormatAlpha(tmp);
188 SDL_FreeSurface(tmp);
190 if( !data) {
191 fprintf( stderr, "Error: %s\n", SDL_GetError());
192 continue;
194 images[i].data = data;
196 draw_button(40, (HEIGHT-40)/2, (WIDTH-80)*i/(GR_COUNT+SOUND_MAX), 40, 100, 250, 100, 0);
197 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);
198 updatescr();
202 void uninit_graphics() {
203 int i;
205 for( i=0; i<GR_COUNT; i++) {
206 SDL_FreeSurface( images[i].data);
209 if( buffer != NULL) {
210 SDL_FreeSurface( buffer);
213 if (background != NULL) {
214 SDL_FreeSurface(background);
217 free(rect_update_cache);
218 free(rect_update_old);
219 free(images);
221 for (i=0; i<FONT_COUNT; i++) {
222 TTF_CloseFont(fonts[i].data);
223 fonts[i].data = NULL;
225 free(fonts);
227 freelist_free_all(font_freelist);
229 TTF_Quit();
233 FreeList* freelist_create()
235 FreeList* list = (FreeList*)malloc(sizeof(FreeList));
236 assert(list != NULL);
238 list->head = NULL;
240 return list;
243 void freelist_append(FreeList* list, const char* data)
245 FreeListItem* new_item = (FreeListItem*)malloc(sizeof(FreeListItem));
247 assert(list != NULL);
248 assert(data != NULL);
250 new_item->data = data;
252 /* Insert item at the beginning of list */
253 new_item->next = list->head;
254 list->head = new_item;
257 void freelist_free_all(FreeList* list)
259 FreeListItem* next;
261 assert(list != NULL);
263 while (list->head != NULL) {
264 /* Remove one item from the head of the list */
265 next = list->head->next;
266 free((void*)list->head->data);
267 free(list->head);
268 list->head = next;
271 free(list);
275 int get_image_width( image_id id) {
276 return images[id].data->w;
279 int get_image_height( image_id id) {
280 return images[id].data->h;
283 int get_sprite_width( image_id id, int items) {
284 return images[id].data->w / items;
287 void show_sprite( image_id id, int pos, int items, int x_offset,
288 int y_offset, int opacity)
290 assert(id < GR_COUNT);
291 SDL_SetAlpha(images[id].data, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
292 blit_surface(images[id].data, x_offset, y_offset, pos, items);
295 void show_image_rotozoom(image_id id, int x, int y, float rotate, float zoom)
297 SDL_Surface* tmp;
298 SDL_Rect src, dst;
300 assert(id < GR_COUNT);
302 tmp = rotozoomSurface(images[id].data, rotate, zoom, SMOOTHING_OFF);
304 dst.w = src.w = tmp->w;
305 dst.h = src.h = tmp->h;
306 src.x = 0;
307 src.y = 0;
308 dst.x = x - src.w/2;
309 dst.y = y - src.h/2;
311 SDL_BlitSurface(tmp, &src, screen, &dst);
312 update_rect2(dst);
313 SDL_FreeSurface(tmp);
316 void fill_image_offset( image_id id, int x, int y, int w, int h, int offset_x, int offset_y) {
317 SDL_Surface *bitmap;
318 SDL_Rect src, dst;
319 int dx = 0, dy = 0, cx, cy;
321 if( id >= GR_COUNT) return;
323 bitmap = images[id].data;
325 /* Make negative offsets positive */
326 if (offset_x < 0) {
327 offset_x = (offset_x%bitmap->w)+bitmap->w;
329 if (offset_y < 0) {
330 offset_y = (offset_y%bitmap->h)+bitmap->h;
333 src.y = offset_y % bitmap->h;
334 while( dy < h) {
335 src.h = dst.h = cy = (h-dy > bitmap->h-src.y)?(bitmap->h-src.y):(h-dy);
336 dst.y = y+dy;
338 dx = 0;
339 src.x = offset_x % bitmap->w;
340 while( dx < w) {
341 src.w = dst.w = cx = (w-dx > bitmap->w-src.x)?(bitmap->w-src.x):(w-dx);
342 dst.x = x+dx;
344 SDL_BlitSurface( bitmap, &src, screen, &dst);
345 update_rect2(dst);
347 dx += cx;
348 src.x = 0;
351 dy += cy;
352 src.y = 0;
356 void line_horiz( int y, Uint8 r, Uint8 g, Uint8 b) {
357 rectangle(0, y, screen->w, 1, r, g, b);
360 void line_vert( int x, Uint8 r, Uint8 g, Uint8 b) {
361 rectangle(x, 0, 1, screen->h, r, g, b);
364 void rectangle( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b) {
365 Uint32 color;
366 SDL_Rect rect;
368 if (w <= 0 || h <= 0 || x < 0 || y < 0 || x+w > WIDTH || y+h > HEIGHT) {
369 return;
372 color = SDL_MapRGB(screen->format, r, g, b);
374 rect.x = x;
375 rect.y = y;
376 rect.w = w;
377 rect.h = h;
379 SDL_FillRect( screen, &rect, color);
380 update_rect(x, y, w, h);
383 void rectangle_alpha(int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, Uint8 opacity)
385 Uint32 color;
386 SDL_Surface *buf;
387 SDL_Rect rect, rect2;
389 if (w <= 0 || h <= 0 || x < 0 || y < 0 || x+w > WIDTH || y+h > HEIGHT) {
390 return;
393 color = SDL_MapRGB(screen->format, r, g, b);
395 buf = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
396 screen->format->BitsPerPixel,
397 screen->format->Rmask,
398 screen->format->Gmask,
399 screen->format->Bmask,
400 screen->format->Amask);
402 rect.x = rect.y = 0;
403 rect2.x = x;
404 rect2.y = y;
405 rect.w = rect2.w = w;
406 rect.h = rect2.h = h;
408 SDL_FillRect(buf, &rect, color);
409 SDL_SetAlpha(buf, SDL_RLEACCEL | SDL_SRCALPHA, opacity);
410 SDL_BlitSurface(buf, &rect, screen, &rect2);
411 update_rect(x, y, w, h);
413 SDL_FreeSurface(buf);
416 void draw_button( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
417 float diff = (pressed?1.0-BUTTON_HIGHLIGHT:1.0+BUTTON_HIGHLIGHT);
418 rectangle(x, y, w, h, MIN(r*diff, 255), MIN(g*diff, 255), MIN(b*diff, 255));
419 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));
420 rectangle(x+BUTTON_BORDER, y+BUTTON_BORDER, w-2*BUTTON_BORDER, h-2*BUTTON_BORDER, r, g, b);
423 void draw_button_text(const char* s, int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
424 int font_x, font_y;
425 float factor = 0.3;
426 pressed = pressed?1:0;
427 draw_button( x, y, w, h, r, g, b, pressed);
428 font_x = x + w/2 - font_get_string_width(FONT_SMALL, s)/2 + pressed*BUTTON_BORDER;
429 font_y = y + h/2 - font_get_height(FONT_SMALL)/2 + pressed*BUTTON_BORDER;
430 font_draw_string_color(FONT_SMALL, s, font_x, font_y, (Uint8)(r*factor), (Uint8)(g*factor), (Uint8)(b*factor));
433 void draw_button_object(MenuButton* b, int mx, int my)
435 if (b->image_id == GR_COUNT) {
436 draw_button_text(b->text, b->x, b->y, b->w, b->h, b->r, b->g, b->b, M_POS_BUTTON(*b, mx, my));
437 } else {
438 show_image_rotozoom(b->image_id, b->x+b->w/2, b->y+b->h/2, 0.0, 1.0-0.2*!M_POS_BUTTON(*b, mx, my));
442 void clear_screen()
444 SDL_Rect rect;
446 rect.x = rect.y = 0;
447 rect.w = WIDTH;
448 rect.h = HEIGHT;
450 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
453 void store_screen()
455 SDL_BlitSurface(screen, NULL, background, NULL);
456 rect_update_old[0].x = rect_update_old[0].y = 0;
457 rect_update_old[0].w = WIDTH;
458 rect_update_old[0].h = HEIGHT;
459 rect_update_old_count = 1;
460 rect_update_cache_current = 0;
463 void reset_screen()
465 int i;
466 SDL_Rect* tmp;
467 SDL_BlitSurface(background, NULL, screen, NULL);
469 for (i=0; i<rect_update_cache_current; i++) {
470 SDL_BlitSurface(background, &rect_update_cache[i], screen, &rect_update_cache[i]);
473 /* Save rects I've just updated for redraw later */
474 tmp = rect_update_cache;
475 rect_update_cache = rect_update_old;
476 rect_update_old = tmp;
477 rect_update_old_count = rect_update_cache_current;
478 rect_update_cache_current = 0;
481 void update_rect(Sint32 x, Sint32 y, Sint32 w, Sint32 h)
483 SDL_Rect* u = NULL;
484 static int inside = 0;
485 if (inside) return;
486 inside = 1;
488 #ifdef DRAW_UPDATE_RECTANGLE
489 rectangle(x, y, w, h, 50+rand()%200, 50+rand()%200 ,50+rand()%200);
490 #endif
492 if ((x >= WIDTH) | (y >= HEIGHT) | (x+w <= 0) | (y+h <= 0) | (!w) | (!h)) {
493 inside = 0;
494 return;
497 if (rect_update_cache_current == RECT_UPDATE_CACHE) {
498 fprintf(stderr, "Overflow\n");
499 rect_update_cache_current = 0;
502 u = &(rect_update_cache[rect_update_cache_current]);
504 if (x < 0/* && x+w > 0*/) {
505 w += x;
506 x = 0;
508 if (y < 0/* && y+h > 0*/) {
509 h += y;
510 y = 0;
513 if (x+w >= WIDTH) {
514 w -= (x+w-WIDTH+1);
517 if (y+h >= HEIGHT) {
518 h -= (y+h-HEIGHT+1);
521 if (w==0 || h==0) {
522 inside = 0;
523 return;
526 u->x = x;
527 u->y = y;
528 u->w = w;
529 u->h = h;
531 rect_update_cache_current++;
533 inside = 0;
536 void updatescr()
538 Uint32 ticks = SDL_GetTicks();
539 SDL_Rect *r_current = NULL, *end_current = NULL;
540 SDL_Rect *r_old = NULL;
542 static int fading_last_time = 0;
543 unsigned char fading_now = ticks < fading_start+FADE_DURATION;
545 if (fading_now) {
546 SDL_SetAlpha(buffer, SDL_SRCALPHA | SDL_RLEACCEL, 255-255*(ticks-fading_start)/FADE_DURATION);
547 SDL_BlitSurface(buffer, NULL, screen, NULL);
548 SDL_UpdateRect(screen, 0, 0, 0, 0);
549 } else if (fading_last_time && !fading_now) {
550 SDL_UpdateRect(screen, 0, 0, 0, 0);
551 } else {
552 if (rect_update_old_count == rect_update_cache_current) {
553 /* Merge rects into one single rect list */
554 r_old = rect_update_old;
555 r_current = rect_update_cache;
556 end_current = rect_update_cache + rect_update_cache_current;
557 while (r_current != end_current) {
558 r_old->w = MAX(r_current->x+r_current->w, r_old->x+r_old->w);
559 r_old->h = MAX(r_current->y+r_current->h, r_old->y+r_old->h);
560 r_old->x = MIN(r_current->x, r_old->x);
561 r_old->y = MIN(r_current->y, r_old->y);
562 r_old->w -= r_old->x;
563 r_old->h -= r_old->y;
565 r_current++;
566 r_old++;
568 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
569 } else {
570 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
571 SDL_UpdateRects(screen, rect_update_cache_current, rect_update_cache);
575 reset_screen();
576 fading_last_time = fading_now;
579 void start_fade() {
580 SDL_BlitSurface( screen, NULL, buffer, NULL);
581 fading_start = SDL_GetTicks();
584 SDL_Surface* font_render_surface(font_id id, const char* text, Uint8 r,
585 Uint8 g, Uint8 b)
587 SDL_Surface *result;
588 SDL_Color color;
590 color.r = r;
591 color.g = g;
592 color.b = b;
594 assert(id < FONT_COUNT);
596 result = TTF_RenderText_Blended(fonts[id].data, text, color);
598 assert(result != NULL);
600 return result;
603 void blit_surface(SDL_Surface* surface, int x, int y, int pos, int count)
605 SDL_Rect src, dst;
607 dst.w = src.w = surface->w/count;
608 dst.h = src.h = surface->h;
609 src.x = src.w*pos;
610 src.y = 0;
611 dst.x = x;
612 dst.y = y;
614 SDL_BlitSurface(surface, &src, screen, &dst);
615 update_rect2(dst);
618 SDL_Surface* get_surface(image_id id)
620 assert(id < GR_COUNT);
621 return images[id].data;
624 void font_draw_string_color(font_id id, const char* s, int x_offset, int y_offset, Uint8 r, Uint8 g, Uint8 b) {
625 SDL_Surface *text;
627 text = font_render_surface(id, s, r, g, b);
628 blit_surface_simple(text, x_offset, y_offset);
629 SDL_FreeSurface(text);
632 int font_get_string_width(font_id id, const char* s) {
633 int w, h;
635 assert(id < FONT_COUNT);
637 if (TTF_SizeText(fonts[id].data, s, &w, &h) != 0) {
638 return 0;
641 return w;
644 int font_get_height(font_id id) {
645 assert(id < FONT_COUNT);
646 return TTF_FontHeight(fonts[id].data);
649 void draw_line_faded( int x1, int y1, int x2, int y2, int r, int g, int b, int r2, int g2, int b2) {
650 float step, dx, dy, x = x1, y = y1;
651 int i;
652 char fade = (r!=r2 || g!=g2 || b!=b2);
654 step = (float)(abs(x2-x1)>abs(y2-y1)?abs(x2-x1):abs(y2-y1));
655 dx = (float)(x2-x1) / step;
656 dy = (float)(y2-y1) / step;
658 SDL_LockSurface( screen);
659 for( i=0; i<step; i++) {
660 x += dx;
661 y += dy;
662 if( x < 0.0 || x >= WIDTH || y < 0.0 || y >= HEIGHT) {
663 continue;
665 if( fade) {
666 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);
667 } else {
668 SET_PIXEL_RGB( screen, (int)x, (int)y, r, g, b);
671 SDL_UnlockSurface( screen);