Page 1 of 1

Problem with cursor and grid ( kinda. )

Posted: Wed Feb 25, 2009 3:27 pm
by Vortex
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:

Code: Select all

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 :)

Re: Problem with cursor and grid ( kinda. )

Posted: Wed Feb 25, 2009 3:44 pm
by avansc
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.

Re: Problem with cursor and grid ( kinda. )

Posted: Wed Feb 25, 2009 3:58 pm
by Vortex
please explain a bit more what thoose variables are :oops:
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 :shock:

Re: Problem with cursor and grid ( kinda. )

Posted: Wed Feb 25, 2009 4:14 pm
by avansc
okay, well you cursor position can be traslated into a tile position.

like lets say you have a screen thats 100x100 and that eash tile is 10x10
so you have 10 x 10 tiles on you screen, or 100 tiles in total.

so if your cursor position is lets say 25, 76

you would be in tile position 2,7 and if you have all your
tiles stored linearly you would be at position 10*7+2 = 72

the only ways i can explain it clearer is with a drawing so let me know if you still dont understand.

Re: Problem with cursor and grid ( kinda. )

Posted: Wed Feb 25, 2009 6:18 pm
by Vortex
i understand how you think im just not sure how i should do it.

please explain what thoose variables are, i think that cX and cY is the cursors offset but what are thoose t´s ? the tiles ? no? :oops:

Re: Problem with cursor and grid ( kinda. )

Posted: Wed Feb 25, 2009 6:36 pm
by Kros
Vortex wrote:i understand how you think im just not sure how i should do it.

please explain what thoose variables are, i think that cX and cY is the cursors offset but what are thoose t´s ? the tiles ? no? :oops:
Right. The tX and tY is the tile's X and Y values, respectively.

Re: Problem with cursor and grid ( kinda. )

Posted: Wed Feb 25, 2009 7:20 pm
by Vortex
okey guys im stuck again :cry: 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 ..

code

Code: Select all

void show_cursor(Tile *tiles[])
{
	int cX = 0, cY = 0;
	int tX = 0, tY = 0;

	SDL_GetMouseState(&cX,&cY);


	tX = cX / 16;
	tY = cY / 16;

	SDL_Rect box = tiles[tY*16+tX]->get_box();
	cX=box.x;
	cY=box.y;
		
	apply_surface( cX, cY, cursor, screen, &cursorClip [ frame ] );
}
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

Re: Problem with cursor and grid ( kinda. )

Posted: Fri Feb 27, 2009 5:58 am
by Vortex
comon guys help me out :bow:

Re: Problem with cursor and grid ( kinda. )

Posted: Fri Feb 27, 2009 9:23 am
by KuramaYoko10
Are you following lazyfoo's tutorials? ... because you code look like it...

Well, you said your problem appears after scrolling through your map... maybe you are not adjusting the camera right...

look at this part of the tutorial:

Code: Select all

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.
Hope this solves your problem... ;)

BTW, the lazyfoo tutorial is right here:
http://lazyfoo.net/articles/article09/index.php

Re: Problem with cursor and grid ( kinda. )

Posted: Fri Feb 27, 2009 6:57 pm
by Vortex
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 :evil:

Re: Problem with cursor and grid ( kinda. )

Posted: Fri Feb 27, 2009 7:18 pm
by MarauderIIC
I'm not too sure why it's not working as-is, but try

tX = int(cX/16.0f);
tY = int(cY/16.0f);

Also perhaps, if you can, bind a key to output the tile x/y and cursor x/y to a file so you can get an idea of what's wrong.

Re: Problem with cursor and grid ( kinda. )

Posted: Sat Feb 28, 2009 2:54 pm
by Vortex
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

Re: Problem with cursor and grid ( kinda. )

Posted: Sat Feb 28, 2009 3:42 pm
by PixelP
lets see... this is how i do my mouse stuff:

void mouse_actions()
{
tile_x = mouse_x/TILE_W;
tile_y = mouse_y/TILE_H;

if(left button is pressed)
{
level[tile_y][tile_x] = GRASS_TILE;
}

apply_surface(mouse_x, mouse_y, cursor, screen, &cursorClip[frame]); // ? apply_surface(tile_x*TILE_W, tile_y*TILE_H, cursor, screen, &cursorClip[frame]);
}

Re: Problem with cursor and grid ( kinda. )

Posted: Sat Feb 28, 2009 6:19 pm
by Vortex
Thanks Pixelp got it too work now :)

Re: Problem with cursor and grid ( kinda. )

Posted: Sun Mar 01, 2009 1:38 am
by MarauderIIC
Glad you got a solution :)