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);
}
}
}