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,
37 static SDL_Surface
*buffer
;
38 static SDL_Surface
*background
;
40 static SDL_Rect
*rect_update_cache
;
41 static SDL_Rect
*rect_update_old
;
42 static int rect_update_cache_current
;
43 static int rect_update_old_count
;
45 Uint32 fading_start
= 0;
47 static char* graphics
[] = {
70 void init_graphics() {
77 rect_update_cache
= (SDL_Rect
*)calloc(RECT_UPDATE_CACHE
, sizeof(SDL_Rect
));
78 rect_update_old
= (SDL_Rect
*)calloc(RECT_UPDATE_CACHE
, sizeof(SDL_Rect
));
79 rect_update_cache_current
= 0;
80 rect_update_old_count
= 0;
82 images
= (Image
*)calloc( GR_COUNT
, sizeof( Image
));
84 buffer
= SDL_CreateRGBSurface(SDL_SWSURFACE
, WIDTH
, HEIGHT
,
85 screen
->format
->BitsPerPixel
,
86 screen
->format
->Rmask
,
87 screen
->format
->Gmask
,
88 screen
->format
->Bmask
,
89 screen
->format
->Amask
);
91 background
= SDL_CreateRGBSurface(SDL_SWSURFACE
, WIDTH
, HEIGHT
,
92 screen
->format
->BitsPerPixel
,
93 screen
->format
->Rmask
,
94 screen
->format
->Gmask
,
95 screen
->format
->Bmask
,
96 screen
->format
->Amask
);
99 fprintf( stderr
, "Cannot create buffer surface: %s\n", SDL_GetError());
102 tnxar
= tnxar_open(ARCHIVE_FILE
);
104 /* not found in cwd - try installed... */
105 tnxar
= tnxar_open(ARCHIVE_FILE_INSTALLED
);
106 assert(tnxar
!= NULL
);
109 draw_button(40, (HEIGHT
-40)/2, WIDTH
-80, 40, 100, 100, 100, 1);
113 for( i
=0; i
<GR_COUNT
; i
++) {
114 if (tnxar_set_current_filename(tnxar
, graphics
[i
]) != 0) {
115 d
= tnxar_read_current(tnxar
);
116 tmp
= IMG_Load_RW(SDL_RWFromMem(d
, tnxar_size_current(tnxar
)), 0);
119 fprintf(stderr
, "Cannot find file: %s\n", graphics
[i
]);
123 fprintf( stderr
, "Error: %s\n", SDL_GetError());
127 if( GRAPHICS_IS_FONT(i
)) {
128 /* Convert to RGB w/ colorkey=black for opacity support */
129 SDL_SetColorKey( tmp
, SDL_SRCCOLORKEY
| SDL_RLEACCEL
, SDL_MapRGB( tmp
->format
, 0, 0, 0));
130 data
= SDL_ConvertSurface( tmp
, screen
->format
, SDL_SRCCOLORKEY
| SDL_RLEACCEL
);
132 /* Convert to RGBA for alpha channel from PNG */
133 data
= SDL_DisplayFormatAlpha( tmp
);
135 SDL_FreeSurface( tmp
);
138 fprintf( stderr
, "Error: %s\n", SDL_GetError());
141 images
[i
].data
= data
;
143 draw_button(40, (HEIGHT
-40)/2, (WIDTH
-80)*i
/(GR_COUNT
+SOUND_MAX
), 40, 100, 250, 100, 0);
144 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);
150 void uninit_graphics() {
153 for( i
=0; i
<GR_COUNT
; i
++) {
154 SDL_FreeSurface( images
[i
].data
);
157 if( buffer
!= NULL
) {
158 SDL_FreeSurface( buffer
);
161 if (background
!= NULL
) {
162 SDL_FreeSurface(background
);
165 free(rect_update_cache
);
166 free(rect_update_old
);
170 int get_image_width( image_id id
) {
171 return images
[id
].data
->w
;
174 int get_image_height( image_id id
) {
175 return images
[id
].data
->h
;
178 int get_sprite_width( image_id id
, int items
) {
179 return images
[id
].data
->w
/ items
;
182 void show_sprite( image_id id
, int pos
, int items
, int x_offset
, int y_offset
, int opacity
) {
186 bitmap
= images
[id
].data
;
190 SDL_SetAlpha( bitmap
, SDL_SRCALPHA
| SDL_RLEACCEL
, opacity
);
192 dst
.w
= src
.w
= bitmap
->w
/items
;
193 dst
.h
= src
.h
= bitmap
->h
;
199 SDL_BlitSurface( bitmap
, &src
, screen
, &dst
);
203 void fill_image_offset( image_id id
, int x
, int y
, int w
, int h
, int offset_x
, int offset_y
) {
206 int dx
= 0, dy
= 0, cx
, cy
;
208 if( id
>= GR_COUNT
) return;
210 bitmap
= images
[id
].data
;
212 /* Make negative offsets positive */
214 offset_x
= (offset_x
%bitmap
->w
)+bitmap
->w
;
217 offset_y
= (offset_y
%bitmap
->h
)+bitmap
->h
;
220 src
.y
= offset_y
% bitmap
->h
;
222 src
.h
= dst
.h
= cy
= (h
-dy
> bitmap
->h
-src
.y
)?(bitmap
->h
-src
.y
):(h
-dy
);
226 src
.x
= offset_x
% bitmap
->w
;
228 src
.w
= dst
.w
= cx
= (w
-dx
> bitmap
->w
-src
.x
)?(bitmap
->w
-src
.x
):(w
-dx
);
231 SDL_BlitSurface( bitmap
, &src
, screen
, &dst
);
243 void line_horiz( int y
, Uint8 r
, Uint8 g
, Uint8 b
) {
244 rectangle(0, y
, screen
->w
, 1, r
, g
, b
);
247 void line_vert( int x
, Uint8 r
, Uint8 g
, Uint8 b
) {
248 rectangle(x
, 0, 1, screen
->h
, r
, g
, b
);
251 void rectangle( int x
, int y
, int w
, int h
, Uint8 r
, Uint8 g
, Uint8 b
) {
252 Uint32 color
= SDL_MapRGB( screen
->format
, r
, g
, b
);
260 SDL_FillRect( screen
, &rect
, color
);
261 update_rect(x
, y
, w
, h
);
264 void draw_button( int x
, int y
, int w
, int h
, Uint8 r
, Uint8 g
, Uint8 b
, char pressed
) {
265 float diff
= (pressed
?1.0-BUTTON_HIGHLIGHT
:1.0+BUTTON_HIGHLIGHT
);
266 rectangle(x
, y
, w
, h
, MIN(r
*diff
, 255), MIN(g
*diff
, 255), MIN(b
*diff
, 255));
267 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));
268 rectangle(x
+BUTTON_BORDER
, y
+BUTTON_BORDER
, w
-2*BUTTON_BORDER
, h
-2*BUTTON_BORDER
, r
, g
, b
);
271 void draw_button_text( char* s
, int x
, int y
, int w
, int h
, Uint8 r
, Uint8 g
, Uint8 b
, char pressed
) {
273 pressed
= pressed
?1:0;
274 draw_button( x
, y
, w
, h
, r
, g
, b
, pressed
);
275 font_x
= x
+ w
/2 - font_get_string_width( GR_SMALLISH_FONT
, s
)/2 + pressed
*BUTTON_BORDER
;
276 font_y
= y
+ h
/2 - get_image_height( GR_SMALLISH_FONT
)/2 + pressed
*BUTTON_BORDER
;
277 font_draw_string( GR_SMALLISH_FONT
, s
, font_x
, font_y
, 0, 0);
288 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, 0, 0, 0));
293 SDL_BlitSurface(screen
, NULL
, background
, NULL
);
294 rect_update_old
[0].x
= rect_update_old
[0].y
= 0;
295 rect_update_old
[0].w
= WIDTH
;
296 rect_update_old
[0].h
= HEIGHT
;
297 rect_update_old_count
= 1;
298 rect_update_cache_current
= 0;
305 SDL_BlitSurface(background
, NULL
, screen
, NULL
);
307 for (i
=0; i
<rect_update_cache_current
; i
++) {
308 SDL_BlitSurface(background
, &rect_update_cache
[i
], screen
, &rect_update_cache
[i
]);
311 /* Save rects I've just updated for redraw later */
312 tmp
= rect_update_cache
;
313 rect_update_cache
= rect_update_old
;
314 rect_update_old
= tmp
;
315 rect_update_old_count
= rect_update_cache_current
;
316 rect_update_cache_current
= 0;
319 void update_rect(Sint32 x
, Sint32 y
, Sint32 w
, Sint32 h
)
321 static int inside
= 0;
325 #ifdef DRAW_UPDATE_RECTANGLE
326 rectangle(x
, y
, w
, h
, 50+rand()%200, 50+rand()%200 ,50+rand()%200);
329 if ((x
>= WIDTH
) | (y
>= HEIGHT
) | (x
+w
<= 0) | (y
+h
<= 0) | (!w
) | (!h
)) {
334 if (rect_update_cache_current
== RECT_UPDATE_CACHE
) {
335 fprintf(stderr
, "Overflow\n");
336 rect_update_cache_current
= 0;
339 SDL_Rect
* u
= &(rect_update_cache
[rect_update_cache_current
]);
341 if (x
< 0/* && x+w > 0*/) {
345 if (y
< 0/* && y+h > 0*/) {
368 rect_update_cache_current
++;
375 int ticks
= SDL_GetTicks();
376 SDL_Rect
*r_current
= NULL
, *end_current
= NULL
;
377 SDL_Rect
*r_old
= NULL
;
379 static int fading_last_time
= 0;
380 int fading_now
= ticks
< fading_start
+FADE_DURATION
;
383 SDL_SetAlpha(buffer
, SDL_SRCALPHA
| SDL_RLEACCEL
, 255-255*(ticks
-fading_start
)/FADE_DURATION
);
384 SDL_BlitSurface(buffer
, NULL
, screen
, NULL
);
385 SDL_UpdateRect(screen
, 0, 0, 0, 0);
386 } else if (fading_last_time
&& !fading_now
) {
387 SDL_UpdateRect(screen
, 0, 0, 0, 0);
389 if (rect_update_old_count
== rect_update_cache_current
) {
390 /* Merge rects into one single rect list */
391 r_old
= rect_update_old
;
392 r_current
= rect_update_cache
;
393 end_current
= rect_update_cache
+ rect_update_cache_current
;
394 while (r_current
!= end_current
) {
395 r_old
->w
= MAX(r_current
->x
+r_current
->w
, r_old
->x
+r_old
->w
);
396 r_old
->h
= MAX(r_current
->y
+r_current
->h
, r_old
->y
+r_old
->h
);
397 r_old
->x
= MIN(r_current
->x
, r_old
->x
);
398 r_old
->y
= MIN(r_current
->y
, r_old
->y
);
399 r_old
->w
-= r_old
->x
;
400 r_old
->h
-= r_old
->y
;
405 SDL_UpdateRects(screen
, rect_update_old_count
, rect_update_old
);
407 SDL_UpdateRects(screen
, rect_update_old_count
, rect_update_old
);
408 SDL_UpdateRects(screen
, rect_update_cache_current
, rect_update_cache
);
413 fading_last_time
= fading_now
;
417 SDL_BlitSurface( screen
, NULL
, buffer
, NULL
);
418 fading_start
= SDL_GetTicks();
421 int font_get_metrics(image_id id
, unsigned char ch
, int* xp
, int* wp
) {
422 /* Caching of x and width values for faster calculation */
423 static int xcache
[0xFF][GRAPHICS_FONT_COUNT
],
424 wcache
[0xFF][GRAPHICS_FONT_COUNT
];
425 static unsigned char cacheflag
[0xFF][GRAPHICS_FONT_COUNT
];
428 int pos
, x
= -1, w
= 0;
429 int search_pos
= 0, search_x
= 0;
430 unsigned char font_index
= id
-GRAPHICS_FONT_FIRST
;
431 Uint8 red
, green
, blue
;
433 if(!GRAPHICS_IS_FONT(id
)) return 0;
435 if (cacheflag
[ch
][font_index
]) {
437 if (xp
!= NULL
) *xp
= xcache
[ch
][font_index
];
438 if (wp
!= NULL
) *wp
= wcache
[ch
][font_index
];
439 return wcache
[ch
][font_index
];
442 pos
= toupper( ch
) - ' '; /* ' ' = first character in font bitmap */
444 bitmap
= images
[id
].data
;
446 SDL_LockSurface( bitmap
);
447 while( search_x
< bitmap
->w
) {
448 GET_PIXEL_RGB( bitmap
, search_x
, 0, &red
, &green
, &blue
);
450 /* Increase pos counter if we have a "marker" pixel (255,0,255) */
451 if( red
> 250 && green
< 10 && blue
> 250) {
453 if( search_pos
== pos
) {
455 } else if( search_pos
== pos
+ 1) {
463 SDL_UnlockSurface( bitmap
);
465 if( wp
!= NULL
) (*wp
) = w
;
466 if( xp
!= NULL
) (*xp
) = x
;
468 cacheflag
[ch
][font_index
]++;
469 xcache
[ch
][font_index
] = x
;
470 wcache
[ch
][font_index
] = w
;
475 int font_draw_char( image_id id
, char ch
, int x_offset
, int y_offset
) {
480 font_get_metrics( id
, ch
, &x
, &w
);
481 if( x
== -1) return w
;
483 bitmap
= images
[id
].data
;
486 dst
.h
= src
.h
= bitmap
->h
- 1;
492 SDL_BlitSurface( bitmap
, &src
, screen
, &dst
);
497 void font_draw_string_alpha( image_id id
, const char* s
, int x_offset
, int y_offset
, int start
, int animation
, int opacity
) {
500 int i
, additional_x
= 0, additional_y
= 0;
501 float xw
= 0.0, xw_diff
;
504 if( id
> GR_COUNT
) return;
506 bitmap
= images
[id
].data
;
507 SDL_SetAlpha( bitmap
, SDL_SRCALPHA
| SDL_RLEACCEL
, opacity
);
509 if( animation
& ANIMATION_BUNGEE
) {
510 xw
= (25.0*sinf( start
/10.0));
515 if( animation
& ANIMATION_PENDULUM
) {
516 x
-= (int)(20.0*sinf( start
/20.0));
520 for( i
=0; i
<strlen(s
); i
++) {
521 if( animation
& ANIMATION_WAVE
) {
522 y
= y_offset
+ (int)(3.0*sinf( start
/10.0 + x
/30.0));
524 x
+= font_draw_char( id
, s
[i
], x
, y
);
525 if( animation
& ANIMATION_BUNGEE
) {
526 xw_diff
+= xw
/strlen(s
);
527 if( xw_diff
> 1.0 || xw_diff
< -1.0) {
537 if (animation
& ANIMATION_WAVE
) {
540 if (animation
& ANIMATION_BUNGEE
) {
543 update_rect(x_offset
-additional_x
, y_offset
-additional_y
, x
-x_offset
+additional_x
*2, get_image_height(id
)+additional_y
*2);
546 int font_get_string_width( image_id id
, const char* s
) {
549 for( i
=0; i
<strlen(s
); i
++) {
550 w
+= font_get_metrics( id
, s
[i
], NULL
, NULL
);
556 void draw_line_faded( int x1
, int y1
, int x2
, int y2
, int r
, int g
, int b
, int r2
, int g2
, int b2
) {
557 float step
, dx
, dy
, x
= x1
, y
= y1
;
559 char fade
= (r
!=r2
|| g
!=g2
|| b
!=b2
);
561 step
= (float)(abs(x2
-x1
)>abs(y2
-y1
)?abs(x2
-x1
):abs(y2
-y1
));
562 dx
= (float)(x2
-x1
) / step
;
563 dy
= (float)(y2
-y1
) / step
;
565 SDL_LockSurface( screen
);
566 for( i
=0; i
<step
; i
++) {
569 if( x
< 0.0 || x
>= WIDTH
|| y
< 0.0 || y
>= HEIGHT
) {
573 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
);
575 SET_PIXEL_RGB( screen
, (int)x
, (int)y
, r
, g
, b
);
578 SDL_UnlockSurface( screen
);