]> git.vomp.tv Git - vompclient.git/blob - vscreensaver.cc
10 CWFs
[vompclient.git] / vscreensaver.cc
1 /*
2     Copyright 2004-2020 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <stdlib.h>
21 #include <time.h>
22 #include <math.h>
23
24 #include "defines.h"
25 #include "log.h"
26 #include "input.h"
27 #include "colour.h"
28 #include "video.h"
29 #include "surface.h"
30
31 #include "vscreensaver.h"
32
33 VScreensaver::VScreensaver()
34 {
35   setPosition(0, 0);
36   Video* video = Video::getInstance();
37   screenWidth = video->getScreenWidth();
38   screenHeight = video->getScreenHeight();
39   area.w = screenWidth;
40   area.h = screenHeight;
41   surface = Surface::getScreen();
42 }
43
44 VScreensaver::~VScreensaver()
45 {
46   stopThread();
47   surface = NULL; // it's the screen. stop view base from killing it.
48 }
49
50 void VScreensaver::draw()
51 {
52   fillColour(DrawStyle::BLACK);
53
54   ssThreadStartProtect.lock();
55   ssThread = std::thread([this]
56   {
57     ssThreadStartProtect.lock();
58     ssThreadStartProtect.unlock();
59     threadMethod();
60   });
61   ssThreadStartProtect.unlock();
62 }
63
64 int VScreensaver::handleCommand(int /*command*/)
65 {
66   stopThread();
67   return 4;
68 }
69
70 void VScreensaver::stopThread()
71 {
72   threadReqStop = true;
73   if (ssThread.joinable()) ssThread.join();
74 }
75
76 void VScreensaver::threadMethod()
77 {
78   srand(time(NULL));
79
80   // Config
81   const int h = 50;  // length of line
82   double deviation = 0.2;  // how quickly can it change direction
83
84
85   int x[h];
86   int y[h];
87   int i;
88   int head = -1;
89   int tail;
90   double direction = 0;
91   double fx, fy, dd;
92   const double pi2 = 6.28318;
93   double halfdeviation = deviation / 2;
94
95   for(i = 0; i < h; i++) x[i] = -1;
96
97   fx = x[0] = 50.0;
98   fy = y[0] = 50.0;
99
100   DrawStyle &black=DrawStyle::BLACK;
101   DrawStyle &light=DrawStyle::SELECTHIGHLIGHT;
102
103   while(1)
104   {
105     if (++head == h) head = 0;
106
107     tail = head + 1;
108     if (tail == h) tail = 0;
109
110     // Undraw oldest point
111     if (x[tail] != -1) surface->drawPoint(x[tail], y[tail], black);// was rgba
112
113     dd = ((rand() / (double)RAND_MAX) * deviation) - halfdeviation;
114     direction += dd;
115
116     if (direction >= pi2) direction -= pi2;
117     if (direction < 0) direction += pi2;
118
119     fx += sin(direction);
120     fy += cos(direction);
121
122     if (fx < 0) fx += screenWidth;
123     if (fx >= screenWidth) fx -= screenWidth;
124     if (fy < 0) fy += screenHeight;
125     if (fy >= screenHeight) fy -= screenHeight;
126
127     x[head] = static_cast<int>(fx);
128     y[head] = static_cast<int>(fy);
129
130     surface->drawPoint(x[head], y[head], light); // was rgba
131
132     if (threadReqStop) return;
133     MILLISLEEP(10);
134   }
135 }