Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

A Star Wars Scroller Demo written in OpenGL (GLUT) using Visual C++ 6.0

4.56/5 (9 votes)
2 Jun 2010CPOL3 min read 43K   1.2K  
A Star Wars Scroller Demo that moves smoothly from the bottom to the top in OpenGL (GLUT)
Image 1

A Star Wars Scroller Closing Credits Demo
By TopCoder23 and Marcus the Snowman Bergquist

I have always been a big fan of the Star Wars movie franchise, and I've always been fascinated by the credits that roll from bottom to top at the end of movies and always wanted to replicate the same in an application.
In movies and in television, closing credits or end credits are added at the end to list the cast and crew involved in the production.
They usually appear as a list of names in small type, which move smoothly from the bottom to the top.

This simple app demonstrates how to create a simple Star Wars Scroller that moves smoothly from the bottom to the top using the OpenGL (GLUT) API in a Win32 console with Visual C++ 6.0.
Since we are using OpenGL (GLUT) API, we must include the header file glut.h in our .cpp file
and if you do not already have GLUT installed on your system, visit http://www.xmission.com/~nate/glut.html to download the latest GLUT binaries.

How to Install Glut

  • Unpack the contents of the archive glut-3.7.6-bin.zip.
  • Copy glut.lib to the \Microsoft Visual Studio\VCxx\Lib directory
  • Copy glut.h to the \Microsoft Visual Studio\VCxx\Include\GL directory
  • Copy glut32.dll to the \WINDOWS\system32 directory

Let's Go

We are going to load a set of famous Darth Vader quotes from a text file using ifstream into a char buffer variable named char quote[200][80], and then render the quotes on the screen using the built in fonts that are in the OpenGL API with the function glutStrokeCharacter().
We load the famous Darth Vader quotes from a text file into a char buffer with loadFile(), a function we've created.

GLUT Callback Functions

In GLUT, things are done by "you" creating functions and having GLUT make function a callback to those functions.
For example, a function to display objects would have the syntax:

C++
glutDisplayFunc( myDisplayFunction );

We will create three functions and put them in GLUT callback functions in order for GLUT to process them. The GLUT callback functions we will use are:

  • glutDisplayFunc();
  • glutReshapeFunc();
  • glutIdleFunc();

glutDisplayFunc is the display callback function for the StarWars scroller window. Similarly glutReshapeFunc is the reshape callback for the StarWars scroller window. The reshape callback function is triggered when a window is reshaped. glutIdleFunc sets the global idle callback to be func so a GLUT program can perform background processing tasks or continuous animation when window system events are not being received.

If enabled, the idle callback is continuously called when events are not being received. glutMainLoop enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered. This callback function should therefore be placed last.

Our three functions are:

  • RenderToDisplay()
  • myDisplayFunction() and
  • timeTick()

The RenderToDisplay() Function

The RenderToDisplay() callback function renders each of the characters of the quotes in our Star Wars Scroller in the chosen font.

C++
void RenderToDisplay()
{
	int l,lenghOfQuote, i;

	glTranslatef(0.0, -100, UpwardsScrollVelocity);
	glRotatef(-20, 1.0, 0.0, 0.0);
	glScalef(0.1, 0.1, 0.1);

	for(  l=0; l < numberOfQuotes;l++)
	{
		lenghOfQuote = (int)strlen(quote[l]);
		glPushMatrix();
		glTranslatef(-(lenghOfQuote*37), -(l*200), 0.0);
		for (i = 0; i < lenghOfQuote; i++)
		{
			glColor3f((UpwardsScrollVelocity/10)+300+(l*10),
				(UpwardsScrollVelocity/10)+300+(l*10),0.0);
			glutStrokeCharacter(GLUT_STROKE_ROMAN, quote[l][i]);
		}
		glPopMatrix();
	}
}

The myDisplayFunction()

The myDisplayFunction(void) callback function is called upon to create a viewing matrix derived from an eye point, a reference point indicating the center of the scene, and an UP vector so that our Star Wars Scroller looks like the one in the movies.

C++
void myDisplayFunction(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glLoadIdentity();
  gluLookAt(0.0, 30.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  RenderToDisplay();
  glutSwapBuffers();
}  

The reshape() Function

The reshape() callback function is called upon when a window is reshaped.

C++
void reshape(int w, int h)
{
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(60, 1.0, 1.0, 3200);
  glMatrixMode(GL_MODELVIEW);
}    

The timeTick(void) Function

The timeTick(void) function is called upon to process continuous animation which in our case is to slowly scroll each of the quotes in our Star Wars Scroller from bottom to top.

C++
void timeTick(void)
{
   if (UpwardsScrollVelocity< -400) view-=0.000001;
   if(view < 0) 
   {
      view=20; 
      UpwardsScrollVelocity = -10.0;
    }
    UpwardsScrollVelocity -= 0.015;
    glutPostRedisplay();
}  

The Main Function

Finally the main is where everything comes together. We load a set of famous Darth Vader quotes from a text file into a char buffer, initiate the GLUT display mode, initiate the size of the window, then create the window and give it a title, and then we make function callbacks to the functions we created, and have GLUT loop our Star Wars Scroller.

C++
int main()
{
    loadFile("captions.txt");
	  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	  glutInitWindowSize(800, 400);
	  glutCreateWindow("StarWars scroller");
	  glClearColor(0.0, 0.0, 0.0, 1.0);
	  glLineWidth(3);

	  glutDisplayFunc(myDisplayFunction);
	  glutReshapeFunc(reshape);
	  glutIdleFunc(timeTick);
	  glutMainLoop();
	return 0;
}

And that is how easy it is to create a Star Wars Scroller using GLUT in a Win32 Console.

Image 2

Thanks for reading.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)