PDA

View Full Version : Sdl Library



peter
19-04-2013, 19:56
Hi Eros,


Here is a Sdl Library for ThinBasic. Would be nice, if it runs with you.
It isn't finish yet, but some small works are possible.


As ever, no problems here with my computer, anything runs fine.

Demo1:


#INCLUDE "sdl.inc"


LoadWinIcon "img/acorn.bmp"
Screen 640,480,1
SetCaption "Hello Banana"


Long x, fnt, toy


Fnt = LoadFont "sdlfonts/choco.ttf",48
toy = LoadImage "img/toy.gif",1


While KEY(27)=0
ClsColor xRgb 0,0,255
SetText fnt,20,0,"Hello, my name is Peter.",xRgb(255,255,255)
Sprite toy,200,100,0
For x=0 To 639 Step 10
SetPoint x,400,4,6, Rgb(0,255,0)
Next
Rectangle 200,420,80,40,4,xRGB(255,200,0)
FillRect 300,420,80,40, xRGB(155,100,255)
Ellipse 440,440,40,20,4,xRGB(255,10,25)
Ellipse 520,440,20,20,2,xRGB(255,110,85)
DrawLine 0,64,639,64,2, xRGB(200,20,255)
Sync
Wend
Quit


Demo2:


#INCLUDE "sdl.inc"


Screen 320,240,1
SetCaption "FORMAT TEST"


Long zx, zy, bild, tizzi, angel, stars
bild = LoadImage "img/bild.jpg",1
tizzi= LoadImage "img/tizzi.png",1
angel= LoadImage "img/angel.bmp",1
stars= LoadTiles "img/star.tga",16,2

While Key(27)=0
Sprite bild,0,0,0
Sprite tizzi,32,0,0
Sprite angel,32,0,0
Tiles stars,0,0,zx,zy
Tiles stars,280,200,zx,zy
Sync()
zx +=1
If zx=16 Then
zx=0
zy +=1
If zy=2 Then zy=0
End If
Wend

Quit()




Demo3:


#INCLUDE "sdl.inc"


Screen 400,300,1
SetCaption "MANDEL DRAGON"


Double cRe,cIm,re,im,oldRe,oldIm,zoom,mx,my
Long maxIt,x,y,i,a,w,h,fnt
fnt = LoadFont "sdlfonts/font1.ttf",32


SetText fnt,100,100,"WAIT......", xRGB 255,255,255
Flip


maxIt= 255
zoom = 1
cRe = -0.7
cIm = 0.27015
w = GetWidth
h = GetHeight


For x=0 To w
For y=0 To h
re = 1.5 * (x-w*.5) / (.5*zoom*w) + mx
im = (y-h*.5) / (.5*zoom*h) + my
For i=0 To maxIt
oldRe = re
oldIm = im
re = oldRe * oldRe - oldIm * oldIm +cRe
im = 2 * oldRe * oldIm + cIm
If((re * re + im * im) >5) Then Exit For
Next
SetPoint x, y, 1,1, xRGB(i*7,i*8,i*4)
Next
Next


KeyWait()
Quit


Demo4:


#INCLUDE "sdl.inc"


Screen 400,400,1
SetCaption "MoonPlasma"


Long m1,m2,m4,m11,m22,y,x,r,g,b


For y=0 To 399
m11= m11+1
m1 = m1 +9
m2 = m1
m22= m11
For x=0 To 399
m4 = m4 +19
m2 = m2 +15
m22= m22+1
r = Abs(Sin(m1/100)+Sin(m2/100)+Sin((m1+m4)/600))*(255/3)
g = Abs(Sin((m22)/50) + Sin(m11/100))*250
b = Abs(Sin(x*y/100000)*200)
Setpoint x,y, 1,1,xRgb(r,g,b)
Next
Next


KeyWait
Quit


Demo5:


#INCLUDE "sdl.inc"


Single Pii= 3.141592654


Screen 640,480,1
SetCaption "Sierpinski Snowflake"


Function degrees(radians As Long) As Long
Return radians * Pii/180
End Function


Single x, y, c, r2, r3, a, x0, y0, u, v
Long ex, sx, sy, i


x=1 : y=0 : c=0.5 / Sqr(3) : r2= Sqr(3) /2 : r3= Sqr(3) /6 : ex=400 : sx=240 : sy=125


