Added application window icon setup code
[tennix.git] / graphics.c
blobb3b35aebed8235ea8f10da91ba4b13a71680964c
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007 Thomas Perl <thp@perli.net>
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>
26 #include "tennix.h"
27 #include "graphics.h"
28 #include "input.h"
30 static Image* images;
32 static SDL_Surface *buffer;
34 static Uint32 fading_start = 0;
36 static const char* filenames[] = {
37 "court.png",
38 "shadow.png",
39 "player-racket.png",
40 "ground.png",
41 "ball.png",
42 "score.png",
43 "menu.png",
44 "animation.png"
47 void init_graphics( const char *data_dir) {
48 int i;
49 SDL_Surface* data;
50 SDL_Surface* tmp;
51 char temp[MAXPATHLEN];
53 strcpy( temp, data_dir);
54 strcat( temp, "icon.png");
55 tmp = IMG_Load( temp);
56 if( tmp != NULL) {
57 SDL_WM_SetIcon( tmp, NULL);
58 SDL_FreeSurface( tmp);
61 images = (Image*)calloc( GR_COUNT, sizeof( Image));
63 for( i=0; i<GR_COUNT; i++) {
64 strcpy( temp, data_dir);
65 strcat( temp, filenames[i]);
66 tmp = IMG_Load( temp);
67 if( !tmp) {
68 fprintf( stderr, "Error: %s\n", SDL_GetError());
69 continue;
72 SDL_SetColorKey( tmp, SDL_SRCCOLORKEY, SDL_MapRGB( tmp->format, 0, 0, 0));
73 data = SDL_DisplayFormatAlpha( tmp);
74 SDL_FreeSurface( tmp);
76 if( !data) {
77 fprintf( stderr, "Error: %s\n", SDL_GetError());
78 continue;
80 images[i].data = data;
83 buffer = SDL_CreateRGBSurface( SDL_HWSURFACE, WIDTH, HEIGHT, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
85 if( buffer == NULL) {
86 fprintf( stderr, "Cannot create buffer surface: %s\n", SDL_GetError());
90 void uninit_graphics() {
91 int i;
93 for( i=0; i<GR_COUNT; i++) {
94 SDL_FreeSurface( images[i].data);
97 if( buffer != NULL) {
98 SDL_FreeSurface( buffer);
101 free( images);
104 void show_sprite( unsigned int id, int pos, int items, int x_offset, int y_offset, int opacity) {
105 SDL_Surface *bitmap;
106 SDL_Rect src, dst;
108 bitmap = images[id].data;
110 if( !bitmap) return;
112 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
114 dst.w = src.w = bitmap->w/items;
115 dst.h = src.h = bitmap->h;
116 src.x = src.w*pos;
117 src.y = 0;
118 dst.x = x_offset;
119 dst.y = y_offset;
121 SDL_BlitSurface( bitmap, &src, screen, &dst);
124 void line_horiz( int y, Uint8 r, Uint8 g, Uint8 b) {
125 Uint32 color = SDL_MapRGB( screen->format, r, g, b);
126 SDL_Rect rect;
128 rect.x = 0;
129 rect.w = screen->w;
130 rect.y = y;
131 rect.h = 1;
133 SDL_FillRect( screen, &rect, color);
136 void line_vert( int x, Uint8 r, Uint8 g, Uint8 b) {
137 Uint32 color = SDL_MapRGB( screen->format, r, g, b);
138 SDL_Rect rect;
140 rect.x = x;
141 rect.w = 1;
142 rect.y = 0;
143 rect.h = screen->h;
145 SDL_FillRect( screen, &rect, color);
148 void rectangle( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b) {
149 Uint32 color = SDL_MapRGB( screen->format, r, g, b);
150 SDL_Rect rect;
152 rect.x = x;
153 rect.y = y;
154 rect.w = w;
155 rect.h = h;
157 SDL_FillRect( screen, &rect, color);
160 void show_image( unsigned int id, int x_offset, int y_offset, int opacity) {
161 show_sprite( id, 0, 1, x_offset, y_offset, opacity);
164 void show_digit( int zahl, int x_offset, int y_offset, int opacity) {
165 show_sprite( GR_SCORE, zahl, 11, x_offset, y_offset, opacity);
168 void introimage( unsigned int id) {
169 int i;
171 for( i=0; i<256; i+=10) {
172 clearscr();
173 show_image( id, 0, 0, i);
174 updatescr();
175 SDL_Delay( 30);
178 //SDL_Delay( 500);
179 wait_keypress();
181 for( i=255; i>=0; i-=10) {
182 clearscr();
183 show_image( id, 0, 0, i);
184 updatescr();
185 SDL_Delay( 30);
188 clearscr();
191 void clearscr() {
192 SDL_Rect rect;
194 rect.x = 0;
195 rect.y = 0;
196 rect.w = WIDTH;
197 rect.h = HEIGHT;
199 SDL_FillRect( screen, &rect, SDL_MapRGB( screen->format, 0, 0, 0));
202 void updatescr() {
203 int ticks = SDL_GetTicks();
205 if( ticks < fading_start+FADE_DURATION) {
206 SDL_SetAlpha( buffer, SDL_SRCALPHA | SDL_RLEACCEL, 255-255*(ticks-fading_start)/FADE_DURATION);
207 SDL_BlitSurface( buffer, NULL, screen, NULL);
210 SDL_UpdateRect( screen, 0, 0, 0, 0);
211 SDL_Flip( screen);
214 void start_fade() {
215 SDL_BlitSurface( screen, NULL, buffer, NULL);
216 fading_start = SDL_GetTicks();