Viewport?

BlitzMax Forums/BlitzMax Beginners Area/Viewport?

Hotshot2005(Posted 2009) [#1]
I have got two tileimages that is overlap each other...is there viewport that could hide other the tileimages as I am making parallax scrolling demo


Hotshot2005(Posted 2009) [#2]
let say I have 5 different layers.....but I am confuse when using setviewport
could anyone give me example on how setviewport work when come using 5 different layers?


Nate the Great(Posted 2009) [#3]
hmm I dont exactly understand your question but viewports dont work on many systems unfortunately...


jkrankie(Posted 2009) [#4]
No, viewports work on most systems, just a few really old ones cause problems.

Cheers
Charlie


Hotshot2005(Posted 2009) [#5]
so does that mean I have do my own viewport functions?


_Skully(Posted 2009) [#6]
Set your viewport for the top layer, draw your images, set your viewport for the next layer down, draw your images... its really that easy.

SetViewport 0,0,Screenwidth,ViewPortheight
SetViewport 0, ViewPortheight,Screenwidth,ViewPortheight
SetViewport 0, ViewPortheight*2,Screenwidth,ViewPortheight
SetViewport 0, ViewPortheight*3,Screenwidth,ViewPortheight



Hotshot2005(Posted 2009) [#7]
thank you for tips skully :)


ImaginaryHuman(Posted 2009) [#8]
The only reason you'd want to set a separate viewport for each layer is if they are to be restricting in drawing or scaled. Surely you can just set it once and draw the parallax layers from rear to near?


_Skully(Posted 2009) [#9]
I think he's talking vertical paralax... so the distant one is nearer the top, the next closest one layer down... think of platformer type games.

Overlapping parallax can look messed to. I've played with that in the TileMax layering system. You get weird artifacting from the movement of the layers when the layers are complex. Think of two window screens moving over one another


xlsior(Posted 2009) [#10]
Moire patterns?


ImaginaryHuman(Posted 2009) [#11]
Ok but the viewport defines the scaling operation for translating world coordinates into window coordinates, I don't see that you really need to change it unless a) you want to confine drawing to within a given area, or b) change the size of pixels. If he has parallax layers that are all just regular image pixel, he doesn't need scaling, and I don't see why you'd have to use a viewport to restrict each layer ... surely you'd just use a strip of an image and draw it full? How is having a separate viewport going to help?


_Skully(Posted 2009) [#12]
The ViewPort just restricts drawing operations to the defined rectangle, so if he draws an image that starts at 1 pixel above the defined bottom, only 1 pixel of it will be drawn... prevents overlap


Hotshot2005(Posted 2009) [#13]
it`s the shadow of beast demo i am doing :)


ImaginaryHuman(Posted 2009) [#14]
Use the new sub-image drawing command which Mark just added to BlitzMax. That'll be faster than using lots of viewports.


asoed(Posted 2009) [#15]
what is the sub-image drawing command, can't find it. i have blitz 1.35


asoed(Posted 2009) [#16]
sorry, never mind. I see 1.36 is up!


Hotshot2005(Posted 2009) [#17]
DrawSubImageRect is good one but I have one problem.....

the tileimage go on forever when come to scrolling and how can I make DrawSubImageRect to the same job as tileimage?


Hotshot2005(Posted 2009) [#18]
when my background image go left then there is nothings there where if i put tileimage with viewport...it go on forever do you understand what am trying to say?

Graphics 640,480,0

Global T_list:TList = New TList

Global Background1=LoadImage("media\bgd1_ciel.png")
Global Background2=LoadImage("media\bgd2_montagnes.PNG")
Global Background3=LoadImage("media\bgd3_sol1.png")
Global Background4=LoadImage("media\bgd4_sol2.png")
Global Background5=LoadImage("media\bgd5_sol3.png")
Global Background6=LoadImage("media\sprite_barriere.bmp")

Type TBackground
     Field x: Int , y:Int
     Field x1:Int, y1:Int
     Field x2:Int, y2:Int
     Field x3:Int, y3:Int
     Field x4:Int, y4:Int
     Field x5:Int, y5:Int

     Method Draw()
         
            DrawSubImageRect(Background1,x,0,640,480,0,0,640,480,0,0,0 ) 
           
            DrawSubImageRect(Background2,x2,150,640,480,0,0,640,480,0,0,0 ) 

            DrawSubImageRect(Background3,x3,370,640,480,0,0,640,480,0,0,0 ) 

            DrawSubImageRect(Background4,x4,381,640,480,0,0,640,420,0,0,0 ) 

            DrawSubImageRect(Background5,x5,405,640,480,0,0,640,420,0,0,0 ) 

            DrawSubImageRect(Background6,x6,395,640,480,0,0,640,420,0,0,0 ) 


            x=x-1
            x1=x1-2
            x3=x3-3
            x4=x4-4
            x5=x5-5
            x6=x6-6
     EndMethod

End Type

T:Tbackground=New Tbackground

Repeat
      Cls
      T.Draw
      Flip
Until MouseHit(1) 



Jesse(Posted 2009) [#19]
can you post a working demo? so I can see what it's doing wrong.
what are the size of your images?
I think your best bet is to do tile images on viewport but you need to take into account the tile size and move it no more than the amount of the image width before returning to the beginning of the image
setviewport(0,y,graphicswidth(),image.Height)' 
tileimage image,y,x
x = (x-6) mod image.width


I think that is what you are trying to do. if you want you can send me the demo to my email addres and I'll try to figure it out for you.


Hotshot2005(Posted 2009) [#20]
I have send you email :)


Jesse(Posted 2009) [#21]
here it is:
SuperStrict
Graphics 640,480,0

Global T_list:TList = New TList

Global Background1:TImage=LoadImage("media\bgd1_ciel.png")
Global Background2:TImage=LoadImage("media\bgd2_montagnes.PNG")
Global Background3:TImage=LoadImage("media\bgd3_sol1.png")
Global Background4:TImage=LoadImage("media\bgd4_sol2.png")
Global Background5:TImage=LoadImage("media\bgd5_sol3.png")
Global Background6:TImage=LoadImage("media\sprite_barriere.bmp")

Type TBackground
     Field x: Int , y:Int
     Field x1:Int, y1:Int
     Field x2:Int, y2:Int
     Field x3:Int, y3:Int
     Field x4:Int, y4:Int
     Field x5:Int, y5:Int
	Field x6:Int, y6:Int

     Method Draw()
         	  SetViewport 0,0,GraphicsWidth(),background1.height
            TileImage Background1,x,0 
            SetViewport 0,150,GraphicsWidth(),background2.height
            TileImage Background2,x2,150
		  SetViewport 0,370,GraphicsWidth(),background3.height 
            TileImage Background3,x3,370 
		  SetViewport 0,380,GraphicsWidth(),background4.height
            TileImage Background4,x4,380 
		  SetViewport 0,400,GraphicsWidth(),background5.height
            TileImage Background5,x5,400 
		  SetViewport 0,390,GraphicsWidth(),background6.height
            TileImage Background6,x6,390 


            x =(x-1 ) Mod background1.width
            x2=(x2-2) Mod background2.width
            x3=(x3-3) Mod background3.width
            x4=(x4-4) Mod background4.width
            x5=(x5-5) Mod background5.width
            x6=(x6-6) Mod background6.width
		 SetViewport 0,0,GraphicsWidth(),GraphicsHeight()
     EndMethod

End Type

Local T:Tbackground=New Tbackground

Repeat
      Cls
      T.Draw
      Flip
Until MouseHit(1) 



Hotshot2005(Posted 2009) [#22]
thank you very much and it is work now. I going to add the clouds and trees :)


Jesse(Posted 2009) [#23]
In my laptop with intel graphics, I noticed that it kind of pauses at times. It seems to me it's the SetViewPort function causing that. I don't know how much processing speed is absorbed by it or what it's making it stall but I suggest you make your own function to scroll the images. Besides making it smoother, It should be faster. It shouldn't be hard to do.


Hotshot2005(Posted 2009) [#24]
need to work on tree and clouds




iaqsuk(Posted 2012) [#25]
Hi All, I create a blitzplus version of this viewport or above codes or similar codes. My question is, is it possible to add another tileimages after reaching the end of the original tileimages or a little before reaching the end of the original?

my code:

Lets say all my tileimages are 800 width, just before reaching end of 800 can I add more tileimages to replace current tileimages? Anyone, Thanks.


Jesse(Posted 2012) [#26]
Create your own function that scrolls and attach another image to the end. It should be a good exercise for a beginner.

if you try it and can't figure out how to solve it, I'll post some code for you but I think you should figure it out your self.


iaqsuk(Posted 2012) [#27]
Ok, I'll try solve it myself first and let you know by posting my code ok. Thanks.


iaqsuk(Posted 2012) [#28]
Hi Jesse, been pretty busy lately, finally had a chance to test my code for new tileimages from post #25.

I just basically added if & then statements if I am correct on the draw tileimages? My new codes:

;draw grass
If x < 800 Then
TileImage grass,x/2.5,0
MaskImage grass,255,255,255
ElseIf x > 800 Then
TileImage newgrass,x/2.5,0
MaskImage newgrass,255,255,255
EndIf

;draw trees
If x < 800 Then
TileImage trees,x/2.1,0
MaskImage trees,255,255,255
ElseIf x > 800 Then
TileImage newtrees,x/2.5,0
MaskImage newtrees,255,255,255
EndIf

I still can't figure out why at the end of the 1st or original image it doesn't change to new image? It only changes if I go left or west on the x axis not right? I wanted it to change at the end going right of the screen. If you can help. Thanks.


Jesse(Posted 2012) [#29]
I guess I didn't explain correctly. instead of using the built in TIleImage function make your own.
the built in one only tiles one image at a time so it's not flexible at all.

I did a quick scroller. all you need is three images to try.

I don't know how familiar you are with OOP. I hope you understand it.

use your own images.
[monkeycode]
Type TScroller
Field x:Float
Field y:Float

Field speed:Float

Field list:TList

Field firstImage:TImage
Field secondImage:TImage
Field thirdImage:TImage

Method New()
list = New TList
End Method

Function Create:TScroller(x:Float,y:Float,image1:TImage,image2:TImage,image3:TImage,speed:Float)
Local s:TScroller = New TScroller
s.x = x
s.y = y
s.firstImage = image1
s.secondImage = image2
s.thirdImage = image3
s.speed = speed
Return s
End Function

Method Update()

x :- speed
If x+ImageWidth(firstImage) < 0
x :+ ImageWidth(firstImage)
Local temp:TImage = firstImage
firstImage = secondImage
secondImage = thirdImage
thirdImage = temp
EndIf

End Method

Method Draw()
Local tx:Float = x
DrawImage firstImage,tx,y
tx :+ ImageWidth(firstImage)
DrawImage secondImage,tx,y
tx :+ ImageWidth(secondImage)
DrawImage thirdImage,tx,y
End Method

End Type

Graphics 640,480

Local image1:TImage = LoadImage("mountains.png")
Local image2:TImage = LoadImage("sky.png")
Local image3:TImage = LoadImage("tree.png")
Local scroller:TScroller = TScroller.Create(0,100,image1,image2,image3,3.0)

Repeat
Cls
scroller.Update()
scroller.Draw()
Flip()
Until KeyDown(KEY_ESCAPE)
[/monkeycode]

Last edited 2012


Hotshot2005(Posted 2012) [#30]
Neat Coding there Jesse :)


Jesse(Posted 2012) [#31]
Thanks HotShot!

here is one using lists which can use as many images as you like minimum 2 images.



Last edited 2012


iaqsuk(Posted 2012) [#32]
Hi Jesse, thank you for your reply. Yes I do understand your OOP Method coding, however, I was thinking more like this and I hope you understand what I am doing:

for example my key movements will be a plane or helicopter for tileimages left or right. I would like to set my default location of helicopter in the middle of image size height and width. If I choose to fly right and my original tileimage is 800 width and once I reach the end of the width tileimage I load another new tileimage. For example, sky, mountains, hills tileimages will stay the same, but my original grass tileimages is drawn then newgrass is drawn after reaching end of 800 width and new image is desert instead of grass.

If helicopter goes left and reaches end of 800 width, original grass tileimages is drawn a new tileimages and instead of newgrass it is a water ground. I was wondering if the post #28 was correct creating new tileimages in that coding?


Jesse(Posted 2012) [#33]
I saw your code but it really doesn't let me conclude much from it as I don't see what you are doing with the x. And how do you manage to scroll?

What I can conclude(guess) is that it is going to tile an image across the screen and when x becomes greater than 800 a different image is going to be tiled across the screen. it is going to be a sudden switch instead of a movement transition. In order to get a tile transition you are going to have to create your own function to do that.
Play with the TileImage function a bit with different numbers negative large numbers and positive large numbers and you will see that it will still draw images starting from the start of the screen or the view window. if your tileImage is at 1000,1000 it will still be drawing from the start of the screen to the end of the screen. so if you start tiling the second image. The second image won't be drawn from say half of the screen. It will just tile the whole screen and the first image won't be displayed at all. and that is why I posted the code above so you can see how tile sequence is drawn.

If you think that I still don't understand you than you are going to have to post a bit more of your code.

Last edited 2012


iaqsuk(Posted 2012) [#34]
Hi Jesse, thanks for reply. Reminder below code is in plus. Also, all my tileimages are: 7200 width, 600 height.

Also, the first codebox is what I want to use for my player movements cause it is more realistic as far as using frames and direction movements.

The 2nd codebox really works for the tileimages but helicopter is only 1 drawimages and movements goes up, down, right, left without frames. However, the tileimages is better.

I would like to end up with 1st codebox player movements with 2nd codebox tileimages. I've been trying and trying to play with the codes of the 1st codebox to keep the helicopter in the middle of the screen while images are being tiled but can't figure it out?

;;;;;;;;;;;;;;1st code box;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;end of 1st codebox;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;2nd codebox;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;end of 2nd codebox;;;;;;;;;;;;;;;;;;;;;;;

As you can tell, I am trying to make the movements like the 1st codebox movements on the 2nd codebox. Hope you can also see the new tileimages I am trying to do after reaching the end of original tileimages. I do know the changes on the new images will be abrupt changes but I eventually will change both original image to match the changes to the 2nd image if I get it to work.

hope this makes sense or you can help me out? Thanks.


Jesse(Posted 2012) [#35]
for this game I created my own scroller function:

http://www.hexobot.com/html5/MonkeyGame.html

what I did is leave the player in the same x position if the player moved forward then I would pas a variable to the scroller that tells the scroller how much to move left
same thing if the player moved left: I would tell the scroller how much to move right.
if the player jumps I would just set the player movement to jump up or down while leaving the character x position in the same place.

I would really like to try your code to see what you are doing wrong but I don't want to spend a bunch of time I don't have to go trough your code and try to process it all. Also 1 all I have installed in my MacBook is blitzMax and Monkey and 2 BlitzPlus doesn't work on my Macs.

If you want I can send you the monkey source code for the above game to see if that helps you.

Last edited 2012


iaqsuk(Posted 2012) [#36]
Hi Jesse,

That would be cool if you can send the source code either here or monkey forum, cause I just purchased monkey too. thanks.

I completely understand that you cant take time to text my codes, but the players movements was when I was getting help from midimaster here on another post: http://blitzbasic.com/Community/posts.php?topic=94869

I saw you posted in this forum too. This is where I got help for the codebox 1 more realistic movements. I'll try and figure out the scrolling issue and player movements myself. thanks again.


Jesse(Posted 2012) [#37]
sent to your yahoo Email.

Last edited 2012


iaqsuk(Posted 2012) [#38]
wow Jesse, thank you for sending the code in monkey. Great stuff, i will certainly study the coding and try to understand what is going on.

The only reason why I purchased monkey is the many platforms it has and it is so similar to max. Thanks again. I will continue to look at my above coding when I have time. later.