Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: Emil_halim on February 21, 2007
-
Ok , here it is the Amiga zowee demo and all stuff of Magic library for freebasic , I.e the include the lib the dll and the demo.
Download it from here
http://bcxdx.spoilerspace.com/FreeBasic/OgreMagic_fb.zip (http://bcxdx.spoilerspace.com/FreeBasic/OgreMagic_fb.zip)
But I have found disappoint thing , it is a little slow , I will test it with pure C++.
Any way , here is the source code of the demo
''------------------------------------------------------------------------------
''
'' Magic Library For Ogre
'' beta version 0.40
'' by Emil Halim
''
'' Name : zowee demo from amiga
'' Date : 21/02/2007
'' Purpose : Teaching you that how to
'' use Magic Library With
'' FreeBasic.
''------------------------------------------------------------------------------
#include once "windows.bi"
#include once "win/d3d9.bi"
#include once "MagicLibrary.bi"
''-----------------------------------------------------------------------------
'' GLOBALS
''-----------------------------------------------------------------------------
dim shared as HWND g_hWnd = NULL
dim shared as LPDIRECT3D9 g_pD3D = NULL
dim shared as LPDIRECT3DDEVICE9 g_pd3dDevice = NULL
dim shared as SpriteData ptr EcoImage
dim shared as SpriteData ptr LineImage
dim shared as SpriteData ptr RedRainImage
dim shared as SpriteData ptr BlueRainImage
dim shared as SpriteData ptr CodeImage
dim shared as SpriteData ptr TickImage
dim shared as SpriteData ptr TutImage
dim shared as BitmapFontData ptr Myfont
dim shared as string Msg
dim shared as integer MsgLen
dim shared as SoundData ptr song
''-----------------------------------------------------------------------------
'' PROTOTYPES
''-----------------------------------------------------------------------------
declare function WindowProc( byval hWnd as HWND, byval msg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) as LRESULT
declare sub init()
declare sub shutDown()
declare sub render()
''-----------------------------------------------------------------------------
'' Name: init()
'' Desc: Initializes Direct3D under DirectX 9.0
''-----------------------------------------------------------------------------
sub init( )
g_pD3D = Direct3DCreate9( D3D_SDK_VERSION )
dim as D3DDISPLAYMODE d3ddm
IDirect3D9_GetAdapterDisplayMode( g_pD3D, D3DADAPTER_DEFAULT, @d3ddm )
dim as D3DPRESENT_PARAMETERS d3dpp
clear( d3dpp, 0, len(d3dpp) )
d3dpp.Windowed = TRUE
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD
d3dpp.BackBufferFormat = d3ddm.Format
d3dpp.EnableAutoDepthStencil = TRUE
d3dpp.AutoDepthStencilFormat = D3DFMT_D16
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE
IDirect3D9_CreateDevice( g_pD3D, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, _
D3DCREATE_SOFTWARE_VERTEXPROCESSING, _
@d3dpp, @g_pd3dDevice )
'' Initial Magic Library here
InitialMagicLibrary(g_pd3dDevice,640, 480)
FmodInit()
ML_SetMediaDirectory("../MagicMedia/")
'/// creating Ecolizer
EcoImage = ML_CreateStaticSprite(1,128)
ML_StartDrawingToSprite(EcoImage)
ML_ClearDrawingArea(0)
clr = &Hffff0000
for h=0 to 128
if (h<64) then
ML_WriteRGBAChannels(0,h,clr)
clr+=&H400
else
ML_WriteRGBAChannels(0,h,&Hff00ff00)
end if
next
ML_StopDrawingToSprite(EcoImage)
'/// creating Line
LineImage = ML_CreateStaticSprite(1,11)
ML_StartDrawingToSprite(LineImage)
ML_ClearDrawingArea(0)
ML_WriteRGBAChannels(0,0,&HFF000000)
ML_WriteRGBAChannels(0,1,&HFF171722)
ML_WriteRGBAChannels(0,2,&HFF212131)
ML_WriteRGBAChannels(0,3,&HFF424252)
ML_WriteRGBAChannels(0,4,&HFF494959)
ML_WriteRGBAChannels(0,5,&HFF636373)
ML_WriteRGBAChannels(0,6,&HFF494959)
ML_WriteRGBAChannels(0,7,&HFF424252)
ML_WriteRGBAChannels(0,8,&HFF212131)
ML_WriteRGBAChannels(0,9,&HFF171722)
ML_WriteRGBAChannels(0,10,&HFF000000)
ML_StopDrawingToSprite(LineImage)
'/// creating Red Rainbow
RedRainImage = ML_CreateStaticSprite(1,110)
ML_StartDrawingToSprite(RedRainImage)
ML_ClearDrawingArea(&HFF000000)
clr=0
for h=0 to 55
ML_WriteRedChannel(0,h,clr)
clr+=&H4
Next
for h=55 to 110
ML_WriteRedChannel(0,h,clr)
clr-=&H4
Next
ML_StopDrawingToSprite(RedRainImage)
'/// creating Blue Rainbow
BlueRainImage = ML_CreateStaticSprite(1,255+16)
ML_StartDrawingToSprite(BlueRainImage)
ML_ClearDrawingArea(0)
clr=0
for h=17 to 128
ML_WriteBlueChannel(0,h,clr)
ML_WriteAlphaChannel(0,h,255)
clr+=&H2
next
for h=128 to 239
ML_WriteBlueChannel(0,h,clr)
ML_WriteAlphaChannel(0,h,255)
clr-=&H2
next
ML_StopDrawingToSprite(BlueRainImage)
CodeImage = ML_LoadStaticSprite("code.bmp")
TickImage = ML_LoadStaticSprite("amiga.bmp")
ML_SetSpriteColorKeyFromPoint(TickImage,0,0)
TutImage = ML_LoadStaticSprite("tut.bmp")
Myfont = ML_LoadBitmapFont("font/Font.bmp",16,16,10)
ML_SetSpriteColorKeyFromPoint(Myfont,0,0)
ML_SetActiveBitmapFont(Myfont)
Myfont = ML_LoadBitmapFont("font/Font.bmp",16,16,10)
ML_SetSpriteColorKeyFromPoint(Myfont,0,0)
ML_SetActiveBitmapFont(Myfont)
Msg = "Welcome Here.... this is a new 2D programming world so you will find that "
Msg += "there are more impressive work that you could produce with this Library..."
Msg += "you could mix 2D with 3D world... playing GIF animation... playing animated Image... "
Msg += "Fading in and out... doing wave Effects... doing 3D star field Effect... scrolling text... "
Msg += "using many fonts... !! please if you found the library is useful for you "
Msg += "so let me know.. see you later.. by......"
' MsgLen = strlen(Msg)
MsgLen = 400
song = ML_LoadSoundData("mod/SATELL.S3M",SongType)
ML_SetSoundVolume(song,250)
ML_SetSoundFreq(song,10)
end sub
''-----------------------------------------------------------------------------
'' Name: render()
'' Desc: Render or draw our scene to the monitor.
''-----------------------------------------------------------------------------
sub render( )
IDirect3DDevice9_Clear( g_pd3dDevice, 0, NULL, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER, _
D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 )
''
'' begin drawing
''
IDirect3DDevice9_BeginScene(g_pd3dDevice)
'' Magic Stuff
''
ML_PlaySound(song)
ML_UseOrthogonalView
ML_SetAlpha(1)
ML_SetColor(255,255,255)
ML_SetBlendMode(ALPHABLEND)
ML_SetSpriteTexture(BlueRainImage)
static as single y =0
y += 0.4
if(y>255) then y=0
for i=0 to 11
ML_DrawPartOfSprite(BlueRainImage,320,16.0*i+20,0,y,640,16)
next
ML_SetScale(1,1)
static as integer depth = false
static as single stheta=0.25,xx=320,n=0.85
if(depth=true) then
ML_SetSpriteTexture(TutImage)
stheta+=0.004*3
if(stheta>2.64) then stheta=0.5
xx+=n
if(xx>580) then n=-n : depth = false
if(xx<60) then n=-n
ML_DrawSprite(TutImage,xx,150+70*cos(stheta*2))
end if
ML_SetSpriteTexture(TickImage)
static as single tim,Rolx
tim += 0.01
Rolx += 0.8*sin(tim)
ML_DrawSprite(TickImage,240+Rolx,80)
if(depth=false) then
ML_SetSpriteTexture(TutImage)
stheta+=0.004*3
if(stheta>2.64) then stheta=0.5
xx+=n
if(xx>580) then n=-n : depth = true
if(xx<60) then n=-n
ML_DrawSprite(TutImage,xx,150+70*cos(stheta*2))
end if
ML_SetSpriteTexture(RedRainImage)
ML_SetScale(640,1)
ML_DrawSprite(RedRainImage,320,260)
ML_SetSpriteTexture(LineImage)
ML_SetScale(640,2)
ML_DrawSprite(LineImage,320,205)
ML_DrawSprite(LineImage,320,305)
ML_SetBlendMode(LIGHTBLEND)
ML_SetColor(0,50,200)
ML_SetScale(1,2)
ML_SetSpriteTexture(Myfont)
static as single a,sy(25),MsgX=640
MsgX -=0.6
if(MsgX < -4500) then MsgX=640
if(a >= 360-0.5) then
a = 0
else
a += 0.5
end if
for k=0 to 24
sy(k) = sin((a+15*k)*3.14159265/180)*25
next
for i=0 to MsgLen
ML_DrawChar(Msg[i],MsgX+i*10,16+240+sy(i mod 24))
next
ML_SetBlendMode(ALPHABLEND)
ML_SetColor(255,255,255)
ML_SetScale(1,1)
ML_SetSpriteTexture(EcoImage)
dim as single ptr sepc
sepc = ML_GetSpectrum()
for i=0 to 39
dim as single ey
ey = sepc[i] * 127 * 4
if (ey > 127) then ey = 127
ML_DrawPartOfSprite(EcoImage,38+15.0*i,445-ey/2,0,128-ey,14,ey)
next
ML_SetSpriteTexture(CodeImage)
ML_DrawSprite(CodeImage,320,445+15)
ML_UsePerspectiveView
''
'' end drawing
''
IDirect3DDevice9_EndScene(g_pd3dDevice)
IDirect3DDevice9_Present(g_pd3dDevice, NULL, NULL, NULL, NULL )
end sub
''-----------------------------------------------------------------------------
'' Name: WinMain()
'' Desc: The application's entry point
''-----------------------------------------------------------------------------
function WinMain(byval hInstance as HINSTANCE, _
byval hPrevInstance as HINSTANCE, _
byval lpCmdLine as string, _
byval nCmdShow as integer _
) as integer
dim as WNDCLASSEX winClass
dim as MSG uMsg
with winClass
.lpszClassName = @"MY_WINDOWS_CLASS"
.cbSize = len(WNDCLASSEX)
.style = CS_HREDRAW or CS_VREDRAW
.lpfnWndProc = @WindowProc
.hInstance = hInstance
.hIcon = LoadIcon(hInstance, cast(LPCTSTR,IDI_APPLICATION))
.hIconSm = LoadIcon(hInstance, cast(LPCTSTR,IDI_APPLICATION))
.hCursor = LoadCursor(NULL, IDC_ARROW)
.hbrBackground = cast(HBRUSH,GetStockObject(BLACK_BRUSH))
.lpszMenuName = NULL
.cbClsExtra = 0
.cbWndExtra = 0
end with
if( RegisterClassEx( @winClass ) = FALSE ) then
return E_FAIL
end if
g_hWnd = CreateWindowEx( NULL, "MY_WINDOWS_CLASS", _
"Direct3D (DX9) - Primitive Types", _
WS_OVERLAPPEDWINDOW or WS_VISIBLE, _
0, 0, 640, 480, NULL, NULL, hInstance, NULL )
if( g_hWnd = NULL ) then
return E_FAIL
end if
ShowWindow( g_hWnd, nCmdShow )
UpdateWindow( g_hWnd )
init()
do while( uMsg.message <> WM_QUIT )
if( PeekMessage( @uMsg, NULL, 0, 0, PM_REMOVE ) ) then
TranslateMessage( @uMsg )
DispatchMessage( @uMsg )
else
render()
sleep 10
end if
loop
shutDown()
UnregisterClass( "MY_WINDOWS_CLASS", winClass.hInstance )
return uMsg.wParam
end function
''-----------------------------------------------------------------------------
'' Name: WindowProc()
'' Desc: The window's message handler
''-----------------------------------------------------------------------------
function WindowProc( byval hWnd as HWND, _
byval msg as UINT, _
byval wParam as WPARAM, _
byval lParam as LPARAM _
) as LRESULT
select case ( msg )
case WM_KEYDOWN
select case( wParam )
case VK_ESCAPE
PostQuitMessage(0)
end select
case WM_CLOSE
PostQuitMessage(0)
case WM_DESTROY
PostQuitMessage(0)
case else
return DefWindowProc( hWnd, msg, wParam, lParam )
end select
return 0
end function
''-----------------------------------------------------------------------------
'' Name: shutDown()
'' Desc: Release all Direct3D resources.
''-----------------------------------------------------------------------------
sub shutDown( )
if( g_pd3dDevice <> NULL ) then
IDirect3DDevice9_Release(g_pd3dDevice)
end if
if( g_pD3D <> NULL ) then
IDirect3D9_Release( g_pD3D )
end if
end sub
''
''
''
end WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )
Enjoy it.
-
So cool!
dim as single ptr sepc
sepc = ML_GetSpectrum()
for i=0 to 39
dim as single ey
ey = sepc[i] * 127 * 4
if (ey > 127) then ey = 127
ML_DrawPartOfSprite(EcoImage,38+15.0*i,445-ey/2,0,128-ey,14,ey)
next
Spectrum analyser in Freebasic!!! Fwiw I don't think that it ran slow at all, it was fullframe rate here and I don't even have a gfx card on this laptop.
This lib is well cool. :clap:
-
-
Depends.. Personally I don't think it's slow but it's definately not up there with C++
I don't really have a lot of expereience with the fb gfx libs so I'm probably not the one to answer the question, I use software rendering for everything.
-
But I did not use any FreeBasic drawing stuff, i.e no fb gfx libs code here. Just used my Magic Library, as you know my Library is pure C++ with C link. So that I think fb does not produce an optimized code.
-
Maybe Victor or someone else over here;
Freebasic.net (http://www.freebasic.net/forum/)
Would be able to explain better for you why there is such a speed difference.
-
Ok i will check it.
-
ok i have found the reason with the help of FreeBasic forum.
it was a copy and paste problem , so i have include a sleep function in main loop in WinMain function so please comment it and you have the full speed of my demo , i will correct it with my next release.
-
Cool :) Thanks very much Emil and I'm glad they could help you there. You might get some more new fans for your excellent library over at freebasic.net.
-
That sure is some nice stuff - well done! I love the amiga so this demo sure is something!!!!
Drew
-
thanks