c# - Why use separate load methods instead of the constructor? -
opentk has separate load() method calls when game has load. xna , monogame take step further , have constructor, initialize , loadcontent. seems me programmer using framework confused when i'm supposed load in , can't 100% sure class initialized when it's constructed. reason done?
there reason xna has constructor, initialize, , loadcontent(). when create new game, such in
static class program { static void main() { using (game1 game = new game1()) { game.run(); } } }
game1's constructor called , takes care of pre-initialize tasks, such
graphics = new graphicsdevicemanager(this); content.rootdirectory = "content"; components.add(new gamecomponent());
and setting class's properties. can use constructor would. after constructor called, game.run()
method called. start game , call initialize method. in above program
, once game.run()
called, several things happen. first, game1
's initialize method called. method looks like:
protected override void initialize() { // graphicsdevice has been created, can create projection matrix. projectionmatrix = matrix.createperspectivefieldofview( mathhelper.toradians(45.0f), graphicsdevice.viewport.aspectratio, 1f, 10000); base.initialize(); }
as may notice, projectionmatrix
game created in method (not constructor) because graphicsdevice has been initialized once game.run()
called. after initialize method completes pre-game tasks base.initialize()
called, 2 things. first, gamecomponents
have added game enumerated through , initialized. second, loadcontent() called once initialized, both in game , gamecomponents, , graphicsdevice ready.
now, might wonder why loadcontent()
isn't part of initialize method. well, believe main reason can reload content if needed, "such when devicereset event occurs", or if need reset things model mesh bones.
so in summary, constructor creates class , properties would, once initialize
method called, game starts running after gamecomponents
have been initialized , content loaded.
Comments
Post a Comment