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,
39 static SDL_Surface
*buffer
;
40 static SDL_Surface
*background
;
42 static SDL_Rect
*rect_update_cache
;
43 static SDL_Rect
*rect_update_old
;
44 static int rect_update_cache_current
;
45 static int rect_update_old_count
;
47 Uint32 fading_start
= 0;
49 static const char* graphics
[] = {
73 void init_graphics() {
80 rect_update_cache
= (SDL_Rect
*)calloc(RECT_UPDATE_CACHE
, sizeof(SDL_Rect
));
81 rect_update_old
= (SDL_Rect
*)calloc(RECT_UPDATE_CACHE
, sizeof(SDL_Rect
));
82 rect_update_cache_current
= 0;
83 rect_update_old_count
= 0;
85 images
= (Image
*)calloc( GR_COUNT
, sizeof( Image
));
87 buffer
= SDL_CreateRGBSurface(SDL_SWSURFACE
, WIDTH
, HEIGHT
,
88 screen
->format
->BitsPerPixel
,
89 screen
->format
->Rmask
,
90 screen
->format
->Gmask
,
91 screen
->format
->Bmask
,
92 screen
->format
->Amask
);
94 background
= SDL_CreateRGBSurface(SDL_SWSURFACE
, WIDTH
, HEIGHT
,
95 screen
->format
->BitsPerPixel
,
96 screen
->format
->Rmask
,
97 screen
->format
->Gmask
,
98 screen
->format
->Bmask
,
99 screen
->format
->Amask
);
101 if( buffer
== NULL
) {
102 fprintf( stderr
, "Cannot create buffer surface: %s\n", SDL_GetError());
105 tnxar
= tnxar_open(ARCHIVE_FILE
);
107 /* not found in cwd - try installed... */
108 tnxar
= tnxar_open(ARCHIVE_FILE_INSTALLED
);
109 assert(tnxar
!= NULL
);
112 draw_button(40, (HEIGHT
-40)/2, WIDTH
-80, 40, 100, 100, 100, 1);
116 for( i
=0; i
<GR_COUNT
; i
++) {
117 if (tnxar_set_current_filename(tnxar
, graphics
[i
]) != 0) {
118 d
= tnxar_read_current(tnxar
);
119 tmp
= IMG_Load_RW(SDL_RWFromMem(d
, tnxar_size_current(tnxar
)), 0);
122 fprintf(stderr
, "Cannot find file: %s\n", graphics
[i
]);
126 fprintf( stderr
, "Error: %s\n", SDL_GetError());
130 if( GRAPHICS_IS_FONT(i
)) {
131 /* Convert to RGB w/ colorkey=black for opacity support */
132 SDL_SetColorKey( tmp
, SDL_SRCCOLORKEY
| SDL_RLEACCEL
, SDL_MapRGB( tmp
->format
, 0, 0, 0));
133 data
= SDL_ConvertSurface( tmp
, screen
->format
, SDL_SRCCOLORKEY
| SDL_RLEACCEL
);
135 /* Convert to RGBA for alpha channel from PNG */
136 data
= SDL_DisplayFormatAlpha( tmp
);
138 SDL_FreeSurface( tmp
);
141 fprintf( stderr
, "Error: %s\n", SDL_GetError());
144 images
[i
].data
= data
;
146 draw_button(40, (HEIGHT
-40)/2, (WIDTH
-80)*i
/(GR_COUNT
+SOUND_MAX
), 40, 100, 250, 100, 0);
147 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);
153 void uninit_graphics() {
156 for( i
=0; i
<GR_COUNT
; i
++) {
157 SDL_FreeSurface( images
[i
].data
);
160 if( buffer
!= NULL
) {
161 SDL_FreeSurface( buffer
);
164 if (background
!= NULL
) {
165 SDL_FreeSurface(background
);
168 free(rect_update_cache
);
169 free(rect_update_old
);
173 int get_image_width( image_id id
) {
174 return images
[id
].data
->w
;
177 int get_image_height( image_id id
) {
178 return images
[id
].data
->h
;
181 int get_sprite_width( image_id id
, int items
) {
182 return images
[id
].data
->w
/ items
;
185 void show_sprite( image_id id
, int pos
, int items
, int x_offset
, int y_offset
, int opacity
) {
189 bitmap
= images
[id
].data
;
193 SDL_SetAlpha( bitmap
, SDL_SRCALPHA
| SDL_RLEACCEL
, opacity
);
195 dst
.w
= src
.w
= bitmap
->w
/items
;
196 dst
.h
= src
.h
= bitmap
->h
;
202 SDL_BlitSurface( bitmap
, &src
, screen
, &dst
);
206 void fill_image_offset( image_id id
, int x
, int y
, int w
, int h
, int offset_x
, int offset_y
) {
209 int dx
= 0, dy
= 0, cx
, cy
;
211 if( id
>= GR_COUNT
) return;
213 bitmap
= images
[id
].data
;
215 /* Make negative offsets positive */
217 offset_x
= (offset_x
%bitmap
->w
)+bitmap
->w
;
220 offset_y
= (offset_y
%bitmap
->h
)+bitmap
->h
;
223 src
.y
= offset_y
% bitmap
->h
;
225 src
.h
= dst
.h
= cy
= (h
-dy
> bitmap
->h
-src
.y
)?(bitmap
->h
-src
.y
):(h
-dy
);
229 src
.x
= offset_x
% bitmap
->w
;
231 src
.w
= dst
.w
= cx
= (w
-dx
> bitmap
->w
-src
.x
)?(bitmap
->w
-src
.x
):(w
-dx
);
234 SDL_BlitSurface( bitmap
, &src
, screen
, &dst
);
246 void line_horiz( int y
, Uint8 r
, Uint8 g
, Uint8 b
) {
247 rectangle(0, y
, screen
->w
, 1, r
, g
, b
);
250 void line_vert( int x
, Uint8 r
, Uint8 g
, Uint8 b
) {
251 rectangle(x
, 0, 1, screen
->h
, r
, g
, b
);
254 void rectangle( int x
, int y
, int w
, int h
, Uint8 r
, Uint8 g
, Uint8 b
) {
255 Uint32 color
= SDL_MapRGB( screen
->format
, r
, g
, b
);
263 SDL_FillRect( screen
, &rect
, color
);
264 update_rect(x
, y
, w
, h
);
267 void draw_button( int x
, int y
, int w
, int h
, Uint8 r
, Uint8 g
, Uint8 b
, char pressed
) {
268 float diff
= (pressed
?1.0-BUTTON_HIGHLIGHT
:1.0+BUTTON_HIGHLIGHT
);
269 rectangle(x
, y
, w
, h
, MIN(r
*diff
, 255), MIN(g
*diff
, 255), MIN(b
*diff
, 255));
270 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));
271 rectangle(x
+BUTTON_BORDER
, y
+BUTTON_BORDER
, w
-2*BUTTON_BORDER
, h
-2*BUTTON_BORDER
, r
, g
, b
);
274 void draw_button_text(const char* s
, int x
, int y
, int w
, int h
, Uint8 r
, Uint8 g
, Uint8 b
, char pressed
) {
276 pressed
= pressed
?1:0;
277 draw_button( x
, y
, w
, h
, r
, g
, b
, pressed
);
278 font_x
= x
+ w
/2 - font_get_string_width( GR_SMALLISH_FONT
, s
)/2 + pressed
*BUTTON_BORDER
;
279 font_y
= y
+ h
/2 - get_image_height( GR_SMALLISH_FONT
)/2 + pressed
*BUTTON_BORDER
;
280 font_draw_string( GR_SMALLISH_FONT
, s
, font_x
, font_y
, 0, 0);
291 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, 0, 0, 0));
296 SDL_BlitSurface(screen
, NULL
, background
, NULL
);
297 rect_update_old
[0].x
= rect_update_old
[0].y
= 0;
298 rect_update_old
[0].w
= WIDTH
;
299 rect_update_old
[0].h
= HEIGHT
;
300 rect_update_old_count
= 1;
301 rect_update_cache_current
= 0;
308 SDL_BlitSurface(background
, NULL
, screen
, NULL
);
310 for (i
=0; i
<rect_update_cache_current
; i
++) {
311 SDL_BlitSurface(background
, &rect_update_cache
[i
], screen
, &rect_update_cache
[i
]);
314 /* Save rects I've just updated for redraw later */
315 tmp
= rect_update_cache
;
316 rect_update_cache
= rect_update_old
;
317 rect_update_old
= tmp
;
318 rect_update_old_count
= rect_update_cache_current
;
319 rect_update_cache_current
= 0;
322 void update_rect(Sint32 x
, Sint32 y
, Sint32 w
, Sint32 h
)
325 static int inside
= 0;
329 #ifdef DRAW_UPDATE_RECTANGLE
330 rectangle(x
, y
, w
, h
, 50+rand()%200, 50+rand()%200 ,50+rand()%200);
333 if ((x
>= WIDTH
) | (y
>= HEIGHT
) | (x
+w
<= 0) | (y
+h
<= 0) | (!w
) | (!h
)) {
338 if (rect_update_cache_current
== RECT_UPDATE_CACHE
) {
339 fprintf(stderr
, "Overflow\n");
340 rect_update_cache_current
= 0;
343 u
= &(rect_update_cache
[rect_update_cache_current
]);
345 if (x
< 0/* && x+w > 0*/) {
349 if (y
< 0/* && y+h > 0*/) {
372 rect_update_cache_current
++;
379 Uint32 ticks
= SDL_GetTicks();
380 SDL_Rect
*r_current
= NULL
, *end_current
= NULL
;
381 SDL_Rect
*r_old
= NULL
;
383 static int fading_last_time
= 0;
384 unsigned char fading_now
= ticks
< fading_start
+FADE_DURATION
;
387 SDL_SetAlpha(buffer
, SDL_SRCALPHA
| SDL_RLEACCEL
, 255-255*(ticks
-fading_start
)/FADE_DURATION
);
388 SDL_BlitSurface(buffer
, NULL
, screen
, NULL
);
389 SDL_UpdateRect(screen
, 0, 0, 0, 0);
390 } else if (fading_last_time
&& !fading_now
) {
391 SDL_UpdateRect(screen
, 0, 0, 0, 0);
393 if (rect_update_old_count
== rect_update_cache_current
) {
394 /* Merge rects into one single rect list */
395 r_old
= rect_update_old
;
396 r_current
= rect_update_cache
;
397 end_current
= rect_update_cache
+ rect_update_cache_current
;
398 while (r_current
!= end_current
) {
399 r_old
->w
= MAX(r_current
->x
+r_current
->w
, r_old
->x
+r_old
->w
);
400 r_old
->h
= MAX(r_current
->y
+r_current
->h
, r_old
->y
+r_old
->h
);
401 r_old
->x
= MIN(r_current
->x
, r_old
->x
);
402 r_old
->y
= MIN(r_current
->y
, r_old
->y
);
403 r_old
->w
-= r_old
->x
;
404 r_old
->h
-= r_old
->y
;
409 SDL_UpdateRects(screen
, rect_update_old_count
, rect_update_old
);
411 SDL_UpdateRects(screen
, rect_update_old_count
, rect_update_old
);
412 SDL_UpdateRects(screen
, rect_update_cache_current
, rect_update_cache
);
417 fading_last_time
= fading_now
;
421 SDL_BlitSurface( screen
, NULL
, buffer
, NULL
);
422 fading_start
= SDL_GetTicks();
425 int font_get_metrics(image_id id
, unsigned char ch
, int* xp
, int* wp
) {
426 /* Caching of x and width values for faster calculation */
427 static int xcache
[0xFF][GRAPHICS_FONT_COUNT
],
428 wcache
[0xFF][GRAPHICS_FONT_COUNT
];
429 static unsigned char cacheflag
[0xFF][GRAPHICS_FONT_COUNT
];
432 int pos
, x
= -1, w
= 0;
433 int search_pos
= 0, search_x
= 0;
434 unsigned char font_index
= id
-GRAPHICS_FONT_FIRST
;
435 Uint8 red
, green
, blue
;
437 if(!GRAPHICS_IS_FONT(id
)) return 0;
439 if (cacheflag
[ch
][font_index
]) {
441 if (xp
!= NULL
) *xp
= xcache
[ch
][font_index
];
442 if (wp
!= NULL
) *wp
= wcache
[ch
][font_index
];
443 return wcache
[ch
][font_index
];
446 pos
= toupper( ch
) - ' '; /* ' ' = first character in font bitmap */
448 bitmap
= images
[id
].data
;
450 SDL_LockSurface( bitmap
);
451 while( search_x
< bitmap
->w
) {
452 GET_PIXEL_RGB( bitmap
, search_x
, 0, &red
, &green
, &blue
);
454 /* Increase pos counter if we have a "marker" pixel (255,0,255) */
455 if( red
> 250 && green
< 10 && blue
> 250) {
457 if( search_pos
== pos
) {
459 } else if( search_pos
== pos
+ 1) {
467 SDL_UnlockSurface( bitmap
);
469 if( wp
!= NULL
) (*wp
) = w
;
470 if( xp
!= NULL
) (*xp
) = x
;
472 cacheflag
[ch
][font_index
]++;
473 xcache
[ch
][font_index
] = x
;
474 wcache
[ch
][font_index
] = w
;
479 int font_draw_char( image_id id
, char ch
, int x_offset
, int y_offset
) {
484 font_get_metrics( id
, ch
, &x
, &w
);
485 if( x
== -1) return w
;
487 bitmap
= images
[id
].data
;
490 dst
.h
= src
.h
= bitmap
->h
- 1;
496 SDL_BlitSurface( bitmap
, &src
, screen
, &dst
);
501 void font_draw_string_alpha( image_id id
, const char* s
, int x_offset
, int y_offset
, int start
, int animation
, int opacity
) {
504 int additional_x
= 0, additional_y
= 0;
505 float xw
= 0.0, xw_diff
= 0.0;
509 if( id
> GR_COUNT
) return;
511 bitmap
= images
[id
].data
;
512 SDL_SetAlpha( bitmap
, SDL_SRCALPHA
| SDL_RLEACCEL
, opacity
);
514 if( animation
& ANIMATION_BUNGEE
) {
515 xw
= (25.0*sinf( start
/10.0));
519 if( animation
& ANIMATION_PENDULUM
) {
520 x
-= (int)(20.0*sinf( start
/20.0));
524 for( i
=0; i
<strlen(s
); i
++) {
525 if( animation
& ANIMATION_WAVE
) {
526 y
= y_offset
+ (int)(3.0*sinf( start
/10.0 + x
/30.0));
528 x
+= font_draw_char( id
, s
[i
], x
, y
);
529 if( animation
& ANIMATION_BUNGEE
) {
530 xw_diff
+= xw
/strlen(s
);
531 if( xw_diff
> 1.0 || xw_diff
< -1.0) {
541 if (animation
& ANIMATION_WAVE
) {
544 if (animation
& ANIMATION_BUNGEE
) {
547 update_rect(x_offset
-additional_x
, y_offset
-additional_y
, x
-x_offset
+additional_x
*2, get_image_height(id
)+additional_y
*2);
550 int font_get_string_width( image_id id
, const char* s
) {
554 for( i
=0; i
<strlen(s
); i
++) {
555 w
+= font_get_metrics( id
, s
[i
], NULL
, NULL
);
561 void draw_line_faded( int x1
, int y1
, int x2
, int y2
, int r
, int g
, int b
, int r2
, int g2
, int b2
) {
562 float step
, dx
, dy
, x
= x1
, y
= y1
;
564 char fade
= (r
!=r2
|| g
!=g2
|| b
!=b2
);
566 step
= (float)(abs(x2
-x1
)>abs(y2
-y1
)?abs(x2
-x1
):abs(y2
-y1
));
567 dx
= (float)(x2
-x1
) / step
;
568 dy
= (float)(y2
-y1
) / step
;
570 SDL_LockSurface( screen
);
571 for( i
=0; i
<step
; i
++) {
574 if( x
< 0.0 || x
>= WIDTH
|| y
< 0.0 || y
>= HEIGHT
) {
578 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
);
580 SET_PIXEL_RGB( screen
, (int)x
, (int)y
, r
, g
, b
);
583 SDL_UnlockSurface( screen
);