DisplayBrushPart() with negative coordinates?

Discuss any general programming issues here
Post Reply
phipslk
Posts: 24
Joined: Sun Oct 29, 2023 7:21 pm

DisplayBrushPart() with negative coordinates?

Post by phipslk »

Hi all,
I want to scroll a picture under a fixed brush using DisplayBrushPart(). But I can't move it to the leftmost and top border of the picture as I had to use negative coordinates. Right now I am stucked in how to solve that problem, perhaps someone has an idea?

Code: Select all

Function _main()
	Cls()
	If IsKeyDown("left") Then part_x=part_x-1
	If IsKeyDown("right") Then part_x = part_x+1
	If IsKeyDown("up") Then part_y = part_y-1
	If IsKeyDown("down") Then part_y=part_y+1
	
	If part_x<-win_width/2 Then part_x = 0
	If part_x>= maxwidth Then part_x=maxwidth
	If part_y<0 Then part_y = 0
	If part_y>maxheight Then part_y = maxheight
	
	DisplayBrushPart(1,part_x,part_y,win_x,win_y,win_width,win_height) 
	DisplayBrush(2,cross_x,cross_y)

	Flip()

EndFunction
phipslk
Posts: 24
Joined: Sun Oct 29, 2023 7:21 pm

Re: DisplayBrushPart() with negative coordinates?

Post by phipslk »

perhaps I did not explain quite good what I want to achieve: I want to move a picture under a fixed cross on the screen, so that not the cross moves but the picture is moved by cursor keys. but the cross must reach all borders of the picture
Flinx
Posts: 192
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: DisplayBrushPart() with negative coordinates?

Post by Flinx »

I think you should provide a (in a way) working example, then it would be easier to help.
phipslk
Posts: 24
Joined: Sun Oct 29, 2023 7:21 pm

Re: DisplayBrushPart() with negative coordinates?

Post by phipslk »

yes, maybe. so this is the "alpha" code ;-) I just changed from Doublebuffer to layers because anims don't work with doublebuffer, and didn't yet finish all the changes (the black box to clear e.g.)

"bg.png" is the picture to be scrolled (720x350px)
"exp2_0.png" is an animation for an explosion

Code: Select all

@DISPLAY 1,{Width = 640, Height = 350, Mode = "fullscreen", Layers = True}
@BRUSH 1,"bg.png"
@BRUSH 3,"exp2_0.png", {LoadAlpha = True}

