~3v1n0/compiz/gtk-decorator-finalize-menu-0.9.10

« back to all changes in this revision

Viewing changes to plugins/firepaint/src/firepaint.cpp

Firepaint, code cleanup:

Declare variables outside of loops.
Declaration and assignment of local variables in one line.
Use pre- instead of postfix increment.
Removed redundant newlines.
Removed redundant casts. (from (float) optionGetFireColor)
Removed redundant brackets.
Added missing break (style issue only).
Added comment and TODO.
Fixed indentation.

Firepaint, speedup:

We just enable GL_BLEND if it is disabled and
we just disable GL_BLEND if it was disabled before.

Introduced the GLfloats xMinusW, xPlusW, yMinusH and
yPlusH to store calculated coordinates instead of
recalculating them multiple times.

Introduced
float fireLife = optionGetFireLife ();,
float fireWidth = optionGetFireSize ();,
float fireHeight = fireWidth * 1.5f; and
bool mystFire = optionGetFireMystical ();
and used those variables inside the loop.

Firepaint, .xml.in cleanup:

Firepaint now has 2 tabs, 'General' and 'Particle Settings'.
The name of this plugin is Firepaint, <short> should not contain
the description.
Better description of the plugin.
Uppercase option titles.
Punctuation for tooltips.
Improved tooltips.

Firepaint, fixes:

Initialize all class member variables in the ParticleSystem::
ParticleSystem () ctor (LP: #1101512, LP: #1101580). Fixes: https://bugs.launchpad.net/bugs/1101512, https://bugs.launchpad.net/bugs/1101580.

Approved by PS Jenkins bot, Sam Spilsbury.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
COMPIZ_PLUGIN_20090315 (firepaint, FirePluginVTable);
25
25
 
26
26
/* 3 vertices per triangle, 2 triangles per particle */
27
 
const unsigned short CACHESIZE_FACTOR = 3 * 2;
28
 
 
 
27
const unsigned short CACHESIZE_FACTOR  = 3 * 2;
29
28
/* 2 coordinates, x and y */
30
 
const unsigned short COORD_COMPONENTS = CACHESIZE_FACTOR * 2;
31
 
 
 
29
const unsigned short COORD_COMPONENTS  = CACHESIZE_FACTOR * 2;
32
30
/* each vertex is stored as 3 GLfloats */
33
31
const unsigned short VERTEX_COMPONENTS = CACHESIZE_FACTOR * 3;
34
 
 
35
32
/* 4 colors, RGBA */
36
 
const unsigned short COLOR_COMPONENTS = CACHESIZE_FACTOR * 4;
 
33
const unsigned short COLOR_COMPONENTS  = CACHESIZE_FACTOR * 4;
37
34
 
38
35
Particle::Particle () :
39
36
    life (0),
69
66
}
70
67
 
71
68
ParticleSystem::ParticleSystem () :
 
69
    slowdown (1.0f),
 
70
    tex (0),
 
71
    active (false),
72
72
    x (0),
73
 
    y (0)
 
73
    y (0),
 
74
    darken (0.0f),
 
75
    blendMode (0)
74
76
{
75
77
    initParticles (0);
76
78
}
81
83
}
82
84
 
83
85
void
84
 
ParticleSystem::initParticles (int            f_numParticles)
 