ClsColor xRGB(100,60,240)
For i=0 To 35000
a = Sin(degrees(Rand(0,45)))
If a > 0.5 Then
x0 = (0.5 * x) + (r3 * y)
y0 = (r3 * x) - (0.5 * y)
Else
x0 = (0.5 * x) - (r3 * y) + 0.5
y0 = (-r3 * x) - (0.5 * y) + r3
End If
x=x0
y=y0
SetPoint(x * (ex + sx), sy - (y * ex),4,2, Rgb 255,255,255)
u = (0.5 * x) - (r2 * y)
v = (-r2 * x) - (0.5 * y)
SetPoint(u * (ex + sx), sy - (v * ex),4,2, Rgb 250,250,250)
u = (-0.5 * x) + (r2 * y) + 1
v = (-r2 * x) - (0.5 * y)
SetPoint(u * (ex + sx), sy - (v * ex),4,2, Rgb 245,245,245)
Next


KeyWait
Quit


Demo6:


#INCLUDE "sdl.inc"


Screen 680,512,1
SetCaption "SNAKE"




Long px, j, tree, ball, weed
Dim x(10) As Long
Dim y(10) As Long
Dim d(10) As Long


tree = LoadImage("snake/tree.bmp",1)
ball = LoadImage("snake/ball.bmp",1)
weed = LoadImage("snake/weed.bmp",1)


For j=1 To 10
x(j) = j*32
y(j) = 0
d(j) = 1
Next


px=32


While Key(27)=0
ClsColor (0)


For j=1 To 10
Sprite ball,x(j),y(j),0
Next


For j=1 To 10
If d(j)=1 Then
If x(j) >0 Then
x(j) = x(j) -1
Else
d(j) =2
End If
End If
Next


For j=1 To 10
If d(j)=2 Then
If y(j) <480-32 Then
y(j) = y(j) +1
Else
d(j)=3
End If
End If
Next


For j=1 To 10
If d(j)=3 Then
If x(j) <640-32 Then
x(j) = x(j) +1
Else
d(j)=4
End If
End If
Next


For j=1 To 10
If d(j)=4 Then
If y(j) >0 Then
y(j) = y(j) -1
Else
d(j)=1
End If
End If
Next


Sprite tree,200,232,0
Sprite weed,0,464,0


Flip
SetFps (260)
Wend
Quit

peter
19-04-2013, 20:01
Demo7:


#INCLUDE "sdl.inc"


Screen 640,480,1
SetCaption "Spring Flower"


ClsColor 0
Single angle, cx, cy, r, na, pii
Long i, x, y
angle=1 : cx=320 : cy=240 : r=50 : Pii= 3.141592654


For i=0 To 20100
r=Sin(angle/100) * 200
na=((r*2*pii)/3600) * angle
x =(Cos(na / r)*r)+cx
y =(Sin(na / r)*r)+cy
SetPoint x,y,4,8, Rgb 255,200,150
SetPoint x,y,1,4, Rgb 255,255,255
angle += .5
Next


KeyWait
Quit


Demo8:


#INCLUDE "sdl.inc"


Screen 320,360,1
SetCaption "Sort Sequence"


Dim a(10) As Long
Long i, tmp, j, p, fnt


fnt = LoadFont "sdlfonts/font3.ttf",18


ClsColor (0)
For i=1 To 10
a(i) = Rand(1,500)
SetText fnt,0,i*14,"Random Number ",xRGB 255,255,255
SetText fnt,224,i*14,a(i), xRGB 247,187,154
Next
SetText fnt,0,i*14,"____________________",xRGB 0,255,55
p=i*17


For i=1 To 10
For j=i To 10
If a(j) < a(i) Then
tmp=a(i)
a(i)=a(j)
a(j)=tmp
End If
Next
Next


Demo9:


#INCLUDE "sdl.inc"
Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long


Screen 320,240,1
SetCaption " PING PONG PANG"


Long fnt,x, y, x2, y2, pspeed, xadj, yadj, delay, score
fnt = LoadFont "sdlfonts/font1.ttf",38


x=50 : y=50 : x2=130 : y2=150 : pspeed=2 : xadj=2 : yadj=2 : delay=1

While key(27)=0
ClsColor xRGB 255,255,255
Ellipse x, y, 2, 2, 4, 0
FillRect x2, y2, 30, 4, 0
SetText fnt,10,4, "SCORE " & score,0
If y <=20 Then
yadj = 1
PlaySound "wav/shot.wav",0,1
End If
If y >=180 Then
yadj = -1
PlaySound "wav/shot.wav",0,1
End If
If x >=300 Then
xadj = -1
PlaySound "wav/shot.wav",0,1
End If
If x <=20 Then
xadj = 1
PlaySound "wav/shot.wav",0,1
End If
If key(276) And x2 > 1 Then x2=x2-pspeed
If key(275) And x2 <290 Then x2=x2+pspeed
x = x + xadj
y = y + yadj
If y > y2-7 And y2 < y2+2 And x < x2+30 And x > x2 Then
yadj = -1
score= score +1
End If
If y > y2+10 Then
SetText fnt,80,200,"GAME OVER!",0
PlaySound "wav/haha.wav",0,1
While Key(32)=0
Sync
Wend
score=0
x=50:y=50:x2=130:y2=150
End If
Sync
Wend
Quit


