Thursday, July 9, 2009

Call MSSQL database using pyAMF


from pyamf.remoting.gateway.wsgi import WSGIGateway

import adodbapi
import dtuple
import ols
import numpy
import csv

db = adodbapi.connect ("Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=TxDB;Integrated Security=SSPI;")

def stats(data,d,i):
"""
Returns summary statistics
y: y-vector
x: x-array
d: Name of dependent variable
i: List of independent variables
"""
dd = numpy.array(data)
y = dd[:,0]
x = dd[:,1:]
try:
m = ols.ols(y, x, y_varnm = d, x_varnm = i)
return m.report()
except:
return "Error in calculation"

def csv(fname,data):
"""
write data to csv file
"""
w = csv.writer(open(fname, "wb"))
w.writerows(data)
return 1

def doSQL(sql,mode):
"""
Run SQL on db
mode 0 = put
mode 1 = get
"""
global db
c = db.cursor()
c.execute(sql)
if mode:
raw_rows = c.fetchall()
desc = dtuple.TupleDescriptor(c.description)
res=[]
for row in raw_rows:
res.append(dtuple.DatabaseTuple(desc, row).asMapping())
return res
else:
db.commit()

def dbClose():
"""
Close db connection
"""
global db
db.close()

def testLogin():
return True;

def auth(uid, pwd):
"""
Authenticate the user.
"""
if uid == 'your encoded uid' and pwd == 'your encoded pwd' :
return True

return False

services = {
'stats' : stats,
'csv' : csv,
'doSQL' : doSQL,
'dbClose' : dbClose,
'testLogin' : testLogin
}

application = WSGIGateway(services,authenticator=auth)

Python script to extract excel block and format for latex table - requires pyGTK and pyExelerator


#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk, gobject
import win32clipboard, win32con, random
from pyExcelerator import *

# Check for new pygtk: this is new class in PyGtk 2.4
if gtk.pygtk_version < (2,3,90):
print "PyGtk 2.3.90 or later required for this example"
raise SystemExit

dialog = gtk.FileChooserDialog("Open..",
None,
gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))

dialog.set_default_response(gtk.RESPONSE_OK)

filter = gtk.FileFilter()
filter.set_name("Excel files")
filter.add_pattern("*.xls")
dialog.add_filter(filter)
dialog.set_current_folder("F:\\Projects\\2009\\NCAT\\Synthesis\\Excel files\\")

response = dialog.run()

