Skip to main content
gamedev.net
🔒 Locked

mousemap question

Started by kslam Mar 8, 2006 at 4:01 AM 10 replies 2.3k views
Original Post
kslam
kslam
Hi all ! I have read the book "Isometric Game Programming with DirectX 7.0", but I don't understand the mousemap code in side the book. Is there anyone have read this book ? or got any other mousemap's articles ? Thanks.
freenity
freenity
This is my mouse mapping code:

TILE_WIDTH = 64
TILE_HEIGHT = 32

Mouse.x and Mouse.y are coords of mouse in screen;

first you need to divide the screen in rows and columns, as in 2D top view map. Then get the square where the cursor is. check the relative mouse position to the left top corner of this square and then get pixel color of this position of mouse map bmp. Then compare colors and that's all.



void GetMousePosition()
{

int x, y;
point offset;
SDL_Color color;

x = Mouse.x / TILE_WIDTH;
y = Mouse.y / TILE_HEIGHT;

offset.x = Mouse.x - (x * TILE_WIDTH);
offset.y = Mouse.y - (y * TILE_HEIGHT);

SDL_LockSurface(mousemap);

color = GetPixel(mousemap, offset.x, offset.y);

SDL_UnlockSurface(mousemap);

y = y * 2;

if (color.r == 255 && color.g == 255 && color.b == 255) //center
{
Cursor.x = x;
Cursor.y = y;
}
if (color.r == 255 && color.g == 0 && color.b == 0) //northwest
{
Cursor.x = x - 1;
Cursor.y = y - 1;
}
if (color.r == 255 && color.g == 255 && color.b == 0) // Northeast
{
Cursor.y = y - 1;
}
if (color.r == 0 && color.g == 255 && color.b == 255) // Southwest
{
Cursor.x = x - 1;
Cursor.y = y + 1;
}
if (color.r == 0 && color.g == 255 && color.b == 0) //Southeast
{
Cursor.y = y + 1;
}


}
TANSTAAFL
TANSTAAFL
@freenity: might I suggest to take the image of the mousemap and convert it into a 2d array of values based on the pixel values, rather than actually looking at a pixel value each time you map the mouse?

@kslam: what, specifically, are you having trouble with? I am... somewhat familiar... with that book.
Get off my lawn!
kslam
kslam
Thanks for the replies.
I don't understand these 2 function :
1)StagMap_MouseMapper(POINT ptMouse,CMouseMap* pmm)
2)StagMap_TileWalker(POINT ptStart, IsoDirection Dir)

In StagMap_MouseMapper() function,
i) Why have to plus and minus ptMouseMapFine and ptMouseMapCoarse value?

if(ptMouseMapFine.x<0)
{
ptMouseMapFine.x+=pmm->ptSize.x;
ptMouseMapCoarse.x--;
}
if(ptMouseMapFine.y<0)
{
ptMouseMapFine.y+=pmm->ptSize.y;
ptMouseMapCoarse.y--;
}

//step4:coarse tilewalk
POINT ptMap;
ptMap.x=0;
ptMap.y=0;
//move north
while(ptMouseMapCoarse.y<0)
{
ptMouseMapCoarse.y++;
ptMap=StagMap_TileWalker(ptMap,ISO_NORTH);
}
//move south
while(ptMouseMapCoarse.y>0)
{
ptMouseMapCoarse.y--;
ptMap=StagMap_TileWalker(ptMap,ISO_SOUTH);
.
.
.
.

ii)Most of the time the ptMouseMapCoarse.y>0, is it ? If I move the mouse to right (east), then the below code would be execute or not ?
//move south
while(ptMouseMapCoarse.y>0)
{
ptMouseMapCoarse.y--;
ptMap=StagMap_TileWalker(ptMap,ISO_SOUTH);

iii) Why every loop have to reset ptMap = 0 ?
then, in the StagMap_TileWalker() function, ptMap pass to this function
become ptStart which only move +1, 2 or -1, 2.
So, I can't imagined that, if I move the mouse, let say from tile (2, 3) to (24, 17),
because every time the StagMap_TileWalker() function only move +-1 or 2 steps.
So, how the ptMap value can become to (24, 17) ?

iv)Why ptMouse have to subtract pmm->ptRef ?
ptMouse.x-=pmm->ptRef.x;
ptMouse.y-=pmm->ptRef.y;

