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.
0 comments:
Post a Comment