偶然得知SDL这个游戏库赶忙迫不及待的学习了一下正好最近在学习DELPHI于是下了DELPHI版的可以在这两个站点了解到相关的信息 SDL库设计的十分的简洁非常容易使用我的代码实例实现了BMPPNGJPG三种图片格式的加载显示并加入了TTF字体的显示都是库使用的例子代码不难发出来共享以下是截图 下面是代码//Program: A simple delphi sdl demo //Author: shaoyun //Mail: shaoyun at (please use @ instead of at) program SDLDemo; uses SysUtils Windows SDL SDL_Image SDL_TTF; var screen: PSDL_Surface; event: TSDL_Event; isOver: boolean = false;
//*******************定义显示位图的函数draw_bmp()************************* //BMP格式的图片通常都上MB了谁会用这种格式做游戏
procedure draw_bmp(surface: PSDL_Surface; img_path: PChar; x_pos: integer; y_pos: integer); var image: PSDL_Surface; dest: TSDL_Rect; begin image := SDL_LoadBMP(img_path); if (image = nil) then begin MessageBox( PChar(Format( Error:%s! # [SDL_GetError])) Error MB_OK or MB_ICONHAND); exit; end; if (imageformatpalette <> nil) then begin SDL_SetColors(surface @imaglors[] imageformatpalettencolors); end; destx := x_pos; desty := y_pos; destw := ; desth := ; if (SDL_BlitSurface(image nil surface @dest) < ) then MessageBox( PChar(Format( BlitSurface error : %s [SDL_GetError])) Error MB_OK or MB_ICONHAND); SDL_UpdateRect(surface imagew imageh); SDL_FreeSurface(image); end;
//*******************定义显示图片的函数draw_img()************************* //这个函数的调用须有SDL_Imagedlljpegdlllibpngdll的支持 可以显示bmpjpgpng三种格式 //文档指明显示png格式需要zlibdll和libpngdll
procedure draw_img(surface: PSDL_Surface; img_path: PChar; x_pos: integer; y_pos: integer); var image: PSDL_Surface; dest: TSDL_Rect;
begin image := IMG_Load(img_path); if image = nil then begin MessageBox( PChar(Format( Error:%s! # [SDL_GetError])) Error MB_OK or MB_ICONHAND); exit; end; destx := x_pos; desty := y_pos; destw := ; desth := ; SDL_BlitSurface(image nil surface @Dest); SDL_FreeSurface(image); end; //*******************定义显示TTF字体的函数draw_text()************************* //不能显示中文不过网上有人实现了中文的显示
procedure draw_text(surface: PSDL_Surface; words: PChar; x_pos: integer; y_pos: integer); var text: PSDL_Surface; font: PTTF_Font; dest: TSDL_Rect; textColor: TSDL_Color; begin textcolorr := $; textcolorg := $FF; textcolorb := $; textcolorunused := ; font := TTF_OpenFont( f ); if font = nil then begin MessageBox( PChar(Format( Error:%s! # [SDL_GetError])) Error MB_OK or MB_ICONHAND); exit; end; text := TTF_RenderText_Blended(font words textColor); destx := x_pos; desty := y_pos; SDL_BlitSurface(text nil surface @Dest); SDL_Flip(screen); SDL_FreeSurface(text); TTF_CloseFont(font); end; //*****************************Main*************************** // begin if (SDL_Init(SDL_INIT_VIDEO) < ) then exit; SDL_WM_SetCaption( Delphi SDL Simple Demo nil); screen := SDL_SetVideoMode( SDL_SWSURFACE); //设置分辨率 if (screen = nil) then begin SDL_Quit; exit; end; //draw_bmp(screenbgbmp); draw_img(screen bgjpg ); //TTF初始化 if TTF_Init() < then begin MessageBox( PChar(Format( Error:%s! # [SDL_GetError])) Error MB_OK or MB_ICONHAND); exit; end; draw_text(screen A Delphi SDL Simple Demo ); draw_text(screen By shaoyun ); draw_text(screen Email: ); //销毁TTF TTF_Quit(); while not isOver do begin while (SDL_PollEvent(@event) <> ) do //处理键盘按键 begin case of SDL_QUITEV: isOver := true; SDL_KEYDOWN: begin case eventkeykeysymsym of SDLK_ESCAPE: isOver := True; end; end; end; end; end; SDL_Quit; end |