SDL2 + OpenGL = Gnome 3错误地抱怨“程序无响应”

时间:2014-12-05 06:38:34

标签: c++ multithreading opengl sdl-2

目前我有一个分为两个循环的程序。第一个是在主线程中,除了print" d00d"之外,它现在并没有真正做任何事情。一秒钟几次。 第二个是在SDL2线程上运行的循环,它在OpenGL中呈现帧并在SDL2窗口中显示它们。

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

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

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

以与使用SDL2 / SDL2-Thread / OpenGl相同的方式使用std :: thread / glut / OpenGl不会导致我现在遇到的任何问题,所以我觉得我感觉像是&#39 ;我对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

1 个答案:

答案 0 :(得分:3)

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