| 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 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
#include "applet-struct.h" |
| 25 |
#include "applet-break.h" |
| 26 |
|
| 27 |
#define xctrl(j) pCtrlPoints[2*(j)] |
| 28 |
#define yctrl(j) pCtrlPoints[2*(j)+1] |
| 29 |
#define xpart(j) pPart->pCoords[2*(j)] |
| 30 |
#define ypart(j) pPart->pCoords[2*(j)+1] |
| 31 |
|
| 32 |
gboolean cd_illusion_init_break (Icon *pIcon, CairoDock *pDock, CDIllusionData *pData) |
| 33 |
{ |
| 34 |
int iNbCtrlPts = 4 + (2 * myConfig.iBreakNbBorderPoints) + (2 * myConfig.iBreakNbBorderPoints + 1); |
| 35 |
double *pCtrlPoints = g_new0 (double, 2 * iNbCtrlPts); |
| 36 |
//g_print ("iNbCtrlPts : %d\n", iNbCtrlPts); |
| 37 |
xctrl(0) = 0.; |
| 38 |
yctrl(0) = 1.; |
| 39 |
xctrl(1) = 1.; |
| 40 |
yctrl(1) = 1.; |
| 41 |
double f, f0 = 1. / (myConfig.iBreakNbBorderPoints + 1); // f0 = fraction moyenne. |
| 42 |
int i,j=2; |
| 43 |
for (i = 0; i < 2*myConfig.iBreakNbBorderPoints+1; i ++, j++) |
| 44 |
{ |
| 45 |
// un nouveau point sur l'un des cotes. |
| 46 |
if (i == 2*myConfig.iBreakNbBorderPoints) // dernier coup. |
| 47 |
f = 1.; |
| 48 |
else |
| 49 |
f = f0 * (.5 + g_random_double ()); // entre .5f0 et 1.5f0, sachant que 1.5f0 <= 3/4 < 1 |
| 50 |
xctrl(j) = ((j>>1) & 1); |
| 51 |
yctrl(j) = (1 - f) * (j > 3 ? yctrl(j-4) : yctrl(0)); |
| 52 |
//g_print ("yctrl(j) : %.3f (%.2f)\n", yctrl(j), f); |
| 53 |
|
| 54 |
// une brisure au milieu du segment forme. |
| 55 |
j ++; |
| 56 |
f = g_random_double (); |
| 57 |
xctrl(j) = f; |
| 58 |
yctrl(j) = f * yctrl(j-1) + (1 - f) * yctrl(j-2); |
| 59 |
} |
| 60 |
xctrl(j) = ((j>>2) & 1); // en fait c'est toujours egal a 0. |
| 61 |
yctrl(j) = 0.; |
| 62 |
|
| 63 |
pData->iNbBreakParts = 2 + (2 * myConfig.iBreakNbBorderPoints + 1); |
| 64 |
pData->pBreakPart = g_new0 (CDIllusionBreak, pData->iNbBreakParts); |
| 65 |
CDIllusionBreak *pPart; |
| 66 |
for (i = 0; i < pData->iNbBreakParts; i ++) |
| 67 |
{ |
| 68 |
pPart = &pData->pBreakPart[i]; |
| 69 |
if (i == 0) |
| 70 |
{ |
| 71 |
pPart->iNbPts = 3; |
| 72 |
xpart(0) = xctrl(0); |
| 73 |
ypart(0) = yctrl(0); |
| 74 |
xpart(1) = xctrl(1); |
| 75 |
ypart(1) = yctrl(1); |
| 76 |
xpart(2) = xctrl(2); |
| 77 |
ypart(2) = yctrl(2); |
| 78 |
} |
| 79 |
else if (i == 1) |
| 80 |
{ |
| 81 |
pPart->iNbPts = 3; |
| 82 |
xpart(0) = xctrl(0); |
| 83 |
ypart(0) = yctrl(0); |
| 84 |
xpart(1) = xctrl(3); |
| 85 |
ypart(1) = yctrl(3); |
| 86 |
xpart(2) = xctrl(4); |
| 87 |
ypart(2) = yctrl(4); |
| 88 |
} |
| 89 |
else if (i == pData->iNbBreakParts - 1) |
| 90 |
{ |
| 91 |
pPart->iNbPts = 3; |
| 92 |
xpart(0) = xctrl(iNbCtrlPts-3); |
| 93 |
ypart(0) = yctrl(iNbCtrlPts-3); |
| 94 |
xpart(1) = xctrl(iNbCtrlPts-2); |
| 95 |
ypart(1) = yctrl(iNbCtrlPts-2); |
| 96 |
xpart(2) = xctrl(iNbCtrlPts-1); |
| 97 |
ypart(2) = yctrl(iNbCtrlPts-1); |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
pPart->iNbPts = 4; |
| 102 |
xpart(0) = xctrl(2*(i-1)); |
| 103 |
ypart(0) = yctrl(2*(i-1)); |
| 104 |
xpart(1) = xctrl(2*(i-1)+1); |
| 105 |
ypart(1) = yctrl(2*(i-1)+1); |
| 106 |
xpart(2) = xctrl(2*(i-1)+3); |
| 107 |
ypart(2) = yctrl(2*(i-1)+3); |
| 108 |
xpart(3) = xctrl(2*(i-1)+4); |
| 109 |
ypart(3) = yctrl(2*(i-1)+4); |
| 110 |
} |
| 111 |
|
| 112 |
pPart->yinf = MIN (MIN (ypart(0), ypart(1)), ypart(2)); |
| 113 |
if (pPart->iNbPts == 4) |
| 114 |
pPart->yinf = MIN (pPart->yinf, ypart(3)); |
| 115 |
pPart->fCrackAngle = 5 + g_random_double () * 15.; // ca separe un peu les morceaux, pour donner l'effet de brisure des le debut. |
| 116 |
pPart->fRotationAngle = (pData->sens == 1 ? pPart->fCrackAngle : 91); |
| 117 |
} |
| 118 |
|
| 119 |
return TRUE; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
void cd_illusion_update_break (Icon *pIcon, CairoDock *pDock, CDIllusionData *pData) |
| 124 |
{ |
| 125 |
int iWidth, iHeight; |
| 126 |
cairo_dock_get_icon_extent (pIcon, &iWidth, &iHeight); |
| 127 |
double fSizeX, fSizeY; |
| 128 |
cairo_dock_get_current_icon_size (pIcon, CAIRO_CONTAINER (pDock), &fSizeX, &fSizeY); |
| 129 |
|
| 130 |
double t_ = (pData->fTime / myConfig.iBreakDuration); // t/T |
| 131 |
pData->dh = t_ * t_; // dh = 1/2 * g * t^2, avec g = 2/T^2 (hauteur comptee unitairement). |
| 132 |
|
| 133 |
CDIllusionBreak *pPart; |
| 134 |
int i; |
| 135 |
for (i = 0; i < pData->iNbBreakParts; i ++) |
| 136 |
{ |
| 137 |
pPart = &pData->pBreakPart[i]; |
| 138 |
if (pPart->yinf - pData->dh < 0) // on a touche le sol. |
| 139 |
{ |
| 140 |
pPart->fRotationAngle += pData->sens * pData->fDeltaT / (.25 * myConfig.iBreakDuration) * 90.; // formule faite a la va-vite, avec une acceleration ca serait mieux ... |
| 141 |
if (pPart->fRotationAngle < pPart->fCrackAngle) |
| 142 |
pPart->fRotationAngle = pPart->fCrackAngle; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
cairo_dock_redraw_container (CAIRO_CONTAINER (pDock)); |
| 147 |
} |
| 148 |
|
| 149 |
void cd_illusion_draw_break_icon (Icon *pIcon, CairoDock *pDock, CDIllusionData *pData) |
| 150 |
{ |
| 151 |
_cairo_dock_enable_texture (); |
| 152 |
_cairo_dock_set_blend_alpha (); |
| 153 |
_cairo_dock_set_alpha (1.); |
| 154 |
glBindTexture (GL_TEXTURE_2D, pIcon->image.iTexture); |
| 155 |
|
| 156 |
double fSizeX, fSizeY; |
| 157 |
cairo_dock_get_current_icon_size (pIcon, CAIRO_CONTAINER (pDock), &fSizeX, &fSizeY); |
| 158 |
|
| 159 |
glPushMatrix (); |
| 160 |
glTranslatef (-fSizeX/2, -fSizeY/2, 0.); |
| 161 |
glMatrixMode(GL_TEXTURE); // On selectionne la matrice des textures |
| 162 |
glPushMatrix (); |
| 163 |
glLoadIdentity(); // On la reset |
| 164 |
glScalef (1., -1., 1.); |
| 165 |
glMatrixMode(GL_MODELVIEW); |
| 166 |
|
| 167 |
CDIllusionBreak *pPart; |
| 168 |
double xt, yt; // coordonnees d'un point de texture |
| 169 |
double x, y; // coordonnees du vertex associe. |
| 170 |
double dh = pData->dh; |
| 171 |
int i, j; |
| 172 |
for (i = 0; i < pData->iNbBreakParts; i ++) |
| 173 |
{ |
| 174 |
pPart = &pData->pBreakPart[i]; |
| 175 |
if (pPart->fRotationAngle > 90) // il est a plat par terre, on ne le voit plus. |
| 176 |
continue; |
| 177 |
|
| 178 |
if (pPart->fRotationAngle != 0) |
| 179 |
{ |
| 180 |
glPushMatrix (); |
| 181 |
glRotatef (pPart->fRotationAngle, 1., 0., 0.); |
| 182 |
} |
| 183 |
|
| 184 |
if (pPart->iNbPts == 3) |
| 185 |
glBegin(GL_TRIANGLES); |
| 186 |
else |
| 187 |
glBegin(GL_QUADS); |
| 188 |
|
| 189 |
for (j = 0; j < pPart->iNbPts; j ++) |
| 190 |
{ |
| 191 |
xt = xpart(j); |
| 192 |
yt = ypart(j); |
| 193 |
|
| 194 |
x = xt * fSizeX; |
| 195 |
if (dh > pPart->yinf) |
| 196 |
y = (yt - pPart->yinf) * fSizeY; |
| 197 |
else |
| 198 |
y = (yt - dh) * fSizeY; |
| 199 |
|
| 200 |
glTexCoord2f (xt, yt); |
| 201 |
glVertex3f (x, y, 0.); |
| 202 |
} |
| 203 |
|
| 204 |
glEnd (); |
| 205 |
|
| 206 |
if (pPart->fRotationAngle != 0) |
| 207 |
{ |
| 208 |
glPopMatrix (); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
glPopMatrix (); |
| 213 |
glMatrixMode(GL_TEXTURE); // On selectionne la matrice des textures |
| 214 |
glPopMatrix (); |
| 215 |
glMatrixMode(GL_MODELVIEW); |
| 216 |
_cairo_dock_disable_texture (); |
| 217 |
} |