From f37162283c31f6a48dc8f2dd9e7059c741618fc9 Mon Sep 17 00:00:00 2001
From: Ian C <ianc@noddybox.co.uk>
Date: Mon, 15 Aug 2005 01:55:25 +0000
Subject: *** empty log message ***

---
 glflag.mod/glflag.bmx | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 198 insertions(+)
 create mode 100644 glflag.mod/glflag.bmx

diff --git a/glflag.mod/glflag.bmx b/glflag.mod/glflag.bmx
new file mode 100644
index 0000000..5b62530
--- /dev/null
+++ b/glflag.mod/glflag.bmx
@@ -0,0 +1,198 @@
+Module noddybox.glflag
+
+ModuleInfo "Framework: Simple Open GL Backdrop flag"
+ModuleInfo "Copyright: Public Domain"
+ModuleInfo "Author: Ian Cowburn"
+ModuleInfo "Version: $Revision$"
+
+' $Id$
+
+Import brl.Basic
+Import pub.OpenGL
+
+Strict
+
+Type GLFlag
+
+	' These are considered private
+	'
+	Const THRESHOLD:Float=0.0001
+	Field v:GLFlagVertex[,]
+	Field width:Int
+	Field height:Int
+	Field txt:Int
+	Field zpos:Float
+
+	' Creates a new flag
+	'	
+	' texture		- Flag texture
+	' horiz		- Number of horizontal vertices
+	' vert		- Number of vertical vertices
+	' size		- Size of each square
+	' tile		- Whether the texture is tiled or not
+	' z			- Distance of flag (flags always created in the XY plane)
+	'
+	' Returns null for invalid parameters.
+	'
+	Function Create:GLFlag(texture:Int, horiz:Int, vert:Int, size:Float, tile:Int, z:Float)
+		Local o:GLFlag
+		
+		If (horiz<2 Or vert<2)
+			Return Null
+		EndIf
+		
+		o=New GLFlag
+		
+		o.txt=texture
+		o.zpos=z
+		o.width=horiz
+		o.height=vert
+		o.v=New GLFlagVertex[horiz,vert]
+		
+		Local ox:Float
+		Local oy:Float
+		
+		ox=-(size*horiz)/2
+		oy=-(size*horiz)/2
+		
+		For Local x=0 Until horiz
+			For Local y=0 Until vert
+				Local v:GLFlagVertex=New GLFlagVertex
+				v.Set(ox+x*size,oy+y*size,0)
+				
+				If (tile)
+					v.u=(x&1)
+					v.v=(y&1)
+				Else
+					v.u=1.0/(horiz-1)*x
+					v.v=1.0/(vert-1)*(vert-1-y)
+				EndIf
+
+				o.v[x,y]=v
+			Next
+		Next
+		
+		Return o
+	End Function
+
+	' Call to render the flag
+	'	
+	' dampen		- Scale to dampen movements by when bouncing movement
+	' friction	- Scale to slow down movements
+	'	
+	Method Render(dampen:Float=1.0, friction:Float=0.95)
+		glPushMatrix()
+		glTranslatef(0,0,zpos)
+		glBindTexture(GL_TEXTURE_2D,txt)
+		
+		For Local vert:GLFlagVertex=EachIn v
+			If (vert.xi<>0 Or vert.yi<>0 Or vert.zi<>0)
+				Local s:Float
+				
+				s=Sgn(vert.xi)
+				vert.xi:*friction
+				If (Abs(vert.xi)<THRESHOLD)
+					vert.initxi=vert.initxi*dampen
+					If (vert.initxi<THRESHOLD)
+						vert.xi=0
+						vert.x=vert.initx
+					Else
+						vert.xi=-s*vert.initxi*2
+					EndIf
+				Else
+					vert.x:+vert.xi
+				EndIf
+
+				s=Sgn(vert.yi)
+				vert.yi:*friction
+				If (Abs(vert.yi)<THRESHOLD)
+					vert.inityi=vert.inityi*dampen
+					If (vert.inityi<THRESHOLD)
+						vert.yi=0
+						vert.y=vert.inity
+					Else
+						vert.yi=-s*vert.inityi*2
+					EndIf
+				Else
+					vert.y:+vert.yi
+				EndIf
+
+				s=Sgn(vert.zi)
+				vert.zi:*friction
+				If (Abs(vert.zi)<THRESHOLD)
+					vert.initzi=vert.initzi*dampen
+					If (vert.initzi<THRESHOLD)
+						vert.zi=0
+						vert.z=vert.initx
+					Else
+						vert.zi=-s*vert.initzi*2
+					EndIf
+				Else
+					vert.z:+vert.zi
+				EndIf
+			EndIf
+		Next
+
+		For Local x:Int=0 Until width-1
+			glBegin(GL_TRIANGLE_STRIP)
+			For Local y:Int=0 Until height
+				glTexCoord2f(v[x,y].u,v[x,y].v)
+				glVertex3f(v[x,y].x,v[x,y].y,v[x,y].z)
+				glTexCoord2f(v[x+1,y].u,v[x+1,y].v)
+				glVertex3f(v[x+1,y].x,v[x+1,y].y,v[x+1,y].z)
+			Next
+			glEnd()
+		Next
+		glPopMatrix()
+	End Method
+	
+	' Sets the movements for a vertex
+	'
+	' x			- The X co-ord of the vertex
+	' y			- The Y co-ord of the vertex
+	' xi			- The X movement
+	' yi			- The Y movement
+	' zi			- The Z movement
+	'
+	Method Nudge(x:Int, y:Int, xi:Float, yi:Float, zi:Float)
+		v[x,y].initxi=Abs(xi)
+		v[x,y].inityi=Abs(yi)
+		v[x,y].initzi=Abs(zi)
+		v[x,y].xi=xi
+		v[x,y].yi=yi
+		v[x,y].zi=zi
+	End Method
+	
+End Type
+
+Type GLFlagVertex
+	Field x#,y#,z#
+	Field u#,v#
+	Field initx#,inity#,initz#
+	Field initxi#,inityi#,initzi#
+	Field xi#,yi#,zi#
+	
+	Method New()
+		x=0
+		y=0
+		z=0
+		initx=0
+		inity=0
+		initz=0
+		initxi=0
+		inityi=0
+		initzi=0
+		xi=0
+		yi=0
+		zi=0
+	End Method
+	
+	Method Set(xc:Float, yc:Float, zc:Float)
+		x=xc
+		initx=xc
+		y=yc
+		inity=yc
+		z=zc
+		initz=zc
+	End Method
+End Type
\ No newline at end of file
-- 
cgit v1.2.3