Click here to Skip to main content
15,881,852 members
Articles / Artificial Intelligence

Rotating Sprite Objects on DirectDraw Wrapper for C#

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
9 Dec 2004CPOL4 min read 83.8K   637   25   2
Evaluate C# on delivering more than 1000 animated sprites on fullscreen 1280x1024.

Sample Image

Introduction

This project's primary objective is to observe C#'s behavior, when delivering hundreds of vision enabled objects, to be drawn by the DirectDraw wrapper (DDW) for Managed Code.

If primary objective is achieved and C#/DDW evaluates to be a solid project foundry working environment, the project's direction goes to creating a game-like tile world, with hundreds of visible standing and moving objects.

Using the code

By design, all methods should return null on success, or in case of failure, the exception generated within.

Here is a list of class descriptions.

  • System.Windows.Forms.Form Form1

    Holds user window, translates user input to engine.

  • CWorld

    A world coordinated system equal to the screen resolution 1280x1024.

  • CEngine

    Maintains state on user requests, units initialization, main cycle while updating and drawing units and text.

  • CDevice

    Encapsulates DDW initialization. Display mode is 1280x1024x16 (Width x Height x Depth).

  • CSprite

    Sprite description, total frames on x and y and respective size, or in case of a single image within the bitmap, 1x1 obviously.

  • CSurface

    Holds space for front and back buffers, and bitmaps (CSprite). Respective description and color keying.

  • CFrameRate

    Count number of frames delivered each second.

  • CUnit

    Basic unit information including class pointers CSprite, CUnitManager and States (ACTION_BASIC enum).

As states may accumulate (in future use), unit might want to STAND but also ROTATE_LEFT. I decided the best approach was to use a BitArray with size equal to number of states in the ACTION_BASIC enum.

C#
public enum ACTION_BASIC
{ 
    STOPPED=0,
    STOPPING,
    ROTATE_LEFT,
    ROTATE_RIGTH,
    MOVE_FORWARD,
    MOVE_BACKWARD,
    RUN_FORWARD,
    RUN_BACKWARD,
    UNABLE_TO_MOVE,
    SLIDE_LEFT,
    SLIDE_RIGTH,
}
public BitArray m_ACTION_BASIC = new BitArray(new bool[11] 
  {true,false,false,false,false,false,false,false,false,false,false});

Calls are made with very simple get/set methods:

C#
//
if (this.m_ACTION_BASIC.Get((int)ACTION_BASIC.ROTATE_LEFT))
    this.Angle += this.m_TurnVelocity / m_FPS;
C#
//
m_ACTION_BASIC.Set((int)ACTION_BASIC.STOPPED,true);

Most important call to DDW is Draw, an example is shown below with full explanation.

C#
s.Draw(    // Surface to write to
     new Rectangle(    // writeto: In this case the screen coordinates rectangle
         (int)m_X,
         (int)m_Y,
         m_CSprite.m_FrameWidth,
         m_CSprite.m_FrameHeigth
         ),
     m_CSprite.m_Surface.m_Surface,    // Surface to read from 
     new Rectangle(    // readfrom: In this case the sprite coordinates rectangle
         (m_Frame % m_CSprite.m_NFrameX)*m_CSprite.m_FrameWidth,
         (m_Frame / m_CSprite.m_NFrameX)*m_CSprite.m_FrameHeigth,
         m_CSprite.m_FrameWidth, 
         m_CSprite.m_FrameHeigth 
         ), 
     DrawFlags.KeySource|DrawFlags.Wait // drawing flags
     );

Easy reading, readers say. Actually it's very very easy, the first rectangle holds the surface to write to s, and coordinated values (remember screen size equals world size, so m_X represents x coordinate of unit on screen, same goes for y). The width and height comes from sprite declaration when loading.

The second rectangle holds the surface to read from, surface from CSprite in use, and coordinated values. So, as in this version, only the blue frames (as seen below) are used. The class has to hold the exact frame (m_Frame) depending on the angle it has. So based on the frame width, the exact x for the whole bitmap seen below is calculated using the formula the reader sees in last code snippet. Same goes for y.

Sample Image

Bitmap file loaded and stored as m_CSprite.

CUnitManager

Responsible for initializing units. Holds all initialized units on a linked list named UNITLIST (declaration is seen below). Delivers CEngine commands to units.

C#
public class UNITLIST
{
     private object pPrev;
     private object pNext;
     private CUnit pItem;

     public UNITLIST _PREV {get{return (UNITLIST)pPrev;}
                            set{pPrev = (UNITLIST)value;}}
     public UNITLIST _NEXT {get{return (UNITLIST)pNext;}
                            set{pNext = (UNITLIST)value;}}

     public CUnit _UNIT {get{return pItem;}set{pItem = value;}}
}

