Something like a mouse selector on a tile may seem mundane, but it’s actually pretty complex(or in computer science terms “fun”).
When the game starts, it loads a black&white “tile_mask.png” into memory and converts it into a line of 4,096 bits. Black is 0, white is 1.
When a tile is drawn, it checks the given bitfield at index [ (mouseX-drawX)+(mouseY-drawY)*TILE_WIDTH ] to see if the mouse is on a “white” part of the tile_mask.png, which is, in a sense, placed relative to the tile being drawn. If the bit that’s checked is a 1, the mouse is hovering over that tile, if it’s a 0 or out of bounds, then it’s not.
After the check is performed, a pointer to the selected tile is given to the WORLD object, and can then be used to tell other entities in the game world which MAPTILE object is currently under the mouse cursor.
EDIT: All that stuff you just read up there? It’s STUPID and USELESS.
NEW SOLUTION! I figured out the formula to translate the mouse coordinates into the map’s coords, thus reducing the check to O(1).
I did this by using simple algebra on the code used to draw the tiles, so that I could sort of… “reverse” it to get the coordinate component. It’s rough, but it works.
double selectX,selectY, mX, mY;
mX = MOUSE.x+camX;
mY = MOUSE.y+camY-TILE_H*1.5;
selectY = mX/(TILE_W/2) – (mX/(TILE_W/2) + mY/(TILE_H/2))/2;
selectX = mX/(TILE_W/2) – selectY;
selected_tile = Grid_Tiles.get((int)selectX,(int)selectY);