SDL2 OpenGL3 Comment initialiser la SDL à l'intérieur d'une fonction

Je suis en train d'expérimenter la nouvelle SDL2 version bêta et une OpenGL3 contexte, et je vais avoir un problème bizarre:

Si j'exécute le code d'initialisation de la SDL dans ma fonction main (), il fonctionne très bien, mais je veux avoir ce code dans un separete init_sdl() fonction.

Si je mets le code d'initialisation dans un autre init_sdl() de la fonction, et d'appeler cette fonction main(), la couleur d'arrière-plan n'est jamais tiré, et le programme commence à follement consommer toutes mes ressources du système.

Quelqu'un pourrait-il m'indiquer un exemple de travail où SDL est initialisé dans une fonction séparée? Je n'arrive pas à en trouver un... Peut-être que ce n'est pas possible? Je pense que je me souviens vaguement d'avoir un problème similaire avec la SDL 1.2, mais il a été quelques années depuis que je l'ai utilisé, et je ne pense pas que j'ai jamais trouvé une solution. En fait, cela peut être la raison pour laquelle j'ai choisi de passer à l'utilisation de la SFML à la place.

Je veux vraiment utiliser SDL2 au lieu de SFML car il fonctionne sur plusieurs plates-formes, mais ne pas être en mesure de séparer les choses en petites fonctions est un briseur d'affaire pour moi. Ce devrait être facile, suis-je raté quelque chose d'évident?

Edit:

Cela fonctionne:

#include <iostream>
#include <GL/glew.h>
#include <SDL.h>
#define PROGRAM_NAME "SDL2 OpenGL3 Example"
int main(int argc, char** argv)
{
SDL_Window* sdl2_window = 0;
SDL_GLContext opengl3_context;
SDL_Init(SDL_INIT_VIDEO);
//set the opengl context version
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
//turn on double buffering set the depth buffer to 24 bits
//you may need to change this to 16 or 32 for your system
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
//create the sdl2 window
sdl2_window = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 512, 512,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
//create the opengl3 context
opengl3_context = SDL_GL_CreateContext(sdl2_window);
GLenum status = glewInit();
if (status != GLEW_OK)
{
std::cerr << "GLEW Error: " << glewGetErrorString(status) << "\n";
exit(1);
}
//sync buffer swap with monitor's vertical refresh rate
SDL_GL_SetSwapInterval(1);
//set background color
glClearColor( 1.0, 0.0, 0.0, 1.0 );
while (true)
{
int status = 0;
glClear( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow(sdl2_window);
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
break;
case SDL_KEYUP:
//if escape is pressed, quit
if (event.key.keysym.sym == SDLK_ESCAPE)
status = 1; //set status to 1 to exit main loop
break;
case SDL_QUIT:
status = 1;
break;
}
}
if (status == 1) //if received instruction to quit
break;
}
//delete opengl3 context, destroy sdl2 window, and shut down sdl subsystems
SDL_GL_DeleteContext(opengl3_context);
SDL_DestroyWindow(sdl2_window);
SDL_Quit();
return 0;
}

Cela ne fonctionne pas:

#include <iostream>
#include <GL/glew.h>
#include <SDL.h>
#define PROGRAM_NAME "SDL2 OpenGL3 Example"
void init_sdl(SDL_Window* sdl2_window, SDL_GLContext& opengl3_context)
{
SDL_Init(SDL_INIT_VIDEO);
//set the opengl context version
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
//turn on double buffering set the depth buffer to 24 bits
//you may need to change this to 16 or 32 for your system
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
//create the sdl2 window
sdl2_window = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 512, 512,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
//create the opengl3 context
opengl3_context = SDL_GL_CreateContext(sdl2_window);
}
int main(int argc, char** argv)
{
SDL_Window* sdl2_window = 0;
SDL_GLContext opengl3_context;
init_sdl(sdl2_window, opengl3_context);
GLenum status = glewInit();
if (status != GLEW_OK)
{
std::cerr << "GLEW Error: " << glewGetErrorString(status) << "\n";
exit(1);
}
//sync buffer swap with monitor's vertical refresh rate
SDL_GL_SetSwapInterval(1);
//set background color
glClearColor( 1.0, 0.0, 0.0, 1.0 );
while (true)
{
int status = 0;
glClear( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow(sdl2_window);
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
break;
case SDL_KEYUP:
//if escape is pressed, quit
if (event.key.keysym.sym == SDLK_ESCAPE)
status = 1; //set status to 1 to exit main loop
break;
case SDL_QUIT:
status = 1;
break;
}
}
if (status == 1) //if received instruction to quit
break;
}
//delete opengl3 context, destroy sdl2 window, and shut down sdl subsystems
SDL_GL_DeleteContext(opengl3_context);
SDL_DestroyWindow(sdl2_window);
SDL_Quit();
return 0;
}
SSCCE de temps.
J'ai ajouté des exemples de code

OriginalL'auteur Defcronyke | 2012-12-11