[.net] Simple C# Question Involving Game Loops

Started by
5 comments, last by thk123 15 years, 10 months ago
This is a really stupid question, but I have been searching the internet for a tutorial that explains this and I haven't found one (I don't believe me either!) Basically, I have drawn a image on a screen using

private void f1_paint(object sender, PaintEventArgs e)
        {
        Graphics g = e.Graphics;
        Image myPic = new Bitmap("character.bmp");
        Point Pos = new Point(50, 50);
        String myString = Pos.X.ToString();
        g.DrawImage(myPic, Pos);

        }
 
Now I want to move it. The first thing I tried was enclosing everything in a while(true) loop, however, this simply causes my system to crash. So how do I get it to do something, say, 20 times a second and not use every available byte to do it. I saw the thing on Coding4Fun, but I am unsure how you are meant to apply it. I appreciate this is a really stupid question, but it has been holding me back for 3 days now and I have always found it is the simple things that hold me back when programming, Once you have done them, the complicated things follow on. Thanks in advance.
-thk123botworkstudio.blogspot.com - Shamelessly advertising my new developers blog ^^
Advertisement
What you could do is setting up a timer ever 1/30th of a second or so and call Invalidate() on your form in the timer callback, which will cause the window to repaint.

By the way, the bitmap is now read from disk every time your window is redrawn. It's better to make myPic a member of the class and have it load in say, the load handler of your form.
Quote:Original post by Sijmen

By the way, the bitmap is now read from disk every time your window is redrawn. It's better to make myPic a member of the class and have it load in say, the load handler of your form.


No kidding. Where images and any other major data hogs are concerned you want to load them as few times as possible for a number of reasons. One is its real slow. If your image was large enough it could take even a few seconds to load. If it takes 1/8 of a second to load an image you can't possibly reach 1/60th frame rates. So even for simple testing applications one NEEDS to be in the habit of loading such resources only once.

The second reason is even if it didn't slow things down you would be working your hard drive reeeeal hard. Such grinding could/would probably shorten its lifespan.


OT what exactly are you doing to move the image? In this example your location is also being created every time you paint at 50,50. The class will probably need to have your location created as a member(like the image) then updated along with a timer. That way your location isn't being reset every time the screen draws.
------------------------------------------------------------- neglected projects Lore and The KeepersRandom artwork
Read this post. It describes how to use the application idle event to drive your game loop. This is the most appropriate way to acheive a game loop for a GDI+ game. However, you should be aware that GDI+ has pretty severe performance limitations. If you are planning to build something that is graphically intensive you should be using another technology such as XNA.
GDI+? Is that what C# renders in? Very valid point about loading the image countless times. I think I managed to get that to work, however, the image still appears to flicker as it moves across the screen. Is this normal? Is it only noticeable because it has a white BackGround.

Thankyou for telling me the invalidate command, I don't know why my searches did not reveal that, I really appreciate your help, you have saved me!
-thk123botworkstudio.blogspot.com - Shamelessly advertising my new developers blog ^^
Quote:Original post by thk123
GDI+? Is that what C# renders in? Very valid point about loading the image countless times. I think I managed to get that to work, however, the image still appears to flicker as it moves across the screen. Is this normal? Is it only noticeable because it has a white BackGround.

Thankyou for telling me the invalidate command, I don't know why my searches did not reveal that, I really appreciate your help, you have saved me!


GDI+ is more or less the basic underlaying graphics system. Its not really a C# thing as it is invoked by any language using basic windows graphic calls. The flickering could something of a side effect of using GDI as it not really a high performance rendering tool. Its fine for normal use but gaming or any other high speed graphics applications are going to be tough. You may find you can elimnate the flicker one way or another but it may take more work and craftiness than just using a high speed graphics API such as DirectX or OpenGL. There are several good graphic engines and APIs that will make use of better methods for you and let you have better results faster and with less trouble. For those you might check out the "alternative game libraries" forum on this website for a list and help using those. For C# XNA is going to get pointed out a lot, but its not your only option.

Also if you are still kinda "green" on basic graphical drawing techniques such as blitting, animation, sprites, tiles, meshes, textures, graphic buffers, etc, then a "intro to" book might be a sound investment.
------------------------------------------------------------- neglected projects Lore and The KeepersRandom artwork
Thanks, hopefully when my new computer turns up I should be able to run XNA (currently, I don't have one of those 1.1 vertex shaders) A book in to this sounds like a very good idea, do you recommend any? If not, I will have a quick search in my local library.
-thk123botworkstudio.blogspot.com - Shamelessly advertising my new developers blog ^^

This topic is closed to new replies.

Advertisement