Sign in to follow this  

Unity [.net] Animation in .Net- No DirectX

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am using .Net 1.1 and I am trying to do some simple animation without using DirectX. I posted a question similar to this one a while back using DirectX Old C# DirectX Post and form members were able to post some very insightful feedback. To do the animation without DirectX I am trying to use a method similar to the one outlined in the post above. Unfortunately, all I am getting is horrible flickering of the first image(ie, no animation at all). Here is a condensed version of my source code:
// Main Form 
void Game::OnPaint(PaintEventArgs *paintEvent)
{
   // Get Graphics to draw on Form
   Graphics *graphicsObject = paintEvent->Graphics;

   // Only draw animation once
   if(once == true) {
      once = false;
      dealerAnim->drawAnimationOneToThree(graphics);
   }
}
...
void DealerAnimation::drawAnimationOneToThree(Graphics *graphics)
{
   // Loop over each animation frame, and draw it
   for(int i = 0; i < NUM_IMAGES_ONE_TO_THREE; i++)
   {
      // Get current frame
      dealertFrame = framesOneToThree[i];

      // Draw frame		
      dealerFrame->Draw(graphics);

      // Redraw area
      myForm->Invalidate(Rectangle(250,0,430,180));

      // Pause
      Thread::Sleep(38);
   }
}
...
void DealerFrame::Draw(Graphics *graphics)
{
   // Draw actual dealer image
   if(dealerImage != NULL) {
      graphics->DrawImage(dealerImage, Point(250,0));
   } else {
      DisplayMSG(S"Image has not been loaded!");
   }
}

Any ideas? Is this a time I want to use DoubleBuffering? Any open source example code, or links to example code would be great. Thanks in advance, cjsst55

Share this post


Link to post
Share on other sites
The GDI is very slow to use in an animation. you should use any other library. you may like to take a look at the Intel Grphics performance primitives.
http://www.intel.com/cd/software/products/asmo-na/eng/perflib/index.htm

Share this post


Link to post
Share on other sites
Quote:
Original post by mohamed adel
The GDI is very slow to use in an animation. you should use any other library. you may like to take a look at the Intel Grphics performance primitives.
http://www.intel.com/cd/software/products/asmo-na/eng/perflib/index.htm

if all he's doing is flipping through frames in an animation, GDI+ is plenty fast. I was working on a game for a while (that I have since lost the time to work on) that was completely in GDI+ Here

Anyway, you are getting flickering because you don't have any double buffering. You need to create a new Bitmap object that is the size of your screen, create a Graphics object from that Bitmap by calling Graphics::FromImage(), and then do all your rendering to that Graphics object. Then, once per frame, you paint that Bitmap to the window's Graphics object using DrawImageUnscaled(). This must be the ONLY operation you perform on the window's Graphics object, everything else MUST be done on the Bitmap's Graphics object.

With graphics, you always want to use double buffering.

Share this post


Link to post
Share on other sites
capn_midnight,

Thanks for the timely response.

Do you think you could post the source code to your game so I could see how it works with double buffering?

Thanks,
cjsst55

Share this post


Link to post
Share on other sites
cjsst55, check out my response in this topic to see an example of double buffering. Like capn_midnight suggested, you create a Graphics object from a Bitmap, do all your drawing the the Graphics object, then flip the Bitmap to the main surface with the DrawImageUnscaled method.

Share this post


Link to post
Share on other sites
It's pretty simple. This example include double buffering as well as pretty much the fastest timer loop you can make in .NET (even the MDX team recommends using this interop method).

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Game
{
public class GameWindow : Form
{
private Bitmap backBuffer;
private Graphics gBack, gFront;
private bool running;
public GameWindow()
{
InitializeBuffers();
Application.Idle += new EventHandler(OnIdle);
running = true;
}
#region interop
//take this stuff as granted.
[StructLayout(LayoutKind.Sequential)]
public struct WinMsg
{
public IntPtr hWnd;
public Message msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(out WinMsg msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
private bool IsIdle
{
get
{
WinMsg msg;
return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
}
#endregion
//call this method anytime the frame is resized
private void InitializeBackBuffer();
{
backBuffer = new Bitmap(this.Width, this.Height);
gBack = Graphics.FromImage(backBuffer);
gFront = this.CreateGraphics();
}
protected override void OnResize(System.EventArgs e)
{
base.OnResize(e);
InitializeBackBuffer();
}
/**
* Timer callback. Updates the current
* state of the game, and makes the window paint.
*/

private void OnIdle(object sender, System.EventArgs e)
{
while(running && IsIdle)
{
int delTime = Time - lastTick;
lastTick = Time;
//update your game state, delTime is the time since the last frame
this.Paint();
}
}
private int Time
{
get
{
DateTime n = DateTime.Now;
return n.Second * 1000 + n.Millisecond;
}
}
private void Paint()
{
//do all your rendering calls with gBack
gBack.Clear(Color.Black);
//...
//blah blah blah
//...

//this must be your ONLY rendering call with gFront, or you will get flicker again.
gFront.DrawImageUnscaled(backBuffer, 0, 0);
}
}
}

Share this post


Link to post
Share on other sites

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this