What is the easiest way to create an interactive grid for a C ++ game?
I was not using C ++ at the time, so I decided to take a small project to get familiar with it again. I'm trying to make a Chinese checkers game but I have no GUI experience in C ++. Is there a simple simple way to just make a grid (i.e. Bitmap hexagons or whatever) that when I click on it will give me the index number of the one I clicked on? If anyone has examples on how to do this, even with a 2D grid of squares, that would be helpful. Any help is appreciated, thanks!
a source to share
First of all, you need to decide which API you are going to use for your application. Qt? SDL? Win32? OpenGL? My recommendation is to use Qt and the QGraphicsView class (you can start with one of the Qt sample applications that uses QGraphicsView and modifies it according to your taste).
a source to share
It depends on your GUI toolkit. However, I believe that the two most likely ways are likely to either change to
-
Store coordinates for each of the squares, holes, or whatever you are trying to click on the game, and the click event handler uses the coordinates it determines to determine which square was clicked on.
-
Make each square its own widget. So when it is clicked it is the square itself that receives the click event and can be handled in a nice object oriented way. However, this would mean quite a few widgets for playing Chinese checkers.
a source to share
Typically the GUI takeit will allow you to override (e.g. via virtual functions) the event handler of the widget representing the board, so you will handle the mouse click event by calling some function of your own, like MouseClicked (int x, int y), where the handler will also give you mouse coordinates x and y.
If it's a rectangular grid, just an integer divides the coordinate by the cell width in pixels.
If it's a grid of hexagonal cells, then figuring it out will be more difficult. My guess is that you could first define a rectangular mesh denoting the rectangular centers of the hexagons, and then add more detection for the four triangular regions around the edges. The game can be quite useful with just defining rectangular cells, though, just ignoring triangular edges.
a source to share