SDL
A source can be difficult to read, and even more if not structured, but has all the logic in "Main" and grows arbitrarily. So, it is often preferable to create our own functions that would hide a bit: Functions as "Initialize", "CargarImagen", "ComprobarTeclas", "DibujarImagenes", etc.
A "classic game loop" would look like this:
- Checked events (keystrokes, mouse clicks and movement, use joystick ...) Move
- game elements (a character, enemies, backgrounds mobile) Check
- collisions game elements (which may involve loss lives or energy, earn points, etc)
- Draw all items on the screen in its current state
- Pause at the end of each "frame" for the speed of the game is the same on any computer, and, step, to help multi-tasking operating system.
therefore create these functions, along with other assistants to initialize the system, draw a hidden screen image, write text on the screen hidden ... The result could be:
/*---------------------------*/
/ * sdl05.cs * /
/ * * /
/ * Fifth approach * /
/ * SDL * /
/ * * /
/ * For Nacho Cabanes * /
/*----------------
-----------*/
using Tao.Sdl;
using System; / / For IntPtr (pointer images, etc)
{public class Game
short anchoPantalla = 800;
short altoPantalla = 600;
bool finished = false;
short x = 400;
short y = 300;
short anchoImagen = 50;
short altoImagen = 50;
Sdl.SDL_Event event;
numkeys int;
byte [] keys;
pantallaOculta IntPtr, IntPtr
tipoDeLetra;
IntPtr image;
void initialize () {
bitsColor int = 24 int flags = Sdl.SDL_HWSURFACE
which is cut off the screen hidden
Sdl.SDL_Rect rect2 = new Sdl.SDL_Rect (0.0, (short) anchoPantalla, (short) altoPantalla)
Sdl.SDL_SetClipRect (pantallaOculta, ref rect2)
/ / Load an image
image = SdlImage.IMG_Load (personaje.png ");
if (image == IntPtr.Zero) {
System.Console.WriteLine (" Image does not exist: personaje.png! ") ;
Environment.Exit (4);
} / / And a font in size 18
SdlTtf.TTF_OpenFont tipoDeLetra = ("FreeSansBold.ttf" 18);
if (tipoDeLetra == IntPtr.Zero) {
System.Console.WriteLine ("Font does not exist: FreeSansBold.ttf!");
Environment.Exit (5);
}}
void
comprobarTeclas ()
{/ / Check
Sdl.SDL_PollEvent events (out event);
keys = Sdl.SDL_GetKeyState (out numkeys)
/ / Let's see if you've pressed a cursor arrow
if (keys [ Sdl.SDLK_UP] == 1)
and -= 2;
if (keys [Sdl.SDLK_DOWN] == 1) y + =
2;
if (keys [Sdl.SDLK_LEFT] == 1)
x -= 2;
if (keys [Sdl.SDLK_RIGHT] == 1)
x + = 2;
if (keys [Sdl.SDLK_ESCAPE] = = 1)
finished = true;}
comprobarColisiones void () {
/ / Still not check collisions with enemies
/ / or with obstacles}
moverElementos void () {
/ / Not yet There is no single element that moves}
dibujarElementos void () {
/ / Clear screen Sdl.SDL_Rect
origin = new Sdl.SDL_Rect (0.0,
anchoPantalla, altoPantalla)
Sdl.SDL_FillRect (pantallaOculta, ref origin, 0);
/ / Draw the image in its new coordinates
dibujarImagenOculta (image, x, y, anchoImagen, altoImagen)
/ / Write the text, image
escribirTextoOculta ("Sample Text"
/ * Coordinates * / 200.100,
/ * Colors * / 50, 50, 255,
tipoDeLetra
)
/ / show the hidden display
Sdl.SDL_Flip (pantallaOculta)
} void pausaFotograma ()
{/ / Wait 20 ms
System.Threading.Thread.Sleep (20);}
void dibujarImagenOculta (IntPtr image, short x, short and short
wide short height) {
Sdl.SDL_Rect
origin = new Sdl.SDL_Rect (0, 0, width, height);
Sdl.SDL_Rect Sdl.SDL_Rect dest = new (x, y, width, height);
Sdl.SDL_BlitSurface ( image, ref origin pantallaOculta, ref dest);}
escribirTextoOculta void (string text,
short x, short and byte r, byte g, byte b, IntPtr source)
{/ / Create an image from the text
Sdl.SDL_Color color = new Sdl.SDL_Color (r, g, b) = SdlTtf.TTF_RenderText_Solid
textoComoImagen IntPtr (
font, text, color )
if (textoComoImagen == IntPtr.Zero) {
System.Console.WriteLine ("I can not render the text");
Environment.Exit (6);}
dibujarImagenOculta (textoComoImagen, x, y, (short) (800-x), (short) (600-y));}
partidaTerminada bool ()
{return finished;
} private static void Main () {
Game
j = new Game ();
j.inicializar ();
/ / game loop do {
j.comprobarTeclas ();
j.moverElementos ();
j.comprobarColisiones ();
j.dibujarElementos ();
j.pausaFotograma ();
} while (! j.partidaTerminada ());
/ / Finally, close SDL
Sdl.SDL_Quit () ;
}}