initial import of tennix SDL port
[tennix.git] / game.c
blob11a2cb2929ab7896590381a0a43033013ddc0e2d
1 #include <stdio.h>
2 #include <math.h>
4 #include <SDL/SDL.h>
6 #include "tennix.h"
7 #include "game.h"
8 #include "graphics.h"
9 #include "input.h"
11 typedef struct {
12 float x;
13 float y;
14 float move_x;
15 float move_y;
16 } Ball;
18 typedef struct {
19 float x;
20 float y;
21 unsigned char state;
22 unsigned int score;
23 } Player;
25 void game() {
26 int i, w;
27 int werbung[4];
29 Ball ball = { GAME_X_MID, GAME_Y_MID, 2.0, 0.2 };
30 Player player1 = { GAME_X_MIN-4, GAME_Y_MID, 0, 0 };
31 Player player2 = { GAME_X_MAX, GAME_Y_MID, 0, 0 };
33 Uint8 *keys;
34 SDL_Event e;
36 for( w=0; w<4; w++) {
37 werbung[w] = rand()%SPONSOR_COUNT;
40 clearscr();
41 updatescr();
42 wait_keypress();
44 while( 1) {
45 clearscr();
46 show_bmp( "data/court.bmp", 0, 0, 255);
48 for( w=0; w<4; w++) {
49 show_sprite( SPONSOR_BMP, werbung[w], SPONSOR_COUNT, 60+50*w+5*(w>1), 7, 255);
52 show_sprite( RACKET_BMP, (!player1.state), 4, player1.x, player1.y, 255);
53 show_sprite( RACKET_BMP, (!player2.state)+2, 4, player2.x, player2.y, 255);
55 show_bmp( "data/ball.bmp", ball.x, ball.y, 255);
57 show_bmp( "data/standings.bmp", 115, 160, 255);
58 show_digit( player1.score/10%10, 140, 180, 100);
59 show_digit( player1.score%10, 148, 180, 100);
60 show_digit( 10, 156, 180, 100);
61 show_digit( player2.score/10%10, 164, 180, 100);
62 show_digit( player2.score%10, 172, 180, 100);
64 updatescr();
66 if( i%2) {
67 if( IS_OUT_X(ball.x) || IS_OUT_Y(ball.y)) {
68 if( (IS_OUT_Y(ball.y) && ball.move_y > 0) || (!(player1.state && IS_NEAR_Y(player1.y, ball.y)) && ball.x < GAME_X_MIN)) {
69 player2.score++;
70 ball.x = GAME_X_MID;
71 ball.y = GAME_Y_MID;
72 ball.move_x = fabsf( ball.move_x);
73 ball.move_y = -0.1;
74 wait_keypress();
77 if( (IS_OUT_Y(ball.y) && ball.move_y < 0) || (!(player2.state && IS_NEAR_Y(player2.y, ball.y)) && ball.x > GAME_X_MAX)) {
78 player1.score++;
79 ball.x = GAME_X_MID;
80 ball.y = GAME_Y_MID;
81 ball.move_x = -fabsf( ball.move_x);
82 ball.move_y = 0.1;
83 wait_keypress();
86 if( IS_OUT_X(ball.x)) {
87 if( ball.move_x < 0) {
88 ball.x = GAME_X_MIN;
89 ball.move_x = (2.0+(2.0*(rand()%10))/10.0);
90 ball.move_y = GET_MOVE(player1.y,ball.y);
91 } else {
92 ball.x = GAME_X_MAX;
93 ball.move_x = -(2.0+(2.0*(rand()%10))/10.0);
94 ball.move_y = GET_MOVE(player2.y,ball.y);
99 ball.x += ball.move_x;
100 ball.y += ball.move_y;
102 if( player1.state) player1.state--;
103 if( player2.state) player2.state--;
106 limit_value( &player1.y, GAME_Y_MIN, GAME_Y_MAX);
107 limit_value( &player2.y, GAME_Y_MIN, GAME_Y_MAX);
109 SDL_PollEvent( &e);
110 keys = SDL_GetKeyState( NULL);
112 if( keys['2']) player1.y-=3;
113 if( keys['w']) player1.y-=2;
114 if( keys['s']) player1.y+=2;
115 if( keys['x']) player1.y+=3;
116 if( keys['d']) player1.state = 10;
118 if( keys['9']) player2.y-=3;
119 if( keys['o']) player2.y-=2;
120 if( keys['l']) player2.y+=2;
121 if( keys['.']) player2.y+=3;
122 if( keys['k']) player2.state = 10;
124 if( keys['f']) SDL_WM_ToggleFullScreen( screen);
126 if( keys[SDLK_ESCAPE] || keys['q']) break;
129 SDL_Delay( 10);
130 i++;
135 void limit_value( float* value, float min, float max) {
136 if( *value < min) {
137 *value = min;
138 } else if( *value > max) {
139 *value = max;