Games with C # using SQLite SDL is a library known to carry games which is available for various operating systems and allowing both draw pictures and check the keyboard, mouse or joystick, and play sounds.
Tao.SDL is an adaptation of this library, which allows using it from C #. It can be downloaded from
http://www.mono-project.com/Tao
SDL is a library not particularly easy, and neither has just Tao.SDL, so the following sources can be difficult to understand, although very basic tasks. Therefore, it is often preferable to "hide" the details of creating our own classes SDL "Hardware", "Picture" etc. We will do later, but for now v masters to see a first example, basic but complete, showing how to enter graphics mode, upload an image, draw on the screen, wait five seconds and return to the operating system. Following comment the main source functions. / * Example SDL
access from C # (1), using Tao.SDL
By Nacho Cabanes * / using Tao. Sdl
;
-
- using System;
-
- / / For IntPtr (pointer images, etc)
-
-
public class Game
private static void
Main
(
)
{
short
width = 800
; short
high = 600 ;
bitsColor int = 24
;
int flags = ( Sdl. SDL_HWSURFACE Sdl . SDL_Init (
Sdl. SDL_INIT_EVERYTHING
)
; pantallaOculta = Sdl. SDL_SetVideoMode
( wide, high ,
bitsColor , flags ) ;
/ / Indicates what is cut out of the hidden display Sdl. SDL_Rect rect2 = new Sdl. SDL_Rect ( 0 , 0
,
(
short
) width, ( short ) high )
; Sdl.
SDL_SetClipRect ( pantallaOculta
, ref rect2 )
;
/ / Load an image
IntPtr image;
image
= Sdl . SDL_LoadBMP (
"personaje.bmp" ) ; if ( image = = IntPtr . Zero ) { System. Console. WriteLine ( "Image does not exist!" ) ;
Environment. Exit ( 4) ;
}
/ / Draw the image
short x = 400 ; short and = 300
; Sdl. SDL_Rect origin = new Sdl. SDL_Rect ( 0
, 0 , wide, high ) ; Sdl.
SDL_Rect dest = Sdl new . SDL_Rect ( x ,
and
,
wide, high
) ; Sdl. SDL_BlitSurface (
image, ref origin, pantallaOculta , ref dest
) ; / / show the hidden display Sdl . SDL_Flip ( pantallaOculta ) ; / / and wait 5 seconds System. Threading
. Thread . Sleep (5000 ) ; / / Finally, close SDL SDL . SDL_Quit (
) ; } }
Some basic ideas:
· SDL_Init is the initialization routine, which goes into graphics mode, with a screen width and height, a certain amount of colors and some additional options. · SDL_Rect type defines a "rectangle" from its origin (y) its width and height, and is used in many operations.
·
SDL_SetClipRect indicates the trimming (clipping) the size of the screen, so you do not have to worry about if we draw an image in part (or entirely) outside the visible screen. · SDL_LoadBMP load a BMP format image (if you only use SDL "pure", we can not use other types that allow smaller sizes, such as JPG, or transparency, such as PNG). The type of data you get is an "IntPtr" (which Daeman no details), and how to check if you really manage to upload the image is looking at whether the value obtained is IntPtr.Zero (and if so, could not have been loaded) or another (and then the image would have read without problems.) · SDL_BlitSurface dumps a rectangle (SDL_Rect) over another, and use it to foresee all the hidden images on a screen, and then dump all this hidden from the display screen visible, thus avoids flicker (a technique known as "double buffer"). ·
SDL_Flip hidden dumps that screen display visible (the final step of this "double buffering").
· pause For we have not used any function of SDL, but Thread.Sleep, which we discussed in the section on timing. ·
SDL_Quit releases resources (which usually would from a destructor).
To compile this example using Mono, we:
·
Enter (or copy and paste) the source.
·
in the same folder Copy the DLL files (Tao.Sdl.Dll and SDL.Dll) and images (in this case, "personaje.bmp").
·
Compile with:
sdl01.cs mcs / r: Tao.Sdl.dll
And if we use Visual Studio or SharpDevelop, we that: ·
Create a draft of the console application. " ·
Replace our main program for this. ·
Tao.Sdl.Dll Copy the file to the Fonts folder and add it to your project references (usually by pressing the right mouse button in the view of the project and choosing classes "Add Reference").
·
Copy executable folder (typically bin / debug) DLL files (Tao.Sdl.Dll and SDL.Dll) and images (in this case, "personaje.bmp"). ·
Compile and test.
If we will not use compressed images (PNG or JPG) or TTF fonts, and sounds in MP3 format or additional drawing functions (lines, boxes, circles, etc). not need any additional DLL. can download the source and executable, the DLL and a sample character, here:
Http://www.nachocabanes.com/fich/descargar.php?nombre=csharp_sdl_01.zip