Nathan Tornquist
Nathan Tornquist

Reputation: 6609

Simulate Screen Size in OpenGL and Display Scaled Image

Based on what I had read when I started working on the game that I am currently developing I came to the conclusion that openGL on android was only good if I was doing stuff in 3D. I also came to the conclusion that a canvas on a surfaceView was the best way to build a 2D game.

In that vein of thought I programmed a custom image class that in theory would scale every loaded image based on the ratio of my development phone's screen to the destination phone. This scaleX and scaleY idea is consistent throughout the entire game. Every point, image, speed, etc. is multiplied by the scale factor.

When I deploy the game to another device it just force closes every time. Somewhere in the massive web of code my custom autoscaleing system went wrong. In recent reading I've decided to adjust everything and go with OpenGL. That is where my question comes in.

Is there a way to make the game think it is played in the native resolution, IE easily convert all of the input coordinates to the coordinate that they would represent on my phone's screen. Essentially I want to simulate a 540 x 960 screen, take a picture of that, and then display that picture scaled appropriately on the user's screen.

How would I go about making a canvas that I drew everything to in absolute coordinates, then taking a picture of that canvas in a 540 x 960 region so that I could draw the picture of the first canvas to the canvas that is displayed to the user?

EDIT:

I would ultimately like to: 1. Create an image/canvas/thing that is the native resolution of my game. 2. Draw everything to said image, creating the current game state at the native resolution. 3. Take that image and scale it to the devices screen size 4. Display the game board to the users.

For touch input I would simply grab the coordinate the user is touching, and multiply it by a scale factor (ratio of native width to present width and the same for the y with height)

This allows me to make simple changes to keep the game functioning without recoding the entire thing. If this will give me a drop in performance, I probably need to use OpenGL, otherwise I will stick with that system.

My plan was to do something like this:

Canvas canvas = new Canvas(bitmap);

Where bitmap is an image the size of the native screen, then use all of my existing code.

EDI2:

To scale with a matrix, would I do something like this:

Canvas canvas; 
Matrix matrix = new Matrix();
matrix.postScale(screenWidth/stockWidth, screenHeight/stockHeight);
canvas.setMatrix(matrix);

....

Game Loop Logic with no image scaling, just drawing and updating of canvas based on   
stock size.

Is that correct?

Upvotes: 0

Views: 2032

Answers (1)

yoah
yoah

Reputation: 7230

You could use the setMatrix method on Canvas, calculate the scaling needed once, and in your code use a fixed coordinate system.

As for openGL for 2D, it works much faster than Canvas, and has a larger memory buffer for bitmaps. Canvas works well for small games, but not intensive ones.

Upvotes: 1

Related Questions