Lesson 2. Place me!

From Project X Wiki
Revision as of 06:54, 8 October 2010 by Nitreo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Hi. In the "Lesson 2. Place me!" you will see an easy way to adjust the position of your interface according to player's resolution. You will also add first debugging parameters and understand the usability of debugging. The result is nicely adjusted inteface, which looks perfect for any resolution, diplays current one and your debug interface position.
Lesson is devided into logical parts. Every part is a "Piece of code" and an explanation. Good luck!

InterfaceTutorialPicture2.2.png


PART 1.

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

Find that piece of code and change it a little:

void CDebugInterface::DrawText()
   {      
    local a = 1;
    local b = 1;
    GS->Disp->DrawText(1,DebuggerGUI, 2, 2, ItoA(GS->Disp->xRes), &a, &b);	
    GS->Disp->DrawText(1,DebuggerGUI, 2, 12, ItoA(GS->Disp->yRes), &a, &b);
   }

xRes and yRes allow you to catch current resolution of the screen. GS->Disp is CGameState->Disp and this is where xRes and yRes stored.

string ItoA(int x);

This function converts integer x to the string. So we converted resolution parameters and sent it to our interface. Here is the result:

InterfaceTutorialPicture2.3.png


PART 2.

Now it's math's time! A bit of theory:

InterfaceTutorialPicture2.1.png

I'll prevent you from calculating the best position for the interface. It's 35 pixels from the border of the screen. I offer you to place the interface in the left top corner, so it will be 35 pixels from top and left. The picture above illustrates how you can calculate the position for the interface for any resolution. Create a new function and call it CalculatePosition():

void CDebugInterface::CalculatePosition()
   {
    PositionX = 0 - GS->Disp->xRes / 2 + 35;
    PositionY = 0 - GS->Disp->yRes / 2 + 35;
   }

We don't want it calculate the position more that once so let's just place it on first frame. But as soon as we are going to call any function for our CDebugInterface object outside of "CDebugInterface::", we should give it a name. Let it be local name DeIn:

void DebugInterface::FirstFrame()
   {
    local DeIn = new CDebugInterface(Root);
    DeIn->CalculatePosition();
   }

Finally we should send our Pos.X and Pos.Y to the bitmap:

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

Done. The result seems to be fine:
InterfaceTutorialPicture2.4.png


PART 3.

It's not hard to find out that for 640x480 resolution Pos.X and Pos.Y of our debug interface will be -285,-205. Lets make sure everything is right. Find our DrawText() Function and add a few lines:

void CDebugInterface::DrawText()
   {                     
   local a = 1;
   local b = 1;
   GS->Disp->DrawText(1,DebuggerGUI, 2, 2, ItoA(GS->Disp->xRes), &a, &b);    
   GS->Disp->DrawText(1,DebuggerGUI, 2, 12, ItoA(GS->Disp->yRes), &a, &b);    
   GS->Disp->DrawText(1,DebuggerGUI, 2, 22, ItoA(PositionX), &a, &b);    
   GS->Disp->DrawText(1,DebuggerGUI, 2, 32, ItoA(PositionY), &a, &b);       
   }

As you can see, we made our interface show 2 more parameters: PositionX and Y of our bitmap. And here is the result:

InterfaceTutorialPicture2.2.png

RESULT.

Finally, your code should look like this:

require Utils;

CDebugInterface : CObject;
CBitmap* GUIBitmap;

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

void DebugInterface::FirstFrame()
   {
   local DeIn = new CDebugInterface(Root);
   DeIn->CalculatePosition();
   }

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

void CDebugInterface::CalculatePosition()
   {
   PositionX = 0 - GS->Disp->xRes / 2 + 35;
   PositionY = 0 - GS->Disp->yRes / 2 + 35;
   }

void CDebugInterface::DrawBitmap()
   {
   AddInterfaceBitmap(0,PositionX ,PositionY,DebuggerGUI,0,0,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, ItoA(GS->Disp->xRes), &a, &b);	
   GS->Disp->DrawText(1,DebuggerGUI, 2, 12, ItoA(GS->Disp->yRes), &a, &b);	
   GS->Disp->DrawText(1,DebuggerGUI, 2, 22, ItoA(PositionX), &a, &b);	
   GS->Disp->DrawText(1,DebuggerGUI, 2, 32, ItoA(PositionY), &a, &b);	
   }

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