Noj
Noj

Reputation: 164

how can i access to a byte array of array of different size?

i facing a problem when i try to access to an array of array with a pointer, i have several arrays declared as following :

BYTE a[5] = {0xcc,0xaa,0xbb,0xcc,0xff};
BYTE b[3] = {0xaa,0xbb,0xff};

thos BYTE arrays represents image that i want to load in memory from a dll, i can access them separately with no difficulty, but i want to access them from a loop with a pointer ...

i tried to put them into an array of array like this :

BYTE* c[2] = {a,b};

but when i want to loop through this pointer c[i] doesent load image at i index into memory, how could i fix it?

im trying to draw images with this method within a loop to avoid repating lines ( consider c is images[i])

void MenuIcon::drawIcons(LPDIRECT3DDEVICE9 npDevice)
{
    isMenuVisible = (Pos.x < 100) && (Pos.x > 0)?  true : false;

    if(isMenuVisible)
    for (int i = 0; i < 2; i++)
    {
        if (icon[i].img == NULL)D3DXCreateTextureFromFileInMemory(npDevice, &images[i], sizeof(images[i]), &icon[i].img);
        DrawTexture(icon[i].coord.x, icon[i].coord.y, icon[i].img);
    }

}

i try to use my method like this after reading your answer :

 void MenuIcon::drawIcons(LPDIRECT3DDEVICE9 npDevice)
{
    isMenuVisible = (Pos.x < 100) && (Pos.x > 0)?  true : false;

    if(isMenuVisible)
    for (size_t i = 0; i < images.size(); i++)
    {

        std::vector<BYTE>& curArray = images[i];
        if (icon[i].img == NULL)D3DXCreateTextureFromFileInMemory(npDevice, &curArray, curArray.size(), &icon[i].img);
        DrawTexture(icon[i].coord.x, icon[i].coord.y, icon[i].img);
    }

}

but it still not drawing nothing .... maybe &curArray is not called proprely?? NB -> i logged both image and size of arrayofarray and it return me correct values....

Upvotes: 0

Views: 541

Answers (1)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

You can use std::vector:

#include <vector>
#include <iostream>
#include <iomanip>

typedef int BYTE;
typedef std::vector<BYTE> ByteArray;
typedef std::vector<ByteArray> ArrayOfByteArray;

using namespace std;

int main()
{
   // fill our array
   ArrayOfByteArray c = {{0xcc,0xaa,0xbb,0xcc,0xff}, 
                         {0xaa,0xbb,0xff}
                         // add more as desired...
                         };

   // loop on each entry
   for (size_t i = 0; i < c.size(); i++)
   {
      // get the byte array for entry i
      std::vector<BYTE>& curArray = c[i];

      // output the values found for this array
      for (size_t j = 0; j < curArray.size(); ++j)
         cout << std::hex << curArray[j] << " ";

      cout << "\n";
   }
}

Live Example

The std::vector knows the sizes of each of the entries, unlike an array.

Take the code here and add to it to suit what you're attempting to do with the images.

Upvotes: 3

Related Questions