Jump to content
  • Advertisement
Sign in to follow this  
Vortez

Help me get rid of this goto!

This topic is 2140 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

I just completed an OpenGL wrapper class, but at one point, i could not see any better solution that using this goto.

void COpenGL::LoadTexture(BYTE *pTex, UINT w, UINT h, UINT bpp, UINT format)
{
        if(!IsInitialized())
                return;
        
        if(Texture.ID == 0){
                CreateTexture();
                goto LoadTexJmp;
        }
        
        if(Texture.ID != 0){
                glBindTexture(GL_TEXTURE_2D, Texture.ID);
                
                if(Texture.Width != w || Texture.Height != h || Texture.BPP != bpp || Texture.Format != format){
                        
                        DeleteTexture();
                        CreateTexture();

                        LoadTexJmp:
                        Texture.Width = w;
                        Texture.Height = h;
                        Texture.BPP = bpp;
                        Texture.Format = format;

                        glTexImage2D(GL_TEXTURE_2D, 0, Texture.BPP, Texture.Width, Texture.Height, 0, Texture.Format, GL_UNSIGNED_BYTE, pTex);
                } else {
                        glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, Texture.Width, Texture.Height, Texture.Format, GL_UNSIGNED_BYTE, pTex);
                }
        }
}

Any idea?

Share this post


Link to post
Share on other sites
Advertisement

Why do you have an If Not Is Something guarding a return that short circuits the rest of the function? Why don't you just do an If Is Something to guard the entire function?

Share this post


Link to post
Share on other sites

Why do you have an If Not Is Something guarding a return that short circuits the rest of the function? Why don't you just do an If Is Something to guard the entire function?



This verges dangerously close to holy war territory, but I prefer that style personally. If you have multiple early-out conditions it makes more sense to do it that way than to have a lot of nested conditionals.

Share this post


Link to post
Share on other sites

Yes, please don't start a goto war, that was not my intention. I decided to keep the goto, after all since it make the code simpler. It's just a tool after all, to use with moderation.

 

Ex. the code above looked good, but

	if(Texture.ID == 0 || Texture.Width != w || Texture.Height != h || Texture.BPP != bpp || Texture.Format != format) {
		if(Texture.ID != 0)
			DeleteTexture();

In this case, the texture never get deleted, i think. Anyway, it's more confusing than letting it as it is.

 

Sorry for wasting your time...

 

This post can be closed.

Edited by Vortez

Share this post


Link to post
Share on other sites

Why do you have an If Not Is Something guarding a return that short circuits the rest of the function? Why don't you just do an If Is Something to guard the entire function?

 

It just feeled better that way for that particular pieces of code, this way avoid an extra tab for the code below, i use the other way for shorter function.

Edited by Vortez

Share this post


Link to post
Share on other sites

Yes, please don't start a goto war, that was not my intention. I decided to keep the goto, after all since it make the code simpler. It's just a tool after all, to use with moderation.
 
Ex. the code above looked good, but

	if(Texture.ID == 0 || Texture.Width != w || Texture.Height != h || Texture.BPP != bpp || Texture.Format != format) {
		if(Texture.ID != 0)
			DeleteTexture();

In this case, the texture never get deleted, i think. Anyway, it's more confusing than letting it as it is.
 
Sorry for wasting your time...
 
This post can be closed.

Erik Rufelt's code is much easier to follow than your original code. The suggestion of using another function is not a bad one either, and It too should be cleaner.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!