Qbasicnews.com
October 22, 2019, 03:05:30 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: TUT : SDL,FreeBASIC,transparency,keyboard and other suspects  (Read 7165 times)
BastetFurry
Forum Regular
**
Posts: 136



WWW
« on: February 09, 2005, 04:06:47 PM »

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. Cheesy

So Long, The Werelion!
Logged

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]
v3cz0r
I hold this place together
*****
Posts: 924



WWW
« Reply #1 on: February 10, 2005, 01:46:53 AM »

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..
Logged

relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #2 on: February 10, 2005, 03:43:55 AM »

Cool!!!
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
BastetFurry
Forum Regular
**
Posts: 136



WWW
« Reply #3 on: February 10, 2005, 05:00:47 AM »

Quote from: "v3cz0r"
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 from: "v3cz0r"
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 from: "v3cz0r"
-- 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.
Logged

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]
Z!re
*/-\*
*****
Posts: 4599


« Reply #4 on: February 10, 2005, 07:37:51 AM »

I thought SDL_net was already ported...?
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!