News:

Welcome to RetroCoders Community

Main Menu

Bubble Universe in D

Started by Tomaaz, Nov 30, 2022, 11:33 PM

Previous topic - Next topic

Tomaaz

Based on a demo posted on RCBasic forum. It's more colorful and, due to playing with alpha channel, a little bit 3D-like. Originally I translated it to SmallBASIC, but it was not fast enough. The D version is much smoother.

import raylib, std.math, std.stdio;

Color rgba(int re, int gr, int bl, int al) {
	return Color(cast(ubyte)re, cast(ubyte)gr, cast(ubyte)bl, cast(ubyte)al);
}

void main() {
	const n = 255;
	const r = 6.283185307179586 / 235;
	const mw = 512;
	const mh = 512;
	const hw = mw / 2;
	const hh = mh / 2;
	double x, u, v, t;
	x = 0.0;
	u = 0.0;
	v = 0.0;
	t = 0.0;
	ubyte k = 255;
	InitWindow(mw, mh, "Bubble Universe");
	SetTargetFPS(60);
	while (!WindowShouldClose()){
		BeginDrawing();
		ClearBackground(Color(0, 0, 0, 255));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				u = sin(i + v) + sin(r * i + x);
				v = cos(i + v) + cos (r * i + x);
				x = u + t;
				DrawPixel(cast(int)(hw + u * hw * 0.4), cast(int)(hh + v * hh * 0.4), rgba(i, j, 255 - i, k));
			}
			k = cast(ubyte)GetRandomValue(64, 255);
		}
		t = t + 0.01;
		EndDrawing();			
	}
	CloseWindow();
}