if response == gtk.RESPONSE_OK:
xl = dialog.get_filename()
print xl
data = parse_xls(xl)
sheet_name=data[3][0].encode('cp866', 'backslashreplace')
values = data[3][1]
s1 = " " + str(int(values[(4,1)])) + " & " + str('%.2f' % round(values[(4,2)],2)) + " & " + str('%.2f' % round(values[(4,3)],2)) + " & " + str('%.2f' % round(values[(4,7)],2)) + " & " + str('%.2f' % round(values[(4,8)],2)) + " & " + str('%.2f' % round(values[(4,12)],2)) + " & " + str('%.2f' % round(values[(4,13)],2)) + r" \\" + "\r"
s2 = " " + str(int(values[(5,1)])) + " & " + str('%.2f' % round(values[(5,2)],2)) + " & " + str('%.2f' % round(values[(5,3)],2)) + " & " + str('%.2f' % round(values[(5,7)],2)) + " & " + str('%.2f' % round(values[(5,8)],2)) + " & " + str('%.2f' % round(values[(5,12)],2)) + " & " + str('%.2f' % round(values[(5,13)],2)) + r" \\" + "\r"
s3 = " " + str(int(values[(6,1)])) + " & " + str('%.2f' % round(values[(6,2)],2)) + " & " + str('%.2f' % round(values[(6,3)],2)) + " & " + str('%.2f' % round(values[(6,7)],2)) + " & " + str('%.2f' % round(values[(6,8)],2)) + " & " + str('%.2f' % round(values[(6,12)],2)) + " & " + str('%.2f' % round(values[(6,13)],2)) + r" \\" + "\r"
s4 = " " + str(int(values[(7,1)])) + " & " + str('%.2f' % round(values[(7,2)],2)) + " & " + str('%.2f' % round(values[(7,3)],2)) + " & " + str('%.2f' % round(values[(7,7)],2)) + " & " + str('%.2f' % round(values[(7,8)],2)) + " & " + str('%.2f' % round(values[(7,12)],2)) + " & " + str('%.2f' % round(values[(7,13)],2)) + r" \\" + "\r"
s5 = " " + str(int(values[(8,1)])) + " & " + str('%.2f' % round(values[(8,2)],2)) + " & " + str('%.2f' % round(values[(8,3)],2)) + " & " + str('%.2f' % round(values[(8,7)],2)) + " & " + str('%.2f' % round(values[(8,8)],2)) + " & " + str('%.2f' % round(values[(8,12)],2)) + " & " + str('%.2f' % round(values[(8,13)],2)) + r" \\" + "\r"
s6 = str(int(values[(9,1)])) + " & " + str('%.2f' % round(values[(9,2)],2)) + " & " + str('%.2f' % round(values[(9,3)],2)) + " & " + str('%.2f' % round(values[(9,7)],2)) + " & " + str('%.2f' % round(values[(9,8)],2)) + " & " + str('%.2f' % round(values[(9,12)],2)) + " & " + str('%.2f' % round(values[(9,13)],2)) + r" \\" + "\r"
s7 = str(int(values[(10,1)])) + " & " + str('%.2f' % round(values[(10,2)],2)) + " & " + str('%.2f' % round(values[(10,3)],2)) + " & " + str('%.2f' % round(values[(10,7)],2)) + " & " + str('%.2f' % round(values[(10,8)],2)) + " & " + str('%.2f' % round(values[(10,12)],2)) + " & " + str('%.2f' % round(values[(10,13)],2)) + r" \\" + "\r"
s8 = str(int(values[(11,1)])) + " & " + str('%.2f' % round(values[(11,2)],2)) + " & " + str('%.2f' % round(values[(11,3)],2)) + " & " + str('%.2f' % round(values[(11,7)],2)) + " & " + str('%.2f' % round(values[(11,8)],2)) + " & " + str('%.2f' % round(values[(11,12)],2)) + " & " + str('%.2f' % round(values[(11,13)],2)) + r" \\" + "\r"
s9 = str(int(values[(12,1)])) + " & " + str('%.2f' % round(values[(12,2)],2)) + " & " + str('%.2f' % round(values[(12,3)],2)) + " & " + str('%.2f' % round(values[(12,7)],2)) + " & " + str('%.2f' % round(values[(12,8)],2)) + " & " + str('%.2f' % round(values[(12,12)],2)) + " & " + str('%.2f' % round(values[(12,13)],2)) + r" \\" + "\r"
s10 = str(int(values[(13,1)])) + " & " + str('%.2f' % round(values[(13,2)],2)) + " & " + str('%.2f' % round(values[(13,3)],2)) + " & " + str('%.2f' % round(values[(13,7)],2)) + " & " + str('%.2f' % round(values[(13,8)],2)) + " & " + str('%.2f' % round(values[(13,12)],2)) + " & " + str('%.2f' % round(values[(13,13)],2)) + r" \\" + "\r"
s11 = str(int(values[(14,1)])) + " & " + str('%.2f' % round(values[(14,2)],2)) + " & " + str('%.2f' % round(values[(14,3)],2)) + " & " + str('%.2f' % round(values[(14,7)],2)) + " & " + str('%.2f' % round(values[(14,8)],2)) + " & " + str('%.2f' % round(values[(14,12)],2)) + " & " + str('%.2f' % round(values[(14,13)],2)) + r" \\" + "\r"
s12 = str(int(values[(15,1)])) + " & " + str('%.2f' % round(values[(15,2)],2)) + " & " + str('%.2f' % round(values[(15,3)],2)) + " & " + str('%.2f' % round(values[(15,7)],2)) + " & " + str('%.2f' % round(values[(15,8)],2)) + " & " + str('%.2f' % round(values[(15,12)],2)) + " & " + str('%.2f' % round(values[(15,13)],2)) + r" \\"
s = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12
#cb = gtk.Clipboard()
#cb.set_text("Hello", len=-1)
win32clipboard.OpenClipboard()
win32clipboard.SetClipboardData(win32con.CF_TEXT, s)
win32clipboard.CloseClipboard()
#raw_input('Hit any key to continue...')
elif response == gtk.RESPONSE_CANCEL:
print 'Closed, no files selected'

