Also add PNG files to binary release
[tennix.git] / graphics.c
blob459692498a115e816073233f445a2644d7d92df5
1 #include <stdio.h>
3 #include <SDL/SDL.h>
4 #include <SDL/SDL_image.h>
6 #include "tennix.h"
7 #include "graphics.h"
8 #include "input.h"
10 static Image* images;
12 static const char* filenames[] = {
13 "data/court.bmp",
14 "data/shadow.png",
15 "data/player-racket.bmp",
16 "data/ground.png",
17 "data/ball.png",
18 "data/score.png",
19 "data/menu.bmp",
20 "data/animation.bmp"
23 void init_graphics() {
24 int i;
25 SDL_Surface* data;
27 images = (Image*)calloc( GR_COUNT, sizeof( Image));
29 for( i=0; i<GR_COUNT; i++) {
30 data = IMG_Load( filenames[i]);
31 if( !data) {
32 fprintf( stderr, "Error: %s\n", SDL_GetError());
33 continue;
35 images[i].data = data;
39 void uninit_graphics() {
40 int i;
42 for( i=0; i<GR_COUNT; i++) {
43 SDL_FreeSurface( images[i].data);
46 free( images);
49 void show_sprite( unsigned int id, int pos, int items, int x_offset, int y_offset, int opacity) {
50 SDL_Surface *bitmap;
51 SDL_Rect src, dst;
53 bitmap = images[id].data;
55 if( !bitmap) return;
57 SDL_SetAlpha( bitmap, SDL_SRCALPHA | SDL_RLEACCEL, opacity);
58 SDL_SetColorKey( bitmap, SDL_SRCCOLORKEY, SDL_MapRGB( bitmap->format, 0, 0, 0));
60 dst.w = src.w = bitmap->w/items;
61 dst.h = src.h = bitmap->h;
62 src.x = src.w*pos;
63 src.y = 0;
64 dst.x = x_offset;
65 dst.y = y_offset;
67 SDL_BlitSurface( bitmap, &src, screen, &dst);
70 void show_image( unsigned int id, int x_offset, int y_offset, int opacity) {
71 show_sprite( id, 0, 1, x_offset, y_offset, opacity);
74 void show_digit( int zahl, int x_offset, int y_offset, int opacity) {
75 show_sprite( GR_SCORE, zahl, 11, x_offset, y_offset, opacity);
78 void introimage( unsigned int id) {
79 int i;
81 for( i=0; i<256; i+=10) {
82 clearscr();
83 show_image( id, 0, 0, i);
84 updatescr();
85 SDL_Delay( 30);
88 //SDL_Delay( 500);
89 wait_keypress();
91 for( i=255; i>=0; i-=10) {
92 clearscr();
93 show_image( id, 0, 0, i);
94 updatescr();
95 SDL_Delay( 30);
98 clearscr();
101 void clearscr() {
102 SDL_Rect rect;
104 rect.x = 0;
105 rect.y = 0;
106 rect.w = WIDTH;
107 rect.h = HEIGHT;
109 SDL_FillRect( screen, &rect, SDL_MapRGB( screen->format, 0, 0, 0));
112 void updatescr() {
113 SDL_UpdateRect( screen, 0, 0, 0, 0);