Click here to Skip to main content
Email Password   helpLost your password?

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.

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

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralThe rotation is an ANIMATION!!!!
khilin
6hrs 19mins ago 
You are never rotating the actual image, you are just animating the sprite, THAT'S NOT A ROTATION!!!


Last Updated 10 Dec 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010