Dark Bit Factory & Gravity
PROGRAMMING => Other languages => Blitz => Topic started by: TinDragon on September 27, 2006
-
Ok here is my first attempt at getting a opengl based framebuffer lib working, it seems to be functioning as expected so I will attempt to convert a couple of examples over from Freebasic.
First the include. Save it as bmaxptc.bmx
'-----------------------------------------------------------------------------------------------------
'
' OpenGL Frame Buffer Lib By Joncom2000 of The Tin Dragons
'
' Based on code by Jim Shaw & Rbaz, Inspired by TinyPTC by Gaffer.
'
'-----------------------------------------------------------------------------------------------------
'----------------------------------------------------------------------------------------------
' Globals used by the lib to store screen size
'----------------------------------------------------------------------------------------------
Global g_w:Int
Global g_h:Int
'-----------------------------------------------------------------------------------------------------
' Place Functions after this
'-----------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------
' NAME : ptc_open()
' PURPOSE : This function opens the display
' INPUTS : Title, width and height for screen Also hertz
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_open(Title$,width=640,height=480,Hertz=60)
'Set globals
g_w=width
g_h=height
'open screen
GLGraphics(g_w,g_h,32,Hertz, GRAPHICS_BACKBUFFER)
AppTitle$=Title$
glViewport(0, 0, g_w,g_h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0,g_w,g_h,0,0,1)
glPixelZoom(1,-1)
glRasterPos2i(0,0)
End Function
'------------------------------------------------------------------------
' NAME : ptc_update()
' PURPOSE : This function updates the display
' INPUTS : pixel buffer pointer
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_update(pBuffer:Byte Ptr)
glDrawPixels(g_w,g_h,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pBuffer)
Flip
End Function
'------------------------------------------------------------------------
' NAME : ptc_close()
' PURPOSE : This function closes the display
' INPUTS : none
' RETURNS : update
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_close()
' Doesnt seem needed but I might add cleanup code at some point if needed
End Function
And a small demo of how to use it, note that I am using an array to store the buffer info it may work with other types of storage like banks but I have not tested that as I like to keep things simple :)
'-----------------------------------------------------------------------------------------------------
'
' The Tin Dragons - OpenGL Frame Buffer test By Joncom2000
'
'-----------------------------------------------------------------------------------------------------
Include "bmaxptc.bmx"
ptc_open("Test",640,480,60)
Global pixels:Int[640*480]
Repeat
For p=0 To 640*480
col=Rand(255)
pixels[p]=col Shl 16+col Shl 8 + col
Next
ptc_update(Varptr(Pixels[0]))
Until KeyDown(KEY_ESCAPE)
End
Oh and one final note, the info is passed to opengl in BGRA format, not the RGB format that most blitzers will be use to but this is to keep the lib inline with the one developed by Jim & Rbaz for use with FB. Also the ptc_update function automaticly flips the screen so none of the standard gl commands will work with it. This can all be changed should anyone want to or ask me if you want a custom version and I will see what I can do ;)
-
Here's a Rotozoom style example for the framebuffer lib.
'-----------------------------------------------------------------------------------------------------
'
' Rotozoom Example for framebuffer lib
'
'-----------------------------------------------------------------------------------------------------
Include "bmaxptc.bmx"
ptc_open("Rotozoom",640,480,60)
HideMouse
Global pixels:Int[640*480]
Repeat
ang#=ang#+0.5
c=750*Cos(ang#)
xo=200+200*Sin(ang#*2)
yo=200+200*Cos(ang#*3)
For y=0 To 479
For x=0 To 639
i=x+xo-320
j=y+yo-240
col=((i+j*c/256) & 255) ~ ((j-i*c/256) & 255)
pixels[x+(y*640)]=col
Next
Next
ptc_update(Varptr(Pixels[0]))
Until KeyDown(KEY_ESCAPE)
End
Enjoy and I will attach an exe for those none bmax owns later.
-
Looking forward to see the exe files...
-
I am one of those non bmax owners so I'll look forward to seeing the exes :) Nice work Jon.
-
For some reason I cant get the code to work when I try using a framework to reduce the amount of stuff included in the exe, resulting in it currently producing bloated exe's only. Not sure why this is yet as I have not had any problems like this before ???
Which means I cant attach the exe as the file size is to big and I dont currently have any webspace to host files on.
-
Looks cool Joncom, will try it out once I've re-installed BMAX.
Thumbs up,
Clyde.
-
Good stuff TinDragon! Sorry I can't help with your bmax problem.
If you change
glDrawPixels(g_w,g_h,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pBuffer)to
glDrawPixels(g_w,g_h,GL_RGBA,GL_UNSIGNED_BYTE,pBuffer)it'll convert the pixels the other way round. If that's more convenient?
Jim
-
No worries Jim, I think I just have to work out which include lib I am missing and it will work I hope :D
As for the rgb, bgr order, well I left the code I posted that way round as it matched the code in the lib you and rbaz posted and I thought it might make it easier to convert routines between the languages should anyone want to and I suspect a couple people might. Once I can workout the import problem I can hopefully make better size exe's rather than 440kb compressed.
Myself I might look to do some stuff in C/C++ using this idea as I need to brush up as I havent done any in ages :)
-
The upload limit is 750kb at the moment mind Jon.
I haven't changed it back to 250kb yet :)
-
@Shockwave, Didnt know that but to be honest I dont think it's worth wasting the bandwidth on the exe's since there not that impressive (yet ;) ) and hopefully I can work out whats gone wrong with the framework thing :)
-
I took at look at that framework problem last night, but couldn't crack it either. Just getting a black screen with nothing going on. It doesn't crash or anything. I will probably give it another try during the weekend.
-
Do you need to tell BMAX which rendering method you want to use; DirectX or OGL?
Dont know it thats of any use to you.
-
I tried the first code which compiles and links fine but I get this error
Unhandled Exception:Attempt to index array element beyond array length upon execution.
Building demo
Executing:demo.debug
Unhandled Exception:Attempt to index array element beyond array length
I tried TinDragon's Rotozoom style example code but also just get a black screen when executing.
-
It works fine on my setup. I am using the latest v1.22 of bmax, what version are you trying it with?
-
Global pixels:Int[640*480]
...
For p=0 To 640*480
In both C and Freebasic, that's one array entry too many. Is that the problem here?
Jim
-
well the rotozoom doesn't do it that way Jim so it cant be the error with that one but it probably should generate an error in the first code :-[ But it actually runs fine on my system and zawrans which is strange.
@Rootuid, are you trying to use a framework or just letting Bmax include everything as the black screen I only get when trying to reduce the imports using a framework ?
[Edit] Oh I just realized I am not building it in debug mode, turn that on and the first code does give an error. here's a qucik fix ;)
'-----------------------------------------------------------------------------------------------------
'
' The Tin Dragons - OpenGL Frame Buffer test By Joncom2000
'
'
'-----------------------------------------------------------------------------------------------------
Include "bmaxptc.bmx"
ptc_open("Test",640,480,60)
Global pixels:Int[640*480]
Repeat
For p=0 To (640*480)-1
col=Rand(255)
pixels[p]=col Shl 16+col Shl 8 + col
Next
ptc_update(Varptr(Pixels[0]))
Until KeyDown(KEY_ESCAPE)
End
-
I only tried the rotozoom one, sorry, but that works at least. The other one would probably crash in release mode though.