Reputation: 163
I am playing around with SDL2 on Windows. I am trying to get the resize event like this.
#include "SDL/SDL.h"
#include <iostream>
int main(int argc, char** argv) {
SDL_Window* window = SDL_CreateWindow("title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,800, 500, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
while (true) {
SDL_Event sdl_event;
if (SDL_WaitEvent(&sdl_event)) {
if (sdl_event.key.repeat == 0) { // no repeat event
switch (sdl_event.type) {
case SDL_QUIT:
{
break;
}
case SDL_WINDOWEVENT:
{
if (sdl_event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED){
std::cout << sdl_event.window.data1 << " " << sdl_event.window.data2 << "\n";
}
break;
}
}
}
}
}
return 0;
}
But I am not getting the event when I try to resize the window by dragger the border. However, I do get the event when I use the maximize button. What is wrong?
Upvotes: 4
Views: 462
Reputation: 52085
ZhuYaDong's SDL_AddEventWatch()
approach works on this Windows 10 box:
#include <SDL.h>
#include <iostream>
#include <thread>
int resizingEventWatcher( void* data, SDL_Event* event )
{
SDL_Window* window = static_cast< SDL_Window* >( data );
SDL_Event& ev = *event;
if( ev.type == SDL_WINDOWEVENT &&
ev.window.event == SDL_WINDOWEVENT_RESIZED )
{
if( SDL_GetWindowFromID( ev.window.windowID ) == window )
{
std::cout << std::this_thread::get_id() << ": " << ev.window.data1 << " " << ev.window.data2 << std::endl;
}
}
return 0;
}
int main( int argc, char** argv )
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_Window* window = SDL_CreateWindow
(
"title",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
800, 500,
SDL_WINDOW_RESIZABLE
);
SDL_AddEventWatch( resizingEventWatcher, window );
bool running = true;
while( running )
{
SDL_Event ev;
while( SDL_PollEvent( &ev ) )
{
switch( ev.type )
{
case SDL_QUIT:
running = false;
break;
case SDL_WINDOWEVENT:
if( ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED )
{
std::cout << std::this_thread::get_id() << ": "<< ev.window.data1 << " " << ev.window.data2 << std::endl;
}
break;
}
}
}
return 0;
}
If you want to get fancy you can create a user event in EDIT: Sadly these user events seem to just get queued up during resizing and don't make it to the main event-handling loop until you stop sizing the window:resizingEventWatcher()
& post it to the main event queue with SDL_PushEvent()
.
#include <SDL.h>
#include <iostream>
#include <thread>
Uint32 ResizingEvent = 0;
int resizingEventWatcher( void* data, SDL_Event* event )
{
SDL_Event& ev = *event;
if( ev.type == SDL_WINDOWEVENT &&
ev.window.event == SDL_WINDOWEVENT_RESIZED )
{
SDL_Event newEvent = { 0 };
newEvent.type = ResizingEvent;
newEvent.user.windowID = ev.window.windowID;
newEvent.user.data1 = reinterpret_cast< void* >( ev.window.data1 );
newEvent.user.data2 = reinterpret_cast< void* >( ev.window.data2 );
SDL_PushEvent( &newEvent );
}
return 0;
}
int main( int argc, char** argv )
{
SDL_Init( SDL_INIT_EVERYTHING );
ResizingEvent = SDL_RegisterEvents( 1 );
SDL_Window* window = SDL_CreateWindow
(
"title",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
800, 500,
SDL_WINDOW_RESIZABLE
);
SDL_AddEventWatch( resizingEventWatcher, window );
bool running = true;
while( running )
{
SDL_Event ev;
while( SDL_PollEvent( &ev ) )
{
switch( ev.type )
{
case SDL_QUIT:
running = false;
break;
case SDL_WINDOWEVENT:
if( ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED )
{
std::cout << std::this_thread::get_id() << ": "<< ev.window.data1 << " " << ev.window.data2 << std::endl;
}
break;
case SDL_USEREVENT:
if( ev.user.type == ResizingEvent )
{
std::cout << std::this_thread::get_id() << ": "<< reinterpret_cast< Sint32 >( ev.user.data1 ) << " " << reinterpret_cast< Sint32 >( ev.user.data2 ) << std::endl;
}
break;
}
}
}
return 0;
}
(Using SDL's release-2.0.10
tag)
Upvotes: 2