Tutorial 3 Part 1

We are now going to look at printing text onto the screen. They are many different ways to do this, I'll just explain two common ways to do it. First of all, the simplest method is to use the Text command. This is very easy to code but you cannot not change the font style or size.

Create a new project in DXC. Make sure you have a black 640 x 480 background image in bank 1. Now enter the following code into the project :

NOTE: If you do not wish to type the code below you may download or open this text file and simply copy and paste the code into your project.


//TUTORIAL 3 PART 1

//DEFINE VARIABLES

Global KEYPRESS$, QUITKEY

//DEFINE PROCEDURES
DefProc INIT_DISPLAY()

//---- START MAIN CODE ----

//CALL THE PROCEDURE TO CREATE THE SCREEN
INIT_DISPLAY()

//START LOOP - THIS LOOP WILL REPEAT UNTIL ESC IS PRESSED
Repeat

    KEYPRESS$ = "" //CLEAR THE CONTENTS OF THE VARIABLE FIRST

    KEYPRESS$ = Inkey$() //GET INPUT

    Text(320,240,KEYPRESS$) //TYPE TEXT TO THE SCREEN AT 320, 240

    Update Display

    QUITKEY = ScanCode() //GET INPUT

Until QUITKEY = 1 //PRESSING THE ESC KEY WILL QUIT

//THE INIT_DISPLAY PROCEDURE CREATES A SCREEN AND A PLANE
Procedure INIT_DISPLAY()

    Create Map 640,480,1,1 In Bank 50 //CREATE A MAP IN BANK 50
    Set Tile 0,0,1 Using Bank 50 //CREATE A TILE USING A BACKGROUND IMAGE IN BANK 1
    Create Plane 1 Using Bank 50 //CREATE PLANE 1 USING THE MAP IN BANK 50
    ReDirect GDI Output To Bank 1 //ALL GDI OUTPUT WILL BE WRITTEN TO BANK 1
    PenColour(255,0,0) //CHANGE TEXT COLOUR TO RED

EndProc

EndProg

In this tutorial we check for the escape key to quit the program, which allows us to use any other keys during the program. To do this we have to use the ScanCode() function. Every key supported by DirectInput has two scan codes; one for when it is pressed down and another code (256 higher) for when it is released. The scan code of the Escape key is 1 when it is pressed down and 257 when it is released.

If you wish to know the scan codes of other keys, take a look at my scan code reference page .

Run the program and press a key (you'll find that most keys will work, but not all). You will see the key you pressed appear on the screen in a red system font. The next key you press will replace the last one.

Its that simple! As you can see, it is only very basic text but useful for doing scores in games, etc. The important thing to note in this program is the "Redirect GDI Output" command in the INIT_DISPLAY procedure. Many people forget to put this in, the text command will not work if you do not Redirect the GDI output.

Next Page