Jump to content
  • Advertisement
Sign in to follow this  
c4c0d3m0n

Switching between SDL and OpenGL for drawing

This topic is 3176 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 all I'm trying to find out how to switch between SDL and OpenGL for drawing. In my game, I would like to use simple SDL blitting for the main menu, and then nice accelerated OpenGL for the gameplay. I threw together some code to try if it would work, but what I tried here clearly didn't work. What happens is that the following code compiles fine (g++ foo.cpp -lSDL -lGL -lGLU) but when I try to switch from SDL to OpenGL the program just crashes and SDL_GetError() isn't telling me anything...
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <iostream>

const int QUIT_PLZ = 0 << 1;

int sdl()
{
    // Initialization
    SDL_Surface* screen = NULL;
    screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
    if (!screen) {
        std::cout << SDL_GetError();
        return QUIT_PLZ;
    }

    SDL_WM_SetCaption("SDL", NULL);

    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0x10, 0xDD, 0x12));
    SDL_Flip(screen);

    // Vars
    SDL_Event event;
    bool done = false;

    // Main loop
    while (!done)
    {
        // Event polling
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) return QUIT_PLZ;

            if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE) {
                done = true;
            }
        }

        // Sleep
        SDL_Delay(10);
    }

    return 0;
}

int opengl()
{
    // Initialization
    if (!SDL_SetVideoMode(800, 600, 32, SDL_OPENGL)) {
        std::cout << SDL_GetError();
        return QUIT_PLZ;
    }

    SDL_WM_SetCaption("SDL + OpenGL", NULL);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat height = (GLfloat) SDL_GetVideoSurface()->h;
    GLfloat width = (GLfloat) SDL_GetVideoSurface()->w;
    gluPerspective(45.0f, width / height, 0.1f, 100.0f);

    // Vars
    SDL_Event event;
    bool done = false;
    GLfloat rotation = 0.0f;

    // Main loop
    while (!done)
    {
        rotation += 1.0f;

        // Event polling
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) return QUIT_PLZ;

            if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE) {
                done = true;
            }
        }

        // OpenGL magix
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glMatrixMode(GL_MODELVIEW);

        glLoadIdentity();
        glTranslatef(-2.0f, 0.0f, -8.0f);

        glRotatef(rotation, 0.2f, 0.7f, 0.0f);

        glBegin(GL_TRIANGLES);
            glColor3f(1, 0, 0); glVertex3f(-1, -1,  1);
            glColor3f(0, 1, 0); glVertex3f( 1,  1,  1);
            glColor3f(0, 0, 1); glVertex3f(-1,  1, -1);

            glColor3f(1, 0, 0); glVertex3f( 1,  1,  1);
            glColor3f(0, 1, 0); glVertex3f(-1, -1,  1);
            glColor3f(0, 0, 1); glVertex3f( 1, -1, -1);

            glColor3f(1, 0, 0); glVertex3f(-1,  1, -1);
            glColor3f(0, 1, 0); glVertex3f( 1, -1, -1);
            glColor3f(0, 0, 1); glVertex3f(-1, -1,  1);

            glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
            glColor3f(0, 1, 0); glVertex3f(-1,  1, -1);
            glColor3f(0, 0, 1); glVertex3f( 1,  1,  1);
        glEnd();

        glLoadIdentity();
        glTranslatef(2.0f, 0.0f, -8.0f);

        SDL_GL_SwapBuffers();
        glFlush();

        // Sleep
        SDL_Delay(10);
    }

    return 0;
}

int main()
{
    SDL_Init(SDL_INIT_EVERYTHING);

    if (sdl() == QUIT_PLZ) { SDL_Quit(); return 0; }
    if (opengl() == QUIT_PLZ) { SDL_Quit(); return 0; }
    if (sdl() == QUIT_PLZ) { SDL_Quit(); return 0; }

    SDL_Quit();

    return 0;
}
I was hoping someone could clear me up on how to do this, or maybe tell me off because it's a bad idea all together to start with. If so, I'd be open for alternatives. I guess in the end I could even write the menu in OpenGL, but I'm more familiar with SDL and I would like to throw together something showable now and then program the actual game behind it later.

Share this post


Link to post
Share on other sites
Advertisement
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!