• 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
Tallkotten

Switching rendering from SDL to OpenGL

8 posts in this topic

Hi!

 

I took it upon myself to finally start migrating from SDL to OpenGl in my project today. So far I've managed to (in isolated code) translate an SDL_Surface to an OpenGl texture which i then use to apply on polygons.

 

In my previous rendering method i used a lot of clips. Meaning I've got one image with maybe 3-5 images on it which i then clip before i render to get the desired image to show. I've managed to translate this over to OpenGl as well but it takes a bit of work and i need to know the loaded image dimensions before i do.

 

Just to clarify how the rendering process works now, so you get a better scope of my problem:

 

1. Loads image to an SDL_Surface

2. Saves it to an array

3. Something fetches the image memory location from the array

4. Converts the SDL_Surface to a OpenGl texture

5. Clips the texture

6. Renders it to QUADS

7. Destroys the texture

 

My Convert code is basically the same as this one here: http://content.gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL

 

Question time: 

 

1. I've noticed a sudden drop of FPS since the tryout change (only the tiles are being rendered at the moment since haven't made it global yet). The drop if from 400+ FPS to around 60 FPS after i use glDeleteTextures(); on the texture I've just drawn. Why is this? I suspect it's the conversion that drains the fps.

 

2. If it is the conversion that drains the FPS, then it is smarter to convert the image once just as it loads and save the texture in the array instead of the surface? Sounds like it... tongue.png

 

3. Is there any reason for me to save the image as a SDL_Surface instead of loading it from OpenGL directly? Can i do that?

 

4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?

 

I think that's all! Thanks for your time, i really appreciate answers!

 

// Tallkotten

 

 

0

Share this post


Link to post
Share on other sites

I'm no expert, but surely you aren't converting those images every frame? Even if you're holding onto the surface to draw with it elsewhere, I can't imagine a scenario where converting it every frame would be good for performance.

 

My suggestions is to do what you yourself suggest in #2, and see how your performance changes. I don't know a whole lot about OpenGL, but I suspect that may be your issue.

 

On #3, there must be a way to load textures via OGL without SDL, Unfortunately I'm not proficient enough to know it offhand, but I suspect a pretty quick search will turn up the result. If you aren't using the SDL Surface for anything but converting to an OGL Texture, there's no real need to load it that way (save, perhaps ease).

 

One thing I'm led to understand about SDL is that it makes using OpenGL a bit easier than if you were trying to do it raw (especially as it relates to dealing with platform specific things like window management). There are other helper libraries out there that help to this end, GLFW and glut are ones I recall seeing pretty regularly for example, but since you already seem familiar with SDL, you may as well use it.

1

Share this post


Link to post
Share on other sites
Hi!

 

I took it upon myself to finally start migrating from SDL to OpenGl in my project today. So far I've managed to (in isolated code) translate an SDL_Surface to an OpenGl texture which i then use to apply on polygons.

 

In my previous rendering method i used a lot of clips. Meaning I've got one image with maybe 3-5 images on it which i then clip before i render to get the desired image to show. I've managed to translate this over to OpenGl as well but it takes a bit of work and i need to know the loaded image dimensions before i do.

 

Just to clarify how the rendering process works now, so you get a better scope of my problem:

 

1. Loads image to an SDL_Surface

2. Saves it to an array

3. Something fetches the image memory location from the array

4. Converts the SDL_Surface to a OpenGl texture

5. Clips the texture

6. Renders it to QUADS

7. Destroys the texture

 

My Convert code is basically the same as this one here: http://content.gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL

 

Question time: 

 

1. I've noticed a sudden drop of FPS since the tryout change (only the tiles are being rendered at the moment since haven't made it global yet). The drop if from 400+ FPS to around 60 FPS after i use glDeleteTextures(); on the texture I've just drawn. Why is this? I suspect it's the conversion that drains the fps.

 

2. If it is the conversion that drains the FPS, then it is smarter to convert the image once just as it loads and save the texture in the array instead of the surface? Sounds like it... tongue.png

 

3. Is there any reason for me to save the image as a SDL_Surface instead of loading it from OpenGL directly? Can i do that?

 

4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?

 

I think that's all! Thanks for your time, i really appreciate answers!

 