86
ParticleSystem::initParticles (int f_numParticles)
85
87
{
86
88
    particles.clear ();
87
89
 
88
 
    tex = 0;
89
 
    slowdown = 1;
90
 
    active = false;
91
 
    darken = 0;
92
 
 
93
90
    // Initialize cache
94
91
    vertices_cache.clear ();
95
92
    coords_cache.clear ();
96
93
    colors_cache.clear ();
97
94
    dcolors_cache.clear ();
98
95
 
99
 
    for (int i = 0; i < f_numParticles; i++)
 
96
    for (int i = 0; i < f_numParticles; ++i)
100
97
    {
101
98
        Particle p;
102
99
        p.life = 0.0f;
105
102
}
106
103
 
107
104
void
108
 
ParticleSystem::drawParticles(const GLMatrix         &transform)
 
105
ParticleSystem::drawParticles(const GLMatrix &transform)
109
106
{
110
107
    int i, j, k, l;
111
108
 
123
120
        if (dcolors_cache.size () < particles.size () * COLOR_COMPONENTS)
124
121
            dcolors_cache.resize (particles.size () * COLOR_COMPONENTS);
125
122
 
126
 
    glEnable (GL_BLEND);
 
123
    GLboolean glBlendEnabled = glIsEnabled (GL_BLEND);
 
124
 
 
125
    if (!glBlendEnabled)
 
126
        glEnable (GL_BLEND);
127
127
 
128
128
    if (tex)
129
129
    {
133
133
 
134
134
    i = j = k = l = 0;
135
135
 
 
136
    GLfloat w, h;
 
137
    GLfloat xMinusW, xPlusW, yMinusH, yPlusH;
 
138
    GLushort r, g, b, a, dark_a;
 
139
 
136
140
    /* for each particle, use two triangles to display it */
137
141
    foreach (Particle &part, particles) 
138
142
    {
139
143
        if (part.life > 0.0f)
140
144
        {
141
 
            float w = part.width / 2;
142
 
            float h = part.height / 2;
143
 
 
144
 
            GLushort r, g, b, a, dark_a;
 
145
            w = part.width  / 2.0f;
 
146
            h = part.height / 2.0f;
145
147
 
146
148
            r = part.r * 65535.0f;
147
149
            g = part.g * 65535.0f;
148
150
            b = part.b * 65535.0f;
149
 
            a = part.life * part.a * 65535.0f;
150
 
            dark_a = part.life * part.a * darken * 65535.0f;
151
 
 
152
 
            w += (w * part.w_mod) * part.life;
153
 
            h += (h * part.h_mod) * part.life;
 
151
            a      = part.life * part.a * 65535.0f;
 
152
            dark_a = part.life * part.a * 65535.0f * darken;
 
153
 
 
154
            w += w * part.w_mod * part.life;
 
155
            h += h * part.h_mod * part.life;
 
156
 
 
157
            xMinusW = part.x - w;
 
158
            xPlusW  = part.x + w;
 
159
 
 
160
            yMinusH = part.y - h;
 
161
            yPlusH  = part.y + h;
154
162
 
155
163
            //first triangle
156
 
            vertices_cache[i + 0] = part.x - w;
157
 
            vertices_cache[i + 1] = part.y - h;
 
164
            vertices_cache[i + 0] = xMinusW;
 
165
            vertices_cache[i + 1] = yMinusH;
158
166
            vertices_cache[i + 2] = part.z;
159
167
 
160
 
            vertices_cache[i + 3] = part.x - w;
161
 
            vertices_cache[i + 4] = part.y + h;
 
168
            vertices_cache[i + 3] = xMinusW;
 
169
            vertices_cache[i + 4] = yPlusH;
162
170
            vertices_cache[i + 5] = part.z;
163
171
 
164
 
            vertices_cache[i + 6] = part.x + w;
165
 
            vertices_cache[i + 7] = part.y + h;
 
172
            vertices_cache[i + 6] = xPlusW;
 
173
            vertices_cache[i + 7] = yPlusH;
166
174
            vertices_cache[i + 8] = part.z;
167
175
 
168
176
            //second triangle
169
 
            vertices_cache[i + 9] = part.x + w;
170
 
            vertices_cache[i + 10] = part.y + h;
 
177
            vertices_cache[i + 9]  = xPlusW;
 
178
            vertices_cache[i + 10] = yPlusH;
171
179
            vertices_cache[i + 11] = part.z;
172
180
 
173
 
            vertices_cache[i + 12] = part.x + w;
174
 
            vertices_cache[i + 13] = part.y - h;
 
181
            vertices_cache[i + 12] = xPlusW;
 
182
            vertices_cache[i + 13] = yMinusH;
175
183
            vertices_cache[i + 14] = part.z;
176
184
 
177
 
            vertices_cache[i + 15] = part.x - w;
178
 
            vertices_cache[i + 16] = part.y - h;
 
185
            vertices_cache[i + 15] = xMinusW;
 
186
            vertices_cache[i + 16] = yMinusH;
179
187
            vertices_cache[i + 17] = part.z;
180
188
 
181
189
            i += 18;
211
219
            colors_cache[k + 6] = b;
212
220
            colors_cache[k + 7] = a;
213
221
 
214
 
            colors_cache[k + 8] = r;
215
 
            colors_cache[k + 9] = g;
 
222
            colors_cache[k + 8]  = r;
 
223
            colors_cache[k + 9]  = g;
216
224
            colors_cache[k + 10] = b;
217
225
            colors_cache[k + 11] = a;
218
226
 
234
242
 
235
243
            k += 24;
236
244
 
237
 
            if(darken > 0)
 
245
            if (darken > 0)
238
246
            {
239
247
                dcolors_cache[l + 0] = r;
240
248
                dcolors_cache[l + 1] = g;
246
254
                dcolors_cache[l + 6] = b;
247
255
                dcolors_cache[l + 7] = dark_a;
248
256
 
249
 
                dcolors_cache[l + 8] = r;
250
 
                dcolors_cache[l + 9] = g;
 
257
                dcolors_cache[l + 8]  = r;
 
258
                dcolors_cache[l + 9]  = g;
251
259
                dcolors_cache[l + 10] = b;
252
260
                dcolors_cache[l + 11] = dark_a;
253
261
 
271
279
            }
272
280
        }
273
281
    }
274
 
    
 
282
 
275
283
    GLVertexBuffer *stream = GLVertexBuffer::streamingBuffer ();
276
284
 
277
285
    if (darken > 0)
299
307
 
300
308
    glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
301
309
    glDisable (GL_TEXTURE_2D);
302
 
    glDisable (GL_BLEND);
 
310
 
 
311
    /* only disable blending if it was disabled before */
 
312
    if (!glBlendEnabled)
 
313
        glDisable (GL_BLEND);
303
314
}
304
315
 
305
316
void
306
 
ParticleSystem::updateParticles (float          time)
 
317
ParticleSystem::updateParticles (float time)
307
318
{
308
 
    float speed = (time / 50.0);
309
 
    float f_slowdown = slowdown * (1 - MAX (0.99, time / 1000.0) ) * 1000;
 
319
    float speed      = (time / 50.0);
 
320
    float f_slowdown = slowdown * (1 - MAX (0.99, time / 1000.0)) * 1000;
310
321
 
311
322
    active = false;
312
323
 
351
362
}
352
363
 
353
364
void
354
 
FireScreen::fireAddPoint (int        x,
355
 
                          int        y,
356
 
                          bool       requireGrab)
 
365
FireScreen::fireAddPoint (int  x,
 
366
                          int  y,
 
367
                          bool requireGrab)
357
368
{
358
 
 
359
369
    if (!requireGrab || grabIndex)
360
370
    {
361
371
        XPoint p;
366
376
        points.push_back (p);
367
377
 
368
378
        toggleFunctions (true);
369
 
 
370
379
    }
371
 
 
372
380
}
373
381
 
374
382
bool
376
384
                         CompAction::State  state,
377
385
                         CompOption::Vector options)
378
386
{
379
 
    float x, y;
380
 
 
381
 
    x = CompOption::getFloatOptionNamed (options, "x", 0);
382
 
    y = CompOption::getFloatOptionNamed (options, "y", 0);
 
387
    float x = CompOption::getFloatOptionNamed (options, "x", 0);
 
388
    float y = CompOption::getFloatOptionNamed (options, "y", 0);
383
389
 
384
390
    fireAddPoint (x, y, false);
385
391
 
394
400
                      CompOption::Vector options)
395
401
{
396
402
    if (screen->otherGrabExist (NULL))
397
 
        return false;
 
403
        return false;
398
404
 
399
405
    if (!grabIndex)
400
 
        grabIndex = screen->pushGrab (None, "firepaint");
 
406
        grabIndex = screen->pushGrab (None, "firepaint");
401
407
 
402
408
    if (state & CompAction::StateInitButton)
403
 
        action->setState (action->state () | CompAction::StateTermButton);
 
409
        action->setState (action->state () | CompAction::StateTermButton);
404
410
 
405
411
    if (state & CompAction::StateInitKey)
406
 
        action->setState (action->state () | CompAction::StateTermKey);
 
412
        action->setState (action->state () | CompAction::StateTermKey);
407
413
 
408
414
    fireAddPoint (pointerX, pointerY, true);
409
415
 
415
421
                       CompAction::State  state,
416
422
                       CompOption::Vector options)
417
423
{
418
 
 
419
424
    if (grabIndex)
420
425
    {
421
426
        screen->removeGrab (grabIndex, NULL);
423
428
    }
424
429
 
425
430
    action->setState (action->state () & ~(CompAction::StateTermKey |
426
 
                                           CompAction::StateTermButton));
427
 
 
 
431
                                           CompAction::StateTermButton));
