Archived

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

NashMan

GDI/MFC Hexagon Troubles

Recommended Posts

I''m a pretty experienced C++/MFC programmer, and have decided to try to write an adaptation of a board game a friend of mine came up with (think of something like minesweeper). It''s played on a hexagon shaped board filled with smaller hexagons. I''ve been working on the basic engine to run it without much luck. I''m using MFC to do it, cause that''s what I''m most familiar with, and DirectX seemed like overkill. I''d like to have some effects using mouseovers on the hexes and that kind of thing, and it would definately involve clicking on specific hexes. To build my map of hexes I have tried two difference approaches: Approach 1: Seemed to me that deriving an object from CWnd for each hex would let me inherit alot of functionality, like clicking, mouseovers and the like. I did this and then created a Hexagon shaped region, and make my CWnd-derived object use that region for it''s window. Worked pretty well, I could get the mouseovers and clicks working, and had no problems with hit testing, but it kind of screwed up my hexagon, making it uneven on one side. Apparently a region isn''t inclusive of all the points you use to define it, and I couldn''t get it to look right by adjusting the size a little. Approach 2: Just using GDI and the device context to draw hexagons. That''s pretty straight forward, but hit-testing on individual hexes is getting kind of nasty. And the map isn''t a square, so that complicates it... Anyone have any suggestions? I''ve looked around but never been able to find a nice GDI hex engine example, anyone know of any? Thanks in advance, Nashman

Share this post


Link to post
Share on other sites
There is an example of this in a directX7 book I have so it might not be applicable to you. Anyway they create a separate layer with hexes in different colors. When the click occurs you check the pixel color against this colormap and determine if it is within the hex.

I am personally writing a boardgame myself. It too has hexagons but I am using directX and to speed up development I only check rectangles that covers 4 hexsides. This works fine for now and the map editor user has not complai1ned at all.



I am actually a Swede, living in paradise.

Share this post


Link to post
Share on other sites
There is a mathematical method for hit testing that is outlined in one of the articles on this site. (click on the articles and resources link above)

It took me a bit to work through it when I first encountered it. I tried to write out an explanation (along with some other hex board related stuff), and posted it here. You may find it useful (or even more confusing).

HTH,
JSwing

Share this post


Link to post
Share on other sites
Here, look for mouse matters near the end of the article, this is the way I do it (but with isometric tiles) in the article you''ll find information on Iso and Hex tiles.

Its TANSTAAFL article, you could buy his book too, Isometric game programing with DirectX 7.0, its pretty good, and the algorithms apply to any api, not just directx (though examples do use DX)

Share this post


Link to post
Share on other sites
The following will fix your problem, doesn''t involve linear algebra, and will still allow you to use the CWnd derivative with a CRgn:

Create a two color bitmap that contains the hex. draw and fill in the hex with one color, and leave the other color as the background color. i suggest a black and white monochrome bitmap, but the actual colors and bit depth do not matter, as long as you have two distinct colors, one that means "inside" and one that means "outside", i.e. a mask.

create a rectangular CRgn that is the same size as the bitmap.

now, scan pixel by pixel for the "outside" of the hex from the image. for each "outside" pixel, create a 1x1 CRgn, and combine it with the rectangular region using RGN_DIFF. you will now have a CRgn that is the exact shape as the hex image, and you can go ahead and use it with the CWnd derivative as you are already doing.

this technique is also good for playing pieces, and just about anything else where you need a region that conforms to a precise pixel pattern.

Share this post


Link to post
Share on other sites