~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to illusion/src/applet-break.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
ImportĀ upstreamĀ versionĀ 2.0.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
/******************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock program, 
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Written by Fabrice Rey (for any bug report, please mail me to fabounet@users.berlios.de)
 
26
 
 
27
******************************************************************************/
 
28
 
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
#include <math.h>
 
32
 
 
33
#include "applet-struct.h"
 
34
#include "applet-break.h"
 
35
 
 
36
#define xctrl(j) pCtrlPoints[2*(j)]
 
37
#define yctrl(j) pCtrlPoints[2*(j)+1]
 
38
#define xpart(j) pPart->pCoords[2*(j)]
 
39
#define ypart(j) pPart->pCoords[2*(j)+1]
 
40
 
 
41
gboolean cd_illusion_init_break (Icon *pIcon, CairoDock *pDock, CDIllusionData *pData)
 
42
{
 
43
        int iNbCtrlPts = 4 + (2 * myConfig.iBreakNbBorderPoints) + (2 * myConfig.iBreakNbBorderPoints + 1);
 
44
        double *pCtrlPoints = g_new0 (double, 2 * iNbCtrlPts);
 
45
        //g_print ("iNbCtrlPts : %d\n", iNbCtrlPts);
 
46
        xctrl(0) = 0.;
 
47
        yctrl(0) = 1.;
 
48
        xctrl(1) = 1.;
 
49
        yctrl(1) = 1.;
 
50
        double f, f0 = 1. / (myConfig.iBreakNbBorderPoints + 1);  // f0 = fraction moyenne.
 
51
        int i,j=2;
 
52
        for (i = 0; i < 2*myConfig.iBreakNbBorderPoints+1; i ++, j++)
 
53
        {
 
54
                // un nouveau point sur l'un des cotes.
 
55
                if (i == 2*myConfig.iBreakNbBorderPoints)  // dernier coup.
 
56
                        f = 1.;
 
57
                else
 
58
                        f = f0 * (.5 + g_random_double ());  // entre .5f0 et 1.5f0, sachant que 1.5f0 <= 3/4 < 1
 
59
                xctrl(j) = ((j>>1) & 1);
 
60
                yctrl(j) = (1 - f) * (j > 3 ? yctrl(j-4) : yctrl(0));
 
61
                //g_print ("yctrl(j) : %.3f (%.2f)\n", yctrl(j), f);
 
62
                
 
63
                // une brisure au milieu du segment forme.
 
64
                j ++;
 
65
                f = g_random_double ();
 
66
                xctrl(j) = f;
 
67
                yctrl(j) = f * yctrl(j-1) + (1 - f) * yctrl(j-2);
 
68
        }
 
69
        xctrl(j) = ((j>>2) & 1);  // en fait c'est toujours egal a 0.
 
70
        yctrl(j) = 0.;
 
71
        
 
72
        pData->iNbBreakParts = 2 + (2 * myConfig.iBreakNbBorderPoints + 1);
 
73
        pData->pBreakPart = g_new0 (CDIllusionBreak, pData->iNbBreakParts);
 
74
        CDIllusionBreak *pPart;
 
75
        for (i = 0; i < pData->iNbBreakParts; i ++)
 
76
        {
 
77
                pPart = &pData->pBreakPart[i];
 
78
                if (i == 0)
 
79
                {
 
80
                        pPart->iNbPts = 3;
 
81
                        xpart(0) = xctrl(0);
 
82
                        ypart(0) = yctrl(0);
 
83
                        xpart(1) = xctrl(1);
 
84
                        ypart(1) = yctrl(1);
 
85
                        xpart(2) = xctrl(2);
 
86
                        ypart(2) = yctrl(2);
 
87
                }
 
88
                else if (i == 1)
 
89
                {
 
90
                        pPart->iNbPts = 3;
 
91
                        xpart(0) = xctrl(0);
 
92
                        ypart(0) = yctrl(0);
 
93
                        xpart(1) = xctrl(3);
 
94
                        ypart(1) = yctrl(3);
 
95
                        xpart(2) = xctrl(4);
 
96
                        ypart(2) = yctrl(4);
 
97
                }
 
98
                else if (i == pData->iNbBreakParts - 1)
 
99
                {
 
100
                        pPart->iNbPts = 3;
 
101
                        xpart(0) = xctrl(iNbCtrlPts-3);
 
102
                        ypart(0) = yctrl(iNbCtrlPts-3);
 
103
                        xpart(1) = xctrl(iNbCtrlPts-2);
 
104
                        ypart(1) = yctrl(iNbCtrlPts-2);
 
105
                        xpart(2) = xctrl(iNbCtrlPts-1);
 
106
                        ypart(2) = yctrl(iNbCtrlPts-1);
 
107
                }
 
108
                else
 
109
                {
 
110
                        pPart->iNbPts = 4;
 
111
                        xpart(0) = xctrl(2*(i-1));
 
112
                        ypart(0) = yctrl(2*(i-1));
 
113
                        xpart(1) = xctrl(2*(i-1)+1);
 
114
                        ypart(1) = yctrl(2*(i-1)+1);
 
115
                        xpart(2) = xctrl(2*(i-1)+3);
 
116
                        ypart(2) = yctrl(2*(i-1)+3);
 
117
                        xpart(3) = xctrl(2*(i-1)+4);
 
118
                        ypart(3) = yctrl(2*(i-1)+4);
 
119
                }
 
120
                
 
121
                pPart->yinf = MIN (MIN (ypart(0), ypart(1)), ypart(2));
 
122
                if (pPart->iNbPts == 4)
 
123
                        pPart->yinf = MIN (pPart->yinf, ypart(3));
 
124
                pPart->fCrackAngle = 5 + g_random_double () * 15.;  // ca separe un peu les morceaux, pour donner l'effet de brisure des le debut.
 
125
                pPart->fRotationAngle = (pData->sens == 1 ? pPart->fCrackAngle : 91);
 
126
        }
 
127
        
 
128
        return TRUE;
 
129
}
 
