Hi all
This demo you will find it in your downloaded OgreMagic/Example folder.
It is a complicated demo , I wrote it for Ogre and ported to freebasic .
Demo feature
=========
1- dark Background Image
2- vertical scrolling text
3- playing movie masked by the scrolling text
4- gradually fading in the lowest scrolling text and fading out the upper scrolling text
Idea of demo
=========
I have rendered a scrolling text to texture , and created an alpha mask sprite so the alpha value is 1.0 in the middle of sprite and it gradually decrease to 0.0 in the upper and lower sprite edge, then I have used the next shader program to make the effect.
''------------------------------------------------------------------------------
''
'' Magic Library For Ogre
'' beta version 0.40
'' by Emil Halim
''
'' Name : CreditsDemo
'' Date : 25/02/2007
'' Purpose : Teaching you that how to
'' use Magic Library With
'' FreeBasic.
''
'' website : http://www.freewebs.com/ogremagic/index.htm
''
''------------------------------------------------------------------------------
'
''Note Well
'' this demo has a shder program , so if your
'' Gfx card does not supports Pixle shader , it may not work
'' correctly in your computer.
''
'
#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 BackGround 'background image
dim shared as AnimatedSpriteData ptr Mickey 'Mickey animated sprite
dim shared as MovieData ptr Movie 'holds a pointer to movie structure
dim shared as SpriteData ptr MovieSprite 'we copy each movie frame to sprite then draw the sprite
dim shared as SpriteData ptr AlphaMaskSprite 'stor the alpha value , so that fade out and in effect acts
dim shared as BitmapFontData ptr Amigafont 'holds a pointer to structure of font image
dim shared as HLSLProgramData ptr HLSLProgram 'here is the high level shader programe that make the effect
dim shared as ScreenSurfaceData ptr Scren 'used to render to it then reuesed as a usual sprite
dim shared as Zstring Ptr st(0 to 14) 'hold the vertical screolled strings
dim shared as single centerX(0 to 14) 'hold the x coordinates so that the text centered on screen
dim shared as SoundData ptr song 'play music while scrolling the text
''-----------------------------------------------------------------------------
'' 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) ' initial the OgreMagic library
FmodInit() ' initial the Fmod library
ML_SetMediaDirectory("../MagicMedia/") ' set default media directory
BackGround = ML_LoadStaticSprite("trika.bmp") ' load a background image
Mickey = ML_LoadAnimatedSprite("Sprites/Micky.bmp") ' load animated sprite
ML_SetAnimatedSpriteFrameSize2(Mickey,84,62,0,0,252,248) ' set the width and height of each frame of animated sprite
ML_SetSpriteColorKeyFromPoint(Mickey,0,0) ' set the transparent color
ML_SetAnimationSpeed(Mickey,10) ' set the framepresecond of animated sprite
Amigafont = ML_LoadBitmapFont("font/AmigaFont2.bmp",32,28,30) ' load the font image and set the width and height of each char and space btween chars
ML_SetSpriteColorKeyFromPoint(Amigafont,0,0) ' set the transparent color of font
ML_SetActiveBitmapFont(Amigafont) ' ues this font when draw a text
Movie = ML_LoadMovie("movie/ahly_america1-0.wmv") ' load the movie
ML_SetMovieFramePerSocand(Movie,25) ' set the frame per second of movie playback
ML_SetMovieLoopedPlaying(Movie,true) ' loop the movie when it reach to end
dim as integer wwidth = ML_GetMovieWidth(Movie)
dim as integer height = ML_GetMovieHeight(Movie)
MovieSprite = ML_CreateStaticSprite(wwidth,height) 'create an empty sprite with the same movie size
HLSLProgram = ML_CreateHLSLProgram() ' create a high level shader program
ML_EditPixelProgram(HLSLProgram) ' start adding the following to Pixel program
ML_AddToProgram("sampler2D tex0; ")
ML_AddToProgram("sampler2D tex1; ")
ML_AddToProgram("sampler2D tex2; ")
ML_AddToProgram("float4 main( float2 TexCoord : TEXCOORD0) : COLOR0 ")
ML_AddToProgram("{ ")
ML_AddToProgram(" float4 Col; ")
ML_AddToProgram(" float4 col0 = tex2D( tex0, TexCoord); ")
ML_AddToProgram(" float4 col1 = tex2D( tex1, TexCoord); ")
ML_AddToProgram(" float4 col2 = tex2D( tex2, TexCoord); ")
ML_AddToProgram(" Col = float4(col0.rgb,col1.a*col2.a); ")
ML_AddToProgram(" return Col; ")
ML_AddToProgram("} ")
ML_CompilePixelProgram(HLSLProgram,"main") ' compile the shader program and set the main function
Scren = ML_CreateScreenSurface(640, 480) ' we ues this as an Off Sprite ,we render to it then we use it as a sprite
ML_SetScreenClearColor(0,0,0,0) ' ues this color to clear OFF sprite buffer
AlphaMaskSprite = ML_CreateStaticSprite(1,255) ' creat an empty sprite that holds alpha data
ML_StartDrawingToSprite(AlphaMaskSprite) ' lock the sprite for editing it
ML_ClearDrawingArea(0) ' fill the sprite buffer with black color
height= ML_GetDrawingHeight()
for h=0 to height-1
if (h>127) then
ML_WriteAlphaChannel(0,h,255-2*h) ' write only to alpha component of sprite
else
ML_WriteAlphaChannel(0,h,2*h)
end if
next
ML_StopDrawingToSprite(AlphaMaskSprite) ' unlock the sprite buffer
for i=0 to 14 : st(i) = callocate(200) : next
*st(0) = "WELCOME"
*st(1) = "TO"
*st(2) = "ELAHLY"
*st(3) = "SPORTING"
*st(4) = "CLUB"
*st(5) = "........"
*st(6) = ""
*st(7) = "THIS"
*st(8) = "IS"
*st(9) = "CREDIT"
*st(10)= "DEMO"
*st(11)= "WRITTEN"
*st(12)= "BY"
*st(13)= "EMIL"
*st(14)= "HALIM"
ML_SetScale(1.5,2.2) ' we will use this scale when drawing the text so we
for i=0 to 14 : centerX(i) = ML_GetCenterX(st(i)) : next ' set it before calculating center of coordinate
song = ML_LoadSoundData("mod/Paul Miseiko - Fantasy of Reality.it",SongType) ' load the sound
ML_SetSoundVolume(song,250) ' set the sound volume
ML_SetSoundFreq(song,10) ' set the frequency
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) ' start to play the music
ML_StartRenderingTo(Scren) ' redirect the render to scren sprite
ML_UseOrthogonalView ' set the 2D drawing Mode
ML_SetAlpha(1) ' set the factor of transparnet
ML_SetColor(255,255,255) ' set the white color
ML_SetBlendMode(ALPHABLEND) ' set blending mode to be alpha blending
dim as single midWidth = ML_GetHalfScreenWidth()
dim as single midHeight = ML_GetHalfScreenHeight()
ML_SetScale(1.5,2.2) ' set the scale that apply for coming drawing until we change it again
ML_SetSpriteTexture(Amigafont) ' set the font texture , must be set before we drawing it
static as single s=65,y=500
y-=0.3
if(y<-17*s)then y=500
for i=0 to 14 : ML_DrawString(st(i),centerX(i),y+s*i) : next ' draw the string to Off sprite
ML_SetScale(2,2) '
ML_SetSpriteTexture(Mickey) ' set the texture of mickey animated sprite
ML_DrawAnimatedSprite(Mickey,midWidth,y+s*15+30) ' draw the mickey at midwidth , y+s*15+30 coordinate
ML_RenderToOriginalScreen() ' so we finished the rendering to off sprite and redirect to original screen
ML_GetNextMovieFrame(Movie) ' update the movie to next frame
dim as integer wwidth = ML_GetMovieWidth(Movie)
dim as integer height = ML_GetMovieHeight(Movie)
ML_DrawMovieFrameToSprite(MovieSprite,0,0,Movie,0,0,wwidth,height) ' draw a reactangle of movie to the sprite at 0,0 cordinate
ML_SetAlpha(0.2) ' set alpha transparent value
ML_SetScale(1.2,1) ' set the scale
ML_SetSpriteTexture(BackGround) ' set the texture of background image
ML_DrawSprite(BackGround,midWidth,midHeight) ' draw the background image
ML_SetAlpha(1.0) ' reset transparent factor
ML_SetScale(1.75,1.0) ' set the scale
ML_StartShaderProgram(HLSLProgram) ' start using the shader programe
ML_SetSpriteTexture(MovieSprite) ' pass the movie sprite to shader program
ML_SetScreenSurfaceAsTexture(Scren,1) ' pass the off sprite to shader program
ML_SetSpriteTexture(AlphaMaskSprite,2) ' pass the alpha sprite to shader program
ML_DrawSprite(MovieSprite,midWidth,midHeight) ' draw the movie sprite by using the shader program
ML_StopShaderProgram() ' sopt using the shader program
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()
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.