gpt4 book ai didi

c++ - SDL2 + OpenGL = Gnome 3 错误地提示 "Program not responding"

转载 作者:搜寻专家 更新时间:2023-10-31 01:42:12 24 4
gpt4 key购买 nike

目前我有一个分为两个循环的程序。第一个在主线程中,现在除了每秒打印几次“d00d”之外实际上什么都不做。第二个是在 SDL2 线程上运行的循环,该线程在 OpenGL 中渲染帧并将它们显示在 SDL2 窗口中。

tldr;线程一:程序循环,线程二:渲染循环

几秒钟后,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++ - SDL2 + OpenGL = Gnome 3 错误地提示 "Program not responding",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27310226/

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