• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ender1618

Proper shutdown for SDL 1.2 with OpenGL

2 posts in this topic

I am using SDL 1.2 in a minimal fashion to create a cross platform OpenGL context (this is on Win7 64bit) in C++. I also use glew to have my context support OpenGL 4.2 (which my driver supports).
 
Things work correctly at run-time but I have been noticing lately a random crash when shutting down on calling SDL_Quit. 
 
What is the proper sequence for SDL (1.2) with OpenGL start up and shutdown?
 
Here is what i do currently:
 
    int MyObj::Initialize(int width, int height, bool vsync, bool fullscreen)
    {
      if(SDL_Init( SDL_INIT_EVERYTHING ) < 0) 
      {
        printf("SDL_Init failed: %s\n", SDL_GetError());
        return 0;
      }

      SDL_GL_SetAttribute(SDL_GL_RED_SIZE,         8);
      SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,       8);
      SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,        8);
      SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,       8);

      SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,       24);
      SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,       8);
      SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,     24);

      SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,  0);
      SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,  vsync ? 1 : 0);

      if((m_SurfDisplay = SDL_SetVideoMode(width, height, 24, 
                                           SDL_HWSURFACE | 
                                           SDL_GL_DOUBLEBUFFER | 
                                           (fullscreen ? SDL_FULLSCREEN : 0) |
                                           SDL_OPENGL)) == NULL)
      {
        printf("SDL_SetVideoMode failed: %s\n", SDL_GetError());
        return 0;
      }

      GLenum err = glewInit();
      if (GLEW_OK != err) 
        return 0;
      
      m_Running = true;
      return 1;
    }

    int MyObj::Shutdown()
    {   
      SDL_FreeSurface(m_SurfDisplay);
      SDL_Quit();


      return 1;
    }
In between the init and shutdown calls i create a number of GL resources (e.g. Textures, VBOs, VAO, Shaders, etc.) and render my scene each frame, with a SDL_GL_SwapBuffers() at the end of each frame (pretty typical). Like so:
 
    int MyObject::Run()
    {
      SDL_Event Event;
    
      while(m_Running) 
      {
        while(SDL_PollEvent(&Event))
        { OnEvent(&Event); } //this eventually causes m_Running to be set to false on "esc"
        
        ProcessFrame();
        SDL_SwapBuffers();
      }
      return 1;
    }
Within the ~MyObject MyObject::Shutdown() is called. Where just recently SDL_Quit crashes the app. I have also tried call Shutdown instead outside of the destructor, after my render loop returns to the same effect.
 
One thing that I do not do (that i didn't think I needed to do) is call the glDelete* functions for all my allocated GL resources before calling Shutdown (i thought they would automatically be cleaned up by the destruction of the context, which i assumed was happening during SDL_FreeSurface or SDL_Quit(). I of course call the glDelete* function in the dtors of there wrapping objects, which eventually get called by the tale of ~MyObject, since the wrapper objects are part of other objects that are members of MyObject.
 
As an experiment i trying forcing all the appropriate glDelete* calls to occur before Shutdown(), and my crash never seems to occur. Funny thing i did not need to do this a week ago, and really nothing has changed according to GIT (may be wrong though). 
 
Is it really necessary to make sure all GL resources are freed before calling MyObject::Shutdown with SDL? Does it look like I might be doing something else wrong?
 
Thanks for any insights.
0

Share this post


Link to post
Share on other sites

Shutting down SDL destroys the OpenGL context, after which all OpenGL calls such as glDelete* will be illegal and likely result in a crash. The destructors for the members of MyObject will be called after the destructor of MyObject, and if that involves any OpenGL calls, that's likely the crash you're experiencing.

 

It would be a good habit to keep track and destroy the OpenGL resources before shutting down the context. It's not absolutely vital for desktop OpenGL programming, but good preparation for other APIs. For example on Direct3D9 you must be able to delete then recreate all your non-managed graphics resources whenever the device is lost, eg. when the user alt-tabs away and back to your application.

0

Share this post


Link to post
Share on other sites

BTW I was told that the call to SDL_FreeSurface in shutdown I was doing was incorrect and that SDL would handle the destruction of the surface during quit, so I am not calling SDL_FreeSurface anymore. But I still get the crash and its random.

 

I understand calling glDetele* after SDL_Quit could be a problem, but the crash seems to occur when calling SDL_Quit, not during the destructors of my gl object wrappers that call glDelete*. Why would that be?

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0