Reputation: 23
I have some trouble to get my glew to work. When I initialize glew I get an error: Missing GL version. I can't create a context aswell: OpenGL not initialized. This is my code:
#include <GL\glew.h>
#include <GL\GLU.h>
#include <SDL2\SDL.h>
#include <SDL2\SDL_opengl.h>
#include <iostream>
#undef main
SDL_GLContext context;
SDL_Renderer * renderer;
SDL_Window * window;
int main(int argc, char *argv[]) {
//init SDL
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
fprintf(stderr, "\n> Unable to initialize SDL: %s\n", SDL_GetError());
}
window = SDL_CreateWindow("Cri Engine 3D", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
if (window == nullptr)
{
printf("> Window could not be created! SDL Error: %s\n", SDL_GetError());
}
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);
context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
if (context == NULL) {
printf("> OpenGL context could not be created! SDL Error: %s\n", SDL_GetError());
}
//Glew
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "> Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "> Using GLEW %s\n", glewGetString(GLEW_VERSION));
glViewport(0, 0, 800, 600);
SDL_Quit();
return 0;
}
These are the linker settings I use(in this order): glew32.lib, glu32.lib, opengl32.lib, SDL2.lib, SDL2main.lib.
I'm sure that the libaries are correctly included. PS: this is my first post, if I am missing some information tell me!
Upvotes: 2
Views: 1535
Reputation: 96013
You're missing SDL_WINDOW_OPENGL
flag for SDL_CreateWindow()
.
Also, you must remove #undef main
.
Otherwise you would need to do some low-level initialization yourself, which you don't do.
Another thing: You must switch to compatibility profile from core profile (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
) because GLEW has a tendency to crash when you ask it to initialize a core profile context on Windows.
Also, many parts of your core are redundant:
SDL_WINDOW_SHOWN
- It is already used by default.SDL_GL_MakeCurrent(window, context);
- Not needed when there is only one context.glViewport(0, 0, 800, 600);
- When you create a context, it automatically sets up a correct viewport for you.SDL_Quit();
- You don't need to call anything when your program ends. It does nothing but makes your program close slower. (At least this is how it works on Windows. On Linux it is sometimes necessary, as @keltar has pointed out. Also, it prevents leak detectors like valgring from yelling at you about SDL internal structures.)#include <SDL2\SDL_opengl.h>
- It's a replacement for <GL/gl.h>
, which you don't need because you already have <GL\glew.h>
.Upvotes: 5