Raylib 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.7 KiB

2 years ago
//Standard lib
#include <stdio.h>
//Third party
#include "raylib.h"
//Our own files
#include "constants.h"
#include "programstate.h"
#include "effect_starfield.h"
extern ProgramState programState;
static int screenWidth;
static int screenHeight;
static const int numberOfStars=540;
static const int starLayers = 3;
typedef struct {
int x;
int y;
int z;
Color color;
float rand;
} Star;
static Star stars[540]; //1080 * 0.5 Not quite one star per line
void reposition() {
//Recreate all stars and begin from blank.
//We want a certain star density, no matter the screen resolution
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
Color color;
int rand;
int layer;
int partitionSize=(starLayers+1)*2 / 8; //8 partions
for (int i=0; i<numberOfStars; i++) {
rand = GetRandomValue(1, (starLayers+1)*2);
if (rand<= 5*partitionSize) layer = 1;
else if (rand<= 7*partitionSize) layer = 2;
else if (rand<= 8*partitionSize) layer = 3;
switch (layer) {
//0 does not exist
case 1:
color = BROWN;
break;
case 2:
color = BEIGE;
break;
case 3:
color = PURPLE;
break;
default:
color = DARKBLUE;
}
float randomFactor = ((float)GetRandomValue(0, 20)-10.0f) / 100.0f;
stars[i] = (Star){ GetRandomValue(0, screenWidth), GetRandomValue(0, screenHeight), layer, color, randomFactor };
}
}
void effect_starfield(bool redrawNeeded, float opacity, int speedModifier, int size) {
float ft = GetFrameTime();
int bpmFactor = programState.bpm/40 * programState.bpm/20; //exponential curve. The higher bpm the faster we get. Below 90 it should feel really slow
Star * star;
if (redrawNeeded) {
reposition();
}
for (int i=0; i<numberOfStars; i++) {
star = &stars[i];
if (programState.transportRolling) {
//Move Forward
star->x -= (star->z * ft * bpmFactor * 3 * speedModifier); //... the mysterious number 3.
//wrap around
if (star->x <= 0) {
star->x += screenWidth;
star->y = GetRandomValue(0, screenHeight);
}
else if (star->x > screenWidth) {
star->x -= screenWidth;
star->y = GetRandomValue(0, screenHeight);
}
}
//Wobble wobble
int rand = GetRandomValue(1, 200);
if (rand == 1) {star->y += 1;}
else if (rand == 2) {star->y -= 1;}
DrawRectangle(star->x, star->y, size, size, Fade(star->color, opacity));
}
}