[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Clicking on hexagon map



>The problem is the following: I have to write a small
>map editor for a roleplaying game. The map is build
>out of lots of hexagon fields (fields with six edges,
>if you have no idea how they are looking, I posted
>a small figure at
>http://traestralon.sourceforge.net/hexagons.gif). For
>working with this map it is necessary to get additional
>information about the content of the field, that can
>not be displayed directly on the map. In order to get
>these information you have to click the field you want
>to get more infos about, then a window opens and tells
>you more about the field. My problem is how to find
>out which field was clicked...

There is a really clever way of doing this kind of thing,
which is shortly described in the official OpenGl user's guide,
(the so-called red book), I need to implement it myself in my app,
but I have first to grasp a few minor details, so I can only give you
the general ideia, which is:

1) You need to have a double buffer, so while the user is
seeing the foreground layer, you can draw what you want in the 
background layer (a GDK pixmap will do).

2) Draw all your objects in the background layer, using a different
color for each one. Get the color in this background buffer of the pixel
 where the user pressed the mouse. Compare this color with the color
 of each object in the background buffer, when the color is the same, 
you found the object selected by the user! Please note: these colors
have nothing to do with the real colors of the objects, the user will
never see them.

This might be a overkill in your case, don't know, judge it yourself.

The advantages are:

1) This works for arbitrarily complex objects that were subject to
arbitrarily complex rotation, scaling etc... operations before
being shown.

2) It is a very quick operation, you just have to draw the whole
picture in the background. The whole procedure is expected 
to take a fraction of a second, depending of the number of objects 
(and the number of colors that your hadware is able to represent,
 see below).

3) It doesn't seem difficult to implement... once you
know how to play with the rgb codes for each pixel...

At first glance, it might appear that you are limited by the 
number of colors of your hardware, but that is not really
the case. If you have more objects than colors, all you need
is to draw again the same picture in the background, as many times
as needed. If your user has a old graphic card, which can 
draw only with 8 bits per pixel, i.e. 256 colors, and you have
800 objects, first you will need to draw all of them, of course
you will have 3-4 objects for each color, so in a second scanning
you will have to draw only these 3-4 objects, with the different
colors, and select which is the right one.

Carlos Pereira
Technical University of Lisboa
Portugal



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]