'http://nehe.gamedev.net/tutorial/flag_effect_(waving_texture)/16002/
'Adapted from Nehe tutorial lesson 11, uses MainWindow (winutil.inc)
'F1 opens Help
$ filename "NeheTut11.exe"
'uses RTL32
'uses RTL64
sys hAccel
sys HelpDlg
macro InMessageLoop
if TranslateAccelerator( hWnd, hAccel, @wm ) = 0 then
if not IsDialogMessage(hWnd, &wm) then
if IsDialogMessage( HelpDlg, &wm ) = 0 then
TranslateMessage(&wm)
DispatchMessage(&wm)
end if
end if
end if
end macro
% FileDialogs 'opens FileDialog in winutil.inc
#case capital
typedef sys SYS 'FileDialog.inc
typedef char CHAR 'FileDialog.inc
uses winutil
uses glWinUtil
uses imgwin
'% review
uses dialogs
#autodim off
indexbase 0
% DS_CENTER=0x0800
% SM_CYCAPTION 4
% SWP_NOSIZE 0X1
% SWP_NOZORDER 0X4
#define IDM_Load 1001
#define IDM_Exit 1002
#define IDM_Play 1003
#define IDM_Reset 1004
#define IDM_Help 1005
'Ids for text controls in Help
% IDC_LText1=1131
% IDC_LText2=1132
/*
declare sub InitGL()
declare function initMenu(sys hWnd) as sys
declare sub LoadGLTextures()
declare sub DrawGLScene ()
declare sub ReSizeGLScene (GLsizei width, GLsizei height)
declare function GetTheFileName(string name, int a, optional string myfilter="", mytitle="") as string
*/
#lookahead
float points[45*45*3] ' The Array For The Points On The Grid Of Our "Wave"
macro points3d float* (r,x1,y1,z1)
'r return value
@r=@points( (x1)*45*3 + (y1)*3 + (z1) )
end macro
int wiggle_count = 0 ' Counter Used To Control How Fast Flag Waves
GLfloat xrot 'X Rotation
GLfloat yrot 'Y Rotation
GLfloat zrot 'Z Rotation
GLfloat hold 'Temporarily Holds A Floating Point Value
static bool p_toggle 'Play ON/OFF
sys GdiplusToken
GLuint texture[1] 'Storage for one Texture
GLuint wi[1],ht[1]
'Prepare Textures
'----------------
glGenTextures 1, texture
bool is_loaded
macro refreshScene()
if is_loaded then DrawGlScene()
SwapBuffers(hWndHDC)
end macro
string fn=""
sys hWndHDC
sys hMenu
sys hInstance=inst
MainWindow(640,480,WS_OVERLAPPEDWINDOW, 2)
function WndProc ( hWnd, wMsg, wParam, lParam ) as sys callback
select wMsg
case WM_CREATE
hMenu = initMenu(hWnd)
if SetMenu( hWnd, hMenu ) = 0 then
mbox "SetMenu hMenu failed!"
end if
sys hGLrc
hWndhDC = GetDC( hWnd )
SetWindowText(hWnd, "Nehe Lesson 11")
'glWinUtil.inc
SelectPixelFormat(hWndHDC, 1)
hGLrc = wglCreateContext( hWndHDC )
if hGLrc = 0 then
MessageBox( hWnd, "wglCreateContext failed", 0, 0 )
DestroyWindow( hWnd )
end if
if wglMakeCurrent( hWndHDC, hGLrc ) = 0 then
MessageBox( hWnd, "wglMakeCurrent failed", 0, 0 )
DestroyWindow( hWnd )
end if
InitGL()
case WM_COMMAND
select loword(wParam)
case IDM_Exit
SendMessage hWnd, WM_CLOSE, 0, 0
case IDCANCEL
SendMessage hWnd, WM_CLOSE, 0, 0
case IDM_Load
fn=GetTheFileName("", 0, "", "Please select Image to load")
if fn then
is_loaded=true
LoadGLTextures()
refreshScene
end if
case IDM_Play
p_toggle = not p_toggle
case IDM_Reset
xrot=0.0f
yrot=0.0f
zrot=0.0f
case IDM_Help
'Open Help Window if not already open
if IsWindow( HelpDlg ) = 0 then
sys lpdt
dyn::init(lpdt)
Dialog( 2, 0, 0, 150, 100, "Buttons used:", lpdt,
WS_OVERLAPPED or WS_SYSMENU or DS_CENTER or WS_VISIBLE or DS_SETFONT,
10, "MS Sans Serif" )
LTEXT( "", IDC_LText1, 5, 5, 50, 120 )
LTEXT( "", IDC_LText2, 55, 5,110, 120 )
HelpDlg = CreateModelessDialog( hWnd, @HelpDlgProc, 0, lpdt )
end if
end select
case WM_PAINT
refreshScene
case WM_SIZE
ReSizeGLScene(loword(lParam),hiword(lParam)) ' LoWord=Width, HiWord=Height
refreshScene
case WM_ERASEBKGND
return 0
case WM_CLOSE
DestroyWindow(hWnd)
case WM_DESTROY
glDeleteTextures 1, texture
Gdiplus 0
if hGLrc then 'Rendering Context?
if not wglMakeCurrent(null,null) then 'Release the DC And RC Contexts
MessageBox(null,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
end if
if not wglDeleteContext(hGLrc) then 'Delete the RC
MessageBox(null,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
end if
hGLrc=null 'Set RC to null
end if
if hWndHDC then
if not ReleaseDC(hWnd,hWndHDC) then 'Release The DC
MessageBox(null,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION)
hWndHDC=null 'Set DC to null
end if
end if
PostQuitMessage 0
case else
return DefWindowProc hWnd,wMsg,wParam,lParam
end select
end function ' WndProc
'====================================================================
function initMenu(sys hWnd) as sys
indexbase 1
MENU(hMenu)
BEGIN
POPUP "&File"
BEGIN
MENUITEM "&Load Image..." tab "Ctrl+O", IDM_Load
MENUITEM "SEPARATOR"
MENUITEM "E&xit" tab "Alt+F4", IDM_Exit
ENDMenu
POPUP "&View"
BEGIN
MENUITEM "&Toggle Start / Stop" tab "Ctrl+P", IDM_Play
MENUITEM "SEPARATOR"
MENUITEM "&Reset" tab "Ctrl+R", IDM_Reset
ENDMenu
POPUP "&Help"
BEGIN
MENUITEM "&Key Options" tab "F1", IDM_Help
ENDMenu
ENDMenu
'Accelerators
indexbase 0
ACCEL accl[3] = {
{FVIRTKEY | FCONTROL, asc("O"), IDM_Load },
{FVIRTKEY | FCONTROL, asc("P"), IDM_Play },
{FVIRTKEY | FCONTROL, asc("R"), IDM_Reset },
{FVIRTKEY, VK_F1, IDM_Help }
}
hAccel = CreateAcceleratorTable( @accl, 4 )
return hMenu
end function
sub LoadGLTextures() ' Load Bitmaps And Convert To Textures
static sys res
string imgs[1]=""
'load an image file directly as a new OpenGL texture
LoadPixImage fn,res,imgs[1],wi[1],ht[1]
'Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[1])
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
glTexImage2D GL_TEXTURE_2D, 0, 4, wi[1], ht[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, strptr imgs[1]
end sub
sub InitGL()
GDIplus 1
'SYNC FRAME RATE - 60HZ
sys p=wglgetprocaddress "wglSwapIntervalEXT" : call p 1
glEnable(GL_TEXTURE_2D) 'Enable Texture Mapping
'All Setup For OpenGL Goes Here
glShadeModel(GL_SMOOTH) 'Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f) 'Black Background
glClearDepth(1.0f) 'Depth Buffer Setup
glEnable(GL_DEPTH_TEST) 'Enables Depth Testing
glDepthFunc(GL_LEQUAL) 'The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) 'Really Nice Perspective Calculations
glPolygonMode( GL_BACK, GL_FILL ) ' Back Face Is Solid
glPolygonMode( GL_FRONT, GL_LINE ) ' Front Face Is Made Of Lines
' Loop Through The X Plane
int x,y
float xf,yf
for x=0 to <45
xf=float(x)
for y=0 to <45
yf=float(y)
points3d(x,y,0)= (xf/5.0f)-4.5f
points3d(x,y,1)= (yf/5.0f)-4.5f
points3d(x,y,2)= sin((((xf/5.0f)*40.0f)/360.0f)*3.141592654*2.0f)
next y
next x
end sub
sub DrawGLScene ()
int x, y
float xf,yf
float float_x, float_y, float_xb, float_yb
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ' Clear The Screen And The Depth Buffer
glLoadIdentity() ' Reset The View
glTranslatef(0.0f,0.0f,-12.0f)
glRotatef(xrot,1.0f,0.0f,0.0f)
glRotatef(yrot,0.0f,1.0f,0.0f)
glRotatef(zrot,0.0f,0.0f,1.0f)
glBindTexture(GL_TEXTURE_2D, texture[0])
glBegin(GL_QUADS)
for x = 0 to <44
xf=float(x)
for y = 0 to <44
yf=float(y)
float_x = xf/44.0f
float_y = yf/44.0f
float_xb = (xf+1)/44.0f
float_yb = (yf+1)/44.0f
glTexCoord2f( float_x, float_y)
glVertex3f( points3d(x,y,0), points3d(x,y,1), points3d(x,y,2) )
glTexCoord2f( float_x, float_yb )
glVertex3f( points3d(x,y+1,0), points3d(x,y+1,1), points3d(x,y+1,2) )
glTexCoord2f( float_xb, float_yb )
glVertex3f( points3d(x+1,y+1,0), points3d(x+1,y+1,1), points3d(x+1,y+1,2) )
glTexCoord2f( float_xb, float_y )
glVertex3f( points3d(x+1,y,0), points3d(x+1,y,1), points3d(x+1,y,2) )
next y
next x
glEnd()
if wiggle_count = 2 then
for y = 0 to <45
hold=points3d(0,y,2)
for x = 0 to <44
points3d(x,y,2) = points3d(x+1,y,2)
next x
points3d(44,y,2)=hold
next y
wiggle_count = 0
end if
if p_toggle then
wiggle_count++
xrot+=0.3f
yrot+=0.2f
zrot+=0.4f
end if
end sub
sub ReSizeGLScene (GLsizei width, GLsizei height) ' Resize And Initialize The GL Window
if height=0 then ' Prevent A Divide By Zero By
height=1 ' Making Height Equal One
end if
glViewport(0,0,width,height) ' Reset The Current Viewport
glMatrixMode(GL_PROJECTION) ' Select The Projection Matrix
glLoadIdentity() ' Reset The Projection Matrix
' Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f, width/ height,0.1f,100.0f)
glMatrixMode(GL_MODELVIEW) ' Select The Modelview Matrix
glLoadIdentity() ' Reset The Modelview Matrix
end sub
function GetTheFileName(string name, int a, optional string myfilter="", mytitle="") as string
'adapted from FileDialog.inc
sys hwnd
char fdir[512] : GetCurrentDirectory 512, fdir
string dir = fdir
string sep=chr(0)
string filter=
"images"+sep+"*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff;*.gif;*.ico"+sep+
"all files"+sep+"*.*"+sep+sep
if len(myfilter) then filter=myfilter + sep
string title
if a then
title="Save File as"
else
title="Load File"
end if
if len mytitle then title=mytitle
sys flags = OFN_EXPLORER or OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST or OFN_OVERWRITEPROMPT or OFN_HIDEREADONLY
string fname = FileDialog(dir,filter,title,name,hwnd,flags,a)
return fname
end function
function HelpDlgProc( sys hDlg, uint uMsg, sys wParam, lParam) as sys callback
string Result
sys hLText1=GetDlgItem(hDlg, IDC_LText1)
sys hLText2=GetDlgItem(hDlg, IDC_LText2)
select case uMsg
case WM_INITDIALOG
string HelpText1 =
"
F1
Ctrl-O
Ctrl-P
Ctrl-R
ESC
Alt-F4
"
string HelpText2 =
"
This Help
Load an image
Start / Stop Animation
Reset to original state
Quit
Quit
"
SetWindowText (hLText1, HelpText1)
SetWindowText (hLText2, HelpText2)
case WM_COMMAND
select case loword(wParam)
case IDCANCEL
DestroyWindow( hDlg )
end select
case WM_CLOSE
DestroyWindow( hDlg )
'Reset Help dialog
HelpDlg = 0
end select
return 0
end function