Thread: Global variable not updating

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    11

    Global variable not updating

    Hello! I am trying to find out why my global variables (textureBank, textureFree, numTextures) are not being updated from the texture object's constructor method texture(SDL_Surface *rawSurface). It is as if there is a local copy in that block, which appears to be updated inside the function, but outside of it retains its original value. I have tried this with and without the static keywords. Thanks!

    Code:
    //texture.cpp
    #include "texture.h"
    #include "iostream"
    
    static GLuint textureBank[256];
    static bool textureFree[256];
    static int numTextures;
    
    texture::texture(SDL_Surface *rawSurface)
    {
        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\n";
                break;
            }
        }
    Code:
    #include "SDL/SDL.h"
    #include "SDL/SDL_opengl.h"
    #include "gfxUtilities.h"
    
    class texture
    {
          private:
                  int glTexture;
                  SDL_Rect imageRect;
                  SDL_Rect textureRect;
    
          public:
                 texture();
                 ~texture();
                 texture(SDL_Surface *rawSurface);
                 int getTextureIndex();
                 void drawTexture(int x, int y);
                 SDL_Rect getImageRect();
                 void generateTextureHandles();
    };

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    All of the textureFree elements will start as false, so your if condition will always be false. Could that be the problem? Or are you initing them to true somewhere?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Quote Originally Posted by oogabooga View Post
    All of the textureFree elements will start as false, so your if condition will always be false. Could that be the problem? Or are you initing them to true somewhere?
    Code:
    void texture::generateTextureHandles()
    {
        std::fill_n(textureFree, 256, true);
        glGenTextures( 256, textureBank);
    }
    Either way, it is reading the values as true at all times, even after I set them to false. I check the value I set in textureFree and it is correct until exiting the function.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    When do you call texture::generateTextureHandles()? If it's not called before you call texture::texture(SDL_Surface*), then textureFree[256] will initially have all elements false so the if condition will be false and nothing will happen.

    And why aren't these class static variables instead of globals?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    All elements test -TRUE-. I call generateTextureHandles() first.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Flustration View Post
    All elements test -TRUE-. I call generateTextureHandles() first.
    Sure, I'm not saying they aren't true. But how/when are you setting them? generateTextureHandles() is a member function of texture. So you must create a default object of type texture and then call generateTextureHandles() (or maybe it's called in the ctor). The simple fact is that you obviously haven't posted enough code for anyone to solve your problem.

    And why are these globals and not static members? Is there a purpose to that or is it incidental?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Okay, I have re-coded this using static member variables... but I am getting the exact same result. Below is my code and output.

    Code:
    //texture.cpp
    #include "texture.h"
    #include "iostream"
    
    void texture::initList()
    {
        glGenTextures( 256, textureBank);
    }
    
    
    GLuint texture::textureBank[256] = {};
    bool texture::textureFree[256] = {true};
    int texture::numTextures = 0;
    bool texture::listInitialized = false;
    
    
    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();
    
    };
    Global variable not updating-5teyc-png

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    IIRC, The below code only set textureFree[0] to true; do you believe otherwise?

    Tim S.

    Code:
    bool texture::textureFree[256] = {true};
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    AFAIK it sets every member as "true". Either way, the program is reading that the first member is "true" even after I set it false...

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Flustration View Post
    AFAIK it sets every member as "true". Either way, the program is reading that the first member is "true" even after I set it false...
    It only sets the first element to true. An initializer with less elements than the array sets all extra elements to 0.

    And I don't see anywhere where listInitialized is set to true.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Just going to post this to humor you...

    Code:
    void texture::initList()
        {
            glGenTextures( 256, textureBank);
            listInitialized = true;
        }
    I feel like no one read or understands the problem here; when I access my static variables from 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;
            }
        }
    they look like they have been updated properly... but when I access them the next time around when running this function again, it is as if I have a whole new set of variables (like my static variables were never updated). THAT is the problem. Please read the output and match it up with the cout statements. It ALWAYS hits the very first element, sees true, changes it to false... but then it turns out it was never changed to false at all.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Just going to post this to humor you...
    No need to be an a-hole about it. Look at the code you posted in post #7
    Code:
    void texture::initList()
    {
        glGenTextures( 256, textureBank);
    }
    You keep changing the code. I can't read your mind, buddy.

    I feel like no one read or understands the problem here
    That's often the feeling when you yourself are confused.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Quote Originally Posted by oogabooga View Post
    No need to be an a-hole about it. Look at the code you posted in post #7
    Code:
    void texture::initList()
    {
        glGenTextures( 256, textureBank);
    }
    You keep changing the code. I can't read your mind, buddy.
    I was not being an ................. My point is that the information was not relevant. I am just trying to refocus this on the input/output. I change a value on a static variable only to read it as another value later (the initial value).

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Just going to post the full code again, in case I'm being incredibly unclear.

    Code:
    //texture.cpp
        #include "texture.h"
        #include "iostream"
    
        void texture::initList()
        {
            glGenTextures( 256, textureBank);
            listInitialized = true;
        }
    
    
        GLuint texture::textureBank[256] = {};
        bool texture::textureFree[256] = {true};
        int texture::numTextures = 0;
        bool texture::listInitialized = false;
    
    
        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();
    
    };

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Remember (if you've been reading MY posts) that this is not doing what you want it to do:
    Code:
    bool texture::textureFree[256] = {true};
    You need to change you init function to:
    Code:
        void texture::initList()
        {
            glGenTextures( 256, textureBank);
            std::fill_n(textureFree, 256, true);
            listInitialized = true;
        }
    Believe it or not, you still may not have posted enough code.
    What about your main function?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

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, 04:11 PM
  3. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM