当前位置: 首页 > 工具软件 > SDL.NET > 使用案例 >

c# sdl 音频_使用SDL.NET教程C用C#编程游戏

王锐
2023-12-01

c# sdl 音频

One of the problems with open source is that projects sometimes seem to fall by the wayside or take confusing turns. Take SDL.NET. Ignoring the website for sale, a search on the web reveals cs-sdl.sourceforge.net a project that seems to have stopped in November 2010. We don't think it has stopped but just looks like it has.

开源的问题之一是项目有时似乎掉到了一边或陷入混乱。 以SDL.NET为例。 忽略要出售的网站,在网上搜索发现cs-sdl.sourceforge.net一个似乎在2010年11月停止的项目。我们不认为它已经停止,但看起来已经停止了。

If you don't know C#, you will first need to learn how to program in C#. Looking elsewhere, we came across the Tao framework linked on the Mono website which seems to cover the same area and adding support for sound etc. But looking on sourceforge (again!), it has been superseded by OpenTK but the focus there is OpenGL. However, it also includes OpenAL so installing the two (cs-sdl and OpenTK) seemed to be the way forward.

如果您不了解C#,则首先需要学习如何使用C#进行编程 。 在其他地方,我们遇到了Mono网站上链接的Tao框架,该框架似乎涵盖了同一领域并增加了对声音等的支持。但是在sourceforge上(再次!),它已被OpenTK取代,但重点是OpenGL。 但是,它也包含OpenAL,因此安装两者(cs-sdl和OpenTK)似乎是前进的道路。

Part of the OpenTk install failed; the NS (shader) because we don't have VS 2008 installed! However, the rest of it was ok. We created a C# Console project and started playing with SDL.NET. The online documentation can be found here.

部分OpenTk安装失败; NS(着色器),因为我们没有安装VS 2008! 但是,其余的一切都还可以。 我们创建了一个C#Console项目,并开始使用SDL.NET。 在线文档可以在这里找到。

Looking back, we can see that the OpenTK framework wasn't needed as such, that SDL.NET installed everything but that wasn't clear at the time. It still uses the Tao Framework even though development of that has been superseded by OpenTK. It's a little confusing and we hope the SDL.NET team will bring out an OpenTk compatible version in the future.

回顾过去,我们可以看到并不需要OpenTK框架,SDL.NET安装了所有内容,但当时还不清楚。 即使OpenTK取代了Tao框架的开发,它仍然使用Tao框架。 这有点令人困惑,我们希望SDL.NET团队将来会推出与OpenTk兼容的版本。

What Exactly is SDL.NET?

SDL.NET到底是什么?

It's not, as we thought, just a thin wrapper round SDL, but adds considerable extra functionality. There are a number of classes provided to provide the following:

正如我们认为的那样,它不只是一个薄的包装圆形SDL,而是增加了相当多的额外功能。 提供了许多类来提供以下内容:

  • Timers

    计时器
  • Provides Sprites, including animation and Text

    提供精灵,包括动画和文本
  • Provides surfaces for 2D and OpenGl

    提供2D和OpenGl的表面
  • Provides support for Movie loading and playing

    提供电影加载和播放的支持
  • Provides support for Audio

    提供音频支持
  • Provides Bezier, polygon (and textures), square, circle, line, pie drawing

    提供贝塞尔曲线,多边形(和纹理),正方形,圆形,直线,饼图
  • Provides particle support with emitters and sprites and manipulators.

    用发射器,子画面和操纵器提供粒子支持。
  • Provides interfacing with Windows forms through a shared PictureBox with surface.

    通过带有表面的共享PictureBox提供与Windows窗体的接口。

准备工作 ( Preparations )

There are several things you have to do to get it set up. Here they are:

要设置它,您需要做几件事。 他们来了:

