Public Group

# Calculate resolution and scaling

This topic is 2877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm working on a 2D strategy game (it's not my first game) and i got this problem:

I got a background image that represent a terrain and i can look this terrain with my camera by moving the mouse.
This terrain is divided in squares.

For the camera i'm using an OrthoOffCenterLH type of camera:

 D3DXMATRIX matProj; D3DXMatrixOrthoOffCenterLH(&matProj,0.0f,800.0f,600.0f,0.0f,1.0f,1000.0f); dev->SetTransform(D3DTS_PROJECTION,&matProj); 

the "cam" variables are there to scroll the camera on the terrain. Their value is increase when the mouse touches an box (rectangle) in the borders.

Anyway, on this terrain i'm also drawing other sprites (units for example).

If i let the sprite as it is the 32x32 (size of unit's sprite) and the 640x640 (terrain's sprite) are stretched, so i scaled them by using the transformation matrix to adapt them to my resolution (800x600):

 sprite->D3DXMatrixScaling(&scaling,640.0f/800.0f,640.0f/600.0f,0.0f); sprite->SetTransform(&scaling); sprite->Draw(Terrain,NULL,NULL,NULL,0xFFFFFFFF); 

Now i'm trying to use a 32x32 sprite on top of the one above. The problem is that the previous scaling doesn't make the new sprite fit properly (it has to occupy a square of the previous sprite).

This is how it looks like :

This one is an image printed in Gimp to show you that the actual sprite it's 32x32 (Ignore the square and the effects around the image):

I cannot still figure out a proper formula to calculate the scaling ratio to achieve my goal.

This problem it's more visible once i've implemented a selection system that draws black borders on the rectangle that the cursor is in, but as i get far from the starting position 0.0, the error of the size gets larger and larger.

Any tips on how to manage this properly?

##### Share on other sites
Updated with images.

Also i've fixed the sprite stretching that was due to my error with the projection matrix:

i was giving that matrix wrong variables (cam variables) while they belonged to the view matrix instead.

The unit it's still smaller than the square as you can see in the image (in case you are wondering, the selector fits the units perfectly)

##### Share on other sites
Drejn, if you are scaling your background to fit a particular resolution, then you must also scale your sprites by the exact same factor in order for them to fit as they would at the old resolution.

Unless I'm misunderstanding what you are saying, here...

##### Share on other sites

I'm a bit messy, i know so forgive me xD

This is actually my Render call (where SelectPosition() gives the rectangle position of the selector and SetupMatrices() setups the view and projection matrices + the camera management):

 dev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255),1.0,0); if(SUCCEEDED(dev->BeginScene())) { sprite->Begin(D3DXSPRITE_ALPHABLEND); SetupMatrices(); D3DXMATRIX scale; D3DXMatrixScaling(&scale,640.0f/800.0f,640.0f/600.0f,0.0f); sprite->SetTransform(&scale); sprite->Draw(terrain,NULL,NULL,NULL,0xFFFFFFFF); sprite->Draw(Tank,NULL,NULL,&pos,0xFFFFFFFF); if(SelectPosition()) sprite->Draw(Select,NULL,NULL,&cursor,0xFFFFFFFF); sprite->End(); dev->EndScene(); } dev->Present(NULL,NULL,NULL,NULL); 

With this setup (and even recalling the SetTransform with the same matrix) the unit size it's still smaller than what it should be (see Image1 in the OP for the resulting image and Image 2 in the OP for how the unit should look inside the square).

Without the scaling matrix i have the same problem, but i'm sure that the terrain it's 640x640, that the squares are 32x32 and that my unit's sprite it's 32x32 (see image 2). The selector it's also affected by the same problem (the selector fits the unit perfectly)

##### Share on other sites
Are you scaling the position of the sprite, as well?

##### Share on other sites

Are you scaling the position of the sprite, as well?

it's position it's 0,0 for every sprite for now, so i don't think it would affect the results.

Like i said: even if i don't scale the sprites the units and selector will be shrinked anyway

##### Share on other sites
Have you tried using sprite->beginscene and sprite->endscene? It could be you are missing a flag...

How about the return value of draw and settransform? What do they say?

It seems to me that, if you call settransform before each batching of a sprite, every sprite should be transformed.

##### Share on other sites

Have you tried using sprite->beginscene and sprite->endscene? It could be you are missing a flag...

How about the return value of draw and settransform? What do they say?

It seems to me that, if you call settransform before each batching of a sprite, every sprite should be transformed.

beginscene and endscene are not part of the sprite class. I'm using the usual Begin and End methods of the sprite's class (check the code few replies before this)

Even if i don't use the scaling at all the unit it's still smaller than the square and i tried to check if they were of the same size.

Now, SetTransform doesn't return anything since it's a void function and tried to execute it even before calling the sprite->Begin method.

The draw seems to be fine, i cannot debug though the draw execution it seems

This is the function that sets my matrices and camera (mp it's a POINT type that stores the mouse position, while the "scroll" variables are RECT type and store the area to check for scrolling with the mouse):

void SetupMatrices() { D3DXVECTOR3 EyePt = D3DXVECTOR3(camx1,camy1,-10.0f); D3DXVECTOR3 LookatPt = D3DXVECTOR3(camx1,camy1,0.0f); D3DXVECTOR3 UpVec(0.0f,1.0f,0.0f); D3DXMATRIX matView; D3DXMatrixLookAtLH(&matView,&EyePt,&LookatPt,&UpVec); dev->SetTransform(D3DTS_VIEW,&matView); D3DXMATRIX matProj; D3DXMatrixOrthoOffCenterLH(&matProj,0.0f,800.0f,600.0f,0.0f,1.0f,1000.0f); dev->SetTransform(D3DTS_PROJECTION,&matProj); if(mp.x > Rscroll.left && mp.x < Rscroll.right) { if(camx<=1400) { camx+=2; camx1+=2; } } if(mp.x < Lscroll.right && mp.x > Lscroll.left) { if(camx1>0) { camx-=2; camx1-=2; } } if(mp.y < Tscroll.bottom && mp.y > Tscroll.top) { if(camy1>0) { camy-=2; camy1-=2; } } if(mp.y > Bscroll.top && mp.y < Bscroll.bottom) { if(camy<=1400) { camy+=2; camy1+=2; } } }

##### Share on other sites
Don't know where I got the idea that sprite had a beginscene and endscene, sorry about that.

You say that even if you don't scale *anything*, the sprite is too small? Well, I'd say you have something wrong with the texture, then. Either it's being loaded too small or something else is happening.

##### Share on other sites

Don't know where I got the idea that sprite had a beginscene and endscene, sorry about that.

You say that even if you don't scale *anything*, the sprite is too small? Well, I'd say you have something wrong with the texture, then. Either it's being loaded too small or something else is happening.

that's what i'm thinking, but by comparing the terrain with the unit, they fit perfectly so that the unit completely occupies the square. That is in an Image editor.

Should i try to create another sprite and see what happens?

UPDATE: I've managed to use primitives instead and the unit finally fits the image as it should. By saying this, that means that the sprite it's creating a larger quad for my texture, but why?

UPDATE 2 : by googling i've maybe found what's the solution:

in this article there's a talking about the sprite->Draw method having some problems with their size being smaller or miscalculating bigger textures. Lots of the thing written there like the "add 0.5 to borders" are actually true even if it talks about DirectX8: when i scaled for the first time i've noticed the scaling was 1.5 time smaller.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 10
• 18
• 13
• 9
• 9