c++ - OpenGL 白色空白屏幕且无响应

标签 c++ opengl sdl

我正在使用带有 OpenGL 的 SDL2.00 创建一个程序。该程序目前不应该做任何事情,它只是一个试验台。但是我遇到了一个问题。当我使用 SDL_CreateWindow 创建窗口时,窗口进入忙碌状态并停止响应。程序流程并没有真正受此影响,但是窗口本身将无法工作。它所做的只是显示一个白色的空白窗口并且不接受任何输入,无法调整大小无法移动也无法退出。我将附上代码,但我怀疑它是否与代码相关,因为我已经使用 SDL 编写了一些程序,它们似乎运行良好。

使用VS2013 SDL2.00 OpenGL

=========main========

#include "stdafx.h"


void init()
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 640.0 / 480.0, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 2.0, -5.0);
glVertex3f(-2.0, -2.0, -5.0);
glVertex3f(2.0, -2.0, -5.0);
glEnd();

}


int main(int argc, char* argv[]){


SDL_Init(SDL_INIT_VIDEO); 
SDL_Window * window;
window = SDL_CreateWindow("OpenGLTest", 300, 300, 640, 480, SDL_WINDOW_SHOWN |       SDL_WINDOW_OPENGL);
init();


while (true){
    display();
    SDL_GL_SwapWindow(window);
}




return 0;

}

=========stdafx======

#pragma once

#include <iostream>
#include <SDL.h>
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>

#define PI 3.14159265

using namespace std;

最佳答案

您忘记处理所有事件,所以 SDL 窗口只是等待、等待、等待,因此“没有响应” - 只需添加即可解决问题!

while (true) {
    SDL_PumpEvents();

    display();
    SDL_GL_SwapWindow(window);
}

您还可以通过调用 SDL_PollEvent(SDL_Event *event) 的循环手动拉取所有事件

SDL_Event event;

while (SDL_PollEvent(&event)) {
    // Process the event...
}

维基百科

关于c++ - OpenGL 白色空白屏幕且无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20929623/

相关文章:

c++ - "nullptr"和 "NULL"的用例是什么以及区别

c++ - 在对象(不是指针)的 std::vector 上调用 push_back 有严重的副作用。那么指针会更好吗?

c++ - opengl gl读取像素

c++ - 对 `InputHandler::InputHandler()' 的 undefined reference

c++ - 从 SDL_Surface 读取像素并转换为颜色值

c++ - 平铺系统 C++ SDL

c++ - 释放动态数组的最后一个元素

c++ - 在 C++ 中为 STL 映射重载 [ ] 运算符

c++ - opengl32.lib 如何在 Windows 上工作(仅 1.1 版)?它实际上实现了 OpenGL 功能吗?

c++ - 我如何在 glm 透视投影中获得代表我屏幕上 1 个像素的距离?