FreeBasi學習     第6課    使用SDL載入圖片 =======================================================================================                         
彰縣湖南國小  許銘堯  老師 編寫  2010.2.8   http://www.isgame.idv.tw    更多學習就在「三本貓」!  
=======================================================================================
◇範例 1:下載此範例 (按滑鼠右鍵 > 另存目標)

#include "SDL\SDL.bi"

#include "SDL\SDL_image.bi"

 

' 保持住螢幕畫面

Dim Shared video As SDL_Surface Ptr

 

' 保持住圖片

Dim Shared temppic As SDL_Surface Ptr

 

' 保持住精靈圖

Dim Shared pic As SDL_Surface Ptr

 

' 宣告SDL繪圖

Dim Shared rect As SDL_Rect

 

' 事件

Dim event As SDL_Event

 

' We initiasize SDL and we only need video. If this tells me anything but zero

' then we can not continiue

If SDL_Init(SDL_INIT_VIDEO) <> 0 Then

   Print "FATAL: Couldnt init SDL"

   End 1

End If

 

' We set up the display mode. We wont do fullscreen, if you like then OR the

' options with SDL_FULLSCREEN to get it.

' SDL_HWSURFACE means that this surface is hold in video ram.

' If you want it to be in system ram then exchange SDL_HWSURFACE with

' SDL_SWSURFACE

' SDL_DOUBLEBUF tells SDL to create two pages.

video = SDL_SetVideoMode (800, 600, 24, SDL_HWSURFACE Or SDL_DOUBLEBUF)

 

' If the surface holds nothing then the display is not created, then get out of

' here.

If video = NULL Then

   Print "FATAL: Couldnt init SDL with vidmode 800x600x24"

   End 2

End If

 

'圖片檔案

temppic = IMG_Load("1.bmp")

' If this is empty then something went wrong

If temppic = 0 Then

   Print "Could not load file: 1.bmp"

   End 3

End If

 

'We load our sprite into memory

pic = IMG_Load("1.bmp")

' If this is empty then something went wrong

If pic = 0 Then

   Print "Could not load file: testsprite.bmp"

   End 3

End If

 

' Now we set the transparency

SDL_SetColorKey(pic, SDL_SRCCOLORKEY, SDL_MapRGB(pic->format, 255, 0, 255))

 

' We blit the image to the screen.

SDL_BlitSurface(temppic,NULL,video,NULL)

 

' This will draw our sprite with transparency on the coordinates stated

rect.x = 40

rect.y = 500

 

' This prints out our sprite

' rect needs to be passed as a SDL_Rect  ptr ! You dont need to fully understand

' what a pointer is, just do it otherwise it wont work!

SDL_BlitSurface(pic,NULL,video,@rect)

 

' We flip

SDL_Flip(video)

 

' We dont need temppic and pic anymore so get rid of it

SDL_FreeSurface(temppic)

SDL_FreeSurface(pic)

 

Do

   ' We grab the newest event from the event stack.

   SDL_PollEvent(@event)

 

   ' We just want the type of the event

   Select Case event.type

      ' Has a key been pressed?

      Case SDL_KEYDOWN:

          'this tests to see which key was pressed, in this case if the ESCAPE key was pressed, we "exit do" or exit the program basically

            If(event.key.keysym.sym=SDLK_ESCAPE) Then

            Exit Do

         End If

   End Select

Loop

 

' We have finished

 

SDL_Quit()


 

=======================================================================================
回  三本貓首頁              回  三本貓FreeBasic學習
=======================================================================================