]> git.vomp.tv Git - vompclient.git/blob - vscreensaver.cc
Switch from deprecated libavresample to libswresample. Some AudioOMX 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 #include "util.h"
31
32 #include "vscreensaver.h"
33
34 VScreensaver::VScreensaver()
35 {
36   setPosition(0, 0);
37   Video* video = Video::getInstance();
38   screenWidth = video->getScreenWidth();
39   screenHeight = video->getScreenHeight();
40   area.w = screenWidth;
41   area.h = screenHeight;
42   surface = Surface::getScreen();
43 }
44
45 VScreensaver::~VScreensaver()
46 {
47   stopThread();
48   surface = NULL; // it's the screen. stop view base from killing it.
49 }
50
51 void VScreensaver::draw()
52 {
53   fillColour(DrawStyle::BLACK);
54
55   ssThreadStartProtect.lock();
56   ssThread = std::thread([this]
57   {
58     ssThreadStartProtect.lock();
59     ssThreadStartProtect.unlock();
60     threadMethod();
61   });
62   ssThreadStartProtect.unlock();
63 }
64
65 int VScreensaver::handleCommand(int /*command*/)
66 {
67   stopThread();
68   return 4;
69 }
70
71 void VScreensaver::stopThread()
72 {
73   threadReqStop = true;
74   if (ssThread.joinable()) ssThread.join();
75 }
76
77 void VScreensaver::threadMethod()
78 {
79   srand(time(NULL));
80
81   // Config
82   const int h = 50;  // length of line
83   double deviation = 0.2;  // how quickly can it change direction
84
85
86   int x[h];
87   int y[h];
88   int i;
89   int head = -1;
90   int tail;
91   double direction = 0;
92   double fx, fy, dd;
93   const double pi2 = 6.28318;
94   double halfdeviation = deviation / 2;
95
96   for(i = 0; i < h; i++) x[i] = -1;
97
98   fx = x[0] = 50.0;
99   fy = y[0] = 50.0;
100
101   DrawStyle &black=DrawStyle::BLACK;
102   DrawStyle &light=DrawStyle::SELECTHIGHLIGHT;
103
104   while(1)
105   {
106     if (++head == h) head = 0;
107
108     tail = head + 1;
109     if (tail == h) tail = 0;
110
111     // Undraw oldest point
112     if (x[tail] != -1) surface->drawPoint(x[tail], y[tail], black);// was rgba
113
114     dd = ((rand() / (double)RAND_MAX) * deviation) - halfdeviation;
115     direction += dd;
116
117     if (direction >= pi2) direction -= pi2;
118     if (direction < 0) direction += pi2;
119
120     fx += sin(direction);
121     fy += cos(direction);
122
123     if (fx < 0) fx += screenWidth;
124     if (fx >= screenWidth) fx -= screenWidth;
125     if (fy < 0) fy += screenHeight;
126     if (fy >= screenHeight) fy -= screenHeight;
127
128     x[head] = static_cast<int>(fx);
129     y[head] = static_cast<int>(fy);
130
131     surface->drawPoint(x[head], y[head], light); // was rgba
132
133     if (threadReqStop) return;
134     MILLISLEEP(10);
135   }
136 }