Jump to content
  • Advertisement
Sign in to follow this  
Kjansen92

OpenGL Using SDL with OpenGL

This topic is 1471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I am primarily using openGL for anything graphics-related. I've discovered SDL 2.0, which will open a window for me, and works great! 

 

...But my situation is that I've done everything so far in openGL, and don't know how to turn my previous code into an SDL window.

 

What I mean is, going from this:

 

mainLoop();
glutInit(&argc, argv);                 // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(800, 1200);   // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutReshapeFunc(reshape);
glutIdleFunc(idle);
initGL();
glutMouseFunc(myMouseMap);
glutPassiveMotionFunc(myMouseMotion);
glutMainLoop();           // Enter the event-processing loop
 
To an SDL window instead.
 
I can create an SDL window, and understand that. But I don't know how to still program in openGL (primarily), and run it through an SDL screen.
 
My rationale for doing this is so that I can use fonts and text display that are available with SDL. 
 
 
Thank you in advance, and I hope this helps somebody,
 
Keviin Jhansen

Share this post


Link to post
Share on other sites
Advertisement

Not tested. smile.png

 

#include <SDL.h>
#include <GL/glew.h>
#include <cstdlib>

int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0) exit(EXIT_FAILURE);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
    SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    SDL_Window* window = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);

    if (!window) exit(EXIT_FAILURE);

    SDL_GLContext context = SDL_GL_CreateContext(window);

    if (!context) exit(EXIT_FAILURE);

    SDL_GL_MakeCurrent(window, context);

    if (glewInit() != GLEW_OK) exit(EXIT_FAILURE);

    SDL_GL_SetSwapInterval(1);
    glClearColor (1.0, 0.0, 0.0, 1.0);

    bool running = true;

    while (running)
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_KEYDOWN:
                    switch (event.key.keysym.sym)
                    {
                        case SDLK_ESCAPE:
                        running = false;
                        break;
                    }
                    break;

                case SDL_QUIT:
                    running = false;
                    break;
            }
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        SDL_GL_SwapWindow(window);
    }

    SDL_GL_DeleteContext(context);
    SDL_DestroyWindow(window);
    SDL_Quit();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!