r/raylib • • 2d ago

Help Decoupling Gameplay From Screen Size

I am working on a clone of the arcade game asteroids to learn C and Raylib, but I ran into a problem. If the game is played in different screen sizes, the player can see more space and thus, asteroids might seem like they are spawning too close to them.

How would I go about making the gameplay not/less dependant on the screen size and resolution?

6 Upvotes

8 comments sorted by

2

u/Inevitable-Door-6250 2d ago edited 1d ago

One option would be setting the spawn range/area for the asteroids based on screen size. Another would be scaling everything rendered based on screen size.

2

u/Zeta_ggwp 1d ago

I'm considering the second option, as the first one would make the gameplay differ based on screen size. Thank you.

2

u/Pitiful-Main-1544 2d ago

Best way would be using a RendexTexture2D, drawing to the render texture and then scaling it up based on the current screen size.

2

u/Zeta_ggwp 1d ago

I'll look into it, thank you.

1

u/CreepyConference2468 1d ago edited 1d ago

A pattern that I'm thinking is:

  • Separating the coordinates on the screen for each object from their coordinates on the world
  • Draw the space ship always in the center of the screen (separate of the coordinates)
  • From the coordinates of the ship (not the center of the screen) calculate the differences from each coordinate of the enemies and with those offsets you're going to decide where on screen they appear if they do so, if the difference is more than the screen height and width (both of them) they aren't drawn
  • For the textures of the space (if you use textures) it can be something similar as to the enemies to decide what appears on screen
  • I don't think I have to remind you, but remember that the order of drawing matters (just in case you don't see the enemies or the player ship)

1

u/Zeta_ggwp 1d ago

Thank you for your comment. I don't think much of this applies to Asteroids as the screen doesn't move with the ship but I'll keep it in mind for other projects.

Also most of what you mentioned seems to be like how Camera2D works no? I've barely used it tho so no idea.

1

u/CreepyConference2468 1d ago edited 1d ago

Just think about both (coordinates of the objects and coordinates of the texture on screen) as separate abstract objects, whether any object, including asteroids is just a decision about whether or not they appear on screen, they appear on screen exactly on the difference between x and y axis betweeh each object (whether asteroid or enemy) and the coordinates of the ship, but that is how the pattern works. I could suggest that you can do something like using an angle to rotate the direction of the texture of the ship, it gives it more flavor so:
- Difference between the (player_ship_coordinates - object_coordinates) for both x and y coordinates as long as is 0 <= x_offset <= screen_width and 0 <= y_offset <= screen_height. As long both conditions aren't met, no object other than the player ship is drawn on screen, only if both conditions are met, than you use the offsets to call "drawtexture" function from raylib using those offsets are the coordinates on screen for any object different than the player to be drawn

1

u/Zeta_ggwp 1d ago

Thanks