Difference between revisions of "Lesson 1. Visualize me!"

From Project X Wiki
Jump to: navigation, search
(RESULT.)
 
 
Line 62: Line 62:
 
  CDebugInterface::CDebugInterface(CObject* parent)
 
  CDebugInterface::CDebugInterface(CObject* parent)
 
  {
 
  {
  super(parent);   
+
  super(parent, GS);   
 
  DebuggerGUI = GUIBitmap;     
 
  DebuggerGUI = GUIBitmap;     
 
  }
 
  }
Line 92: Line 92:
 
  void CDebugInterface::DrawBitmap()
 
  void CDebugInterface::DrawBitmap()
 
   {
 
   {
   AddInterfaceBitmap(0,0,0,DebuggerGUI,0,0,50,50,0);  
+
   AddInterfaceBitmap(0,0,0,DebuggerGUI,50,50,0);  
 
   }
 
   }
 
   
 
   
Line 112: Line 112:
 
DrawBitmap(), DrawBackground(), DrawText() are the names of fuctions and CDebugInterface:: before them tells that those functions will ONLY be called for CDebugInterface objects and it’s children classes.
 
DrawBitmap(), DrawBackground(), DrawText() are the names of fuctions and CDebugInterface:: before them tells that those functions will ONLY be called for CDebugInterface objects and it’s children classes.
  
  AddInterfaceBitmap(0,0,0,DebuggerGUI,0,0,50,50,0);
+
  AddInterfaceBitmap(0,0,0,DebuggerGUI,50,50,0);
  
 
To understand how any function works take a look at mini-help or wiki.
 
To understand how any function works take a look at mini-help or wiki.
  
  void CObject::AddInterfaceBitmap(int unk,fixed x,fixed y,CBitmap* bitmap,int srcX,int srcY,int sx,int sy,int flags)
+
  void CObject::AddInterfaceBitmap(int unk,fixed x,fixed y,CBitmap* bitmap,int sx,int sy,int flags)
  
 
[[File:InterfaceTutorialPicture5.png ]]
 
[[File:InterfaceTutorialPicture5.png ]]
Line 204: Line 204:
 
  CDebugInterface::CDebugInterface(CObject* parent)
 
  CDebugInterface::CDebugInterface(CObject* parent)
 
   {
 
   {
     super(parent);   
+
     super(parent, GS);   
 
     DebuggerGUI = GUIBitmap;     
 
     DebuggerGUI = GUIBitmap;     
 
   }
 
   }
Line 210: Line 210:
 
  void CDebugInterface::DrawBitmap()
 
  void CDebugInterface::DrawBitmap()
 
   {
 
   {
     AddInterfaceBitmap(0,0,0,DebuggerGUI,0,0,50,50,0);  
+
     AddInterfaceBitmap(0,0,0,DebuggerGUI,50,50,0);  
 
   }
 
   }
 
   
 
   

Latest revision as of 15:57, 15 January 2012

In this lesson you will learn how to create simple interface for the game, paint it and add some text.<br\> The result of the lesson is a new interface element, which is always in the center of your screen, with the text “MyDebug”.<br\> Lesson is devided into logical parts. Every part is a "Piece of code" and explanation. Good luck!


InterfaceTutorialPicture4.png

PART 1.

Let’s create a Library.<br\> Name: “DebugInterface”<br\> Scriptname: “DebugInterface”<br\>


InterfaceTutorialPicture1.png


require utils;
CDebugInterface : CObject;
CBitmap* GUIBitmap;


1. This library will be useful to capture target worm.(GetCurrentWorm()).<br\> 2. We declared that CDebugInterface will be CObject’s child class. <br\> 3. We declared that GUIBitmap will be a pointer to CBitmap object. GUIBitmap is just a name for variable. CDebugInterface is also the name for the class so you can change it if you want.<br\>


PART 2.

void DebugInterface::InitGraphic()
{ 
     GUIBitmap = MakeBitmap(50,50);
}

void DebugInterface::FirstFrame()
{
     new CDebugInterface(Root);
}

InitGraphic() and FirstFrame() are special functions called, obviously, on graphic initialization and on the first moment of the game.<br\> This constructions allow you to catch those moments and call any functions you want:

YourScriptname::InitGraphic()
{
     // do here what you want
} 
YourScriptname::FirstFrame()
{
     // do here what you want
}

So, on InitGraphic() we have created a CBitmap32 object and cought it with GUIBitmap variable.<br\> On FirstFrame() we have created a new CDebugInterface object and used Root as a parent.

IMPORTANT: Root is not available on InitGraphic() so use FirstFrame() instead.

PART 3.

CDebugInterface::CDebugInterface(CObject* parent)
{
super(parent, GS);   
DebuggerGUI = GUIBitmap;    
}


CDebugInterface::CDebugInterface(CObject* parent)

It’s our class constructor. It takes only 1 argument – parent. Our CDebugInterface is still ALMOST a CObject.<br\> To make it be full CObject let’s take a look at CObject constructor:

CObject::CObject(CObject* Parent)

