Scaling background without scaling foreground in platformer?

Posted by David Xu on Game Development See other posts from Game Development or by David Xu
Published on 2013-12-26T18:44:50Z Indexed on 2014/05/27 3:44 UTC
Read the original article Hit count: 449

Filed under:
|
|
|
|

I'm currently developing a platform game and I've run into a problem with scaling resolutions.

I want a different resolution of the game to still display the foreground unscaled (characters, tiles, etc) but I want the background to be scaled to fit into the window.

To explain this better, my viewport has 4 variables: (x, y, width, height) where x and y are the top left corner and width and height are the dimensions. These can be either 800x600, 1024x768 or 1280x960. When I design my levels, I design everything for the highest resolution (1280x960) and expect the game engine to scale it down if a user is running in a lower resolution.

I have tried the following to make it work but nothing I've come up with solves it so far:

scale = view->width/1280;
drawX = x * scale;
drawY = y * scale;

(this makes the translation too small for low resolution)

and

scale = view->width/1280;
bgWidth = background->width*scale;
bgHeight = background->height*scale;
drawX = x + background->width/2 - bgWidth/2;
drawY = y + background->height/2 - bgHeight/2;

(this makes the translation completely wrong at the edges of the map)

The thing is, no matter what resolution the game is run at, the map remains the same size, and the foreground is unscaled. (With a lower resolution you just see less of the foreground in the viewport)

I was wondering if anyone had any idea how to solve this problem?

Thank you in advance!

© Game Development or respective owner

Related posts about 2d

Related posts about graphics