image not loading correctly

Blitz3D Forums/Blitz3D Beginners Area/image not loading correctly

767pilot(Posted 2004) [#1]
Can anyone help with the below?

When the crosshair image and the target 1 image are both loaded and displayed they are the same image. When going through the debugger they have the same id. Is this a bug within blitz itself?

if you need images etc I can email the whole program

thanks

;==========================================================================================
;create globals
Global gfx_x=640
Global gfx_y=480
Global gfx_colour_depth=16
Global target_image_width=40
Global target_image_height=40
Global target_found=0
Global target_id=0
Global target_mouseover=0
Global mouse_crosshair_image=LoadImage("gfx\crosshair.bmp")

;==========================================================================================
;create window
Graphics gfx_x,gfx_y,gfx_colour_depth,0 ;screen =640,480,16 bit colour
AppTitle="Shape Shoot"
SetBuffer BackBuffer()
;SeedRnd MilliSecs();get better random result

;==========================================================================================
;create types
Type target
Field x=0 ;x pos
Field y=0 ; y pos
Field id ; id number of target
Field hit=False ;target hit/miss
Field image ;target graphic
Field xmax ;max x postion of graphic on screen
Field ymax ;max y postion of graphic on screen
Field xmin ;min x postion of graphic on screen
Field ymin ;min y postion of graphic on screen
Field moveleft=0 ;is the target moving left or right? 0 right 1 left
End Type

;==========================================================================================
;create objects from types
;#Region create target objects
target_1.target=New target
target_1\x=target_image_width
target_1\y=target_image_height
target_1\id=1
target_1\hit=False
target_1\xmax=gfx_x-(target_image_width*2)
target_1\ymax=gfx_y-(target_image_height*2)
target_1\xmin=0+(target_image_width)
target_1\ymin=0+(target_image_height)
target_1\image=LoadImage("gfx\target.bmp") ;x40,y40 pixels
target_1\moveleft=0
MaskImage (target_1\image,255,255,255) ;get rid of the surrounding white of the image

target_2.target=New target
target_2\x=target_image_width*4
target_2\y=target_image_height*4
target_2\id=2
target_2\hit=False
target_2\xmax=gfx_x-(target_image_width*2)
target_2\ymax=gfx_y-(target_image_height*2)
target_2\xmin=0+(target_image_width)
target_2\ymin=0+(target_image_height)
target_2\image=LoadImage("gfx\target2.bmp") ;x40,y40 pixels
target_2\moveleft=0
MaskImage (target_2\image,255,255,255) ;get rid of the surrounding white of the image
;#End Region

;==========================================================================================
;main loop
While Not KeyHit (1) ;do below until ESC is press then quit

If KeyDown (57) Then WaitKey ;if spacebar is pressed then pause

;clear the screen
Cls


;draw the targets on the screen
DrawImage target_1\image,target_1\x,target_1\y
DrawImage target_2\image,target_2\x,target_2\y
DrawImage mouse_crosshair_image, MouseX(), MouseY()

Gosub move_targets

;check if mouse if over the position of targets
If target_found=0 Then check_target_pos(1)
If target_found=0 Then check_target_pos(2)

target_found=0 ;reset target found flag


Gosub target_debug ;display debug info

;flip the buffer screen
Flip



Wend

End




;==========================================================================================
;functions

Function check_target_pos(id)
;used to check the position of any targets on screen and find out if mouse is over/target shot

Local target.target=Null

target=get_target_id(id) ; try and find 'number'
If target <> Null ; check to see whether it's found it.

;ok so i found the target number

;check if the mouse is over the target
target_mouseover=ImageRectCollide (target\image, target\x, target\y,0, MouseX(),MouseY(),1, 1)

;if it is then get then find out which target it is over
If target_mouseover=1 Then
target_id=target\id ;get id of the target

;check to see if mouse button 1 is down
If MouseDown(1) And target_mouseover=1 Then

target\hit=1
target\image =LoadImage("gfx\target_hit.bmp")
;FreeImage target\image ;remove the target from the screen

End If

target_found=True ;we found it so no need to look anymore
Else
;if mouse is not over any of the targets
target_id=0 ;no target to find id of of
target_found=False ;not found a target so keep looking until mouse is over a target
End If

;target\x=Rand (target\xmin,target\xmax);move x pos of target to random x pos
;target\y=Rand (target\ymin,target\ymax);move y pos of target to random y pos
Else
EndIf

End Function

Function get_target_id.target(id)
;cycle through all available targets and return the id

For i.target=Each target

If i\id = id Then Return i ;return the id of the target we find

Next

End Function




;==========================================================================================
;subroutines etc

;move the targets around the screen
;#Region move targets
.move_targets
target_num=1

For target.target=Each target

Select target_num

Case 1

If target\moveleft=0 Then
target\x=target\x+3

If target\x=>target\xmax Then
target\moveleft=1
target\x=target\xmax
End If

Else

target\x=target\x-3

If target\x<=target\xmin Then
target\moveleft=0
target\x=target\xmin
End If

End If

Case 2

If target\moveleft=0 Then
target\x=target\x+5

If target\x=>target\xmax Then
target\moveleft=1
target\x=target\xmax
End If

Else

target\x=target\x-5

If target\x<=target\xmin Then
target\moveleft=0
target\x=target\xmin
End If

End If

End Select

target_num=target_num+1

Next

Return
;#End Region

;debug
;#Region debug
.target_debug

;display mouse and target info for debug purpose
y=390

For target.target=Each target


Text(20,y,"target "+target\id +" x="+target\x+" y="+target\y+" xmin="+target\xmin +" xmax="+target\xmax+" ymin="+target\ymin +" ymax="+target\ymax)

y=y+10

Next

Text(20,y,"mouse x=" +MouseX() +" y=" +MouseY() +" button1=" +MouseDown(1) +" collide=" +target_mouseover)

y=y+10

Text(20,y,"target id="+target_id)

Return
;#End Region


Shambler(Posted 2004) [#2]
target_1 and target_2 are not global variables so can't be accessed by your functions. oops ignore that -.-


semar(Posted 2004) [#3]
target_1 and target_2 are not global variables so can't be accessed by your functions.

@Shambler,
I'm sorry but that is wrong. Any type pointer is automatic Global, even if it is not declared so.

@gjpollitt,

You are using:
target_1.target=New target
and
target_2.target=New target

I guess you want two separate target elements, sadly the target type collection is unique.

That means that you don't have two separate target collections, but just one, named: target.

Perhaps this is the problem..

Try to use an unique name instead of target_1 and target_2.

You will sort it out quickly then.

Another thing.

In your main loop, you call the function << check_target_pos(id) >> at each loop.

In that function, you have this statement:
target\image =LoadImage("gfx\target_hit.bmp")

You should never have any Load... command in a loop (or in a function called from a loop), otherwise the program will slow down, and lots of videomemory will be wasted.

You should, instead, put all the Load... statements OUT from the main loop. Make a function called 'Initialization' which is outside of the main loop, and call it once.

Put all the Load... (sound, images, models...) statements there, and live happy !

:)

Hope this has sense for you,
Sergio.


767pilot(Posted 2004) [#4]
ok ok, my fault

the problem i am having is the mouse cursor bmp. The 2 target bmps are loaded fine no problems but when i load the bmp for the mouse_crosshair_image i get the bmp of target 1. How is that? even when the crosshair is loaded first i get the image of target 1? looking at the debug info the id of the 2 images is the same, target 2 image loads ok


semar(Posted 2004) [#5]
I've added some more info to my previous post. Read it carefully.

Sergio.


767pilot(Posted 2004) [#6]
semar
thanks for the help but the

target\image =LoadImage("gfx\target_hit.bmp")

is only loaded to either target 1 or 2 when it is hit ie mouse over and button 1 pressed. it will only be run the once. I cannot put it into an initialisation routine as the image prior to being hit is different so I load it with a target image, check if its been 'hit' then change the image if it has.

The program runs fine, target 1 is red, target 2 is yellow, both move left to right at a set speed. Problem still is that mouse cursor is target 1 image, even though it has been set for its own unique image.

Maybe it would be best for you to run the prog to see exactly what the problem is, but thanks for your input