It also takes parent. That’s why we use super(parent). It calls CObject constructor inside our CDebugInterface class,<br\> so “parent” goes from

CDebugInterface::CDebugInterface(CObject* parent) 

and now it is really a full CObject. If you create new classes from CObject, CGObject and etc. calling super() in the constructor will prevent a lot of bugs, crashes, etc.<br\>

DebuggerGUI = GUIBitmap;

Our class gets DebuggerGUI field and takes GUIBitmap as a meaning.


PART 4.

void CDebugInterface::DrawBitmap()
 {
  AddInterfaceBitmap(0,0,0,DebuggerGUI,50,50,0); 
 }

void CDebugInterface::DrawBackground()
 {
  DebuggerGUI->Rect(0,0,50,50,ARGB(255,0,0,0));
 }

void CDebugInterface::DrawText()
 {           		 
  local a = 1;
  local b = 1;
  GS->Disp->DrawText(1,DebuggerGUI, 2, 2, "MyDebug", &a, &b);	
 }


DrawBitmap(), DrawBackground(), DrawText() are the names of fuctions and CDebugInterface:: before them tells that those functions will ONLY be called for CDebugInterface objects and it’s children classes.

AddInterfaceBitmap(0,0,0,DebuggerGUI,50,50,0);

To understand how any function works take a look at mini-help or wiki.

void CObject::AddInterfaceBitmap(int unk,fixed x,fixed y,CBitmap* bitmap,int sx,int sy,int flags)

InterfaceTutorialPicture5.png

For fully newbies:<br\> void – tells that this functions returns nothing.<br\> CObject:: - method's class name.<br\> AddInterfaceBitmap - function name.<br\> unk – unknown argument, maybe layer.<br\> x, y – Position X and Position Y<br\> bitmap – a pointer to CBitmap object.<br\>

Basically we have added our DebuggerGUI on the screen, as x and y are 0 it will be always in the center of the screen. The Bitmap usually born without any colors, so it’s time to paint it.


void CBitmap::Rect(int left,int top,int right,int bottom,int clr)

InterfaceTutorialPicture2.png

Paints a rectangular on our bitmap.

The picture above illustrates how the Bitmap is painted with a Rectangular according to the arguments.<br\> Clr is the Color. Use ARGB Fuction to get the right interger<br\> ARGB(a,r,g,b);<br\> A – ALPHA (opacity)<br\> R – RED<br\> G – GREEN<br\> B - BLUE<br\>

DebuggerGUI->Rect(0,0,50,50,ARGB(255,0,0,0));

It fills our DebuggerGUI with black color(actually paints a black rectangular).<br\> Now it’s time to add some text.<br\>

int CDisplay::DrawText(int fnt,CBitmap* bitmap,int x,int y,string txt,int* outX,int* outY)

InterfaceTutorialPicture3.png

This function retuns integer.<br\> CDisplay is method’s class. By the way, CDisplay is created by the game in GS, and called Disp. So don’t create it. <br\> Drawtext – name of the function.<br\> FNT is the number of the Font.<br\> BITMAP is for the name of the bitmap you want to place text in. <br\> TXT is the text. <br\> Use &a and &b as outX and outY <br\>

local a = 1; local b = 1; GS->Disp->DrawText(1,DebuggerGUI, 2, 2, "MyDebug", &a, &b);

PART 5.

Finally it’s time to manage calling of this methods. We will use message because all of our functions must be called on every frame or drawqueue.<br\>

void CDebugInterface::Message(CObject* sender,EMType Type,int MSize,CMessageData* MData)
 {
  if (Type == M_FRAME)
   {      
   DrawBackground();
   DrawText();
   }

  if (Type == M_DRAWQUEUE)
   {
   DrawBitmap();
   }
 }


RESULT.

So finally your code should look like:

require Utils;
CDebugInterface : CObject;
CBitmap* GUIBitmap;
 
void DebugInterface::InitGraphic()
  { 
   GUIBitmap = MakeBitmap(50,50);
  }

void DebugInterface::FirstFrame()
  {
   new CDebugInterface(Root);
  }

CDebugInterface::CDebugInterface(CObject* parent)
  {
   super(parent, GS);   
   DebuggerGUI = GUIBitmap;    
  }

void CDebugInterface::DrawBitmap()
  {
   AddInterfaceBitmap(0,0,0,DebuggerGUI,50,50,0); 
  }

void CDebugInterface::DrawBackground()
  {
   DebuggerGUI->Rect(0,0,50,50,ARGB(255,0,0,0));
  }

void CDebugInterface::DrawText()
  {           		 
   local a = 1;
   local b = 1;
   GS->Disp->DrawText(1,DebuggerGUI, 2, 2, "MyDebug", &a, &b);	
  }

void CDebugInterface::Message(CObject* sender,EMType Type,int MSize,CMessageData* MData)
  {
   if (Type == M_FRAME)
     {      
      DrawBackground();
      DrawText();
     }

   if (Type == M_DRAWQUEUE)
     {
      DrawBitmap();
     }
 }

To The Next Chapter Lesson 2. Place me!