Image handles in global types don't work?

Blitz3D Forums/Blitz3D Programming/Image handles in global types don't work?

pangyan(Posted 2004) [#1]
I've got the following code:
Type InterfaceGlobals
Field interfaceImage
end type

Then in another file I have:
global theInterfaceGlobals.InterfaceGlobals

I call a function:
function loadinterfaceImage(filename$)
theInterfaceGlobals = new interfaceGlobals
theInterfaceGlobals\interfaceImage = loadimage(filename$)
end function

I've done a Stop in this function and verified that a image handle is got.

However trying to access the image handle does not work from any other functions. I have to move the image handle outside into a global variable and then access it. Can't I store a image handle in a global type?


Tiger(Posted 2004) [#2]
How do you access handle to the image??
You can store image handle like you do, so you must do something wrong when you try to access it.


pangyan(Posted 2004) [#3]
I use it like so:
DrawImage(theInterfaceGlobals\interfaceImage,.....)
It doesn't work... I get a memory access violation, eventhough I placed a Stop to ensure that the handle has the same value as when I load up the image.


Mo(Posted 2004) [#4]
Maybe you set/reset the graphics-mode after loading the image?


pangyan(Posted 2004) [#5]
No I didn't change anything in the graphics mode. I only set it once.


Tiger(Posted 2004) [#6]
Can you post the source code so we can have a good look at it?? Or it is all you have?? (in the first post?)


Tiger(Posted 2004) [#7]
Test this:

Graphics 800,600

Type InterfaceGlobals 
Field interfaceImage% 
End Type 

im=CreateImage(800,600)
SetBuffer ImageBuffer(im)
For x=0 To 40
Color x*2+50,x*2+50,x*2+50
Rect Rand(0,800),Rand(0,600),Rand(10,20),Rand(10,20),1
Next
SetBuffer BackBuffer()

SaveImage(im,"Test.bmp")
FreeImage(im):im=0

;Then in another file I have: 
Global theInterfaceGlobals.InterfaceGlobals 


 loadinterfaceImage("Test.bmp")

 DrawImage(theInterfaceGlobals\interfaceImage,0,0)
 Flip
 WaitKey
 
Function loadinterfaceImage(FileName$) 
theInterfaceGlobals = New interfaceGlobals 
theInterfaceGlobals\interfaceImage = LoadImage(FileName$)  
End Function


Works for me.


Graythe(Posted 2004) [#8]
That works here too.


Ross C(Posted 2004) [#9]
i think that

function loadinterfaceImage(filename$) 
theInterfaceGlobals = new interfaceGlobals 
theInterfaceGlobals\interfaceImage = loadimage(filename$) 
end function


Would make that type variable a local one. Usually any variables created in a function will become local, and override any global ones.


Tiger(Posted 2004) [#10]
Ross C:
It don't get local because you don't make it local.
if you want it local you must type:
function loadinterfaceImage(filename$) 
Local theInterfaceGlobals = new interfaceGlobals ;<--Local
theInterfaceGlobals\interfaceImage = loadimage(filename$) 
end function



Ross C(Posted 2004) [#11]
Ah, sorry, never noticed the Global at the top :o)


RiverRatt(Posted 2004) [#12]
Type InterfaceGlobals
Field interfaceImage
end type

Should Field interfaceImage be Field interfaceImage$ ?

Fogive me I'm still new


Dreamora(Posted 2004) [#13]
nope it might be interfaceImage%
but as this is the default, the % is not needed


pangyan(Posted 2004) [#14]
That code worked strangely enough, of course I do have a LOT more code in my program. Let me see if I can reproduce the bug. (I fixed the code, by taking the image handle out of the type and putting it in a global variable).


aab(Posted 2004) [#15]
would THIS not work?



Its the pointer to the type object collections that allows globality, not the individual objects on their own (i think). This is the way i usually do it.

EDIT: checked it: works perfectly


pangyan(Posted 2004) [#16]
Here's my code, it's driving me crazy. It's just not working, no matter how many times I look at it. It feels like a bug, but I know there must be something wrong:

First file: (Main.bb)
Include "RobotCommandClass.bb"
Include "globalDefinitions.bb"
Include "StartupClass.bb"

SetupGlobals()
SetupRobotCommandTools()
SetupGraphics()
While Not KeyHit(1)
Cls
SetBuffer BackBuffer()
DrawRobotCommandTools()
Flip
Wend

Second file: (Globaldefinitions.bb)
;This handles all the functions related to handling a robot command

Type RobotCommandGlobals
;This image is obsolete due to an annoying bug.
;Field Image
Field deployedCommandIndexToUse
Field commandOnCursor
Field toolboxScreenX
Field toolboxScreenY
Field toolboxWidth
Field toolboxHeight
End Type

Type RobotCommandTool
Field screenX
Field screenY
Field Number
Field Total;Number of this type of command available to player to place on level
End Type

Type DeployedCommand
Field Number
Field LevelCol
Field LevelRow
Field State
End Type

Const MAX_ROBOT_COMMANDS = 100
Const MAX_COMMAND_TYPES = 11

Global theRobotCommandGlobals.RobotCommandGlobals

Global robotImage
Global robotCommandImage

Third file: (robotCommandClass.bb)
Dim theRobotCommandTools.RobotCommandTool(MAX_COMMAND_TYPES)
Dim theDeployedCommands.DeployedCommand(MAX_ROBOT_COMMANDS)

Function CreateRobotCommandGlobals(filename$, toolboxX, toolboxY, toolboxWidth, toolboxHeight)
theRobotCommandGlobals = New RobotCommandGlobals
robotCommandImage = LoadAnimImage(filename$, 48, 48, 0, 12)
theRobotCommandGlobals\deployedCommandIndexToUse = 0
theRobotCommandGlobals\commandOnCursor = COMMAND_NONE
theRobotCommandGlobals\toolboxScreenX = toolboxX
theRobotCommandGlobals\toolboxScreenY = toolboxY
theRobotCommandGlobals\toolboxWidth = toolboxWidth
theRobotCommandGlobals\toolboxHeight = toolboxHeight
End Function

Function DrawRobotCommandTools()

Local toolIndex

For toolIndex = 1 To MAX_COMMAND_TYPES
DrawRobotCommandTool(toolIndex)
Next

End Function

Function DrawRobotCommandTool(toolIndex)

;Draw command tile
DrawImageRect(robotCommandImage, theRobotCommandTools(toolIndex)\screenX, theRobotCommandTools(toolIndex)\screenY, 0, 0, 48, 48, 0)

;Draw command icon on tile
DrawImageRect(robotCommandImage, theRobotCommandTools(toolIndex)\screenX, theRobotCommandTools(toolIndex)\screenY, 0, 0, 48, 48, theRobotCommandTools(toolIndex)\Number)

;Print number of tools remaining
Local numberDrawX, numberDrawY

numberDrawX = theRobotCommandTools(toolIndex)\screenX + 48 - FontWidth()
numberDrawY = theRobotCommandTools(toolIndex)\screenY + 48 - FontHeight()

Color (255, 255, 255)
Text (numberDrawX, numberDrawY, Trim(Str(theRobotCommandTools(toolIndex)\Number)))

End Function

Function CreateRobotCommandTool(drawX, drawY, number, Total)

theRobotCommandTools(number) = New RobotCommandTool
theRobotCommandTools(number)\Number = number
theRobotCommandTools(number)\Total = Total
theRobotCommandTools(number)\screenX = drawX
theRobotCommandTools(number)\screenY = drawY

End Function

Function DrawAllCommands()

Local commandIndex

For commandIndex = 1 To theRobotCommandGlobals\deployedCommandIndexToUse - 1
If theDeployedCommands(commandIndex)\State = STATE_TAKEN Then DrawCommand(commandIndex)
Next

End Function

Function DrawCommand(commandIndex)

Local drawAtX, drawAtY
Local cacheDisplayCol, cacheDisplayRow

cacheDisplayCol = 0
cacheDisplayRow = 0

If cacheDisplayCol <> LEFT_EDGE And cacheDisplayCol <> RIGHT_EDGE And cacheDisplayRow <> LEFT_EDGE And cacheDisplayCol <> RIGHT_EDGE Then
drawAtX = cacheDisplayCol * 48
drawAtY = cacheDisplayRow * 48
;Draw the command tile first of all
DrawImageRect(robotCommandImage, drawAtX, drawAtY, 0, 0, 48, 48, 0)
;Draw command icon on tile
DrawImageRect(robotCommandImage, drawAtX, drawAtY, theDeployedCommands(commandIndex)\Number * 48, 0, 48, 48)
EndIf

End Function

FInal file: Startupclass.bb
Function SetupGraphics()
Graphics 1024, 768, 16
End Function

Function SetupGlobals()
CreateRobotCommandGlobals("Assets/Graphics/robotCommands.bmp", 768, 0, 200, 200)
End Function

Function SetupRobotCommandTools()
CreateRobotCommandTool(0, 0, 1, 1)
CreateRobotCommandTool(120, 120, 2, 2)
CreateRobotCommandTool(0, 0, 3, 0)
CreateRobotCommandTool(120, 120, 4, 0)
CreateRobotCommandTool(0, 0, 5, 0)
CreateRobotCommandTool(120, 120, 6, 0)
CreateRobotCommandTool(0, 0, 7, 0)
CreateRobotCommandTool(120, 120, 8, 0)
CreateRobotCommandTool(0, 0, 9, 0)
CreateRobotCommandTool(120, 120, 10, 0)
CreateRobotCommandTool(0, 0, 11, 0)

End Function

Can anyone help me out? I just don't know why the image can't be found, even though when it errors out, there is a handle in robotCommandImage.


skidracer(Posted 2004) [#17]
You need to reorder your include files so that your Globals are the first thing that are defined in your program:

Include "globalDefinitions.bb"
Include "StartupClass.bb"
Include "RobotCommandClass.bb"


dangerdave(Posted 2004) [#18]
Remember that when Blitz sees an include it just pastes the contents where the include line is.


pangyan(Posted 2004) [#19]
No, thats not it. I still get the error. Could someone please confirm:
1) That my code gives an error


pangyan(Posted 2004) [#20]
Thought so, it is a glitch with the program. I knew something didn't feel right. Anyway the above code compiles on Blitz Plus, but does not compile on Blitz3D 1.87. Am I missing something?


skidracer(Posted 2004) [#21]
You ALSO will need to call SetUpGraphics before you call SetupGlobals, the Graphics command puts Blitz in a graphics mode so it is able to handle images, at present your program is calling LoadAnimImage before it calls the Graphics command.


pangyan(Posted 2004) [#22]
Thanks a lot! That was the problem, namely setting up the graphics first. I've just regained my faith in Blitz Basic :) I was confused because the Image handle seemed to have a value and it was still unable to display the image.


Strider Centaur(Posted 2004) [#23]
Alot of times I will creat a image just to be sure to avoid the errors. This way the image handel is valid and will, in worlst case display nothing. If you have a default errer graphic, as I do when I do tile based games, this will instantly allert you to problems in the code when you see that read and yellow, ERROR, in your map, without crashing the game, good for running while still in development.

This is usually only a problem when trying to alter tiles on the map at runtime, so its nice to see the problem so clearly.