// Tallkotten

 

the part 7 of your code is I'm worried about. are you deleting the textures on every frame?? or before exiting the program ? if you are deleting the texture on every frame than that explains your low FPS. you should be init and generate textures  once and then use the glBindTexture( ) to assign them to the quads.

1

Share this post


Link to post
Share on other sites
 
1. I've noticed a sudden drop of FPS since the tryout change (only the tiles are being rendered at the moment since haven't made it global yet). The drop if from 400+ FPS to around 60 FPS after i use glDeleteTextures(); on the texture I've just drawn. Why is this? I suspect it's the conversion that drains the fps.

Copy the image data from the SDL_Surface into an OpenGL texture once during startup, delete the surface, and reuse the texture. Only when you no longer need the texture should you delete it on the OpenGL side.
 
2. If it is the conversion that drains the FPS, then it is smarter to convert the image once just as it loads and save the texture in the array instead of the surface? Sounds like it... tongue.png

You don't even need the array. When you pass the data to OpenGL, the driver makes a copy. So any resources you have allocated to manipulate the image data on your side, be it an SDL_Surface or an array, can safely be released. The drive will manage the texture data and you can forget about it. Until you no longer need the texture, at which point you call glDeleteTextures.
 
3. Is there any reason for me to save the image as a SDL_Surface instead of loading it from OpenGL directly? Can i do that?

OpenGL does not provide any mechanism for loading image files from disk. You can use any one of a number of libraries or implement your own loader, but OpenGL isn't going to offer any help there. The SDL_image library works just as well as any other.
 
4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?

Every image loading library provides a means to query the width and height of an image. And if you decide one day to implement your own loader, all of the commonly used image formats store width/height data in the file header (as should you if you ever create your own image file format).
1

Share this post


Link to post
Share on other sites

Thank you both for the answers. I suspected that was the FPS theif but like i said i am only trying out this new way atm.

 

I'm no expert, but surely you aren't converting those images every frame? Even if you're holding onto the surface to draw with it elsewhere, I can't imagine a scenario where converting it every frame would be good for performance.

 

My suggestions is to do what you yourself suggest in #2, and see how your performance changes. I don't know a whole lot about OpenGL, but I suspect that may be your issue.

 

On #3, there must be a way to load textures via OGL without SDL, Unfortunately I'm not proficient enough to know it offhand, but I suspect a pretty quick search will turn up the result. If you aren't using the SDL Surface for anything but converting to an OGL Texture, there's no real need to load it that way (save, perhaps ease).

 

One thing I'm led to understand about SDL is that it makes using OpenGL a bit easier than if you were trying to do it raw (especially as it relates to dealing with platform specific things like window management). There are other helper libraries out there that help to this end, GLFW and glut are ones I recall seeing pretty regularly for example, but since you already seem familiar with SDL, you may as well use it.

 

I use SDL for other stuff like keyboard detection as well, so i might stick around with it. 

 

---

 

Thanks very much you both! I'll try altering my code a bit when i get home and see if i see any performance boost

0

Share this post


Link to post
Share on other sites
4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?

Every image loading library provides a means to query the width and height of an image. And if you decide one day to implement your own loader, all of the commonly used image formats store width/height data in the file header (as should you if you ever create your own image file format).

 

I'm thinking of saving the image size from the SDL_Surface when i load an image. Then i link the size to a texture or something, 

 

Thanks for the tip, i'll have to look into that when i get home!

0

Share this post


Link to post
Share on other sites

You can follow this site for SDL tutorials http://www.lazyfoo.net/SDL_tutorials/http://www.lazyfoo.net/SDL_tutorials/
And tutorial number 36 is what you need http://www.lazyfoo.net/SDL_tutorials/lesson36/index.php
But in this tutorial no opengl texturing is done, so I have added texture mapping component from your gpwiki link, so that you that you have a basic idea of how to do. I am showing you where to change what,

1. Declare a global textuer object like this

GLuint texture ; // Texture object handle

 

