c# - How do I scale the graphics of a game? -
i'm making game in c# , xna 4.0. it's pretty finished, want add settings players can change window size if want to. current setup goes this:
void initialize() { //the window size initally 800x480 graphics.preferredbackbufferwidth = 800; graphics.preferredbackbufferheight = 480; graphics.applychanges(); } void update() { //if player completes action, window size changed if (windowsizechanged) { graphics.preferredbackbufferwidth = 1024; graphics.preferredbackbufferheight = 720; graphics.applychanges(); } }
using code, game looks @ specific resolutions:
800x480
1024x720
as can see, when window size changed not affect actual graphics of game. sprites , hitboxes of of objects stay same size, instead fill small box in corner of screen rather entire window. can tell me how can scale sprites fill window? assume need use matrix of sort, can point me in right direction?
edit:
here's draw code.
void draw(gametime gametime) { graphicsdevice.clear(color.cornflowerblue); base.draw(gametime); spritebatch.begin(); //background texture drawn @ default window size spritebatch.draw(background, new rectangle(0, 0, 800, 480), color.white); //each object in level (player, block, etc.) drawn specific texture , rectangle variable specifying size , coordinates //e.g. each block size of 64x64 pixels , in level placed @ x-coordinates 0, 64, 128 , on //each y-coordinate blocks in level '480 - 64' (window height minus block height) foreach (/*object in level*/) { spritebatch.draw(object.texture, object.texturesize, color.white); } spritebatch.end(); }
by default, spritebatch assumes world space same client space, size of window. can read spritebatch , different spaces in post andrew russell.
when resizing backbuffer, window size change changing world space (which don't want). in order not allow that, should stick transformation matrix in between transformation pipeline make correction.
spritebatch.begin
allows in one of overloads.
there numerous ways approach scaling, assume want scale uniformly, meaning sprites don't stretched out when aspect ratio changes compared initial aspect ratio. following code adjust scaling based on initial screen height.
... const float initialscreenheight = 480f; matrix transform = matrix.createscale(graphicsdevice.viewport.height / viewportheightinworldspace); spritebatch.begin(spritesortmode.deferred, null, null, null, null, null, transform); ...
note when changing resolution such aspect ratio changes compared initial aspect ratio, run issues such drawing out of screen (to right) or not drawing @ right edge of screen (getting similar blue background currently).
also, don't want calculate scaling matrix every frame in draw method, when resolution changed.
Comments
Post a Comment