Help me get rid of this goto!

Started by
8 comments, last by King Mir 10 years, 4 months ago

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?

Advertisement

Use another function? Make it private to the class so it is just an implementation detail.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

void COpenGL::LoadTexture(BYTE *pTex, UINT w, UINT h, UINT bpp, UINT format)
{
	if(!IsInitialized())
		return;
	
	if(Texture.ID == 0 || Texture.Width != w || Texture.Height != h || Texture.BPP != bpp || Texture.Format != format) {
		if(Texture.ID != 0)
			DeleteTexture();
		
		CreateTexture();
		
		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 {
		glBindTexture(GL_TEXTURE_2D, Texture.ID);
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, Texture.Width, Texture.Height, Texture.Format, GL_UNSIGNED_BYTE, pTex);
	}
}

hehe right, i knew i was missing something, thx.

You weren't missing anything. It's just an exercise in refactoring, which is something you simply have to get experience in (like you are right now).

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?

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.


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.

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.

This topic is closed to new replies.

Advertisement