Create games with the AllegNet library for .NET 2.0






3.32/5 (9 votes)
Dec 9, 2005
2 min read

52993

1745
An article on how to use the AllegNet library to write games with managed languages for the .NET 2.0 framework.
- Download DLLs - 520 Kb
- Download AllegNet library - 32.2 Kb
- Download binary and source (C#) - 31.5 Kb
- Download binary and source (VB.NET) - 28.8 Kb
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.