Click here to Skip to main content
Click here to Skip to main content

Create games with the AllegNet library for .NET 2.0

, 9 Dec 2005
Rate this:
Please Sign up or sign in to vote.
An article on how to use the AllegNet library to write games with managed languages for the .NET 2.0 framework.

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# version
//Don't forget to add
using using AllegNet;
class MainClass : API
{
    //Code here... 
}
'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# Version

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

Install_keyboard();
'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# 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 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# 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 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.

//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 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# 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 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# 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 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

Share

About the Author

Allegnet
Web Developer
France France
No Biography provided

Comments and Discussions

 
QuestionKey codes? PinmemberAnttiKoo16-Feb-06 2:53 
AnswerRe: Key codes? PinmemberAllegnet20-Feb-06 4:53 
GeneralRe: Key codes? PinmemberAnttiKoo20-Feb-06 10:29 
GeneralRe: Key codes? PinmemberAllegnet20-Feb-06 10:41 
General.NET Conventions PinmemberAnttiKoo15-Feb-06 9:13 
GeneralRe: .NET Conventions PinmemberAllegnet20-Feb-06 5:00 
GeneralRe: .NET Conventions PinmemberAllegnet2-Mar-06 21:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 9 Dec 2005
Article Copyright 2005 by Allegnet
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid