| 1 | /* |
| 2 | * nervousTV - The name says it all... |
| 3 | * Copyright (C) 2002 TANNENBAUM Edo |
| 4 | * |
| 5 | * 2002/2/9 |
| 6 | * Original code copied same frame twice, and did not use memcpy(). |
| 7 | * I modified those point. |
| 8 | * -Kentarou Fukuchi |
| 9 | * |
| 10 | * 2009/8/26 |
| 11 | * Ported to frei0r from the old FreeJ filter API |
| 12 | * -Jaromil |
| 13 | * |
| 14 | * This source code is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU Public License as published by |
| 16 | * the Free Software Foundation; either version 3 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This source code is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Please refer |
| 22 | * to the GNU Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Public License along |
| 25 | * with this source code; if not, write to: Free Software Foundation, |
| 26 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <assert.h> |
| 34 | #include <math.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | #include <frei0r.hpp> |
| 38 | |
| 39 | |
| 40 | #define PLANES 32 |
| 41 | |
| 42 | // freej compat facilitator |
| 43 | typedef struct { |
| 44 | int16_t w; |
| 45 | int16_t h; |
| 46 | uint8_t bpp; |
| 47 | uint32_t size; |
| 48 | } ScreenGeometry; |
| 49 | |
| 50 | |
| 51 | |
| 52 | class Nervous: public frei0r::filter { |
| 53 | |
| 54 | public: |
| 55 | |
| 56 | Nervous(int width, int height); |
| 57 | |
| 58 | ~Nervous(); |
| 59 | |
| 60 | virtual void update(double time, |
| 61 | uint32_t* out, |
| 62 | const uint32_t* in); |
| 63 | |
| 64 | private: |
| 65 | |
| 66 | ScreenGeometry geo; |
| 67 | |
| 68 | void _init(int wdt, int hgt); |
| 69 | int32_t *buffer; |
| 70 | int32_t *planetable[PLANES]; |
| 71 | int mode; |
| 72 | int plane, stock, timer, stride, readplane; |
| 73 | |
| 74 | /* cheap & fast randomizer (by Fukuchi Kentarou) */ |
| 75 | uint32_t randval; |
| 76 | uint32_t fastrand() { return (randval=randval*1103515245+12345); }; |
| 77 | void fastsrand(uint32_t seed) { randval = seed; }; |
| 78 | |
| 79 | }; |
| 80 | |
| 81 | Nervous::Nervous(int wdt, int hgt) { |
| 82 | int c; |
| 83 | _init(wdt, hgt); |
| 84 | |
| 85 | buffer = (int32_t*) calloc(geo.size, PLANES); |
| 86 | if(!buffer) { |
| 87 | fprintf(stderr,"ERROR: nervous plugin can't allocate needed memory: %u bytes\n", |
| 88 | geo.size*PLANES); |
| 89 | return; |
| 90 | } |
| 91 | for(c=0;c<PLANES;c++) |
| 92 | planetable[c] = &buffer[geo.w*geo.h*c]; |
| 93 | |
| 94 | plane = 0; |
| 95 | stock = 0; |
| 96 | timer = 0; |
| 97 | readplane = 0; |
| 98 | mode = 1; |
| 99 | } |
| 100 | |
| 101 | Nervous::~Nervous() { |
| 102 | free(buffer); |
| 103 | } |
| 104 | |
| 105 | void Nervous::_init(int wdt, int hgt) { |
| 106 | geo.w = wdt; |
| 107 | geo.h = hgt; |
| 108 | geo.bpp = 32; |
| 109 | geo.size = geo.w*geo.h*(geo.bpp/8); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | void Nervous::update(double time, |
| 116 | uint32_t* out, |
| 117 | const uint32_t* in) { |
| 118 | memcpy(planetable[plane],in,geo.size); |
| 119 | |
| 120 | if(stock<PLANES) stock++; |
| 121 | |
| 122 | if(mode) { |
| 123 | if(timer) { |
| 124 | readplane = readplane + stride; |
| 125 | while(readplane < 0) readplane += stock; |
| 126 | while(readplane >= stock) readplane -= stock; |
| 127 | timer--; |
| 128 | } else { |
| 129 | readplane = fastrand() % stock; |
| 130 | stride = fastrand() % 5 - 2; |
| 131 | if(stride >= 0) stride++; |
| 132 | timer = fastrand() % 6 + 2; |
| 133 | } |
| 134 | } else |
| 135 | if(stock > 0) |
| 136 | readplane = fastrand() % stock; |
| 137 | |
| 138 | plane++; |
| 139 | if(plane==PLANES) plane=0; |
| 140 | |
| 141 | memcpy(out,planetable[readplane],geo.size); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | frei0r::construct<Nervous> plugin("Nervous", |
| 148 | "flushes frames in time in a nervous way", |
| 149 | "Tannenbaum, Kentaro, Jaromil", |
| 150 | 3,1); |