Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TUTORIAL: SDL Basics with FreeBASIC
#1
SDL Basics with FreeBASIC - Tutorial 1

In this tutorial you will learn to set up SDL and get an image on the screen,
pretty cool, uh?
Ok, it is just to teach you the basics of SDL.

So, have a look at this little piece of code:

Code:
'$include: "SDL\SDL.bi"
'$include: "SDL\SDL_image.bi"

' Surfaces are the A and O of SDL.
' You place your backgrounds, your sprites and everything else that has
' something to do with graphics inside a surface.

' This will hold the screen
dim shared video as SDL_Surface ptr
' This will hold our picture
dim shared temppic as SDL_Surface ptr

' Here goes the event holder
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

' Now we load the Picture to be displayed into system memory
' You can load bmp, pnm, xpm, xcf, pcx, gif, jpg, tif, png and lbm
' I stick with png because gif is patented and just 256 colors thick and the
' others are just too bloated or destroy information in the picture.
temppic = IMG_Load("testpic.png")
' If this is empty then something went wrong
if temppic = 0 then
   print "Could not load file: "; picfile$
   end 3
end if

' We blit the image to the screen.
' SDL_BlutSurface has the parameters
' (start,startcoordinates,destination, destinationcoordinates)
' Where coordinates is a simple SDL_Rect structure.
SDL_BlitSurface(temppic,NULL,video,NULL)

' With this command we flip the video ram locations so that our painting gets
' visible.
SDL_Flip(video)

' We dont need temppic anymore so get rid of it
SDL_FreeSurface(temppic)

do
   ' We grab the newest event from the event stack.
   ' I havent tryed it, but AFAIK you can even grab Win32 events like WM_CLOSE
   ' this way ;)
   SDL_PollEvent(@event)

   ' We just want the type of the event
   select case event.type
      ' Has a key been pressed?
      case SDL_KEYDOWN:
         exit do
      ' Has a mousebutton been pressed?
      case SDL_MOUSEDOWN:
         exit do
      ' In both cases we just get out of here
   end select
loop

' And as we dont asume that the compiler calls it, old C habbid ;), we assure
' that it is done and SDL gets deinitialised
SDL_Quit()

In the next issue i will tell you how to move objects and achive transparency.

So Long, The Werelion!
color=red]Look at you, Hacker. A pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?" - Shodan, AI at Citadel Station orbiting Earth[/color]
Reply
#2
Nice!

Simple, pretty clean code.
This will be good Big Grin
Can't wait for the next coming editions.
·~¹'°¨°'¹i|¡~æthérFòx~¡|i¹'°¨°'¹~·-
avinash.vora - http://www.avinashv.net
Reply
#3
Pete needs articles for QB Express... Why don't you submit this or a series of these?
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#4
Quote:Pete needs articles for QB Express... Why don't you submit this or a series of these?

He can add it if he likes, ill do the next one this evening, i still need to fetch newspapers around. Something has to feed my needs in computers Wink
color=red]Look at you, Hacker. A pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?" - Shodan, AI at Citadel Station orbiting Earth[/color]
Reply
#5
Well, you can PM him about this and I bet he would be glad to put in in his mag. :*)
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#6
Quote:Well, you can PM him about this and I bet he would be glad to put in in his mag. :*)

[X] Done!
color=red]Look at you, Hacker. A pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?" - Shodan, AI at Citadel Station orbiting Earth[/color]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)