v) In bool Prog_Init() sub, why mmMouseMap.ptRef.x have to add the tsIso.GetTileList()[0].rcDstExt.left ?

mmMouseMap.ptRef.x+=tsIso.GetTileList()[0].rcDstExt.left;
mmMouseMap.ptRef.y+=tsIso.GetTileList()[0].rcDstExt.top;

Thanks
TANSTAAFL
TANSTAAFL
i) in the case of negative values, the modulo operator may return negative remainders, and mousemapping needs non-negative x and y values to index into the mousemap.

ii) most of the time, the coarse y is greater than 0, yes. But the code written was written to be generic enough to be able to handle cases where it is not.

iii) if you'll notice, the return value of the tilewalk function is assigned to be the new value of ptMap, so while each function call moves only one step, the variable does have the correct value.

iv and v) reference points and extents modify the worldspace to tilespace transformation
Get off my lawn!
kslam
kslam
Regarding the question iii),
I notice that the tilewalk function only move +-1 or 2 step,
then pass back ptMap variable to StagMap_MouseMapper function.
The ShowIsoCursor() Sub take the ptMap value and convert to screen coordinate then plot to the screen. But the StagMap_MouseMapper function reset the ptMap
value to 0 every loop, then the tilewalk function only move +-1 or 2 step.
So, if I moved from tile (2, 3) to (24, 17), how the ptMap value can become
(24, 17). If like this, the ptMap value can become (2 "+-1 or 2", 3 "+-1 or 2").
This what I don't understand, may you explain it in more detail.

Thanks.
TANSTAAFL
TANSTAAFL
ptMap=StagMap_TileWalker(ptMap,ISO_NORTH);

ptMap is passed into this function as the initial position, along with the intended direction of movement, ISO_NORTH.

the function returns a new value which is copied into ptMap, so while ptMap was initialized to (0,0) before this, it won't be by the time all of this stepping is done.

Get off my lawn!
kslam
kslam
Everytime the StagMap_TileWalker can move 1 or 2 step, then everytime the
StagMap_MouseMapper function reset ptMap to (0, 0).
1) That's mean the range of the ptMap value is between (-2, -2) to (+2, +2), is it ?
2) If No, how to get the ptMap value, if I moved the mouse pointer to tile (15, 17) ? (The mouse move ("Tile walk") more than 2 step)


TANSTAAFL
TANSTAAFL
Let me try this again:

the function(StagMap_TileWalker)

returns a new value which is copied into ptMap

, so while ptMap was initialized to (0,0) before this, it won't be by the time all of this stepping is done.

Get off my lawn!
kslam
kslam
Actually my question( which a little bit hard to explain and understand)
is that,
As I understand from the code, the tilewalk function can move 1 or 2 step,
then pass the ptStart variable to StagMap_MouseMapper function as ptMap.
After that, the ShowIsoCursor function use the ptMap
(exact tile, such as (12, 18)) to plot the cursor position.

The tilewalk function's ptMap (move 1 or 2 step only),
but the ShowIsoCursor function's ptMap value could be (12, 18).

An Example,
Let's say, my map have 30 x 30 tiles,
If I move from tile (4, 5) to (15, 21).

In the code,
First, StagMap_MouseMapper function reset ptMap = 0.
tilewalk function's ptMap (move 1 or 2 step only).
the ShowIsoCursor function got the ptMap (exact tile, such as (15, 21))

(tilewalk function pass ptMap value to StagMap_MouseMapper function,
StagMap_MouseMapper function to ShowIsoCursor function.)

After that, ptMap will reset by StagMap_MouseMapper function for next mouse movement.

What I don't understand is, why the ptMap value is <= 2 (Because move 1 or 2 step only, will reset for next mouse movement.) in tilewalk function and StagMap_MouseMapper function, but the ShowIsoCursor function's ptMap value could be > 2, such as (15, 21).

Because the ptMap value is pass from tilewalk function ...... to ShowIsoCursor function.

Why the ptMap value in tilewalk function and StagMap_MouseMapper is different from ShowIsoCursor function's ptMap value ? (this what I don't understand)

I hope you can understand my question.
Thanks.
JayeAeotiv
JayeAeotiv
TANSTAFFL I love your book, it's on the book shelf where my bible used to be.

And everyone loves Heinlein. You rock.
--before honor is humility--
TANSTAAFL
TANSTAAFL
Thanks, Jaye. Now put the Bible back.
Get off my lawn!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.