428
432
    return false;
429
433
}
430
434
 
438
442
}
439
443
 
440
444
void
441
 
FireScreen::preparePaint (int      time)
 
445
FireScreen::preparePaint (int time)
442
446
{
443
 
    float bg = (float) optionGetBgBrightness () / 100.0;
 
447
    float bg = optionGetBgBrightness () / 100.0f;
444
448
 
445
449
    if (init && !points.empty ())
446
450
    {
458
462
        glBindTexture (GL_TEXTURE_2D, 0);
459
463
 
460
464
        ps.slowdown  = optionGetFireSlowdown ();
461
 
        ps.darken    = 0.5;
 
465
        ps.darken    = 0.5f; /* TODO: Magic number */
462
466
        ps.blendMode = GL_ONE;
463
 
 
464
467
    }
465
468
 
466
469
    if (!init)
468
471
 
469
472
    if (!points.empty ())
470
473
    {
471
 
        float max_new = MIN ((int) ps.particles.size (),  (int) points.size () * 2) *
472
 
                        ((float) time / 50.0) *
473
 
                        (1.05 - optionGetFireLife());
 
474
        int   rVal2;
474
475
        float rVal, size = 4;
475
 
        int rVal2;
 
476
        float fireLife   = optionGetFireLife ();
 
477
        float fireWidth  = optionGetFireSize ();
 
478
        float fireHeight = fireWidth * 1.5f;
 
479
        bool  mystFire   = optionGetFireMystical ();
 
480
        float max_new    = MIN ((int) ps.particles.size (),  (int) points.size () * 2) *
 
481
                           ((float) time / 50.0f) * (1.05f - fireLife);
476
482
 
477
 
        for (unsigned int i = 0;
478
 
             i < ps.particles.size () && max_new > 0; i++)
 
483
        for (unsigned int i = 0; i < ps.particles.size () && max_new > 0; ++i)
479
484
        {
480
485
            Particle &part = ps.particles.at (i);
 
486
 
481
487
            if (part.life <= 0.0f)
482
488
            {
483
489
                /* give gt new life */
484
490
                rVal = (float) (random () & 0xff) / 255.0;
485
491
                part.life = 1.0f;
486
492
                /* Random Fade Value */
487
 
                part.fade = (rVal * (1 - optionGetFireLife ()) +
488
 
                              (0.2f * (1.01 - optionGetFireLife ())));
 
493
                part.fade = (rVal * (1 - fireLife) +
 
494
                             (0.2f * (1.01 - fireLife)));
489
495
 
490
496
                /* set size */
491
 
                part.width  = optionGetFireSize ();
492
 
                part.height = optionGetFireSize () * 1.5;
 
497
                part.width  = fireWidth;
 
498
                part.height = fireHeight;
493
499
                rVal = (float) (random () & 0xff) / 255.0;
494
500
                part.w_mod = size * rVal;
495
501
                part.h_mod = size * rVal;
498
504
                rVal2 = random () % points.size ();
499
505
                part.x = points.at (rVal2).x;
500
506
                part.y = points.at (rVal2).y;
501
 
                part.z = 0.0;
 
507
                part.z = 0.0f;
502
508
                part.xo = part.x;
503
509
                part.yo = part.y;
504
510
                part.zo = part.z;
511
517
                part.zi = 0.0f;
512
518
                rVal = (float) (random () & 0xff) / 255.0;
513
519
 
514
 
                if (optionGetFireMystical () )
 
520
                if (mystFire)
515
521
                {
516
522
                    /* Random colors! (aka Mystical Fire) */
517
523
                    rVal = (float) (random () & 0xff) / 255.0;
523
529
                }
524
530
                else
525
531
                {
526
 
                    part.r = (float) optionGetFireColorRed () / 0xffff -
527
 
                              (rVal / 1.7 *
528
 
                               (float) optionGetFireColorRed () / 0xffff);
529
 
                    part.g = (float) optionGetFireColorGreen () / 0xffff -
530
 
                              (rVal / 1.7 *
531
 
                              (float) optionGetFireColorGreen () / 0xffff);
532
 
                    part.b = (float) optionGetFireColorBlue () / 0xffff -
533
 
                              (rVal / 1.7 *
534
 
                              (float) optionGetFireColorBlue () / 0xffff);
 
532
                    part.r = optionGetFireColorRed () / 0xffff -
 
533
                             (rVal / 1.7 * optionGetFireColorRed () / 0xffff);
 
534
                    part.g = optionGetFireColorGreen () / 0xffff -
 
535
                             (rVal / 1.7 * optionGetFireColorGreen () / 0xffff);
 
536
                    part.b = optionGetFireColorBlue () / 0xffff -
 
537
                             (rVal / 1.7 * optionGetFireColorBlue () / 0xffff);
535
538
                }
536
539
 
537
540
                /* set transparency */
547
550
                max_new -= 1;
548
551
            }
549
552
            else
550
 
            {
551
553
                part.xg = (part.x < part.xo) ? 1.0 : -1.0;
552
 
            }
553
554
        }
554
555
    }
555
556
 
558
559
        float div = 1.0 - bg;
559
560
        div *= (float) time / 500.0;
560
561
        brightness = MAX (bg, brightness - div);
561
 
 
562
562
    }
