]> git.vomp.tv Git - vompclient.git/blob - vscreensaver.cc
Fix for possible crash when removing bar
[vompclient.git] / vscreensaver.cc
1 /*
2     Copyright 2004-2005 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, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "vscreensaver.h"
22
23 VScreensaver::VScreensaver()
24 {
25   setScreenPos(0, 0);
26   Video* video = Video::getInstance();
27   screenWidth = video->getScreenWidth();
28   screenHeight = video->getScreenHeight();
29   area.w = screenWidth;
30   area.h = screenHeight;
31   surface = Surface::getScreen();
32 }
33
34 VScreensaver::~VScreensaver()
35 {
36   surface = NULL; // it's the screen. stop view base from killing it.
37   threadCancel();
38 }
39
40 void VScreensaver::draw()
41 {
42   setBackgroundColour(Colour::BLACK);
43   View::draw();
44   threadStart();
45 }
46
47 int VScreensaver::handleCommand(int command)
48 {
49   threadCancel();
50
51   return 4;
52 }
53
54 void VScreensaver::threadMethod()
55 {
56   srand(time(NULL));
57
58   threadSetKillable();
59
60   // Config
61   const int h = 50;  // length of line
62   float deviation = 0.2;  // how quickly can it change direction
63
64
65
66   int x[h];
67   int y[h];
68   int i;
69   int head = -1;
70   int tail;
71   float direction = 0;
72   float fx, fy, dd;
73   const float pi2 = 6.28318;
74   float halfdeviation = deviation / 2;
75
76   for(i = 0; i < h; i++) x[i] = -1;
77
78   fx = x[0] = 50;
79   fy = y[0] = 50;
80
81   while(1)
82   {
83     if (++head == h) head = 0;
84
85     tail = head + 1;
86     if (tail == h) tail = 0;
87
88     // Undraw oldest pixel
89     if (x[tail] != -1) surface->drawPixel(x[tail], y[tail], Colour::BLACK.rgba());
90
91     dd = ((rand() / (double)RAND_MAX) * deviation) - halfdeviation;
92     direction += dd;
93
94     if (direction >= pi2) direction -= pi2;
95     if (direction < 0) direction += pi2;
96
97     fx += sin(direction);
98     fy += cos(direction);
99
100     if (fx < 0) fx += screenWidth;
101     if (fx >= screenWidth) fx -= screenWidth;
102     if (fy < 0) fy += screenHeight;
103     if (fy >= screenHeight) fy -= screenHeight;
104
105     x[head] = (int)fx;
106     y[head] = (int)fy;
107
108     surface->drawPixel(x[head], y[head], Colour::SELECTHIGHLIGHT.rgba());
109     MILLISLEEP(10);
110   }
111 }