【问题标题】:SDL2 + OpenGL = Gnome 3 falsely complains "Program not responding"SDL2 + OpenGL = Gnome 3 错误地抱怨“程序没有响应”
【发布时间】:2014-12-05 06:38:33
【问题描述】:

目前我有一个分成两个循环的程序。第一个是在主线程中,除了每秒打印几次“d00d”之外,现在并没有真正做任何事情。 第二个是在 SDL2 线程上运行的循环,它在 OpenGL 中渲染帧并将它们显示在 SDL2 窗口中。

tldr;线程 1:程序循环,线程 2:渲染循环

几秒钟后,Gnome 3 抱怨程序没有响应,即使它继续渲染/更新正常。虽然我会注意到由于某种原因移动窗口是一个很大的紧张/滞后。

我还没有在 SDL2 中设置任何输入处理,我只是通过命令行终止程序。我不认为这会导致这样一个奇怪的问题,但我想我会提到它。

以与我使用 SDL2/SDL2-Thread/OpenGl 相同的方式使用 std::thread/glut/OpenGl 不会导致我现在遇到的任何问题,所以我觉得我做错了什么SDL2.

这是整个源代码:

Main.cpp

#include <iostream>

#include <stdio.h>
#include <string>

#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <SDL2/SDL_opengl.h>
#include <GL/glu.h>
#include <GL/gl.h>

#include "SDL2/SDL_thread.h"
#include "SDL2/SDL_timer.h"

#include <curl/curl.h>

#include "res_path.h"

using namespace std;

void testCurl(string f); size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream);
bool initRT(); int initSDL(); bool initGL(); int renderMain(void *ptr); void render(); void close();
float random(float max);

SDL_Window* gWindow = NULL;
SDL_GLContext gContext;

bool run = true;
int main(int argc, char **argv) {
    //testCurl("http://magiccards.info/scans/en/vma/4.jpg");

    if(!initRT()) { std::cout << "Failed to start RT!\n"; return 1; }

    while(run) {
        std::cout << "d00d!\n";
        SDL_Delay(33);
    }

    close();

    return 0;
}

//Render Thread
bool initRT() {
    SDL_Thread *thread;
    thread = SDL_CreateThread(renderMain, "RenderThread", (void *)NULL);

    if (NULL == thread) {
        printf("\nSDL_CreateThread failed: %s\n", SDL_GetError());
        return false;
    }
    return true;
}

const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480;
int initSDL() {
    //Initialization flag
    bool success = true;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
        success = false;
    }
    else
    {
        //Use OpenGL 3.1 core
        SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
        SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
        SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );

        //Create window
        gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
        if( gWindow == NULL )
        {
            printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
            success = false;
        }
        else
        {
            //Create context
            gContext = SDL_GL_CreateContext( gWindow );
            if( gContext == NULL )
            {
                printf( "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError() );
                success = false;
            }
            else
            {
                //Initialize GLEW
                glewExperimental = GL_TRUE;
                GLenum glewError = glewInit();
                if( glewError != GLEW_OK )
                {
                    printf( "Error initializing GLEW! %s\n", glewGetErrorString( glewError ) );
                }

                //Use Vsync
                if( SDL_GL_SetSwapInterval( 1 ) < 0 )
                {
                    printf( "Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError() );
                }

                //Initialize OpenGL
                if( !initGL() )
                {
                    printf( "Unable to initialize OpenGL!\n" );
                    success = false;
                }
            }
        }
    }

    return success ? 1 : 0;
}


bool initGL() {
    //Success flag
    bool success = true;
    //Not dealing with this right now. Call me when you grow a fragment~
    return success;
}

int renderMain(void *ptr) {

    if(!initSDL()) { std::cout << "Failed to start SDL!\n"; return 1; }

    while(run) {
        std::cout << "br0!\n";
        //Render quad
        render();

        //Update screen
        SDL_GL_SwapWindow( gWindow );
        SDL_Delay(3);
    }

    return 0;
}


void close() {
    //Destroy window
    SDL_DestroyWindow( gWindow );
    gWindow = NULL;

    //Quit SDL subsystems
    SDL_Quit();
}

