Commits

dtrg committed 2a031fb

Move the incomplete OpenGL bindings into the OpenGL test app.

  • Participants
  • Parent commits 007620d

Comments (0)

Files changed (5)

include/GL.ch

-/* cowbel test suite
- *
- * Written in 2012 by David Given.
- *
- * To the extent possible under law, the author has dedicated all copyright
- * and related and neighboring rights to this software to the public domain
- * worldwide. This software is distributed without any warranty.
- *
- * Please see the file COPYING.CC0 in the distribution package for more
- * information.
- */
-
-#ifndef COWBEL_GL
-#define COWBEL_GL
-
-#include <Stdlib.ch>
-
-extern '#include <GL/gl.h>';
-extern '#include <GL/glu.h>';
-
-IMPORT_CONST(GL_COLOR_BUFFER_BIT, int)
-IMPORT_CONST(GL_DEPTH_BUFFER_BIT, int)
-
-IMPORT_CONST(GL_PROJECTION, int)
-IMPORT_CONST(GL_MODELVIEW, int)
-
-IMPORT_CONST(GL_DEPTH_TEST, int)
-
-IMPORT_CONST(GL_LEQUAL, int)
-
-IMPORT_CONST(GL_TRIANGLES, int)
-
-var GL =
-{
-	function Begin(t: int)
-	{
-		extern 'glBegin(${t});';
-	}
-	
-	function ClearColor(red: real, green: real, blue: real, alpha: real)
-	{
-		extern 'glClearColor(${red}, ${green}, ${blue}, ${alpha});';
-	}
-	
-	function Clear(bits: int)
-	{
-		extern 'glClear(${bits});';
-	}
-	
-	function ClearDepth(depth: real)
-	{
-		extern 'glClearDepth(${depth});';
-	}
-
-	function DepthFunc(func: int)
-	{
-		extern 'glDepthFunc(${func});';
-	}
-		
-	function Enable(feature: int)
-	{
-		extern 'glEnable(${feature});';
-	}
-	
-	function End()
-	{
-		extern 'glEnd();';
-	}
-	
-	function LoadIdentity()
-	{
-		extern 'glLoadIdentity();';
-	}
-	
-	function MatrixMode(mode: int)
-	{
-		extern 'glMatrixMode(${mode});';
-	}
-	
-	function Rotate(angle: real, x: real, y: real, z: real)
-	{
-		extern 'glRotated(${angle}, ${x}, ${y}, ${z});';
-	}
-	
-	function Vertex3f(x: real, y: real, z: real)
-	{
-		extern 'glVertex3f(${x}, ${y}, ${z});';
-	}
-	
-	function Viewport(x: int, y: int, w: int, h: int)
-	{
-		extern 'glViewport(${x}, ${y}, ${w}, ${h});';
-	}
-};
-
-var GLU =
-{
-	function Perspective(fovy: real, aspect: real, zNear: real, zFar: real)
-	{
-		extern 'gluPerspective(${fovy}, ${aspect}, ${zNear}, ${zFar});';
-	}
-};
-
-#endif

include/GLFW.ch

