View Full Version : FBGFX module for thinBasic, to handle 2D graphics
misthema
26-03-2008, 22:51
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:
'**********************
'** 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
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.
ErosOlmi
26-03-2008, 23:23
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:
dim t as double
dim x as double
dim y as double
dim c as double
Ciao and thanks again.
Eros
All of you creating these fantastic modules, thanks. My hats off to you and the work you guys are doing!
Petr Schreiber
27-03-2008, 08:20
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
Michael Clease
27-03-2008, 10:19
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
misthema
27-03-2008, 10:56
Thanks everyone!
Good ideas Abraxas. I might do so. :) I'll let you know more as soon as possible!
Thanks again,
misthema.
misthema
28-03-2008, 13:31
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.
'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
Thanks,
misthema.
Michael Clease
28-03-2008, 13:55
try this
'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
Results
Laptop = 183700 microseconds
Work = 283810 microseconds
ErosOlmi
28-03-2008, 14:38
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:
c= (Sin((x+y+t)/100)+Cos((x-t)/100)+Cos((y-t)/100)+Cos((x-y+t)/100))*2+Sin(t/100)*15
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
Rgb(Sin(c)*64+128,Cos(c)*64+128,Cos(-c/2)*64+128)
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
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
In this way the work the parser has to do is much less.
Of course, this is just a possible way.
Ciao
Eros
Petr Schreiber
28-03-2008, 14:49
... or the calculation could be solved using Oxygen module ;)
Here is a decomposition of:
c = (Sin((x+y+t)/100)+Cos((x-t)/100)+Cos((y-t)/100)+Cos((x-y+t)/100))*2+Sin(t/100)*15
to RPN set of operations:
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
misthema
28-03-2008, 15:56
Hi,
thanks Eros. That might be better to do some of those functions straight to the module.
I still made it go faster (smaller loop):
'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 step 3
For y = 0 To h step 3
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_Color(Rgb(Sin(c)*64+128,Cos(c)*64+128,Cos(-c/2)*64+128), 0)
FBGFX_Circle(x,y,3)
Next
Next
'pageflip
page=-page+1
FBGFX_Sync(1)
FBGFX_ScreenSet(page,-page+1)
wend
Also you guys can suggest some functions that I'll put to the module.
Thanks everyone,
misthema.
ErosOlmi
28-03-2008, 16:02
eh :D
smaller loops are a trick. But I'm sure that with module functions you will get some real boost.
The key point to decide to have a module function is if that function can be enough general to be used also in other scripts.
If yes, than go with module function. You will have a better module and faster scripts.
Ciao
Eros
Michael Clease
28-03-2008, 17:19
change from double to single you dont need the precision should make it faster.
If you want some ideas just google 2d librarys and see what they do and recreate them.
Petr Schreiber
28-03-2008, 20:23
Hi,
I created new thread for assembler optimization of plasma code (http://community.thinbasic.com/index.php?topic=1643.msg11869#msg11869), to not break high-level structure of samples in this thread.
I did first optimization of one expression, just 1.3-1.5 faster - not bad sign for thinBASIC parsing engine :).
But it gives me 3FPS instead of 2, which is thing many would die for ... or at least someone ... hey ... anyone ? :D
Petr
ErosOlmi
28-03-2008, 20:45
And here my version with an external DLL written in Power Basic implementing the two functions I mentioned.
I get a speed incremented by 3/4 times the original speed.
Ciao
Eros
Cool demo Petr and Eros. The one in Eros's post is significantly faster than the copy and paste one from Petr's. In both however, I can't close the window in any standard way and have to use the TaskManager to kill the running app.
Other than that, those kind of nice smooth color changes always impress me!
ErosOlmi
29-03-2008, 08:54
Kent,
you have to press "q" key to close the window.
You can get it from the main WHILE/WEND loop.
Ciao
Eros
misthema
30-03-2008, 12:50
In both however, I can't close the window in any standard way and have to use the TaskManager to kill the running app.
I have to use the "q" key, becouse I haven't figured out how can I use ESCAPE in FBGFX_InKey function. Maybe I'll make it work with scancodes and make equates for them.
Bye,
misthema.
Michael Hartlef
30-03-2008, 13:27
Thank for this module. Is there a way to make it work with thinBasic windows?
misthema
30-03-2008, 16:23
Michael,
I don't think so, becouse FBGFX creates its own window and viewport where it draws everything.
Or did I miss what you ment?
Bye,
misthema.
Michael Hartlef
31-03-2008, 05:07
No, that is what I ment. Thanks.
Michael Clease
31-03-2008, 10:04
Mike from reading the FB docs it looks like its just a standard OpenGL window with some with some commands which probably generate somekind of managed displaylists.
TBGL should of been able to do this along time ago Petr :P only messing.
Petr Schreiber
31-03-2008, 11:09
Hi,
Abraxas, could you please make a post in TBGL / Suggest new features?
I use that to check where I should go, and although I must admit we were speaking about this possiblity, I simply forgot it.
So if you could make post specifying what you need from TBGL control, I can keep it in mind more easily
I did not know FreeBASIC 2D commands are OpenGL based, I thought it is just "soft" rendering to bitmap.
Thanks,
Petr
Michael Clease
31-03-2008, 18:00
Its not totally clear, it maybe a bit of both GL and some "soft" stuff.
If you have a look at the commands available they all seem possible in GL.
misthema
01-04-2008, 08:59
Hi,
reading Abraxas's posts, this module feels like it's useless. If TBGL is able to do what this module does, why should I develop it?
I've thought that I'll make new 2D module from other programming language or C++ library.
Maybe the ClanLib that Abraxas's suggested in other post, or allegro? They seem to be much more powerfull than this...
Petr Schreiber
01-04-2008, 09:53
Misthema,
no, no, no! Your module is very important!
The lines and points in OpenGL are possible, good to have them, but I would not be surprised if the performance would be lower ( in case of many entities ) than in your module.
Lines and points in TBGL/OpenGL break a bit the rasterization pipeline, so they are not a fast ones as they need special handling.
Also your FBGFX window seems to be persistent, TBGL one needs continuous redraws.
I think it will be good if you will continue your project!
Petr
misthema
01-04-2008, 10:00
Ok... Maybe I really should continue with this.. Thanks Petr. :)
Also there's an update coming soon! I've changed few command names and made more commands. :)
I'll let you know!
Thanks,
misthema.
Michael Clease
01-04-2008, 13:26
Dont stop the development of your module.
Even if it did the same functions as TBGL the freebasic version may be slightly faster at certain things.
I think its better to have more than 1 option to do graphics, If i was to look at DirectX(3d) and develope a module should I bother because opengl can do can do the same sort of functions, simple answer YES.
Come on I want to see this update, so carry on the good work.
ErosOlmi
01-04-2008, 14:46
Misthema,
maybe you was just making some experiment or maybe you was thinking to something more serious.
In any case, developing a thinBasic module give a great responsability because you are creating a kind of trust between you (the developer) and the users using your work. It is important to consider also the "others" in those situations. And it means: coding, listening to suggestions, being active in development, think at documentation, think to release source code or just compiled dlls. In other words: have a planning.
Again, it is your choice if to continue or not or just say "I was just making some experiment" or whatever. Up to you.
Just consider your motivations.
Ciao and good work.
Eros
Michael Hartlef
01-04-2008, 22:20
Again, thank you for all the work you did so far.
I totally agree with Eros. It's up to you. If you don't go further, then please release the source so other people can work on it if the need to or want to.
One thing that you also should be aware of, this comunity is small and so it can take some time, if ever, someone will release some work using your module. But that should not stop you from developing modules. With your work you support thinBasic as a whole and make it more powerfull. Just like Abraxas said, the more the better.
Michael
sandyrepope
02-04-2008, 01:28
I would first like to say that I think the module is a good idea. I would like to use it if you are willing to continue working on it. I do have a question about its further development.
Are there any plans for the module to be able to handle bitmaps? I was thinking that in a game using it that it would be good to be able to put bitmaps in the window and then move them around using the arrow keys.
I have a couple of ideas that sound like they would be workable using the module.
Thanks
Sandy
misthema
13-04-2008, 19:15
Hi,
I'm working with the bitmap handling at the moment. I'm trying to make it as simple as I can, so you don't have to work with it so much (it's really hard to handle bitmaps in FB (imo), so that's why I'm simpling it for you).
The developing has been a bit slow in these few weeks, 'coz I've started to play MMORPG (it really wastes my time, but it's easier to play than develop :D) I'm still working on the module and I'll let you know the news as soon as possible!
Thanks,
misthema.
Petr Schreiber
13-04-2008, 19:44
Thanks for the message,
don't worry about playing games too much, better to code with relaxed brain than to bite fingers in nervous state :)
Petr
Gaming is inspiration and should be considered work for us too, well maybe research ? :)
sandyrepope
30-04-2008, 15:32
I was wondering.....
Is the FBGFX module still being developed?
Thanks
Sandy
ErosOlmi
30-04-2008, 19:57
FBGFX is not an official module. I released FBGFX module as source code to show how to implement a thinBasic module using FreeBasic compiler. Everyone can take it and play with it as preferred.
Misthema took it and implemented further adding new functions.
Everyone can do the same. It can be a very good way to understand how to develop thinBasic modules. And maybe it can be the basis for more advanced modules.
Ciao
Eros
sandyrepope
01-05-2008, 00:18
FBGFX is not an official module. I released FBGFX module as source code to show how to implement a thinBasic module using FreeBasic compiler. Everyone can take it and play with it as preferred.
Where can I get a copy of the source?
Thanks
Sandy
ErosOlmi
01-05-2008, 02:04
http://community.thinbasic.com/index.php?board=105.0
http://community.thinbasic.com/index.php?topic=1215.0
You should also have a copy into your \thinBasic\SDK\SDK.zip file.
Ciao
Eros
sandyrepope
01-05-2008, 13:29
Eros, Thank you. I found it under \thinBasic\SDK\SDK.zip file.
Sandy
ErosOlmi
01-05-2008, 14:04
OK, perfect.
\thinBasic\SDK\SDK.zip file contains info and examples on how to implement thinBasic modules in various languages.
Once you will be more familiar with DLL creation, you will see that a thinBasic module is quite easy to be implemented.
Ciao
Eros