How to do controls like buttons in directx?

Started by
9 comments, last by SelethD 20 years, 3 months ago
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.
Advertisement
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();
}
>>>Syntax is same, Thinking is Different<<<
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.
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
}
Isn''t there a sample in the DirectX SDK that shows how to have Windows controls coexist with DirectDraw surfaces?
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.

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
Thanks everyone, ill research it some more.
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?
Well can you show me an example of a a ''pretty'' gameloop??

--
You''re Welcome,
Rick Wong
- sitting in his chair doing the most time-consuming thing..

This topic is closed to new replies.

Advertisement