Global variable not updating

This is a discussion on Global variable not updating within the C++ Programming forums, part of the General Programming Boards category; I actually already changed that. -Anyway-, again, if you see my output here: You will see that the texture constructor ...

  1. #16
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    I actually already changed that.

    -Anyway-, again, if you see my output here:

    Name:  11604d1334796302-global-variable-not-updating-5teyc.png
Views: 76
Size:  16.5 KB

    You will see that the texture constructor is seeing that first element of textureFree[] as being "true" even after I set it to false here:

    Code:
    texture::texture(SDL_Surface *rawSurface)
        {
             if(listInitialized == false)
            {
                initList();
    
            }
    
            for(int a = 0; a < 256; a++)
            {
                std::cout << "texture # " << a << " status is " << textureFree[a];
    
                if(textureFree[a] == true)
                {
    
                    glTexture = a;
                    numTextures++;
                    textureFree[a] = false;
                    std::cout << " true, taking, numtextures: " << numTextures << ".\n";
                    break;
                }
            }

  2. #17
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    My point is that the information was not relevant.
    But we hear that every day. The fact is that you can't possibly know what's relevant or you would be able to solve the problem yourself. I understand exactly what your code is TRYING to do.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #18
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Again I KNOW WHAT THE CODE IS DOING!!!!!
    But you haven't posted enough code.

    Where is your main function?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #19
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I actually already changed that.
    AGAIN ... believe it or not, I can't read your mind!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #20
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Code:
        #include "texture.h"
        #include "iostream"
    
    
        GLuint texture::textureBank[256] = {};
        bool texture::textureFree[256] = {};
        int texture::numTextures = 0;
        bool texture::listInitialized = false;
    
         void texture::initList()
        {
            glGenTextures( 256, textureBank);
            std::fill_n(textureFree, 256, true);
            listInitialized = true;
        }
    
        void texture::drawTexture(int x, int y)
        {
              y = y - imageRect.y;
             // Bind the texture to which subsequent calls refer to
            glBindTexture( GL_TEXTURE_2D, textureBank[glTexture] );
    
            glColor3f(1,1,1);
            glBegin( GL_QUADS );
                //Bottom-left vertex (corner)
                glTexCoord2i( 0, 0 );
                glVertex3f( x, (y + textureRect.h), 0.0f );
    
                //Bottom-right vertex (corner)
                glTexCoord2i( 1, 0 );
                glVertex3f( (x + textureRect.w), (y + textureRect.h), 0.f );
    
                //Top-right vertex (corner)
                glTexCoord2i( 1, 1 );
                glVertex3f( (x + textureRect.w), y, 0.f );
    
                //Top-left vertex (corner)
                glTexCoord2i( 0, 1 );
                glVertex3f( x, y, 0.f );
            glEnd();
        }
    
        int texture::getTextureIndex()
        {
               return glTexture;
        }
    
        texture::texture()
        {
            if(listInitialized == false)
            {
                initList();
    
            }
    
        }
    
        texture::~texture()
        {
            numTextures--;
            textureFree[glTexture] = true;
        }
    
        SDL_Rect texture::getImageRect()
        {
                 return imageRect;
        }
    
        texture::texture(SDL_Surface *rawSurface)
        {
             if(listInitialized == false)
            {
                initList();
    
            }
    
            for(int a = 0; a < 256; a++)
            {
                std::cout << "texture # " << a << " status is " << textureFree[a];
    
                if(textureFree[a] == true)
                {
    
                    glTexture = a;
                    numTextures++;
                    textureFree[a] = false;
                    std::cout << " true, taking, numtextures: " << numTextures << ".\n";
                    break;
                }
            }
    
            SDL_Surface *surface;
    
            textureRect.w = nextPowerOfTwo(rawSurface->w);
            textureRect.h = nextPowerOfTwo(rawSurface->h);
    
            imageRect.w = rawSurface->w;
            imageRect.h = rawSurface->h;
            imageRect.x = 0;
            imageRect.y = textureRect.h - imageRect.h;
    
            surface = SDL_CreateRGBSurface(SDL_SRCALPHA, textureRect.w, textureRect.h, 32, 0, 0, 0, 0);
            blitFast(rawSurface, surface, 0, 0);
    
            GLint  nOfColors = surface->format->BytesPerPixel;
            GLenum texture_format;
    
             if (nOfColors == 4)     // contains an alpha channel
                {
                        if (surface->format->Rmask == 0x000000ff)
                                texture_format = GL_RGBA;
                        else
                                texture_format = GL_BGRA;
                } else if (nOfColors == 3)     // no alpha channel
                {
                        if (surface->format->Rmask == 0x000000ff)
                                texture_format = GL_RGB;
                        else
                                texture_format = GL_BGR;
                }
    
            glBindTexture(GL_TEXTURE_2D, textureBank[glTexture]);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexImage2D(GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0,texture_format, GL_UNSIGNED_BYTE, surface->pixels);
            if(surface)
               {SDL_FreeSurface( surface );}
        }
    Code:
    //texture.h
    #pragma once
    #include "SDL/SDL.h"
    #include "SDL/SDL_opengl.h"
    #include "gfxUtilities.h"
    
    class texture
    {
          private:
                  int glTexture;
                  SDL_Rect imageRect;
                  SDL_Rect textureRect;
                  void initList();
                  static bool listInitialized;
    
          public:
                 static GLuint textureBank[256];
                 static bool textureFree[256];
                 static int numTextures;
    
                 texture();
                 ~texture();
                 texture(SDL_Surface *rawSurface);
                 int getTextureIndex();
                 void drawTexture(int x, int y);
                 SDL_Rect getImageRect();
    
    };
    Code:
    //timeline.h
    #include "timeline.h"
    
    void timeline::handle(input inPut)
    {
     if(intersectRect(inPut.getMouseRect(), leftRect))
     {
         leftRed.drawTexture(0,500);
     }
     else
     {
         leftGreen.drawTexture(0,500);
    }
    
     blockHighlight.drawTexture(50,500);
     blockStd.drawTexture(150,500);
     blockStd.drawTexture(250,500);
     blockStd.drawTexture(350,500);
     blockStd.drawTexture(450,500);
     rightGreen.drawTexture(550,500);
    }
    
    timeline::timeline()
    {
        SDL_Surface* tempSurf;
    
        tempSurf = SDL_LoadBMP("gfx/timeline_block_highlight.bmp");
        blockHighlight = texture( tempSurf);
        SDL_FreeSurface( tempSurf );
    
        tempSurf = IMG_Load("gfx/timeline_block_std.png");
        blockStd = texture( tempSurf);
        SDL_FreeSurface( tempSurf );
    
        tempSurf = SDL_LoadBMP("gfx/timeline_left_green.bmp");
        leftGreen = texture( tempSurf);
        SDL_FreeSurface( tempSurf );
    
        tempSurf = SDL_LoadBMP("gfx/timeline_left_green_down.bmp");
        leftGreenDown = texture( tempSurf);
        SDL_FreeSurface( tempSurf );
    
        tempSurf = SDL_LoadBMP("gfx/timeline_left_green_highlight.bmp");
        leftGreenHighlight = texture( tempSurf);
        SDL_FreeSurface( tempSurf );
    
        tempSurf = SDL_LoadBMP("gfx/timeline_left_red.bmp");
        leftRed = texture( tempSurf);
        SDL_FreeSurface( tempSurf );
    
        tempSurf = SDL_LoadBMP("gfx/timeline_right_green.bmp");
        rightGreen = texture( tempSurf);
        SDL_FreeSurface( tempSurf );
    
        tempSurf = SDL_LoadBMP("gfx/timeline_right_green_down.bmp");
        rightGreenDown = texture( tempSurf);
        SDL_FreeSurface( tempSurf );
    
        tempSurf = SDL_LoadBMP("gfx/timeline_right_green_highlight.bmp");
        rightGreenHighlight = texture( tempSurf);
        SDL_FreeSurface( tempSurf );
    
        tempSurf = SDL_LoadBMP("gfx/timeline_right_red.bmp");
        rightRed = (tempSurf);
        SDL_FreeSurface( tempSurf );
    
        leftRect = leftGreen.getImageRect();
        leftRect.x = 0;
        leftRect.y = 500;
    
        rightRect = rightGreen.getImageRect();
        leftRect.x = 0;
        leftRect.y = 500;
    }
     timeline::~timeline()
    {
        //glDeleteTextures( 1, &texture );
    }
    Code:
    //timeline.h
    #pragma once
    #include "SDL_image.h"
    #include "SDL/SDL_opengl.h"
    #include "texture.h"
    #include "input.h"
    #include "gfxUtilities.h"
    
    
    
    class timeline
    {
          private:
                  texture blockStd;
                  texture blockHighlight;
                  texture leftGreen;
                  texture leftGreenDown;
                  texture leftGreenHighlight;
                  texture leftRed;
                  texture rightGreen;
                  texture rightGreenDown;
                  texture rightGreenHighlight;
                  texture rightRed;
    
                  SDL_Rect leftRect;
                  SDL_Rect rightRect;
    
          public:
                 timeline();
                 ~timeline();
                 void handle(input inPut);
    };
    Code:
    //main.cpp
    #include "SDL/SDL.h"
    #include "SDL/SDL_opengl.h"
    #include "texture.h"
    #include "gfxUtilities.h"
    #include "animation.h"
    #include "timeline.h"
    #include "FTGL/ftgl.h"
    #include <sstream>
    
    using std::stringstream;
    using std::string;
    
    int main( int argc, char* args[] )
    {
        freopen("CON", "w", stdout);
        freopen("CON", "w", stderr);
    
        //Start SDL,
    
        if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
        {
            printf("SDL failed to initialize: %s\n", SDL_GetError());
            return 1;
        }
    
        //Set up screen
    
        SDL_Surface* screen;
    
        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);
    
        if((screen = SDL_SetVideoMode( 900, 600, 32, SDL_OPENGL)) == NULL)
        {
         printf("Error creating SDL surface: %s\n", SDL_GetError());
        }
    
        //3d options
    
       glEnable( GL_TEXTURE_2D );
       glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
       glEnable(GL_BLEND);
       glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f);
       glViewport( 0, 0, 900, 600 );
       glClear( GL_COLOR_BUFFER_BIT );
       glMatrixMode( GL_PROJECTION );
       glLoadIdentity();
       glOrtho(0.0f, 900, 600, 0.0f, -1.0f, 1.0f);
       glMatrixMode( GL_MODELVIEW );
       glScalef(1, -1, 1);
       glLoadIdentity();
    
        //debug text
        FTGLPixmapFont font("gfx/font.ttf");
    
        // If something went wrong, bail out.
        if(font.Error())
        {
                        fprintf(stderr, "Error loading font: %d", font.Error());
        return -1;
        }
        // Set the font size and render a small text.
        font.FaceSize(20);
    
        //main app class instantiation
    
    
        timeline oTimeline;
        input oInput;
    
        //main loop
        bool quit = false; //loop quit var
    
        while(quit == false)
        {
         //quit if necessary
         quit = oInput.poll();
    
         //clear the buffer
         glClear(GL_COLOR_BUFFER_BIT);
    
         //handle the timeline including input/drawing
         oTimeline.handle(oInput);
    
         //debug text
         SDL_Rect mouseRect = oInput.getMouseRect();
         FTPoint textPoint(mouseRect.x, mouseRect.y);
         stringstream ssDebug;
         ssDebug << "X: " << mouseRect.x << " Y: " << mouseRect.y;
         font.Render(ssDebug.str().c_str(), -1, textPoint);
    
         SDL_GL_SwapBuffers();
        }
    
        //Quit SDL
        SDL_Quit();
    
        return 0;
    }
    Last edited by Flustration; 04-18-2012 at 08:57 PM.

  6. #21
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Aha, now we have it! (Sorry if I was a bit of an a-hole.)

    This line in main:
    Code:
        timeline oTimeline;
    fires off a bunch of default ctors for all the textures in the timeline class before the code for the timeline ctor executes. Those default texture ctors do not actually aquire textures and therefore do not increment the count or set any flags to false. All that happens is that the first invocation calls initList() (which of course sets all the flags to true).

    But then in the code of the timeline ctor all the textures are replaced by statements like this
    Code:
    blockHighlight = texture( tempSurf);
    The above will call the dtor for blockHighlight before replacing it with the new texture object. That's the problem. They are calling the dtor which decrements the count and sets index glTexture to true (glTexture presumably being 0 all the time). And that's done AFTER the ctor for the new texture fires!

    One solution is to make them all pointers so they don't fire off the texture ctor before the code in the timeline ctor gets to do it's thing.

    Then you'd have lines like
    Code:
    blockHighlight = new texture( tempSurf);
    and you'd have to delete them all in timelines dtor.


    Here's a mini example of the problem:
    Code:
    #include <iostream>
    using std::cout;
    
    class A {
    public:
        static int n;
        A() {}
        A(int x) { ++n; }
        ~A()     { --n; }
        static void show() { cout << n << "\n"; }
    };
    
    int A::n = 0;
    
    class B {
    public:
        A a1, a2, a3;
        B() {
            a1 = A(1);
            a2 = A(2);
            a3 = A(3);
        }
    };
    
    int main() {
        B b;
        A::show();
    }
    You might think that A::show() should print 3, but it will print 0.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #22
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Quote Originally Posted by oogabooga View Post
    Aha, now we have it! (Sorry if I was a bit of an a-hole.)

    This line in main:
    Code:
        timeline oTimeline;
    fires off a bunch of default ctors for all the textures in the timeline class before the code for the timeline ctor executes. Those default texture ctors do not actually aquire textures and therefore do not increment the count or set any flags to false. All that happens is that the first invocation calls initList() (which of course sets all the flags to true).

    But then in the code of the timeline ctor all the textures are replaced by statements like this
    Code:
    blockHighlight = texture( tempSurf);
    The above will call the dtor for blockHighlight before replacing it with the new texture object. That's the problem. They are calling the dtor which decrements the count and sets index glTexture to true (glTexture presumably being 0 all the time). And that's done AFTER the ctor for the new texture fires!

    One solution is to make them all pointers so they don't fire off the texture ctor before the code in the timeline ctor gets to do it's thing.

    Then you'd have lines like
    Code:
    blockHighlight = new texture( tempSurf);
    and you'd have to delete them all in timelines dtor.


    Here's a mini example of the problem:
    Code:
    #include <iostream>
    using std::cout;
    
    class A {
    public:
        static int n;
        A() {}
        A(int x) { ++n; }
        ~A()     { --n; }
        static void show() { cout << n << "\n"; }
    };
    
    int A::n = 0;
    
    class B {
    public:
        A a1, a2, a3;
        B() {
            a1 = A(1);
            a2 = A(2);
            a3 = A(3);
        }
    };
    
    int main() {
        B b;
        A::show();
    }
    You might think that A::show() should print 3, but it will print 0.
    cheers

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global variable and static extern global variable
    By gunjansethi in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 01:00 AM
  2. Replies: 8
    Last Post: 09-27-2010, 05:11 PM
  3. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 04:35 AM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 04:31 PM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21