In the example method below, CEngine may use CUnitManager to draw each CUnit in surface s. The cycle starts with a header element pHead and moves down the list, down to the last element inserted, executing a Draw method on each unit.

C#
public System.Exception Draw(Microsoft.DirectX.DirectDraw.Surface s)
{
     try
     {
         UNITLIST ul = pHead;
         for (int i = 0; i < m_Count; i++)
         {
             ul._UNIT.Draw(s);
             ul = ul._NEXT;
         }
         return null;
     }
     catch(Exception e)
     {
         this.m_Trace.T("CUnitManager::Draw>"+e);
         return e;
     }
}

CUtils

This class holds 2D math actually. Each unit position is characterized as a x,y,angle VECTOR structure. These methods have small use in the present version but they will be very important when, in next version, as example: units try to move from point p1 to mouse-clicked point p2. The decision to include them in the present version demonstrates the concern about the accuracy of these calculations and gives readers time to criticize. Go nuts // thank you.

DistancePercentageFromOrigin(VECTOR vInicial,VECTOR vFinal,VECTOR vCurrent)
C#
public double DistancePercentageFromOrigin(VECTOR vInicial, 
                                  VECTOR vFinal, VECTOR vCurrent)
{
     double dInicial=Math.Sqrt(Math.Pow((vFinal.x-vInicial.x),2) + 
                     Math.Pow((vFinal.y-vInicial.y),2));
     double dCurrent=Math.Sqrt(Math.Pow((vFinal.x-vCurrent.x),2) + 
                     Math.Pow((vFinal.y-vCurrent.y),2));
     if (dInicial==0)
         return 0;
     return dCurrent*100/dInicial;
}
InternalProduct(VECTOR v1, VECTOR v2)
C#
 public double InternalProduct(VECTOR v1, VECTOR v2)
{
     return v1.x*v2.x+v1.y*v2.y;
}
Modulus(VECTOR v1)
C#
public double Modulus(VECTOR v1)
{
     return Math.Sqrt(Math.Pow(v1.x,2)+Math.Pow(v1.y,2));
}
AngleBetweenTwoVectorsWithSameOrigin(VECTOR v1, VECTOR v2)
C#
public double AngleBetweenTwoVectorsWithSameOrigin(VECTOR v1, VECTOR v2)
{
     double m1=Modulus(v1)*Modulus(v2);
     if (m1==0)
         return 0;
     double m2=InternalProduct(v1,v2)/m1;
     return Math.Acos(m2);
}
DecideLeftRigthOnAngle(VECTOR v1, double dAngle)
C#
public int DecideLeftRigthOnAngle(VECTOR v1, double dAngle)
{
     if (v1.x*-1*Math.Sin(dAngle)<=Math.Cos(dAngle)*v1.y)
         return -1;
     return 1;
}

CTrace

As this is a full-screen application and there is no IDE, thus no debug; the project includes a text file debug engine.

Points of Interest

  • (--) The transparency model on the DDW wouldn't work with a white background bitmap - so the project had to go with the black background the reader will see under the name: spr_blackdrop.png.
  • (--) The CUnitManager was a bit of a problem because the C# compiler would not accept a self referencing structure. The CUnitManager class has to hold the structure because it needs to deliver actions to each CUnit visible on screen. The first workaround was creating assessors to each variable on the structure. That did not work. In the second workaround, this issue was declaring the structure as a class. That works fine.
  • (++) Microsoft promised 98% of normal blitting with DirectDraw and C++. This project experienced a much easier way to code. C# really leverages the code learning experience and it does deliver the promise. Up to around 1000 units, turning left and right at really great speeds - a normal game viewing, using Radeon 9600 and 2.8MHz P4. Because of application/screen size limitations (no new sprite may overlap others at creation time), and also in this version, the world window equals the screen. Putting more than around 1000 units on screen will be a test in the next version.
  • (++) The reader should understand at this point where the project is going, a Real Time Strategy application.

History

  • 2004/12 0.0 Initial version with fixed world size. No Artificial Intelligence (AI) Units. Unit user commands include simple object manipulation (Add, Rotate).
  • 2005/01 0.1 Create a World file loader, and have a screen window size different than world size. New World Objects (buildings/trees). Unit AI level 1 (More Basic Body Actions) and Unit interaction (Who do I see?).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Portugal Portugal
Financial Apps addict since early programming ages;
Statistical Apps writer;
Computer gamming and graphics programmer with a 2 commercial titles: Armor PC/PPC, MidWay XBOX/PC/PPC;
Manic Basketball fan and player;
Traveller;

Comments and Discussions

 
GeneralThe rotation is an ANIMATION!!!! Pin
khilin19-Mar-10 8:55
khilin19-Mar-10 8:55 
GeneralRe: The rotation is an ANIMATION!!!! Pin
Sergio Mach4-Feb-11 16:11
Sergio Mach4-Feb-11 16:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.