Sign in to follow this  
phil67rpg

space wars

Recommended Posts

well I have rewritten my code to include two space ships using opengl and c++.
I want them to work independtly of each other.

#include <stdlib.h>
#include <glut.h>

static GLint w = 800;
static GLint h = 600;

// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(-0.0f,-0.25f,0.0f);
glEnd();

glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(1.0f,-0.25f,0.0f);
glVertex3f(0.75f,-0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(1.0f,0.5f,0.0f);
glVertex3f(1.5f,-0.5f,0.0f);
glVertex3f(1.25f,-0.5f,0.0f);
glVertex3f(1.0f,-0.25f,0.0f);
glEnd();
glutSwapBuffers();
}
void RenderSceneLeft()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(-0.0f,-0.25f,0.0f);
glEnd();
glRotatef(1.0,0.0,0.0,1.0);
glutSwapBuffers();
}
void RenderSceneRight()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(-0.0f,-0.25f,0.0f);
glEnd();
glRotatef(-1.0,0.0,0.0,1.0);
glutSwapBuffers();
}
void RenderSceneUp()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(-0.0f,-0.25f,0.0f);
glEnd();
glTranslatef(0.0f,0.1f,0.0f);
glutSwapBuffers();
}
void RenderSceneDown()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(-0.0f,-0.25f,0.0f);
glEnd();
glTranslatef(0.0f,-0.1f,0.0f);
glutSwapBuffers();
}
void RenderSceneTwoUp()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(1.0f,-0.25f,0.0f);
glVertex3f(0.75f,-0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(1.0f,0.5f,0.0f);
glVertex3f(1.5f,-0.5f,0.0f);
glVertex3f(1.25f,-0.5f,0.0f);
glVertex3f(1.0f,-0.25f,0.0f);
glEnd();
glTranslatef(0.0f,0.1f,0.0f);
glutSwapBuffers();
}
void RenderSceneTwoDown()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(1.0f,-0.25f,0.0f);
glVertex3f(0.75f,-0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(1.0f,0.5f,0.0f);
glVertex3f(1.5f,-0.5f,0.0f);
glVertex3f(1.25f,-0.5f,0.0f);
glVertex3f(1.0f,-0.25f,0.0f);
glEnd();
glTranslatef(0.0f,-0.1f,0.0f);
glutSwapBuffers();
}
void RenderSceneTwoLeft()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(1.0f,-0.25f,0.0f);
glVertex3f(0.75f,-0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(1.0f,0.5f,0.0f);
glVertex3f(1.5f,-0.5f,0.0f);
glVertex3f(1.25f,-0.5f,0.0f);
glVertex3f(1.0f,-0.25f,0.0f);
glEnd();
glRotatef(1.0,0.0,0.0,1.0);
glutSwapBuffers();
}
void RenderSceneTwoRight()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(1.0f,-0.25f,0.0f);
glVertex3f(0.75f,-0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(1.0f,0.5f,0.0f);
glVertex3f(1.5f,-0.5f,0.0f);
glVertex3f(1.25f,-0.5f,0.0f);
glVertex3f(1.0f,-0.25f,0.0f);
glEnd();
glRotatef(-1.0,0.0,0.0,1.0);
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0f,10.0f,-10.0f,10.0f,-1.0f,1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Setup the rendering state
void SetupRC(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glShadeModel(GL_FLAT);
}

void mySpecialKeys(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
{
RenderSceneLeft();
break;
}
case GLUT_KEY_RIGHT:
{
RenderSceneRight();
break;
}
case GLUT_KEY_UP:
{
RenderSceneUp();
break;
}
case GLUT_KEY_DOWN:
{
RenderSceneDown();
break;
}
default:
break;
}

}

void myKeyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
{
RenderSceneTwoLeft();
break;
}
case 'd':
{
RenderSceneTwoRight();
break;
}
case 'w':
{
RenderSceneTwoUp();
break;
}
case 'x':
{
RenderSceneTwoDown();
break;
}
case 27:
exit(0);
break;
}

}

// Main program entry point
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,600);
glutCreateWindow("Asteroids");
glutDisplayFunc(RenderScene);
glutReshapeFunc(reshape);
glutKeyboardFunc(myKeyboard);
glutSpecialFunc(mySpecialKeys);
SetupRC();
glutMainLoop();
}


any tips on how to be a better coder would be greatly appreciated.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this