Multiple loading in Type.

BlitzMax Forums/BlitzMax Beginners Area/Multiple loading in Type.

Amon(Posted 2007) [#1]
If I do this:

global ThisImage = loadimage blah

type peewee

field image:timage
field x
field y

function create()
for local xiter = 0 to 30
local p:peewee = new peewee

p.image = ThisImage

next
blah
blah

Will the line in Bold >> p.image = ThisImage<< load the same image 31 times?

Or is the image loaded once and then copied?

I basically don't want to load the image 30 times but want to assign the loaded image to 30 objects.

How can I tell if its doing this or not?

Thanks.

Forgive me if this is obvious.


tonyg(Posted 2007) [#2]
The image is loaded once into thisimage and then the same pointer to the image is used 30 times. Each peewee will point to the same instance of the image so if you change the image it will change for all PeeWees


Amon(Posted 2007) [#3]
Thanks. :) Another question.. Looking at my code below, how can I make only 1 tile change frame when the mouse is over it?

SuperStrict

Graphics 800,600

Global ThisImage:TImage = LoadAnimImage("12.png",32,32,0,2)

Global MAPWIDTH:Int = 22
Global MAPHEIGHT:Int = 18

Type T12Image
	Field x:Int
	Field y:Int
	Field frame:Int
	Field image:TImage
	
	Global List:TList
	Global tempID:Int
	
	Function create()
		If Not T12Image.List
			t12image.List = CreateList()
		End If
		For Local xiter:Int = 0 Until MAPWIDTH
			For Local yiter:Int = 0 Until MAPHEIGHT
				If TileMap[ xiter , yiter ] = 1
					Local t:T12Image = New T12Image
					t.image = ThisImage
					t.x = xiter *32
					t.y = yiter *32
					t.frame = 0
					T12image.List.AddLast(t)
				End If
			Next
		Next
	End Function
	
		Method draw()
			For Local xiter:Int = 0 Until MAPWIDTH
				For Local yiter:Int = 0 Until MAPHEIGHT
					If TileMap[ xiter , yiter] = 1
						DrawImage Image , xiter*32 , yiter*32 , frame
					EndIf
				Next
			Next
		End Method
		
End Type

Global TileMap:Int[ MAPWIDTH , MAPHEIGHT ]
Global mapdata:Int


For Local yiter:Int = 0 Until MAPHEIGHT
	For Local xiter:Int = 0 Until MAPWIDTH
		ReadData mapdata
		TileMap[ xiter, yiter] = mapdata
	Next
Next


'For Local x:Int = 0 Until MAPWIDTH
'	For Local y:Int = 0 Until MAPHEIGHT 
'		TileMap[ x , y ] = mapdata
'	Next
'Next

t12image.create

While Not KeyHit(KEY_ESCAPE)
	Cls
	
		For Local t:T12Image = EachIn T12Image.List
			t.draw
		Next
		
		
			For Local t:T12Image = EachIn T12Image.List
				If TileMap [ (MouseX()/32) , (MouseY()/32) ] = 1
					t.frame = 1
				End If
			Next
		
		
	Flip
Wend


DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1


Basically, if my player is over a tile I want its frame to change but not change the frames of the rest of them, only the tile he's currently on.

:)


tonyg(Posted 2007) [#4]
I think you've got a design problem.
This code
			For Local t:T12Image = EachIn T12Image.List
				If TileMap [ (MouseX()/32) , (MouseY()/32) ] = 1
					t.frame = 1
				End If
			Next


loops through changing all the frames just because the mouse is in a frame '1' area. Personally, I would redesign to have my tilemap[] be an array of 't12image' .
Then, when you do the array check you can simply change the tilemap entry e.g
			For Local t:T12Image = EachIn T12Image.List
				If TileMap [ (MouseX() / 32) , (MouseY() / 32) ] = 1
						TileMap [ (MouseX()/32) , (MouseY()/32) ].frame = 1 '(or whatever)
				End If
			Next

Didn't you ask these sorts of question before?


Amon(Posted 2007) [#5]
Didn't you ask these sorts of question before?



I did yes but none of them were for help with TileMaps using Types.

I tried what you said but it doesnt compile

SuperStrict

Graphics 800,600

Global ThisImage:TImage = LoadAnimImage("12.png",32,32,0,2)



Type T12Image
	Field x:Int
	Field y:Int
	Field frame:Int
	Field ID:Int
	Field image:TImage
	
	Global List:TList
	Global tempID:Int = 0
	Global MAPWIDTH:Int = 22
	Global MAPHEIGHT:Int = 18
	Global TileMap:Int[ t12image.MAPWIDTH , t12image.MAPHEIGHT]
	
	Function create()
		If Not T12Image.List
			t12image.List = CreateList()
		End If
		For Local xiter:Int = 0 Until t12image.MAPWIDTH
			For Local yiter:Int = 0 Until t12image.MAPHEIGHT
				If t12image.TileMap[ xiter , yiter ] = 1
					Local t:T12Image = New T12Image
					t.image = ThisImage
					t.x = xiter *32
					t.y = yiter *32
					t.frame = 0
					t.id = T12Image.tempID
					T12image.List.AddLast(t)
					t12image.tempID:+1
				End If
			Next
		Next
	End Function
	
		Method draw()
			For Local xiter:Int = 0 Until t12image.MAPWIDTH
				For Local yiter:Int = 0 Until t12image.MAPHEIGHT
					If t12image.TileMap[ xiter , yiter] = 1
						DrawImage Image , x , y , frame
					EndIf
				Next
			Next
		End Method
		
		Method FrameChange()
			If t12image.Tilemap[ MouseX()/32 , MouseY()/32] = 1
				t12image.TileMap.frame = 1
			End If
		End Method
End Type

'Global TileMap:Int[ t12image.MAPWIDTH , t12image.MAPHEIGHT ]
Global mapdata:Int


For Local yiter:Int = 0 Until t12image.MAPHEIGHT
	For Local xiter:Int = 0 Until t12image.MAPWIDTH
		ReadData mapdata
		T12Image.TileMap[ xiter, yiter] = mapdata
	Next
Next


t12image.create

While Not KeyHit(KEY_ESCAPE)
	Cls
	
		For Local t:T12Image = EachIn T12Image.List
			t.draw
			t.FrameChange
		Next
				
	Flip
Wend


DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1



Amon(Posted 2007) [#6]
I'm Using ID's now. It seems to make a difference but not exactly what i want.

SuperStrict

Graphics 800,600

Global ThisImage:TImage = LoadAnimImage("12.png",32,32,0,2)



Type T12Image
	Field x:Int
	Field y:Int
	Field frame:Int
	Field ID:Int
	Field image:TImage
	
	Global List:TList
	Global tempID:Int = 0
	Global MAPWIDTH:Int = 22
	Global MAPHEIGHT:Int = 18
	Global TileMap:Int[ t12image.MAPWIDTH , t12image.MAPHEIGHT]
	
	Function create()
		If Not T12Image.List
			t12image.List = CreateList()
		End If
		For Local xiter:Int = 0 Until t12image.MAPWIDTH
			For Local yiter:Int = 0 Until t12image.MAPHEIGHT
				If t12image.TileMap[ xiter , yiter ] = 1
					Local t:T12Image = New T12Image
					t.image = ThisImage
					t.x = xiter *32
					t.y = yiter *32
					t.frame = 0
					t.id = T12Image.tempID
					T12image.List.AddLast(t)
					t12image.tempID:+1
				End If
			Next
		Next
	End Function
	
		Method draw()
			For Local xiter:Int = 0 Until t12image.MAPWIDTH
				For Local yiter:Int = 0 Until t12image.MAPHEIGHT
					If t12image.TileMap[ xiter , yiter] = 1
						DrawImage Image , x , y , frame
					EndIf
				Next
			Next
		End Method
		
		Method FrameChange()
		If ID = 2
			If t12image.Tilemap[ MouseX()/32 , MouseY()/32] = 1
				't12image.TileMap[MouseX()/32 , MouseY()/32].frame = 1
				frame = 1
			End If
		EndIf
		
		End Method
		
		Function DrawT()
			
		End Function
End Type

'Global TileMap:Int[ t12image.MAPWIDTH , t12image.MAPHEIGHT ]
Global mapdata:Int


For Local yiter:Int = 0 Until t12image.MAPHEIGHT
	For Local xiter:Int = 0 Until t12image.MAPWIDTH
		ReadData mapdata
		T12Image.TileMap[ xiter, yiter] = mapdata
	Next
Next


t12image.create

While Not KeyHit(KEY_ESCAPE)
	Cls
	
		For Local t:T12Image = EachIn T12Image.List
			t.draw
			t.FrameChange
		Next
				
	Flip
Wend


DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1


Basically I give each tile an ID. If the mouse positions is on a selected tile and its ID matches then its frame changes.

It kinda works.

Any more help would be appreciated. :)


Amon(Posted 2007) [#7]
		Method FrameChange()
			If MouseX()/32 *32 And MouseY()/32 *32 = id
				frame = 1
			End If
		End Method


This code also kinda works. I think I'm getting close.

I just need some more knowledgable minds to help out.

Please. :)


tonyg(Posted 2007) [#8]
Amon,
What is this doing?
If MouseX()/32 *32 And MouseY()/32 *32 = id

It divides by 32 then multiples by 32.
If mousex() is 200 it'll be 200/32 = 6. 6*32 = 200.
That's what you were doing wrong here .
Anyway, I would strongly recommend redesigning.
This works but needs more cleaning :

<edit> Code edited a couple of times to get rid of stupid mistakes and clean it up a bit. Could still be more encapsulated but there you go.


Amon(Posted 2007) [#9]
That works perfect, thanks. :)

Amon,
What is this doing?

If MouseX()/32 *32 And MouseY()/32 *32 = id



it was my lame ass attempt at trying to find the ID of the tile at the current mouse position.