void render() {
    glClearColor(0.7, 0.7, 0.7, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glLoadIdentity();
    glOrtho(-3.0f,3.0f,-3.0f,3.0f,0.0f,1.0f);

    glBegin(GL_LINES);
        glColor4f(random(1.0f),random(1.0f),random(1.0f),random(1.0f));
        glVertex2f(1.0f,1.0f);
        glColor4f(random(1.0f),random(1.0f),random(1.0f),random(1.0f));
        glVertex2f(1.0f,-1.0f);
        glColor4f(random(1.0f),random(1.0f),random(1.0f),random(1.0f));
        glVertex2f(1.0f,-1.0f);
        glColor4f(random(1.0f),random(1.0f),random(1.0f),random(1.0f));
        glVertex2f(-1.0f,-1.0f);
        glColor4f(random(1.0f),random(1.0f),random(1.0f),random(1.0f));
        glVertex2f(-1.0f,-1.0f);
        glColor4f(random(1.0f),random(1.0f),random(1.0f),random(1.0f));
        glVertex2f(-1.0f,1.0f);
        glColor4f(random(1.0f),random(1.0f),random(1.0f),random(1.0f));
        glVertex2f(-1.0f,1.0f);
        glColor4f(random(1.0f),random(1.0f),random(1.0f),random(1.0f));
        glVertex2f(1.0f,1.0f);
    glEnd();

    glFlush();
}

void testCurl(string url) {
    CURL *curl;
     FILE *fp;
     CURLcode res;
     char outfilename[FILENAME_MAX] = "/home/inferno/test.jpg";
     curl = curl_easy_init();
     if (curl) {
         fp = fopen(outfilename,"wb");
         curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
         res = curl_easy_perform(curl);
         /* always cleanup */
         curl_easy_cleanup(curl);
         fclose(fp);
     }
}

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written = fwrite(ptr, size, nmemb, stream);
    return written;
}

float random(float max) {
    return static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/max));
}

res_path.h

    #ifndef RES_PATH_H
#define RES_PATH_H

#include <iostream>
#include <string>
#include <SDL2/SDL.h>

/*
 * Get the resource path for resources located in res/subDir
 * It's assumed the project directory is structured like:
 * bin/
 *  the executable
 * res/
 *  Lesson1/
 *  Lesson2/
 *
 * Paths returned will be Lessons/res/subDir
 */
std::string getResourcePath(const std::string &subDir = ""){
    //We need to choose the path separator properly based on which
    //platform we're running on, since Windows uses a different
    //separator than most systems
#ifdef _WIN32
    const char PATH_SEP = '\\';
#else
    const char PATH_SEP = '/';
#endif
    //This will hold the base resource path: Lessons/res/
    //We give it static lifetime so that we'll only need to call
    //SDL_GetBasePath once to get the executable path
    static std::string baseRes;
    if (baseRes.empty()){
        //SDL_GetBasePath will return NULL if something went wrong in retrieving the path
        char *basePath = SDL_GetBasePath();
        if (basePath){
            baseRes = basePath;
            SDL_free(basePath);
        }
        else {
            std::cerr << "Error getting resource path: " << SDL_GetError() << std::endl;
            return "";
        }
        //We replace the last bin/ with res/ to get the the resource path
        size_t pos = baseRes.rfind("bin");
        baseRes = baseRes.substr(0, pos) + "res" + PATH_SEP;
    }
    //If we want a specific subdirectory path in the resource directory
    //append it to the base path. This would be something like Lessons/res/Lesson0
    return subDir.empty() ? baseRes : baseRes + subDir + PATH_SEP;
}

#endif

【问题讨论】:

  • 也许您应该接受(然后忽略)输入事件。我猜系统看到输入事件没有从队列中提取,并假设程序被卡住了。
  • 成功了。如果你愿意,你可以把它作为答案,我会接受的。移动窗口仍然有点紧张,但当我实际上在渲染循环上有一个真正的计时器而不是延迟时,我会研究更多。

标签: c++ multithreading opengl sdl-2


【解决方案1】:

也许您应该接受(然后忽略)输入事件。我猜系统看到输入事件没有从队列中提取,并假设程序被卡住了。

【讨论】:

    猜你喜欢
    • 2020-11-09
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2015-06-01
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多