www.xbdev.net xbdev - software development
Monday December 10, 2018
home | about | contact | Donations

     
 

XBOX Programming..

More than just a toy...

 

Prt5 - "Hello World"

Author: Ben_3D@xbdev.net

 

Well every new programmer knows of hello world - its tradition - you can't go against tradition :)  So when you compile this little piece of code and run it, you'll get a small piece of text (green text) in top left of the screen saying "Hello World"..

So to start off, the initilisation, as before, nothing much has changed.  The only difference is the added header files "#include <xfont.h>, #include <stdio.h>".

 

//Main header file for the XDK

#include <xtl.h>

 

//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

#include <xfont.h> //used for directX textout

#include <stdio.h> //used for swprintf

//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

 

LPDIRECT3D8 g_pD3D = NULL;                      // DirectX Object

LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;          // Screen Object

 

void InitialiseD3D()

{

    //First of all, create the main D3D object. If it is created successfully we

    //should get a pointer to an IDirect3D8 interface.

    g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);

 

    //Create a structure to hold the settings for our device

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));

 

    //Fill the structure.

    // Set fullscreen 640x480x32 mode

 

      d3dpp.BackBufferWidth = 640;

      d3dpp.BackBufferHeight = 480;

      d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;

 

 

      // Create one backbuffer and a zbuffer

      d3dpp.BackBufferCount = 1;

 

      // Set up how the backbuffer is "presented" to the frontbuffer each time

      d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

 

    //Create a Direct3D device.

    g_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL,

                                   D3DCREATE_HARDWARE_VERTEXPROCESSING,

                                                   &d3dpp, &g_pD3DDevice);

 

    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

}

 

 

 

void CleanUp()

{

    g_pD3DDevice->Release();

    g_pD3D->Release();

}

 

And for the main part of the program, the entry point - we have:

 

//Application entry point

void __cdecl main()

{

      InitialiseD3D();

      while(true)

      {

            //Clear the backbuffer to black

            g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);

            //Begin the scene

            g_pD3DDevice->BeginScene();

 

 

                  //NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

                  DisplayText();

 

            //End the scene

            g_pD3DDevice->EndScene();

 

            //Filp the back and front buffers so that whatever has been rendered on the back buffer

            //will now be visible on screen (front buffer).

 

            g_pD3DDevice->Present(NULL, NULL, NULL, NULL);

      }    

      CleanUp();

}

 

Now for the part of code which works its magic, and loads in the bitmap font from file, and displays our wonderful wonderful exciting text :)

 

void DisplayText()

{

      //Create some DirectX text buffers

      XFONT*      m_pArial18BitmapFont;    // Pointer to the Arial18Normal Bitmap font   

      LPDIRECT3DSURFACE8 g_pFrontBuffer;

 

    //InitialiseFonts

    g_pD3DDevice->GetBackBuffer(-1,D3DBACKBUFFER_TYPE_MONO,&g_pFrontBuffer);

      DWORD dwFontCacheSize = 16 * 1024;

 

      //Load our font in - have to sepcify its loacation

      XFONT_OpenBitmapFont( L"D:\\Arial18Normal.bmf",

                           dwFontCacheSize, &m_pArial18BitmapFont );

 

      WCHAR szbuff[200] = {0};

      swprintf(szbuff, L"Hello World");

      //Top left corner of where we want to draw our text.

      float xpos = 100.0f;

      float ypos = 100.0f;

 

      //Display our text.

      m_pArial18BitmapFont->SetTextColor(D3DCOLOR_XRGB(30,255,20));

 

      m_pArial18BitmapFont->TextOut( g_pFrontBuffer, szbuff, -1, (long)xpos, (long)ypos );

 

      //Release our TextBuffers

      m_pArial18BitmapFont->Release();

      g_pFrontBuffer->Release();

 

}

 

I didn't write this piece of code for optimisation!  Every time you call it, it is creating and destroying the font buffer.  But it is exceptionally easy to understand I think, that is why I wrote it this way.  You should be able to understand the jist of what is happening.  In time this will be made into a class, or the XFONT* buffer could be put in the directX initilisation code.

Don't forget to put the file "Arial18Normal.bmf" in your directory with the .xbe, or else it won't do anything, and will probably just crash.

 

 

Developers Note:

Using the command prompt.... yup I hear you..."the dos prompt!!!".
Yup, the only way is using the dos prompt I'm afraid.

Well get a ttf font (true type font), e.g. times.ttf.

goto the prompt and type:

"MakeFont -d E:\fonts\ times.rsp times.bmf"


The -d E:\fonts\ specifies the directory being used

The times.rsp is a text document, open it in notepad to see what is happening.

Finally the times.bmf is the output file :)

 

(Back Tutorials Page)

 

 

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.