Skip to content

2. Rendering Textures

Justin Skiles edited this page Apr 17, 2020 · 1 revision
  1. Follow the steps from the above Creating a Blank Window example.

  2. Add a LoadContent method to your MainGame class. Update the MainGame constructor to set the engine's LoadContent action to your method. This will inject your implementation into the game loop.

    private IWindow window;
    private IRenderer renderer;
    private Texture texture;
    
    public MainGame(IGameEngine engine)
    {
    	this.engine = engine;
    	engine.Initialize = () => Initialize();
    	engine.LoadContent = () => LoadContent();
    }
    
    // Load a surface into memory from the image at your path.
    // Create a GPU-driven texture using a renderer and a surface.
    private void LoadContent()
    {
    	Surface surface = new Surface("image.png", SurfaceType.PNG);
    	texture = new Texture(renderer, surface);
    }
  3. Add a Draw method to your MainGame class. Update the MainGame constructor to set the engine's Draw action to your method. This will inject your implementation into the game loop.

    private IRenderer renderer;
    private Texture texture;
    
    public MainGame(IGameEngine engine)
    {
    	this.engine = engine;
    	engine.Initialize = () => Initialize();
    	engine.LoadContent = () => LoadContent();
    	engine.Draw = (gameTime) => Draw(gameTime);
    }
    
    // Clear the screen to prevent stale renders.
    // Draw the texture at (100, 100).
    // Commit renders to the window.
    private void Draw(GameTime gameTime)
    {
    	renderer.ClearScreen();
    	texture.Draw(100, 100);
    	renderer.RenderPresent();
    }
  4. Add an UnloadContent method to your MainGame class. Update the MainGame constructor to set the engine's UnloadContent action to your method. This will inject your implementation into the game loop.

    private Texture texture;
    
    public MainGame(IGameEngine engine)
    {
    	this.engine = engine;
    	engine.Initialize = () => Initialize();
    	engine.LoadContent = () => LoadContent();
    	engine.Draw = (gameTime) => Draw(gameTime);
    	engine.UnloadContent = () => UnloadContent(gameTime);
    }
    
    // Always dispose your assets!
    private void UnloadContent()
    {
    	texture.Dispose();
    }
  5. Build and run to see drawn textures

    dotnet build

    dotnet run

Clone this wiki locally