mirror of
https://github.com/ScrelliCopter/Lesson10-SDL3.git
synced 2025-02-21 07:19:26 +11:00
simpler event loop
This commit is contained in:
43
Lesson10.c
43
Lesson10.c
@@ -421,34 +421,42 @@ bool CreateGLWindow(char *title, int width, int height, int bits, bool fullscree
|
||||
return true; // Success
|
||||
}
|
||||
|
||||
void WndProc(SDL_Event *uMsg)
|
||||
|
||||
bool done = false; // Bool Variable To Exit Loop
|
||||
|
||||
void WndProc(SDL_Event *event)
|
||||
{
|
||||
switch (uMsg->type) // Check For Windows Messages
|
||||
switch (event->type) // Check For Windows Messages
|
||||
{
|
||||
case SDL_EVENT_QUIT: // Have We Received A Quit Message?
|
||||
{
|
||||
done = true; // If So done=TRUE
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_EVENT_KEY_DOWN: // Is A Key Being Held Down?
|
||||
{
|
||||
keys[uMsg->key.keysym.scancode] = true; // If So, Mark It As TRUE
|
||||
keys[event->key.keysym.scancode] = true; // If So, Mark It As TRUE
|
||||
break; // Jump Back
|
||||
}
|
||||
|
||||
case SDL_EVENT_KEY_UP: // Has A Key Been Released?
|
||||
{
|
||||
keys[uMsg->key.keysym.scancode] = false; // If So, Mark It As FALSE
|
||||
keys[event->key.keysym.scancode] = false; // If So, Mark It As FALSE
|
||||
break; // Jump Back
|
||||
}
|
||||
|
||||
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: // Resize The OpenGL Window
|
||||
{
|
||||
ReSizeGLScene(uMsg->window.data1, uMsg->window.data2); // LoWord=Width, HiWord=Height
|
||||
ReSizeGLScene(event->window.data1, event->window.data2); // data1=Width, data2=Height
|
||||
break; // Jump Back
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Event msg; // Windows Message Structure
|
||||
bool done = false; // Bool Variable To Exit Loop
|
||||
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
|
||||
@@ -478,26 +486,19 @@ int main(int argc, char *argv[])
|
||||
|
||||
while (!done) // Loop That Runs While done=FALSE
|
||||
{
|
||||
if (SDL_PollEvent(&msg) > 0) // Is There A Message Waiting?
|
||||
SDL_Event event; // SDL Event Structure
|
||||
while (SDL_PollEvent(&event) > 0) // Is There A Message Waiting?
|
||||
{
|
||||
if (msg.type == SDL_EVENT_QUIT) // Have We Received A Quit Message?
|
||||
{
|
||||
done = true; // If So done=TRUE
|
||||
WndProc(&event); // Deal with events
|
||||
}
|
||||
else // If Not, Deal With Window Messages
|
||||
{
|
||||
WndProc(&msg);
|
||||
}
|
||||
}
|
||||
else // If There Are No Messages
|
||||
{
|
||||
|
||||
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
|
||||
if ((active && !DrawGLScene()) || keys[SDL_SCANCODE_ESCAPE]) // Active? Was There A Quit Received?
|
||||
{
|
||||
done = true; // ESC or DrawGLScene Signalled A Quit
|
||||
break;
|
||||
}
|
||||
else // Not Time To Quit, Update Screen
|
||||
{
|
||||
|
||||
SDL_GL_SwapWindow(win); // Swap Buffers (Double Buffering)
|
||||
if (keys[SDL_SCANCODE_B] && !bpressed)
|
||||
{
|
||||
@@ -609,8 +610,6 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown
|
||||
FreeResources();
|
||||
|
||||
Reference in New Issue
Block a user