Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / DirectX

Create games with the AllegNet library for .NET 2.0

3.32/5 (9 votes)
9 Dec 20052 min read 1   1.7K  
An article on how to use the AllegNet library to write games with managed languages for the .NET 2.0 framework.

Image 1

Image 2

Contents

Introduction

AllegNet is a game programming library based on the famous C++ Allegro library. At first, AllegNet was a wrapper, but due to the need of dealing with lots of objects inside the managed environment, I have change it by re-creating a set of classes, structures and functions in order to have full managed objects to play with.

With AllegNet, you can create games and multimedia applications with the support of DirectX, when available on a user's PC. AllegNet provides the same functions of the Allegro library 4.2.0 but with some modifications in order to bring the power of the .NET Framework 2.0.

AllegNet is a full manage library, so you can use C#, VB.NET, C++.NET and J#. All examples in the Examples section are provided for C#, VB.NET and sometimes J#.

This article will explain how to start creating multimedia applications with DirectX support using the AllegNet library.

Prerequisites

In order to install and run some code, you have to check two points:

  • The file msvcr80.dll must be present in the %windir%\system32 directory.
  • The file alleg42.dll must be present in the %windir%\system32 directory.

The two files can be found here.

Create your first AllegNet compatible program

Open Visual Studio Express, and create a new console application project. Next, add a reference to the AllegNet library. You can download a version here.

In order to have full access to the library easily, you can inherit the main class from the AllegNet.API class.

C#
//C# version
//Don't forget to add
using using AllegNet;
class MainClass : API
{
    //Code here... 
}
VB
'VB version

'Don't forget to add imports
imports AllegNet

Public Class MainClass inherits API

    'Code here... 

End Class

You have to initialize the library before using it. The Allegro_Init function can do it. You need to add keyboard events too.

C#
//C# Version

if (Allegro_init() != 0)
  return 1;

Install_keyboard();
VB
'VB version

If Allegro_init() <> 0 Then
    Return 1
End If
Install_keyboard()

Using the code

Now, you will create a function that will show a kind of gradual bar that shows the color depth of the screen.