Locate the two SDL.NET dlls (SdlDotNet.dll and Tao.Sdl.dll) as well as the OpenTK dlls, and add them to the project references. After installation, the dlls are located in Program Files\SdlDotNet\bin (on a 32 bit Windows and Program Files (x86)\SdlDotNet\bin on 64 bit Windows. Right click on the References section in Solution Explorer then click Add Reference and select the Browse tab. That opens an Explorer dialog and after locating the dlls select then and click ok.

找到两个SDL.NET dll(SdlDotNet.dll和Tao.Sdl.dll)以及OpenTK dll,并将它们添加到项目引用中。 安装后,这些dll位于32位Windows上的Program Files \ SdlDotNet \ bin(和64位Windows上的Program Files(x86)\ SdlDotNet \ bin)中。右键单击“解决方案资源管理器”中的“引用”部分,然后单击“添加引用”并选择打开浏览器对话框,找到dll后,选择,然后单击确定。

SDL.NET uses the SDL set of dlls and installs them under the lib folder. Don't delete them!

SDL.NET使用SDL的一组dll,并将它们安装在lib文件夹下。 不要删除它们!

One last thing, click on the View\Properties so it opens up the Property pages and on the first tab (Application) Change Output type from Console Application to Windows Application. If you don't do this when the program first runs and opens up the SDL main Window it will open up a console Window as well.

最后一件事,单击“视图\属性”,以打开“属性”页面,并在第一个选项卡(“应用程序”)上,将“输出类型”从“控制台应用程序”更改为“ Windows应用程序”。 如果在程序首次运行并打开SDL主窗口时不执行此操作,它将同时打开一个控制台窗口。

We're now ready to start and I've created a short application below. This blits randomly sized and located rectangles and circles on the Window surface at 1,700 drawn per second at a frame rate of 50 frames per second.

现在我们准备开始,并且在下面创建了一个简短的应用程序。 这会以每秒50帧的帧速率以每秒1,700的速度在窗口表面随机绘制大小和定位的矩形和圆形。

That 1,700 comes from setting the number drawn per frame to 17 and displaying the frames per second in the Window caption using Video.WindowCaption. Each frame it draws 17 filled circles and rectangles, 17 x 2 x 50 = 1,700. This figure depends on the video card, CPU etc. It's an impressive speed.

这1,700来自将每帧绘制的数目设置为17并使用Video.WindowCaption在Window字幕中显示每秒的帧数。 每帧绘制17个填充的圆形和矩形,即17 x 2 x 50 = 1,700。 这个数字取决于视频卡,CPU等。这是令人印象深刻的速度。

// By David Bolton, http://cplus.about.comusing System;using System.Drawing;using SdlDotNet.Graphics;using SdlDotNet.Core;using SdlDotNet.Graphics.Primitives;public class ex1{private const int wwidth = 1024;private const int wheight = 768;private static Surface Screen;private static Random r = new Random() ;public static void Main(string[] args){Screen = Video.SetVideoMode( wwidth, wheight, 32, false, false, false, true) ;Events.TargetFps = 50;Events.Quit += (QuitEventHandler) ;Events.Tick += (TickEventHandler) ;Events.Run() ;}private static void QuitEventHandler(object sender, QuitEventArgs args){Events.QuitApplication() ;}private static void TickEventHandler(object sender, TickEventArgs args){for (var i = 0; i < 17; i++){var rect = new Rectangle(new Point(r.Next(wwidth- 100),r.Next(wheight-100)),new Size(10 + r.Next(wwidth - 90), 10 + r.Next(wheight - 90))) ;var Col = Color.FromArgb(r.Next(255),r.Next (255),r.Next(255)) ;var CircCol = Color.FromArgb(r.Next(255), r.Next (255), r.Next(255)) ;short radius = (short)(10 + r.Next(wheight - 90)) ;var Circ = new Circle(new Point(r.Next(wwidth- 100),r.Next(wheight-100)),radius) ;Screen.Fill(rect,Col) ;Circ.Draw(Screen, CircCol, false, true) ;Screen.Update() ;Video.WindowCaption = Events.Fps.ToString() ;}}}

// David Bolton,http://cplus.about.com。使用系统;使用System.Drawing;使用SdlDotNet.Graphics;使用SdlDotNet.Core;使用SdlDotNet.Graphics.Primitives;公共类ex1 {private const int wwidth = 1024; private const int wheight = 768; private static Surface Screen; private static Random r = new Random(); public static void Main(string [] args){Screen = Video.SetVideoMode(wwidth,wheight,32,false,false,false ,true); Events.TargetFps = 50; Events.Quit + =(QuitEventHandler); Events.Tick + =(TickEventHandler); Events.Run();}私有静态无效QuitEventHandler(对象发送方,QuitEventArgs参数){Events.QuitApplication ();}私有静态void TickEventHandler(对象发送者,TickEventArgs参数){for(var i = 0; i <17; i ++){var rect = new Rectangle(new Point(r.Next(wwidth-100),r。 Next(wheight-100)),new Size(10 + r.Next(wwidth-90),10 + r.Next(weight-90))); var Col = Color.FromArgb(r.Next(255),r .Next(255),r.Next(255)); var CircCol = Color.FromArgb(r.Next(255),r.Next(255),r.Next( 255));短半径=(短)(10 + r.Next(高度-90)); var Circ =新圆(新点(r.Next(宽度100),r.Next(高度100)) ,radius); Screen.Fill(rect,Col); Circ.Draw(Screen,CircCol,false,true); Screen.Update(); Video.WindowCaption = Events.Fps.ToString();}}}

面向对象的开发 ( Object Oriented Development )

SDL.NET is very Object Oriented and there are two predefined objects that are used in every SDL.NET application.

SDL.NET非常面向对象,每个SDL.NET应用程序中都有两个预定义的对象。

Video provides methods to set the video mode, create video surfaces, hide and show the mouse cursor, and interact with OpenGL. Not that we'll be doing OpenGL for a while.

视频提供了设置视频模式,创建视频表面,隐藏和显示鼠标光标以及与OpenGL交互的方法。 并不是说我们会做一段时间的OpenGL。

The Events class contains events which can be attached to to read user input and other miscellaneous occurrences.

Events类包含一些事件,可以附加这些事件来读取用户输入和其他杂项。

Here the Video object is used to set the size and resolution of the game Window (full screen is an option). The parameters for SetVideoMode let you change these and 13 overloads provide plenty of variety. There's a .chm file (Windows html help format) in the doc folder documenting all the classes and members.

这里的Video对象用于设置游戏窗口的大小和分辨率(全屏选项)。 SetVideoMode的参数使您可以更改这些参数,并且13种重载提供了多种选择。 doc文件夹中有一个.chm文件(Windows html帮助格式),记录了所有类和成员。

The Events object has a Quit events handler that lets you add close down logic and you should call Events.QuitApplication() to make it respond to the user closing the application. The Events.Tick is possibly the most important event handler. It calls the specified event handler each frame. This is the model for all SDL.NET development.

Events对象具有Quit事件处理程序,可让您添加关闭逻辑,并且应调用Events.QuitApplication()使其响应用户关闭应用程序。 Events.Tick可能是最重要的事件处理程序。 它每帧调用指定的事件处理程序。 这是所有SDL.NET开发的模型。

You can set your desired frame rate and my reducing the loop to 5 and changing the Targetfps to 150 we got it running at 164 frames per second. TargetFps is a ballpark figure; it puts in delays to get you near that figure but the Events.Fps is what is delivered.

您可以设置所需的帧速率,然后将循环数减少到5,并将Targetfps更改为150,我们使其以每秒164帧的速度运行。 TargetFps是一个球场人物。 它会延迟您使您接近该数字,但Events.Fps就是交付的东西。

表面 ( Surfaces )

Like the original non Windowed version of SDL, the SDL.NET uses surfaces for rendering to the screen. A surface can be constructed from a graphics file. There are a large number of properties and methods that make it possible to read or write pixels as well as draw the graphics primitives, blit other surfaces, even dump a surface to a disk file for taking screenshots.

像原始的非Windowed SDL版本一样,SDL.NET使用曲面将其渲染到屏幕上。 可以从图形文件构造表面。 有许多属性和方法可以读取或写入像素,以及绘制图形基元,遮盖其他表面,甚至将表面转储到磁盘文件以进行屏幕截图。

SDL>NET provides just about everything to let you create games. We'll be looking at the various features over the next few tutorials then move onto creating games with it.

SDL> NET提供了几乎所有使您可以创建游戏的内容。 在接下来的几篇教程中,我们将介绍各种功能,然后继续使用它来创建游戏。

翻译自: https://www.thoughtco.com/programming-games-using-sdl-net-958608

c# sdl 音频

 类似资料: