• ### Announcements

#### Archived

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

# Need help with a "button" class...

## Recommended Posts

Hiya alls I''m trying to write a generic "button" class that I can use in my projects. The stuff I''m writing at the moment is all 2D using OpenGL. I''m not using Ortho mode (basically because I don''t really get it - yet!). All my "buttons" so far have been simple textured quads, so here''s what I was trying to do: Class CButton { private: int x, y; // coordinates on screen (Z = 0) int height, width; // height and width int texture; // which texture to apply bool clickable, visible // whether it''s visible/clickable void (*pfn)(void) // pointer to a function public: void render(void); }; OK, to explain the function pointer a bit: I''m basically going to use this for interfaces, so I''d like to make, say, a button for "New Game". Assuming this calls a function NewGame(); if clicked, I''d need: CButton NewGameButton(50,50,50,50,1,1,1,???????????); ...to declare the new instance of CButton, right? Now, I''m not great at C++, so I''m having a couple of problems. Firstly, I cannot seem to correctly write the constructor for it. I''ve tried to apply examples from other classes, but I think it''s the function pointer which is screwing me up. Basically what happens is it all compiles fine, and then when I try to declare a new object of the CButton type, I get the compiler message "CButton::CButton - no overloaded function takes 8 parameters" which makes BUGGER ALL sense to me. Secondly, I''m basically not sure I''m actually doing it the right way. I asked my C++ lecturer (I''m at Uni btw) about it and he told me two things: (1) I should be using "container" classes, and (2) Concentrate on the syllabus please Can someone please help me out here? I''m fed up rewriting code over and over again just to make little clickable boxes Thanks! RM. ------------------------------------------------------------ Yes. it''''s true, I DO wield the ugly stick. And I see I have beaten you with it before!

##### Share on other sites
This better not be a task from your teacher or I will hunt you down .

cbutton.h

  class CButton{public:   // constructor   CButton( int a_nX, int a_nY, int a_nHeight, int a_nWidth );         ...protected:   int m_nHeight, m_nWidth, m_nXPos, m_nYPos;   int m_nTextureID;   bool m_bClickable, m_bVisible;}

cbutton.cpp

  // ConstructorCButton::CButton( int a_nX, int a_nY, int a_nHeight, int a_nWidth ){  this->m_nXPos = a_nX;  this->m_nYPos = a_nY;  this->m_nWidth = a_nWidth;  this->m_nHeight = a_nHeight;  // Visible and clickable by default.  this->m_bClickable = m_bVisible = true;};

Also, instead of using function pointers, make a message system of somekind. When you know how to do it. Never write could you wont manage and you wont be so frustrated :D.

- Zorak - Neat Fella.

##### Share on other sites
Thanks Zorak For some reason your constructor code made more sense than any of the examples I got from books Oh, and it''s not a class assignment - it''s something I want to write to include as part of my game basecode for menus.

However, I still need to know how to "tie" a function to an instance of Cbutton - or was that what you meant about the message system? Sorry if it is, but I didn''t understand - I guess I''m just dumb

Oh, and is protected the same as private?

Thanks,

RM.

------------------------------------------------------------
Yes. it''''s true, I DO wield the ugly stick. And I see I have beaten you with it before!

##### Share on other sites
Protected is the same as private, but inherited classes can still access the methods/variables.

As for a message system, you can do it in a number of different ways. An easy one would be to have a method in the button class that you call when your mousebutton is pressed. When its pressed you call the method with the X and Y position and see if its in the box of your button.

ie.

  void CButton::CheckMouse ( int a_nX, int a_nY ){    // Sets this->m_bPressed to true if position a_nX and a_nY    // is within the button box.    this->m_bPressed = (a_nX > this->m_nX &&                        a_nX < (this->m_nX + this->m_nWidth) &&                        a_nY > this->m_nY &&                        a_nY < (this->m_nY + this->m_nHeight));};bool CButton::isPressed ( void ){    return this->m_bPressed;};

And then in your loop you check with the isPressed() method. Thats an easy solution. You could also call your callback function in the CheckMouse() method directly.

- Zorak - Neat Fella.

##### Share on other sites
Hmmm, thanks again Z. I''m going to have to think a little about this to understand it properly. The help is much appreciated

Have a nice day!

RM.

------------------------------------------------------------
Yes. it''''s true, I DO wield the ugly stick. And I see I have beaten you with it before!

• ### Forum Statistics

• Total Topics
627700
• Total Posts
2978695

• 21
• 14
• 12
• 10
• 12