Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I kind of need help understanding how rendering graphics works in C#. I have a java background and I've made a basic game engine with custom rendering that I use for all of my projects. After trying to adopt some principles from Java of making a game loop in C#, I've pretty much hit a dead end and need some help figuring out why the form window won't show up.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Media; using System.Linq; using System.Text; using System.Windows.Forms;

namespace GameEngine { public partial class CoreEngine : Form { public Timer timer1 = new Timer();

    public CoreEngine()
    {
        InitializeComponent();
        this.Size = new Size(700, 500);
        this.StartPosition = FormStartPosition.CenterScreen;
        this.BackColor = Color.Black;
        this.DoubleBuffered = true;
        this.Text = "GameEngine in C#";
        //timer1.Tick += new EventHandler(update);
        //timer1.Interval = 1000 / 60;
        //timer1.Start();
        loop();
    }

    public void loop()
    {
        while (true)
        {
            update();
            this.Refresh();
        }
    }

    public void update()
    {
        //this.Refresh();
        xLoc += xVel;
        yLoc += yVel;

        if (xLoc > this.Size.Width - 65)
        {
            xVel *= -1;
        }
        else if (xLoc < 0)
        {
            xVel *= -1;
        }
        if (yLoc > this.Size.Height - 95)
        {
            yVel *= -1;
        }
        else if (yLoc < 0)
        {
            yVel *= -1;
        }
        Console.WriteLine("{0} and {1}", xLoc, yLoc);
    }

    public int xLoc = 50, yLoc = 50, xVel = 5, yVel = 5;

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawString("Hello, World", new Font("Arial", 24), new SolidBrush(Color.White), 300, 300);
        g.FillRectangle(new SolidBrush(Color.Purple), xLoc, yLoc, 50, 50);
    }
}

}

share|improve this question
2  
How to fix your code is too localized a question for the site. Start with a working example of a C# game loop, then move on from there. A Google search should help you find working examples and tutorials to get you started. See the FAQ about how to use this site. – Byte56 May 2 at 14:41
1  
you are missing this->Show, i assume. you are not telling the window to showup. – Tordin May 2 at 14:41
I have a very specific way of wanting to implement this. It doesn't seem like C# gives you a way to render without calling Refresh or Invalidate. Also I want to implement a fixed time step loop without the need of any timers. The problem is just that I can't render anything without the need of using a Timer to run my update method. – Ben May 2 at 14:46
Oh I see, the Show method worked. Thank you Tordin. – Ben May 2 at 14:47
1  
If you're planning to make a game, I'd highly recommend using XNA over GDI--. GDI works for small things, but they've removed the hardware acceleration and it can be very slow. – John McDonald May 2 at 14:55

closed as too localized by Byte56, John McDonald, Nate, Josh Petrie, Patrick Hughes May 2 at 15:46

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.