1 Attachment(s)
FBGFX module for thinBasic, to handle 2D graphics
Hi,
I started developing FBGFX right away, when Eros gave me the full source to it.
I've now added few commands to it, and it works nicely. :)
Here's an example code what it looks like:
[code=thinbasic]'**********************
'** FBGFX Example #1 **
'**********************
uses "FBGFX"
FBGFX_ScreenRes(320,240,32) 'Create FBGFX-window (width, height, dept)
dim t
dim x
dim y
dim c
while FBGFX_InKey() <> "q" 'Program runs until "q" is pressed
x = 160 + sin(c) * 40
y = 120 + cos(c) * 40
c = c + .01
FBGFX_Pset(x,y,FBGFX_RGB(255,255,255)) 'This function draws pixel on the screen
FBGFX_Circle(x,y,16,FBGFX_RGB(255,50,50)) 'This draws circle ( who would've known ;o )
FBGFX_Circle(x,y,32,FBGFX_RGB(255,0,255))
FBGFX_Sync(1) 'Syncronize screen
FBGFX_Cls() 'And then clear it
wend[/code]
Developing this is so wonderful! And if someone takes it away from me, I will die! :<
Just love making this! ^^
And I just wanted to make own topic for this, so sorry if it disturbes you. :(
FBGFX module is attached in this post.
Thanks,
misthema.
Re: FBGFX module for thinBasic, to handle 2D graphics
Perfect mistema. Nice job!
A little note. In thinBasic, if you do not define a specific type for variable declaration, variable is defined as VARIANT. VARIANTs are very powerful kind of data because they can store almost anything but are also quite slow due to the time needed to manage thir complexity. So you can get a little speed improvement declaring variable with type like:
[code=thinbasic]dim t as double
dim x as double
dim y as double
dim c as double[/code]
Ciao and thanks again.
Eros
Re: FBGFX module for thinBasic, to handle 2D graphics
All of you creating these fantastic modules, thanks. My hats off to you and the work you guys are doing!
Re: FBGFX module for thinBasic, to handle 2D graphics
Thanks misthema,
you are fast, hope you enjoy being parent of next thinBASIC module :)
New sample is nice and fast on my PC, good job!
Thanks,
Petr
Re: FBGFX module for thinBasic, to handle 2D graphics
good work.
Perhaps FBGFX_GraphicWindow would be a better name than FBGFX_screenres.
only thing i would add is some equates for screenres colour depth
%FBGFX_DEPTH16 = 16
%FBGFX_DEPTH24 = 24
%FBGFX_DEPTH32 = 32
and the sync could use
%FBGFX_SYNCON = 1
%FBGFX_SYNCOFF = 0
Just Ideas
Re: FBGFX module for thinBasic, to handle 2D graphics
Thanks everyone!
Good ideas Abraxas. I might do so. :) I'll let you know more as soon as possible!
Thanks again,
misthema.
Re: FBGFX module for thinBasic, to handle 2D graphics
Hi,
I'm a bit worried about the speed.. The next plasma-like example runs very slow at higher resolution on my computer.
Could you guys test this and say is it slow or not.
[code=thinbasic]'Plasma-like effect with FBGFX
uses "FBGFX"
Dim w,h As Double
w=200
h=150
FBGFX_ScreenRes(w,h,32,2)
Dim x,y,page As Double
Dim c,t As Double
while FBGFX_InKey() <> "q" 'Program runs until "q" is pressed
t=t+1
For x = 0 To w
For y = 0 To h
c= (Sin((x+y+t)/100)+Cos((x-t)/100)+Cos((y-t)/100)+Cos((x-y+t)/100))*2+Sin(t/100)*15
FBGFX_PSet(x,y,Rgb(Sin(c)*64+128,Cos(c)*64+128,Cos(-c/2)*64+128))
Next
Next
'pageflip
page=-page+1
FBGFX_Sync(1)
FBGFX_ScreenSet(page,-page+1)
wend[/code]
Thanks,
misthema.
Re: FBGFX module for thinBasic, to handle 2D graphics
try this
[code=thinbasic]
'Plasma-like effect with FBGFX
uses "FBGFX"
Dim w,h As Double
w=200
h=150
Dim T1, T2 As QUAD
'---Initialize hi resolution timer
HiResTimer_Init
FBGFX_ScreenRes(w,h,32,2)
Dim x,y,page As Double
Dim c,t As Double
'while FBGFX_InKey() <> "q" 'Program runs until "q" is pressed
T1 = HiResTimer_Get
t=t+1
For x = 0 To w
For y = 0 To h
c= (Sin((x+y+t)/100)+Cos((x-t)/100)+Cos((y-t)/100)+Cos((x-y+t)/100))*2+Sin(t/100)*15
FBGFX_PSet(x,y,Rgb(Sin(c)*64+128,Cos(c)*64+128,Cos(-c/2)*64+128))
Next
Next
T2 = HiResTimer_Get
MSGBOX 0, "Elapsed time in microseconds: " & FORMAT$(T2-T1, "#0")
'pageflip
page=-page+1
FBGFX_Sync(1)
FBGFX_ScreenSet(page,-page+1)
'wend
[/code]
Results
Laptop = 183700 microseconds
Work = 283810 microseconds
Re: FBGFX module for thinBasic, to handle 2D graphics
mistema,
we have always to remember that thinBasic is an interpreted language so big loops are not the strongest area.
In your FOR/FOR/NEXT/NEXT example you are making 30000 loops (200x150) so thinBasic engione is calculating (well parsing plus interpreting) 30000 times a match expression (c = ...) plus 30000 times the second line.
When we are in similar situations the best way is to see if some of the interpreted code can be generalized as Module Function (so compiled code).
For example:
[code=thinbasic]c= (Sin((x+y+t)/100)+Cos((x-t)/100)+Cos((y-t)/100)+Cos((x-y+t)/100))*2+Sin(t/100)*15[/code]
can be setup as modules function and exposed as a new keyword with 3 numeric param (x, y, t) or maybe other parameters
Let's say FBGFX_NewFunction1 function
Also the following
[code=thinbasic]Rgb(Sin(c)*64+128,Cos(c)*64+128,Cos(-c/2)*64+128)[/code]
can be a module function with c as param (or other params)
Let's say FBGFX_NewFunction2 function
So the final Code can be something
[code=thinbasic]
For x = 0 To w
For y = 0 To h
c= FBGFX_NewFunction1(x, y, t)
FBGFX_PSet(x, y, FBGFX_NewFunction2(c) )
Next
Next
[/code]
In this way the work the parser has to do is much less.
Of course, this is just a possible way.
Ciao
Eros
Re: FBGFX module for thinBasic, to handle 2D graphics
... or the calculation could be solved using Oxygen module ;)
Here is a decomposition of:
[code=thinbasic]
c = (Sin((x+y+t)/100)+Cos((x-t)/100)+Cos((y-t)/100)+Cos((x-y+t)/100))*2+Sin(t/100)*15
[/code]
to RPN set of operations:
Code:
x
y
+
t
+
100
/
SIN
x
t
-
100
/
COS
+
y
t
-
100
/
COS
x
y
-
t
+
100
/
COS
+
2
*
t
100
/
SIN
15
*
So only one step is necessary RPN -> ASM syntax, sadly I am not very good at floating point assembly, but should be doable :)
Petr