# Scaling background without scaling foreground in platformer?

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?

• Use OpenGL for graphics? Dec 26, 2013 at 19:15
• Yes, I'm using OpenGL. Dec 26, 2013 at 19:31
• Well then, show some code about your background rendering function perhaps? The math isnt all that can cause problems with OpenGL. Anyway, doesnt it look stupid if only the background scales to the window? Also check that you are not dividing (or multiplying) integers - i would use scale = (float)view->width/1280.0f; (C++ syntax). What language you are using anyway? Dec 26, 2013 at 22:31
• I'm using Java, no that isn't the problem, all the numbers are correct, it's the algorithm that isn't Dec 27, 2013 at 8:18
• So you have checked that scale isnt just integers? Its common problem that people try to divide by integers and doesnt realise the result isnt floats afterwards. Dec 28, 2013 at 23:50

Really depends on how you're going about drawing.

Let's suppose you're using an image for your back ground

Sounds like you're using an image for your background and laying sprites and tiles over?

In that case, do the resizing yourself instead of relying on Java or whatever library you're using.

Pretty simple to do, really: have a drawBackground() method, which checks the size of the frame you're drawing in, and if necessary, resizes the image. You don't want to do that every paint cycle (unless it's a continuously changing image) so hang on to the resized image.

Once you've got the background drawn, then you can tile the foreground objects without any need to rescale them. write it in a seperate method to keep your head straight.

pseudocode goes something like this: (been a while since I did this in Java, my syntax is probably atrocious)

Image i, backgroundImage; drawBackground(i); drawForeground(i);

drawBackGround(Image i); { if !(backGroundImageIsCorrectSize(backgroundImage.getDimensions())) { resizeBackgroundImage(backGroundImage); } i = backgroundImage.copy(); }

drawForeground(Image i) { foreach(tile in tileList)
tile.draw(i) }

Then, just hand off the completed image to be drawn. If you're savvy, you can even do all this in a second thread, though for simple graphics, that may not be necessary.

It's hard to be more specific without more details.

If you really wanna get a handle on java graphics, I recommend a book called "Filthy Rich Clients." It's not aimed at games per se, but it gives a really great overview of the java 2D library in plain English and it's full of good, inspiring ideas. here's the link