Sign in to follow this  
cjsst55

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

Recommended Posts

cjsst55    122
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
mohamed adel    174
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
capn_midnight    1707
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
cjsst55    122
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
DaWanderer    538
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
capn_midnight    1707
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  

  • Similar Content

    • By codeliftsleep
      I'm building an American football simulation(think football manager), and am wondering about the best way of implementing AI based on various inputs which are weighted based on the personality of the NPC...I have a version of Raymond Cattell's16PF model built into the game to be able to use their various personality traits to help guide decisions.
      I am going to use this extensively so I need this to be both flexible and able to handle many different scenarios.
      For instance, a GM has to be able to decide whether he wants to resign a veteran player for big dollars or try and replace them through the draft. They need to be able to have a coherent system for not only making a decision in a vacuum as a single decision but also making a decision as part of a "plan" as to how to build the team...For instance it makes no sense for a GM to make decisions that don't align with each other in terms of the big picture. I want to be able to have the decisions take in a wide range of variables/personality traits to come up with a decision. 
       
      There is no NPC per se...There isn't going to be any animations connected to this, no shooting, following, etc...just decisions which trigger actions. In a situation like a draft, there is one team "on the clock" and 31 other teams behind the scenes working on trying to decide if they want to try and trade up, trade down, etc which can change based on things like who just got picked, the drop off between the highest graded player at their position/group and the next highest graded player in that position/next group, if a player lasts past a certain point, etc...
      There needs to be all of these things going on simultaneous for all the teams, obviously the team on the clock is goifn to have to decide whether it wants to make a pick or take any of the offers to move down in the draft from other teams that might want to move up, etc..
      So I am planning on making use of something called Behavior Bricks by Padaone Games( bb.padaonegames.com )which is a Behavior Tree but in conversations with others who have worked on AI in major projects like this(EA sports) they said to combine this with a State Machine.
      My question is would I be able to do this using just Behavior Bricks or would I need to build the state machine separately?  Is there something else already created for this type of purpose that I could take advantage of?
    • By khawk
      Unity has announced that support for Xbox One X is now available. This means developers can now deploy their projects to any member of the XBox One family: Xbox One, Xbox One S, and Xbox One X. Unity developers can now take advantage of the increased power and 4K HDR output.
      From their blog announcement:
       

      View full story
    • By khawk
      Unity has announced that support for Xbox One X is now available. This means developers can now deploy their projects to any member of the XBox One family: Xbox One, Xbox One S, and Xbox One X. Unity developers can now take advantage of the increased power and 4K HDR output.
      From their blog announcement:
       
    • By Liquid1Phantom
      Hello, my name is Thomas and I am currently starting up a project. 
      The project is going to be a very hardcore, and tactical FPS that is similar to Escape from Tarkov. It will be made within Unity, and will be programmed in C#. Currently, I have a Unity project started, an organized Discord setup, and as well as a very organized Google Drive. The Google Drive has art references, and plenty of folders for organization.
      I am looking for anyone who can contribute: 2D artists, 3D artists, composers, programmers, etc.
      If you would like to help out and be of use, please email me at: thomasmunson2277@gmail.com
      Alternatively, add my Discord: Thomas#3788
    • By ilovegames
      Crowds of alien robots fill the streets of a small town in the US, killing everyone who crosses their path. You are the commander of an elite military unit, and your task is to repel alien attacks as long as possible. Good luck!
      Download http://falcoware.com/RobotsAttack.php



  • Popular Now