data

BlitzPlus Forums/BlitzPlus Programming/data

Jerome Squalor(Posted 2007) [#1]
hi,
i keep seeing people use the data command to create a tile-like look on their game. Like on platform games where they have different numbers translate to a different image.

example:

data 1,1,1,1,1
data 1,0,0,0,1
data 1,0,0,0,1
data 1,0,0,0,1
data 1,1,1,1,1

this would create like an outline sort of thing.

anyway if you understand a thing i tried to explain please help me out and show me how to do it.

thanks in advance.


b32(Posted 2007) [#2]
I suppose this would be the most basic way to do that:

(the drawing is really small)


Andy_A(Posted 2007) [#3]
Like b32 said only bigger so you don't have to squint :)

sw = 800
sh = 600

Graphics sw,sh,32,2
SetBuffer BackBuffer()

;use 'box' data
Restore box

;change these for different size boxes
boxX = 48
boxY = 48

;pixel spacing between boxes
;change to 0 for no spacing
spacing = 2

For y = 0 To 4
	For x = 0 To 4
		;read a color (i.e. a number 1-4)
		Read colorData
		;Define your color scheme here
		Select colorData
			Case 0: Color 255,255,255
			Case 1: Color 255,  0,  0
			Case 2: Color   0,255,  0
			Case 3: Color   0,  0,255
			Case 4: Color 255,255,  0
			Default: Color 255,255,255
		End Select

		;Rect x*(boxX + spacing ), y*(boxY + spacing ), boxX, boxY, True
		
		;just add an offset to move the tiles  to where ever you want on screen (centered here)
		offSetX = (sw - (5 * (boxX + spacing))) /2
		offSetY = (sh - (5 * (boxY + spacing))) /2
		Rect x*(boxX + spacing ) + offSetX, y*(boxY + spacing ) + offSetY, boxX, boxY, True
		
	Next
Next

Text 5,560,"Left click to exit"

Flip

WaitMouse() ;wait for a mouse click to exit
End

.box
Data 4,1,1,1,1
Data 4,0,0,0,2
Data 4,0,0,0,2
Data 4,0,0,0,2
Data 3,3,3,3,2



Petron(Posted 2007) [#4]
This may interest you: http://www.blitzbasic.com/codearcs/codearcs.php?code=1987 you need to add the setcursorpos command in the user32 decls.


CS_TBL(Posted 2007) [#5]
I'd avoid data statements and use one of the following:
- a binary file created by a map editor that goes into a 2d array.
- a set of functions that *create* high-level things like platforms, boxes etc. Then a map is a matter of performing some of those functions. Again going into a 2d array.

Data can be sufficient for 4 values orso, but when your map has like 256 different tiles, you aren't going to love data statements.

In addition: your signature is truly over-sized!


Andy_A(Posted 2007) [#6]
CS_TBL is absolutely correct, in that large, complex maps are best stored in binary files on disk.

Data statements are good for learning how to implement those binary files. Using data statements to change a small detail just change one of the values; instant gratification. Using binary files you'll need to use a small routine or better yet a map editor to update the map file.

Binary files are best used when you already know what you're doing, data statements can be useful while learning but you'll soon outgrow them.


CS_TBL(Posted 2007) [#7]
Here's a small example of my 2nd point however, using functions to create a map. Note that I've been using a bank, but it's easy to see how to deal with it in the Showmap and Maprect functions.

Graphics 640,480

map=CreateMap(32,24)

Maprect map,0,0,32,24,255,100

Maprect map,1,1,30,22,32,100

For t=1 To 9
	Maprect map,1,1,30,22,32+(t*12),40
Next

Maprect map,0,4,8,2,255,100

Maprect map,8,12,8,4,255,100

Maprect map,24,16,6,4,255,100


Showmap Map

Flip

Repeat
Until KeyDown(1) ; Esc
End

Function Showmap(map)
	mapw=PeekShort(map,0)
	maph=PeekShort(map,2)
	For y=0 To maph-1
		For x=0 To mapw-1
			v=PeekByte(map,4+x+mapw*y) ; 'v' here contains the number of the tile (0..255 in this case)
			Color v,v,v
			Rect x*8,y*8,8,8,True
		Next
	Next
End Function

Function CreateMap(w,h)
	bank=CreateBank(w*h+4)
	PokeShort bank,0,w
	PokeShort bank,2,h
	Return bank
End Function

Function Maprect(map,px,py,w,h,v,chance)
	mapw=PeekShort(map,0)
	maph=PeekShort(map,2)
	If w<1 w=1
	If h<1 h=1	
	For y=0 To h-1
		For x=0 To w-1
			If InBox(x+px,y+py,0,0,mapw,maph)
				If Rnd(100)<=chance
					PokeByte map,4+(px+x+mapw*(py+y)),v
				EndIf
			EndIf
		Next
	Next
End Function

Function InBox(px,py,x,y,w,h)
	If px>=x And px<(x+w) And py>=y And py<(y+h) Return 1 Else Return 0
End Function



Rob Farley(Posted 2007) [#8]
Your InBox function... You might want to look at rectsoverlap.


CS_TBL(Posted 2007) [#9]
Yeah, well.. :P

In a number of cases, making a small function takes less time than finding something appropriate in the help. ^_^ Btw, this one works for 1 pixel, not for a rect.


Jerome Squalor(Posted 2007) [#10]
I ran into a problem.
When i put cls in the loop the tiles aren't drawn.


CS_TBL(Posted 2007) [#11]
Here's a loop in which to draw things:

-------------------
CLS

DrawAllYourStuff

FLIP
-------------------


Jerome Squalor(Posted 2007) [#12]
i tried that but it says it ran out of data because i put the whole for...next loop in the main loop. I know im doing something wrong but i don't know how to fix it.


CS_TBL(Posted 2007) [#13]
The standard forum answer: show us the code!

Oh wait, don't tell me, I know.. :P

You have your 'read' function inside the mainloop!

Wanna bet? :P


Jerome Squalor(Posted 2007) [#14]



here is my code.
if i don't put the Read command in the main loop where do i put it


Jerome Squalor(Posted 2007) [#15]
never mind i got it!!

finally!!

thanks for all the help!


Jerome Squalor(Posted 2007) [#16]
hold on i thought i had it but it's not working again.


b32(Posted 2007) [#17]
You could also put 'Restore' right before the first For..Next loop that reads the data.


Petron(Posted 2007) [#18]
Could someone post an example of how to use binary files with the code that I posted, or give an example that shows its use instead of data.


b32(Posted 2007) [#19]
Sure .. at some point, you read the map from the Data.
Right after that, you can put this map in a file.
I made a 'short version' that shows this:



Jerome Squalor(Posted 2007) [#20]
the images aren't drawing can you guys show me how to do it


CS_TBL(Posted 2007) [#21]
Type bullet
	Field x#,y#
	Field xv#,yv#
	Field frame
End Type

Global bullet.bullet


Here's an advice: stick to the common rule to call your types T<name> and the variable you're going to use bullet, so in this case: TBullet and bullet. (or Bullet, whatever you want) This makes the difference between a type definition and an instance more clear.

	Select filled  
			Case 1
				DrawImage cl1,x*32,y*32
			Case 2
				DrawImage cl2,x*32,y*32
			Case 3
				DrawImage pl1,x*32,y*32
			Case 4
				DrawImage pl7,x*32,y*32
			Case 5
				DrawImage pl6,x*32,y*32
			Case 6
				DrawImage wl2,x*32,y*32
			Case 6
				DrawImage vn1,x*32,y*32
; was this second case 6 supposed to be right??
		End Select	


You may want to consider arrays. Like Dim picture(10) can hold 10 images. Then in your draw routine you don't need to Select-Case on the value, you just draw the right picture from the array according to the mapvalue.
So if you mapvalue is 4, then:
DrawImage picture(mapvalue),x*32,y*32

draws image 4. This will especially be practical when you have 100+ tiles.. :P

In any case, when you're done experimenting with 'data', really consider working with binaries. Did you try/test my map-function example?


b32(Posted 2007) [#22]
As for your code, the images are drawing, so I think they are not loaded. Try putting them in the same folder as your code first, as a test.



Jerome Squalor(Posted 2007) [#23]
everything is loaded fine. my problem is that i can't figure out what part of the code to in the main loop so they draw.

i know they're loaded because when i remove the Cls command the show but when i put it back in they don't because the drawimage commands aren't in the main loop.


b32(Posted 2007) [#24]
Are we still talking about the code you posted above ? I've included the Cls command, and placed the character in the middle of the screen:

I replaced LoadImage/LoadAnimImage with a custom function, for testing. If you run it, you can see the blue thingie walk around the red thingies world.
If you comment the functions out, it should load the original image files again.


Jerome Squalor(Posted 2007) [#25]
i finally got it every one. My mistake was not putting the restore command in the loop.

thanks for all the help!