[SDL] SDL_PollEvent freezes on alt-tab

lemmn dwlemon at gmail.com
Mon Sep 19 09:57:05 PDT 2016


Heres a slightly simpler repro:


Code:

#include "stdafx.h"

#include <vector>
#include <map>
#include <fstream>
#include <memory>
#include <limits>
#include <iomanip>
#include <thread>

#include <Windows.h>

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>

#include "MyMusic.h"


bool quitGame;


// window and screen
SDL_Window* m_Window;
SDL_GLContext m_Context;
SDL_Renderer* m_Renderer = nullptr;
std::thread* m_RenderThread = nullptr;


static bool stopRenderer;

void RenderThread()
{
	SDL_GL_MakeCurrent( m_Window, m_Context ); // not sure what this does - it was happening in the example

	m_Renderer = SDL_CreateRenderer( m_Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
	SDL_SetRenderDrawBlendMode( m_Renderer, SDL_BLENDMODE_BLEND );

	while( 1 )
	{
		if ( stopRenderer )
		{
			goto quit;
		}

		SDL_Rect rect{ 10, 10, 10, 10 };
		SDL_SetRenderDrawColor( m_Renderer, 0xff, 0xff, 0xff, 0xff );
		SDL_RenderFillRect( m_Renderer, &rect );
		SDL_RenderPresent( m_Renderer );
	}

quit:
	// shut down
	SDL_DestroyRenderer( m_Renderer );
	m_Renderer = NULL;
}

int main( int argc, char* argv[] )
{
	IMG_Init( IMG_INIT_PNG );

	TTF_Init();


	SDL_Init( SDL_INIT_AUDIO | SDL_INIT_VIDEO );

	SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "linear" );

	SDL_Rect windowedRect;

	// make a sane default window position and size
	if ( 0 == SDL_GetDisplayBounds( 0, &windowedRect ) )
	{
		windowedRect.x = windowedRect.w * 0.05f;
		windowedRect.y = windowedRect.h * 0.05f;
		windowedRect.w = windowedRect.w * 0.9f;
		windowedRect.h = windowedRect.h * 0.9f;
	}
	else
	{
		windowedRect = { 10, 10, 1024, 768 };
	}

	int32_t flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
	flags |= SDL_WINDOW_FULLSCREEN;

	SDL_Rect resolution;
	resolution = windowedRect;

	m_Window = SDL_CreateWindow( "hi friend", resolution.x, resolution.y, resolution.w, resolution.h, flags );



	// not entirely sure this is needed but it was happening before creating the render thread in example
	m_Context = SDL_GL_GetCurrentContext();
	SDL_GL_MakeCurrent( m_Window, nullptr );


	// start the renderer
	m_RenderThread = new std::thread( RenderThread );


	// main loopy thing
	while ( 1 )
	{
		if ( quitGame )
		{
			break;
		}

		SDL_Event e;
		while ( SDL_PollEvent( &e ) )
		{
			if ( e.type == SDL_KEYDOWN )
			{
				if ( e.key.keysym.sym == SDLK_SPACE )
				{
					quitGame = true;
				}
			}
		}

		Sleep( 10 );
	}


	// kill the renderer thread
	stopRenderer = true;
	m_RenderThread->join();
	delete m_RenderThread;
	m_RenderThread = nullptr;


	// desroy the window
	SDL_DestroyWindow( m_Window );
	m_Window = NULL;


	SDL_Quit();
	return 0;
}








-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20160919/8fa9fb3a/attachment-0001.htm>


More information about the SDL mailing list