0

I am trying to make a retro game engine in C#. I want to use a resolution of 320x200, but the screen does not natively support that, so I'm trying to decide what is the most efficient method of emulating that. Do I create a Bitmap object and then use SetPixel and create methods for drawing basic shapes? Then scale the image to the size of the screen. Should I draw little Rectangle objects instead to mimic the pixels? What do you think would be the most efficient? Also any other ideas?

3
  • GDI+ is too slow for game rendering, even with a small resolution. I've heard good things about SFML, and it has C# bindings, maybe look at that? Apr 8, 2014 at 22:48
  • GDI+ is slow only if you don't know how to use it properly. This resolution should be easy to handle with GDI+, since performance is generally proportional to the number of pixels drawn. Apr 18, 2014 at 17:18
  • Note that DOS 320x200 is actually seen on an aspect ratio of 4:3, meaning its pixels were elongated, and all graphics made for such resolutions are vertically flattened to compensate for that. Visually, this should really be considered to be 320x240.
    – Nyerguds
    Jan 10, 2018 at 14:21

2 Answers 2

0

You could just use the DrawImage of the Graphics object to paint your 320x200 bitmap on a rectangle of any size.

By setting the interpolation mode on the graphics object first, you can control the way the image is painted when resized. Different interpolation modes should give different visual results and chances are you will be satisfied with one of built-in modes so that you don't have to provide any custom implementation of the streching algorithm.

On the other hand, have you considered OpenGL/DirectX rather than GDI+?

0

If you must use GDI+ then let it handle the scaling using this method can leverage hardware acceleration if available as opposed to drawing into bitmaps. But agreed with other poster there are better frameworks for this have a look at XNA.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        var gameTick = new Timer {Interval = 10};
        gameTick.Tick += (s, e) => BeginInvoke((Action)(Invalidate));
        gameTick.Start();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        var g = e.Graphics;

        //calculate the scale ratio to fit a 320x200 box in the form
        var width = g.VisibleClipBounds.Width;
        var height = g.VisibleClipBounds.Height;
        var widthRatio = width / 320f;
        var heightRatio = height / 200f;
        var scaleRatio = Math.Min(widthRatio, heightRatio);
        g.ScaleTransform(scaleRatio, scaleRatio);

        //draw a 320x200 rectangle (because of scale transform this always fills form)
        g.FillRectangle(Brushes.Gray, 0, 0, 320, 200);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.