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
[] = {
89 "loc_margaret_court_arena.png",
90 "loc_stade_roland_garros.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"
100 void init_graphics() {
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());
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
);
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);
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);
173 fprintf(stderr
, "Cannot find file: %s\n", graphics
[i
]);
177 fprintf( stderr
, "Error: %s\n", SDL_GetError());
181 /* Convert to RGBA in the display format */
182 data
= SDL_DisplayFormatAlpha(tmp
);
183 SDL_FreeSurface(tmp
);
186 fprintf( stderr
, "Error: %s\n", SDL_GetError());
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);
198 void uninit_graphics() {
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
);
217 for (i
=0; i
<FONT_COUNT
; i
++) {
218 TTF_CloseFont(fonts
[i
].data
);
219 fonts
[i
].data
= NULL
;
223 freelist_free_all(font_freelist
);
229 FreeList
* freelist_create()
231 FreeList
* list
= (FreeList
*)malloc(sizeof(FreeList
));
232 assert(list
!= NULL
);
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
)
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
);
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
) {
287 bitmap
= images
[id
].data
;
291 SDL_SetAlpha( bitmap
, SDL_SRCALPHA
| SDL_RLEACCEL
, opacity
);
293 dst
.w
= src
.w
= bitmap
->w
/items
;
294 dst
.h
= src
.h
= bitmap
->h
;
300 SDL_BlitSurface( bitmap
, &src
, screen
, &dst
);
304 void show_image_rotozoom(image_id id
, int x
, int y
, float rotate
, float zoom
)
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
;
320 SDL_BlitSurface(tmp
, &src
, screen
, &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
) {
328 int dx
= 0, dy
= 0, cx
, cy
;
330 if( id
>= GR_COUNT
) return;
332 bitmap
= images
[id
].data
;
334 /* Make negative offsets positive */
336 offset_x
= (offset_x
%bitmap
->w
)+bitmap
->w
;
339 offset_y
= (offset_y
%bitmap
->h
)+bitmap
->h
;
342 src
.y
= offset_y
% bitmap
->h
;
344 src
.h
= dst
.h
= cy
= (h
-dy
> bitmap
->h
-src
.y
)?(bitmap
->h
-src
.y
):(h
-dy
);
348 src
.x
= offset_x
% bitmap
->w
;
350 src
.w
= dst
.w
= cx
= (w
-dx
> bitmap
->w
-src
.x
)?(bitmap
->w
-src
.x
):(w
-dx
);
353 SDL_BlitSurface( bitmap
, &src
, screen
, &dst
);
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
);
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
);
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
);
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
) {
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
));
438 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, 0, 0, 0));
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;
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
)
472 static int inside
= 0;
476 #ifdef DRAW_UPDATE_RECTANGLE
477 rectangle(x
, y
, w
, h
, 50+rand()%200, 50+rand()%200 ,50+rand()%200);
480 if ((x
>= WIDTH
) | (y
>= HEIGHT
) | (x
+w
<= 0) | (y
+h
<= 0) | (!w
) | (!h
)) {
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*/) {
496 if (y
< 0/* && y+h > 0*/) {
519 rect_update_cache_current
++;
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
;
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);
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
;
556 SDL_UpdateRects(screen
, rect_update_old_count
, rect_update_old
);
558 SDL_UpdateRects(screen
, rect_update_old_count
, rect_update_old
);
559 SDL_UpdateRects(screen
, rect_update_cache_current
, rect_update_cache
);
564 fading_last_time
= fading_now
;
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
) {
574 SDL_Rect rect
, rect2
;
581 assert(id
< FONT_COUNT
);
583 text
= TTF_RenderText_Blended(fonts
[id
].data
, s
, color
);
585 rect
.w
= rect2
.w
= text
->w
;
586 rect
.h
= rect2
.h
= text
->h
;
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
) {
601 assert(id
< FONT_COUNT
);
603 if (TTF_SizeText(fonts
[id
].data
, s
, &w
, &h
) != 0) {
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
;
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
++) {
628 if( x
< 0.0 || x
>= WIDTH
|| y
< 0.0 || y
>= HEIGHT
) {
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
);
634 SET_PIXEL_RGB( screen
, (int)x
, (int)y
, r
, g
, b
);
637 SDL_UnlockSurface( screen
);