Demo10:


#INCLUDE "sdl.inc"


Screen 800,600,1
SetCaption "SDL STARS"


Type Star
xStar As Long
yStar As Long
speed As Long
End Type


Dim Stars(800) As Star
Long xRes, yRes
xRes =800
yRes =600


Function InitStars()
Local i As Long
For i=1 To 800
Stars(i).xStar = Rand(1, xRes -2)
Stars(i).yStar = Rand(1, yRes -2)
Stars(i).speed = Rand(1, 4)
Next
End Function


Function MoveStars()
Local i As Long
For i=1 To 800
SetPoint Stars(i).xStar,Stars(i).yStar,Rand(2,4),2, xRGB(Rand(64,255),200,255)
Stars(i).xStar = Stars(i).xStar + Stars(i).speed
If Stars(i).xStar > xRes Then
Stars(i).xStar = 0
Stars(i).yStar = Rand(1, yRes -2)
Stars(i).speed = Rand(1, 4)
End If
Next
End Function


InitStars()


While Key(27)=0
ClsColor 0
MoveStars
Flip
SetFps (80)
Wend
Quit

peter
19-04-2013, 22:03
Yes, very hard and unnecessary. Looks like ScriptBasic. :D
This Sdl isn't really a good choice, I think it's a little bit too slow.

I wrote a test game and got 23 frames, compared with OpenGl, I got 120 frames !
And SimpleWindow brought 120 frames too, but isn't so smooth like OpenGl.

peter
20-04-2013, 00:21
OopsGL
LOL :dance1:

Billbo
20-04-2013, 19:01
Peter,

First, do I put the sdl.inc in the inc directory and the dll's in the lib directory?
Second, where do I put the LittleThings directory? I see that the examples
above use items in the LittleThings directory, I just want to get it in the right
place.

Thanks,

Bill

peter
20-04-2013, 19:21
Hi Billbo,

Put everything in your ThinBasic directory!

peter
20-04-2013, 19:23
Peter,

Have you tried getting your Simple Window library working with thinBasic? You already have a bunch of examples that should port from O2 to thinBasic with ease.

John

No John, I think in this forum isn't much interest in such a library.

Billbo
20-04-2013, 19:45
Peter,

I did as you indicated. But when I try to execute any of
the abobe scripts, thinBasic locks up and stops running.

Bill

peter
20-04-2013, 20:28
Hi Bill,

What is "abobe scripts" ?

Bill, if you cut and paste the source code, do you save it as name.tbasic ?

Billbo
20-04-2013, 23:05
Peter,

If you notice the "b" is next to the "v" on the keyboard.
It's suppose to be"above". Meaning your examples on
page 1. It was just the first page when I typed the reply.

What I did was just paste into thinAir and execute. I'll
try your way.

Thanks,

Bill

P.S. I made all 10 with an .tbasic extention. All ran,
except the "FORMAT TEST" locked up as before, and
the "Sort Sequence" just flashes and disappears. I
really like the "Snake".

peter
21-04-2013, 00:48
Locks up Fromat test !

Bill, do you have all the graphics also in your thinbasic with its originally directories ?
The Graphics must be in this directory, which you have downloaded.
Do nothing into thinAir !

Billbo
21-04-2013, 03:21
Peter,

I have "LitteThings" and its four sub directories in a directory
directory named LitteThings. I also have the subdirectories
separately in thinBasic's root dir. I also put the SDL files in
the inc and lib dir's. Like I indicated, 8 of the 10 run fine.

Bill

