Move release information to makefile, update win32 target
[tennix.git] / graphics.c
blob0f6aa2da6d5ab4a799d1a6d8b465d9735ffe589e
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 images = (Image*)calloc( GR_COUNT, sizeof( Image));
55 for( i=0; i<GR_COUNT; i++) {
56 strcpy( temp, data_dir);
57 strcat( temp, filenames[i]);
58 tmp = IMG_Load( temp);
59 if( !tmp) {
60 fprintf( stderr, "Error: %s\n", SDL_GetError());
61 continue;
64 SDL_SetColorKey( tmp, SDL_SRCCOLORKEY, SDL_MapRGB( tmp->format, 0, 0, 0));
65 data = SDL_DisplayFormatAlpha( tmp);
66 SDL_FreeSurface( tmp);
68 if( !data) {
69 fprintf( stderr, "Error: %s\n", SDL_GetError());
70 continue;
72 images[i].data = data;
75 buffer = SDL_CreateRGBSurface( SDL_HWSURFACE, WIDTH, HEIGHT, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
77 if( buffer == NULL) {
78 fprintf( stderr, "Cannot create buffer surface: %s\n", SDL_GetError());
82 void uninit_graphics() {
83 int i;
85 for( i=0; i<GR_COUNT; i++) {
86 SDL_FreeSurface( images[i].data);
89 if( buffer != NULL) {
90 SDL_FreeSurface( buffer);
93 free( images);
96 void show_sprite( unsigned int id, int pos, int items, int x_offset, int y_offset, int opacity) {
97 SDL_Surface *bitmap;
98 SDL_Rect src, dst;
100 bitmap = images[id].data;
102 if( !bitmap) return;
104 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
106 dst.w = src.w = bitmap->w/items;
107 dst.h = src.h = bitmap->h;
108 src.x = src.w*pos;
109 src.y = 0;
110 dst.x = x_offset;
111 dst.y = y_offset;
113 SDL_BlitSurface( bitmap, &src, screen, &dst);
116 void line_horiz( int y, Uint8 r, Uint8 g, Uint8 b) {
117 Uint32 color = SDL_MapRGB( screen->format, r, g, b);
118 SDL_Rect rect;
120 rect.x = 0;
121 rect.w = screen->w;
122 rect.y = y;
123 rect.h = 1;
125 SDL_FillRect( screen, &rect, color);
128 void line_vert( int x, Uint8 r, Uint8 g, Uint8 b) {
129 Uint32 color = SDL_MapRGB( screen->format, r, g, b);
130 SDL_Rect rect;
132 rect.x = x;
133 rect.w = 1;
134 rect.y = 0;
135 rect.h = screen->h;
137 SDL_FillRect( screen, &rect, color);
140 void rectangle( int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b) {
141 Uint32 color = SDL_MapRGB( screen->format, r, g, b);
142 SDL_Rect rect;
144 rect.x = x;
145 rect.y = y;
146 rect.w = w;
147 rect.h = h;
149 SDL_FillRect( screen, &rect, color);
152 void show_image( unsigned int id, int x_offset, int y_offset, int opacity) {
153 show_sprite( id, 0, 1, x_offset, y_offset, opacity);
156 void show_digit( int zahl, int x_offset, int y_offset, int opacity) {
157 show_sprite( GR_SCORE, zahl, 11, x_offset, y_offset, opacity);
160 void introimage( unsigned int id) {
161 int i;
163 for( i=0; i<256; i+=10) {
164 clearscr();
165 show_image( id, 0, 0, i);
166 updatescr();
167 SDL_Delay( 30);
170 //SDL_Delay( 500);
171 wait_keypress();
173 for( i=255; i>=0; i-=10) {
174 clearscr();
175 show_image( id, 0, 0, i);
176 updatescr();
177 SDL_Delay( 30);
180 clearscr();
183 void clearscr() {
184 SDL_Rect rect;
186 rect.x = 0;
187 rect.y = 0;
188 rect.w = WIDTH;
189 rect.h = HEIGHT;
191 SDL_FillRect( screen, &rect, SDL_MapRGB( screen->format, 0, 0, 0));
194 void updatescr() {
195 int ticks = SDL_GetTicks();
197 if( ticks < fading_start+FADE_DURATION) {
198 SDL_SetAlpha( buffer, SDL_SRCALPHA | SDL_RLEACCEL, 255-255*(ticks-fading_start)/FADE_DURATION);
199 SDL_BlitSurface( buffer, NULL, screen, NULL);
202 SDL_UpdateRect( screen, 0, 0, 0, 0);
203 SDL_Flip( screen);
206 void start_fade() {
207 SDL_BlitSurface( screen, NULL, buffer, NULL);
208 fading_start = SDL_GetTicks();