Enable all warnings
[tennix.git] / src / graphics.cc
blob9789600ec49a8a5a41d37784a936473c97189bae
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 <ctype.h>
29 #include <string.h>
31 #include "tennix.h"
32 #include "graphics.h"
33 #include "archive.h"
34 #include "sound.h"
36 #include "SDL_rotozoom.h"
38 static Image* images;
40 static Font* fonts;
41 FreeList* font_freelist = NULL;
43 static FontDescription font_desc[] = {
44 /**
45 * This is the list of fonts to be loaded. Be sure to
46 * use NULL as filename when using the same .ttf file
47 * the second time in a row - this saves memory in the
48 * loader function.
49 **/
50 { "dustismo.ttf", 15, TTF_STYLE_NORMAL },
51 { NULL, 20, TTF_STYLE_NORMAL },
52 { "itwasntme.ttf", 22, TTF_STYLE_NORMAL },
53 { NULL, 28, TTF_STYLE_NORMAL }
56 static SDL_Surface *buffer;
57 static SDL_Surface *background;
59 static SDL_Rect *rect_update_cache;
60 static SDL_Rect *rect_update_old;
61 static int rect_update_cache_current;
62 static int rect_update_old_count;
64 Uint32 fading_start = 0;
66 static const char* graphics[] = {
67 "court.png",
68 "shadow.png",
69 "player-racket.png",
70 "ball.png",
71 "referee.png",
72 "ctt_hard.png",
73 "ctt_clay.png",
74 "ctt_grass.png",
75 "sidebar.png",
76 "tennixlogo.png",
77 "btnplay.png",
78 "btnresume.png",
79 "btnquit.png",
80 "stadium.png",
81 "fog.png",
82 "fog2.png",
83 "night.png",
84 "talk.png",
85 "cursor.png",
86 "worldmap.png",
87 "input_gamepad.png",
88 "input_keyboard_arrows.png",
89 "input_keyboard_ol.png",
90 "input_keyboard_ws.png",
91 "input_maemo_dpad.png",
92 "input_mouse.png",
93 "input_touchscreen.png",
94 "input_ai.png",
95 "back.png",
96 "forward.png",
97 "loc_margaret_court_arena.png",
98 "loc_stade_roland_garros.png",
99 "loc_court_no_1.png",
100 "loc_arthur_ashe_stadium.png",
101 #ifdef NONFREE_LOCATIONS
102 "loc_training_camp.png",
103 "loc_austrian_open.png",
104 "loc_olympic_green_tennis.png"
105 #endif
108 void init_graphics(TennixArchive& tnxar) {
109 int i;
110 char *d;
111 SDL_Surface* data;
112 SDL_Surface* tmp;
113 struct SDL_RWops* rw;
114 const char* font_data = NULL;
116 if (TTF_Init() == -1) {
117 fprintf(stderr, "Cannot init TTF: %s\n", TTF_GetError());
118 exit(EXIT_FAILURE);
121 rect_update_cache = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
122 rect_update_old = (SDL_Rect*)calloc(RECT_UPDATE_CACHE, sizeof(SDL_Rect));
123 rect_update_cache_current = 0;
124 rect_update_old_count = 0;
126 images = (Image*)calloc( GR_COUNT, sizeof( Image));
127 fonts = (Font*)calloc(FONT_COUNT, sizeof(Font));
129 buffer = 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 background = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT,
137 screen->format->BitsPerPixel,
138 screen->format->Rmask,
139 screen->format->Gmask,
140 screen->format->Bmask,
141 screen->format->Amask);
143 if( buffer == NULL) {
144 fprintf( stderr, "Cannot create buffer surface: %s\n", SDL_GetError());
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 tnx_assert(tnxar.setItemFilename(font_desc[i].filename) != 0);
153 font_data = tnxar.getItemBytes();
154 freelist_append(font_freelist, font_data);
156 tnx_assert(font_data != NULL);
157 rw = SDL_RWFromMem((void*)font_data, tnxar.getItemSize());
158 fonts[i].data = TTF_OpenFontRW(rw, 1, font_desc[i].size);
159 if (fonts[i].data == NULL) {
160 fprintf(stderr, "TTF error: %s\n", TTF_GetError());
161 tnx_assert(fonts[i].data != NULL);
163 TTF_SetFontStyle(fonts[i].data, font_desc[i].style);
166 draw_button(40, (HEIGHT-40)/2, WIDTH-80, 40, 100, 100, 100, 1);
167 store_screen();
168 updatescr();
170 for( i=0; i<GR_COUNT; i++) {
171 if (tnxar.setItemFilename(graphics[i]) != 0 ) {
172 d = tnxar.getItemBytes();
173 tmp = IMG_Load_RW(SDL_RWFromMem(d, tnxar.getItemSize()), 1);
174 free(d);
175 } else {
176 fprintf(stderr, "Cannot find file: %s\n", graphics[i]);
177 exit(EXIT_FAILURE);
179 if( !tmp) {
180 fprintf( stderr, "Error: %s\n", SDL_GetError());
181 continue;
184 /* Convert to RGBA in the display format */
185 data = SDL_DisplayFormatAlpha(tmp);
186 SDL_FreeSurface(tmp);
188 if( !data) {
189 fprintf( stderr, "Error: %s\n", SDL_GetError());
190 continue;
192 images[i].data = data;
194 draw_button(40, (HEIGHT-40)/2, (WIDTH-80)*i/(GR_COUNT+SOUND_MAX), 40, 100, 250, 100, 0);
195 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);
196 updatescr();
200 void uninit_graphics() {
201 int i;
203 for( i=0; i<GR_COUNT; i++) {
204 SDL_FreeSurface( images[i].data);
207 if( buffer != NULL) {
208 SDL_FreeSurface( buffer);
211 if (background != NULL) {
212 SDL_FreeSurface(background);
215 free(rect_update_cache);
216 free(rect_update_old);
217 free(images);
219 for (i=0; i<FONT_COUNT; i++) {
220 TTF_CloseFont(fonts[i].data);
221 fonts[i].data = NULL;
223 free(fonts);
225 freelist_free_all(font_freelist);
227 TTF_Quit();
231 FreeList* freelist_create()
233 FreeList* list = (FreeList*)malloc(sizeof(FreeList));
234 tnx_assert(list != NULL);
236 list->head = NULL;
238 return list;
241 void freelist_append(FreeList* list, const char* data)
243 FreeListItem* new_item = (FreeListItem*)malloc(sizeof(FreeListItem));
245 tnx_assert(list != NULL);
246 tnx_assert(data != NULL);
248 new_item->data = data;
250 /* Insert item at the beginning of list */
251 new_item->next = list->head;
252 list->head = new_item;
255 void freelist_free_all(FreeList* list)
257 FreeListItem* next;
259 tnx_assert(list != NULL);
261 while (list->head != NULL) {
262 /* Remove one item from the head of the list */
263 next = list->head->next;
264 free((void*)list->head->data);
265 free(list->head);
266 list->head = next;
269 free(list);
273 int get_image_width( image_id id) {
274 return images[id].data->w;
277 int get_image_height( image_id id) {
278 return images[id].data->h;
281 int get_sprite_width( image_id id, int items) {
282 return images[id].data->w / items;
285 void show_sprite( image_id id, int pos, int items, int x_offset,
286 int y_offset, int opacity)
288 tnx_assert(id < GR_COUNT);
289 SDL_SetAlpha(images[id].data, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
290 blit_surface(images[id].data, x_offset, y_offset, pos, items);
293 void show_image_rotozoom(image_id id, int x, int y, float rotate, float zoom)
295 SDL_Surface* tmp;
296 SDL_Rect src, dst;
298 tnx_assert(id < GR_COUNT);
300 tmp = rotozoomSurface(images[id].data, rotate, zoom, SMOOTHING_OFF);
302 dst.w = src.w = tmp->w;
303 dst.h = src.h = tmp->h;
304 src.x = 0;
305 src.y = 0;
306 dst.x = x - src.w/2;
307 dst.y = y - src.h/2;
309 SDL_BlitSurface(tmp, &src, screen, &dst);
310 update_rect2(dst);
311 SDL_FreeSurface(tmp);
314 void fill_image_offset( image_id id, int x, int y, int w, int h, int offset_x, int offset_y) {
315 SDL_Surface *bitmap;
316 SDL_Rect src, dst;
317 int dx = 0, dy = 0, cx, cy;
319 if( id >= GR_COUNT) return;
321 bitmap = images[id].data;
323 /* Make negative offsets positive */
324 if (offset_x < 0) {
325 offset_x = (offset_x%bitmap->w)+bitmap->w;
327 if (offset_y < 0) {
328 offset_y = (offset_y%bitmap->h)+bitmap->h;
331 src.y = offset_y % bitmap->h;
332 while( dy < h) {
333 src.h = dst.h = cy = (h-dy > bitmap->h-src.y)?(bitmap->h-src.y):(h-dy);
334 dst.y = y+dy;
336 dx = 0;
337 src.x = offset_x % bitmap->w;
338 while( dx < w) {
339 src.w = dst.w = cx = (w-dx > bitmap->w-src.x)?(bitmap->w-src.x):(w-dx);
340 dst.x = x+dx;
342 SDL_BlitSurface( bitmap, &src, screen, &dst);
343 update_rect2(dst);
345 dx += cx;
346 src.x = 0;
349 dy += cy;
350 src.y = 0;
354 void line_horiz( int y, Uint8 r, Uint8 g, Uint8 b) {
355 rectangle(0, y, screen->w, 1, r, g, b);
358 void line_vert( int x, Uint8 r, Uint8 g, Uint8 b) {
359 rectangle(x, 0, 1, screen->h, r, g, b);
362 void rectangle( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b) {
363 Uint32 color;
364 SDL_Rect rect;
366 if (w <= 0 || h <= 0 || x < 0 || y < 0 || x+w > WIDTH || y+h > HEIGHT) {
367 return;
370 color = SDL_MapRGB(screen->format, r, g, b);
372 rect.x = x;
373 rect.y = y;
374 rect.w = w;
375 rect.h = h;
377 SDL_FillRect( screen, &rect, color);
378 update_rect(x, y, w, h);
381 void rectangle_alpha(int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, Uint8 opacity)
383 Uint32 color;
384 SDL_Surface *buf;
385 SDL_Rect rect, rect2;
387 if (w <= 0 || h <= 0 || x < 0 || y < 0 || x+w > WIDTH || y+h > HEIGHT) {
388 return;
391 color = SDL_MapRGB(screen->format, r, g, b);
393 buf = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
394 screen->format->BitsPerPixel,
395 screen->format->Rmask,
396 screen->format->Gmask,
397 screen->format->Bmask,
398 screen->format->Amask);
400 rect.x = rect.y = 0;
401 rect2.x = x;
402 rect2.y = y;
403 rect.w = rect2.w = w;
404 rect.h = rect2.h = h;
406 SDL_FillRect(buf, &rect, color);
407 SDL_SetAlpha(buf, SDL_RLEACCEL | SDL_SRCALPHA, opacity);
408 SDL_BlitSurface(buf, &rect, screen, &rect2);
409 update_rect(x, y, w, h);
411 SDL_FreeSurface(buf);
414 void draw_button( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
415 float diff = (pressed?1.0-BUTTON_HIGHLIGHT:1.0+BUTTON_HIGHLIGHT);
416 rectangle(x, y, w, h, MIN(r*diff, 255), MIN(g*diff, 255), MIN(b*diff, 255));
417 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));
418 rectangle(x+BUTTON_BORDER, y+BUTTON_BORDER, w-2*BUTTON_BORDER, h-2*BUTTON_BORDER, r, g, b);
421 void draw_button_text(const char* s, int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, char pressed) {
422 int font_x, font_y;
423 float factor = 0.3;
424 pressed = pressed?1:0;
425 draw_button( x, y, w, h, r, g, b, pressed);
426 font_x = x + w/2 - font_get_string_width(FONT_SMALL, s)/2 + pressed*BUTTON_BORDER;
427 font_y = y + h/2 - font_get_height(FONT_SMALL)/2 + pressed*BUTTON_BORDER;
428 font_draw_string_color(FONT_SMALL, s, font_x, font_y, (Uint8)(r*factor), (Uint8)(g*factor), (Uint8)(b*factor));
431 void draw_button_object(MenuButton* b, int mx, int my)
433 if (b->image_id == GR_COUNT) {
434 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));
435 } else {
436 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));
440 void clear_screen()
442 SDL_Rect rect;
444 rect.x = rect.y = 0;
445 rect.w = WIDTH;
446 rect.h = HEIGHT;
448 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
451 void store_screen()
453 SDL_BlitSurface(screen, NULL, background, NULL);
454 rect_update_old[0].x = rect_update_old[0].y = 0;
455 rect_update_old[0].w = WIDTH;
456 rect_update_old[0].h = HEIGHT;
457 rect_update_old_count = 1;
458 rect_update_cache_current = 0;
461 void reset_screen()
463 int i;
464 SDL_Rect* tmp;
465 SDL_BlitSurface(background, NULL, screen, NULL);
467 for (i=0; i<rect_update_cache_current; i++) {
468 SDL_BlitSurface(background, &rect_update_cache[i], screen, &rect_update_cache[i]);
471 /* Save rects I've just updated for redraw later */
472 tmp = rect_update_cache;
473 rect_update_cache = rect_update_old;
474 rect_update_old = tmp;
475 rect_update_old_count = rect_update_cache_current;
476 rect_update_cache_current = 0;
479 void update_rect(Sint32 x, Sint32 y, Sint32 w, Sint32 h)
481 SDL_Rect* u = NULL;
482 static int inside = 0;
483 if (inside) return;
484 inside = 1;
486 #ifdef DRAW_UPDATE_RECTANGLE
487 rectangle(x, y, w, h, 50+rand()%200, 50+rand()%200 ,50+rand()%200);
488 #endif
490 if ((x >= WIDTH) | (y >= HEIGHT) | (x+w <= 0) | (y+h <= 0) | (!w) | (!h)) {
491 inside = 0;
492 return;
495 if (rect_update_cache_current == RECT_UPDATE_CACHE) {
496 fprintf(stderr, "Overflow\n");
497 rect_update_cache_current = 0;
500 u = &(rect_update_cache[rect_update_cache_current]);
502 if (x < 0/* && x+w > 0*/) {
503 w += x;
504 x = 0;
506 if (y < 0/* && y+h > 0*/) {
507 h += y;
508 y = 0;
511 if (x+w >= WIDTH) {
512 w -= (x+w-WIDTH+1);
515 if (y+h >= HEIGHT) {
516 h -= (y+h-HEIGHT+1);
519 if (w==0 || h==0) {
520 inside = 0;
521 return;
524 u->x = x;
525 u->y = y;
526 u->w = w;
527 u->h = h;
529 rect_update_cache_current++;
531 inside = 0;
534 void updatescr()
536 Uint32 ticks = SDL_GetTicks();
537 SDL_Rect *r_current = NULL, *end_current = NULL;
538 SDL_Rect *r_old = NULL;
540 static int fading_last_time = 0;
541 unsigned char fading_now = ticks < fading_start+FADE_DURATION;
543 if (fading_now) {
544 SDL_SetAlpha(buffer, SDL_SRCALPHA | SDL_RLEACCEL, 255-255*(ticks-fading_start)/FADE_DURATION);
545 SDL_BlitSurface(buffer, NULL, screen, NULL);
546 SDL_UpdateRect(screen, 0, 0, 0, 0);
547 } else if (fading_last_time && !fading_now) {
548 SDL_UpdateRect(screen, 0, 0, 0, 0);
549 } else {
550 if (rect_update_old_count == rect_update_cache_current) {
551 /* Merge rects into one single rect list */
552 r_old = rect_update_old;
553 r_current = rect_update_cache;
554 end_current = rect_update_cache + rect_update_cache_current;
555 while (r_current != end_current) {
556 r_old->w = MAX(r_current->x+r_current->w, r_old->x+r_old->w);
557 r_old->h = MAX(r_current->y+r_current->h, r_old->y+r_old->h);
558 r_old->x = MIN(r_current->x, r_old->x);
559 r_old->y = MIN(r_current->y, r_old->y);
560 r_old->w -= r_old->x;
561 r_old->h -= r_old->y;
563 r_current++;
564 r_old++;
566 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
567 } else {
568 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
569 SDL_UpdateRects(screen, rect_update_cache_current, rect_update_cache);
573 reset_screen();
574 fading_last_time = fading_now;
577 void start_fade() {
578 SDL_BlitSurface( screen, NULL, buffer, NULL);
579 fading_start = SDL_GetTicks();
582 SDL_Surface* font_render_surface(font_id id, const char* text, Uint8 r,
583 Uint8 g, Uint8 b)
585 SDL_Surface *result;
586 SDL_Color color;
588 color.r = r;
589 color.g = g;
590 color.b = b;
592 tnx_assert(id < FONT_COUNT);
594 result = TTF_RenderText_Blended(fonts[id].data, text, color);
596 tnx_assert(result != NULL);
598 return result;
601 void blit_surface(SDL_Surface* surface, int x, int y, int pos, int count)
603 SDL_Rect src, dst;
605 dst.w = src.w = surface->w/count;
606 dst.h = src.h = surface->h;
607 src.x = src.w*pos;
608 src.y = 0;
609 dst.x = x;
610 dst.y = y;
612 SDL_BlitSurface(surface, &src, screen, &dst);
613 update_rect2(dst);
616 SDL_Surface* get_surface(image_id id)
618 tnx_assert(id < GR_COUNT);
619 return images[id].data;
622 void font_draw_string_color(font_id id, const char* s, int x_offset, int y_offset, Uint8 r, Uint8 g, Uint8 b) {
623 SDL_Surface *text;
625 text = font_render_surface(id, s, r, g, b);
626 blit_surface_simple(text, x_offset, y_offset);
627 SDL_FreeSurface(text);
630 int font_get_string_width(font_id id, const char* s) {
631 int w, h;
633 tnx_assert(id < FONT_COUNT);
635 if (TTF_SizeText(fonts[id].data, s, &w, &h) != 0) {
636 return 0;
639 return w;
642 int font_get_height(font_id id) {
643 tnx_assert(id < FONT_COUNT);
644 return TTF_FontHeight(fonts[id].data);
647 void draw_line_faded( int x1, int y1, int x2, int y2, int r, int g, int b, int r2, int g2, int b2) {
648 float step, dx, dy, x = x1, y = y1;
649 int i;
650 char fade = (r!=r2 || g!=g2 || b!=b2);
652 step = (float)(abs(x2-x1)>abs(y2-y1)?abs(x2-x1):abs(y2-y1));
653 dx = (float)(x2-x1) / step;
654 dy = (float)(y2-y1) / step;
656 SDL_LockSurface( screen);
657 for( i=0; i<step; i++) {
658 x += dx;
659 y += dy;
660 if( x < 0.0 || x >= WIDTH || y < 0.0 || y >= HEIGHT) {
661 continue;
663 if( fade) {
664 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);
665 } else {
666 SET_PIXEL_RGB( screen, (int)x, (int)y, r, g, b);
669 SDL_UnlockSurface( screen);