Return some kind of array or structure?

This is a discussion on Return some kind of array or structure? within the C++ Programming forums, part of the General Programming Boards category; Hey people I'm making a function to load an opengl texture, but instead of returning the texture ID as GLuint, ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    22

    Return some kind of array or structure?

    Hey people

    I'm making a function to load an opengl texture, but instead of returning the texture ID as GLuint, I want it to return also other values such as width, height, etc. So I can access them as pointers. Something like this pseudo-code:

    Code:
    tex = LoadTexture("texture.jpg");
    
    printf("Width: %i \n",tex->width);
    printf("Height: %i \n",tex->height);
    
    BindToSprite(tex->TextureID);
    What are the steps I'll need to follow in order to return something like that? I know I can have a structure to hold values since I have one that holds the FPS and other global variables, but how would I go into returning a structure of its own, that's not global but that can be spawned as many textures as I want to load?

    Thanks in advance!

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Make a class that acts as a container for image data. That way the member functions just load the texture into the class itself.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    22
    Quote Originally Posted by indigo0086 View Post
    Make a class that acts as a container for image data. That way the member functions just load the texture into the class itself.
    Yeah, the thing is... how? I mean, I know theories but I don't know how to do it code-wise :/ that was my idea too, using a class, but I dunno how would it apply in my case, code-wise.

    Thanks for the fast reply though!

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    So your header file would look something like this

    Code:
    typedef struct
    {
       int width, height;
       int alpha;  //etc...
    } header;
    
    class BMPimg
    {
       header* BMPheader;
       unsigned char imgData[]; //array that will hold your data 
       public:
           BMPimg(const char* fileName) { //loads file and manipulates private data and header info}
           ~BMPimg();
           int getWidth() { return BMPheader.width; }
           int getHeight() { return BMPheader.height; }
           unsigned char* retData { return imgData }
          //etc....
    }
    this way when you need to create an image using pointers you do this

    Code:
    int main()
    {
        BMPimg* Happyface = new BMPimg("happyface.bmp");
       
        //do what you will with the image such as
        glDrawPixels(HappyFace->getWidth(), HappyFace->getHeight(), //etc);
        delete HappyFace;
    
        return 0
    }

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    22

    Talking

    Quote Originally Posted by indigo0086 View Post
    So your header file would look something like this

    Code:
    typedef struct
    {
       int width, height;
       int alpha;  //etc...
    } header;
    
    class BMPimg
    {
       header* BMPheader;
       unsigned char imgData[]; //array that will hold your data 
       public:
           BMPimg(const char* fileName) { //loads file and manipulates private data and header info}
           ~BMPimg();
           int getWidth() { return BMPheader.width; }
           int getHeight() { return BMPheader.height; }
           unsigned char* retData { return imgData }
          //etc....
    }
    this way when you need to create an image using pointers you do this

    Code:
    int main()
    {
        BMPimg* Happyface = new BMPimg("happyface.bmp");
       
        //do what you will with the image such as
        glDrawPixels(HappyFace->getWidth(), HappyFace->getHeight(), //etc);
        delete HappyFace;
    
        return 0
    }
    COOL! exactly what I was looking for! ^^... thanks hehe I'm going to work right now but when I'm back home I'll try that... I didn't know you could place an "identifier" to the class right after the closing bracket... now it makes sense, huge thanks!

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    header* BMPheader;
    should be
    header BMPheader;

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    22
    Well, I tried your code... and I'm confused because if I try to modify the value inside the function (for example, to change the value in BMPheader.width ) it says it's not declared in that ambit... how can I solve that? :/ the code looks ok to me, but C++ is frickin' weird as always >.<

    Inside
    Code:
    BMPimg(const char* fileName) { //loads file and manipulates private data and header info}
    try adding BMPheader.width = 100; between the brackets. It doesn't work for me :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 12:29 PM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM

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