tilemap offsets help?

BlitzMax Forums/BlitzMax Beginners Area/tilemap offsets help?

Amon_old(Posted 2005) [#1]
Ok, I'm trying to create a tilemap editor. I know how to code one but the problem I am having now is that when I try to use offsets, i.e if I want the map to start at say 100,200 I can get the map to start from there. What happens though is that the actual function to place the tile in the map doesnt appear to want to work.

The two functions below draw the map to screen and place the current tile. I get an error with the second function telling me I am out of the bouds of the array.

Function factory_drawmap()

	For Local x:Int = 0 To mapx-1
	
		For Local y:Int = 0 To mapy-1
		
			DrawImage squares,Fxoff+x*64,Fyoff+y*64,Fmap[x,y]
			
		Next
		
	Next
	
End Function

'The function below draws the tile in the map
Function factory_placetile()

	If MouseDown(KEY_MOUSELEFT) And MouseX() >= Fxoff And MouseY() >= Fyoff
	
		Fmap[MouseX()/64,MouseY()/64] = Fcurtile 'I'm trying to draw the current tile in the array posotion.
		
	EndIf
	
End Function


As you'll notice above Fxoff and Fyoff are the positions where the map starts.

Below is the 2 lines of code that draw the curser to the screen at 64*64 positions.

mx:Int = MouseX()/64*64
my:Int = MouseY()/64*64


ANy help would be great. :) If you need me to post all the code I will but I wont post the images needed.

[edit] I fixed the second function. I still get an array out of bounds error.


