File: animated.cxx

package info (click to toggle)
fltk1.3 1.3.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,224 kB
  • sloc: cpp: 105,017; ansic: 91,383; sh: 6,825; makefile: 1,948; perl: 24; xml: 7
file content (155 lines) | stat: -rw-r--r-- 3,804 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// "$Id$"
//
// Alpha rendering benchmark program for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2015 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
//     http://www.fltk.org/str.php
//

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Image.H>
#include <FL/x.H>
#include <FL/fl_draw.H>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum {
  FRAMES = 48,
  DIM = 256
};

static Fl_RGB_Image *img[FRAMES];
static uchar curframe;

static void make_images() {

  unsigned i;
  for (i = 0; i < FRAMES; i++) {
    const unsigned size = DIM * DIM * 4;
    uchar *data = new uchar[size];

    memset(data, 0, size);

    // First a black box, 10x10 pixels in the top-left corner
    int x, y;
    for (x = 0; x < 10; x++) {
      for (y = 0; y < 10; y++) {
        data[y * DIM * 4 + x * 4 + 3] = 255;
      }
    }

    // A fading sphere
    uchar alpha = 255;
    if (i < FRAMES / 2)
      alpha = (uchar)(255 * (i / ((float) FRAMES / 2)));
    else
      alpha = (uchar)(255 * (((FRAMES / 2) - (i - FRAMES / 2)) / ((float) FRAMES / 2)));

    const int spherew = 60;
    const int spherex = (DIM - spherew) / 2;
    const int maxdist = (spherew / 2) * (spherew / 2);
    for (x = spherex; x < spherex + spherew; x++) {
      for (y = 20; y < 20 + spherew; y++) {

        float distx = x - (spherex + (float) spherew / 2);
        float disty = y - (20 + (float) spherew / 2);
        float dist = distx * distx + disty * disty;

        if (dist > maxdist)
          continue;

        const float fill = dist / maxdist;
        const uchar grey = (uchar)(fill * 255);

        uchar myalpha = alpha;
        if (fill > 0.9)
          myalpha = (uchar)(myalpha * (1.0f - fill) * 10);

        data[y * DIM * 4 + x * 4 + 0] = grey;
        data[y * DIM * 4 + x * 4 + 1] = grey;
        data[y * DIM * 4 + x * 4 + 2] = grey;
        data[y * DIM * 4 + x * 4 + 3] = myalpha;
      }
    }

    // A moving blob
    const float pos = (i / (float) FRAMES) * 2 - 0.5;

    const int xoffset = (int)(pos * DIM);
    const int yoffset = 2 * DIM / 3;
    const int w = DIM / 4;

    for (x = -w; x < w; x++) {
      if (x + xoffset < 0 || x + xoffset >= DIM)
        continue;
      for (y = yoffset - w; y < yoffset + w; y++) {
        const uchar grey = abs(y - yoffset);
//        data[y * DIM * 4 + (x + xoffset) * 4 + 0] = grey;
//        data[y * DIM * 4 + (x + xoffset) * 4 + 1] = grey;
        data[y * DIM * 4 + (x + xoffset) * 4 + 2] = grey;
        data[y * DIM * 4 + (x + xoffset) * 4 + 3] = 127;
      }
    }

    img[i] = new Fl_RGB_Image(data, DIM, DIM, 4);
  }
}

class window: public Fl_Double_Window {
public:
  window(int X, int Y, const char *lbl): Fl_Double_Window(X, Y, lbl) {}

  void draw() {
    Fl_Double_Window::draw();

    // Test both cx/cy offset and clipping. Both borders should have a 5-pixel edge,
    // and the upper-left black box should not be visible.
    fl_push_clip(5, 5, w() - 5, h() - 5);
    img[curframe]->draw(0, 0, DIM, DIM, 5, 5);
    fl_pop_clip();
  }
};

static window *win;

static void cb(void *) {

  win->redraw();

  Fl::repeat_timeout(1.0f / 24, cb);

  curframe++;
  curframe %= FRAMES;
}

int main(int argc, char **argv) {
  win = new window(256, 256, "Alpha rendering benchmark, watch CPU use");
  win->color(fl_rgb_color(142, 0, 0));

  make_images();

  win->end();
  win->show(argc, argv);

  Fl::add_timeout(1.0f / 24, cb);

  return Fl::run();
}

//
// End of "$Id$".
//