#!/usr/bin/env ascript

// Orignal source file is written in C and is available from the site below.
// http://www.sgi.com/products/software/opengl/examples/samples/
// bitmap1.c

import(glu) {*}
import(opengl) {*}

OPENGL_WIDTH = 24
OPENGL_HEIGHT = 13

boxA = @{0, 0, 0}
boxB = @{-100, 0, 0}
boxC = @{100, 0, 0}
boxD = @{0, 95, 0}
boxE = @{0, -105, 0}

OpenGL_bits1 = @{
	0x00, 0x03, 0x00,
	0x7f, 0xfb, 0xff,
	0x7f, 0xfb, 0xff,
	0x00, 0x03, 0x00,
	0x3e, 0x8f, 0xb7,
	0x63, 0xdb, 0xb0,
	0x63, 0xdb, 0xb7,
	0x63, 0xdb, 0xb6,
	0x63, 0x8f, 0xf3,
	0x63, 0x00, 0x00,
	0x63, 0x00, 0x00,
	0x63, 0x00, 0x00,
	0x3e, 0x00, 0x00,
}.pack(e"${3 * 13}B")

OpenGL_bits2 = @{
	0x00, 0x00, 0x00,
	0xff, 0xff, 0x01,
	0xff, 0xff, 0x01,
	0x00, 0x00, 0x00,
	0xf9, 0xfc, 0x01,
	0x8d, 0x0d, 0x00,
	0x8d, 0x0d, 0x00,
	0x8d, 0x0d, 0x00,
	0xcc, 0x0d, 0x00,
	0x0c, 0x4c, 0x0a,
	0x0c, 0x4c, 0x0e,
	0x8c, 0xed, 0x0e,
	0xf8, 0x0c, 0x00,
}.pack(e"${3 * 13}B")

logo_bits = @{
	0x00, 0x66, 0x66,
	0xff, 0x66, 0x66,
	0x00, 0x00, 0x00,
	0xff, 0x3c, 0x3c,
	0x00, 0x42, 0x40,
	0xff, 0x42, 0x40,
	0x00, 0x41, 0x40,
	0xff, 0x21, 0x20,
	0x00, 0x2f, 0x20,
	0xff, 0x20, 0x20,
	0x00, 0x10, 0x90,
	0xff, 0x10, 0x90,
	0x00, 0x0f, 0x10,
	0xff, 0x00, 0x00,
	0x00, 0x66, 0x66,
	0xff, 0x66, 0x66,
}.pack(e"${3 * 16}B")

Init() = {
	glClearColor(0.0, 0.0, 0.0, 0.0)
	glClearIndex(0.0)
}

Reshape(width:number, height:number) = {
	glViewport(0, 0, width, height)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	gluOrtho2D(-175, 175, -175, 175)
	glMatrixMode(GL_MODELVIEW)
}

Draw() = {
	glClear(GL_COLOR_BUFFER_BIT)
	glRasterPos3fv(boxA)
	glPixelStorei(GL_UNPACK_ROW_LENGTH, 24)
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, 8)
	glPixelStorei(GL_UNPACK_SKIP_ROWS, 2)
	glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE)
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
	glBitmap(16, 12, 8.0, 0.0, 0.0, 0.0, logo_bits)
	glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)
	glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0)
	glPixelStorei(GL_UNPACK_SKIP_ROWS, 0)
	glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE)
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
	glColor3f(1.0, 1.0, 1.0)
	glRasterPos3fv(boxB)
	glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
		 OpenGL_bits1)
	glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
		OpenGL_bits2)
	glColor3f(0.0, 1.0, 1.0)
	glRasterPos3fv(boxC)
	glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
		OpenGL_bits1)
	glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
		OpenGL_bits2)
	glColor3f(1.0, 0.0, 1.0)
	glRasterPos3fv(boxD)
	glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
		OpenGL_bits1)
	glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
		OpenGL_bits2)
	glColor3f(1.0, 1.0, 0.0)
	glRasterPos3fv(boxE)
	glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
		OpenGL_bits1)
	glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
		OpenGL_bits2)
	glFlush()
}

lambda {
	import(gltester)
	gltester.mainloop(300, 300, 0, `none) {
		`onInit => lambda {
			Init()
		}
		`onResize => lambda(width:number, height:number) {
			Reshape(width, height)
		}
		`onDraw => lambda {
			Draw()
		}
	}
}()
