Skip to main content
gamedev.net
🔒 Locked

SDL Issue

Started by reaptide Nov 21, 2003 at 2:42 AM 4 replies 650+ views
Original Post
reaptide
reaptide
Hey everyone, Alright SDL kicks ass. Getting my window setup and rendering my OpenGL code was a snap. I love it. One problem, I''m hogging the CPU. With my Win32 code I would check the WM_ACTIVATE message for WA_CLICKACTIVE and WA_ACTIVE. If they were present I would set a boolean to true. Everytime the program loops this boolean is checked, if its true everything goes as normal, if its false then I call WaitMessage() to let Windows have some play time. Worked wonderfully and let the user task out and work without mollassas for a system. How would I go about this with SDL. The Event system has got me kinda confused. I''ve worked out SDL_QUIT, but it is almost identical to WM_QUIT. I''ve tried polling SDL_GetAppState() and setting an active variable based on that, but it doesn''t seem to work too great and I''m sure this system would work better if integrated into the event system.
Last Attacker
Last Attacker
Just wondering, does WaitMessage allow you to pass an instance or something thorugh? If so you can pass your SDL instance var. through (Sorry but I can''t remember my Win32 API funcs. well, since I use it rearly).

HOPE THIS HELPS :D


// Last Attacker \\---=( LA )=---// LA Extreme \\

ICQ Number : 120585863

E-mail: laextr@icqmail.com

"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
reaptide
reaptide
Nope. It doesn''t. WaitMessage is defined as:

BOOL WaitMessage(void);

The MSDN reference is here.
LousyPhreak
LousyPhreak
what you are searching for is SDL_ACTIVEEVENT

assuming you just want to disable looping once you are ''out of focus''

WaitMessage() is also in sdl its called SDL_WaitEvent. the difference is though it returns an event on return

besides than that you should really read the docs they are (mostly) quite good...
http://mitglied.lycos.de/lousyphreak/
reaptide
reaptide
Thanks for the reply. Its actually with the SDL docs that I''m trying to work this out. I''ll post some code to illustrate what I''m trying to do.


//-------------------------------------------------------------------------------------------------

// CApp::Run()

//

// Description:

// This is the main game loop. As long as it keeps returning non-zero, things keep running.

//-------------------------------------------------------------------------------------------------

int CApp::Run()
{
// Begin main processing if the program window is in focus

if (m_Active == true)
{
// Check for any pending events and take care of them

// Returns zero if a SDL_QUIT event is recieved

if (!HandleEvents())
return(0);
}
else
{
// Stop processing until an event is recieved

SDL_WaitEvent(NULL);
}

return(1);
}

//-------------------------------------------------------------------------------------------------

// CApp::HandleEvents()

//

// Description:

// Takes care of SDL events.

//-------------------------------------------------------------------------------------------------

int CApp::HandleEvents()
{
// Get any queued events, return if there are none available

if (!SDL_PollEvent(&m_Event))
return(1);

// Check each recieved event

switch(m_Event.type)
{
case SDL_QUIT:
{
return(0); // Quit the program

} break;

case SDL_ACTIVEEVENT:
{
// Check if the program has lot keyboard focus

if (m_Event.active.state == SDL_APPINPUTFOCUS)
{
if (m_Event.active.gain == 0)
m_Active = false;

if (m_Event.active.gain == 1)
m_Active = true;
}
// Check if the program has lost window focus

else if (m_Event.active.state == SDL_APPACTIVE)
{
if (m_Event.active.gain == 0)
m_Active = false;

if (m_Event.active.gain == 1)
m_Active = true;
}
} break;

default: break;
}

// Finish processing events. Return to the main loop

return(1);
}


m_Active (bool) and m_Event (SDL_Event) are members of the CApp class. When I task out of the program once it works. When I minimize the program it doesn''t work. Any subsequent tries to pause processing after the first don''t work.

Any idea what I''m doing wrong here?
Greatwolf
Greatwolf
you got a bit of redundant code there. Like for example,


if (m_Active == true)

//more straight forward to just do

if(m_Active)


//your switch

switch(m_Event.type)
{
case SDL_QUIT:
{
return(0);// Quit the program

} break;
...

//what the heck you need break for? You're returning :p

//and you don't need break for the default either as that's

//the last option anyway



As for your problem, according to the docs, SDL_WaitEvent() stops your program until some kind of event is given. When you do get an event there, is it being handled so that m_Active gets updated appropriately? Like for example, does it get toggled to true if your program gains focus again?





--{You fight like a dairy farmer!}

[edited by - Greatwolf on November 22, 2003 3:01:39 AM]
--{You fight like a dairy farmer!}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.