dialog.destroy()


NeHe Lesson 4 using SDL and Timer for smoother animation


#include
#include
#include "SDL/SDL.h"

#define WIDTH 640
#define HEIGHT 480
#define FPS (33/10)*10
#define EVENT_REDRAW 0

float rtri=0.0f;
float rquad=0.0f;
int done=0;
SDL_TimerID redraw_timer;

void InitGL(int Width, int Height)
{
glViewport(0,0,Width,Height);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}

void DrawGLScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glRotatef(rtri,0.0f,1.0f,0.0f);
glBegin(GL_POLYGON);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
glLoadIdentity();
glTranslatef(1.5f,0.0f,-6.0f);
glRotatef(rquad,1.0f,0.0f,0.0f);
glColor3f(0.5f,0.5f,1.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
rtri+=15.0f;
rquad-=15.0f;
SDL_GL_SwapBuffers();
}

Uint32 redraw_timer_callback(Uint32 interval, void *param)
{
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = EVENT_REDRAW;
event.user.data1 = NULL;
event.user.data2 = NULL;

SDL_PushEvent(&event);
return interval;
}

void GameLoop(void)
{
SDL_Event event;
int t1;
while (!done)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_USEREVENT:
switch (event.user.code)
{
case EVENT_REDRAW:
DrawGLScene();
break;
}
break;
case SDL_QUIT:
done=1;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym==SDLK_ESCAPE) done = 1;
break;
}
}
SDL_RemoveTimer(redraw_timer);
}

int Init(int Width, int Height)
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)<0)
{
fprintf(stderr,"Unable to initialize SDL: %s\n",SDL_GetError());
return 1;
}
if (SDL_SetVideoMode(Width,Height,0,SDL_OPENGL)==NULL)
{
fprintf(stderr,"Unable to create OpenGL screen: %s\n",SDL_GetError());
SDL_Quit();
return 1;
}
SDL_WM_SetCaption("Lesson 04 with Timer",NULL);
InitGL(Width,Height);
redraw_timer = SDL_AddTimer(FPS, redraw_timer_callback, NULL);
return 0;
}

int main(int argc, char **argv)
{
SDL_Event event;
if (Init(WIDTH,HEIGHT)) return 1;
GameLoop();
SDL_Quit();
return 0;
}

piczle


// piczle.c - the*butler - Jan '09
// generate a shuffle or sliding block puzzle from a picture
// free as in beer
// compiled using: gcc -o piczle piczle.c -lGL -lGLU -lSDL (-lftgl)
// I removed the font stuff for now
// Also bitmap used (p01.rgba) is a headerless (raw) bitmap of
// size 512 x 512. I used Gimp & ImageMagick's convert to generate it.
//download it here: freevryheid.x10hosting.com/piczle.zip

#include
#include
#include
#include
#include
#include
//#include // openGL fonts

#define randrange(N) (rand()/(RAND_MAX/(N)+1))
#define NTEXTURES 17 // 1 x Raw (512 x 512) + 16 x Puzzle blocks (128 x 128)
#define WIDTH 1024
#define HEIGHT 768
#define NFPS (33/10)*10
#define EVENT_REDRAW 0

// define clickable areas
typedef struct {
GLfloat x1,y1,x2,y2;
} area;
area a[19];

enum areas
{
RAW,PUZ,INP,
B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12,B13,B14,B15,B16
};

int done=0;
SDL_TimerID redraw_timer;
//FTGLfont *font;
float width,height;
GLuint tex[NTEXTURES]; // array for opengl textures
int rnd[16]; // 16 randomized blocks for puzzle

