Sunday, March 1, 2020
Programming games in C# using SDL.NET
Programming games in C# using SDL.NET 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 dont think it has stopped but just looks like it has. If you dont 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. Part of the OpenTk install failed; the NS (shader) because we dont 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. Looking back, we can see that the OpenTK framework wasnt needed as such, that SDL.NET installed everything but that wasnt clear at the time. It still uses the Tao Framework even though development of that has been superseded by OpenTK. Its a little confusing and we hope the SDL.NET team will bring out an OpenTk compatible version in the future. What Exactly is SDL.NET? Its 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: TimersProvides Sprites, including animation and TextProvides surfaces for 2D and OpenGlProvides support for Movie loading and playingProvides support for AudioProvides Bezier, polygon (and textures), square, circle, line, pie drawingProvides particle support with emitters and sprites and manipulators.Provides interfacing with Windows forms through a shared PictureBox with surface. 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 uses the SDL set of dlls and installs them under the lib folder. Dont delete them! 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 dont do this when the program first runs and opens up the SDL main Window it will open up a console Window as well. Were now ready to start and Ive 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. 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. Its an impressive speed. // 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 rad ius (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() ;}}} Object Oriented Development SDL.NET is very Object Oriented and there are two predefined objects that are used in every SDL.NET application. Video provides methods to set the video mode, create video surfaces, hide and show the mouse cursor, and interact with OpenGL. Not that well be doing OpenGL for a while. The Events class contains events which can be attached to to read user input and other miscellaneous occurrences. 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. Theres a .chm file (Windows html help format) in the doc folder documenting all the classes and members. 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. 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. 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. SDLNET provides just about everything to let you create games. Well be looking at the various features over the next few tutorials then move onto creating games with it.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.