]> git.vomp.tv Git - vompclient.git/blob - vscreensaver.cc
A _very_ simple screensaver
[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   threadSetKillable();
57
58   // Config
59   int h = 50;  // length of line
60   float deviation = 0.2;  // how quickly can it change direction
61
62
63
64   int x[h];
65   int y[h];
66   int i;
67   float direction = 0;
68   float fx;
69   float fy;
70   float dd;
71   const float pi = 3.14159;
72   const float pi2 = 6.28318;
73   float halfdeviation = deviation / 2;
74
75   for(i = 1; i < h; i++)
76   {
77     x[i] = -1;
78     y[i] = -1;
79   }
80
81   fx = x[0] = 50;
82   fy = y[0] = 50;
83
84   while(1)
85   {
86     // Undraw oldest pixel
87     if ((x[h-1] != -1) && (y[h-1] != -1))
88     {
89       surface->drawPixel(x[h-1], y[h-1], Colour::BLACK.rgba());
90     }
91
92     for(i = h - 1; i > 0; i--)
93     {
94       x[i] = x[i-1];
95       y[i] = y[i-1];
96     }
97
98     dd = ((rand() / (double)RAND_MAX) * deviation) - halfdeviation;
99     direction += dd;
100
101     if (direction >= pi2) direction -= pi2;
102     if (direction < 0) direction += pi2;
103
104     fx += sin(direction);
105     fy += cos(direction);
106
107     if (fx < 0) fx += screenWidth;
108     if (fx >= screenWidth) fx -= screenWidth;
109     if (fy < 0) fy += screenHeight;
110     if (fy >= screenHeight) fy -= screenHeight;
111
112     x[0] = (int)fx;
113     y[0] = (int)fy;
114
115     surface->drawPixel(x[0], y[0], Colour::SELECTHIGHLIGHT.rgba());
116     MILLISLEEP(10);
117   }
118 }