563
563
 
564
564
    if (points.empty () && brightness != 1.0)
566
566
        float div = 1.0 - bg;
567
567
        div *= (float) time / 500.0;
568
568
        brightness = MIN (1.0, brightness + div);
569
 
 
570
569
    }
571
570
 
572
571
    if (!init && points.empty () && !ps.active)
580
579
 
581
580
bool
582
581
FireScreen::glPaintOutput (const GLScreenPaintAttrib &attrib,
583
 
                           const GLMatrix            &transform,
584
 
                           const CompRegion          &region,
585
 
                           CompOutput                *output,
586
 
                           unsigned int              mask)
 
582
                           const GLMatrix            &transform,
 
583
                           const CompRegion          &region,
 
584
                           CompOutput                *output,
 
585
                           unsigned int              mask)
587
586
{
588
 
    bool status;
589
 
 
590
 
    status = gScreen->glPaintOutput (attrib, transform, region, output, mask);
591
 
 
592
 
    if ( (!init && ps.active) || brightness < 1.0)
 
587
    bool status = gScreen->glPaintOutput (attrib, transform, region, output, mask);
 
588
 
 
589
    if ((!init && ps.active) || brightness < 1.0)
593
590
    {
594
591
        GLMatrix sTransform = transform;
595
592
 
600
597
            /* cover the screen with a rectangle and darken it
601
598
             * (coded as two GL_TRIANGLES for GLES compatibility)
602
599
             */
603
 
 
604
600
            GLfloat vertices[18];
605
601
            GLushort colors[24];
606
602
 
616
612
            vertices[7] = (GLfloat)output->region ()->extents.y2;
617
613
            vertices[8] = 0.0f;
618
614
 
619
 
            vertices[9] = (GLfloat)output->region ()->extents.x2;
 
615
            vertices[9]  = (GLfloat)output->region ()->extents.x2;
620
616
            vertices[10] = (GLfloat)output->region ()->extents.y2;
621
617
            vertices[11] = 0.0f;
622
618
 
628
624
            vertices[16] = (GLfloat)output->region ()->extents.y1;
629
625
            vertices[17] = 0.0f;
630
626
 
631
 
            for (int i = 0; i <= 5; i++)
 
627
            for (int i = 0; i <= 5; ++i)
632
628
            {
633
629
                colors[i*4+0] = 0;
634
630
                colors[i*4+1] = 0;
636
632
                colors[i*4+3] = (1.0 - brightness) * 65535.0f;
637
633
            }
638
634
 
639
 
            GLVertexBuffer *stream = GLVertexBuffer::streamingBuffer ();
640
 
            glEnable (GL_BLEND);
 
635
            GLVertexBuffer *stream        = GLVertexBuffer::streamingBuffer ();
 
636
            GLboolean      glBlendEnabled = glIsEnabled (GL_BLEND);
 
637
 
 
638
            if (!glBlendEnabled)
 
639
                glEnable (GL_BLEND);
 
640
 
641
641
            stream->begin (GL_TRIANGLES);
642
642
            stream->addVertices (6, vertices);
643
643
            stream->addColors (6, colors);
645
645
            if (stream->end ())
646
646
                stream->render (sTransform);
647
647
 
648
 
            glDisable (GL_BLEND);
 
648
            /* only disable blending if it was already disabled */
 
649
            if (!glBlendEnabled)
 
650
                glDisable (GL_BLEND);
649
651
        }
650
652
 
651
653
        if (!init && ps.active)
652
654
            ps.drawParticles (sTransform);
653
 
 
654
655
    }
655
656
 
656
657
    return status;
660
661
FireScreen::donePaint ()
661
662
{
662
663
    if ( (!init && ps.active) || !points.empty () || brightness < 1.0)
663
 
    {
664
664
        cScreen->damageScreen ();
665
 
    }
666
665
    else
667
666
        toggleFunctions (false);
668
667
 
674
673
{
675
674
    switch (event->type)
676
675
    {
677
 
 
678
676
    case MotionNotify:
679
677
        fireAddPoint (pointerX, pointerY, true);
680
678
        break;
682
680
    case EnterNotify:
683
681
    case LeaveNotify:
684
682
        fireAddPoint (pointerX, pointerY, true);
 
683
        break;
 
684
 
685
685
    default:
686
686
        break;
687
687
    }