Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

space wars

This topic is 2843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!