void VScreensaver::threadMethod()
{
+ srand(time(NULL));
+
threadSetKillable();
// Config
- int h = 50; // length of line
+ const int h = 50; // length of line
float deviation = 0.2; // how quickly can it change direction
int x[h];
int y[h];
int i;
+ int head = -1;
+ int tail;
float direction = 0;
- float fx;
- float fy;
- float dd;
- const float pi = 3.14159;
+ float fx, fy, dd;
const float pi2 = 6.28318;
float halfdeviation = deviation / 2;
- for(i = 1; i < h; i++)
- {
- x[i] = -1;
- y[i] = -1;
- }
+ for(i = 0; i < h; i++) x[i] = -1;
fx = x[0] = 50;
fy = y[0] = 50;
while(1)
{
- // Undraw oldest pixel
- if ((x[h-1] != -1) && (y[h-1] != -1))
- {
- surface->drawPixel(x[h-1], y[h-1], Colour::BLACK.rgba());
- }
+ if (++head == h) head = 0;
+
+ tail = head + 1;
+ if (tail == h) tail = 0;
- for(i = h - 1; i > 0; i--)
- {
- x[i] = x[i-1];
- y[i] = y[i-1];
- }
+ // Undraw oldest pixel
+ if (x[tail] != -1) surface->drawPixel(x[tail], y[tail], Colour::BLACK.rgba());
dd = ((rand() / (double)RAND_MAX) * deviation) - halfdeviation;
direction += dd;
if (fy < 0) fy += screenHeight;
if (fy >= screenHeight) fy -= screenHeight;
- x[0] = (int)fx;
- y[0] = (int)fy;
+ x[head] = (int)fx;
+ y[head] = (int)fy;
- surface->drawPixel(x[0], y[0], Colour::SELECTHIGHLIGHT.rgba());
+ surface->drawPixel(x[head], y[head], Colour::SELECTHIGHLIGHT.rgba());
MILLISLEEP(10);
}
}