Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / backdrop scrolling

Author
Message
ketchup mustard
16
Years of Service
User Offline
Joined: 31st Jan 2008
Location:
Posted: 18th Apr 2010 22:14
my backdrop is a repeating picture where if it goes off the screen it will reappear at (0,0). Except it will only do it in one direction, right. It is a 2D backdrop, side-scroller type implementation:


all i want to do is when my character 'moves left' (left key), move the backdrop right, vice-versa. Only, I need the backdrop to replace itself at (o,o) without "sticking". I used to an elongated picture at exactly 1024 pixels in width. Halp : D

Don't Clap, Just Throw Money!
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 19th Apr 2010 22:45 Edited at: 19th Apr 2010 22:49
Why do you create animated sprite when the backdrop is just one picture without animation? A simple sprite is enough.

In your code, these lines seem OK at first sight:



However, then you position the sprite first at X, then at X-1024... And I don't understand this part:



What is the screenf value? You adjust bdropX back to zero regardless if it is greater or smaller than screenf - in other words, you always set it back to zero. If this statement is within your main loop, then probably that's why the position of your backdrop does not want to change. (Is that what you meant by "sticking?")

Is the backdrop picture large enough to cover horizontally the entire level or does it only cover the visible window? Depending on the size of your background image, the result may be that part of the background will be empty after scrolling, since part of the picture will slide off the screen. This can be solved by making two copies of the picture and sticking them after each other.

Alternative idea: To scroll the backdrop, you can also try texture scrolling. By changing the texture coordinates, you can create the illusion of movement without actually moving the sprite. I made a texture scrolling implementation in the side-scroller demo in the Dark GDK coding challenges thread. Here are the relevant lines:



sprBackground is the number of the background sprite. TexScroll is the amount how much the texture must be scrolled. It is applied to all four corners of the image. The TexScroll value can be positive or negative (left or right).

You only need to find out a suitable calculation method for this TexScroll value. There is a calculation method in my demo, but I calculated screen left edge position instead of player position which may not be good for you. Try to calculate the background movement from the player velocity.
ketchup mustard
16
Years of Service
User Offline
Joined: 31st Jan 2008
Location:
Posted: 20th Apr 2010 03:07
The screenf value is 1024. The image width is 1024, or longer than my debug screen width.

I added a 1024 integer as a naked number, which is irrelevant because I already have the screenf value. How do I /not/ put the function in the game loop? My definition presides outside, but the declaration: backdrop(); is inside said game loop.

I added the 'Or Pipe Symbol' ( || ) so that if the backdrop is less than or equal to, or greater than or equal to 1024 ( end of picture ) it will repeat the picture left or right of the original at pixel 0,0. At least, that is what I aim to do. Thanks for the help :3 i will work with the ideas you gave me.

Don't Clap, Just Throw Money!
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 20th Apr 2010 08:49 Edited at: 20th Apr 2010 09:07
If you want to check the X coordinate of the picture to see when it should be looped back, I would rather do something like this:



(ImageSize is 1024 in your case.)

The left and right checking conditions surely cannot be the same, you need different conditions for the different moving directions. The above code will move the background and loop back when the picture is completely outside the screen, but as I said above, part of the background will be "uncovered" with only one moving picture. Either two pictures or the texture scroll method is required for complete coverage.

If you make two pictures and stick them after each other, then the X coordinate of the second picture can be calculated like this:



I didn't test the above code snippets but they should work. Ask if there are further problems.
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 20th Apr 2010 09:12 Edited at: 20th Apr 2010 09:13
Here is one more code snippet for you. If you have a background picture which is wider than the playing window and you do not want to scroll outside the picture limits, then try this code:



This will not "loop back" the picture, it will just stop scrolling when the edge of the image is reached, so you can scroll in the beginning only to the right, and then you can scroll back left until zero. (Again, the code is not tested.) In this case, only one background picture is enough. It depends which way you need to implement in your game.
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 21st Apr 2010 05:42
Here's a class I had wrote long-ago that provides the same effects that I believe you are looking to achieve, in fact, I believe you can achieve what you are describing as well as what Mireben was describing...

Only, it has now be GDK-afied! LOL...

Just create a new GDK project, copy these files (and add to) that project, compile and run...


I hope this helps.

JTK

Attachments

Login to view attachments
ketchup mustard
16
Years of Service
User Offline
Joined: 31st Jan 2008
Location:
Posted: 17th May 2010 01:24
okay boys n girls, I cleaned up the code. Here is the result:


There is one problem, and I can pretty much detect where the problem resides. There is something wrong with the last three lines. I believe this to be true because I have done everything to the remainder of the code and it has not changed anything much. So I mimicked Mireben's snippet.

The only problem is that as soon as my character heads right to the end of the backdrop, it will not repeat. However the backdrop will infinitely repeat heading left. This is to create the illusion of movement as my character stays in the middle of the screen - i.e. side scrolling backdrop.

Don't Clap, Just Throw Money!
Plystire
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location: Staring into the digital ether
Posted: 17th May 2010 07:05


That is invalid, and is probably why it created a problem and had you comment it out.

9.5 is not a valid ID number. ID's should be integer values... like 10.

Give it a proper value and it should work.

Also:


What is "bdropf"? Should you not be using "screenf" like you did in the right movement code?


The one and only,


JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 17th May 2010 07:09 Edited at: 17th May 2010 07:10
I suggest *looking* at the code I gave you. It does exactly what you want...

You are free to use the code in any way you want, if that's cut-and-paste, so be it... lol

In short, it's not as *simple* as you think...

Regards,
JTK
ketchup mustard
16
Years of Service
User Offline
Joined: 31st Jan 2008
Location:
Posted: 24th May 2010 23:27
Quote: "That is invalid, and is probably why it created a problem and had you comment it out.

9.5 is not a valid ID number. ID's should be integer values... like 10.

Give it a proper value and it should work."

That is entirely the problem. Thank you, Plystire, it works wonders~! Also, JTK, I did look at your source code. It was against my better judgement to do so as I am apprehensive towards opening unknown files online. Your code is way over my head, but I appreciate your efforts

Don't Clap, Just Throw Money!
ketchup mustard
16
Years of Service
User Offline
Joined: 31st Jan 2008
Location:
Posted: 24th May 2010 23:50

*the integer name choices are not consistent with what was meant or logical to the code, but are subject to change LOL i feel so legal. XP

Don't Clap, Just Throw Money!

Login to post a reply

Server time is: 2024-07-07 00:20:30
Your offset time is: 2024-07-07 00:20:30