130
 
 
131
 
 
132
void cd_illusion_update_break (Icon *pIcon, CairoDock *pDock, CDIllusionData *pData)
 
133
{
 
134
        int iWidth, iHeight;
 
135
        cairo_dock_get_icon_extent (pIcon, pDock, &iWidth, &iHeight);
 
136
        double fSizeX, fSizeY;
 
137
        cairo_dock_get_current_icon_size (pIcon, pDock, &fSizeX, &fSizeY);
 
138
        
 
139
        double t_ = (pData->fTime / myConfig.iBreakDuration);  // t/T
 
140
        pData->dh = t_ * t_;  // dh = 1/2 * g * t^2, avec g = 2/T^2 (hauteur comptee unitairement).
 
141
        
 
142
        double yinf;
 
143
        CDIllusionBreak *pPart;
 
144
        int i, j;
 
145
        for (i = 0; i < pData->iNbBreakParts; i ++)
 
146
        {
 
147
                pPart = &pData->pBreakPart[i];
 
148
                if (pPart->yinf - pData->dh < 0)  // on a touche le sol.
 
149
                {
 
150
                        pPart->fRotationAngle += pData->sens * pData->fDeltaT / (.25 * myConfig.iBreakDuration) * 90.;  // formule faite a la va-vite, avec une acceleration ca serait mieux ...
 
151
                        if (pPart->fRotationAngle < pPart->fCrackAngle)
 
152
                                pPart->fRotationAngle = pPart->fCrackAngle;
 
153
                }
 
154
        }
 
155
        
 
156
        cairo_dock_redraw_icon (pIcon, CAIRO_CONTAINER (pDock));
 
157
}
 
158
 
 
159
void cd_illusion_draw_break_icon (Icon *pIcon, CairoDock *pDock, CDIllusionData *pData)
 
160
{
 
161
        _cairo_dock_enable_texture ();
 
162
        _cairo_dock_set_blend_alpha ();
 
163
        _cairo_dock_set_alpha (1.);
 
164
        glBindTexture (GL_TEXTURE_2D, pIcon->iIconTexture);
 
165
        
 
166
        double fSizeX, fSizeY;
 
167
        cairo_dock_get_current_icon_size (pIcon, pDock, &fSizeX, &fSizeY);
 
168
        
 
169
        glPushMatrix ();
 
170
        glTranslatef (-fSizeX/2, -fSizeY/2, 0.);
 
171
        glMatrixMode(GL_TEXTURE); // On selectionne la matrice des textures
 
172
        glPushMatrix ();
 
173
        glLoadIdentity(); // On la reset
 
174
        glScalef (1., -1., 1.);
 
175
        glMatrixMode(GL_MODELVIEW);
 
176
        
 
177
        CDIllusionBreak *pPart;
 
178
        double xt, yt;  // coordonnees d'un point de texture
 
179
        double x, y;  // coordonnees du vertex associe.
 
180
        double dh = pData->dh;
 
181
        int i, j;
 
182
        for (i = 0; i < pData->iNbBreakParts; i ++)
 
183
        {
 
184
                pPart = &pData->pBreakPart[i];
 
185
                if (pPart->fRotationAngle > 90)  // il est a plat par terre, on ne le voit plus.
 
186
                        continue;
 
187
                
 
188
                if (pPart->fRotationAngle != 0)
 
189
                {
 
190
                        glPushMatrix ();
 
191
                        glRotatef (pPart->fRotationAngle, 1., 0., 0.);
 
192
                }
 
193
                
 
194
                if (pPart->iNbPts == 3)
 
195
                        glBegin(GL_TRIANGLES);
 
196
                else
 
197
                        glBegin(GL_QUADS);
 
198
                
 
199
                for (j = 0; j < pPart->iNbPts; j ++)
 
200
                {
 
201
                        xt = xpart(j);
 
202
                        yt = ypart(j);
 
203
                        
 
204
                        x = xt * fSizeX;
 
205
                        if (dh > pPart->yinf)
 
206
                                y = (yt - pPart->yinf) * fSizeY;
 
207
                        else
 
208
                                y = (yt - dh) * fSizeY;
 
209
                        
 
210
                        glTexCoord2f (xt, yt);
 
211
                        glVertex3f (x,  y,  0.);
 
212
                }
 
213
                
 
214
                glEnd ();
 
215
                
 
216
                if (pPart->fRotationAngle != 0)
 
217
                {
 
218
                        glPopMatrix ();
 
219
                }
 
220
        }
 
221
        
 
222
        glPopMatrix ();
 
223
        glMatrixMode(GL_TEXTURE); // On selectionne la matrice des textures
 
224
        glPopMatrix ();
 
225
        glMatrixMode(GL_MODELVIEW);
 
226
        _cairo_dock_disable_texture ();
 
227
}