Hey, Im pretty new to Dark GDK, but ive just made a couple of functions that work well for making simple game buttons (e.g. for game menus and stuff). It uses DGDK sprites to make the buttons.
* I have attached a Demo to show how it works
It is very untidy (no indents and stuff), im busy fixing it.
This is certainly not the way experts would do it, but it might be usefull to newbies. So I thought I would share it.
Any ideas would be helpful.
Just put this outside of the game funciton:
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
#define MaxButtons 50
#define ButtonSpriteIndex 1
#define ButtonInterval 10
#define ButtonIdle 0
#define ButtonActive 1
#define ButtonHidden 2
struct ButtonVars {int ID; int priority; int normal; int over; int pressed; int xpos; int ypos; int sound; int timer; int state; int sprite; bool mouseover; bool exists; bool visible;}; ButtonVars Button[MaxButtons];
int GetFreeButtonSprite(int start)
{
int finder = start;bool foundObject = false;
while (foundObject == false){if (dbSpriteExist (finder) == 1) {finder ++;}if (dbSpriteExist (finder) == 0) {foundObject = true;}}
return finder;
}
void MakeButton (int buttonID, int ButtonPriority, int x, int y, int buttonSound, int buttonNormal, int buttonOver, int buttonPressed)
{
if (buttonID < MaxButtons){
Button[buttonID].exists = true;
Button[buttonID].visible = true;
Button[buttonID].ID = buttonID;
Button[buttonID].priority = ButtonPriority;
Button[buttonID].normal = buttonNormal;
Button[buttonID].over = buttonOver;
Button[buttonID].pressed = buttonPressed;
Button[buttonID].xpos = x; Button[buttonID].ypos = y;
Button[buttonID].sound = buttonSound; Button[buttonID].timer = 0;
Button[buttonID].state = ButtonIdle;
Button[buttonID].sprite = GetFreeButtonSprite(ButtonSpriteIndex);
Button[buttonID].mouseover = false;
dbSprite(Button[buttonID].sprite,Button[buttonID].xpos,Button[buttonID].ypos,Button[buttonID].normal);
dbSetSpritePriority (Button[buttonID].sprite, Button[buttonID].priority);
}
}
int IsMouseOverButton(int buttonID)
{
int test_X = 0;
int test_Y = 0;
if ((dbMouseX() <= dbSpriteX(Button[buttonID].sprite) + dbSpriteWidth (Button[buttonID].sprite))&& (dbMouseX() >= dbSpriteX(Button[buttonID].sprite))){test_X = 1;}
if ((dbMouseY() <= dbSpriteY(Button[buttonID].sprite) + dbSpriteHeight (Button[buttonID].sprite))&& (dbMouseY() >= dbSpriteY(Button[buttonID].sprite))){test_Y = 1;}
if ((test_X == 1)&&(test_Y == 1)){return 1;} else {return 0;}
}
void ButtonRunCode(int buttonID)
{
if (Button[buttonID].visible == true){
if (IsMouseOverButton(buttonID) == 0)
{
dbSetSpriteImage (Button[buttonID].sprite, Button[buttonID].normal);
Button[buttonID].state = ButtonIdle;
}
if ((IsMouseOverButton(buttonID) == 1) && (dbMouseClick() == 0))
{
dbSetSpriteImage (Button[buttonID].sprite, Button[buttonID].over);
Button[buttonID].state = ButtonIdle;
}
if ((IsMouseOverButton(buttonID) == 1) && (dbMouseClick() == 1))
{
if (Button[buttonID].timer <= 0)
{
if (dbSoundExist(Button[buttonID].sound)){if (dbSoundPlaying (Button[buttonID].sound )==0) {dbPlaySound (Button[buttonID].sound);}}
}
dbSetSpriteImage (Button[buttonID].sprite, Button[buttonID].pressed);
if (Button[buttonID].state != ButtonActive){Button[buttonID].state = ButtonActive;}
}
if (Button[buttonID].timer > 0) {Button[buttonID].timer --;}
}
}
void RunButtons()
{
int buttonCycle = 0;
while (buttonCycle < MaxButtons)
{
if (Button[buttonCycle].exists == true)
{ButtonRunCode(buttonCycle);}
buttonCycle ++;
}
}
int IsButtonDown(int buttonID)
{
if (Button[buttonID].visible == true){
if (Button[buttonID].state == ButtonIdle){return 0;}
if (Button[buttonID].state == ButtonActive)
{
if (Button[buttonID].timer <= 0)
{
Button[buttonID].timer = ButtonInterval; return 1;}}} else {return 0;}
}
void ShowButton(int buttonID)
{
if (Button[buttonID].visible == false)
{
Button[buttonID].visible = true;
dbShowSprite (Button[buttonID].sprite);
Button[buttonID].state = ButtonIdle;
}}
void HideButton(int buttonID)
{
if (Button[buttonID].visible == true)
{
Button[buttonID].visible = false;
dbHideSprite (Button[buttonID].sprite);
Button[buttonID].state = ButtonHidden;
}}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
This first 3 values you can change to suite your need:
#define MaxButtons 50
- this is the max number of buttons that you can use in your
game (default is 50) - that does not mean you have to use all of them, it just tells the program how many buttons it might possibly have to run.
#define ButtonSpriteIndex 1
- The program will look for free sprites to use, this is the number from which it will start looking for free sprites. You can make this something like 1000 if you want to be sure that it does not effect your game.
#define ButtonInterval 10
- This is the timer for the buttons to guard agains repeated clicks
To make a button:
MakeButton (int buttonID, int ButtonPriority, int x, int y, int buttonSound, int buttonNormal, int buttonOver, int buttonPressed)
buttonID - The Button's Id
ButtonPriority - The Button's Sprite Priority
x, - The Button's X position
y, - The Button's Y position
buttonSound, - The Button's sound (make it 0 for no sound)
buttonNormal - The Button's Normal Image
buttonOver - The Button's Mouse over Image
buttonPressed - The Button's pressed Image
Important:
In your game loop you have to put RunButtons();
Other functions:
IsButtonDown(int buttonID)
- Returns 1 if the button is pressed and 0 if not.
e.g.
if (IsButtonDown(3) == 1)
{
dbText ( 10, 10, "The Button is down");
}
IsMouseOverButton(int buttonID)
- Returns 1 if the mouse is over the button and 0 if not.
e.g.
if (IsMouseOverButton(3) == 1)
{
dbText ( 10, 10, "The mouse is over the button");
}
ShowButton(int buttonID)
- Shows a button if it is hidden
HideButton(int buttonID)
- Hides a button, hidden buttons are inactive
Other stuff:
Button[buttonID].exists - will be false if the button does not exists and true if it does
Button[buttonID].visible - will be false if the button is hidden and true if it is visible
Button[buttonID].sprite - use this to refer to a button's sprite ID
________________
I am still new to C++ but I hope this is useful to someone
See demo attached