- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
目前我有一个分为两个循环的程序。第一个在主线程中,现在除了每秒打印几次“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/
问题解决了 转到底部查看解决方案。 大家好,我用谷歌搜索了很长时间,但没有找到任何东西,所以,我们开始吧: 我有这样的设置: Compiler: MinGW IDE: Netbeans Library
如何在 SDL 中创建一个插入主窗口的新窗口?所以它可以是有重点的,有单独的绘图上下文和单独的处理事件。 最佳答案 窗中窗 您可以使用下面的示例在窗口中创建一个窗口。该示例将生成两个窗口,其中 sub
我是 SDL 的新手,我只是好奇为什么 sdl 使用静态和动态库?我的意思是,sdl.dll 中有哪些函数,为什么它是动态链接而不是静态链接?谢谢。 最佳答案 SDL.dll包含您在 SDL 中使用的
我通过 brew 在我的 mac 上安装了 SDL,但我无法包含它!这是我太简单的代码: #include int main(){ return 0; } 用cc编译时,CC找不到SDL.h
我正在尝试制作一个以恒定帧速率运行的 SDL 程序。但是我发现即使我的程序滞后很多并且跳过了很多帧(即使它以低帧运行并且没有渲染太多)。 你们有什么建议可以让我的程序运行更流畅吗? #include
我目前正在编写一款非常复杂和狡猾的游戏,它会让您充满敬畏和胜利 - 哦,好吧,这是 15 个谜题,我只是在熟悉 SDL。 我在窗口模式下运行,并使用 SDL_Flip 作为一般情况的页面更新,因为它会
我知道 SDL_TEXTUREACCESS_STATIC 和 SDL_TEXTUREACCESS_TARGET 之间的区别。我正在为一款游戏编写 SDL 后端,该游戏的平台无关核心不会告诉平台相关纹理
我们最近移植了 Bitfighter从 GLUT 到 SDL。这样做有很多好处,但也有一些缺点,特别是在窗口管理领域。 Bitfighter 在固定纵横比窗口(800x600 像素)中运行。用户可以将
我正在尝试使用 SDL 2.0 了解整个 2D 加速渲染过程。 所以我的问题是在屏幕上绘制圆圈最有效的方法是什么,为什么? 一些方法是: 首先创建一个软件表面,然后在该表面上绘制必要的像素,然后从该表
我正在尝试将纹理保存到 png 中,而我唯一得到的是 是屏幕的一部分的屏幕截图。 我的代码示例: src_texture = SDL_CreateTextureFromSurface( render
我正在使用 SDL1.2 在我的 openGL 框架中处理窗口管理。 程序运行时是否可以在不调用 SDL_Quit() 的情况下破坏窗口(表面)? ? 背景:我的框架实际上只是多媒体环境中的一个 gf
如何在 SDL 中缩放 Sprite ? 最佳答案 SDL 不直接提供缩放功能,但有 an additional library called SDL_gfx它提供旋转和缩放功能。还有another
我刚刚开始使用SDL2_ttf。我已经弄清楚如何使用 TTF_RenderText_Blished 在屏幕上获取一些文本,但是如何让它进行换行和自动换行? 似乎不支持\n;它只是创造一个空间而不是沿着
我安装了 FEDORA 和 SDL,并希望在编译时用 C 编程图形,我得到了很多对 SDL_MapRGB、SDL_Init 等的 undefined reference 我搜索了文件系统 SDL.dl
我在我的项目中使用 SDL 库,并且在 Windows 平台上工作。 当我决定将项目迁移到 SDL 2 时,我遇到了一个问题: SDL 1.2 中有一个选项可以将 stdout/stderr 的输出打
我正在尝试使用 Derelict 和 D 编写一个简单的图形测试程序。 当我尝试用 SDL 做几乎任何事情时,它都会出现段错误。这是有问题的代码: import std.stdio; import
我在考虑是否尝试使用 SDL 作为 DirectX 的替代品,似乎我只创建 2D 游戏,但是我找不到任何地方是否需要在最终用户计算机上安装 SDL 才能玩用 C# 开发的游戏,图形使用 SDL。 有人
我想使用 SDL_net 作为一个供少数程序使用的帮助程序库。然而,其中一些程序本身可能已经在使用 SDL。如果我理解正确的话,这意味着我在初始化/释放我的库时不能盲目使用 SDL_Init 和 SD
我希望我的游戏引擎停止将鼠标移动到中心(用于偏航和俯仰相机计算)。我写了一些应该处理它的代码,但鼠标在最小化时仍然移动。 void mainLoop() { // This is the ma
结构SDL_Keysym 有SDL_Scancode 和SDL_Keycode 成员。它们之间有什么区别?该文档并没有真正为我清除它。我都试过了,它们似乎做同样的事情。 最佳答案 参见 the SDL
我是一名优秀的程序员,十分优秀!