Add LASE demo source code

This commit is contained in:
Hector Martin 2012-04-12 20:05:31 +01:00
parent 088e8f7992
commit d442131957
9 changed files with 1274 additions and 0 deletions

View file

@ -42,3 +42,7 @@ add_executable(harp harp.c)
target_link_libraries(harp openlase)
add_subdirectory(27c3_slides)
# Note: this target needs modplug but I'm too lazy to add a module finder for
# it.
#add_subdirectory(lase_demo)

View file

@ -0,0 +1,6 @@
include_directories (${CMAKE_SOURCE_DIR}/include)
link_directories (${CMAKE_BINARY_DIR}/libol)
add_executable(demo demo.c trace.c)
target_link_libraries(demo openlase modplug)

BIN
examples/lase_demo/GLOS-pope.xm Executable file

Binary file not shown.

17
examples/lase_demo/README Normal file
View file

@ -0,0 +1,17 @@
Yes, this is the source code for LASE. Keep in mind that a good chunk of this
was coded in a rush at the party, and it's also basically the first nontrivial
code written for openlase. It has been modified slightly to make it work with
the current API (which was slightly incompatible) but otherwise remains
untouched, although it will look slightly better due to libol improvements
since the demo was made.
Danger: Here be dragons. :-)
To compile: I've been too lazy to add a CMake module to conditionally search
for libmodplug, so this is disabled by default. Uncomment the subdir at the end
of ../CMakeLists.txt and make sure you have libmodplug and its headers (-dev
package) installed.
To run: make sure that GLOS-pope.xm is in your $PWD before running the binary
(e.g. run it from this directory).

1118
examples/lase_demo/demo.c Normal file

File diff suppressed because it is too large Load diff

Binary file not shown.

Binary file not shown.

123
examples/lase_demo/trace.c Normal file
View file

@ -0,0 +1,123 @@
#include "libol.h"
#include <math.h>
#include <string.h>
#include "trace.h"
#define ABS(a) ((a)<0?(-(a)):(a))
#define OVERDRAW 8
int trace(int *field, uint8_t *tmp, int thresh, int w, int h, int decimate)
{
int x, y, cx, cy, px, py, i;
int iters = 0;
int objects = 0;
int sx[OVERDRAW], sy[OVERDRAW];
memset(tmp, 0, w*h);
for (y=1; y<h-1; y++) {
for (x=1; x<w-1;x++) {
int idx = y*w+x;
if (field[idx] > thresh && (!(field[idx-w] > thresh)
|| !(field[idx+w] > thresh)
|| !(field[idx-1] > thresh)
|| !(field[idx+1] > thresh))) {
tmp[idx] = 1;
}
}
}
int total = h*w;
int dir = 0;
int minx = 0, miny = 0;
int maxx = w-1, maxy = h-1;
int div = 0;
px = 0;
py = 0;
while (total--)
{
if (tmp[py*w+px]) {
x = cx = px;
y = cy = py;
iters = 0;
olBegin(OL_POINTS);
while (1)
{
int idx = y*w+x;
if(div==0) {
if (iters < OVERDRAW) {
sx[iters] = x;
sy[iters] = y;
}
olVertex(x, y, C_WHITE);
iters++;
}
div = (div+1)%decimate;
tmp[idx] = 0;
if (tmp[idx-1]) {
x--;
} else if (tmp[idx+1]) {
x++;
} else if (tmp[idx-w]) {
y--;
} else if (tmp[idx+w]) {
y++;
} else if (tmp[idx-w-1]) {
y--; x--;
} else if (tmp[idx-w+1]) {
y--; x++;
} else if (tmp[idx+w-1]) {
y++; x--;
} else if (tmp[idx+w+1]) {
y++; x++;
} else {
break;
}
}
if (iters) {
objects++;
if (ABS(cx-x) <= 1 && ABS(cy-y) <= 1) {
if (iters > OVERDRAW)
iters = OVERDRAW;
for (i=0; i<iters; i++)
olVertex(sx[i], sy[i], C_GREY((int)(255.0 * (OVERDRAW - 1 - i) / (float)OVERDRAW)));
}
}
olEnd();
}
switch(dir) {
case 0:
px++;
if (px > maxx) {
px--; py++; maxx--; dir++;
}
break;
case 1:
py++;
if (py > maxy) {
py--; px--; maxy--; dir++;
}
break;
case 2:
px--;
if (px < minx) {
px++; py--; minx++; dir++;
}
break;
case 3:
py--;
if (py < miny) {
py++; px++; miny++; dir=0;
}
break;
}
}
return objects;
}

View file

@ -0,0 +1,6 @@
#ifndef TRACE_H
#define TRACE_H
int trace(int *field, uint8_t *tmp, int thresh, int width, int height, int decimate);
#endif