Effective drawing of 2D sprites (bitmaps)

I am writing a 2d mosaic engine. Currently, my drawing program uses the C # Drawing library to redraw every visible tile every time the screen refreshes. I have scrolling and scaling to work, and everything looks the way I want. But the routine is slow. Now I'm trying to improve it. I have a few questions:

Firstly, I think that it is not necessary to redraw tiles with each update (since they never change). Now I'm trying to change the algorithm so that it writes the entire map into one bitmap during initialization, and then cuts the correct part of the bitmap when it draws time. Do you think this is the right way? (I also thought about leaving the image in the background and just scrolling through it. But then I decided that I didn’t want to draw the material out of sight. However, maybe this is cheaper than cutting / pasting? Memory and time error?)

Secondly, as I understand it, C # Drawing routines do not use the full power of the GPU. I think I should try drawing in OpenGL (or DirectX, but I prefer the former, since it is multi-platform). Will this help? Do you know that a secret (or general drawing) tutorial is used for OpenGL? A link to a book may also help.

I also do not do multithreading at the moment (in fact, I only have a vague idea of ​​what it is). Should I try a multi-threaded box? Or will OpenGL do multithreading for graphical redundancy?

Thank.

+3
source share
3 answers

? WinForms (Win32) WPF.

, .NET GPU. DirectX OpenGL, (, , , , ) . N x, y. , , , , , , . GPU, .

OpenGL vs DirectX - , IMO DirectX , , OpenGL. OpenGL Windows . DirectX.

, OpenGL DirectX 2D-. OpenGL DirectX , , . OpenGL DirectX . GDI, .

, , . , . , , .

, . ( ), , , . : 9 , 9 1 1 ?; >

, . , .

- , - , , , , , .

+2

OpenGL, 2d SDL. OpenGL # - , .NET.

XNA - #, , SDL ( .net) (, ).

, . , , GDI + ( System.Drawing). , , , .

+2

OpenGL, ManagedDX ( XNA). ManagedDX , SlimDX, .

DX Texture. (, Texture.FromFile() Texture.FromStream()), Sprite. . Point Vector2 , , , , draw. , , IO , .

struct Tile
{
    public Point Location;
    public Texture Texture;
}

Device device;
Sprite sprite;
List<Tile> tiles = new List<Tile>();

.ctor() {
    this.device = new Device(...)
    this.sprite = new Sprite(this.device);
}

void Draw() {
    this.device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);
    this.device.BeginScene();
    this.sprite.Begin(SpriteFlags.AlphaBlend);
    foreach (Tile tile in this.tiles) {
        this.sprite.Draw2D(tile.Texture, 
            new Point(0, 0), 0.0f, tile.Location, Color.White);
    }
    this.sprite.End();
    this.device.EndScene();
    this.device.Present();
}
+2

Source: https://habr.com/ru/post/1760846/


All Articles