防止 SDL 程序消耗额外资源 [英] Prevent SDL program from consuming extra resources

查看:43
本文介绍了防止 SDL 程序消耗额外资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计应该在图像上展示开放简历的程序.我注意到基本 SDL 应用程序的概念非常糟糕——它由循环和延迟组成.

I'm designing program that should demonstrate open CV on images. I've noticed very bad concept of basic SDL application - it consists of loop and delay.

while(true) {
    while(event_is_in_buffer(event)) {
        process_event(event);
    }
    do_some_other_stuff();
    do_some_delay(100);       //Program is stuck here, unable to respond to user input
}

这使得程序即使在后台执行和渲染(或者如果首先不需要重新渲染).如果我使用更长的延迟,我消耗的资源更少,但我必须等待更长的时间才能处理鼠标单击等事件.
我想要的是让程序等待事件,就像 WinApi 那样或像套接字请求那样.这可能吗?
我想要的概念:

This makes the program execute and render even if it is on background (or if re-rendering is not necessary in the first place). If I use longer delay, I get less consumed resources but I must wait longer before events, like mouse click, are processed.
What I want is to make program wait for events, like WinApi does or like socket requests do. Is that possible?
Concept I want:

bool go=true;
while(get_event(event)&&go) {  //Program gets stuck here if no events happen
    switch(event.type){
       case QUIT: go=false;
    }
}

推荐答案

您可以使用 SDL_WaitEvent(SDL_Event *event) 等待 SDL 中的事件.与您目前拥有的轮询循环设计相比,它将使用更少的资源.请参阅此 doc 中的示例:

You can use SDL_WaitEvent(SDL_Event *event) to wait for an event in the SDL. It will use less resources than the polling loop design you currently have. See the example in this doc:

{
    SDL_Event event;

    while ( SDL_WaitEvent(&event) ) {
        switch (event.type) {
                ...
                ...
        }
    }
}

这篇关于防止 SDL 程序消耗额外资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