我想在mac osx yosemite上使用SDL2和OpenGL,我跟隨了SDL頁面上的示例以及Lazy Foo的示例,但每次運行代碼時,同樣的結果,與黑色背景上的空白窗口,我已搜查谷歌上有一段時間了,但香港專業教育學院還沒有得到任何解決方案,我會很感激的任何幫助Mac上的SDL2 + Opengl黑色窗口
下面是我的代碼
/*
* Main.cpp
*
* Created on: Sep 15, 2015
* Author: Damian-Machine
*/
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include<SDL.h>
#include<SDL_opengl.h>
#include <stdio.h>
#include <string>
#include<iostream>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
bool init();
bool initGL();
void handleKeys(unsigned char key, int x, int y);
void update();
void render();
void close();
SDL_Window *gWindow = NULL;
SDL_GLContext gContext;
bool gRenderQuad = true;
int main(){
SDL_Event e;
bool quit = false;
if(!init()){
printf("Failed to initialize!\n");
return 0;
}
SDL_StartTextInput();
while(!quit){
while(SDL_PollEvent(&e) !=0){
if(e.type == SDL_QUIT){
quit = true;
}else if (e.type == SDL_TEXTINPUT){
int x = 0, y = 0;
SDL_GetMouseState(&x, &y);
handleKeys(e.text.text[0], x, y);
}
}
render();
}
SDL_StopTextInput();
close();
}
bool init(){
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}else{
printf("SDL Opengl context created successfully\n");
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
printf("Checking failed status: %s\n", SDL_GetError());
//Create window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
//std::cout << "OpenGL version " << glGetString(GL_VERSION) << std::endl;
//std::cout << "GLSL version " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endendl;
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 { //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;
}
bool initGL(){
bool success = true;
GLenum error = GL_NO_ERROR;
glViewport(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT);
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
error = glGetError();
if(error != GL_NO_ERROR){
printf("Error initializing OpenGL! %s\n", gluErrorString(error));
success = false;
}
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
error = glGetError();
if(error != GL_NO_ERROR){
printf("Error initializing OpenGL %s\n", gluErrorString(error));
success = false;
}
glClearColor(1.f, 0.f, 0.f, 1.f);
error = glGetError();
if(error != GL_NO_ERROR){
printf("Error initializing OpenGL %s\n", gluErrorString(error));
success = false;
}
return success;
}
void handleKeys(unsigned char key, int x, int y) {
//Toggle quad
if(key == 'q')
{
gRenderQuad = !gRenderQuad;
}
}
void update(){
}
void render(){
glClear(GL_COLOR_BUFFER_BIT);
if(gRenderQuad){
glRotatef(0.4f,0.0f,1.0f,0.0f); // Rotate The cube around the Y axis
glRotatef(0.2f,1.0f,1.0f,1.0f);
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_QUADS);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
}
SDL_GL_SwapWindow(gWindow);
}
void close(){
SDL_GL_DeleteContext(gContext);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
}
您正在創建一個核心配置文件上下文('SDL_GL_CONTEXT_PROFILE_CORE'),您使用的OpenGL調用中有一半以上不推薦,並且在覈心配置文件中不受支持。所有矩陣堆棧功能,即時模式繪圖等在覈心配置文件中不可用。 –