SDL and OpenGL program compilation fails
I am trying to build a simple openGL and SDL program that just refuses to work.
I am using the latest nvidia driver (285.something if i remember) and i use mingw to compile. The machine i use runs Windows7. My knowledge of mingw is somewhat limited so this might be a very simple linking error.
The version of SDL im using is 1.2 (should i upgrade to 1.3?)
At first, everything worked fine, but i was merely opening an opengl window through SDL. here's the code for the original makefile
all:
g++ -o sdlGL.exe esg.cpp tiletest.cpp -lmingw32 -lSDLmain -lSDL -lopengl32 -lglu32
and these are the 2 functions that were called
void init_sdl(int width, int height) {
//Start SDL
if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
fDebug << "Failed to init SDL" << std::endl;
quit(1);
}
//Set SDL attrib.
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
//Set SDL video mode
if( SDL_SetVideoMode(width, height, 0, SDL_OPENGL) == 0 ) {
fDebug << "Failed to set SDL video mode" << std::endl;
quit(1);
}
}
//Initializes OpenGL
void init_opengl(int width, int height, bool alpha) {
// Set the OpenGL state after creating the context with SDL_SetVideoMode
glClearColor( 0, 0, 0, 0 );
glClear(GL_COLOR_BUFFER_BIT);
glEnable( GL_TEXTURE_2D ); // Need this to display a texture
if(alpha) {
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, width, height, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
Now for the inclusions:
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_opengl.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
With that code, everything worked fine. Then i added some functions to take care of shaders and shader programs
void CreateShader(GLenum eShaderType, const std::string &strShaderFile, std::vector<GLuint> &shaderList) {
GLuint shader = glCreateShader(eShaderType);
const char *strFileData = strShaderFile.c_str();
glShaderSource(shader, 1, &strFileData, NULL);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
shaderList.push_back(shader);
}
void ClearShader(std::vector<GLuint> &shaderList) {
std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);
}
GLuint CreateProgram(const std::vector<GLuint> &shaderList) {
GLuint program = glCreateProgram();
for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
glAttachShader(program, shaderList[iLoop]);
glLinkProgram(program);
GLint status;
glGetProgramiv (program, GL_LINK_STATUS, &status);
for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
glDetachShader(program, shaderList[iLoop]);
return program;
}
Now, the compiler complains that glCreateShader, glShaderSource, glCompileShader, glGetShaderiv, glDeleteShader, glCreateProgram, glAttachShader, glLinkProgram, glGetProgramiv and glDetachShader were not declared in this scope.
i figured i needed to include glee or glew (right?) so i went with glew. I then added -glew32 to my makefile and added glew.h to my inclusions
#include "GL/glew.h"
Now, the compiler lists a ton of errors for glew.h like ---- does not name a type and ---- was not declared in this scope.
I have no idea what to do to fix this. I should add that i have not much experience with openGL.
GLEW handles the platform-specific #include
s for the GL header(s), so you no longer need SDL_opengl.h
. It also #include
s the appropriate GLU
header so you don't need that either.
As Xavier pointed out #include <GL/glew.h>
before anything else. If I recall correctly it tends to conflict with windows.h
.
I then added -glew32 to my makefile and added glew.h to my inclusions
-glew32
is not right, it's -lglew32
. The #include <GL/glew.h>
header should be included before everything else that interacts with OpenGL.
上一篇: 在OSX上使用SDL2创建OpenGL 3.x上下文(Macbook Air 2012)
下一篇: SDL和OpenGL程序编译失败