ReneMiner
21-04-2013, 09:50
I ran all your demos - all runs smooth and fine but there are two of them demos which make some problem:
Demo 2 instantly crashes (including windows lamenting thinBasic won't work any more)
in Demo 8 window opens & closes immediately - nothing happens.

All others run fine.

Now just question of usage and what possibilities that offers:
Would it be possible to open "screen" inside UI-Canvas or is there another way to display multiple "screen"s (or viewports) in one or more windows?

peter
21-04-2013, 12:39
Hi Bill,

I also put the SDL files in the inc and lib dir's.

Everything have to lie directly in the thinBasic directory.
Also the Graphics sub-directories.

peter
21-04-2013, 12:47
Hi ReneMiner,

Would it be possible to open "screen" inside UI-Canvas or is there another way to display multiple "screen"s (or viewports) in one or more windows?

No.

peter
21-04-2013, 12:53
Hi John,

I'm beginning to think interpreters don't like you.

Not all! :D

ErosOlmi
21-04-2013, 12:58
Peter,

I just tested your new library and the 10 script examples.
Except demo 2, all other scripts are working fine. No problem at exit when firing the QUIT command like in previous library.
THANKS A LOT, great present.


Regarding Demo 2, for some reason it GPF when executing the first LoadImage command:

bild = LoadImage "img/bild.jpg", 1
In all other examples, LoadImage has no problems.
Maybe .jpg format is not ok.

Ciao
Eros

peter
21-04-2013, 14:53
Hi Eros,

No bug with jpg, nowadays is everthing mysterious. :(

here is a test.


#INCLUDE "sdl.inc"


Screen 800,600,0


Long star, zStar, x, y


star = LoadImage "img/image1.jpg",14


While Key(%SDLK_ESCAPE)=0
ClsColor (0)
For y=0 To 600 Step 64
For x=0 To 800 Step 64
Sprite star,x,y,zStar
Next
Next

zStar +=1
If zStar=14 Then zStar=0
Sync
SetFps (14)
Wend
Quit

Billbo
21-04-2013, 17:23
Peter,

I downloaded your image file and ran it. The
screen shrunk in from the sides, went to
640x480 (?), and AVG popped up with a
virus. I had to reboot, because I could not
do anything. I'm back to normal now.

I cannot afford to lose what I have on this
computer, even though I backup most files
to a 2 TB external drive. So, as far as this
this project goes, I'll stick with what I have.

Bill

peter
21-04-2013, 17:53
Hi Bill,

Sorry, for circumstances!

There is no virus at all. AVG gives false alarm!
Don't worry about that. It's okay when you avoid this library.

p.s
I didn't make the fullscreen, that is a part of the sdl.dll. Here at me, I have a normal fullscreen without shrink.

matthew
14-05-2013, 14:01
Nice library Peter :-)

For anyone having difficulties running example 2, it appears to depend upon libjpeg-8.dll and libpng15-15.dll which can be found here (http://www.libsdl.org/projects/SDL_image/) in SDL_image-1.2.12-win32.zip. The SDL_image dll by itself doesn't contain the necessary information to deal with those formats sadly.

Also Peter is some of the code missing for example 8? I had to put the following at the end...

KeyWait
Quit

I would assume that the program is supposed to sort the random numbers and then display them on the screen which it currently doesn't do. :-(

peter
14-05-2013, 16:38
Thanks Matthew.

Other Dll's are not necessary, as you can read here.

SDL_image is an image loading library that is used with the SDL library, and almost as portable. It allows a programmer to use multiple image formats without having to code all the loading and conversion algorithms themselves.

And here something fun for old friends
I hope that you have hold the Library for another exciting adventures.


#INCLUDE "sdl.inc"

SDL_Window 640,590,1

Long i,snk,pln
Dim x(4) As Long
Dim y(4) As Long
Dim z(4) As Long
Dim t(4) As Long

snk=SDL_LoadImage "png/guy.bmp",1
pln=SDL_LoadImage "png/plant.bmp",1

x(1)=0
x(2)=40
x(3)=80
x(4)=120

Sub CircleSnake()
SDL_Sprite(snk,x(1),y(1),0)
SDL_Sprite(snk,x(2),y(2),0)
SDL_Sprite(snk,x(3),y(3),0)
SDL_Sprite(snk,x(4),y(4),0)
End Sub

While SDL_key(27)=0
SDL_Cls SDL_RGB(0,0,255)
For i=1 To 4
If z(i)=0 Then
x(i)=x(i)+1
If x(i) >=640-64 Then
z(i)=1
End If
End If
Next
For i=1 To 4
If z(i)=1 Then
y(i)=y(i)+1
t(i)=t(i)+1
If t(i)=40 Then
t(i)=0
z(i)=2
End If
End If
Next
For i=1 To 4
If z(i)=2 Then
x(i)=x(i)+1*(-1)
If x(i) <=0 Then
z(i)=3
End If
End If
Next
For i=1 To 4
If z(i)=3 Then
y(i)=y(i)+1
t(i)=t(i)+1
If t(i)=40 Then
t(i)=0
z(i)=0
End If
If y(i)>=SDL_GetHeight()-64 Then
z(i)=4
End If
End If
Next
For i=1 To 4
If z(i)=4 Then
y(i)=y(i)-1
If y(i)=0 Then
z(i)=0
t(i)=0
End If
End If
Next

CircleSnake
SDL_Sprite pln,0,0,0
SDL_Redraw
SDL_SetFps 260
Wend
SDL_Quit