Sign in to follow this  
cjsst55

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

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

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  

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
      627668
    • Total Posts
      2978541
  • Similar Content

    • By Kurupted Playz
      Hey, I'm looking for some people that can help me with a game that I'm making that is a 3d platformer.
      I mostly need someone for character design and animation,  I can do some coding but if you can do coding as well you're still welcome because I'm still not the greatest at coding just yet.
      if you need to contact me add me on discord @ Kurupted#1206
       
    • By Jcyshadow97
      Hi,guys.I m working on a Fantasy RPG.Currently i m work alone on the project.I really need someone can make UI stuff.If someone can handle it please feel free to contact me on email: 270514974@libero.it.
      Thank you guys and sorry for my english.
       
       



    • By STRATUM the Game
      Hey, everyone! This is my first post here.
      I would like to know what you think about my project called STRATUM. It's a 2D platformer that is heavily based on storytelling and boss fighting while trekking through the world.

      Everything in STRATUM takes place in the first century AD, in a world that wraps our own universe, called  The Stratum. A parallel Universe that is the home of the Christian deities . In this game you will play as a Dacian warrior, unfamiliar with everything in this world, you’ll get to know and understand The Stratum together with him.
      The main thing that I want with STRATUM is to reinvent the known lore and history of the Christian deities and realms. 
      The story is unconventional, it plays down a lot of the mysticism of Hell or Heaven and it gives it a more rational view while keeping the fantastic in it. What do I mean by that? Well, think about Hell. What do you know about it? It's a bad place where bad people go, right? Well, that's not the case in STRATUM. I don't want to describe such a world. In STRATUM, there is a reason for everything, especially for the way Hell is what it is in the game. "Hell" is called The Black Stratum in the game.
      This world is not very different from Earth, but it is governed by different natural laws.
      The story will also involve the reason why this world entered in touch with ours.

       
      What do you think about all that I said? Would you be interested in such a game? I have to say that everything is just a work of fiction made with my imagination. I do not want to offend anyone's beliefs.
      I want this to be a one man game. I have been working alone on it (this was my decision from the beginning) from art to effects to programming to music to sound effects to everything.
      I also have a youtube video HERE if you want to see the way the game moves and the way my music sounds.
      Please, any kind of feedback will be highly appreciated. If you have something bad to say, do it, don't keep it for yourself only. I want to hear anything that you don't like about my project.
       
    • By LimeJuice
      Hi, it's my first post on this forum and I would like to share the game I am working on at the moment.
      Graphics have been made with Blender3D using Cycle as a renderer and I am using Unity3D. It's a 2D game, one touch side-scrolling game for iOS and Android.
      Here some pictures, and you can have a look to the gameplay on this video :
      Feedbacks ?
      And if you want to try it, send me your email and I will add you to the beta tester list!
       
       








    • By Kirill Kot
      An adventure indie game with quests in a beautiful, bright world. Characters with unique traits, goals, and benefits. Active gameplay will appeal to players found of interactivity, especially lovers of quests and investigations.
      Available on:
      Gameroom (just open the web page and relax)
      AppStore
      GooglePlay
      WindowsPhone

  • Popular Now