Archived

This topic is now archived and is closed to further replies.

How to do controls like buttons in directx?

This topic is 5091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make a clone of a good old classic, Tradewars, from the BBS days, however I am going to make it in 2d graphics ala DirectDraw. So how do I get controls like buttons and text input boxes to be able to display on the DirectDrawSurface? Or do I have to actually create my own from scratch (I hope not)? Any help with this will really be beneficial, since I have no clue how to do it. Also if you can give me some code or pseudo code example, please. btw im using VC++6 and DirectX 8.

Share this post


Link to post
Share on other sites
Hi!
to do buttons just make a class with these variables:
int x,y; //For button location
int Length,Width; //For button dimensions
bool BClicked; //It will be true if your buttons was clicked
bool BMouseOn; //It will be true if mouse is on the button

now to render my button here''s my code, i change colour of my button when mouse comes over it:
void Button::Render(long* MouseXPos,long* MouseYPos,int IsClicked)
{

//Check if the mouse pointer is on the button then do the followoing
if( ( (*MouseXPos)>=x && (*MouseXPos)<=(x+length) ) && ( (*MouseYPos)>=y && (*MouseYPos)<=y+height) )
{
MouseOn=true;
//And also check if it is also cliked or not.
//If it is clicked changed vertex colour to normal and
//if not then change vertex colour to a dimmer state
if(IsClicked)
{vertexColour=0xFFFFFFFF;
Clicked=true;}
else
{vertexColour=0xDDDDDDFF;
Clicked=false;}
}
else
{
vertexColour=0xFFFFFFFF;
Clicked=false;
MouseOn=false;
}

//Render the button
RenderButton();
}

Share this post


Link to post
Share on other sites
Please read up on the following design patterns. What you are doing has been done before and if you Google the net for more articles on these patterns, you will most deffinately find examples with your specific problem outlined.

composite
Factory
Abstract Factory

Unless there''s an opensoure project fitting your needs at sourceforge, I''d say that you have to create your own system tackling this problem. These patterns will give you direction an robustness to your solution.

Share this post


Link to post
Share on other sites
Thaks for the info, ill check those links.

I had to make my own buttons back in the DOS days, but I was hoping with directx that I wouldnt have to go back there.

Any suggestions on where to put everything since its a flowing enviroment?

Here is my way of doing it, dont know if ther is a better way or not

Frame function //is called 60 times per second by the app
{
read in mouse x,y
read in mouse button states

draw background
draw buttons
blit to display

process buttons //check if clicked or not
adjust mouse button states
}

Share this post


Link to post
Share on other sites
Yes, but its with a Dailog Box that is created and stored as a resource.

I just need a simple button on the screen, not in a dialog box
and a simple input for IP address, that can be right on the screen, not dialog box.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think you should be able to use the windows controls in the DX app so you won''t have to write the GUI from scratch. I''m no DX user my self so I can''t swear on it But I think if you google a bit. You''ll find the info there.

Regarding your code, I would actually do the input/button-state-checking before I rendered it. Because if the mouse button is down (over the GUI-button) You would probably like to change the GUI-button state to down. And then render it with the state down. If you don''t you''ll get an ugly hack

A lame example:

void Game::Update()
{
input.Pool(); // Pools the input devices
gui.Update(); // Updates the gui, and checks for control-state changes (button, checkbox, etc)

game.Render();
gui.Render();
graphicsManager.Flip();
}

I''m currently also writing my own 3d engine with a whole new in-game gui written in OGL.

I''m a big fan of design and everything around it so I''m quite into it actually :D

If you think I could help I''ll be glad to do so.
You can find me in #haste on quakenet with the nick Shakta
or at my e-mail. daniel.persson@cenara.com

Probably lot''s of typos and error but what the heck I''ve been in school sunday from 13.30 to 03.00 writing on a damn compiler for a school project and I''m dead tired so this will have to do

Enjoy

Share this post


Link to post
Share on other sites
quote:
Yes, but its with a Dailog Box that is created and stored as a resource.

Well, since a dialog is a window, just like buttons are, use the same technique they do, except with a button whose parent is your window. Right?

Share this post


Link to post
Share on other sites
quote:
Original post by SelethD
I just need a simple button on the screen, not in a dialog box
and a simple input for IP address, that can be right on the screen, not dialog box.

In this case, I recant the suggestion for the use of the aformentioned patterns. Although great reading to expand your thinking, they will not be necessary to implement such simple controls.

I suggest (simple) polymorphic control classes. The base class for both the edit box and the button should have a focus boolean and some functions to test a point for OnClick(point).

Functions like:

public:
virtual Activate() = 0;
virtual OnClick(POINT p);
virtual OnButtonDown() = 0;
virtual OnEnter(){if(Focused){Activate()};};
protected:
bool Focued;


Then derive the edit and the button class from this same class to work a rudimentary GUI. The control Class should have a character array associated with it filled when the OnButton is called.

Also, look up "sscanf"

int a,b,c,d;
char buffer[256] = {0};
//enter text
sscanf(buffer,"%i.%i.%i.%i", &a, &b, &c, &d);

cheers

Share this post


Link to post
Share on other sites