Advertisement

Why is the SDLQuit function not working

Started by July 29, 2020 02:35 PM
1 comment, last by RulerOfNothing 4 years, 3 months ago

Hi this is my first attempt at messing around with open gl and SDL2.0 to do some graphics test programs that I plan to add music to when I get better at it.

Why is

	if( e.type == SDL_QUIT )
					{
						quit = true;
					

	//Free resources and close SDL
	close();

not responding to kill the window when I click the x.

Thanks.

Entire code

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <string>

// -lSDL2 -lGLU -lglut -lGL

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//Starts up SDL and creates window
bool init();

//Frees media and shuts down SDL
void close();

//The window we'll be rendering to
SDL_Window* gWindow = NULL;

//The open gl square
float glSquareblk();
float glSquarerd();
float glSquaregrn();
float glSquarebl();

//Opengl context
SDL_GLContext gContext;

bool init()
{
	//Initialization flag
	bool success = true;

	//Initialize SDL
	if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
	{
		printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
		success = false;
	}
	else
	{
		//Create window
		gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL |  SDL_WINDOW_SHOWN );
		if( gWindow == NULL )
		{
			printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
			success = false;
		}
		else
		{
		
		}
	}

	return success;
}

void close()
{
	//Destroy window
	SDL_DestroyWindow( gWindow );
	gWindow = NULL;

	//Quit SDL subsystems
	SDL_Quit();
}

float glSquareblk()
{
	glClearColor(0.0,0.0,0.0,1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,1.0,1.0);
	glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
	glBegin(GL_QUADS);
		glVertex2f(-0.5,-0.5);
		glVertex2f(-0.5,0.5);
		glVertex2f(0.5,0.5);
		glVertex2f(0.5,-0.5);
		glEnd();
		glFlush();
}


float glSquarerd()
{
	glClearColor(1.0,0.0,0.0,1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,1.0,1.0);
	glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
	glBegin(GL_QUADS);
		glVertex2f(-0.5,-0.5);
		glVertex2f(-0.5,0.5);
		glVertex2f(0.5,0.5);
		glVertex2f(0.5,-0.5);
		glEnd();
		glFlush();
}


float glSquaregrn()
{
	glClearColor(0.0,1.0,0.0,1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,1.0,1.0);
	glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
	glBegin(GL_QUADS);
		glVertex2f(-0.5,-0.5);
		glVertex2f(-0.5,0.5);
		glVertex2f(0.5,0.5);
		glVertex2f(0.5,-0.5);
		glEnd();
		glFlush();
}


float glSquarebl()
{
	glClearColor(0.0,0.0,1.0,1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,1.0,1.0);
	glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
	glBegin(GL_QUADS);
		glVertex2f(-0.5,-0.5);
		glVertex2f(-0.5,0.5);
		glVertex2f(0.5,0.5);
		glVertex2f(0.5,-0.5);
		glEnd();
		glFlush();
}

int main( int argc, char* args[] )
{
	//Start up SDL and create window
	if( !init() )
	{
		printf( "Failed to initialize!\n" );
	}
	else
	{		
			//Main loop flag
			bool quit = false;
			
			gContext = SDL_GL_CreateContext( gWindow );
			
			//Event handler
			SDL_Event e;

			//While application is running
			while( !quit )
			{
				//Handle events on queue
				while( SDL_PollEvent( &e ) != 0 )
				{
					glSquareblk();
					SDL_Delay(3000);
					SDL_GL_SwapWindow( gWindow );
					
					glSquarerd();
					SDL_Delay(3000);
					SDL_GL_SwapWindow( gWindow );
					
					glSquaregrn();
					SDL_Delay(3000);
					SDL_GL_SwapWindow( gWindow );
					
					glSquarebl();
					SDL_Delay(3000);
					SDL_GL_SwapWindow( gWindow );
					
					//User requests quit
					if( e.type == SDL_QUIT )
					{
						quit = true;
					

	//Free resources and close SDL
	close();

	return 0;
}
}
}
}
}

Because of the SDL_Delay(3000) statements in the event loop, (that occur between when you call SDL_PollEvent and when you actually check the event) it would take about 12 seconds to respond to a quit event (assuming it is not already responding to another event).

This topic is closed to new replies.

Advertisement