-/* cowbel test suite
- *
- * Written in 2012 by David Given.
- *
- * To the extent possible under law, the author has dedicated all copyright
- * and related and neighboring rights to this software to the public domain
- * worldwide. This software is distributed without any warranty.
- *
- * Please see the file COPYING.CC0 in the distribution package for more
- * information.
- */
-
-#ifndef COWBEL_GLFW
-#define COWBEL_GLFW
-
-#include <Stdlib.ch>
-
-extern '#include <GL/glfw.h>';
-
-IMPORT_CONST(GLFW_WINDOW, int)
-
-IMPORT_CONST(GLFW_MOUSE_BUTTON_LEFT, int)
-IMPORT_CONST(GLFW_MOUSE_BUTTON_MIDDLE, int)
-IMPORT_CONST(GLFW_MOUSE_BUTTON_RIGHT, int)
-
-var GLFW =
-{
-	function Init(): (r: int)
-	{
-		r = extern(int);
-		extern '${r} = glfwInit();';
-	}
-	
-	function Terminate()
-	{
-		extern 'glfwTerminate();';
-	}
-	
-	function SetWindowTitle(title: string)
-	{
-		extern 'glfwSetWindowTitle(s_string_cdata(${title}));';
-	}
-	
-	function OpenWindow(width: int, height: int,
-		redbits: int, greenbits: int, bluebits: int,
-		alphabits: int, depthbits: int, stencilbits: int,
-		mode: int): (r: int)
-	{
-		r = extern(int);
-		extern '${r} = glfwOpenWindow(${width}, ${height}, ${redbits}, ${greenbits}, ${bluebits}, ${alphabits}, ${depthbits}, ${stencilbits}, ${mode});';
-	}
-	
-	function CloseWindow()
-	{
-		extern 'glfwCloseWindow();';
-	}
-	
-	function SwapBuffers()
-	{
-		extern 'glfwSwapBuffers();';
-	}
-	
-	function GetKey(key: int): (r: boolean)
-	{
-		r = extern(boolean);
-		extern '${r} = glfwGetKey(${key});';
-	}
-	
-	function GetMousePos(): (x: int, y: int)
-	{
-		x = extern(int);
-		y = extern(int);
-		extern 'glfwGetMousePos(&${x}, &${y});';
-	}
-	
-	function GetMouseButton(button: int): (r: boolean)
-	{
-		r = extern(boolean);
-		extern '${r} = glfwGetMouseButton(${button});';
-	}
-	
-	function GetTime(): (r: real)
-	{
-		r = extern(real);
-		extern '${r} = glfwGetTime();';
-	}
-};
-
-#endif

include/Stdlib.ch

 	}
 };
 
-/** This nasty hack provides an easy way to import a C constant. Use it like
- * this:
- *
- * IMPORT_CONST(INT_MAX, int)
- *
- * (no trailing semicolon)
- */
-#define IMPORT_CONST(n, t) \
-	var n = extern(t); \
-	extern "${" #n "} = " #n ";";
-		
 #endif

samples/glfw.cow

-/* cowbel test suite
- *
- * Written in 2012 by David Given.
- *
- * To the extent possible under law, the author has dedicated all copyright
- * and related and neighboring rights to this software to the public domain
- * worldwide. This software is distributed without any warranty.
- *
- * Please see the file COPYING.CC0 in the distribution package for more
- * information.
- */
-
-#include <SimpleIO.ch>
-#include <GLFW.ch>
-#include <GL.ch>
-
-function drawscene()
-{
-	GL.Clear(GL_COLOR_BUFFER_BIT);
-	GL.LoadIdentity();
-	
-	GL.Rotate(GLFW.GetTime() * 10.0, 0.0, 0.0, 1.0);
-	
-	GL.Begin(GL_TRIANGLES);
-	    GL.Vertex3f( 0.0,  0.5, 0.0);
-	    GL.Vertex3f(-0.5, -0.5, 0.0);
-	    GL.Vertex3f( 0.5, -0.5, 0.0);
-	GL.End();                 
-}
-
-GLFW.Init();
-
-GLFW.OpenWindow(800, 600, 0, 0, 0, 0, 16, 0, GLFW_WINDOW);
-GLFW.SetWindowTitle("Cowbel GLFW test");
-
-GL.Viewport(0, 0, 800, 600);
-GL.ClearColor(0.0, 0.0, 0.0, 0.0);
-GL.ClearDepth(1.0);
-GL.Enable(GL_DEPTH_TEST);
-GL.DepthFunc(GL_LEQUAL);
-
-GL.MatrixMode(GL_PROJECTION);
-GLU.Perspective(45.0, 800.0/600.0, 0.1, 100.0);
-GL.LoadIdentity();
-
-GL.MatrixMode(GL_MODELVIEW);
-GL.LoadIdentity();
-
-while (!GLFW.GetMouseButton(GLFW_MOUSE_BUTTON_LEFT))
-{
-	drawscene();
-	GLFW.SwapBuffers();
-	
-	var x, y = GLFW.GetMousePos();
-	//println("x="+x.toString()+" y="+y.toString());
-}
-
-GLFW.Terminate();

