ok dbCLS() (even though its slow) solved the problem, but i found a new one (
) i want to make an update function in the class, so when the mouse is over it it moves abit around, and when the mouse is away it goes back to its position, the question is the following:
see my code:
UIClasses.h
#pragma once
#include <d3dx9.h>
class button
{
private:
int buttonID;
int x_pos;
int y_pos;
int width;
int hight;
int SpriteID;
char *text;
bool activated;
bool mouseover;
D3DXFONT_DESC lf;
ID3DXFont* Font;
public:
//contructors
button();
button(int iSprite);
button(int iSprite, int x, int y, char *szText);
button(int iSprite, int x, int y, int bWidth, int bHight, char *szText);
~button(); //destructor
void SetPosition(int ix, int iy);
void SetSize(int bWidth, int bHight);
void SetText(char *szText);
int GetPositionX() {return x_pos;};
int GetPositionY() {return y_pos;};
int GetWidth() {return width;};
int GetHight() {return hight;};
bool Draw();
void Update();
};
UIClasses.cpp
#include "UIClasses.h"
#include "DarkGDK.h"
int ButtonImageID = 4001;
int BTNSPRITEIDGENERATOR =4001;
int ButID = 1;
int BarID = 1;
int actbutimgID = 4201;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
button::button()
{
buttonID = ButID++;
dbLoadImage("Dataimagesbutton.jpg",ButtonImageID);
activated = false;
mouseover = false;
x_pos=0;
y_pos=0;
width=200;
hight=50;
SpriteID = BTNSPRITEIDGENERATOR++;
dbDrawSpritesFirst();
ZeroMemory(&lf, sizeof(D3DXFONT_DESC));
lf.Height = 25; // in logical units
lf.Width = 12; // in logical units
lf.Weight = 500; // boldness, range 0(light) - 1000(bold)
lf.CharSet = DEFAULT_CHARSET;
lf.Quality = 0;
lf.PitchAndFamily = 0;
strcpy(lf.FaceName, "Times New Roman"); // font style
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
button::button(int iSprite)
{
buttonID = ButID++;
dbLoadImage("Dataimagesbutton.jpg",ButtonImageID);
activated = false;
mouseover = false;
x_pos=0;
y_pos=0;
width=200;
hight=50;
SpriteID = iSprite;
dbDrawSpritesFirst();
ZeroMemory(&lf, sizeof(D3DXFONT_DESC));
lf.Height = 25; // in logical units
lf.Width = 12; // in logical units
lf.Weight = 500; // boldness, range 0(light) - 1000(bold)
lf.CharSet = DEFAULT_CHARSET;
lf.Quality = 0;
lf.PitchAndFamily = 0;
strcpy(lf.FaceName, "Times New Roman"); // font style
}
button::button(int iSprite, int x, int y, char *szText)
{
buttonID = ButID++;
dbLoadImage("Dataimagesbutton.jpg",ButtonImageID);
activated = false;
mouseover = false;
x_pos = x;
y_pos=y;
width=100;
hight=50;
SpriteID = iSprite;
text=new char;
strcpy(text,szText);
dbDrawSpritesFirst();
ZeroMemory(&lf, sizeof(D3DXFONT_DESC));
lf.Height = 25; // in logical units
lf.Width = 12; // in logical units
lf.Weight = 500; // boldness, range 0(light) - 1000(bold)
lf.CharSet = DEFAULT_CHARSET;
lf.Quality = 0;
lf.PitchAndFamily = 0;
strcpy(lf.FaceName, "Times New Roman"); // font style
}
button::button(int iSprite, int x, int y, int bWidth, int bHight, char *szText)
{
buttonID = ButID++;
dbLoadImage("Dataimagesbutton.jpg",ButtonImageID);
activated = false;
mouseover = false;
x_pos=x;
y_pos=y;
width=bWidth;
hight=bHight;
SpriteID = iSprite;
text = new char;
strcpy(text,szText);
dbDrawSpritesFirst();
ZeroMemory(&lf, sizeof(D3DXFONT_DESC));
lf.Height = 25; // in logical units
lf.Width = 12; // in logical units
lf.Weight = 500; // boldness, range 0(light) - 1000(bold)
lf.CharSet = DEFAULT_CHARSET;
lf.Quality = 0;
lf.PitchAndFamily = 0;
strcpy(lf.FaceName, "Times New Roman"); // font style
}
button::~button()
{
dbDeleteSprite(SpriteID);
dbCLS();
}
void button::SetPosition(int ix, int iy)
{
x_pos = ix;
y_pos = iy;
}
void button::SetSize(int bWidth, int bHight)
{
width = bWidth;
hight = bHight;
}
void button::SetText(char *szText)
{
strcpy(text, szText);
}
bool button::Draw()
{
if (x_pos < 0 || y_pos < 0)
{
return false;
goto end;
}
if (width < 0 || hight < 0)
{
return false;
goto end;
}
if (ButtonImageID > 4100)
{
return false;
goto end;
}
dbSprite(SpriteID, x_pos, y_pos, ButtonImageID);
dbSetSprite(SpriteID,1,0);
dbSizeSprite(SpriteID, width, hight);
Font = 0;
RECT rect;
IDirect3DDevice9* Device = dbGetDirect3DDevice();
D3DXCreateFontIndirect(Device, &lf, &Font);
rect.left = x_pos;
rect.top = y_pos;
rect.right = x_pos+width;
rect.bottom = y_pos+hight;
Font->DrawTextA( NULL, (LPCSTR)text, -1, &rect, DT_CENTER | DT_VCENTER, dbRGB(255,200,19) );
Device->Release();
end:
return true;
}
void button::Update()
{
if ((dbMouseX() > x_pos) && (dbMouseY() > y_pos) && (dbMouseX() < x_pos+width) && (dbMouseY() < y_pos+hight))
{
if(mouseover == false)
{
mouseover = true;
x_pos-=5;
y_pos+=5;
}
}
else
{
if (mouseover == true)
{
mouseover = false;
y_pos-=5;
x_pos+=5;
}
}
dbSprite(SpriteID, x_pos, y_pos, ButtonImageID);
if (dbSpriteExist(SpriteID))
{
Font = 0;
RECT rect;
IDirect3DDevice9* Device = dbGetDirect3DDevice();
D3DXCreateFontIndirect(Device, &lf, &Font);
rect.left = x_pos;
rect.top = y_pos;
rect.right = x_pos+width;
rect.bottom = y_pos+hight;
Font->DrawTextA( NULL, (LPCSTR)text, -1, &rect, DT_CENTER | DT_VCENTER, dbRGB(255,200,19) );
Device->Release();
}
}
main.cpp
// Dark GDK - The Game Creators - www.thegamecreators.com
// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application
// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
#include "UIClasses.h"
#include <vector>
using namespace std;
// the main entry point for the application is this function
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
vector<button> BTN(0);
int ypos = 0;
int isid = 1;
for (int i = 0; i < 3; i++)
{
button nbtn(isid++,0, ypos, 200, 40, "test");
ypos+=50;
BTN.push_back(nbtn);
}
for( int i = 0; i < 3; i++)
{
BTN[i].Draw();
}
// our main loop
while ( LoopGDK ( ) )
{
for(int i2 = 0; i2 < BTN.size(); i2++)
{
BTN[i2].Update();
}
// update the screen
dbSync ( );
}
// return back to windows
return;
}
so as you see when i call the function button::Update() in the loop it works fine, updates the position but it doesnt remove the old sprite, so it looks like 2 sprites over each other, even though they both have the same sprite ID, looks like this:
....____________
...|___________...|
|........................|..| <----- old sprite
|........................|_|
|____________| <--------new sprite