Fix a severe bug on non-x86 machines (archive segfault)
[tennix.git] / graphics.c
blob0fb4612e433cd79e4080aa7e8d1ce177e744dbfc
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 <assert.h>
30 #include "tennix.h"
31 #include "graphics.h"
32 #include "archive.h"
33 #include "sound.h"
35 static Image* images;
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[] = {
48 "court.png",
49 "shadow.png",
50 "player-racket.png",
51 "ball.png",
52 "smallish-font.png",
53 "dkc2.png",
54 "referee.png",
55 "ctt_hard.png",
56 "ctt_clay.png",
57 "ctt_grass.png",
58 "sidebar.png",
59 "tennixlogo.png",
60 "btnplay.png",
61 "btnresume.png",
62 "btnquit.png",
63 "stadium.png",
64 "fog.png",
65 "fog2.png",
66 "night.png",
67 "talk.png"
70 void init_graphics() {
71 int i;
72 char *d;
73 SDL_Surface* data;
74 SDL_Surface* tmp;
75 TennixArchive *tnxar;
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);
98 if( buffer == NULL) {
99 fprintf( stderr, "Cannot create buffer surface: %s\n", SDL_GetError());
102 tnxar = tnxar_open(ARCHIVE_FILE);
103 if (tnxar == NULL) {
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);
110 store_screen();
111 updatescr();
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);
117 free(d);
118 } else {
119 fprintf(stderr, "Cannot find file: %s\n", graphics[i]);
120 exit(EXIT_FAILURE);
122 if( !tmp) {
123 fprintf( stderr, "Error: %s\n", SDL_GetError());
124 continue;
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);
131 } else {
132 /* Convert to RGBA for alpha channel from PNG */
133 data = SDL_DisplayFormatAlpha( tmp);
135 SDL_FreeSurface( tmp);
137 if( !data) {
138 fprintf( stderr, "Error: %s\n", SDL_GetError());
139 continue;
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);
145 updatescr();
147 tnxar_close(tnxar);
150 void uninit_graphics() {
151 int i;
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);
167 free(images);
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) {
183 SDL_Surface *bitmap;
184 SDL_Rect src, dst;
186 bitmap = images[id].data;
188 if( !bitmap) return;
190 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
192 dst.w = src.w = bitmap->w/items;
193 dst.h = src.h = bitmap->h;
194 src.x = src.w*pos;
195 src.y = 0;
196 dst.x = x_offset;
197 dst.y = y_offset;
199 SDL_BlitSurface( bitmap, &src, screen, &dst);
200 update_rect2(dst);
203 void fill_image_offset( image_id id, int x, int y, int w, int h, int offset_x, int offset_y) {
204 SDL_Surface *bitmap;
205 SDL_Rect src, dst;
206 int dx = 0, dy = 0, cx, cy;
208 if( id >= GR_COUNT) return;
210 bitmap = images[id].data;
212 /* Make negative offsets positive */
213 if (offset_x < 0) {
214 offset_x = (offset_x%bitmap->w)+bitmap->w;
216 if (offset_y < 0) {
217 offset_y = (offset_y%bitmap->h)+bitmap->h;
220 src.y = offset_y % bitmap->h;
221 while( dy < h) {
222 src.h = dst.h = cy = (h-dy > bitmap->h-src.y)?(bitmap->h-src.y):(h-dy);
223 dst.y = y+dy;
225 dx = 0;
226 src.x = offset_x % bitmap->w;
227 while( dx < w) {
228 src.w = dst.w = cx = (w-dx > bitmap->w-src.x)?(bitmap->w-src.x):(w-dx);
229 dst.x = x+dx;
231 SDL_BlitSurface( bitmap, &src, screen, &dst);
232 update_rect2(dst);
234 dx += cx;
235 src.x = 0;
238 dy += cy;
239 src.y = 0;
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);
253 SDL_Rect rect;
255 rect.x = x;
256 rect.y = y;
257 rect.w = w;
258 rect.h = h;
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) {
272 int font_x, font_y;
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);
280 void clear_screen()
282 SDL_Rect rect;
284 rect.x = rect.y = 0;
285 rect.w = WIDTH;
286 rect.h = HEIGHT;
288 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
291 void store_screen()
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;
301 void reset_screen()
303 int i;
304 SDL_Rect* tmp;
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;
322 if (inside) return;
323 inside = 1;
325 #ifdef DRAW_UPDATE_RECTANGLE
326 rectangle(x, y, w, h, 50+rand()%200, 50+rand()%200 ,50+rand()%200);
327 #endif
329 if ((x >= WIDTH) | (y >= HEIGHT) | (x+w <= 0) | (y+h <= 0) | (!w) | (!h)) {
330 inside = 0;
331 return;
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*/) {
342 w += x;
343 x = 0;
345 if (y < 0/* && y+h > 0*/) {
346 h += y;
347 y = 0;
350 if (x+w >= WIDTH) {
351 w -= (x+w-WIDTH+1);
354 if (y+h >= HEIGHT) {
355 h -= (y+h-HEIGHT+1);
358 if (w==0 || h==0) {
359 inside = 0;
360 return;
363 u->x = x;
364 u->y = y;
365 u->w = w;
366 u->h = h;
368 rect_update_cache_current++;
370 inside = 0;
373 void updatescr()
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;
382 if (fading_now) {
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);
388 } else {
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;
402 r_current++;
403 r_old++;
405 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
406 } else {
407 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
408 SDL_UpdateRects(screen, rect_update_cache_current, rect_update_cache);
412 reset_screen();
413 fading_last_time = fading_now;
416 void start_fade() {
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];
427 SDL_Surface *bitmap;
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]) {
436 /* saved! */
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) {
452 search_pos++;
453 if( search_pos == pos) {
454 x = search_x;
455 } else if( search_pos == pos + 1) {
456 w = search_x - x;
457 break;
461 search_x++;
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;
472 return w;
475 int font_draw_char( image_id id, char ch, int x_offset, int y_offset) {
476 SDL_Surface *bitmap;
477 SDL_Rect src, dst;
478 int x = -1, w = 0;
480 font_get_metrics( id, ch, &x, &w);
481 if( x == -1) return w;
483 bitmap = images[id].data;
485 dst.w = src.w = w;
486 dst.h = src.h = bitmap->h - 1;
487 src.x = x;
488 src.y = 1;
489 dst.x = x_offset;
490 dst.y = y_offset;
492 SDL_BlitSurface( bitmap, &src, screen, &dst);
494 return src.w;
497 void font_draw_string_alpha( image_id id, const char* s, int x_offset, int y_offset, int start, int animation, int opacity) {
498 int y = y_offset;
499 int x = x_offset;
500 int i, additional_x = 0, additional_y = 0;
501 float xw = 0.0, xw_diff;
502 SDL_Surface *bitmap;
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));
511 xw_diff = 0.0;
512 x -= xw / 2;
515 if( animation & ANIMATION_PENDULUM) {
516 x -= (int)(20.0*sinf( start/20.0));
517 additional_x += 21;
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) {
528 x += (int)(xw_diff);
529 if( xw_diff > 1.0) {
530 xw_diff -= 1.0;
531 } else {
532 xw_diff += 1.0;
537 if (animation & ANIMATION_WAVE) {
538 additional_y += 4;
540 if (animation & ANIMATION_BUNGEE) {
541 additional_x += 26;
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) {
547 int w = 0, i;
549 for( i=0; i<strlen(s); i++) {
550 w += font_get_metrics( id, s[i], NULL, NULL);
553 return w;
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;
558 int i;
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++) {
567 x += dx;
568 y += dy;
569 if( x < 0.0 || x >= WIDTH || y < 0.0 || y >= HEIGHT) {
570 continue;
572 if( fade) {
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);
574 } else {
575 SET_PIXEL_RGB( screen, (int)x, (int)y, r, g, b);
578 SDL_UnlockSurface( screen);