2. Add the following code in initGL()

 

    SDL_Surface *surface; // Gives us the information to make the texture
    
    if ( (surface = SDL_LoadBMP("image.bmp")) ) {
    
        // Check that the image's width is a power of 2
        if ( (surface->w & (surface->w - 1)) != 0 ) {
            printf("warning: image.bmp's width is not a power of 2\n");
        }
    
        // Also check if the height is a power of 2
        if ( (surface->h & (surface->h - 1)) != 0 ) {
            printf("warning: image.bmp's height is not a power of 2\n");
        }
    
        // Have OpenGL generate a texture object handle for us
        glGenTextures( 1, &texture );
    
        // Bind the texture object
        glBindTexture( GL_TEXTURE_2D, texture );
        
        // Set the texture's stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
        
        // Edit the texture object's image data using the information SDL_Surface gives us
        glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0,
                      GL_BGR, GL_UNSIGNED_BYTE, surface->pixels );
    }
    else {
        printf("SDL could not load image.bmp: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }    
    
    // Free the SDL_Surface only if it was successfully created
    if ( surface ) {
        SDL_FreeSurface( surface );
    }

    glViewport( 0, 0, 640, 480 );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    glOrtho( 0, 640, 480, 0, -1, 1 );
    
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glEnable( GL_TEXTURE_2D );
    //Initialize clear color
    glClearColor( 0.f, 0.f, 0.f, 1.f );

 

 

3. Draw the quad in render function

    // Bind the texture to which subsequent calls refer to
    glBindTexture( GL_TEXTURE_2D, texture );

    glBegin( GL_QUADS );
        // Top-left vertex (corner)
        glTexCoord2i( 0, 0 );
        glVertex3f( 100, 100, 0 );
    
        // Bottom-left vertex (corner)
        glTexCoord2i( 1, 0 );
        glVertex3f( 228, 100, 0 );
    
        // Bottom-right vertex (corner)
        glTexCoord2i( 1, 1 );
        glVertex3f( 228, 228, 0 );
    
        // Top-right vertex (corner)
        glTexCoord2i( 0, 1 );
        glVertex3f( 100, 228, 0 );
    glEnd();

 

4. Delete your textures in clean_up()

glDeleteTextures( 1, &texture );

 

Sorry I totally missed your questions tongue.png

 

 

1. I've noticed a sudden drop of FPS since the tryout change (only the tiles are being rendered at the

moment since haven't made it global yet). The drop if from 400+ FPS to around 60 FPS after i use glDeleteTextures(); on the texture I've just drawn. Why is this? I suspect it's the conversion that drains the fps.

 

 It's because creating and deleting texture is expensive operation, so you should only create a texture  once and delete once you about to close your program or dont want to use that texture again.

 

 

2. If it is the conversion that drains the FPS, then it is smarter to convert the image once just as it loads and save the texture in the array instead of the surface? Sounds like it... tongue.png

 

Yes, there is, the global you declared is the identifier for that image, its GLuint, so to create more textuer you need an array , like

 

Gluint texture [5] // this will hold 5 texture for you 

 Changes in InitGL()

// then you have to change to following where i = 0, 1 , 2 , 3 , 4
glGenTextures( 1, &texture[i] );
glBindTexture( GL_TEXTURE_2D, texture[i] );

 

 

3. Is there any reason for me to save the image as a SDL_Surface instead of loading it from OpenGL directly? Can i do that?

 

Yes, the above way shows you how to convert your SDL_Surface to opengl texture for later use, you need to convert each image exactly once.

 

 

4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?

 

You have to write a bit of code, for this, you can create a class or struct that will hold the image information for you, like image width, height, image_id and other information.

 

 

 I am just a learner so tried you help you, hope this helps smile.png

0

Share this post


Link to post
Share on other sites
Quote

 

Hi and thanks for the help man!

 

I actually have a pretty sophisticated image loader that i've written myself but i only takes SDL_Surface's so i only have to make it work with the GL texture instead.

 

I plan to: 

1.open a image in SDL

2.convert it to a texture and save the proportions

3.maybe make a class that keeps track of if the image is in use (like a smart pointer) and if it isnt dispose of it

4.release the SDL_Surface and save all of the other stuff for later use

0

Share this post


Link to post
Share on other sites

If you dont want to use SDL_Surface then you can use any third party image library, I found SOIL very easy and simple to work with opengl, it supports wide range of Images and comes with handy functions. But always do what you are comfortable with :)

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