summaryrefslogtreecommitdiff
path: root/ball.c
blob: e5a0c4e9dc43371a3e767ba6cbbbf7f45b346dc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "SDL.h"
#include "SDL_opengl.h"

#include "ball.h"

typedef struct {
	Coord_t coord;
	GLfloat w, h;
	GLfloat r, g, b;
	GLfloat xv, yv, zv;
	GLfloat rotate;
} Ball_t;

static Ball_t ball;
static GLuint ball_texture;

void
GLPong_BallDraw(void) {
	glLoadIdentity();
	glBindTexture(GL_TEXTURE_2D, ball_texture);
	glEnable(GL_TEXTURE_2D);
	glColor3f(ball.r, ball.g, ball.b);
	glTranslatef(ball.coord.x, ball.coord.y, ball.coord.z);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f); glVertex2f(ball.w, ball.h);	/* Lower Left */
		glTexCoord2f(1.0f, 1.0f); glVertex2f(0.0f, ball.h);		/* Lower Right */
		glTexCoord2f(1.0f, 0.0f); glVertex2f(0.0f, 0.0f);			/* Upper Right */
		glTexCoord2f(0.0f, 0.0f); glVertex2f(ball.w, 0.0f);		/* Upper Left */
	glEnd();
	glDisable(GL_TEXTURE_2D);

#ifdef DEBUG
	/* Lower Left */
	glLoadIdentity();
	glColor3f(1.0f, 0.0f, 0.0f);
	glTranslatef(ball.coord.x, ball.coord.y, ball.coord.z);
	glBegin(GL_POINTS);
		glVertex3f(0.0f, 0.0f, 0.0f);
	glEnd();

	/* Lower Right */
	glLoadIdentity();
	glTranslatef(ball.coord.x + ball.w, ball.coord.y, ball.coord.z);
	glBegin(GL_POINTS);
		glVertex3f(0.0f, 0.0f, 0.0f);
	glEnd();
	
	/* Top Right */
	glLoadIdentity();
	glTranslatef(ball.coord.x + ball.w, ball.coord.y + ball.h, ball.coord.z);
	glBegin(GL_POINTS);
		glVertex3f(0.0f, 0.0f, 0.0f);
	glEnd();
	/* Top Left */
	glLoadIdentity();
	glTranslatef(ball.coord.x, ball.coord.y + ball.h, ball.coord.z);
	glBegin(GL_POINTS);
		glVertex3f(0.0f, 0.0f, 0.0f);
	glEnd();
#endif
	
	glLoadIdentity();
	glTranslatef(0.0f, 0.0f, ball.coord.z);
	glCallList(box);
}

void
GLPong_BallInit(GLuint texture) {
	ball.w = 0.5f;
	ball.h = 0.5f;
	ball.coord.x = -(ball.w / 2);
	ball.coord.y = -(ball.h / 2);
	ball.coord.z = GLPONG_FRONT_Z;
	ball.r = 1.0f;
	ball.g = 0.0f;
	ball.b = 0.0f;
	ball.xv = 0.02f;
	ball.yv = 0.01f;
	ball.zv = -0.05f;

	ball_texture = texture;
}

void
GLPong_BallMove(void) {
	ball.coord.x += ball.xv;
	if (ball.coord.x > 1.0f) {
		ball.coord.x = 1.0f;
		ball.xv = -ball.xv;
	} else if (ball.coord.x  + ball.w < -1.0f) {
		ball.coord.x = -1.0f - ball.w;
		ball.xv = -ball.xv;
	}

	ball.coord.y += ball.yv;
	if (ball.coord.y > 0.5f) {
		ball.coord.y = 0.5f;
		ball.yv = -ball.yv;
	} else if (ball.coord.y + ball.h < -0.5f) {
		ball.coord.y = -0.5f - ball.h;
		ball.yv = -ball.yv;
	}

	ball.coord.z += ball.zv;
	if (ball.coord.z < GLPONG_BACK_Z) {
		ball.coord.z = GLPONG_BACK_Z;
		ball.zv = -ball.zv;
	} else if (ball.coord.z > GLPONG_FRONT_Z) {
		ball.coord.z = GLPONG_FRONT_Z;
		ball.zv = -ball.zv;
	}
}