熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

SDL學習筆記一圖片和字體顯示

2013-11-11 21:03:56  來源: Delphi編程 

  偶然得知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


From:http://tw.wingwit.com/Article/program/Delphi/201311/8386.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.