Ok, so im making an leveleditor and i want my cursor to be within a grid that i place my tiles in right?
right now it works until i scroll some where in the level editor then its position get a bit pixels wrong im not sure how to fix this.
heres my code that places the cursor within the grid:
void show_cursor(Tile *tiles[])
{
int x = 0, y = 0;
SDL_GetMouseState(&x,&y);
for( int t = 0; t < TOTAL_TILES; t++ )
{
SDL_Rect box = tiles[ t ]->get_box();
//If the mouse is inside the tile
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
//set the cursor to that position
x=box.x;
y=box.y;
break;
}
}
apply_surface( x, y, cursor, screen, &cursorClip [ frame ] );
}
im not sure if thats how u do it but that was the only was i figured so if u know a better way please tell me
rather than checking against each tile, make your x,y cursor position be translated into a tile position.
if your tiles are 10 by 10 you would do something like this.
getTileXY(int cX, int cY, pass by reference your tx and ty)
{
tx = cX / 10; // i would floor these numbers, so it would give you the tile number
ty = cY / 10;
}
you can then get to that tile
tiles[ty*10+tx] hope this helps.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
please explain a bit more what thoose variables are
is cX and cY my cursor position right?
and tX and tY being my tile positon?
please explain how that works im a bit confused
okey guys im stuck again ive tryed front and back but nothing works! maybe im just tired ( late here in sweden ) maybe its something really simple ive missed but i dont see it!
please help right now the cursor jumps with like 10+ tiles of spaces ..
i think it goes linear or something now cause at the very first row it works ( until i scroll then it jumps a few pixel wrong like it did in the beginning ) but on all the other rows its messed up
void put_tile( Tile *tiles[], int tileType ) {
//Mouse offsets
int x = 0, y = 0;
//Get mouse offsets
SDL_GetMouseState( &x, &y );
//Adjust to camera
x += camera.x;
y += camera.y;
.
.
.
}
So we add the camera offsets to the mouse offsets because the postion in the level is the mouse offsets plus how much the camera has scrolled.
yea im following lazyfoos tutorials, thanks for the advice but that dont work also mostly need help with fixing the grid right now it only works on the first row, please guys help me im getting fucking frustrated
Hmm that dosent work marauder still the same thing that happens.
on the first row the grid works fine but as soon as i jump down a row it gets strange
i guess it has something to do with
SDL_Rect box = tiles[tY*16+tX]->get_box(); this part of my code, i guess its maybe some math im doing wrong or something,
should i film it so u guys see it?
my code looks the same as the one i posted before exept marauders thing