I have a class Rect such that it holds the width, height, x and y values of a shape. The class can draw using the values in the parameter and move the drawn rect.
Rect::Rect(w, h, x, y, const std::string &image_path) : _w(w), _h(h),
_x(x), _y(y)
{
SDL_Surface *surface = IMG_Load(image_path.c_str());
if (!surface) {
std::cerr << "Failed to create surface.";
}
//create texture
texture = SDL_CreateTextureFromSurface(Window::renderer, surface);
if (!texture) {
std::cerr << "Failed to create worker texture.";
}
SDL_FreeSurface(surface);
}
Rect::~Rect()
{
SDL_DestroyTexture(texture);
}
Rect::draw()
{
//where the constructor parameters are parsed
SDL_Rect rect= {_x, _y, _w, _h} ;
//extra code about some SDL texture stuff and RenderCopy
}
Rect::moveX(int x){
_x +=x;
}
In my Unit class, I include the class Rect and I create my units, draw them in the same function. There is another function in unit that moves rect by checking another value from another class that changes.
Unit::Unit()
Unit::~Unit()
void Unit::createUnit(int type, int x, int y){
if (type == 0)
{
Rect unit1(unitImageWidth, unitImageSizeHeight, x, y, "res/unit1.png");
}
if (type == 1)
{
Rect unit2(unitImageWidth, unitImageSizeHeight, x, y, "res/unit2.png");
}
}
void Unit::moveUnit(int x){
if(selection == 0)
{
unit1.movex(x);
}
if (selection == 1)
{
unit2.movex(x);
}
}
My question is:
When in Unit::moveUnit(), how can I reference the object Rect "unit1" and Rect "unit2" that are initialized in Unit::createUnit()?
When I try to compile, it say that unit1 and unit2 are undefined.