Ok, I know GUI isn't the funnest thing on the planet. But here is a nice OOP way to handle buttons for your login screen, or even in game GUI.
So here we go. First your going to need to create GUI.cpp and GUI.h. Paste the following code into them.
GUI.h
// Button Handler Class
// Coded by: Dylan Marsh (aka Crank)
#pragma once
class ButtonHandler
{
private:
int Image;
int Priority;
int X;
int Y;
int Width;
int Height;
public:
int ID;
bool Clicked(void);
bool Hovered(void);
void Delete(void);
void Create(int iID, char* buttonFile, int iX, int iY, int iPriority);
};
GUI.cpp
// Button Handler Class
// Coded by: Dylan Marsh (aka Crank)
#include "DarkGDK.h"
#include "GUI.h"
void ButtonHandler::Create(int iID, char* buttonFile, int iX, int iY, int iPriority)
{
dbLoadImage(buttonFile, iID);
ID = iID;
Image = iID;
Priority = iPriority;
X = iX;
Y = iY;
dbSprite(ID, X, Y, Image);
dbSetSpritePriority(ID, Priority);
Width = dbSpriteWidth(ID);
Height = dbSpriteHeight(ID);
}
bool ButtonHandler::Hovered(void)
{
if(dbMouseX() > X
&& dbMouseX() < X + Width
&& dbMouseY() > Y
&& dbMouseY() < Y + Height )
{
return true;
}
else
{
return false;
}
}
bool ButtonHandler::Clicked(void)
{
if(Hovered() && dbMouseClick())
{
return true;
}
else
{
return false;
}
}
void ButtonHandler::Delete(void)
{
dbDeleteSprite(ID);
dbDeleteImage(Image);
ID = 0;
Image = 0;
Priority = 0;
X = 0;
Y = 0;
Width = 0;
Height = 0;
}
Now that we have the code taken care of, lets learn how to use it! First we need to make a button object. So in our Main.cpp, lets call the following code:
ButtonHandler PlayButton;
PlayButton.Create( 1, "playbutton.png", 0, 0, 1 );
But lets say we want to have multiple buttons at the same time. No problem:
// Tip: Make sure you use DIFFERENT ID's for each. Could be a common mistake in your code.
ButtonHandler PlayButton;
PlayButton.Create( ID, "playbutton.png", X, Y, Priority );
ButtonHandler QuitButton;
PlayButton2.Create( ID, "quitbutton.png", X, Y, Priority );
There you have it! I also included the ability to check for hover, click and also a little function to delete your button. Feel free to add to this and use it any way you wish. Just credit me please. Also if you add more functionality or improve this code, post your changes here so I can update this post. Also, if I made a mistake please let me know. Constructive criticism is welcome!