• Create Account

Banner advertising on our site currently available from just \$5!

# Bikonja

Member Since 02 Sep 2007
Offline Last Active Mar 02 2013 06:36 AM

### 2D isometric picking problem

03 December 2012 - 12:57 AM

Hello all, I have a problem with my 2D isometric picking.
I've asked on gamedev.stackexchange too, but so far I have no answer. You can read the stackexchange question here. It contains details. I will write a short version here.

Here's the code I have for drawing the tiles
```// Draw code
int col = 0;
int row = 0;
for (int i = 0; i < nrOfTiles; ++i)
{
// XOffset and YOffset are currently hardcoded values, but will represent camera offset combined with HUD offset
Point tile = IsoToScreen(col, row, TileWidth / 2, TileHeight / 2, XOffset, YOffset);
int x = tile.X;
int y = tile.Y;
spriteBatch.Draw(_tiles[i], new Rectangle(tile.X, tile.Y, TileWidth, TileHeight), Color.White);
col++;
if (col >= Columns) // Columns is the number of tiles in a single row
{
col = 0;
row++;
}
}
// Get selection overlay location (removed check if selection exists for simplicity sake)
Point tile = IsoToScreen(_selectedTile.X, _selectedTile.Y, TileWidth / 2, TileHeight / 2, XOffset, YOffset);
spriteBatch.Draw(_selectionTexture, new Rectangle(tile.X, tile.Y, TileWidth, TileHeight), Color.White);
// End of draw code
public Point IsoToScreen(int isoX, int isoY, int widthHalf, int heightHalf, int xOffset, int yOffset)
{
Point newPoint = new Point();
newPoint.X = widthHalf * (isoX + isoY) + xOffset;
newPoint.Y = heightHalf * (-isoX + isoY) + yOffset;
return newPoint;
}
```

And here is the code for getting which tile is the mouse on
```public Point? ScreenToIso(int screenX, int screenY, int tileHeight, int offsetX, int offsetY)
{
Point? newPoint = null;
int nX = -1;
int nY = -1;
int tX = screenX - offsetX;
int tY = screenY - offsetY;
nX = -(tY - tX / 2) / tileHeight;
nY = (tY + tX / 2) / tileHeight;
newPoint = new Point(nX, nY);
return newPoint;
}
```

This code is close, but not even close enough for a public beta or something like that (even though this is just a proof of concept, which I will later transform into my game). I have no idea why this code is so close. I drew the tiles by thinking up the formula, and by googling I believe I've seen that it is correct (however it's still possible it's not). The code for picking - what ever I came up with, didn't work. What ever I googled, didn't work. This code I currently have is a derivation of something I googled - it was not really close, but closer than others, so I modified it and it's even closer now - I did it by trial and error as I have no idea behaved and behaves the way it did/does.

Can anyone point out to me my error? Whenever I think about it, it doesn't make sense to me that it behaves that way. I tried reversing the formula for drawing and it didn't pan out which doesn't make sense either. I must be doing something wrong then so please, if someone sees my error, could you point it out?

### Get pixel color with GetBitmapBits

02 September 2007 - 08:44 PM

Hi! I am a begginer in C++ builder, but I have started working on a plugin and I did manage to make some cool functions, but I have GetPixel and SetPixel too, which would be awesome if it weren't so slow... Btw, I am using only WinAPI functions to make the plugin much smaller (from aprox. 50 kb to aprox. 10 kb) so I would like it to stay that way... I was stumped with how to fix this so I asked how to fix this, what to do and I got this answer:
```   HDC hDC,                  // Handle to the display device context
hDCMem;               // Handle to the memory device context
HBITMAP hBitmap;          // Handle to the new bitmap
int iWidth, iHeight;      // Width and height of the bitmap
int iDoneBits;	    // Changed bits
int bitColorARRAY_SIZE;   // Size of the array
int *bitColorARRAY_PTR;   // Pointer to the array with pixels
// Retrieve the handle to a display device context for the client
// area of the window.
hDC = GetDC (hwnd);

// Create a memory device context compatible with the device.
hDCMem = CreateCompatibleDC (hDC);

// Retrieve the width and height of window display elements.
iWidth = GetSystemMetrics (SM_CXSCREEN) / 10;
iHeight = GetSystemMetrics (SM_CYSCREEN) / 10;

// Create a bitmap compatible with the device associated with the
// device context.
hBitmap = CreateCompatibleBitmap (hDC, iWidth, iHeight);

// HERE USE
iDoneBits = SetBitmapBits(hBitmap, bitColorARRAY_SIZE, bitColorARRAY_PTR)

// HERE USE
// Insert code to use the bitmap.

// Delete the bitmap object and free all resources associated with it.
DeleteObject (hBitmap);

// Delete the memory device context and the display device context.
DeleteDC (hDCMem);
DeleteDC (hDC);```
I couldn't figure out this code completly... I have tried to implement it, but never succesfully... I understand some parts of the code, while I don't understand others... Some parts I understand, but I don't know how the code should look exactly... So could someone please explain some things to me? Why is iWidth and iHeight /10 and not just the function return value? Where do I use the MemoryDC? What I need to do with it? I know something about the lines need to be aligned and stuff, but I just can't seem to get it from everything I read on the net... So if someone would be please be so kind to try and explain this stuff a bit to me... How would I get a string in format "R,G,B" from an exact pixel... So let's say I want to get the color of the pixel on the screen with coordinates 20,53 and I need to get this in a string like "255,238,13"... How would I set a pixel like that? I am sorry I can't understand this code and I bug you, but for some reason I just can't seem to figure this code out... Thanx!

PARTNERS