02-17-2023, 03:31 AM
(02-17-2023, 01:53 AM)PhilOfPerth Wrote:(02-17-2023, 01:17 AM)TerryRitchie Wrote:(02-17-2023, 12:34 AM)PhilOfPerth Wrote: I have a folder containing several images (.jpg) that I want to place in an array, then pick any (or all) from that array to display. I don't see any appropriate commands that allow this; are there any? (simplicity is important to me!)
I do this all the time in my games. I use an array of LONG INTEGERS to preload images then call upon each array index when I need the images.
DIM Image(10) AS LONG
Image(0) = _LOADIMAGE("image001.jpg", 32)
Image(1) = _LOADIMAGE("image002.jpg", 32)
Image(2) = _LOADIMAGE("image003.jpg", 32)
Image(3) = _LOADIMAGE("image004.jpg", 32)
Image(4) = _LOADIMAGE("image005.jpg", 32)
etc..
etc..
You can assign descriptive variable names if you like as well:
WaterFall& = Image(0)
Forest& = Image(1)
SunSet& = Image(2)
etc..
_PUTIMAGE (0, 0), SunSet&
or
_PUTIMAGE (0, 0), Image(2)
Don't forget to free the images before your program terminates:
FOR i = 0 TO 10
_FREEIMAGE Image(i)
NEXT i
Look at the code for any of my games and you'll see I use this method quite extensively.
Thanks Terry.
This looks exactly like what I'm trying to do.
But when I used this to build a simple model to work on, by typing this:
Dim Image(10) As Long
Image(0) = _LoadImage("RecPics/cat.jpg", 32)
Image(1) = _LoadImage("RecPics/cat.jpg", 32)
Image(2) = _LoadImage("RecPics/cat.jpg", 32)
Image(3) = _LoadImage("RecPics/cat.jpg", 32)
Image(4) = _LoadImage("RecPics/cat.jpg", 32)
_PutImage (0, 0), Image(2)
Sleep
_FreeImage (Image(2))
I get an "Illegal function call" error on line 7 (the _PutImage line).
(I have the cat.jpg image in a folder called RecPicS)
I'll have a look at some of your progs to see if i can spot what I'm doing wrong.
Ok, good, you have the correct idea. Here is a simplified version of the sort of image management system I use in games. The UDT IMAGE can have many more fields added to it as needed, I included x and y to show their use.
These routines allow for a master image array to be manipulated very easily. You can even create image arrays within the master image array.
Note: I wrote this over the last 30 minutes and didn't test it for 100% accuracy. It should work though, let me know if you have any questions.
Code: (Select All)
'
' Simple image management system
'
TYPE IMAGE ' IMAGE PROPERTIES
InUse AS INTEGER ' is this array index in use?
Pic AS LONG ' the image
x AS INTEGER ' x coordinate of image
y AS INTEGER ' y coordinate of image
END TYPE
REDIM Image(0) AS IMAGE ' master image array
' There are a few different ways to use this:
'------------------------------
' 1. Create an individual image
'------------------------------
DIM MyImage AS INTEGER ' individual image handle pointer
MyImage = GetImage("MyImage.jpg") ' load a single image into the array
'-----------------------------------------------------------
' 2. Create an array of images within the master image array
'-----------------------------------------------------------
DIM Trees(10) AS INTEGER ' an array of handle pointers
DIM SunSets(10) AS INTEGER ' an array of handle pointers
FOR i = 1 TO 10 ' this will load 20 images into the array
TreeFileName$ = "tree" + _TRIM$(STR$(i)) + ".jpg"
SunSetFileName$ = "sunset" + _TRIM$(STR$(i)) + ".jpg"
Trees(i) = GetImage(TreeFileName$)
SunSets(i) = GetImage(SunSetFileName$)
NEXT i
'----------------------------------
' 3. Make a change within the array
'----------------------------------
FreeImage SunSets(3) ' remove image from array
SunSets(3) = GetImage("NewSunSet.jpg") ' replace with new image
'--------------------------------
' 4. Set the location of an image
'--------------------------------
Set_Image_X MyImage, 100 ' location of image 100,150
Set_Image_Y MyImage, 150
'--------------------------------
' 5. Get the location of an image
'--------------------------------
MyImageX = Get_Image_X(MyImage) ' get the x,y location of MyImage
MyImageY = Get_Image_Y(MyImage)
'
' 6. Clear all images before leaving
'
FreeAllImages ' clear all images from RAM
SYSTEM ' return to operating system
'----------------------------------------------------------------------------------------------------------------
FUNCTION GetImage (FileName AS STRING)
'
'Get an image, add it to the image array, return the pointer handle
'
SHARED Image() AS IMAGE ' need access to image array
DIM Handle AS INTEGER ' handle pointer assigned to new image
IF NOT _FILEEXISTS(FileName) THEN ' does the image file exist?
SCREEN 0, 0, 0, 0 ' no, switch to a pure screen 0 (text)
PRINT "ERROR: The image file does not exist" ' report error
END ' leave program
END IF
Handle = -1 ' set pointer
DO ' begin image array search
Handle = Handle + 1 ' increment hande pointer
IF Image(Handle).InUse = 0 THEN EXIT DO ' if this aryay index is free then leave the loop
LOOP UNTIL Handle > UBOUND(Image) ' leave the loop when all array indexes have been searched
IF Image(Handle).InUse THEN ' is this array index in use?
Handle = Handle + 1 ' yes, then we need to add another index to the array
REDIM _PRESERVE Image(Handle) AS IMAGE ' increase the array size while preserving existing contents
END IF
Image(Handle).InUse = -1 ' mark this array index as having an image loaded
Image(Handle).Pic = _LOADIMAGE(FileName, 32) ' load the image into the array
GetImage = Handle ' return the handle pointer
END FUNCTION
'----------------------------------------------------------------------------------------------------------------
SUB FreeImage (Handle AS INTEGER)
'
'Remove an image from the image array
'
SHARED Image() AS IMAGE ' need access to image array
IF NOT ValidImageHandle(Handle) THEN ' is this a valid image handle?
SCREEN 0, 0, 0, 0 ' no, switch to a pure screen 0 (text)
PRINT "ERROR: the handle supplied is invalid" ' report error
END ' leave program
END IF
Image(Handle).InUse = 0 ' free this index for a future image
_FREEIMAGE Image(Handle).Pic ' remove the current image from RAM
END SUB
'----------------------------------------------------------------------------------------------------------------
SUB FreeAllImages ()
'
'Remove all images from RAM (useful before the program exits)
'
SHARED Image() AS IMAGE ' need access to image array
DIM Index AS INTEGER ' array index counter
Index = -1 ' set index counter
DO ' begin array search
Index = Index + 1 ' increment index counter
IF Image(Index).InUse THEN ' does this index contain an image?
_FREEIMAGE Image(Index).Pic ' yes, free image from RAM if one exists
Image(Index).InUse = 0 ' free this index for a future image
END IF
LOOP UNTIL Index = UBOUND(Image) ' leave when all indexes searched
END SUB
'----------------------------------------------------------------------------------------------------------------
SUB Set_Image_X (Handle AS INTEGER, x AS INTEGER)
'
' Set the x coordinate of an image
'
SHARED Image() AS IMAGE ' need access to image array
IF NOT ValidImageHandle(Handle) THEN ' is this a valid image handle?
SCREEN 0, 0, 0, 0 ' no, switch to a pure screen 0 (text)
PRINT "ERROR: the handle supplied is invalid" ' report error
END ' leave program
END IF
Image(Handle).x = x ' set image's x coordinate
END SUB
'----------------------------------------------------------------------------------------------------------------
SUB Set_Image_Y (Handle AS INTEGER, y AS INTEGER)
'
' Set the y coordicnate of an image
'
SHARED Image() AS IMAGE ' need access to image array
IF NOT ValidImageHandle(Handle) THEN ' is this a valid image handle?
SCREEN 0, 0, 0, 0 ' no, switch to a pure screen 0 (text)
PRINT "ERROR: the handle supplied is invalid" ' report error
END ' leave program
END IF
Image(Handle).y = y ' set image's y coordinate
END SUB
'----------------------------------------------------------------------------------------------------------------
FUNCTION Get_Image_X (Handle AS INTEGER)
'
' Get the x coordinate of an image
'
SHARED Image() AS IMAGE ' need access to image array
IF NOT ValidImageHandle(Handle) THEN ' is this a valid image handle?
SCREEN 0, 0, 0, 0 ' no, switch to a pure screen 0 (text)
PRINT "ERROR: the handle supplied is invalid" ' report error
END ' leave program
END IF
Get_Image_X = Image(Handle).x ' report image's x location
END FUNCTION
'----------------------------------------------------------------------------------------------------------------
FUNCTION Get_Image_Y (Handle AS INTEGER)
'
' Get the y coordinate of an image
'
SHARED Image() AS IMAGE ' need access to image array
IF NOT ValidImageHandle(Handle) THEN ' is this a valid image handle?
SCREEN 0, 0, 0, 0 ' no, switch to a pure screen 0 (text)
PRINT "ERROR: the handle supplied is invalid" ' report error
END ' leave program
END IF
Get_Image_Y = Image(Handle).y ' report image's y location
END FUNCTION
'----------------------------------------------------------------------------------------------------------------
FUNCTION ValidImageHandle (Handle AS INTEGER)
'
' Check for a valid image handle
' Returns 0 (false) if invalid or -1 (true) if valid
'
SHARED Image() AS IMAGE ' need access to image array
IF Handle < 0 OR Handle > UBOUND(Image) THEN ' is handle within array boundary?
ValidImageHandle = 0 ' no, return invalid
EXIT FUNCTION ' leave function
END IF
IF Image(Handle).InUse = 0 THEN ' is there an image at this index?
ValidImageHandle = 0 ' no, return invalid
EXIT FUNCTION ' leave function
END IF
ValidImageHandle = -1 ' return valid
END FUNCTION
'----------------------------------------------------------------------------------------------------------------