SDL draw screen mechanism

 
SDL( Simple DirectMedia Layer)是一个跨平台的multimedia library ,包含了对video,audio,keyboard,mouse的支持。
它的接口比较简洁,你甚至不需要知道Win32 API,就可以写出游戏来。它封装了跟
平台相关的部分。它使我想起了TC下面的graphics BGI

官方地址:   http://www.libsdl.org/

下面是我用SDL写的一个小游戏
run.jpg


1.骨架程序

#include  " sdl.h "
#pragma comment(lib,
" sdl.lib " )
#pragma comment(lib,
" sdlmain.lib " )

int  main( int  argc, char   **  argv)
{
    
// Init SDL
    SDL_Init( SDL_INIT_VIDEO );
    atexit(SDL_Quit);
    screen 
=  SDL_SetVideoMode( 400 , 480 , 0 ,SDL_SWSURFACE);
    SDL_WM_SetCaption(
" RUN " ,NULL);

    
// load resource
    
//

    
bool  pause  =   false ;
    
bool  gone  =   true ;

    SDL_Event 
event ;
    
while (gone)
    {
        
if (SDL_PollEvent( & event ) > 0 )
        {
            
if ( event .type  ==  SDL_QUIT)
            {
                
break ;
            }
            
else   if ( event .type  ==  SDL_KEYDOWN)
            {
        
// handle event
        
//
            }
            
else   if ( event .type  ==  SDL_KEYUP)
            {
        
// handle event
        
//
            }
        }
        
else   if ( ! pause)
        {
            run(); 
        }
    }

    
// release resource
    
//
     return   0 ;
}


SDL_Init : 初始化子系统
SDL_SetVideoMode:设置分辨率
SDL_WM_SetCaption: 设置窗口标题
SDL_PollEvent:查询事件,用户的输入

2.表面的概念

表面(SDL_Surface)是SDL中一个重要的概念,精灵都是保存在表面中

SDL_Surface  *  temp  =  SDL_CreateRGBSurface(SDL_SRCCOLORKEY, w, h,  32 ,
                
0 0 0 0 );
            
rc.x 
=  x  *  w ;
rc.y 
=  y  *  h;
            
SDL_BlitSurface(surf,
& rc,temp, 0 );
           
SDL_SetColorKey(temp,SDL_SRCCOLORKEY,transcolor);

SDL_CreateRGBSurface 创建一个表面
SDL_BlitSurface 表面之间的copy
SDL_SetColorKey 设定表面的透明色

ps:主表面是由SDL_SetVideoMode返回的,也可以通过 SDL_GetVideoSurface查询到

3. 声音的使用

游戏离不开声音,SDL也提供了支持

void  play_sound( const   char   * file)
{
    
int  index;
    SDL_AudioSpec wave;
    Uint8 
* data;
    Uint32 dlen;
    SDL_AudioCVT cvt;

    
/*  寻找一个空的(或者完成了的)音频口  */
    
for  ( index = 0 ; index < NUM_SOUNDS;  ++ index ) {
        
if  ( sounds[index].dpos  ==  sounds[index].dlen ) {
            
break ;
        }
    }
    
if  ( index  ==  NUM_SOUNDS )
        
return ;

    
/*  加载声音文件,并转换成16位、立体声、22kHz格式  */
    
if  ( SDL_LoadWAV(file,  & wave,  & data,  & dlen)  ==  NULL ) {
        fprintf(stderr, 
" 无法加载 %s: %s\n " , file, SDL_GetError());
        
return ;
    }
    SDL_BuildAudioCVT(
& cvt, wave.format, wave.channels, wave.freq,
                            AUDIO_S16,   
2 ,              22050 );
    cvt.buf 
=  (unsigned  char   * )malloc(dlen * cvt.len_mult);
    memcpy(cvt.buf, data, dlen);
    cvt.len 
=  dlen;
    SDL_ConvertAudio(
& cvt);
    SDL_FreeWAV(data);

    
/*  将音频数据放入音频口(立刻开始回放了)  */
    
if  ( sounds[index].data ) {
        free(sounds[index].data);
    }
    SDL_LockAudio();
    sounds[index].data 
=  cvt.buf;
    sounds[index].dlen 
=  cvt.len_cvt;
    sounds[index].dpos 
=   0 ;
    SDL_UnlockAudio();
}

4.一些有用的模块

#include  " sdl_image.h "