//test if click is in defined area
int inArea(area A, int x, int y)
{
if ((x>A.x1) && (xA.y1) && (y else return 0;
}

void LoadTexture(char * f)
{
void * buff = 0;
int sob; // Size of buffer
FILE * fp;
fp=fopen(f,"rb");
fseek(fp,0,SEEK_END);
sob=ftell(fp);
buff=malloc(sob);
fseek(fp,0,SEEK_SET);
fread(buff, sob, 1, fp);
fclose(fp);

glBindTexture(GL_TEXTURE_2D, tex[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void *)buff);
free(buff);
glNewList(1, GL_COMPILE);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, tex[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f( 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f( 512.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f( 512.0f, 512.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f( 0.0f, 512.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glEndList();
}

// calc & print frames per second
float fps(void)
{
static int frame,tb;
int t;
float f;

frame++;
t=SDL_GetTicks();
if (t-tb>1000)
{
f=frame*1000.0/(t-tb);
tb=t;
frame=0;
printf("FPS: %f\n",f);
}
return f;
}

void doPuzzle(void)
{
int i,j,k=0;
for (j=0;j<=3;j++)
{
for (i=0;i<=3;i++)
{
glLoadIdentity();
glPushMatrix();
glTranslatef(512+i*128, j*128, 0);
glCallList(rnd[k]+1);
glPopMatrix();
k++;
}
}
}

void doRaw(void)
{
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glCallList(1);
glPopMatrix();
}

void DrawGLScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
doRaw();
doPuzzle();
//fps();
SDL_GL_SwapBuffers();
}

void GameLoop(void)
{
SDL_Event event;
while (!done)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_USEREVENT:
switch (event.user.code)
{
case EVENT_REDRAW:
DrawGLScene();
break;
}
break;
case SDL_QUIT:
done=1;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym==SDLK_ESCAPE) done = 1;
break;
case SDL_MOUSEMOTION:
//Change mouse cursor depending where we are on the screen
//if (inArea(a[PUZ],event.motion.x,event.motion.y)) SDL_ShowCursor(SDL_ENABLE);
//else SDL_ShowCursor(SDL_DISABLE);
break;
case SDL_MOUSEBUTTONDOWN:
if (inArea(a[RAW],event.button.x,event.button.y))
{
break;
}
else if (inArea(a[B01],event.button.x,event.button.y))
{
if (rnd[1]==16)
{
// move right
rnd[1]=rnd[0];
rnd[0]=16;
}
else if (rnd[4]==16)
{
// move down
rnd[4]=rnd[0];
rnd[0]=16;
}
//printf("Block 1\n");
}
else if (inArea(a[B05],event.button.x,event.button.y))
{
if (rnd[0]==16)
{
// move up
rnd[0]=rnd[4];
rnd[4]=16;
}
else if (rnd[5]==16)
{
// move right
rnd[5]=rnd[4];
rnd[4]=16;
}
else if (rnd[8]==16)
{
// move down
rnd[8]=rnd[4];
rnd[4]=16;
}
//printf("Block 5\n");
}
else if (inArea(a[B09],event.button.x,event.button.y))
{
if (rnd[4]==16)
{
// move up
rnd[4]=rnd[8];
rnd[8]=16;
}
else if (rnd[9]==16)
{
// move right
rnd[9]=rnd[8];
rnd[8]=16;
}
else if (rnd[12]==16)
{
// move down
rnd[12]=rnd[8];
rnd[8]=16;
}
//printf("Block 9\n");
}
else if (inArea(a[B13],event.button.x,event.button.y))
{
if (rnd[8]==16)
{
// move up
rnd[8]=rnd[12];
rnd[12]=16;
}
else if (rnd[13]==16)
{
// move right
rnd[13]=rnd[12];
rnd[12]=16;
}
//printf("Block 13\n");
}
else if (inArea(a[B02],event.button.x,event.button.y))
{
if (rnd[0]==16)
{
// move left
rnd[0]=rnd[1];
rnd[1]=16;
}
else if (rnd[2]==16)
{
// move right
rnd[2]=rnd[1];
rnd[1]=16;
}
else if (rnd[5]==16)
{
// move down
rnd[5]=rnd[1];
rnd[1]=16;
}
//printf("Block 2\n");
}
else if (inArea(a[B06],event.button.x,event.button.y))
{
if (rnd[1]==16)
{
// move up
rnd[1]=rnd[5];
rnd[5]=16;
}
else if (rnd[6]==16)
{
// move right
rnd[6]=rnd[5];
rnd[5]=16;
}
else if (rnd[9]==16)
{
// move down
rnd[9]=rnd[5];
rnd[5]=16;
}
else if (rnd[4]==16)
{
// move left
rnd[4]=rnd[5];
rnd[5]=16;
}
//printf("Block 6\n");
}
else if (inArea(a[B10],event.button.x,event.button.y))
{
if (rnd[5]==16)
{
// move up
rnd[5]=rnd[9];
rnd[9]=16;
}
else if (rnd[10]==16)
{
// move right
rnd[10]=rnd[9];
rnd[9]=16;
}
else if (rnd[13]==16)
{
// move down
rnd[13]=rnd[9];
rnd[9]=16;
}
else if (rnd[8]==16)
{
// move left
rnd[8]=rnd[9];
rnd[9]=16;
}
//printf("Block 10\n");
}
else if (inArea(a[B14],event.button.x,event.button.y))
{
if (rnd[9]==16)
{
// move up
rnd[9]=rnd[13];
rnd[13]=16;
}
else if (rnd[14]==16)
{
// move right
rnd[14]=rnd[13];
rnd[13]=16;
}
else if (rnd[12]==16)
{
// move left
rnd[12]=rnd[13];
rnd[13]=16;
}
//printf("Block 14\n");
}
else if (inArea(a[B03],event.button.x,event.button.y))
{
if (rnd[1]==16)
{
// move left
rnd[1]=rnd[2];
rnd[2]=16;
}
else if (rnd[3]==16)
{
// move right
rnd[3]=rnd[2];
rnd[2]=16;
}
else if (rnd[6]==16)
{
// move down
rnd[6]=rnd[2];
rnd[2]=16;
}
//printf("Block 3\n");
}
else if (inArea(a[B07],event.button.x,event.button.y))
{
if (rnd[2]==16)
{
// move up
rnd[2]=rnd[6];
rnd[6]=16;
}
else if (rnd[7]==16)
{
// move right
rnd[7]=rnd[6];
rnd[6]=16;
}
else if (rnd[10]==16)
{
// move down
rnd[10]=rnd[6];
rnd[6]=16;
}
else if (rnd[5]==16)
{
// move left
rnd[5]=rnd[6];
rnd[6]=16;
}
//printf("Block 7\n");
}
else if (inArea(a[B11],event.button.x,event.button.y))
{
if (rnd[6]==16)
{
// move up
rnd[6]=rnd[10];
rnd[10]=16;
}
else if (rnd[11]==16)
{
// move right
rnd[11]=rnd[10];
rnd[10]=16;
}
else if (rnd[14]==16)
{
// move down
rnd[14]=rnd[10];
rnd[10]=16;
}
else if (rnd[9]==16)
{
// move left
rnd[9]=rnd[10];
rnd[10]=16;
}
//printf("Block 11\n");
}
else if (inArea(a[B15],event.button.x,event.button.y))
{
if (rnd[10]==16)
{
// move up
rnd[10]=rnd[14];
rnd[14]=16;
}
else if (rnd[15]==16)
{
// move right
rnd[15]=rnd[14];
rnd[14]=16;
}
else if (rnd[13]==16)
{
// move left
rnd[13]=rnd[14];
rnd[14]=16;
}
//printf("Block 15\n");
}
else if (inArea(a[B04],event.button.x,event.button.y))
{
if (rnd[2]==16)
{
//move left
rnd[2]=rnd[3];
rnd[3]=16;
}
else if (rnd[7]==16)
{
// move down
rnd[7]=rnd[3];
rnd[3]=16;
}
//printf("Block 4\n");
}
else if (inArea(a[B08],event.button.x,event.button.y))
{
if (rnd[3]==16)
{
// move up
rnd[3]=rnd[7];
rnd[7]=16;
}
else if (rnd[11]==16)
{
// move down
rnd[11]=rnd[7];
rnd[7]=16;
}
else if (rnd[6]==16)
{
// move left
rnd[6]=rnd[7];
rnd[7]=16;
}
//printf("Block 8\n");
}
else if (inArea(a[B12],event.button.x,event.button.y))
{
if (rnd[7]==16)
{
// move up
rnd[7]=rnd[11];
rnd[11]=16;
}
else if (rnd[15]==16)
{
// move down
rnd[15]=rnd[11];
rnd[11]=16;
}
else if (rnd[10]==16)
{
// move left
rnd[10]=rnd[11];
rnd[11]=16;
}
//printf("Block 12\n");
}
else if (inArea(a[B16],event.button.x,event.button.y))
{
if (rnd[11]==16)
{
// move up
rnd[11]=rnd[15];
rnd[15]=16;
}
else if (rnd[14]==16)
{
// move left
rnd[14]=rnd[15];
rnd[15]=16;
}
//printf("Block 16\n");
}
if ((rnd[0]==1) && (rnd[1]==2) && (rnd[2]==3) && (rnd[3]==4)
&& (rnd[4]==5) && (rnd[5]==6) && (rnd[6]==7) && (rnd[7]==8)
&& (rnd[8]==9) && (rnd[9]==10) && (rnd[10]==11) && (rnd[11]==12)
&& (rnd[12]==13) && (rnd[13]==14) && (rnd[14]==15) && (rnd[15]==16))
{
printf("Congrats - completed");
}
break;
}
}
}

Uint32 redraw_timer_callback(Uint32 interval, void *param)
{
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = EVENT_REDRAW;
event.user.data1 = NULL;
event.user.data2 = NULL;

SDL_PushEvent(&event);
return interval;
}

void Tiles(void)
{
int i,j,k=0;
for (j=0;j<=3;j++)
{
for (i=0;i<=3;i++)
{
glBindTexture(GL_TEXTURE_2D, tex[k+1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, i*128, height-128*(j+1), 128, 128, 0);
glNewList(k+2, GL_COMPILE);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, tex[k+1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f( 0.0f, 128.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f( 128.0f, 128.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f( 128.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f( 0.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glEndList();
k++;
}
}
}

// assign random sequence of ints for rnd[]
void Shuffle()
{
int p,c,t;
int nvalues = 16;
for(p = 0; p < nvalues; p++) rnd[p] = p + 1;
for(p = 0; p < nvalues-1; p++)
{
c = randrange(nvalues-p);
t = rnd[p]; rnd[p] = rnd[p+c]; rnd[p+c] = t;
}
}

// Set up 2D screen
void InitGL(int Width, int Height)
{
width=(float)Width;height=(float)Height;
glViewport(0,0,Width,Height);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Width, Height, 0, 0, 1);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Displacement trick for exact pixelization
glTranslatef(0.375, 0.375, 0);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.01);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glGenTextures(NTEXTURES, &tex[0]);
}

void Areas(void)
{
// init areas
a[RAW].x1=0; a[RAW].x2=512; a[RAW].y1=0; a[RAW].y2=512;
a[PUZ].x1=512; a[PUZ].x2=1024; a[PUZ].y1=0; a[PUZ].y2=512;
a[INP].x1=0; a[INP].x2=1024; a[INP].y1=512; a[INP].y2=768;

a[B01].x1=512; a[B01].x2=640; a[B01].y1=0; a[B01].y2=128;
a[B02].x1=640; a[B02].x2=768; a[B02].y1=0; a[B02].y2=128;
a[B03].x1=768; a[B03].x2=896; a[B03].y1=0; a[B03].y2=128;
a[B04].x1=896; a[B04].x2=1024; a[B04].y1=0; a[B04].y2=128;
a[B05].x1=512; a[B05].x2=640; a[B05].y1=128; a[B05].y2=256;
a[B06].x1=640; a[B06].x2=768; a[B06].y1=128; a[B06].y2=256;
a[B07].x1=768; a[B07].x2=896; a[B07].y1=128; a[B07].y2=256;
a[B08].x1=896; a[B08].x2=1024; a[B08].y1=128; a[B08].y2=256;
a[B09].x1=512; a[B09].x2=640; a[B09].y1=256; a[B09].y2=384;
a[B10].x1=640; a[B10].x2=768; a[B10].y1=256; a[B10].y2=384;
a[B11].x1=768; a[B11].x2=896; a[B11].y1=256; a[B11].y2=384;
a[B12].x1=896; a[B12].x2=1024; a[B12].y1=256; a[B12].y2=384;
a[B13].x1=512; a[B13].x2=640; a[B13].y1=384; a[B13].y2=512;
a[B14].x1=640; a[B14].x2=768; a[B14].y1=384; a[B14].y2=512;
a[B15].x1=768; a[B15].x2=896; a[B15].y1=384; a[B15].y2=512;
a[B16].x1=896; a[B16].x2=1024; a[B16].y1=384; a[B16].y2=512;
}

// Initiate SDL, FTGL & openGL
int Init(int Width, int Height)
{
srand(time(NULL)); // Randomize things
Shuffle(); // Assign rnd[]
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)<0)
{
fprintf(stderr,"Unable to initialize SDL: %s\n",SDL_GetError());
return 1;
}
if (SDL_SetVideoMode(Width,Height,0,SDL_OPENGL | SDL_FULLSCREEN)==NULL)
{
fprintf(stderr,"Unable to create OpenGL screen: %s\n",SDL_GetError());
SDL_Quit();
return 1;
}
// locate ttf fonts on your system and replace with what you want/got
/*
font=ftglCreatePixmapFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf");
if(!font)
{
printf("Unable to create font \n");
return 1;
}
ftglSetFontFaceSize(font, 72, 72);
*/
// SDL_WM_SetCaption("piczle",NULL); // not needed as running fullscreen
InitGL(Width,Height);
LoadTexture("p01.rgba");
//blit the texture to the screen so we can copy it
doRaw();
//copy & randomize it
Tiles();
Areas();
redraw_timer = SDL_AddTimer(NFPS, redraw_timer_callback, NULL);
return 0;
}

void Quit(void)
{
SDL_RemoveTimer(redraw_timer);
SDL_Quit();
// ftglDestroyFont(font);
}

int main(int argc, char **argv)
{
if (Init(WIDTH,HEIGHT)) return 1;
GameLoop();
Quit();
return 0;
}

simple test for compiler speed


#include
#include
#include

#define NRUNS 1000

int main()
{
long h=0,i,j,k;
double d=0,pi = 4.0*atan(1.0);
time_t start,end;
printf("=====================================================\n");
printf("= Simple exection speed test - the*butler - Jan '09 =\n");
printf("=====================================================\n");
printf("Starting now ...");
start=clock();
for (i=0;i {
for (j=0;j {
for (k=0;k {
d+=sqrt(pi);
h++;
}
}
}
end=clock();
printf(" done. \n");
printf("h: %d \n", h);
printf("d: %f \n", d);
printf("Time (seconds): %15.3f \n", (double)(end-start)/CLOCKS_PER_SEC);
printf("=====================================================\n");
return 0;
}



Simple makefile

Texture wavelength spectra



#from scipy import *

from numpy import *

from pylab import *



print '==============================='

print 'ULIP texture wavelength spectra'

print 'AdF Smit - UTA - APR 08'

print '==============================='

print ''

# 1/3rd octave bands

f13 = array([1.000, 1.250, 1.600, 2.000, 2.500, 3.150, \

4.000, 5.000, 6.300, 8.000, 10.00, 12.50, \

16.00, 20.00, 25.00, 31.50, 40.00, 50.00, \

63.00, 80.00, 100.0, 125.0, 160.0, 200.0, \

250.0, 315.0, 400.0, 500.0, 630.0, 800.0, 1000.0])

print 'Determine lower and upper limits of each 1/3 octave band ...'

b13=zeros((2,31))

b13[0,]=f13/2.**(1./6.)

b13[1,]=f13*2.**(1./6.)

print 'Read in TOT file ...'

file='DATA01.TOT'

y=load(file)

m=len(y)

print 'Data points in file:',m

y=y/1000 # convert to m

base=50 #base length for moving average

print 'Apply moving average with base length',base,'...'

print 'This is a low-pass filter to extract the macrotexture profile'

print 'from the megatexture or roughness profile'

y1=movavg(y,base)

n=len(y1)

y=y[base/2:m-base/2]

y1=y1[1:n]

y2=y1-y

print 'Run FFT ...'

y3=fft(y2)

print 'Calculate power amplitudes and wavelengths ...'

o=len(y3)

power=abs(y3[0:(o/2.)])**2.

sr=2 # cycles/mm sampling rate

p=o*1.

ftx(i)=ftx(i-1)+%SR/(2*n)

x=arange(sr/p,sr/(2.*p),sr/p)

freq=1./x

speed=2./1000. #

wave=speed/freq

plot(freq,power)

show()