drawimage from a string variable

Blitz3D Forums/Blitz3D Programming/drawimage from a string variable

Dicon(Posted 2011) [#1]
I want to use a variable ( int) to access the array of a dim string array.
i.e.
For t=1 To number_of_icons
DrawImage icon_name$(2),20,20*t
Next

It seems that this isn't going to work. Is there a simple work around?
Thanks
Dicon


Yasha(Posted 2011) [#2]
...eh?

The only thing "t" affects in that code is the Y position of the drawing. Did you mean
For t=1 To number_of_icons
DrawImage icon_name$(t),20,20*t
Next

...?

All arrays are indexed by integer in B3D.


K(Posted 2011) [#3]
It seems you are attempting to use the value of a string variable as a handle for the image.This will not work. Instead you could put the INT handle in an array as the INT return of the INT array index. Another option is to create a custom type "icon" with fields "image,index". Or easier still is to put them all on an animimage and make the index the frame reference.


_PJ_(Posted 2011) [#4]
Aside from the syntax issues described by Yasha,

If you really need the string, try:

DrawImage(LoadImage(icon_name$(t)),20,20

But this is bad, since you don't retrieve the handle for the loaded image, causing a memory leak.

What K suggests is the best advice:

; Initialise Screen
Graphics 1024,768,32,6
SetBuffer BackBuffer()

; Initialise some values

Const ICON_LOCAL_DIRECTORY$="Icons/"
Const ICON_EXTENSION$=".bmp"
Const ICON_PREFIX$="icon"
Const NUMBER_OF_ICONS%=20; ? Or however many you have
Dim icon_handle_array%(NUMBER_OF_ICONS)

;-----------------------------------


BuildIconArray

;Test Loop
While Not (KeyDown(1))
	DrawAllIcons
	Flip
Wend



;-----------------------------------
; Builds the array of Filehandles
Function BuildIconArray()
Local IconFile$
Local FilePath$
Local IconDirectory$=CurrentDir()+ICON_LOCAL_DIRECTORY

Local DirectoryHandle=ReadDir(IconDirectory)
IconFile=NextFile(DirectoryHandle)

Local IconCount%=0
While ((IconFile<>"") And (IconCount<(NUMBER_OF_ICONS)))
	FilePath=IconDirectory+IconFile
	If (Len(IconFile)>8)
		If (Right(Lower(FilePath),4)=ICON_EXTENSION)
			If (Left(Lower(IconFile),4)=ICON_PREFIX)
				icon_handle_array(IconCount)=LoadImage(FilePath)
				IconCount=IconCount+1
			End If
		End If
	End If
	IconFile=NextFile(DirectoryHandle)
Wend
End Function

; Displays the icons
Function DrawAllIcons()
	Local IterIcons%
	
	For IterIcons=0 To NUMBER_OF_ICONS-1
		DrawImage icon_handle_array(IterIcons),20,20*IterIcons
	Next
End Function



Axel Wheeler(Posted 2011) [#5]
I think dicon needs to describe exactly what he is trying to do with the string array. I think he is just creating an array of images, in which case I would think this would work:

image(1)=LoadImage("kitty.png")
image(2)=LoadImage("doggy.png")
etc.


or

dim image(100)
for i=0 to 99
    image(i)=LoadImage("image"+i+".png")
next


then DrawImage(image(1)) should do the trick (haven't tried it myself). The key is to only load each image one time, as a separate step from displaying it.