Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TUT : SDL,FreeBASIC,transparency,keyboard and other suspects
#1
SDL, FreeBASIC, transparency, keyboard and other suspects - Tutorial 2

In this issue i will tell you how to add transparency and how to move your
objects around.
For this we need a graphic, preferable 32x32 with 24 bit colors.
Rip somewhere or make your own, it wont matter.
Now, get one information, what will be the transparency.
I prefer "Magic Pink" which is 255,0,255 , as long as you know which color
number your transparency color has it will work.

Now, you load your image/sprite/whatever with IMG_Load as usual and after that you just do the
following:
Code:
SDL_SetColorKey(YourSpriteSurface, SDL_SRCCOLORKEY, SDL_MapRGB(YourSpriteSurface->format, 255, 0, 255))
The last three numbers represent your transparency color.
Now everytime you blit your image you will get a nice image with transparent
areas as SDL now does the rest for you, cool eh?

For exercise, add a small sprite to your first SDL programm that has
transparency.

So, it should look somewhat like this:
Code:
'$include: "SDL\SDL.bi"
'$include: "SDL\SDL_image.bi"

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

''' NEW CODE STARTS HERE!
' This will hold our sprite
dim shared pic as SDL_Surface ptr

' This will be explained later, its for telling SDL where to draw ;)
dim shared rect as SDL_Rect

''' NEW CODE STOPS HERE!


' 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.
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
temppic = IMG_Load("testpic.png")
' If this is empty then something went wrong
if temppic = 0 then
   print "Could not load file: testpic.png"
   end 3
end if

''' NEW CODE STARTS HERE!

'We load our sprite into memory
pic = IMG_Load("testsprite.png")
' If this is empty then something went wrong
if pic = 0 then
   print "Could not load file: testsprite.png"
   end 3
end if

' Now we set the transparency
SDL_SetColorKey(pic, SDL_SRCCOLORKEY, SDL_MapRGB(pic->format, 255, 0, 255))

''' NEW CODE STOPS HERE!

' We blit the image to the screen.
SDL_BlitSurface(temppic,NULL,video,NULL)

''' NEW CODE STARTS HERE!

' This will draw our sprite with transparency on the coordinates stated
rect.x = 50
rect.y = 150
' SDL_Rect needs to be passed as a pointer! You dont need to fully understand
' what a pointer is, just do it otherwise it wont work ;)
SDL_BlitSurface(temppic,NULL,video,@rect)
''' NEW CODE STOPS HERE!


' 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:
         exit do
      ' Has a mousebutton been pressed?
      case SDL_MOUSEDOWN:
         exit do
      ' In both cases we just get out of here
   end select
loop

' We have finished
SDL_Quit()

Ok, lets get on with that mysterious SDL_Rect...
This dude is a structure that holds 4 variables.
x and y for upper left corner of drawing and we have w for widht and h for
height. The last two are not interesting for the pasting part, but when you use
one big image for, for example, all enemy spaceships, you can select what
portion of your image is blited to the screen.

But now we go on on using that dude for moving something around the screen.

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

declare sub updatescreen()

' This will hold the keyboard data
dim keys as Uint8 ptr

' We need to know if we need to redraw the screen
dim shared redraw%

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

' This will hold our sprite
dim shared pic as SDL_Surface ptr

' This will be explained later, its for telling SDL where to draw ;)
dim shared rect as SDL_Rect

' 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.
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
temppic = IMG_Load("testpic.png")
' If this is empty then something went wrong
if temppic = 0 then
   print "Could not load file: testpic.png"
   end 3
end if

'We load our sprite into memory
pic = IMG_Load("testsprite.png")
' If this is empty then something went wrong
if pic = 0 then
   print "Could not load file: testsprite.png"
   end 3
end if

' Now we set the transparency
SDL_SetColorKey(pic, SDL_SRCCOLORKEY, SDL_MapRGB(pic->format, 255, 0, 255))


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:
      
''' NEW CODE STARTS HERE

         ' After this
         redraw%=1
         ' Grab the state of the keyboard
         keys = SDL_GetKeyState(NULL)
         ' Test all interesting possibilities with boundary check.
         ' It wont matter if you draw outside the screen, SDL clips for you
         if keys[SDLK_UP] and rect.y > 0 then rect.y = rect.y - 1
         if keys[SDLK_DOWN] and rect.y < 568 then rect.y = rect.y + 1
         if keys[SDLK_LEFT] and rect.x > 0 then rect.x = rect.x - 1
         if keys[SDLK_RIGHT] and rect.x < 768 then rect.x = rect.x + 1
         if keys[SDLK_ESCAPE] then exit do

''' NEW CODE STOPS HERE

      ' Has a mousebutton been pressed?
      case SDL_MOUSEDOWN:
         print "MB pressed"
      ' In both cases we just get out of here
   end select

   if redraw% = 1 then
      redraw% = 0
      updatescreen()
   end if
loop

' We have finished
SDL_Quit()

''' NEW CODE STARTS HERE

' It is a good habid not coding everything into the message loop
' This sub updates the screen
sub updatescreen()
   ' Nowadays its normal updating the whole screen as the PC has no universal
   ' scrolling routines, neither does it come with native sprite support.
   ' Besides, with SDL even an old 486 can maintain 25-30 fps

   ' We blit the image to the screen.
   SDL_BlitSurface(temppic,NULL,video,NULL)
   ' We blit the sprite to the screen.
   SDL_BlitSurface(temppic,NULL,video,@rect)

   ' We flip
   SDL_Flip(video)
end sub

''' NEW CODE STOPS HERE

Thats it, this is what SDL mostly is about.

Now, for exercice, have a try and make a simple labyrinth game or a simple space
shooter with SDL.

The next tutorial will be about something YOU decide, the only thing i wont
do is SDL+OpenGL because i dont know OGL Wink

And i would be very happy if someone could proofread my bad english. Big Grin

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
Cool, it was really simple to understand, maybe Pete would be interested to include it on the next QB Express, more people would learn SDL. Soon there will be a lot of people using SDL with BASIC, i guess the lib authors never thought that would be possible ;) -- i know about sdlbasic, but it stills a wrapper, the lib is not used directly, anyways..
Reply
#3
Cool!!!
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#4
Quote:Cool, it was really simple to understand, maybe Pete would be interested to include it on the next QB Express, more people would learn SDL.

Thats why i wrote it, that people learn a good and universal libary.
Interesting thing now would be porting SDL_net to FB.
If you are interested in a mingw32 version, i did one some time ago as there is just a precompiled version for VC6 on windows by the author :roll:
http://nachtkatzen.de/sdl/SDL_net.mingw32.devel.zip

Quote:Soon there will be a lot of people using SDL with BASIC, i guess the lib authors never thought that would be possible Wink


Lets wait for the first FBSDLRPG Tongue

Quote: -- i know about sdlbasic, but it stills a wrapper, the lib is not used directly, anyways..


AFAIK that one is just an interpreter, so it sucks anyway Wink
Thats just the thing why i use CC65 for coding on the C64 and not internal BASIC 2.0... besides, i dont like linenummbers too.
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
I thought SDL_net was already ported...?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)