CreateBrush (2,32,32)
SelectBrush (2)
Line(16,0,16,32,#YELLOW, {Thickness = "2"})  
Line(0,15,32,15,#YELLOW, {Thickness = "2"})
EndSelect()
SetBrushTransparency(2, #BLACK)
explosion = CreateAnim(1, 3, 64, 64, 16, 4)

part_x = 20	 ; x coordinate of the part of landscape
part_y = 0	; y coordinate of shown part

win_width = 200			; width of the shown part of landscape
win_height = 200		; height of ...

win_x = GetAttribute(#DISPLAY,1,#ATTRWIDTH)/2	- win_width / 2		; koordinaten für sichtfenster
win_y = GetAttribute(#DISPLAY,1,#ATTRHEIGHT)/2  - win_height / 2

cross_x = #CENTER ;GetAttribute(#DISPLAY,1,#ATTRWIDTH)/2
cross_y = #CENTER ;GetAttribute(#DISPLAY,1,#ATTRHEIGHT)/2 


maxwidth = GetAttribute(#BRUSH,1,#ATTRWIDTH) - win_width/2	; das sichtbare fenster abziehen
maxheight = GetAttribute(#BRUSH,1,#ATTRHEIGHT) - win_height/2
i=0
explode=False

;SetFillStyle(#FILLCOLOR)

Function _explode(x,y)
	expanim = PlayAnim(1, x, y, {Async = True});, table])
	Repeat
	   done = AsyncDrawFrame(expanim)
	   VWait
	Until done = True
	explode=False

EndFunction

Function _main()
;	Cls()

	If IsKeyDown("any") Then Box(win_x, win_y, win_width, win_height,#BLACK)
	If IsKeyDown("left") Then part_x=part_x-1
	If IsKeyDown("right") Then part_x = part_x+1
	If IsKeyDown("up") Then part_y = part_y-1
	If IsKeyDown("down") Then part_y=part_y+1
	If IsKeyDown("space") Then explode=True

	If part_x<0 And win_x<320 
		part_x = 0
		win_x = win_x + 1
	EndIf
	 
	If part_x>= maxwidth Then part_x=maxwidth
	If part_y<0 Then part_y = 0
	If part_y>maxheight Then part_y = maxheight
	
	DisplayBrushPart(1,part_x,part_y,win_x,win_y,win_width,win_height) 
	DisplayBrush(2,cross_x,cross_y)

	If explode=True Then _explode(cross_x,cross_y)

EndFunction

EscapeQuit(True)

SetInterval(1, _main, 1000/50)

Repeat
	WaitEvent
Forever
Flinx
Posts: 192
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: DisplayBrushPart() with negative coordinates?

Post by Flinx »

In layer mode you should not constantly create new box and brush layers, the layers may only be created once.
Write an OpenResourceMonitor() in your example to see what happens.

I see two ways to achieve the desired effect. Either you create Brush 1 with a mask and move the layer and redraw the mask in the _main() function if necessary or you place an additional layer as a window in front of the Brush 1 layer.
I have quickly hacked together the second variant so that you can see the principle. The checks of the boundaries have not yet been adapted.

Code: Select all

@DISPLAY 1,{Width = 640, Height = 350, Mode = "fullscreen", Layers = True}

@BRUSH 1,"bg.png"
@BRUSH 3,"exp2_0.png", {LoadAlpha = True}

explosion = CreateAnim(1, 3, 64, 64, 16, 4)

part_x = 20	 ; x coordinate of the part of landscape
part_y = 0	; y coordinate of shown part

win_width = 200			; width of the shown part of landscape
win_height = 200		; height of ...

win_x = GetAttribute(#DISPLAY,1,#ATTRWIDTH)/2	- win_width / 2		; koordinaten für sichtfenster
win_y = GetAttribute(#DISPLAY,1,#ATTRHEIGHT)/2  - win_height / 2

cross_x = #CENTER ;GetAttribute(#DISPLAY,1,#ATTRWIDTH)/2
cross_y = #CENTER ;GetAttribute(#DISPLAY,1,#ATTRHEIGHT)/2 


maxwidth = GetAttribute(#BRUSH,1,#ATTRWIDTH) - win_width/2	; das sichtbare fenster abziehen
maxheight = GetAttribute(#BRUSH,1,#ATTRHEIGHT) - win_height/2
i=0
explode=False

Function _explode(x,y)
	expanim = PlayAnim(1, x, y, {Async = True});, table])
	Repeat
	   done = AsyncDrawFrame(expanim)
	   VWait
	Until done = True
	explode=False

EndFunction

Function _main()

	If IsKeyDown("left") Then part_x=part_x+1
	If IsKeyDown("right") Then part_x = part_x-1
	If IsKeyDown("up") Then part_y = part_y+1
	If IsKeyDown("down") Then part_y=part_y-1
	If IsKeyDown("space") Then explode=True

	If part_x<0 And win_x<320 
		part_x = 0
		win_x = win_x + 1
	EndIf
	 
	If part_x>= maxwidth Then part_x=maxwidth
	If part_y<0 Then part_y = 0
	If part_y>maxheight Then part_y = maxheight

	SetLayerStyle(1, {X=part_x, Y=part_y})

	If explode=True Then _explode(cross_x,cross_y)
EndFunction

EscapeQuit(True)

DisplayBrush(1,part_x,part_y)

CreateBrush (4,GetAttribute(#DISPLAY,1,#ATTRWIDTH),GetAttribute(#DISPLAY,1,#ATTRHEIGHT), #BLACK)
SelectBrush (4)
SetFillStyle(#FILLCOLOR)
Box(win_x, win_y, win_width, win_height,#GRAY)
EndSelect()
SetBrushTransparency(4, #GRAY)
DisplayBrush(4,0,0)

CreateBrush (2,32,32)
SelectBrush (2)
Box(win_x, win_y, win_width, win_height,#GRAY)
Line(16,0,16,32,#YELLOW, {Thickness = "2"})  
Line(0,15,32,15,#YELLOW, {Thickness = "2"})
EndSelect()
SetBrushTransparency(2, #BLACK)
DisplayBrush(2,cross_x,cross_y)

SetInterval(1, _main, 1000/50)

Repeat
	WaitEvent
Forever
phipslk
Posts: 24
Joined: Sun Oct 29, 2023 7:21 pm

Re: DisplayBrushPart() with negative coordinates?

Post by phipslk »

Hey Flink, moving the layer did the trick well. Thank you! I am still reading through all the layer commands and didn't know them all...
Post Reply