samples/opengl.cow

+/* cowbel test suite
+ *
+ * Written in 2012 by David Given.
+ *
+ * To the extent possible under law, the author has dedicated all copyright
+ * and related and neighboring rights to this software to the public domain
+ * worldwide. This software is distributed without any warranty.
+ *
+ * Please see the file COPYING.CC0 in the distribution package for more
+ * information.
+ */
+
+#include <SimpleIO.ch>
+
+/* This is an incredibly simple program which binds to OpenGL and draws a
+ * simple scene (a spinning triangle). It uses GLFW and GLU as OpenGL support
+ * libraries.
+ *
+ * I haven't done full OpenGL bindings because there's rather a lot of them,
+ * but this demonstrates how it's done.
+ *
+ * Compile with:
+ * 
+ * cowbel -Iinclude -X-O3 -X-lglfw -lGLU -lGL -o a.out samples/opengl.cow
+ */
+
+/* ======================================================================= */
+/*                            GL & GLU BINDINGS                            */
+/* ======================================================================= */
+
+extern '#include <GL/gl.h>';
+extern '#include <GL/glu.h>';
+
+/** This nasty hack provides an easy way to import a C constant. Use it like
+ * this:
+ *
+ * IMPORT_CONST(INT_MAX, int)
+ *
+ * (no trailing semicolon)
+ */
+#define IMPORT_CONST(n, t) \
+	var n = extern(t); \
+	extern "${" #n "} = " #n ";";
+		
+IMPORT_CONST(GL_COLOR_BUFFER_BIT, int)
+IMPORT_CONST(GL_DEPTH_BUFFER_BIT, int)
+
+IMPORT_CONST(GL_PROJECTION, int)
+IMPORT_CONST(GL_MODELVIEW, int)
+
+IMPORT_CONST(GL_DEPTH_TEST, int)
+
+IMPORT_CONST(GL_LEQUAL, int)
+
+IMPORT_CONST(GL_TRIANGLES, int)
+
+var GL =
+{
+	function Begin(t: int)
+	{
+		extern 'glBegin(${t});';
+	}
+	
+	function ClearColor(red: real, green: real, blue: real, alpha: real)
+	{
+		extern 'glClearColor(${red}, ${green}, ${blue}, ${alpha});';
+	}
+	
+	function Clear(bits: int)
+	{
+		extern 'glClear(${bits});';
+	}
+	
+	function ClearDepth(depth: real)
+	{
+		extern 'glClearDepth(${depth});';
+	}
+
+	function DepthFunc(func: int)
+	{
+		extern 'glDepthFunc(${func});';
+	}
+		
+	function Enable(feature: int)
+	{
+		extern 'glEnable(${feature});';
+	}
+	
+	function End()
+	{
+		extern 'glEnd();';
+	}
+	
+	function LoadIdentity()
+	{
+		extern 'glLoadIdentity();';
+	}
+	
+	function MatrixMode(mode: int)
+	{
+		extern 'glMatrixMode(${mode});';
+	}
+	
+	function Rotate(angle: real, x: real, y: real, z: real)
+	{
+		extern 'glRotated(${angle}, ${x}, ${y}, ${z});';
+	}
+	
+	function Vertex3f(x: real, y: real, z: real)
+	{
+		extern 'glVertex3f(${x}, ${y}, ${z});';
+	}
+	
+	function Viewport(x: int, y: int, w: int, h: int)
+	{
+		extern 'glViewport(${x}, ${y}, ${w}, ${h});';
+	}
+};
+
+var GLU =
+{
+	function Perspective(fovy: real, aspect: real, zNear: real, zFar: real)
+	{
+		extern 'gluPerspective(${fovy}, ${aspect}, ${zNear}, ${zFar});';
+	}
+};
+
+/* ======================================================================= */
+/*                              GLFW BINDINGS                              */
+/* ======================================================================= */
+
+extern '#include <GL/glfw.h>';
+
+IMPORT_CONST(GLFW_WINDOW, int)
+
+IMPORT_CONST(GLFW_MOUSE_BUTTON_LEFT, int)
+IMPORT_CONST(GLFW_MOUSE_BUTTON_MIDDLE, int)
+IMPORT_CONST(GLFW_MOUSE_BUTTON_RIGHT, int)
+
+var GLFW =
+{
+	function Init(): (r: int)
+	{
+		r = extern(int);
+		extern '${r} = glfwInit();';
+	}
+	
+	function Terminate()
+	{
+		extern 'glfwTerminate();';
+	}
+	
+	function SetWindowTitle(title: string)
+	{
+		extern 'glfwSetWindowTitle(s_string_cdata(${title}));';
+	}
+	
+	function OpenWindow(width: int, height: int,
+		redbits: int, greenbits: int, bluebits: int,
+		alphabits: int, depthbits: int, stencilbits: int,
+		mode: int): (r: int)
+	{
+		r = extern(int);
+		extern '${r} = glfwOpenWindow(${width}, ${height}, ${redbits}, ${greenbits}, ${bluebits}, ${alphabits}, ${depthbits}, ${stencilbits}, ${mode});';
+	}
+	
+	function CloseWindow()
+	{
+		extern 'glfwCloseWindow();';
+	}
+	
+	function SwapBuffers()
+	{
+		extern 'glfwSwapBuffers();';
+	}
+	
+	function GetKey(key: int): (r: boolean)
+	{
+		r = extern(boolean);
+		extern '${r} = glfwGetKey(${key});';
+	}
+	
+	function GetMousePos(): (x: int, y: int)
+	{
+		x = extern(int);
+		y = extern(int);
+		extern 'glfwGetMousePos(&${x}, &${y});';
+	}
+	
+	function GetMouseButton(button: int): (r: boolean)
+	{
+		r = extern(boolean);
+		extern '${r} = glfwGetMouseButton(${button});';
+	}
+	
+	function GetTime(): (r: real)
+	{
+		r = extern(real);
+		extern '${r} = glfwGetTime();';
+	}
+};
+
+/* ======================================================================= */
+/*                              MAIN PROGRAM                               */
+/* ======================================================================= */
+
+function drawscene()
+{
+	GL.Clear(GL_COLOR_BUFFER_BIT);
+	GL.LoadIdentity();
+	
+	GL.Rotate(GLFW.GetTime() * 10.0, 0.0, 0.0, 1.0);
+	
+	GL.Begin(GL_TRIANGLES);
+	    GL.Vertex3f( 0.0,  0.5, 0.0);
+	    GL.Vertex3f(-0.5, -0.5, 0.0);
+	    GL.Vertex3f( 0.5, -0.5, 0.0);
+	GL.End();                 
+}
+
+GLFW.Init();
+
+GLFW.OpenWindow(800, 600, 0, 0, 0, 0, 16, 0, GLFW_WINDOW);
+GLFW.SetWindowTitle("Cowbel GLFW test");
+
+GL.Viewport(0, 0, 800, 600);
+GL.ClearColor(0.0, 0.0, 0.0, 0.0);
+GL.ClearDepth(1.0);
+GL.Enable(GL_DEPTH_TEST);
+GL.DepthFunc(GL_LEQUAL);
+
+GL.MatrixMode(GL_PROJECTION);
+GLU.Perspective(45.0, 800.0/600.0, 0.1, 100.0);
+GL.LoadIdentity();
+
+GL.MatrixMode(GL_MODELVIEW);
+GL.LoadIdentity();
+
+while (!GLFW.GetMouseButton(GLFW_MOUSE_BUTTON_LEFT))
+{
+	drawscene();
+	GLFW.SwapBuffers();
+	
+	var x, y = GLFW.GetMousePos();
+	//println("x="+x.toString()+" y="+y.toString());
+}
+
+GLFW.Terminate();