So it's this program which isn't behaving as it should.
these are the classes I'm haing problems with:
#ifndef _CMENU_H_
#define _CMENU_H_
#include "Vec2.h"
#include "Background.h"
#include "Sprite.h"
#include "BackBuffer.h"
class Button
{
public:
enum ButtonState
{
NORMAL, //when cursor is not near the button
ANIMATED //mouseover event
};
Button(const char*,const char*,const char*,Vec2&);
//const char* 's are the name of the bitmaps for the mask, normal image, and animated image;
//Vec2 is the position on the screen
virtual ~Button(void);
bool Update(POINT&); //takes the curent cursor position(POINT)and changes the button state
void Draw(const BackBuffer*); //draws the button
protected:
Sprite* image[2]; //image[0] is normal state,image[1] the animated image of the button
RECT rc; //rectangle which defines borders of the button
ButtonState State; //button state
};
class Menu
{
protected:
int NrButtons;
Background* m_imgBackground;//the screen background image
vector<Button*> v_button; //the button container
public:
Menu(const BackBuffer *pBackBuffer,const char*);//const char* is the name of the textfile
virtual ~Menu (void);
int Update ( POINT& mouse_pos,bool press_mouse);
void Draw (const BackBuffer *pBackBuffer);
};
#endif //CMENU_H
and this is the definition:
#include "CMenu.h"
Button::Button(const char* img1, const char* img2, const char* mask,Vec2& pos)
{
this->image[0]=new Sprite(img1,mask);
this->image[1]=new Sprite(img2,mask);
for (int i=0;i<2;i++)
this->image[i]->mPosition=pos;
this->rc.left =pos.x-this->image[0]->width()/2;
this->rc.top =pos.y-this->image[0]->height()/2;
this->rc.right =this->rc.left+ this->image[0]->width();
this->rc.bottom =this->rc.top+ this->image[0]->height();
this->State=NORMAL;
}
Button::~Button()
{
if(this->image!=NULL)
{
delete this->image[0];
this->image[0]=NULL;
delete this->image[1];
this->image[1]=NULL;
}
}
bool Button::Update(POINT& tmp)
{
if(this->rc.top<=tmp.y && this->rc.bottom>=tmp.y && this->rc.left<=tmp.x && this->rc.right>=tmp.x)
{
this->State=ANIMATED;
return true;
}
else
{
this->State=NORMAL;
return false;
}
}
void Button::Draw(const BackBuffer* pBackBuffer)
{
this->image[State]->setBackBuffer(pBackBuffer);
this->image[State]->draw();
}
Menu::Menu(const BackBuffer *pBackBuffer,const char* filename)
{
ifstream File;
string mask,imag,imag1;
Vec2 bt_position;
Button* tButton=NULL;
this->m_imgBackground=NULL;
m_imgBackground = new CImageFile;
File.open(filename);
File>>imag;
File>>this->NrButtons;
m_imgBackground->LoadBitmapFromFile(imag.c_str(), pBackBuffer->getDC());
for(int bt_index = 0; bt_index < NrButtons; bt_index++)
{
File >> mask; //rading the mask
File >> imag; //normal image
File >> imag1;//animated image
File >> bt_position.x;
File >> bt_position.y;
if(tButton!=NULL)
{
delete tButton;
tButton=NULL;
}
tButton=new Button(imag1.c_str(),imag.c_str(),mask.c_str(),bt_position);
this->v_button.push_back(tButton);
}
File.close();
}
Menu::~Menu()
{
if(this->m_imgBackground!=NULL)
{
delete this->m_imgBackground;
this->m_imgBackground=NULL;
}
this->v_button.empty();
}
int Menu::Update(POINT& mouse_pos,bool press_mouse)
{
for(int i=0;i<NrButtons;i++)
if(this->v_button[i]->Update(mouse_pos)&& true==press_mouse)
return i++;
return -1;
}
void Menu::Draw(const BackBuffer* temp)
{
m_imgBackground->Paint(temp->getDC(), 0, 0);
for(int i=0;i<this->NrButtons;i++)
this->v_button[i]->Draw(temp);
}
The trouble I'm having is with the last function, because only the last button read(from the textfile) gets drawn