New input selection UI + Mouse support, updated TODO list
[tennix.git] / graphics.c
blob76c3978b8c9f0fe36a53fd7d07ea241c2be539fd
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>
29 #include <ctype.h>
30 #include <string.h>
31 #include <assert.h>
33 #include "tennix.h"
34 #include "graphics.h"
35 #include "archive.h"
36 #include "sound.h"
38 #include "SDL_rotozoom.h"
40 static Image* images;
42 static Font* fonts;
43 FreeList* font_freelist = NULL;
45 static FontDescription font_desc[] = {
46 /**
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
50 * loader function.
51 **/
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[] = {
69 "court.png",
70 "shadow.png",
71 "player-racket.png",
72 "ball.png",
73 "referee.png",
74 "ctt_hard.png",
75 "ctt_clay.png",
76 "ctt_grass.png",
77 "sidebar.png",
78 "tennixlogo.png",
79 "btnplay.png",
80 "btnresume.png",
81 "btnquit.png",
82 "stadium.png",
83 "fog.png",
84 "fog2.png",
85 "night.png",
86 "talk.png",
87 "cursor.png",
88 "worldmap.png",
89 "input_gamepad.png",
90 "input_keyboard_arrows.png",
91 "input_keyboard_ol.png",
92 "input_keyboard_ws.png",
93 "input_maemo_dpad.png",
94 "input_mouse.png",
95 "input_touchscreen.png",
96 "input_ai.png",
97 "loc_margaret_court_arena.png",
98 "loc_stade_roland_garros.png",
99 "loc_court_no_1.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"
105 #endif
108 void init_graphics() {
109 int i;
110 char *d;
111 SDL_Surface* data;
112 SDL_Surface* tmp;
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());
119 exit(EXIT_FAILURE);
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);
149 if (tnxar == NULL) {
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);
172 store_screen();
173 updatescr();
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);
179 free(d);
180 } else {
181 fprintf(stderr, "Cannot find file: %s\n", graphics[i]);
182 exit(EXIT_FAILURE);
184 if( !tmp) {
185 fprintf( stderr, "Error: %s\n", SDL_GetError());
186 continue;
189 /* Convert to RGBA in the display format */
190 data = SDL_DisplayFormatAlpha(tmp);
191 SDL_FreeSurface(tmp);
193 if( !data) {
194 fprintf( stderr, "Error: %s\n", SDL_GetError());
195 continue;
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);
201 updatescr();
203 tnxar_close(tnxar);
206 void uninit_graphics() {
207 int i;
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);
223 free(images);
225 for (i=0; i<FONT_COUNT; i++) {
226 TTF_CloseFont(fonts[i].data);
227 fonts[i].data = NULL;
229 free(fonts);
231 freelist_free_all(font_freelist);
233 TTF_Quit();
237 FreeList* freelist_create()
239 FreeList* list = (FreeList*)malloc(sizeof(FreeList));
240 assert(list != NULL);
242 list->head = NULL;
244 return list;
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)
263 FreeListItem* next;
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);
271 free(list->head);
272 list->head = next;
275 free(list);
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)
301 SDL_Surface* tmp;
302 SDL_Rect src, dst;
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;
310 src.x = 0;
311 src.y = 0;
312 dst.x = x - src.w/2;
313 dst.y = y - src.h/2;
315 SDL_BlitSurface(tmp, &src, screen, &dst);
316 update_rect2(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) {
321 SDL_Surface *bitmap;
322 SDL_Rect src, dst;
323 int dx = 0, dy = 0, cx, cy;
325 if( id >= GR_COUNT) return;
327 bitmap = images[id].data;
329 /* Make negative offsets positive */
330 if (offset_x < 0) {
331 offset_x = (offset_x%bitmap->w)+bitmap->w;
333 if (offset_y < 0) {
334 offset_y = (offset_y%bitmap->h)+bitmap->h;
337 src.y = offset_y % bitmap->h;
338 while( dy < h) {
339 src.h = dst.h = cy = (h-dy > bitmap->h-src.y)?(bitmap->h-src.y):(h-dy);
340 dst.y = y+dy;
342 dx = 0;
343 src.x = offset_x % bitmap->w;
344 while( dx < w) {
345 src.w = dst.w = cx = (w-dx > bitmap->w-src.x)?(bitmap->w-src.x):(w-dx);
346 dst.x = x+dx;
348 SDL_BlitSurface( bitmap, &src, screen, &dst);
349 update_rect2(dst);
351 dx += cx;
352 src.x = 0;
355 dy += cy;
356 src.y = 0;
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);
370 SDL_Rect rect;
372 rect.x = x;
373 rect.y = y;
374 rect.w = w;
375 rect.h = h;
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);
384 SDL_Surface *buf;
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);
394 rect.x = rect.y = 0;
395 rect2.x = x;
396 rect2.y = y;
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) {
416 int font_x, font_y;
417 float factor = 0.3;
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));
425 void clear_screen()
427 SDL_Rect rect;
429 rect.x = rect.y = 0;
430 rect.w = WIDTH;
431 rect.h = HEIGHT;
433 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
436 void store_screen()
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;
446 void reset_screen()
448 int i;
449 SDL_Rect* tmp;
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)
466 SDL_Rect* u = NULL;
467 static int inside = 0;
468 if (inside) return;
469 inside = 1;
471 #ifdef DRAW_UPDATE_RECTANGLE
472 rectangle(x, y, w, h, 50+rand()%200, 50+rand()%200 ,50+rand()%200);
473 #endif
475 if ((x >= WIDTH) | (y >= HEIGHT) | (x+w <= 0) | (y+h <= 0) | (!w) | (!h)) {
476 inside = 0;
477 return;
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*/) {
488 w += x;
489 x = 0;
491 if (y < 0/* && y+h > 0*/) {
492 h += y;
493 y = 0;
496 if (x+w >= WIDTH) {
497 w -= (x+w-WIDTH+1);
500 if (y+h >= HEIGHT) {
501 h -= (y+h-HEIGHT+1);
504 if (w==0 || h==0) {
505 inside = 0;
506 return;
509 u->x = x;
510 u->y = y;
511 u->w = w;
512 u->h = h;
514 rect_update_cache_current++;
516 inside = 0;
519 void updatescr()
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;
528 if (fading_now) {
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);
534 } else {
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;
548 r_current++;
549 r_old++;
551 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
552 } else {
553 SDL_UpdateRects(screen, rect_update_old_count, rect_update_old);
554 SDL_UpdateRects(screen, rect_update_cache_current, rect_update_cache);
558 reset_screen();
559 fading_last_time = fading_now;
562 void start_fade() {
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,
568 Uint8 g, Uint8 b)
570 SDL_Surface *result;
571 SDL_Color color;
573 color.r = r;
574 color.g = g;
575 color.b = b;
577 assert(id < FONT_COUNT);
579 result = TTF_RenderText_Blended(fonts[id].data, text, color);
581 assert(result != NULL);
583 return result;
586 void blit_surface(SDL_Surface* surface, int x, int y, int pos, int count)
588 SDL_Rect src, dst;
590 dst.w = src.w = surface->w/count;
591 dst.h = src.h = surface->h;
592 src.x = src.w*pos;
593 src.y = 0;
594 dst.x = x;
595 dst.y = y;
597 SDL_BlitSurface(surface, &src, screen, &dst);
598 update_rect2(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) {
608 SDL_Surface *text;
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) {
616 int w, h;
618 assert(id < FONT_COUNT);
620 if (TTF_SizeText(fonts[id].data, s, &w, &h) != 0) {
621 return 0;
624 return w;
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;
634 int i;
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++) {
643 x += dx;
644 y += dy;
645 if( x < 0.0 || x >= WIDTH || y < 0.0 || y >= HEIGHT) {
646 continue;
648 if( fade) {
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);
650 } else {
651 SET_PIXEL_RGB( screen, (int)x, (int)y, r, g, b);
654 SDL_UnlockSurface( screen);