Sledge(Posted 2005) [#2]

mx:Int = MouseX()/64*64
my:Int = MouseY()/64*64



x/64*64=x What's that about? You're not doing anything to x.

EDIT: And on the matter of going out of bounds... are those conditions correct? Can you end a condition with "<=" in BMax?


Amon_old(Posted 2005) [#3]
ok, I've coded a little tilemap editor that uses MapStartX and MapStartY to hold where the tilemap should start on screen.

The problem I'm having is that the mouse doesnt tile up with the location of the map array and the placetile function causes an error. The full code is below with image and commented.

Strict

Graphics 1024,768,32,60

SetMaskColor 255,0,255' set the masking color

Global tiles:TImage = LoadAnimImage("pole.png",32,32,0,5)'load the tiles

Global mapstartX = 100' where the tilemap starts on the x axis
Global mapstartY = 100' where the tilemap starts on the y axis

Global map:Int[7,7]' Global array to hold map data

Global mx:Int' Tracks mousex() movement 
Global my:Int' Tracks mousey() movement

Global curtile:Int = 0'Holds the current tile number





Repeat

	Cls
	
		mx:Int = MouseX()/32*32'= set the mouse to move 32 pixels at a time
		my:Int = MouseY()/32*32'= set the mouse to move 32 pixels at a time
		
		init_map()' Call the init_map function
		draw_map()' Call the draw_map function
		place_tile()'Call the place_tile function
		
		DrawImage tiles,mx,my,curtile ' Draw the current tile at mouse&mousey locations
				
		If MouseHit(KEY_MOUSERIGHT)'If right mouse button is hit
		
			curtile:+1'Increase curtile by 1
			
			If curtile > 4 Then curtile = 0' if curtile is greater than 4 set curtile to 0
			
		EndIf
		
	
	FlushMem
	Flip
	
Until KeyHit(KEY_ESCAPE)

'the Function below sets all the map positions To 0
Function init_map()

	For Local x:Int = 0 To 6
		For Local y:Int = 0 To 6
			map[x,y] = 0
		Next
	Next
	
End Function

'the Function below updates the map and draws it to screen
Function draw_map()

	For Local x:Int = 0 To 6
		For Local y:Int = 0 To 6
			'below we draw the tiles at mapstartx and mapstarty coords of the screen
			DrawImage tiles,MapstartX+x*32,mapstartY+y*32,map[x,y]
		Next
	Next
	
End Function

'The function below places the tile on the map
Function place_tile()

	If MouseDown(KEY_MOUSELEFT)
		'Below we devide a map position by the tile size to get the correct place on the array
		map[MouseX() / 32, MouseY() /32] = curtile
		
	EndIf
	
End Function




Run this in blitzmax and you will see what I'm trying to do.


klepto2(Posted 2005) [#4]
1 fault I think is to have the Initmap() function within the loop, because you reset your map each loop and fill the array with 0.


Amon_old(Posted 2005) [#5]
oops. Fixed. Thanks. :)

It stil doesn't work though. :/


klepto2(Posted 2005) [#6]
I think I have found your problem:

simply change this:
Function place_tile()

	If MouseDown(KEY_MOUSELEFT)
		'Below we devide a map position by the tile size to get the correct place on the array
		map[MouseX() / 32, MouseY() /32] = curtile
		
	EndIf
	
End Function


to

Function place_tile()

	If MouseDown(KEY_MOUSELEFT)
		'Below we devide a map position by the tile size to get the correct place on the array
		map[(MouseX()-mapstartX) / 32, (MouseY()-mapstartY) /32] = curtile
		
	EndIf
	
End Function



Amon_old(Posted 2005) [#7]
Hey man it works. Thanks for that. I didnt figure it would have something to do with the mapstart locations.

Thanks again :)


RGR(Posted 2005) [#8]
Anon there are several things wrong?
First if you want to place your tiles in a 32 by 32 system you cannot set the Startx and Starty at 100 it should be 96, or better a number divideable (can you say that in english) by 32
Then you should concentrate on a system you want to use throughout your program. Either you want to use the Array indexes or the screencoordinates.
You start the tiledrawing map[0,0] at 100,100 and don't add that to mousex mousey before - so no wonder you will not place the tiles where your mouse is...
Try this ;-)
Strict
Graphics 1024,768,32,60
SetMaskColor 255,0,255' set the masking color
Global tiles:TImage = LoadAnimImage("pole.png",32,32,0,5)'load the tiles
Global mapstartX = 96' where the tilemap starts on the x axis
Global mapstartY = 96' where the tilemap starts on the y axis
Global map:Int[7,7]' Global array to hold map data
Global mx:Int' Tracks mousex() movement 
Global my:Int' Tracks mousey() movement
Global curtile:Int = 0'Holds the current tile number
init_map()' Call the init_map function
Repeat
	Cls
		mx:Int = MouseX()/32*32'= set the mouse to move 32 pixels at a time
		my:Int = MouseY()/32*32'= set the mouse to move 32 pixels at a time
		draw_map()' Call the draw_map function
		place_tile()'Call the place_tile function
		DrawImage tiles,mx,my,curtile ' Draw the current tile at mouse&mousey locations
		SetColor 255,255,255
		DrawLine mx-1,my-1,mx+33,my-1
		DrawLine mx-1,my-1,mx-1,my+33
		DrawLine mx+33,my-1,mx+33,my+33
		DrawLine mx-1,my+33,mx+33,my+33
		If MouseHit(KEY_MOUSERIGHT)'If right mouse button is hit
			curtile:+1'Increase curtile by 1
			If curtile > 4 Then curtile = 0' if curtile is greater than 4 set curtile to 0
		EndIf
	FlushMem
	Flip
Until KeyHit(KEY_ESCAPE)

'the Function below sets all the map positions To 0
Function init_map()
	For Local x:Int = 0 To 6
		For Local y:Int = 0 To 6
			map[x,y] = 0
		Next
	Next
End Function

'the Function below updates the map and draws it to screen
Function draw_map()
	For Local x:Int = 0 To 6
		For Local y:Int = 0 To 6
			'below we draw the tiles at mapstartx and mapstarty coords of the screen
			DrawImage tiles,MapstartX+x*32,mapstartY+y*32,map[x,y]
		Next
	Next
End Function

'The function below places the tile on the map
Function place_tile()
	If MouseDown(KEY_MOUSELEFT)
		Local mx:Int = MouseX()/32-3'= set mousex position according to the array
		Local my:Int = MouseY()/32-3'= set mousey position according to the array
		If mx>=0 And mx<=6 And my>=0 And my<=6
			map[mx, my] = curtile
		EndIf
	EndIf
End Function

Edited klepto2 was quicker :-P
Try this one as well. I added a rectangle and security question to avoid an error when you klick outside the array dimensions


Amon_old(Posted 2005) [#9]
Thanks for your help RaGr. I was having a problem with the mapstart locations and the mouse coordinates not lining up and tried minusing a few pixels from the mouse coords but it was to shabby.

Thanks for your explanation, its helped.

:)