2011-05-06, 05:41 PM
Something's wrong with my code. Any ideas what it might be?
Spoiler
/*************************
Project: Space Eater //
1047110, Any Munoz //
1042271. Javier Chavez //
Team: Black Magic //
*************************/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <glut.h>
#include <Windows.h>
#include <winuser.h>
/* Control */
#define SOLID 1
/* Stuff that are constants */
#define GAP 30
#define MILISECONDS 10
#define Z -20.0
#define MACH 0.01
/* Stuff that affects the windows */
GLuint mainWindow, screenSub;
GLuint subWidth = 256, subHeight = 256;
/* Stuff that affects the hero */
GLfloat heroX = 0.0, heroY = 0.0, heroSpeed = 0.1, heroRadius = 1.0;
GLfloat heroGreen = 1.0, heroLife = 1.0;
GLenum movingLeft = GL_FALSE, movingRight = GL_FALSE, movingUp = GL_FALSE, movingDown = GL_FALSE;
/* Stuff that affects the fruit */
GLfloat fruitX, fruitY, fruitRadius = 0.4;
GLenum fruitExists = GL_FALSE;
/* Stuff that affects the score */
GLint heroScore = -10;
GLfloat scoreX = -8.5, scoreY = 10.8;
/* Stuff that's a box */
GLfloat minX = -7.5, maxX = 7.5, minY = -10, maxY = 10;
/* Other stuff */
GLfloat distance, getX, getY;
GLdouble projection[16], modelview[16];
GLvoid *font_style = GLUT_BITMAP_HELVETICA_18;
/* WASD */
void keyPressFunc(unsigned char Key, int x, int y){
switch(Key){
case 'A':
case 'a':
movingLeft = GL_TRUE;
movingRight = GL_FALSE;
movingUp = GL_FALSE;
movingDown = GL_FALSE;
break;
case 'D':
case 'd':
movingRight = GL_TRUE;
movingLeft = GL_FALSE;
movingUp = GL_FALSE;
movingDown = GL_FALSE;
break;
case 'W':
case 'w':
movingUp = GL_TRUE;
movingRight = GL_FALSE;
movingLeft = GL_FALSE;
movingDown = GL_FALSE;
break;
case 'S':
case 's':
movingDown = GL_TRUE;
movingRight = GL_FALSE;
movingUp = GL_FALSE;
movingLeft = GL_FALSE;
break;
case 27:
exit(1);
}
}
/* Magic */
void printw(float x, float y, float z, char* format, ...){
va_list args;
int len;
int i;
char * text;
va_start(args, format);
len = _vscprintf(format, args) + 1;
text = malloc(len *sizeof(char));
vsprintf_s(text, len, format, args);
va_end(args);
glRasterPos3f(x, y, z);
for(i = 0; text[i] != '\0'; i++){
glutBitmapCharacter(font_style, text[i]);
}
free(text);
}
void displayScore(void){
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
printw(scoreX, scoreY, Z, "%d", heroScore);
glPopMatrix();
}
/* Crashes with limit */
void collisionBox(void){
if(heroX >= maxX){
if(heroLife > 0){
heroLife -= MACH;
}
}
if(heroX <= minX){
if(heroLife > 0){
heroLife -= MACH;
}
}
if(heroY >= maxY){
if(heroLife > 0){
heroLife -= MACH;
}
}
if(heroY <= minY){
if(heroLife > 0){
heroLife -= MACH;
}
}
}
/* Crashes with fruit */
void collisionFruit(void){
getX = heroX - fruitX;
getY = heroY - fruitY;
distance = sqrt((getX * getX) + (getY * getY));
}
void heroDraw(void){
if(heroLife > 1.0)
{
heroLife = 1.0;
}
heroGreen = heroLife;
glPushMatrix();
glTranslatef(heroX, heroY, Z);
glColor3f(1.0, heroGreen, 0.0);
/* Determine solid or wire */
if(SOLID){
glutSolidSphere(heroRadius, 30, 30);
}
else{
glutWireSphere(heroRadius, 30, 30);
}
glPopMatrix();
}
/* Gives fruit it's position */
void fruitBearer(void){
fruitX = rand() % 15 + minX;
fruitY = rand() % 20 + minY;
}
void fruitDraw(void){
glPushMatrix();
/* Shake the tree */
if(fruitExists == GL_FALSE){
fruitBearer();
fruitExists = GL_TRUE;
}
glColor3f(0.8, 0.0, 0.0);
glTranslatef(fruitX, fruitY, Z);
/* Crashing is true */
if(distance <= heroRadius + fruitRadius){
fruitBearer();
heroSpeed += MACH;
heroScore += 10;
/* Recover health */
if(heroLife < 1.0)
{
heroLife += MACH * 5;
}
}
/* Determine solid or wire */
if(SOLID){
glutSolidSphere(fruitRadius, 15, 15);
}
else{
glutWireSphere(fruitRadius, 15, 15);
}
glPopMatrix();
}
/* Outer window stuff */
void mainDisplay(void){
glClearColor(0.0, 0.0, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3ub(0, 0, 0); //Do I even need this?
glutSwapBuffers();
}
/* Do not touch */
void mainReshape(int width, int height){
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, height, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* Window resizing stuff */
subWidth = (width - GAP*3)/2.0;
subHeight = (height - GAP*3)/2.0;
glutSetWindow(screenSub);
glutPositionWindow(GAP, GAP);
glutReshapeWindow(subWidth + GAP + subWidth, subHeight + GAP + subHeight);
}
/* Insert object modifactions here */
void screenDisplay(void){
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Collisions */
collisionFruit();
collisionBox();
/* Draw stuff */
displayScore();
fruitDraw();
heroDraw();
glutSwapBuffers();
/* Direction control */
if(movingLeft && heroX > minX){
heroX -= heroSpeed;
}
if(movingRight && heroX < maxX){
heroX += heroSpeed;
}
if(movingUp && heroY < maxY){
heroY += heroSpeed;
}
if(movingDown && heroY > minY){
heroY -= heroSpeed;
}
/* Game over message */
if(heroLife <= 0){
MessageBox(NULL,L"GAME OVER.",L"GAME OVER",MB_OK | MB_ICONEXCLAMATION);
restarAll();
//exit(1);
}
glFlush();
}
void restarAll(void){
heroX = 0.0; heroY = 0.0; heroSpeed = 0.1; heroGreen = 1.0; heroLife = 1.0;
heroScore = 0;
}
void screenReshape(int width, int height){
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float)width/height, 0.5, 20.0);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
}
/* Al, God of Redisplaying! */
void redisplayAll(void){
glutSetWindow(screenSub);
glutPostRedisplay();
}
/* Chronos, God of Time! */
void chronos(int value){
redisplayAll();
glutTimerFunc(MILISECONDS, chronos, 0);
}
/* Why hello there, main */
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(480+GAP*3, 640+GAP*3);
glutInitWindowPosition(100, 70);
/* Main window */
mainWindow = glutCreateWindow("Space Eater");
glutDisplayFunc(mainDisplay);
glutReshapeFunc(mainReshape);
glutKeyboardFunc(keyPressFunc);
/* Real window */
screenSub = glutCreateSubWindow(mainWindow, GAP, GAP, 256, 256);
glutReshapeFunc(screenReshape);
glutDisplayFunc(screenDisplay);
/* Pringles */
glutTimerFunc(0, chronos, 0);
redisplayAll();
glutMainLoop();
return(0);
}
/*************************
Project: Space Eater //
1047110, Any Munoz //
1042271. Javier Chavez //
Team: Black Magic //
*************************/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <glut.h>
#include <Windows.h>
#include <winuser.h>
/* Control */
#define SOLID 1
/* Stuff that are constants */
#define GAP 30
#define MILISECONDS 10
#define Z -20.0
#define MACH 0.01
/* Stuff that affects the windows */
GLuint mainWindow, screenSub;
GLuint subWidth = 256, subHeight = 256;
/* Stuff that affects the hero */
GLfloat heroX = 0.0, heroY = 0.0, heroSpeed = 0.1, heroRadius = 1.0;
GLfloat heroGreen = 1.0, heroLife = 1.0;
GLenum movingLeft = GL_FALSE, movingRight = GL_FALSE, movingUp = GL_FALSE, movingDown = GL_FALSE;
/* Stuff that affects the fruit */
GLfloat fruitX, fruitY, fruitRadius = 0.4;
GLenum fruitExists = GL_FALSE;
/* Stuff that affects the score */
GLint heroScore = -10;
GLfloat scoreX = -8.5, scoreY = 10.8;
/* Stuff that's a box */
GLfloat minX = -7.5, maxX = 7.5, minY = -10, maxY = 10;
/* Other stuff */
GLfloat distance, getX, getY;
GLdouble projection[16], modelview[16];
GLvoid *font_style = GLUT_BITMAP_HELVETICA_18;
/* WASD */
void keyPressFunc(unsigned char Key, int x, int y){
switch(Key){
case 'A':
case 'a':
movingLeft = GL_TRUE;
movingRight = GL_FALSE;
movingUp = GL_FALSE;
movingDown = GL_FALSE;
break;
case 'D':
case 'd':
movingRight = GL_TRUE;
movingLeft = GL_FALSE;
movingUp = GL_FALSE;
movingDown = GL_FALSE;
break;
case 'W':
case 'w':
movingUp = GL_TRUE;
movingRight = GL_FALSE;
movingLeft = GL_FALSE;
movingDown = GL_FALSE;
break;
case 'S':
case 's':
movingDown = GL_TRUE;
movingRight = GL_FALSE;
movingUp = GL_FALSE;
movingLeft = GL_FALSE;
break;
case 27:
exit(1);
}
}
/* Magic */
void printw(float x, float y, float z, char* format, ...){
va_list args;
int len;
int i;
char * text;
va_start(args, format);
len = _vscprintf(format, args) + 1;
text = malloc(len *sizeof(char));
vsprintf_s(text, len, format, args);
va_end(args);
glRasterPos3f(x, y, z);
for(i = 0; text[i] != '\0'; i++){
glutBitmapCharacter(font_style, text[i]);
}
free(text);
}
void displayScore(void){
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
printw(scoreX, scoreY, Z, "%d", heroScore);
glPopMatrix();
}
/* Crashes with limit */
void collisionBox(void){
if(heroX >= maxX){
if(heroLife > 0){
heroLife -= MACH;
}
}
if(heroX <= minX){
if(heroLife > 0){
heroLife -= MACH;
}
}
if(heroY >= maxY){
if(heroLife > 0){
heroLife -= MACH;
}
}
if(heroY <= minY){
if(heroLife > 0){
heroLife -= MACH;
}
}
}
/* Crashes with fruit */
void collisionFruit(void){
getX = heroX - fruitX;
getY = heroY - fruitY;
distance = sqrt((getX * getX) + (getY * getY));
}
void heroDraw(void){
if(heroLife > 1.0)
{
heroLife = 1.0;
}
heroGreen = heroLife;
glPushMatrix();
glTranslatef(heroX, heroY, Z);
glColor3f(1.0, heroGreen, 0.0);
/* Determine solid or wire */
if(SOLID){
glutSolidSphere(heroRadius, 30, 30);
}
else{
glutWireSphere(heroRadius, 30, 30);
}
glPopMatrix();
}
/* Gives fruit it's position */
void fruitBearer(void){
fruitX = rand() % 15 + minX;
fruitY = rand() % 20 + minY;
}
void fruitDraw(void){
glPushMatrix();
/* Shake the tree */
if(fruitExists == GL_FALSE){
fruitBearer();
fruitExists = GL_TRUE;
}
glColor3f(0.8, 0.0, 0.0);
glTranslatef(fruitX, fruitY, Z);
/* Crashing is true */
if(distance <= heroRadius + fruitRadius){
fruitBearer();
heroSpeed += MACH;
heroScore += 10;
/* Recover health */
if(heroLife < 1.0)
{
heroLife += MACH * 5;
}
}
/* Determine solid or wire */
if(SOLID){
glutSolidSphere(fruitRadius, 15, 15);
}
else{
glutWireSphere(fruitRadius, 15, 15);
}
glPopMatrix();
}
/* Outer window stuff */
void mainDisplay(void){
glClearColor(0.0, 0.0, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3ub(0, 0, 0); //Do I even need this?
glutSwapBuffers();
}
/* Do not touch */
void mainReshape(int width, int height){
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, height, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* Window resizing stuff */
subWidth = (width - GAP*3)/2.0;
subHeight = (height - GAP*3)/2.0;
glutSetWindow(screenSub);
glutPositionWindow(GAP, GAP);
glutReshapeWindow(subWidth + GAP + subWidth, subHeight + GAP + subHeight);
}
/* Insert object modifactions here */
void screenDisplay(void){
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Collisions */
collisionFruit();
collisionBox();
/* Draw stuff */
displayScore();
fruitDraw();
heroDraw();
glutSwapBuffers();
/* Direction control */
if(movingLeft && heroX > minX){
heroX -= heroSpeed;
}
if(movingRight && heroX < maxX){
heroX += heroSpeed;
}
if(movingUp && heroY < maxY){
heroY += heroSpeed;
}
if(movingDown && heroY > minY){
heroY -= heroSpeed;
}
/* Game over message */
if(heroLife <= 0){
MessageBox(NULL,L"GAME OVER.",L"GAME OVER",MB_OK | MB_ICONEXCLAMATION);
restarAll();
//exit(1);
}
glFlush();
}
void restarAll(void){
heroX = 0.0; heroY = 0.0; heroSpeed = 0.1; heroGreen = 1.0; heroLife = 1.0;
heroScore = 0;
}
void screenReshape(int width, int height){
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float)width/height, 0.5, 20.0);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
}
/* Al, God of Redisplaying! */
void redisplayAll(void){
glutSetWindow(screenSub);
glutPostRedisplay();
}
/* Chronos, God of Time! */
void chronos(int value){
redisplayAll();
glutTimerFunc(MILISECONDS, chronos, 0);
}
/* Why hello there, main */
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(480+GAP*3, 640+GAP*3);
glutInitWindowPosition(100, 70);
/* Main window */
mainWindow = glutCreateWindow("Space Eater");
glutDisplayFunc(mainDisplay);
glutReshapeFunc(mainReshape);
glutKeyboardFunc(keyPressFunc);
/* Real window */
screenSub = glutCreateSubWindow(mainWindow, GAP, GAP, 256, 256);
glutReshapeFunc(screenReshape);
glutDisplayFunc(screenDisplay);
/* Pringles */
glutTimerFunc(0, chronos, 0);
redisplayAll();
glutMainLoop();
return(0);
}

