]> git.vomp.tv Git - vompclient.git/blob - vscreensaver.cc
Improve connection failure handling
[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 "input.h"
26 #include "colour.h"
27 #include "video.h"
28 #include "surface.h"
29 #include "util.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   while(1)
101   {
102     if (++head == h) head = 0;
103
104     tail = head + 1;
105     if (tail == h) tail = 0;
106
107     // Undraw oldest point
108     if (x[tail] != -1) surface->drawPoint(x[tail], y[tail], DrawStyle::BLACK);// was rgba
109
110     dd = ((rand() / (double)RAND_MAX) * deviation) - halfdeviation;
111     direction += dd;
112
113     if (direction >= pi2) direction -= pi2;
114     if (direction < 0) direction += pi2;
115
116     fx += sin(direction);
117     fy += cos(direction);
118
119     if (fx < 0) fx += screenWidth;
120     if (fx >= screenWidth) fx -= screenWidth;
121     if (fy < 0) fy += screenHeight;
122     if (fy >= screenHeight) fy -= screenHeight;
123
124     x[head] = static_cast<int>(fx);
125     y[head] = static_cast<int>(fy);
126
127     surface->drawPoint(x[head], y[head], DrawStyle::SELECTHIGHLIGHT); // was rgba
128
129     if (threadReqStop) return;
130     MILLISLEEP(10);
131   }
132 }