I've just started programming in C++ and I'm having a problem with accessing an array from a structure that I defined.
I'm using OpenGL to draw an array of pixel data to the screen using a texture. The array of pixel data is stored in an Image structure:
struct Image {
GLubyte *pixels;
int width;
int height;
int bitrate;
Image(int w, int h, GLubyte* pixels, int bitrate){
this->width = w;
this->height = h;
this->pixels = pixels;
this->bitrate = bitrate;
}
};
The image is initialized like so:
GLubyte* pix = new GLubyte[WIDTH*HEIGHT*3];
Image image(WIDTH, HEIGHT, pix, 3);
There is a separate structure called Screen which is initialized using an Image instance:
struct Screen{
int width, height;
Image* screen;
void setScreen(Image* screen, const int width, const int height){
this->screen = screen;
this->width = width;
this->height = height;
}
};
The screen is initialized like so (right after the Image declaration):
displayData = new Screen();
displayData->setScreen(&image, WIDTH, HEIGHT);
When I access the Width or Height variables it gets the correct value. I then pass the screen instance to this method:
void renderScreen(Screen *screen){
std::cout << "Rendering Screen" << std::endl;
for(int x = 0; x < screen->width; x++){
for(int y = 0; y < screen->height; y++){
std::cout << "rendering " << x << " " << y << std::endl;
//Gets here and crashes on x - 0 and y - 0
screen->write(0xFFFFFFFF, x, y);
}
}
std::cout << "done rendering Screen" << std::endl;
}
which is called like this:
render(displayData); (displayData is a Screen pointer)
Pointers are quite new to me and I don't know rules about passing pointers to structures or anything like that.