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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/* xscreensaver, Copyright (c) 1999 Jamie Zawinski <jwz@jwz.org>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*
* Matrix -- simulate the text scrolls from the movie "The Matrix".
*
* The movie people distribute their own Windows/Mac screensaver that does
* a similar thing, so I wrote one for Unix. However, that version (the
* Windows/Mac version at http://www.whatisthematrix.com/) doesn't match my
* memory of what the screens in the movie looked like, so my `xmatrix'
* does things differently.
*/
#include <stdio.h>
#include "images/small.xpm"
#include "images/medium.xpm"
#include "images/large.xpm"
#include "images/matrix.xbm"
/*
#define CHAR_HEIGHT 4
*/
#include "matrix.h"
/*
#include "resources.h"
*/
extern GC NormalGC;
extern GC EraseGC;
extern Pixel back_pix, fore_pix;
extern int PixmapSize;
int CHAR_HEIGHT;
static void load_images (m_state *state) {
if (state->xgwa.depth > 1) {
XpmAttributes xpmattrs;
int result;
xpmattrs.valuemask = 0;
xpmattrs.valuemask |= XpmCloseness;
xpmattrs.closeness = 40000;
xpmattrs.valuemask |= XpmVisual;
xpmattrs.visual = state->xgwa.visual;
xpmattrs.valuemask |= XpmDepth;
xpmattrs.depth = state->xgwa.depth;
xpmattrs.valuemask |= XpmColormap;
xpmattrs.colormap = state->xgwa.colormap;
if (PixmapSize == 1){
CHAR_HEIGHT = 4;
result = XpmCreatePixmapFromData (state->dpy, state->window, small,
&state->images, 0 /* mask */,
&xpmattrs);
} else if (PixmapSize == 2){
CHAR_HEIGHT = 6;
result = XpmCreatePixmapFromData (state->dpy, state->window, medium,
&state->images, 0 /* mask */,
&xpmattrs);
} else {
CHAR_HEIGHT = 8;
result = XpmCreatePixmapFromData (state->dpy, state->window, large,
&state->images, 0 /* mask */,
&xpmattrs);
}
if (!state->images || (result != XpmSuccess && result != XpmColorError))
state->images = 0;
state->image_width = xpmattrs.width;
state->image_height = xpmattrs.height;
state->nglyphs = state->image_height / CHAR_HEIGHT;
} else {
state->image_width = matrix_width;
state->image_height = matrix_height;
state->nglyphs = state->image_height / CHAR_HEIGHT;
state->images = XCreatePixmapFromBitmapData (state->dpy, state->window,
(char *) matrix_bits,
state->image_width, state->image_height,
back_pix, fore_pix, state->xgwa.depth);
}
}
m_state *init_matrix( Display *dpy, Window window ) {
m_state *state = (m_state *) calloc (sizeof(*state), 1);
state->dpy = dpy;
state->window = window;
XGetWindowAttributes (dpy, window, &state->xgwa);
load_images (state);
state->draw_gc = NormalGC;
state->erase_gc = EraseGC;
state->char_width = state->image_width / 2;
state->char_height = CHAR_HEIGHT;
state->grid_width = state->xgwa.width / state->char_width;
state->grid_height = state->xgwa.height / state->char_height;
state->grid_width++;
state->grid_height++;
state->cells = (m_cell *)calloc (sizeof(m_cell), state->grid_width * state->grid_height);
state->feeders = (m_feeder *)calloc (sizeof(m_feeder), state->grid_width);
state->density = 40;
state->insert_top_p = False;
state->insert_bottom_p = True;
return state;
}
static void insert_glyph (m_state *state, int glyph, int x, int y) {
Bool bottom_feeder_p = (y >= 0);
m_cell *from, *to;
if (y >= state->grid_height) return;
if (bottom_feeder_p) {
to = &state->cells[state->grid_width * y + x];
} else {
for (y = state->grid_height-1; y > 0; y--) {
from = &state->cells[state->grid_width * (y-1) + x];
to = &state->cells[state->grid_width * y + x];
*to = *from;
to->changed = True;
}
to = &state->cells[x];
}
to->glyph = glyph;
to->changed = True;
if (!to->glyph) ;
else if (bottom_feeder_p) to->glow = 1 + (random() % 2);
else to->glow = 0;
}
static void feed_matrix (m_state *state) {
int x;
/*
* Update according to current feeders.
*/
for (x = 0; x < state->grid_width; x++) {
m_feeder *f = &state->feeders[x];
if (f->throttle) { /* this is a delay tick, synced to frame. */
f->throttle--;
} else if (f->remaining > 0) { /* how many items are in the pipe */
int g = (random() % state->nglyphs) + 1;
insert_glyph (state, g, x, f->y);
f->remaining--;
if (f->y >= 0) f->y++; /* bottom_feeder_p */
} else { /* if pipe is empty, insert spaces */
insert_glyph (state, 0, x, f->y);
if (f->y >= 0) f->y++; /* bottom_feeder_p */
}
if ((random() % 10) == 0) { /* randomly change throttle speed */
f->throttle = ((random() % 5) + (random() % 5));
}
}
}
static int densitizer (m_state *state) {
/* Horrid kludge that converts percentages (density of screen coverage)
to the parameter that actually controls this. I got this mapping
empirically, on a 1024x768 screen. Sue me. */
if (state->density < 10) return 85;
else if (state->density < 15) return 60;
else if (state->density < 20) return 45;
else if (state->density < 25) return 25;
else if (state->density < 30) return 20;
else if (state->density < 35) return 15;
else if (state->density < 45) return 10;
else if (state->density < 50) return 8;
else if (state->density < 55) return 7;
else if (state->density < 65) return 5;
else if (state->density < 80) return 3;
else if (state->density < 90) return 2;
else return 1;
}
static void hack_matrix (m_state *state) {
int x;
/* Glow some characters. */
if (!state->insert_bottom_p) {
int i = random() % (state->grid_width / 2);
while (--i > 0) {
int x = random() % state->grid_width;
int y = random() % state->grid_height;
m_cell *cell = &state->cells[state->grid_width * y + x];
if (cell->glyph && cell->glow == 0) {
cell->glow = random() % 10;
cell->changed = True;
}
}
}
/* Change some of the feeders. */
for (x = 0; x < state->grid_width; x++) {
m_feeder *f = &state->feeders[x];
Bool bottom_feeder_p;
if (f->remaining > 0) /* never change if pipe isn't empty */
continue;
if ((random() % densitizer(state)) != 0) /* then change N% of the time */
continue;
f->remaining = 3 + (random() % state->grid_height);
f->throttle = ((random() % 5) + (random() % 5));
if ((random() % 4) != 0)
f->remaining = 0;
if (state->insert_top_p && state->insert_bottom_p)
bottom_feeder_p = (random() & 1);
else
bottom_feeder_p = state->insert_bottom_p;
if (bottom_feeder_p)
f->y = random() % (state->grid_height / 2);
else
f->y = -1;
}
}
void draw_matrix (m_state *state, int d) {
int x, y;
int count = 0;
state->density = d;
feed_matrix( state );
hack_matrix( state );
for (y = 0; y < state->grid_height; y++) {
for (x = 0; x < state->grid_width; x++) {
m_cell *cell = &state->cells[state->grid_width * y + x];
if ( cell->glyph ) count++;
if ( !cell->changed ) continue;
if ( cell->glyph == 0 ) {
XFillRectangle( state->dpy, state->window, state->erase_gc,
x * state->char_width, y * state->char_height,
state->char_width, state->char_height );
} else {
XCopyArea( state->dpy, state->images, state->window, state->draw_gc,
(cell->glow ? state->char_width : 0), (cell->glyph - 1) * state->char_height,
state->char_width, state->char_height, x * state->char_width, y * state->char_height );
}
cell->changed = False;
if (cell->glow > 0) {
cell->glow--;
cell->changed = True;
}
}
}
#if 0
{
static int i = 0;
static int ndens = 0;
static int tdens = 0;
i++;
if (i > 50)
{
int dens = (100.0 *
(((double)count) /
((double) (state->grid_width * state->grid_height))));
tdens += dens;
ndens++;
printf ("density: %d%% (%d%%)\n", dens, (tdens / ndens));
i = 0;
}
}
#endif
}
|