With my sprite abstraction layer in place I needed some larger structure that could manage groups of sprites and the application logic associated with them. What I ended up with was something I called a 'scene'. A scene would be some discrete function of the game, like a main menu, a map screen or a battle screen. Each scene manages its own sprites by registering them with the GraphicsManager and has an UpdateScene method that gets called by the main form on each game loop right before the graphics are updated in the GraphicsManager. All of this is handled by another interface called IScene.
I created a class named SampleScene which loads a single sprite into memory, my favorite blue dot, and handles the logic to move this dot with the arrow keys. When the enter key is pressed the scene tells the main form that it should load the next scene (which in this case is the exact same scene. The first scene is torn down and the next scene is created and away we go. On the screen this means that the blue dot just moves back to the starting location, but under the hood a scene has been swapped out.
Along with all this I added a few nice things here and there. Sprites and Scenes can dispose of themselves now to get their resources out of memory. This is done behind the scenes when a scene is torn down before the incoming scene is initialized. I also built out some factory classes for sprite and scene creation. Both need access to resources like the GraphicsManager to intialize properly so I wrapped all that up in the factory pattern. Requesting a new sprite or scene flows nicely now.
Quite a productive weekend!
Showing posts with label abstraction. Show all posts
Showing posts with label abstraction. Show all posts
Sunday, May 3, 2009
Saturday, May 2, 2009
Abstract Thinking
As I mentioned yesterday, I set out to create an abstraction layer around the early graphics work I did yesterday and ended up with something I'm quite happy with. I worked and reworked my classes several times before I came up with something that seemed straightforward and hid the implementation of the graphics rendering well enough. There's still a few things being passed around that I'd rather not pass around, but I'm not too worried about it.
I now have a class called GraphicsManager which houses the Direct3D device instance. It's job is to setup the instance and handle the rendering of each frame in the game loop. The GraphicsManager is created in the 'static void main' class (which I named Launch) along side the windows form and is immediately passed into the windows form so the form holds the reference to it. GraphicsManager has a method called PerformFrameRender which the form calls in it's overloaded OnPaint event. That was the form doesnt need to know a thing about rendering any graphics. It acts purely as a container and pump to drive the game loop. Later, the logic loop will be driven from here too.
With this framework in place I can create sprites on the screen very easy with code like this:
BasicSprite sprite1 = this.graphicsManager.CreateBasicSprite();
sprite1.LoadSprite(@"../../images/blue.tga", 100, 200);
this.graphicsManager.RegisterSprite(sprite1);
I am requesting a BasicSprite object from the GraphicsManager, loading it with an image file and a screen location, and then registering it with the GraphicsManager. (I went round and round about creating and registering in the same step or seperate steps, but figured this was more flexible when it cames to preloading a ton of image data before pushing it into the pipeline).
All sprites inherit from an ISprite interface I created. This way the rendering of the sprite is up to the implementation of the particular sprite class, but the GraphicsManager can just loop through all registered sprites and draw them. Right now I just have BasicSprite, a stationary image, but later I can make things like AnimatedSprite and MovingSprite with this framework.
I now have a class called GraphicsManager which houses the Direct3D device instance. It's job is to setup the instance and handle the rendering of each frame in the game loop. The GraphicsManager is created in the 'static void main' class (which I named Launch) along side the windows form and is immediately passed into the windows form so the form holds the reference to it. GraphicsManager has a method called PerformFrameRender which the form calls in it's overloaded OnPaint event. That was the form doesnt need to know a thing about rendering any graphics. It acts purely as a container and pump to drive the game loop. Later, the logic loop will be driven from here too.
With this framework in place I can create sprites on the screen very easy with code like this:
BasicSprite sprite1 = this.graphicsManager.CreateBasicSprite();
sprite1.LoadSprite(@"../../images/blue.tga", 100, 200);
this.graphicsManager.RegisterSprite(sprite1);
I am requesting a BasicSprite object from the GraphicsManager, loading it with an image file and a screen location, and then registering it with the GraphicsManager. (I went round and round about creating and registering in the same step or seperate steps, but figured this was more flexible when it cames to preloading a ton of image data before pushing it into the pipeline).
All sprites inherit from an ISprite interface I created. This way the rendering of the sprite is up to the implementation of the particular sprite class, but the GraphicsManager can just loop through all registered sprites and draw them. Right now I just have BasicSprite, a stationary image, but later I can make things like AnimatedSprite and MovingSprite with this framework.
Subscribe to:
Posts (Atom)