use pngs instead of bmps
[tennix.git] / graphics.c
blobff29435a6eec39506f620249a1d2091246351c6c
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 const char* filenames[] = {
33 "court.png",
34 "shadow.png",
35 "player-racket.png",
36 "ground.png",
37 "ball.png",
38 "score.png",
39 "menu.png",
40 "animation.png"
43 void init_graphics( const char *data_dir) {
44 int i;
45 SDL_Surface* data;
46 SDL_Surface* tmp;
47 char temp[MAXPATHLEN];
49 images = (Image*)calloc( GR_COUNT, sizeof( Image));
51 for( i=0; i<GR_COUNT; i++) {
52 strcpy( temp, data_dir);
53 strcat( temp, filenames[i]);
54 tmp = IMG_Load( temp);
55 if( !tmp) {
56 fprintf( stderr, "Error: %s\n", SDL_GetError());
57 continue;
60 SDL_SetColorKey( tmp, SDL_SRCCOLORKEY, SDL_MapRGB( tmp->format, 0, 0, 0));
61 data = SDL_DisplayFormatAlpha( tmp);
62 SDL_FreeSurface( tmp);
64 if( !data) {
65 fprintf( stderr, "Error: %s\n", SDL_GetError());
66 continue;
68 images[i].data = data;
72 void uninit_graphics() {
73 int i;
75 for( i=0; i<GR_COUNT; i++) {
76 SDL_FreeSurface( images[i].data);
79 free( images);
82 void show_sprite( unsigned int id, int pos, int items, int x_offset, int y_offset, int opacity) {
83 SDL_Surface *bitmap;
84 SDL_Rect src, dst;
86 bitmap = images[id].data;
88 if( !bitmap) return;
90 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
92 dst.w = src.w = bitmap->w/items;
93 dst.h = src.h = bitmap->h;
94 src.x = src.w*pos;
95 src.y = 0;
96 dst.x = x_offset;
97 dst.y = y_offset;
99 SDL_BlitSurface( bitmap, &src, screen, &dst);
102 void show_image( unsigned int id, int x_offset, int y_offset, int opacity) {
103 show_sprite( id, 0, 1, x_offset, y_offset, opacity);
106 void show_digit( int zahl, int x_offset, int y_offset, int opacity) {
107 show_sprite( GR_SCORE, zahl, 11, x_offset, y_offset, opacity);
110 void introimage( unsigned int id) {
111 int i;
113 for( i=0; i<256; i+=10) {
114 clearscr();
115 show_image( id, 0, 0, i);
116 updatescr();
117 SDL_Delay( 30);
120 //SDL_Delay( 500);
121 wait_keypress();
123 for( i=255; i>=0; i-=10) {
124 clearscr();
125 show_image( id, 0, 0, i);
126 updatescr();
127 SDL_Delay( 30);
130 clearscr();
133 void clearscr() {
134 SDL_Rect rect;
136 rect.x = 0;
137 rect.y = 0;
138 rect.w = WIDTH;
139 rect.h = HEIGHT;
141 SDL_FillRect( screen, &rect, SDL_MapRGB( screen->format, 0, 0, 0));
144 void updatescr() {
145 SDL_UpdateRect( screen, 0, 0, 0, 0);
146 SDL_Flip( screen);