I already used SDL2 before, and now I try to use it with openGL 2.1. But when I try to create a renderer, I get "Invalid renderer". For creating a renderer I already use the same line before for simple SDL2 project, but with openGL it appears to not work properly. Does someone have an idea why ? I tried to change the flags of CreateRenderer, and I have this error for all of them except "SDL_RENDERER_SOFTWARE", but with him I can't grab input properly, and when the mouse cursor goes out the Window I have this error : "Invalid window".
Here is my code :
#include <SDL2/SDL.h>
#include <GL/glut.h>
#include <iostream>
const int WIDTH = 500, HEIGHT = 500;
using namespace std;
SDL_Window* pWindow;
SDL_Renderer* pRenderer;
SDL_Event event;
void initOGL()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70,(double)WIDTH/HEIGHT,1,1000);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
}
void initSDL()
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        cout << "Video init failed" << endl;
        exit(EXIT_FAILURE);
    }
    cout << "Video initialized" << endl;
    // Window creation
    pWindow = SDL_CreateWindow("Test OGL w/ SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
    if (!pWindow)
    {
        cout << "Window creation failed" << endl;
        exit(EXIT_FAILURE);
    }
    cout << "Window created" << endl;
    // Renderer creation
    pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED);
    if (!pRenderer)
    {
        cout << "Renderer creation failed" << endl;
    }
    cout << "Renderer created" << endl;
    // OGL version
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    // OGL context
    SDL_GLContext contexteOGL = SDL_GL_CreateContext(pWindow);
    if (!contexteOGL)
    {
        cout << "Echec création du contexte" << endl;
    }
    cout << "Contexte créé" << endl;
}
int main(int argc, char const *argv[])
{
    initSDL();
    initOGL();
    cout << "SDL_ERROR : " << SDL_GetError() << endl;
    return 0;
}
Aucun commentaire:
Enregistrer un commentaire