Hi all. This tutorial will be diferent from previous ones. We will talk about how to use the engine intrinsic objects to handle common tasks like loading/creating assets, render objects and recover graphic related datas. (tutorials series here)
We already used most of this functions in the last tutorials, but now we will explain each of them in great details.
This tutorial is the last from the series Introduction Demos, the next ones will talk about more advanced features.
The core Classes that exposes graphical related functions to the user are:
Before talking about each one, i will tell you where you can get those classes instances.
Every IScreen has a GraphicInfo and a GraphicFactory instance (c# property). They are created internally (the user should not try to create another one) before the IScreen virtual method Initialize being called. (So dont use them in the constructor of the screen, the best practice is to leave the constructor blank and create everything in the initialize/load content methods)
The engine instance is the wrapper for the XNA GAME class (in PC and XBOX, the Phone 7 Version works a little different) You can access it ONLY in the Initialize and Clean up methods of the IScreen. Normally you should not need to use the wrapper as a “wrapper”. The ploobsengine offers lots of functions that allows you to do everything xna does easier, but we still left to the user a change to recover the xna game class.
The main uses of the EngineStuff class is to Handle Components (explained in previous tutorials), apply general app configurations (showed in the last tutorial) and handle specific tasks like hide the mouse cursor, but it should be avoided.
The RenderHelper is recovered in all DRAW functions of the engine, again, the user should not try to create it.
This class is a read-only repository of graphics informations like:
- BackBufferHeight and BackBufferWidth
- FullScreenRectangle (Rectangle containing all the screen)
- HalfPixel (used in some DirectX 9 shaders)
- Multisample quality
- BackBuffer DepthFormat
- Texture Access information (mipmap, sampling, filters …)
- Current Graphic Adapter Info
- Recover the Window where the Game is running (In Windows plataform only)
You also have a c# event that you can listen to, that tell you when some of this properties changes.
The Graphic Factory is responsible for creating and loading all kind of Graphics related resources. To make the life easier for everyone, the functions of this factory have two prefix names. The function starting with GET means that you are loading some existing resource from you HD or somewhere else, the functions starting with CREATE means that you are creating something from ground 0.
Bellow you can find a list of everything that the factory can build (methods):
- public DynamicIndexBuffer CreateDynamicIndexBuffer(IndexElementSize elementSize, int indexCount, BufferUsage usage);
- public DynamicVertexBuffer CreateDynamicVertexBuffer(VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage);
- public IndexBuffer CreateIndexBuffer(IndexElementSize elementSize, int indexCount, BufferUsage usage);
- public RenderTarget2D CreateRenderTarget(int width, int height, SurfaceFormat SurfaceFormat = SurfaceFormat.Color, bool mipmap = false, DepthFormat DepthFormat = DepthFormat.Depth24Stencil8, int preferedMultisampleCount = 0, RenderTargetUsage RenderTargetUsage = RenderTargetUsage.DiscardContents);
- public Texture2D CreateTexture2D(int width, int height, bool mipmap = false, SurfaceFormat format = SurfaceFormat.Color);
- public Texture2D CreateTexture2DBlackAndWhite(int width, int height, bool mipmap = false);
- public Texture2D CreateTexture2DColor(int width, int height, Color color, bool mipmap = false);
- public Texture2D CreateTexture2DPerlinNoise(int width, int height, float frequencia, float amplitude, float persistence, int octave, bool mipmap = false);
- public Texture2D CreateTexture2DRandom(int width, int height, bool mipmap = false);
- public VertexBuffer CreateVertexBuffer(VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage);
- public AlphaTestEffect GetAlphaTestEffect();
- public DualTextureEffect GetDualTextureEffect();
- public Effect GetEffect(string name, bool clone = false, bool isInternal = false);
- public EnvironmentMapEffect GetEnvironmentMapEffect();
- public BasicEffect GetBasicEffect();
- public SkinnedModel GetAnimatedModel(string name, bool isinternal = false);
- public T GetAsset<T>(string assetName, bool isInternal = false);
- public Model GetModel(string name, bool isinternal = false);
- public IntermediateRenderTarget GetRenderTargetFromPool(int width, int height, SurfaceFormat SurfaceFormat = SurfaceFormat.Color, bool mipmap = false, DepthFormat DepthFormat = DepthFormat.Depth24Stencil8, int preferedMultisampleCount = 0, RenderTargetUsage RenderTargetUsage = RenderTargetUsage.DiscardContents);
- public Texture2D GetScaledTexture(Texture2D texture, Vector2 Scale);
- public Texture2D GetScaledTexture(Texture2D texture, int width, int height);
- public SkinnedEffect GetSkinnedEffect();
- public SpriteBatch GetSpriteBatch();
- public Texture2D GetTexture2D(string textureName, bool isInternal = false);
- public TextureCube GetTextureCube(string textureName, bool isInternal = false);
- public Texture2D GetTexturePart(Texture2D texture, Rectangle rectangle);
Remember: Every kind of Graphic Stuff is built using this class instance.
The Render Helper is the object responsible for handling graphic states, handling Render Targets and drawing model, text, textures, fullscreen quads and user primitives.
For us, the Graphic States are:
Instead of using the stateless XNA aproach we preferred something diferent.
In PloobsEngine, you dont set the state, instead you put it in a Stack, the top of the stack is the current State. You have a stack for each state.
The Obvious good point of this aproach is that we dont need to save our last state, cause it will be on the stack, for example:
We are drawing models using CounterClockWise Culling, but for some reason the current model NEED to be rendered with ClockWise Culling mode. You just need to push in the stack the ClockWise state, render the model and after pop it. Simple.
This simple behavior give lots of beneficts also cause we can chain states without problems.
The XNA has some “strange” behaviors sometimes, one of them is when we call the SpriteBatch methods.The documentation says that it restores the last state of the GPU, but it is not true, so when using spritebatch directely (if you use the RenderHelper methods there will be no problem) you need to resync the stacks states with the GPU states. For this just call the RenderHelper Resync Method.
For 3D, the PloobsEngine does not use the XNA Model class cause it is not flexible enough, so we use our IModelo class. The RenderHelper knows only how to render BatchInformation instances, so we need to get them from the IModelo class and pass to the RenderHelper.
This class was explained in previous tutorials. The only thing we should mention is that you should try to not use the XNA classes like GraphicsDevice that you can recover from EngineStuff for drawing or anything else. The reason is that you can change some of the internal GPU state (and forget to restore it to the original value) and affect our complex render system.
GraphicInfo: Used to recover Graphic Related data like the size of the back buffer
GraphicFactory: Used to create or load Graphic Resources
EngineStuff: Used to handle components and make XNA very specific tasks.
RenderHelper: Manage Graphic states and draw assets.
That is all for today =P
OBS: The released version does not contains support for 2D yet, but in our repo we already have a version supporting it (with 2D sprite animation, 2D physics, particles …). We also added support for Windows Phone 7 (2D and 3D)
Check it =P
The next tutorials will start to talk about our advanced features (like complex materials, water ….).
- PloobsEngine Alpha Xna 4.0: http://ploobs.com.br/?p=725
- Project Site: http://ploobsengine.codeplex.com/
- Our Blog: http://ploobs.com.br/ (most of the contents is in portuguese, try translating =P)
- Our Forum: http://www.ploobs.com.br/forum/
- Contact: firstname.lastname@example.org
- PloobsEngine Release in portuguese: http://www.bitabit.eng.br/2011/03/23/apresentando-ploobs-engine/