gpt4 book ai didi

c++ - SDL_WINDOWEVENT_SIZE_CHANGED 拖动边框调整大小时不出现?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:01:53 27 4
gpt4 key购买 nike

我正在 Windows 上玩 SDL2。我正在尝试获取这样的调整大小事件。

#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;
}

但是当我尝试通过拖动边框来调整窗口大小时,我没有收到事件。但是,当我使用最大化按钮时,我确实得到了事件。怎么了?

最佳答案

ZhuYaDong's SDL_AddEventWatch() approach适用于此 Windows 10 机器:

#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;
}

如果您想要更有趣,您可以在 resizingEventWatcher() 中创建一个用户事件,并使用 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;
}

(使用 SDL 的 release-2.0.10 tag )

关于c++ - SDL_WINDOWEVENT_SIZE_CHANGED 拖动边框调整大小时不出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58279191/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com