C#
//C# Version
public static void TestColor(COLOR_DEPTH colordepth)
{
  int x;
  //You need a PALETTE object to deal with 8bits color depth
  PALETTE pal = new PALETTE();
 
            
 
  /* set the screen mode */
  API.Set_color_depth(colordepth);
VB
'VB Version
Public Shared Sub TestColor(ByVal colordepth As COLOR_DEPTH)
    Dim x As Integer Dim pal As PALETTE = New PALETTE()

    'set the screen mode  API.Set_color_depth(colordepth)

Next, you will create a fake 332 palette to simulate the graduate palette for 8bits depth. You will create a 640*480 BITMAP buffer and set the screen to 640*480 resolution and set the color depth to the color depth variable.

C#
//C# Version

BITMAP Buffer = API.Create_bitmap(SCREEN_RESOLUTIONS.SIZE_640_480);
 
if (API.Set_gfx_mode(GFX_MODES.GFX_DIRECTX_WIN, 
        SCREEN_RESOLUTIONS.SIZE_640_480) != 0)
    return;

Generate_332_palette(ref pal); Set_palette(pal);
 
API.Clear_to_color(Buffer,API.Makecol(0,0,0));
VB
'VB Version
Dim Buffer As BITMAP = _
   API.Create_bitmap(SCREEN_RESOLUTIONS.SIZE_640_480)
 
If API.Set_gfx_mode(GFX_MODES.GFX_AUTODETECT_FULLSCREEN, _
       SCREEN_RESOLUTIONS.SIZE_640_480) <> 0 Then
    Return
End If
 
Generate_332_palette(pal)
Set_palette(pal)
 
API.Clear_to_color(Buffer, API.Makecol(0, 0, 0))

Now you can draw some colored string in the screen at the left side. You can use the Textout_ex function with the buffer, and with the default font object.

VB
//C# Version

Textout_ex(Buffer, Font, ((int)colordepth).ToString() + 
           " bit color...", 0, 0,
            Makecol(255, 255, 255), -1);
 
Textout_ex(Buffer, Font, "Red", 32, 80, 
                   Makecol(255, 0, 0), -1);
Textout_ex(Buffer, Font, "Green", 32, 100, 
                   Makecol(0, 255, 0), -1);
Textout_ex(Buffer, Font, "Blue", 32, 120, 
                   Makecol(0, 0, 255), -1);
Textout_ex(Buffer, Font, "Yellow", 32, 140, 
                   Makecol(255, 255, 0), -1);
Textout_ex(Buffer, Font, "Cyan", 32, 160, 
                   Makecol(0, 255, 255), -1);
Textout_ex(Buffer, Font, "Magenta", 32, 180, 
                   Makecol(255, 0, 255), -1);
Textout_ex(Buffer, Font, "Grey", 32, 200, 
                   Makecol(128, 128, 128), -1);
VB
'VB Version
Textout_ex(Buffer, Font, _
          (CType(colordepth, Integer)).ToString() + _
          " bit color...", 0, 0, 
Makecol(255, 255, 255), -1)
 
Textout_ex(Buffer, Font, "Red", 32, 80, Makecol(255, 0, 0), -1)
Textout_ex(Buffer, Font, "Green", 32, 100, Makecol(0, 255, 0), -1)
Textout_ex(Buffer, Font, "Blue", 32, 120, Makecol(0, 0, 255), -1)
Textout_ex(Buffer, Font, "Yellow", 32, 140, Makecol(255, 255, 0), -1)
Textout_ex(Buffer, Font, "Cyan", 32, 160, _
                         Makecol(0, 255, 255), -1)
Textout_ex(Buffer, Font, "Magenta", 32, _
                         180, Makecol(255, 0, 255), -1)
Textout_ex(Buffer, Font, "Grey", 32, 200, _
                         Makecol(128, 128, 128), -1)

And then you can now draw the gradient colored bar into the buffer.

C#
//C# Version
/* or we could draw some nice smooth color gradients... */

for (x = 0; x < 256; x++)
{
  Vline(Buffer, 192 + x, 112, 176, Makecol(x, 0, 0));
  Vline(Buffer, 192 + x, 208, 272, Makecol(0, x, 0));
  Vline(Buffer, 192 + x, 304, 368, Makecol(0, 0, x));
}

Textout_centre_ex(Buffer, Font, "<press a key>", 
                      Buffer.w / 2, Buffer.h - 16,
                      Makecol(255, 255, 255), -1);
VB
'VB Version
'or we could draw some nice smooth color gradients...
For x = 0 To 256 - 1 Step x + 1
    Vline(Buffer, 192 + x, 112, 176, Makecol(x, 0, 0))
    Vline(Buffer, 192 + x, 208, 272, Makecol(0, x, 0))
    Vline(Buffer, 192 + x, 304, 368, Makecol(0, 0, x))
Next

Textout_centre_ex(Buffer, Font, 
       "<press a key>", 
       Buffer.w / 2, Buffer.h - 16, Makecol(255, 255, 255), -1)

And now, to finish, you will draw the result on the screen and wait for the user to press a key. In the main function, you will just call the function for each depth you want to test.

C#
//C# Version
Blit(Buffer,Screen,0,0,0,0,Screen.w,Screen.h);
 
Readkey();

//And in the main function public static int Main()
{
  if (Allegro_init() != 0)
    return 1;
  Install_keyboard();
 
  /* try each of the possible possible color depths... */
  TestColor(COLOR_DEPTH.BITS_8);
  TestColor(COLOR_DEPTH.BITS_15);
  TestColor(COLOR_DEPTH.BITS_16);
  TestColor(COLOR_DEPTH.BITS_24);
  TestColor(COLOR_DEPTH.BITS_32);

  return 0;
}
VB
'VB Version
Blit(Buffer, Screen, 0, 0, 0, 0, Screen.w, Screen.h)
Readkey()

'And in the main function
Public Shared Function Main() As Integer

    If Allegro_init() <> 0 Then
        Return 1
    End If
    Install_keyboard()
  
      'Try each of the possible possible color depths...
      TestColor(COLOR_DEPTH.BITS_8)
      TestColor(COLOR_DEPTH.BITS_15)
      TestColor(COLOR_DEPTH.BITS_16)
      TestColor(COLOR_DEPTH.BITS_24)
      TestColor(COLOR_DEPTH.BITS_32)
  
    Return 0
End Function

History

  • 12/01/2005 - First online version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here