All pastes #808860 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-12-07 23:33 UTC
rendered paste body
#include "glWindow.h"

 //The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface *screen = NULL;

//The event structure that will be used
SDL_Event event;
SDL_Thread *eventThread = NULL;

SDL_Thread *paintThread = NULL;
 
GLWindow* glWindow=NULL;

bool quit;

int main()
{
	glWindow=new GLWindow();
	glWindow->runGame();
	return 0;
} 

GLWindow::GLWindow()
{
	if(init())
	{
		printf("Error in init");
	}
}

void GLWindow::runPaint()
{
	while(!quit)
	{
		paintFrame();
		SDL_Delay( 5 );
	}
}

void GLWindow::paintFrame()
{
	//glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	
	/*glBegin( GL_QUADS );
		glColor4f( 1.0, 1.0, 1.0, 1.0 );
		glVertex3f( 20,20,1 );
		glVertex3f( 20,80,1 );
		glVertex3f( 80,80,1 );
		glVertex3f( 80,20,1 );
	glEnd();*/
    //Start quad
	
	printf(glGetError()+"");
		glBegin( GL_QUADS );
    
		printf(glGetError()+"");
        //Set color to white
		glColor4f( 1.0, 1.0, 1.0, 1.0 );
           
		printf(glGetError()+"");
        //Draw square
		glVertex3f( 20,            0,             0 );
		glVertex3f( 80, 0,             0 );
		glVertex3f( 80, 80, 0 );
		glVertex3f( 20,            80, 0 );
	    
		printf(glGetError()+"");
    //End quad   
		glEnd();
		printf(glGetError()+"");
	
		//SDL_GL_SwapBuffers();
		printf(glGetError()+"");
}

int runListener(void* data)
{
	glWindow->runGame();
}

int paintListener(void* data)
{
	glWindow->runPaint();
}

void GLWindow::runGame()
{
	while(!quit)
	{
		runStep();
		SDL_Delay( 10 );
	}
}

void GLWindow::runStep()
{
	
}

int eventListener( void *data )
{
    //While the program is not over
	while( true )
	{
		        //While there's an event to handle
		while( SDL_PollEvent( &event ) )
		{

			
			//If the user has Xed out the window
			if( event.type == SDL_QUIT )
			{
				printf("chansu");
				quit=true;
			}    
		}
		SDL_Delay( 10 );            
	}
    
	return 0;    
}

int GLWindow::init()
{
	    //Initialize all SDL subsystems
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
	{
		return 1;    
	}
	
	//SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
	//Set up the screen
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL );
	
	//If there was an error in setting up the screen
	if( screen == NULL )
	{
		return 1;
	}    
	
	glClearColor( 0, 0, 0, 0 );
	
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
	
    //Initialize modelview matrix
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
	
	if( glGetError() != GL_NO_ERROR )
	{
		return 1;    
	}
    //Set the window caption
	SDL_WM_SetCaption( "Hello World", NULL );
	
	//Create and run the thread
	eventThread = SDL_CreateThread( eventListener, NULL );
	paintThread = SDL_CreateThread( paintListener, NULL );

	return 0;
}

void GLWindow::cleanUp()
{	
	//Quit SDL
	SDL_Quit();
}