5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
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,
38 #include "SDL_rotozoom.h"
43 FreeList
* font_freelist
= NULL
;
45 static FontDescription font_desc
[] = {
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
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
[] = {
90 "input_keyboard_arrows.png",
91 "input_keyboard_ol.png",
92 "input_keyboard_ws.png",
93 "input_maemo_dpad.png",
95 "input_touchscreen.png",
97 "loc_margaret_court_arena.png",
98 "loc_stade_roland_garros.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"
108 void init_graphics() {
113 TennixArchive
*tnxar
;
114 struct SDL_RWops
* rw
;
115 char *font_data
= NULL
;
117 if (TTF_Init() == -1) {
118 fprintf(stderr
, "Cannot init TTF: %s\n", TTF_GetError());
122 rect_update_cache
= (SDL_Rect
*)calloc(RECT_UPDATE_CACHE
, sizeof(SDL_Rect
));
123 rect_update_old
= (SDL_Rect
*)calloc(RECT_UPDATE_CACHE
, sizeof(SDL_Rect
));
124 rect_update_cache_current
= 0;
125 rect_update_old_count
= 0;
127 images
= (Image
*)calloc( GR_COUNT
, sizeof( Image
));
128 fonts
= (Font
*)calloc(FONT_COUNT
, sizeof(Font
));
130 buffer
= SDL_CreateRGBSurface(SDL_SWSURFACE
, WIDTH
, HEIGHT
,
131 screen
->format
->BitsPerPixel
,
132 screen
->format
->Rmask
,
133 screen
->format
->Gmask
,
134 screen
->format
->Bmask
,
135 screen
->format
->Amask
);
137 background
= SDL_CreateRGBSurface(SDL_SWSURFACE
, WIDTH
, HEIGHT
,
138 screen
->format
->BitsPerPixel
,
139 screen
->format
->Rmask
,
140 screen
->format
->Gmask
,
141 screen
->format
->Bmask
,
142 screen
->format
->Amask
);
144 if( buffer
== NULL
) {
145 fprintf( stderr
, "Cannot create buffer surface: %s\n", SDL_GetError());
148 tnxar
= tnxar_open(ARCHIVE_FILE
);
150 /* not found in cwd - try installed... */
151 tnxar
= tnxar_open(ARCHIVE_FILE_INSTALLED
);
152 assert(tnxar
!= NULL
);
155 font_freelist
= freelist_create();
157 /* Load fonts from resource file */
158 for (i
=0; i
<FONT_COUNT
; i
++) {
159 if (font_desc
[i
].filename
!= NULL
) {
160 assert(tnxar_set_current_filename(tnxar
, font_desc
[i
].filename
) != 0);
161 font_data
= tnxar_read_current(tnxar
);
162 freelist_append(font_freelist
, font_data
);
164 assert(font_data
!= NULL
);
165 rw
= SDL_RWFromMem(font_data
, tnxar_size_current(tnxar
));
166 fonts
[i
].data
= TTF_OpenFontRW(rw
, 1, font_desc
[i
].size
);
167 assert(fonts
[i
].data
!= NULL
);
168 TTF_SetFontStyle(fonts
[i
].data
, font_desc
[i
].style
);
171 draw_button(40, (HEIGHT
-40)/2, WIDTH
-80, 40, 100, 100, 100, 1);
175 for( i
=0; i
<GR_COUNT
; i
++) {
176 if (tnxar_set_current_filename(tnxar
, graphics
[i
]) != 0) {
177 d
= tnxar_read_current(tnxar
);
178 tmp
= IMG_Load_RW(SDL_RWFromMem(d
, tnxar_size_current(tnxar
)), 1);
181 fprintf(stderr
, "Cannot find file: %s\n", graphics
[i
]);
185 fprintf( stderr
, "Error: %s\n", SDL_GetError());
189 /* Convert to RGBA in the display format */
190 data
= SDL_DisplayFormatAlpha(tmp
);
191 SDL_FreeSurface(tmp
);
194 fprintf( stderr
, "Error: %s\n", SDL_GetError());
197 images
[i
].data
= data
;
199 draw_button(40, (HEIGHT
-40)/2, (WIDTH
-80)*i
/(GR_COUNT
+SOUND_MAX
), 40, 100, 250, 100, 0);
200 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);
206 void uninit_graphics() {
209 for( i
=0; i
<GR_COUNT
; i
++) {
210 SDL_FreeSurface( images
[i
].data
);
213 if( buffer
!= NULL
) {
214 SDL_FreeSurface( buffer
);
217 if (background
!= NULL
) {
218 SDL_FreeSurface(background
);
221 free(rect_update_cache
);
222 free(rect_update_old
);
225 for (i
=0; i
<FONT_COUNT
; i
++) {
226 TTF_CloseFont(fonts
[i
].data
);
227 fonts
[i
].data
= NULL
;
231 freelist_free_all(font_freelist
);
237 FreeList
* freelist_create()
239 FreeList
* list
= (FreeList
*)malloc(sizeof(FreeList
));
240 assert(list
!= NULL
);
247 void freelist_append(FreeList
* list
, char* data
)
249 FreeListItem
* new_item
= (FreeListItem
*)malloc(sizeof(FreeListItem
));
251 assert(list
!= NULL
);
252 assert(data
!= NULL
);
254 new_item
->data
= data
;
256 /* Insert item at the beginning of list */
257 new_item
->next
= list
->head
;
258 list
->head
= new_item
;
261 void freelist_free_all(FreeList
* list
)
265 assert(list
!= NULL
);
267 while (list
->head
!= NULL
) {
268 /* Remove one item from the head of the list */
269 next
= list
->head
->next
;
270 free(list
->head
->data
);
279 int get_image_width( image_id id
) {
280 return images
[id
].data
->w
;
283 int get_image_height( image_id id
) {
284 return images
[id
].data
->h
;
287 int get_sprite_width( image_id id
, int items
) {
288 return images
[id
].data
->w
/ items
;
291 void show_sprite( image_id id
, int pos
, int items
, int x_offset
,
292 int y_offset
, int opacity
)
294 assert(id
< GR_COUNT
);
295 SDL_SetAlpha(images
[id
].data
, SDL_SRCALPHA
| SDL_RLEACCEL
, opacity
);
296 blit_surface(images
[id
].data
, x_offset
, y_offset
, pos
, items
);
299 void show_image_rotozoom(image_id id
, int x
, int y
, float rotate
, float zoom
)
304 assert(id
< GR_COUNT
);
306 tmp
= rotozoomSurface(images
[id
].data
, rotate
, zoom
, SMOOTHING_OFF
);
308 dst
.w
= src
.w
= tmp
->w
;
309 dst
.h
= src
.h
= tmp
->h
;
315 SDL_BlitSurface(tmp
, &src
, screen
, &dst
);
317 SDL_FreeSurface(tmp
);
320 void fill_image_offset( image_id id
, int x
, int y
, int w
, int h
, int offset_x
, int offset_y
) {
323 int dx
= 0, dy
= 0, cx
, cy
;
325 if( id
>= GR_COUNT
) return;
327 bitmap
= images
[id
].data
;
329 /* Make negative offsets positive */
331 offset_x
= (offset_x
%bitmap
->w
)+bitmap
->w
;
334 offset_y
= (offset_y
%bitmap
->h
)+bitmap
->h
;
337 src
.y
= offset_y
% bitmap
->h
;
339 src
.h
= dst
.h
= cy
= (h
-dy
> bitmap
->h
-src
.y
)?(bitmap
->h
-src
.y
):(h
-dy
);
343 src
.x
= offset_x
% bitmap
->w
;
345 src
.w
= dst
.w
= cx
= (w
-dx
> bitmap
->w
-src
.x
)?(bitmap
->w
-src
.x
):(w
-dx
);
348 SDL_BlitSurface( bitmap
, &src
, screen
, &dst
);
360 void line_horiz( int y
, Uint8 r
, Uint8 g
, Uint8 b
) {
361 rectangle(0, y
, screen
->w
, 1, r
, g
, b
);
364 void line_vert( int x
, Uint8 r
, Uint8 g
, Uint8 b
) {
365 rectangle(x
, 0, 1, screen
->h
, r
, g
, b
);
368 void rectangle( int x
, int y
, int w
, int h
, Uint8 r
, Uint8 g
, Uint8 b
) {
369 Uint32 color
= SDL_MapRGB( screen
->format
, r
, g
, b
);
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
= SDL_MapRGB(screen
->format
, r
, g
, b
);
385 SDL_Rect rect
, rect2
;
387 buf
= SDL_CreateRGBSurface(SDL_SWSURFACE
, w
, h
,
388 screen
->format
->BitsPerPixel
,
389 screen
->format
->Rmask
,
390 screen
->format
->Gmask
,
391 screen
->format
->Bmask
,
392 screen
->format
->Amask
);
397 rect
.w
= rect2
.w
= w
;
398 rect
.h
= rect2
.h
= h
;
400 SDL_FillRect(buf
, &rect
, color
);
401 SDL_SetAlpha(buf
, SDL_RLEACCEL
| SDL_SRCALPHA
, opacity
);
402 SDL_BlitSurface(buf
, &rect
, screen
, &rect2
);
403 update_rect(x
, y
, w
, h
);
405 SDL_FreeSurface(buf
);
408 void draw_button( int x
, int y
, int w
, int h
, Uint8 r
, Uint8 g
, Uint8 b
, char pressed
) {
409 float diff
= (pressed
?1.0-BUTTON_HIGHLIGHT
:1.0+BUTTON_HIGHLIGHT
);
410 rectangle(x
, y
, w
, h
, MIN(r
*diff
, 255), MIN(g
*diff
, 255), MIN(b
*diff
, 255));
411 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));
412 rectangle(x
+BUTTON_BORDER
, y
+BUTTON_BORDER
, w
-2*BUTTON_BORDER
, h
-2*BUTTON_BORDER
, r
, g
, b
);
415 void draw_button_text(const char* s
, int x
, int y
, int w
, int h
, Uint8 r
, Uint8 g
, Uint8 b
, char pressed
) {
418 pressed
= pressed
?1:0;
419 draw_button( x
, y
, w
, h
, r
, g
, b
, pressed
);
420 font_x
= x
+ w
/2 - font_get_string_width(FONT_SMALL
, s
)/2 + pressed
*BUTTON_BORDER
;
421 font_y
= y
+ h
/2 - font_get_height(FONT_SMALL
)/2 + pressed
*BUTTON_BORDER
;
422 font_draw_string_color(FONT_SMALL
, s
, font_x
, font_y
, (Uint8
)(r
*factor
), (Uint8
)(g
*factor
), (Uint8
)(b
*factor
));
433 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, 0, 0, 0));
438 SDL_BlitSurface(screen
, NULL
, background
, NULL
);
439 rect_update_old
[0].x
= rect_update_old
[0].y
= 0;
440 rect_update_old
[0].w
= WIDTH
;
441 rect_update_old
[0].h
= HEIGHT
;
442 rect_update_old_count
= 1;
443 rect_update_cache_current
= 0;
450 SDL_BlitSurface(background
, NULL
, screen
, NULL
);
452 for (i
=0; i
<rect_update_cache_current
; i
++) {
453 SDL_BlitSurface(background
, &rect_update_cache
[i
], screen
, &rect_update_cache
[i
]);
456 /* Save rects I've just updated for redraw later */
457 tmp
= rect_update_cache
;
458 rect_update_cache
= rect_update_old
;
459 rect_update_old
= tmp
;
460 rect_update_old_count
= rect_update_cache_current
;
461 rect_update_cache_current
= 0;
464 void update_rect(Sint32 x
, Sint32 y
, Sint32 w
, Sint32 h
)
467 static int inside
= 0;
471 #ifdef DRAW_UPDATE_RECTANGLE
472 rectangle(x
, y
, w
, h
, 50+rand()%200, 50+rand()%200 ,50+rand()%200);
475 if ((x
>= WIDTH
) | (y
>= HEIGHT
) | (x
+w
<= 0) | (y
+h
<= 0) | (!w
) | (!h
)) {
480 if (rect_update_cache_current
== RECT_UPDATE_CACHE
) {
481 fprintf(stderr
, "Overflow\n");
482 rect_update_cache_current
= 0;
485 u
= &(rect_update_cache
[rect_update_cache_current
]);
487 if (x
< 0/* && x+w > 0*/) {
491 if (y
< 0/* && y+h > 0*/) {
514 rect_update_cache_current
++;
521 Uint32 ticks
= SDL_GetTicks();
522 SDL_Rect
*r_current
= NULL
, *end_current
= NULL
;
523 SDL_Rect
*r_old
= NULL
;
525 static int fading_last_time
= 0;
526 unsigned char fading_now
= ticks
< fading_start
+FADE_DURATION
;
529 SDL_SetAlpha(buffer
, SDL_SRCALPHA
| SDL_RLEACCEL
, 255-255*(ticks
-fading_start
)/FADE_DURATION
);
530 SDL_BlitSurface(buffer
, NULL
, screen
, NULL
);
531 SDL_UpdateRect(screen
, 0, 0, 0, 0);
532 } else if (fading_last_time
&& !fading_now
) {
533 SDL_UpdateRect(screen
, 0, 0, 0, 0);
535 if (rect_update_old_count
== rect_update_cache_current
) {
536 /* Merge rects into one single rect list */
537 r_old
= rect_update_old
;
538 r_current
= rect_update_cache
;
539 end_current
= rect_update_cache
+ rect_update_cache_current
;
540 while (r_current
!= end_current
) {
541 r_old
->w
= MAX(r_current
->x
+r_current
->w
, r_old
->x
+r_old
->w
);
542 r_old
->h
= MAX(r_current
->y
+r_current
->h
, r_old
->y
+r_old
->h
);
543 r_old
->x
= MIN(r_current
->x
, r_old
->x
);
544 r_old
->y
= MIN(r_current
->y
, r_old
->y
);
545 r_old
->w
-= r_old
->x
;
546 r_old
->h
-= r_old
->y
;
551 SDL_UpdateRects(screen
, rect_update_old_count
, rect_update_old
);
553 SDL_UpdateRects(screen
, rect_update_old_count
, rect_update_old
);
554 SDL_UpdateRects(screen
, rect_update_cache_current
, rect_update_cache
);
559 fading_last_time
= fading_now
;
563 SDL_BlitSurface( screen
, NULL
, buffer
, NULL
);
564 fading_start
= SDL_GetTicks();
567 SDL_Surface
* font_render_surface(font_id id
, const char* text
, Uint8 r
,
577 assert(id
< FONT_COUNT
);
579 result
= TTF_RenderText_Blended(fonts
[id
].data
, text
, color
);
581 assert(result
!= NULL
);
586 void blit_surface(SDL_Surface
* surface
, int x
, int y
, int pos
, int count
)
590 dst
.w
= src
.w
= surface
->w
/count
;
591 dst
.h
= src
.h
= surface
->h
;
597 SDL_BlitSurface(surface
, &src
, screen
, &dst
);
601 SDL_Surface
* get_surface(image_id id
)
603 assert(id
< GR_COUNT
);
604 return images
[id
].data
;
607 void font_draw_string_color(font_id id
, const char* s
, int x_offset
, int y_offset
, Uint8 r
, Uint8 g
, Uint8 b
) {
610 text
= font_render_surface(id
, s
, r
, g
, b
);
611 blit_surface_simple(text
, x_offset
, y_offset
);
612 SDL_FreeSurface(text
);
615 int font_get_string_width(font_id id
, const char* s
) {
618 assert(id
< FONT_COUNT
);
620 if (TTF_SizeText(fonts
[id
].data
, s
, &w
, &h
) != 0) {
627 int font_get_height(font_id id
) {
628 assert(id
< FONT_COUNT
);
629 return TTF_FontHeight(fonts
[id
].data
);
632 void draw_line_faded( int x1
, int y1
, int x2
, int y2
, int r
, int g
, int b
, int r2
, int g2
, int b2
) {
633 float step
, dx
, dy
, x
= x1
, y
= y1
;
635 char fade
= (r
!=r2
|| g
!=g2
|| b
!=b2
);
637 step
= (float)(abs(x2
-x1
)>abs(y2
-y1
)?abs(x2
-x1
):abs(y2
-y1
));
638 dx
= (float)(x2
-x1
) / step
;
639 dy
= (float)(y2
-y1
) / step
;
641 SDL_LockSurface( screen
);
642 for( i
=0; i
<step
; i
++) {
645 if( x
< 0.0 || x
>= WIDTH
|| y
< 0.0 || y
>= HEIGHT
) {
649 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
);
651 SET_PIXEL_RGB( screen
, (int)x
, (int)y
, r
, g
, b
);
654 SDL_UnlockSurface( screen
);