Skip to content

Instantly share code, notes, and snippets.

@alexmaryin
Created May 5, 2022 12:24
Show Gist options
  • Save alexmaryin/441f43a6aa6b97e30d642cf742162669 to your computer and use it in GitHub Desktop.
Save alexmaryin/441f43a6aa6b97e30d642cf742162669 to your computer and use it in GitHub Desktop.
starfield_cpp_1
struct Star
{
float x, y, z, radius, brightness, viewX, viewY;
Star() { newStar(); }
void newStar()
{
x = viewX = std::experimental::randint(0, WIDTH) - H_WIDTH;
y = viewY = std::experimental::randint(0, HEIGHT) - H_HEIGHT;
z = 256;
brightness = 0.0;
radius = 1.0;
}
bool isOffScreen()
{
return z <= 0 || viewX <= -H_WIDTH || viewY <= -H_HEIGHT || viewX >= H_WIDTH || viewY >= H_HEIGHT;
}
void processStar()
{
viewX = std::round(x * 256 / z);
viewY = std::round(y * 256 / z);
z -= speed;
radius += radius_delta;
if (isOffScreen()) { newStar(); }
if (brightness < 256) { brightness += 0.15; }
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment