Problem with cursor and grid ( kinda. )

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Problem with cursor and grid ( kinda. )

Post 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 :)
TorteXXX
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Problem with cursor and grid ( kinda. )

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: Problem with cursor and grid ( kinda. )

Post 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:
TorteXXX
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Problem with cursor and grid ( kinda. )

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: Problem with cursor and grid ( kinda. )

Post 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:
TorteXXX
User avatar
Kros
Chaos Rift Regular
Chaos Rift Regular
Posts: 136
Joined: Tue Feb 24, 2009 4:01 pm
Current Project: N/A
Favorite Gaming Platforms: PC, Playstation/2/3
Programming Language of Choice: C++
Location: Oregon,USA
Contact:

Re: Problem with cursor and grid ( kinda. )

Post 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.
Isaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
YouTube Channel
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: Problem with cursor and grid ( kinda. )

Post 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
TorteXXX
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: Problem with cursor and grid ( kinda. )

Post by Vortex »

comon guys help me out :bow:
TorteXXX
User avatar
KuramaYoko10
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 55
Joined: Fri Oct 31, 2008 8:02 pm

Re: Problem with cursor and grid ( kinda. )

Post 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
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: Problem with cursor and grid ( kinda. )

Post 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:
TorteXXX
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Problem with cursor and grid ( kinda. )

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: Problem with cursor and grid ( kinda. )

Post 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
TorteXXX
User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

Re: Problem with cursor and grid ( kinda. )

Post 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]);
}
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: Problem with cursor and grid ( kinda. )

Post by Vortex »

Thanks Pixelp got it too work now :)
TorteXXX
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Problem with cursor and grid ( kinda. )

Post by MarauderIIC »

Glad you got a solution :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply