Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Created November 8, 2016 11:06
Show Gist options
  • Save krakjoe/a8f770f0487afe28216dc2abbb28a76b to your computer and use it in GitHub Desktop.
Save krakjoe/a8f770f0487afe28216dc2abbb28a76b to your computer and use it in GitHub Desktop.
Starfield in D, uses krakjoe/dui (or any other 1:1 module)
import std.random;
import andlabs.libui;
struct Star {
double X;
double Y;
double V;
bool F;
double G;
}
Star stars[];
extern (C) {
int closing(uiWindow *win, void *data) {
uiControlDestroy(cast(uiControl*) win);
uiQuit();
return 0;
}
void draw(uiAreaHandler *handlers, uiArea *area, uiAreaDrawParams *params) {
uiDrawBrush blackBrush = {uiDrawBrushTypeSolid, 0, 0, 0, 1};
uiDrawBrush whiteBrush = {uiDrawBrushTypeSolid, 1, 1, 1, 1};
double hWidth = params.AreaWidth / 2;
double hHeight = params.AreaHeight / 2;
uiDrawPath *path = uiDrawNewPath(uiDrawFillModeWinding);
uiDrawPathAddRectangle(path, 0, 0, params.AreaWidth, params.AreaHeight);
uiDrawPathEnd(path);
uiDrawFill(params.Context, path, &blackBrush);
uiDrawFreePath(path);
for (int start = 0; start < 1024; start++) {
Star *star = &stars[start];
star.V -= 0.2;
if (star.V <= 0) {
star.X = uniform(-25, 26);
star.Y = uniform(-25, 26);
star.V = 128;
}
double px = star.X * (128 / star.V) + hWidth;
double py = star.Y * (128 / star.V) + hHeight;
if (px >= 0 && px <= params.AreaWidth && py >= 0 && py <= params.AreaHeight) {
double channel = (1 - star.V / 32) * 5;
uiDrawPath *next = uiDrawNewPath(uiDrawFillModeWinding);
uiDrawPathNewFigureWithArc(next, px, py, channel/2, 0, uiPi*2, 0);
uiDrawPathEnd(next);
uiDrawBrush brush = {
uiDrawBrushTypeSolid,
channel,
channel,
channel,
star.F && star.G++ % 3 == 0 ? uniform(0, 11)/10 : 1
};
uiDrawFill(params.Context, next, &brush);
uiDrawFreePath(next);
}
}
}
void mouse(uiAreaHandler *handlers, uiArea *area, uiAreaMouseEvent *ev) {}
void crossed(uiAreaHandler *handlers, int left) {}
void drag(uiAreaHandler *handlers, uiArea *area) {}
int key(uiAreaHandler *handlers, uiArea *area, uiAreaKeyEvent *ev) {
return 0;
}
uiAreaHandler handlers = {
&draw,
&mouse,
&crossed,
&drag,
&key
};
}
void main()
{
uiInitOptions o;
uiWindow *win;
uiBox *box;
uiArea *area;
const char *err = uiInit(&o);
if (err != null) {
uiFreeInitError(err);
return;
}
win = uiNewWindow("Starfield", 640, 480, 0);
uiWindowOnClosing(win, &closing, win);
box = uiNewVerticalBox();
uiWindowSetChild(win, cast(uiControl*) box);
area = uiNewArea(&handlers);
uiBoxAppend(box, cast(uiControl*) area, 1);
stars = new Star[1024];
for (int i = 0; i < 1024; i++) {
stars[i].X = uniform(-25, 26);
stars[i].Y = uniform(-25, 26);
stars[i].V = uniform(1, 129);
stars[i].F = cast(bool) uniform(0, 2);
stars[i].G = 0;
}
uiControlShow(cast(uiControl*) win);
uiMainSteps();
while (uiControlVisible(cast(uiControl*) win)) {
uiAreaQueueRedrawAll(area);
uiMainStep(1);
}
uiUninit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment