Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / Win32

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

Rate me:
Please Sign up or sign in to vote.
4.56/5 (9 votes)
2 Jun 2010CPOL3 min read 41.9K   1.2K   28  
A Star Wars Scroller Demo that moves smoothly from the bottom to the top in OpenGL (GLUT)
#include <windows.h>
#include <string.h>
#include <GL\glut.h>
#include <iostream.h>
#include <fstream.h>


//#pragma comment(lib, "OpenGL32.lib")
//#pragma comment(lib, "GLu32.lib")
//#pragma comment(lib, "GLaux.lib")
//#pragma comment(lib, "GDI32.LIB")
//#pragma comment(lib, "Kernel32.LIB")
//#pragma comment(lib, "User32.LIB")
//#pragma comment(lib, "Winmm.lib")

GLfloat UpwardsScrollVelocity = -10.0;
float view=20.0;

char quote[200][80];
int numberOfQuotes=0,i;

//*********************************************
//*  glutIdleFunc(timeTick);                  *
//*********************************************

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

}

//*********************************************
//* loadFile(char *file)                   *
//*********************************************


void loadFile(char *file)
{
	char buffer[256];
	ifstream inFile;
   inFile.open(file);
	if (!inFile.is_open())
	{
		cout << "\nError opening file :"<<file;
		exit(1);
	}
	while (!(inFile.eof()))
	{
		inFile.getline(buffer,80);
		strcpy(quote[i],buffer);
		i++;
	}
	inFile.close();
	numberOfQuotes=i;
}
//*********************************************
//* printToConsoleWindow()                *
//*********************************************

void printToConsoleWindow()
{
	int l,lenghOfQuote, i;

	for(  l=0;l<numberOfQuotes;l++)
	{
		lenghOfQuote = (int)strlen(quote[l]);

		for (i = 0; i < lenghOfQuote; i++)
		{
		  cout<<quote[l][i];
		}
          cout<<endl;
	}

}

//*********************************************
//* RenderToDisplay()                       *
//*********************************************

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();
	}

}
//*********************************************
//* glutDisplayFunc(myDisplayFunction);       *
//*********************************************

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();
}
//*********************************************
//* glutReshapeFunc(reshape);               *
//*********************************************

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);
}

//*********************************************
//* int main()                                *
//*********************************************


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();
//    disp();
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Sweden Sweden
About me:
I attended programming college and I have a degree in three most famous and successful programming languages. C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
.
I am a professional, I am paid tons of cash to teach or do software development. I am roughly 30 years old .

I hold lectures in programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.

I've written hundreds of thousands of code syntax lines for small simple applications and games.

Comments and Discussions