' xvideocapture.bas 
' video 32bit pixel 12 FPS
' with audio 16bit mono 44khz

#include once "xvidcapture.bi"
#Include Once "gui_chung.bi"
#include once "win/mmsystem.bi"
#Include Once "wavein.bi"

Dim  as integer iWidth    = 500'320
Dim  as integer iHeight   = 400'240
const as integer iBPP      = 32
const as integer iFPS      = 12'24
const as integer iQuality  = 75
const as integer iChannels =  2
const as integer iBits     = 16
const as integer iRate     = 44100'22050

chdir exepath

if OpenVideoAudioStream("test.avi",iWidth,iHeight,iFPS,iQuality,iChannels,iBits,iRate)=0 then
  guinotice "error: OpenVideoAudioStream() !"
  guiclose:guiquit
  end 1
end if

' how many audio samples per frame ?
const as integer nSamplesvideo = ichannels*iRate\iFPS
' how many bytes per audio buffer
const as integer BufferSize = nSamplesvideo*sizeof(short)
' a 16 bit mono audio buffer
dim as short Samples(nSamplesvideo-1)

Dim Shared As WAVEIN_DEVICE mywavein

mywavein.wstart(-1)'mydev) wavein mapper

Declare Sub initgl
Declare Sub drawgl

Dim Shared As Integer quit=0,wx,wy,depth,winx,winy,windx,windy
Sub subquit
    quit=1
End Sub
winx=100:winy=100
windx=iwidth'+20
windy=iheight'+20
button("win.quit","quit",@subquit,5,5,50,20)
graphicbox("win.graph",1,10,iwidth-20,iheight-20,"opengl")
openwindow("win","xvideocapture",winy,winx,windx,windy)

trapclose("win",@subquit)

guistartOpenGL("win.graph")
initgl

Dim as integer i,j,k
Dim Shared As Double time0,time1

'Dim As Any Ptr myimage
'myimage=ImageCreate(iwidth,iheight,RGB(100,200,0),iBPP)
Dim As hbitmap hbmp
Dim As hdc bmpdc
Dim As Integer x,y
Dim As UByte bit8
Dim As UByte Ptr bmpbits
x=iwidth:y=iheight
hbmp=guicreateDIBbmp(x,-y)
bmpbits=guigetbmpdibbits(hbmp)
bmpdc=createcompatibleDC(guiwindc0)
selectobject(bmpdc,hbmp)
Dim hWnd As hwnd
Dim hDC As hdc 
hWnd=getguih("win")'Cast(hwnd,GetDesktopWindow())
hDC= Cast(hdc,GetWindowDC(Cast(hwnd,hWnd)))

time0=Timer:time1=Timer

while quit=0 And guitestkey(vk_escape)=0 And Timer<(time0+30)
  guiscan 
  drawgl
  For i=0 To iheight-1
  	  	'glreadpixels(0,iheight-i-1,iwidth,1,gl_rgba,gl_unsigned_byte,myimage+i*iwidth*ibpp\8)
  ' 	glreadpixels(0,iheight-i-1,iwidth,1,gl_rgba,gl_unsigned_byte,bmpbits+i*iwidth*ibpp\8)
  Next
  'glreadpixels(0,0,iwidth,iheight,gl_rgba,gl_unsigned_byte,bmpbits)
  'bitblt(bmpdc,0,0,x,y,hdc,winx,winy,srccopy)
  bitblt(bmpdc,0,0,x,y,hdc,0,0,srccopy)
  For i=0 To (x*y-1)*4 Step 4
      bit8=bmpbits[i]
      bmpbits[i]=bmpbits[i+2]
      bmpbits[i+2]=bit8
  Next
  select case iBPP
  	Case 32 : Write32BitPixels(bmpbits)'myimage)'ScreenPtr())
  	Case 16 : Write16BitPixels(bmpbits)'myimage)'ScreenPtr())
  end select
  
  for i=0 to nSamplesvideo-1
  	 If Abs(mybuffersoundj-mybuffersoundi)>SamplesPerBuffer Then 
       mybuffersoundj+=1:If mybuffersoundj>nsample Then mybuffersoundj=0
    EndIf 
    samples(i)=mybuffersound(mybuffersoundj)
  next
  WriteAudioSamples(@Samples(0),BufferSize)
  time1+=1/ifps
  If Timer<time1 Then
  	Sleep Int(min(0.2,time1-Timer)*1000)
  Else
   Sleep 0
  EndIf 
  guirefreshopenGL()
wend
CloseStream()
guicloseOpenGL()
'ImageDestroy(myimage)
mywavein.wclose()
deletedc(bmpdc)
deleteobject(hbmp)
ReleaseDC(Cast(hwnd,hWnd),Cast(hdc,hDC))

guiclose()
guiquit()
End 


Sub initgl	
	glMatrixMode GL_PROJECTION
	glLoadIdentity
	'              anglevue    xmax/ymax   mxmin,mxmax
	gluPerspective   45.0,    640.0/480.0,  5.0, 50000.0
	glMatrixMode GL_MODELVIEW
	glLoadIdentity
	
	glShadeModel GL_SMOOTH
	glClearColor 0.7, 0.7, 0.7, 0.0
	glClearDepth 1.0
	glEnable GL_DEPTH_TEST
	glDepthFunc GL_LEQUAL
	glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST

End Sub
Dim Shared As Single o1
Sub drawgl
		glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT

		glLoadIdentity
		glTranslatef 0, 0.0, -60.0
		glRotatef o1, 0, 1, 0
		glBegin GL_TRIANGLES
			glColor3f 1.0, 0.0, 0.0
			glVertex3f 0.0, 10.0, 0.0
			glColor3f 0.0, 1.0, 0.0
			glVertex3f -10.0, -10.0, 0.0
			glColor3f 0.0, 0.0, 1.0
			glVertex3f 10.0, -10.0, 0.0
		glEnd
		
		o1+=1
		
End Sub 