c# - Scale all sprites on screen based on resolution -


i decided move game windowed fullscreen mode , that's first problem face. i'm looking way of resizing of sprites based on screen resolution. background in (0, 0) coordinates, need have , sprites scale kind of fixed aspect ratio (16:9 preferred). , resize them portion background stretched fill screen. , not more, not less.

i've looked online tutorials couldn't understand concept used. can explain how that? read using rendertarget2d , passing spritebatch.begin() call, has kind of effect, there's got more code.

i'm not looking supporting resolution change option, adapting sprites current resolution.

it sounds you're talking resolution independence.

the general idea make game using virtual resolution , scale or down fit actual resolution of screen.

var scalex = (float)actualwidth / virtualwidth; var scaley = (float)actualheight / virtualheight; var matrix = matrix.createscale(scalex, scaley, 1.0f);  _spritebatch.begin(transformmatrix: matrix); 

for example, if virtual resolution 800x480 render sprites relative that. before rendering sprite batch, create transformation matrix pass begin call.

the other thing should know you'll need scale mouse / touch input coordinates in reverse deal them in virtual resolution. in update method can scale mouse position in reverse this:

var mousestate = mouse.getstate(); // you're doing var mouseposition = new vector2(mousestate.x, mousestate.y); var scaledmouseposition = vector2.transform(mouseposition, matrix.invert(matrix)); 

then can use scaled value in places you're using mousestate.x , mousestate.y.

it gets more complicated if want implement letterboxing or pillarboxing. take @ viewport adapters in monogame.extended if want know how works.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -