Ok dude, here we go:
'
' Clyde Gets Into Windows.
' October 2008
' Note: You will need the media files found in the zip to run.
'
Option Explicit
Option Static
#Include Once "Windows.bi"
#Include Once "crt.bi"
#Include Once "media\FaceP.bas"
#Include Once "media\FaceR.bas"
#Include Once "media\Face2P.bas"
#Include Once "media\Face2R.bas"
Const As Single PI=3.14159265
Const As Single D2R=PI/180.0
Const MAXBUTTONS=2
Const XRES=640
Const YRES=480
Const APPNAME As String="Windows Framework"
Type GfxBuffer
wwidth As Integer
height As Integer
pixels As UInteger pointer
End Type
Type Button
Region As HRGN
Rect As RECT
PosX As Integer
PosY As Integer
Image1 As GFXBuffer Pointer
Image2 As GFXBuffer Pointer
State As Integer
End Type
Dim Shared As GfxBuffer Pointer Image1,Image2, ScreenBuffer, Palette1
Dim Shared As Button Pointer Button( 0 To MAXBUTTONS-1 )
Dim Shared As Single Cosine( 0 to 1500 )
Dim Shared As Integer wave1,wave2,wave3
Declare Sub InitializeWindows()
Declare Sub UpdatePlasma( ByVal Dest As HDC,_
ByVal Source As GFXBuffer Pointer,_
ByVal Pal As GFXBuffer Pointer,_
ByVal Val1 As Integer,_
ByVal Val2 As Integer,_
ByVal Val3 As Integer )
Declare Sub DrawButton ( ByVal hdc as HDC,_
ByVal Buton As Button Pointer )
Declare Sub DrawGFXBuffer( ByVal Dest As HDC,_
ByVal Source As GfxBuffer Pointer,_
ByVal PosX As Integer,_
ByVal PosY As Integer )
Declare Function CreateButton( ByVal Image1 As GFXBuffer Pointer,_
ByVal Image2 As GFXBuffer Pointer,_
ByVal StartPosX As Integer,_
ByVal StartPosY As Integer ) As Button Pointer
Declare Function CreateButtonRegion( ByVal PosX0 As Integer,_
ByVal PosY0 As Integer,_
ByVal PosX1 As Integer,_
ByVal PosY1 As Integer) As HRGN
Declare Function CreateGFXBuffer( Byval WWidth As Integer,_
Byval Height As integer ) As GfxBuffer Pointer
Declare Function CreatePal( ByVal Val1 As Integer,_
ByVal Val2 As Integer,_
ByVal Val3 As Integer,_
ByVal Divv As Integer=360) As GFXBuffer Pointer
Declare Function LoadGraphics8Bit( ByVal WWidth As Integer,_
ByVal Height As Integer,_
ByVal Raw As UByte Pointer,_
ByVal Palet As UByte Pointer ) As GfxBuffer Pointer
Declare Function WndProc ( ByVal hWnd As HWND,_
ByVal message As UINT,_
ByVal wParam As WPARAM,_
ByVal lParam As LPARAM ) As LRESULT
Declare Function WinMain ( ByVal hInstance As HINSTANCE,_
ByVal hPrevInstance As HINSTANCE,_
ByRef szCmdLine As String,_
ByVal iCmdShow As Integer ) As Integer
WinMain( GetModuleHandle( Null ), Null, Command, SW_NORMAL )
End
Sub UpdatePlasma( ByVal Dest As HDC,_
ByVal Source As GFXBuffer Pointer,_
ByVal Pal As GFXBuffer Pointer,_
ByVal Val1 As Integer,_
ByVal Val2 As Integer,_
ByVal Val3 As Integer )
Dim bmi As BITMAPINFO
With bmi.bmiheader
.biSize = SizeOf (BITMAPINFOHEADER)
.biWidth = ScreenBuffer->WWidth
.biHeight = -ScreenBuffer->Height
.biPlanes = 1
.biBitCount = 32
.biCompression = BI_RGB
.biSizeImage = 0
.biClrUsed = 0
.biClrImportant = 0
End With
Dim As Integer x,y,d,f
For y = 0 To Source->Height-1
d = cosine( y + wave2 ) + cosine( y + wave3 )
For x = 0 To Source->WWidth-1
f = cosine( x + wave1 ) + cosine( x + y ) + d and 255
Source->Pixels [ x+y*Source->WWidth ]=Pal->Pixels[ f and 255 ]
Next
Next
wave1+=Val1 : If wave1 >= Source->WWidth-1 Then wave1 = wave1 - Source->WWidth
wave2+=Val2 : If wave2 >= Source->WWidth-1 Then wave2 = wave2 - Source->WWidth
wave3+=Val3 : If wave3 >= Source->WWidth-1 Then wave3 = wave3 - Source->WWidth
SetDIBitsToDevice( Dest,_
0,_
0,_
Source->WWidth,_
Source->Height,_
0,_
0,_
0,_
Source->Height,_
@Source->Pixels[0],_
@bmi,_
DIB_RGB_COLORS )
End Sub
Function CreateButton( ByVal Image1 As GFXBuffer Pointer,_
ByVal Image2 As GFXBuffer Pointer,_
ByVal StartPosX As Integer,_
ByVal StartPosY As Integer ) As Button Pointer
Dim Btn As Button Pointer=Callocate(Len( Button )+Len( UInteger ))
Btn->PosX=StartPosX
Btn->PosY=StartPosY
Btn->Image1=Image1
Btn->Image2=Image2
Btn->State=0
Btn->Region=CreateButtonRegion( StartPosX,_
StartPosY,_
StartPosX+Btn->Image1->WWidth,_
StartPosY+Btn->Image1->Height )
Btn->Rect.left =StartPosX
Btn->Rect.top =StartPosY
Btn->Rect.right =StartPosX+Btn->Image1->WWidth
Btn->Rect.bottom=StartPosY+Btn->Image1->Height
Function=Btn
End Function
Function CreateButtonRegion( ByVal PosX0 As Integer, ByVal PosY0 As Integer, ByVal PosX1 As Integer, ByVal PosY1 As Integer) As HRGN
Dim Region As HRGN
Region=CreateRectRgn( PosX0,_ ' x-coordinate of region's upper-left corner
PosY0,_ ' y-coordinate of region's upper-left corner
PosX1,_ ' x-coordinate of region's lower-right corner
PosY1 ) ' y-coordinate of region's lower-right corner
Function=Region
End Function
Sub InitializeWindows()
ScreenBuffer = CreateGFXBuffer( XRES, YRES )
Image1= LoadGraphics8bit(32,32,@facer(0),@facep(0))
Image2= LoadGraphics8bit(32,32,@facer2(0),@facep2(0))
Button(0)=CreateButton( Image1,_
Image2,_
100,_
100)
Button(1)=CreateButton( Image1,_
Image2,_
280,_
32)
Palette1=CreatePal(10,45,90)
Dim As Integer a
For a=0 To 1499
Cosine( a ) = Cos( (( 115*PI * a ) / ScreenBuffer->WWidth )*D2R ) * 128
Next
End Sub
Sub DrawButton( ByVal hdc as HDC,_
ByVal Buton As Button Pointer )
Select Case As Const Buton->State
Case 0:
DrawGFXBuffer( hdc, Buton->Image1, Buton->PosX, Buton->PosY )
Case 1:
DrawGFXBuffer( hdc, Buton->Image2, Buton->PosX, Buton->PosY )
End Select
End Sub
Sub DrawGFXBuffer( ByVal Dest As HDC,_
ByVal Source As GfxBuffer Pointer,_
ByVal PosX As Integer,_
ByVal PosY As Integer )
Dim bmi As BITMAPINFO
With bmi.bmiheader
.biSize = SizeOf (BITMAPINFOHEADER)
.biWidth = Source->WWidth
.biHeight = -Source->Height
.biPlanes = 1
.biBitCount = 32
.biCompression = BI_RGB
.biSizeImage = 0
.biClrUsed = 0
.biClrImportant = 0
End With
SetDIBitsToDevice( Dest,_
PosX,_
PosY,_
Source->WWidth,_
Source->Height,_
0,_
0,_
0,_
Source->Height,_
@Source->Pixels[0],_
@bmi,_
DIB_RGB_COLORS)
End Sub
Function CreatePal( ByVal Val1 As Integer,_
ByVal Val2 As Integer,_
ByVal Val3 As Integer,_
ByVal Divv As Integer=360) As GFXBuffer Pointer
Dim As GFXBuffer Pointer Pal=CreateGFXBuffer(1,256)
Dim As Integer a, red, grn, blu
Dim As Single m
For a=0 to 255
m = a*(Divv/255)
red = cos( (m+Val1)*D2R ) *127+127
grn = cos( (m+Val2)*D2R ) *127+127
blu = cos( (m+Val3)*D2R ) *127+127
Pal->Pixels[ a*Pal->WWidth ] = rgb(red,grn,blu)
Next
Return Pal
End Function
Function CreateGFXBuffer( Byval WWidth As Integer,_
Byval Height As Integer ) As GfxBuffer Pointer
Dim As GfxBuffer Pointer Buffer=Callocate(Len( GfxBuffer )+Len( UInteger )*WWidth*Height)
Buffer->wwidth=WWidth
Buffer->height=Height
Buffer->pixels=Cast( UInteger Pointer, Cast( Byte Pointer, Buffer )+Len( GfxBuffer ))
Function=Buffer
End Function
Function WndProc ( ByVal hWnd As HWND,_
ByVal message As UINT,_
ByVal wParam As WPARAM,_
ByVal lParam As LPARAM ) As LRESULT
Dim As PAINTSTRUCT SBuffer
Dim As HDC hDC
Dim As Integer MouseX, MouseY, a
Select Case ( message )
Case WM_CREATE
InitializeWindows()
Function=0
Case WM_TIMER
InvalidateRect(hWnd, NULL, False)
Case WM_ERASEBKGND
'Function=1
Return 1
Case WM_PAINT
hDC = BeginPaint( hWnd, @SBuffer )
UpdatePlasma( hdc, ScreenBuffer, Palette1, 2, 4, 2 )
For a=0 To MAXBUTTONS-1
DrawButton( hdc, Button(a) )
Next
EndPaint( hWnd, @SBuffer )
Function=0
case WM_LBUTTONDOWN
'If Button(0)->State=1 Then DoSomething()
Case WM_MOUSEMOVE
MouseX = LoWord(lParam)' and &Hffff
MouseY = HiWord(lParam)' shr 16
For a=0 To MAXBUTTONS-1
If (PtInRegion(Button(a)->Region, MouseX, MouseY) <> 0) then
Button(a)->State=1
Else
Button(a)->State=0
End if
Next
Case WM_KEYDOWN
Select Case LoByte( wParam )
Case VK_ESCAPE
PostMessage( hWnd, WM_CLOSE, 0, 0 )
Function=0
End Select
Case WM_DESTROY
PostQuitMessage( 0 )
Function=0
End Select
Function=DefWindowProc( hWnd, message, wParam, lParam )
End function
Function WinMain ( ByVal hInstance As HINSTANCE,_
ByVal hPrevInstance As HINSTANCE,_
ByRef szCmdLine As String,_
ByVal iCmdShow As Integer ) As Integer
Dim wMsg As MSG
Dim wClass As WNDCLASS
Dim hWnd As HWND
With wClass
.style = CS_HREDRAW or CS_VREDRAW
.lpfnWndProc = @WndProc
.cbClsExtra = 0
.cbWndExtra = 0
.hInstance = hInstance
.hIcon = LoadIcon( null, IDI_APPLICATION )
.hCursor = LoadCursor( null, IDC_ARROW )
.hbrBackground = null
.lpszMenuName = null
.lpszClassName = StrPtr( APPNAME )
end with
If( RegisterClass( @wClass ) = False ) Then
MessageBox( Null,"Sorry there dude. Unable To Register Class", APPNAME, MB_ICONERROR )
Exit Function
End If
hWnd = CreateWindowEx( 0,_
APPNAME,_
APPNAME,_
WS_OVERLAPPEDWINDOW, _
CW_USEDEFAULT,_
CW_USEDEFAULT,_
XRES,_
YRES,_
null,_
null,_
hInstance,_
null )
ShowWindow ( hWnd, iCmdShow )
UpdateWindow( hWnd )
SetTimer(hWnd,0,1000/60,null)
While ( GetMessage( @wMsg, null, 0, 0 ) <> False )
TranslateMessage( @wMsg )
DispatchMessage ( @wMsg )
Wend
Function = wMsg.wParam
End Function
Function LoadGraphics8Bit( ByVal WWidth As Integer,_
ByVal Height As Integer,_
ByVal Raw As UByte Pointer,_
ByVal Palet As UByte Pointer ) As GfxBuffer Pointer
Dim As GfxBuffer Pointer Buffer=CreateGFXBuffer( WWidth, Height )
Dim As UInteger pal(0 to 255)
Dim As Integer a,x,y
'
' Retrieve Palette Info.
'
For a=0 to 255
pal(a)=(palet[a*3] Shl 16) Or (palet[a*3+1] Shl 8) Or(palet[a*3+2])
Next
'
' Make Image Based On Colour Indexing With The Raw values.
'
For y=0 to Height-1
For x=0 to WWidth-1
Buffer->Pixels[ x+y*WWidth ]=Pal(Raw[x+y*WWidth])
Next
Next
Function=Buffer
End Function
I have seen some people changing things to a dc and selecting objects. Dont know what thats about, or if its needed.
Cheers and very special thanks,
Clyde.