使用.NET 2.0的AllegNet库创建游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/create-games-with-the-allegnet-library-for-net-2-0-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2495 个词 阅读量 0使用.NET 2.0的AllegNet库创建游戏(译文)
原文地址:https://www.codeproject.com/Articles/12259/Create-games-with-the-AllegNet-library-for-NET-2-0
原文作者:Allegnet
译文由本站 robot-v1.0 翻译
前言
An article on how to use the AllegNet library to write games with managed languages for the .NET 2.0 framework.
有关如何使用AllegNet库为.NET 2.0框架使用托管语言编写游戏的文章.
- 下载DLL-520 Kb(Download DLLs - 520 Kb)
- 下载AllegNet库-32.2 Kb(Download AllegNet library - 32.2 Kb)
- 下载二进制文件和源代码(C#)-31.5 Kb(Download binary and source (C#) - 31.5 Kb)
- 下载二进制文件和源代码(VB.NET)-28.8 Kb(Download binary and source (VB.NET) - 28.8 Kb)
内容(Contents)
- 介绍(Introduction)
- 先决条件(Prerequisites)
- 创建您的第一个AllegNet兼容程序(Create your first AllegNet compatible program)
- 使用代码(Using the code)
- 历史(History)
介绍(Introduction)
AllegNet是基于著名的C ++ Allegro库的游戏编程库.起初,AllegNet是一个包装器,但是由于需要在托管环境中处理大量对象,因此我通过重新创建一组类,结构和函数来进行更改,以便可以使用完整的托管对象.(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.) 使用AllegNet,您可以在用户PC上可用的情况下,在DirectX的支持下创建游戏和多媒体应用程序. AllegNet提供了与Allegro库4.2.0相同的功能,但进行了一些修改,以带来.NET Framework 2.0的强大功能.(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是一个完整的管理库,因此您可以使用C#,VB.NET,C ++.NET和J#. “示例"部分中的所有示例均针对C#,VB.NET(有时还针对J#)提供.(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#.) 本文将说明如何使用AllegNet库开始创建具有DirectX支持的多媒体应用程序.(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(msvcr80.dll)必须存在于(must be present in the)%windir%\ system32(%windir%\system32*)*目录.(*directory.*)
- 文件(The file)alleg42.dll(alleg42.dll)必须存在于(must be present in the)%windir%\ system32(%windir%\system32*)*目录.(*directory.*) 可以找到两个文件(*The two files can be found*) 这里(here) .(*.*)
创建您的第一个AllegNet兼容程序(Create your first AllegNet compatible program)
打开Visual Studio Express,然后创建一个新的控制台应用程序项目.接下来,添加对AllegNet库的引用.您可以下载一个版本(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)
接下来,您将创建一个伪造的332调色板来模拟8bits深度的渐变调色板.您将创建一个640 * 480(Next, you will create a fake 332 palette to simulate the graduate palette for 8bits depth. You will create a 640480*) BITMAP
缓冲区并将屏幕设置为640 * 480分辨率,并将颜色深度设置为颜色深度变量.(*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)
现在,要完成操作,您将在屏幕上绘制结果并等待用户按下键.在main函数中,您只需为要测试的每个深度调用该函数.(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)
- 2005年12月1日-第一个在线版本.(12/01/2005 - First online version.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VB C# WinXP Windows .NET .NET2.0 Visual-Studio GDI+ VS2005 Dev 新闻 翻译