/*
 * Return the pixel value at (x, y)
 * NOTE: The surface must be locked before calling this!
 
*/
Uint32 getpixel(SDL_Surface 
* surface,  int  x,  int  y)
{
    
int  bpp  =  surface -> format -> BytesPerPixel;
    
/*  Here p is the address to the pixel we want to retrieve  */
    Uint8 
* =  (Uint8  * )surface -> pixels  +  y  *  surface -> pitch  +  x  *  bpp;

    
switch (bpp) {
    
case   1 :
        
return   * p;

    
case   2 :
        
return   * (Uint16  * )p;

    
case   3 :
        
if (SDL_BYTEORDER  ==  SDL_BIG_ENDIAN)
            
return  p[ 0 <<   16   |  p[ 1 <<   8   |  p[ 2 ];
        
else
            
return  p[ 0 |  p[ 1 <<   8   |  p[ 2 <<   16 ;

    
case   4 :
        
return   * (Uint32  * )p;

    
default :
        
return   0 ;        /*  shouldn't happen, but avoids warnings  */
    }
}


/*
 * Set the pixel at (x, y) to the given value
 * NOTE: The surface must be locked before calling this!
 
*/
void  putpixel(SDL_Surface  * surface,  int  x,  int  y, Uint32 pixel)
{
    
int  bpp  =  surface -> format -> BytesPerPixel;
    
/*  Here p is the address to the pixel we want to set  */
    Uint8 
* =  (Uint8  * )surface -> pixels  +  y  *  surface -> pitch  +  x  *  bpp;

    
switch (bpp) {
    
case   1 :
        
* =  pixel;
        
break ;

    
case   2 :
        
* (Uint16  * )p  =  pixel;
        
break ;

    
case   3 :
        
if (SDL_BYTEORDER  ==  SDL_BIG_ENDIAN) {
            p[
0 =  (pixel  >>   16 &   0xff ;
            p[
1 =  (pixel  >>   8 &   0xff ;
            p[
2 =  pixel  &   0xff ;
        } 
else  {
            p[
0 =  pixel  &   0xff ;
            p[
1 =  (pixel  >>   8 &   0xff ;
            p[
2 =  (pixel  >>   16 &   0xff ;
        }
        
break ;

    
case   4 :
        
* (Uint32  * )p  =  pixel;
        
break ;
    }
}

bool  showpic(SDL_Surface  * surface, const   char   * filename)
{
    SDL_Surface 
* image;

    
/*  Load the BMP file into a surface  */
    image 
=  IMG_Load(filename);
    
if  (image  ==  NULL) {
        fprintf(stderr, 
" Couldn't load %s: %s\n " ,filename,SDL_GetError());
        
return   false ;
    }

    
/*  Blit onto the screen surface  */
    
if (SDL_BlitSurface(image, NULL, surface, NULL)  <   0 )
        fprintf(stderr, 
" BlitSurface error: %s\n " , SDL_GetError());

    SDL_UpdateRect(surface, 
0 0 , image -> w, image -> h);

    
/*  Free the allocated BMP surface  */
    SDL_FreeSurface(image);

    
return   true ;
}

SDL_Surface
*  createsurf( const   char   *  filename)
{
    SDL_Surface 
* image;

    
/*  Load the BMP file into a surface  */
    image 
=  IMG_Load(filename);
    
if  (image  ==  NULL) {
        fprintf(stderr, 
" Couldn't load %s: %s\n " ,filename,SDL_GetError());
        
return   0 ;
    }

    
return  image;
}

getpixel 取得像素的颜色
putpixel 画点
showpic 加载一个图片到某个表面
createsurf 从文件中创造表面

SDL有大量的示例和第三方库可以使用,非常的不错
最后推荐一个SDL写的超级玛丽游戏
http://smclone.sourceforge.net/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include #include //用键盘控制精灵移动 int main(int argc, char ** argv) { SDL_Surface * screen; //主表面 SDL_Surface * image; //用来放MM-----的图片信息(像素) SDL_Surface * PlayerImage; //用来测试的图片 SDL_Event event; Uint32 BeginTicks, EndTicks; SDL_Rect PRect, BRect; //PRect对应精灵的移动的小图位置(实现动画),BRect对应精灵在屏幕的位置。 unsigned char PlayerStarts = 0; unsigned char PlayerIndex = 0; bool bAppRun = true; //初始化SDL if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { fprintf(stderr, "SDL_Init %s\n", SDL_GetError()); return -1; } //初始化成功设置退出要调用的函数SDL_Quit atexit(SDL_Quit); //创建一个640X480 16-bit 模式的主表面 16位可以让MM的效果好看一点 screen = SDL_SetVideoMode(230, 230, 16, SDL_SWSURFACE); if (screen == NULL) { fprintf(stderr, "Couldn't set 640x480x8 video mode %s\n", SDL_GetError()); return -1; } //读取MM图片信息,并创建一个表面,并把数据填入该表面中。 image = SDL_LoadBMP("./mm.bmp"); //请在终端里运行该程序 if (image == NULL) { fprintf(stderr, "Couldn't load MM, %s\n", SDL_GetError()); //遗憾你不能显示MM了,不过你可以用图片浏览程序看。 return -1; } //读取player.bmp PlayerImage = SDL_LoadBMP("./player.bmp"); //请在终端里运行该程序 if (image == NULL) { fprintf(stderr, "Couldn't load MM, %s\n", SDL_GetError()); //遗憾你不能显示MM了,不过你可以用图片浏览程序看。 return -1; } //读取第一个像素 Uint8 key = *((Uint8 *)PlayerImage->pixels); //设置色键 SDL_SetColorKey(PlayerImage, SDL_SRCCOLORKEY, key); //有了MM的表面了,就可以显示了。 //将MM的表面画在我们的主表面上,用MM来作为背景 if (SDL_BlitSurface(image, NULL, screen, NULL) < 0) { //解释一下NULL,第一个是按照image的尺寸显示,第二个是默认显示。你也可以指定大小,不过要用到SDL_Rect你可以看一看帮助。 fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError()); //看看提示吧 return -1; } PRect.x = 0; //初始化动画显示的图片。 PRect.y = 0; PRect.w = 32; PRect.h = 48; BRect.x = 0; //初始化精灵的位置。 BRect.y = 0; BRect.w = 32; BRect.h = 48; //贴上测试用的表面 if (SDL_BlitSurface(PlayerImage, &PRect, screen, &BRect) w, image->h); BeginT

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值