Click here to Skip to main content
6,596,602 members and growing! (20,499 online)
Email Password   helpLost your password?
Multimedia » DirectX » General     Intermediate

Rotating Sprite Objects on DirectDraw Wrapper for C#

By Sérgio Machado

Evaluate C# on delivering more than 1000 animated sprites on fullscreen 1280x1024.
C#.NET 1.0, .NET 1.1, WinXP, DirectX, VS.NET2003, Dev
Posted:9 Dec 2004
Views:55,350
Bookmarked:22 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.81 Rating: 3.00 out of 5
1 vote, 25.0%
1
1 vote, 25.0%
2

3
1 vote, 25.0%
4
1 vote, 25.0%
5

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.

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:

//

if (this.m_ACTION_BASIC.Get((int)ACTION_BASIC.ROTATE_LEFT))
    this.Angle += this.m_TurnVelocity / m_FPS;
//

m_ACTION_BASIC.Set((int)ACTION_BASIC.STOPPED,true);

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

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.

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.

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)
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)
 public double InternalProduct(VECTOR v1, VECTOR v2)
{
     return v1.x*v2.x+v1.y*v2.y;
}
Modulus(VECTOR v1)
public double Modulus(VECTOR v1)
{
     return Math.Sqrt(Math.Pow(v1.x,2)+Math.Pow(v1.y,2));
}
AngleBetweenTwoVectorsWithSameOrigin(VECTOR v1, VECTOR v2)
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)
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sérgio Machado


Member
Computer Programming Addict since i can remember.

Computer Ciences Graduate since 1997.

Day Job includes developing/maintaning a HomeBanking Transactional OnLine System with 500,000 clients.

Passion/Dream Job will address all game development challenges.
Location: Portugal Portugal

Other popular DirectX articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Dec 2004
Editor: Smitha Vijayan
Copyright 2004 by Sérgio Machado
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project