Saturday, October 23, 2010

What's The Black Boogers By Cat's Nose

The game loop

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 () ;



}}





Tuesday, October 19, 2010

Sample Letter Of Offer Beauty Therapist

PNG and JPG images

know
display images in BMP format
from a program in C # that use Tao.Sdl. But the images in this format take up much space, and do not allow advanced features such as transparency (although one could imitate). If we use images in modern formats such as JPG or PNG, we need only include a few more DLL files and make a small change in the program.




The new files you need are: SDL_image.dll (the principal), libpng13.dll (PNG Image), zlib1.dll (Auxiliary above) and jpeg.dll (if you want to use JPG images).


SdlImage.IMG_Load image = ("personaje.png");

Wednesday, October 13, 2010

Discount Platypus Flossers

Tao.SDL TTF fonts with SDL and C #

If we write text using TrueType fonts (the standard in Windows and Linux), the changes are not large:
We must include the DLL file in the folder called SDL_ttf.DLL our program executable and the TTF file for each type font you want to use.
 We must declare a new data that will be our font, the generic type "IntPtr" 
tipoDeLetra IntPtr;

SdlTtf also need to initialize after basic initialization of SDL: SdlTtf.TTF_Init ();


Then prepare the font you want to use, indicating which file from TTF and what size:


/ / A font in size 18
 SdlTtf.TTF_OpenFont tipoDeLetra = ("FreeSansBold.ttf", 18); 
if (tipoDeLetra == IntPtr.Zero) {
System.Console.WriteLine ("Type nonexistent letter: FreeSansBold.ttf! ");
Environment.Exit (5);}

 
Then we can create an image from a phrase


/ / And create an image from the text
Sdl.SDL_Color colorAzulIntenso = new Sdl.SDL_Color (50, 50, 255);
 string text = "Sample text"; 
textoComoImagen IntPtr = SdlTtf.TTF_RenderText_Solid (
tipoDeLetra text, colorAzulIntenso)

if (textoComoImagen == IntPtr.Zero) {
System.Console.WriteLine ("I can not render the text");
Environment.Exit (6);}



And we could draw that image anywhere on the screen, like any other image:
 

/ / Write the text, image source = new Sdl.SDL_Rect
(0.0, anchoPantalla, altoPantalla)
dest = new Sdl.SDL_Rect (200,100, anchoPantalla, altoPantalla)
Sdl.SDL_BlitSurface (textoComoImagen, ref origin
pantallaOculta, ref dest);


As this way of working can be cumbersome, soon we will improve, creating a class "source" that we hide all these details and allow us to easily write text.


Sunday, October 10, 2010

Restoril Small Quantity

C #: An image that moves with the keyboard


We have seen the basics of how
display images on screen using C # and SDL
. 



A second more detailed example could afford to move the character with the arrow keys at a rate of 50 frames per second, so (you have the following comments the source):


/ * Example SDL
access from C # (2), using Tao.SDL
By Nacho Cabanes
* /


 
using Tao. Sdl

;


using System;

/ / For IntPtr (pointer images, etc)
public class Sdl02
{

private static

void
Main
( ) { short anchoPantalla =
800 ;


short altoPantalla = 600 ;
int bitsColor = 24 ;

int flags = Sdl . SDL_HWSURFACE Sdl
. SDL_DOUBLEBUF ( anchoPantalla , altoPantalla , bitsColor , flags
) ;

/ / indicate that it is cut to leave the hidden display

Sdl. SDL_Rect rect2
= new Sdl. SDL_Rect ( 0 , 0 ,

( short )
anchoPantalla ,
(
short
) altoPantalla ) ;
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) ;
} / / Part repetitive bool finished = false ; short x = 400

; short and = 300 ; short anchoImagen =
50 ; short altoImagen = 50 ;

SDL .
SDL_Event event
;
int numkeys ;
byte [ ]

keys ; of
{ / / Check event Sdl. SDL_PollEvent

( out event ) ; keys
= Sdl. SDL_GetKeyState ( out numkeys
) ;

/ / Let's see if you've pressed a cursor arrow if ( keys [
Sdl
. SDLK_UP
]
= 1 =
) y - = 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 ; / / Clear screen Sdl. SDL_Rect origin = new SDL.

SDL_Rect ( 0, 0
, anchoPantalla , altoPantalla ) ; SDL. SDL_FillRect ( pantallaOculta , ref

origin, 0)


;
/ / Draw in their new home coordinates = new Sdl. SDL_Rect ( 0 , 0 ,
anchoImagen
, altoImagen ) ;
Sdl. SDL_Rect dest = new Sdl. SDL_Rect ( x , and ,

anchoImagen ,
altoImagen
) ; Sdl. SDL_BlitSurface ( image, ref origin, pantallaOculta , ref dest ) ;

/ / show the screen Sdl hidden . SDL_Flip ( pantallaOculta ) ;
/ / And wait 20 ms
System. Threading . Thread . Sleep
( 20) ;
} while (

!
finished )
; / / Finally, close SDL Sdl . SDL_Quit (

)
; } } the differences with the previous source are:
When initializing, we add a new option, Sdl.SDL_FULLSCREEN, so that the "game" to run full screen instead of on window. We use a loop

"do ... while" to repeat until you press the ESC key. SDL_Event is the type of data used to check the "events", such as keystrokes or mouse, or using the joystick. With SDL_PollEvent forcing them to look at what events are pending analysis. SDL_GetkeyState get an array that gives us the current status of each key. Then we can look each of these keys by accessing the array with the key name in English as well: keys [Sdl.SDLK_RIGHT] SDL_FillRect fills a rectangle with some color. It's an easy way to clear the screen or part of it.