Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Creating a wrapping game world  (Read 3768 times)

0 Members and 1 Guest are viewing this topic.

Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Creating a wrapping game world
« on: December 30, 2014, 01:29:07 am »
Hi
I'm trying to create a game where there are no boundaries to the 2d game world. If you scroll off the left of the map, graphics from the far right hand side will appear.

I thought about using views but I will need at least two views just for the horizontal, in order to draw graphics nearby which internally are far apart.

Any suggestion on how to go about this?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Creating a wrapping game world
« Reply #1 on: December 30, 2014, 02:21:23 am »
The two view technique would fail as soon as you'd try to move up and to the left at the same time, plus it'd be rather tedious to write this "view stitching" procedure.

Since your game world would be limited by the size of your window, you'll never have "too many" objects, as such the most simple and straight forward way is to draw the objects which are on the border of the window twice. If an object sticks out 10px to the left, draw the object at position window.width-10px.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Creating a wrapping game world
« Reply #2 on: December 30, 2014, 03:30:29 am »
I was tinkering with a 4 view trick for this a while back but I think I got hung on the part of making the 4 views look and act like one big view when near the edge of the map.  Checking for collisions also got me as well on it since the map was truly endless in that game.


The trick is to use 4 or so views at worst when in the corners but the issue is what do you display from where?  It easier to use exploiter's solution of copying the image of the object into view.
I have many ideas but need the help of others to find way to make use of them.

Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Creating a wrapping game world
« Reply #3 on: January 01, 2015, 10:40:02 pm »
Ok I have an idea. Render the game into a texture and then draw the texture a few times for each edge to create the wrapping effect.

Is that a good idea? Is render to texture slow?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Creating a wrapping game world
« Reply #4 on: January 01, 2015, 10:52:43 pm »
Quote
Is that a good idea? Is render to texture slow?

Depends on a million details about your program that we don't know.

Rendering to a texture every frame should not be a performance problem though.  One of the biggest use cases for RenderTexture is post-processing effects, which kind of requires that there be both a draw() to the RenderTexture and a draw() to the RenderWindow every frame.

However, this method by itself won't solve the problem of getting entities on opposite sides of the world to see or collide with each other.  I don't know if you need that behavior or not.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Creating a wrapping game world
« Reply #5 on: January 01, 2015, 10:57:20 pm »
Is that a good idea? Is render to texture slow?
Depends on your performance needs. Sure, there is some overhead but it shouldn't be too bad. Why don't you just try it and see if it is "fast enough"?  That's what actually matters; not whether it's faster or slower than something else, but whether it is fast enough for your needs.


Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Creating a wrapping game world
« Reply #6 on: January 01, 2015, 11:40:14 pm »
Thanks for your suggestions.

I will make sure to handle the collisions etc, I am just doing the graphics right now.

One last question: Is it ok to create sf::RenderTexture objects each frame or should I keep one around as a member and reuse it?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Creating a wrapping game world
« Reply #7 on: January 01, 2015, 11:42:09 pm »
Only construct it once.  Same as RenderWindow.

Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Creating a wrapping game world
« Reply #8 on: January 01, 2015, 11:45:30 pm »
Actually this won't work because the game world could be huge.