Skip to content

Instantly share code, notes, and snippets.

@r1r11
Created April 19, 2019 17:06
Show Gist options
  • Save r1r11/11980aa20b948e428dbc3d3c55d13263 to your computer and use it in GitHub Desktop.
Save r1r11/11980aa20b948e428dbc3d3c55d13263 to your computer and use it in GitHub Desktop.
/**
* GraphicsLibrary
*/
const render = fn(array scene, array size) {
int center = Math.floor(size[0] / 2.0);
array colors = split(' ,`,.,.,-,/,+,+,=,L,H,S,#,@', ',');
for (y, size[1]) {
string s = '';
for (x, size[0]-1) {
int idx = 0;
for (o, scene) {
let obj = scene[o];
array offset = [x+obj[2][0], y+obj[2][1]];
if (obj[0](offset[0], offset[1], center)) {
idx = obj[1](offset[0], offset[1], center);
}
}
s = s + colors[idx];
}
print(s);
}
}
/* shape functions return true or false. */
const Shape = {
triangle: fn(int x, int y, int c, int width, int height) {
/* x index, y index, x-center index */
return (Math.abs(x - c)*height < (y*width));
},
box: fn(int x, int y, int x2, int y2, int width, int height) {
/* screen x, screen y, box left x, box top y, box width, box height */
return (x > x2 && x < (x2+width) && y > y2 && y < y2+height);
},
circle: fn(int x, int y, int cx, int cy, int cr, float xScale) {
int dx = x-cx,
dy = y-cy;
if (typeof xScale == "null") {
xScale = 1.0/3.8;
}
return (dx*dx*xScale)+(dy*dy) < (cr*cr);
}
column: fn(x, y, c) {
int x2 = (Math.abs(x - c));
int y2 = (Math.abs(y - c));
return (x2 * y2) < 1000;
}
};
/* all materials are int functions, returning the color index */
const Material = {
body: fn(x, y, c) {
int x2 = (Math.abs(x - c));
int y2 = (Math.abs(y - c));
return 10 - Math.floor(x2 * x2 * y2 * y2 * 0.1) % 10;
},
booster: fn(x, y, c) {
int x2 = (Math.abs(x - c)),
y2 = (Math.abs(y - c));
int x3 = Math.floor(Math.sin(x2/12.0)*12.0),
y3 = Math.floor(Math.cos(y2/12.0)*12.0);
return (144 + x3 * y3) % 13;
},
stars: fn(x, y, c) {
return Math.floor(22+Math.sin(x * y * 0.1)*22.0) % 7;
}
};
const Shaders = {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment