IFS: Style fixes

This commit is contained in:
John Scipione
2014-05-15 17:40:49 -04:00
parent 4c901476bf
commit 98b054fe02
4 changed files with 421 additions and 376 deletions
+277 -253
View File
@@ -1,12 +1,13 @@
/*
* Copyright 2006, Haiku.
* Copyright (c) 1997 by Massimino Pascal <[email protected]>
* Copyright 2006-2014, Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT License.
*
* Authors:
* Massimino Pascal <[email protected]>
* Stephan Aßmus <[email protected]>
* Stephan Aßmus, [email protected]
* Massimino Pascal, [email protected]
* John Scipione, [email protected]
*/
/*! When shown ifs, Diana Rose (4 years old) said, "It looks like dancing."
@@ -24,128 +25,136 @@
#include <Screen.h>
#include <View.h>
#include <unistd.h> // for getpid()
#include <sys/time.h> // for gettimeofday()
#include <unistd.h>
// for getpid()
#include <sys/time.h>
// for gettimeofday()
using std::nothrow;
#define HALF 0
#define random() ya_random()
#define RAND_MAX 0xFFFFFFFF
#define FLOAT_TO_INT(x) (int32)((float)(UNIT)*(x))
#define LRAND() ((long) (random() & 0x7fffffff))
#define NRAND(n) ((int) (LRAND() % (n)))
#define MAXRAND (2147483648.0)
// unsigned 1<<31 as a float
#define SRAND(n)
// already seeded by screenhack.c TODO: ?!? is it?
// The following 'random' numbers are taken from CRC, 18th Edition, page 622.
// Each array element was taken from the corresponding line in the table,
// except that a[0] was from line 100. 8s and 9s in the table were simply
// skipped. The high order digit was taken mod 4.
#define VectorSize 55
static unsigned int a[VectorSize] = {
035340171546, 010401501101, 022364657325, 024130436022, 002167303062, // 5
037570375137, 037210607110, 016272055420, 023011770546, 017143426366, // 10
014753657433, 021657231332, 023553406142, 004236526362, 010365611275, // 14
007117336710, 011051276551, 002362132524, 001011540233, 012162531646, // 20
007056762337, 006631245521, 014164542224, 032633236305, 023342700176, // 25
002433062234, 015257225043, 026762051606, 000742573230, 005366042132, // 30
012126416411, 000520471171, 000725646277, 020116577576, 025765742604, // 35
007633473735, 015674255275, 017555634041, 006503154145, 021576344247, // 40
014577627653, 002707523333, 034146376720, 030060227734, 013765414060, // 45
036072251540, 007255221037, 024364674123, 006200353166, 010126373326, // 50
015664104320, 016401041535, 016215305520, 033115351014, 017411670323 // 55
#define VECTOR_SIZE 55
static unsigned int a[VECTOR_SIZE] = {
035340171546, 010401501101, 022364657325, 024130436022, 002167303062, // 5
037570375137, 037210607110, 016272055420, 023011770546, 017143426366, // 10
014753657433, 021657231332, 023553406142, 004236526362, 010365611275, // 14
007117336710, 011051276551, 002362132524, 001011540233, 012162531646, // 20
007056762337, 006631245521, 014164542224, 032633236305, 023342700176, // 25
002433062234, 015257225043, 026762051606, 000742573230, 005366042132, // 30
012126416411, 000520471171, 000725646277, 020116577576, 025765742604, // 35
007633473735, 015674255275, 017555634041, 006503154145, 021576344247, // 40
014577627653, 002707523333, 034146376720, 030060227734, 013765414060, // 45
036072251540, 007255221037, 024364674123, 006200353166, 010126373326, // 50
015664104320, 016401041535, 016215305520, 033115351014, 017411670323 // 55
};
static int i1, i2;
// ya_random
static int i1;
static int i2;
unsigned int
ya_random (void)
ya_random(void)
{
register int ret = a[i1] + a[i2];
a[i1] = ret;
if (++i1 >= VectorSize) i1 = 0;
if (++i2 >= VectorSize) i2 = 0;
return ret;
register int ret = a[i1] + a[i2];
a[i1] = ret;
if (++i1 >= VECTOR_SIZE)
i1 = 0;
if (++i2 >= VECTOR_SIZE)
i2 = 0;
return ret;
}
// ya_rand_init
void
ya_rand_init(unsigned int seed)
{
int i;
if (seed == 0)
{
struct timeval tp;
struct timezone tzp;
gettimeofday(&tp, &tzp);
/* ignore overflow */
seed = (999*tp.tv_sec) + (1001*tp.tv_usec) + (1003 * getpid());
}
int i;
if (seed == 0) {
struct timeval tp;
struct timezone tzp;
gettimeofday(&tp, &tzp);
// ignore overflow
seed = (999*tp.tv_sec) + (1001*tp.tv_usec) + (1003 * getpid());
}
a[0] += seed;
for (i = 1; i < VectorSize; i++)
{
seed = a[i-1]*1001 + seed*999;
a[i] += seed;
}
a[0] += seed;
for (i = 1; i < VECTOR_SIZE; i++) {
seed = a[i-1]*1001 + seed*999;
a[i] += seed;
}
i1 = a[0] % VectorSize;
i2 = (i1 + 024) % VectorSize;
i1 = a[0] % VECTOR_SIZE;
i2 = (i1 + 024) % VECTOR_SIZE;
}
#define random() ya_random()
#define RAND_MAX 0xFFFFFFFF
#define FLOAT_TO_INT(x) (int32)( (float)(UNIT)*(x) )
#define LRAND() ((long) (random() & 0x7fffffff))
#define NRAND(n) ((int) (LRAND() % (n)))
#define MAXRAND (2147483648.0) // unsigned 1<<31 as a float
#define SRAND(n) // already seeded by screenhack.c TODO: ?!? is it?
// gauss_rand
static float
gauss_rand(float c, float A, float S)
{
float y;
y = (float) LRAND() / MAXRAND;
float y = (float) LRAND() / MAXRAND;
y = A * (1.0 - exp(-y * y * S)) / (1.0 - exp(-S));
if (NRAND(2))
return (c + y);
return (c - y);
}
// half_gauss_rand
static float
half_gauss_rand(float c, float A, float S)
{
float y;
y = (float) LRAND() / MAXRAND;
float y = (float) LRAND() / MAXRAND;
y = A * (1.0 - exp(-y * y * S)) / (1.0 - exp(-S));
return (c + y);
}
// transform
inline void
transform(SIMI* Simi, int32 xo, int32 yo, int32* x, int32* y)
transform(SIMILITUDE* Similitude, int32 xo, int32 yo, int32* x, int32* y)
{
int32 xx, yy;
int32 xx;
int32 yy;
xo = xo - Simi->Cx;
xo = (xo * Simi->R) / UNIT;
yo = yo - Simi->Cy;
yo = (yo * Simi->R) / UNIT;
xo = xo - Similitude->Cx;
xo = (xo * Similitude->R) / UNIT;
yo = yo - Similitude->Cy;
yo = (yo * Similitude->R) / UNIT;
xx = xo - Simi->Cx;
xx = (xx * Simi->R2) / UNIT;
yy = -yo - Simi->Cy;
yy = (yy * Simi->R2) / UNIT;
xx = xo - Similitude->Cx;
xx = (xx * Similitude->R2) / UNIT;
yy = -yo - Similitude->Cy;
yy = (yy * Similitude->R2) / UNIT;
*x = ((xo * Simi->Ct - yo * Simi->St + xx * Simi->Ct2 - yy * Simi->St2) / UNIT) + Simi->Cx;
*y = ((xo * Simi->St + yo * Simi->Ct + xx * Simi->St2 + yy * Simi->Ct2) / UNIT) + Simi->Cy;
*x = ((xo * Similitude->Ct - yo * Similitude->St + xx * Similitude->Ct2
- yy * Similitude->St2) / UNIT) + Similitude->Cx;
*y = ((xo * Similitude->St + yo * Similitude->Ct + xx * Similitude->St2
+ yy * Similitude->Ct2) / UNIT) + Similitude->Cy;
}
// constructor
IFS::IFS(BRect bounds)
:
fRoot(NULL),
@@ -160,61 +169,62 @@ IFS::IFS(BRect bounds)
ya_rand_init(system_time());
int i;
FRACTAL *Fractal;
int i;
FRACTAL* Fractal;
if (fRoot == NULL) {
fRoot = (FRACTAL*) calloc(1, sizeof (FRACTAL));
fRoot = (FRACTAL*)calloc(1, sizeof(FRACTAL));
if (fRoot == NULL)
return;
}
Fractal = fRoot;
_FreeBuffers(Fractal);
i = (NRAND(4)) + 2; // Number of centers
i = (NRAND(4)) + 2;
// Number of centers
switch (i) {
case 2:
default:
Fractal->Depth = fAdditive ? MAX_DEPTH_2 + 1 : MAX_DEPTH_2;
Fractal->r_mean = 0.7;
Fractal->dr_mean = 0.3;
Fractal->dr2_mean = 0.4;
break;
case 3:
Fractal->Depth = fAdditive ? MAX_DEPTH_3 + 1 : MAX_DEPTH_3;
Fractal->r_mean = .6;
Fractal->dr_mean = .4;
Fractal->dr2_mean = .3;
Fractal->r_mean = 0.6;
Fractal->dr_mean = 0.4;
Fractal->dr2_mean = 0.3;
break;
case 4:
Fractal->Depth = MAX_DEPTH_4;
Fractal->r_mean = .5;
Fractal->dr_mean = .4;
Fractal->dr2_mean = .3;
Fractal->r_mean = 0.5;
Fractal->dr_mean = 0.4;
Fractal->dr2_mean = 0.3;
break;
case 5:
Fractal->Depth = MAX_DEPTH_5;
Fractal->r_mean = .5;
Fractal->dr_mean = .4;
Fractal->dr2_mean = .3;
break;
case 2:
default:
Fractal->Depth = fAdditive ? MAX_DEPTH_2 + 1 : MAX_DEPTH_2;
Fractal->r_mean = .7;
Fractal->dr_mean = .3;
Fractal->dr2_mean = .4;
Fractal->r_mean = 0.5;
Fractal->dr_mean = 0.4;
Fractal->dr2_mean = 0.3;
break;
}
// fprintf( stderr, "N=%d\n", i );
Fractal->Nb_Simi = i;
Fractal->Max_Pt = Fractal->Nb_Simi - 1;
for (i = 0; i <= Fractal->Depth + 2; ++i)
Fractal->Max_Pt *= Fractal->Nb_Simi;
if ((Fractal->buffer1 = (Point *) calloc(Fractal->Max_Pt,
sizeof (Point))) == NULL) {
Fractal->SimilitudeCount = i;
Fractal->MaxPoint = Fractal->SimilitudeCount - 1;
for (i = 0; i <= Fractal->Depth + 2; ++i)
Fractal->MaxPoint *= Fractal->SimilitudeCount;
if ((Fractal->buffer1 = (Point *)calloc(Fractal->MaxPoint,
sizeof(Point))) == NULL) {
_FreeIFS(Fractal);
return;
}
if ((Fractal->buffer2 = (Point *) calloc(Fractal->Max_Pt,
sizeof (Point))) == NULL) {
if ((Fractal->buffer2 = (Point *)calloc(Fractal->MaxPoint,
sizeof(Point))) == NULL) {
_FreeIFS(Fractal);
return;
}
@@ -226,73 +236,77 @@ IFS::IFS(BRect bounds)
Fractal->Width = bounds.IntegerWidth() + 1;
Fractal->Height = bounds.IntegerHeight() + 1;
#endif
Fractal->Cur_Pt = 0;
Fractal->CurrentPoint = 0;
Fractal->Count = 0;
Fractal->Lx = (Fractal->Width - 1) / 2;
Fractal->Ly = (Fractal->Height - 1) / 2;
Fractal->Col = NRAND(Fractal->Width * Fractal->Height - 1) + 1;
_RandomSimis(Fractal, Fractal->Components, 5 * MAX_SIMI);
_RandomSimilitudes(Fractal, Fractal->Components, 5 * MAX_SIMILITUDE);
delete Fractal->bitmap;
Fractal->bitmap = new (nothrow) BBitmap(BRect(0.0, 0.0,
Fractal->Width - 1,
Fractal->Height - 1),
0,
B_RGB32);
Fractal->bitmap = new BBitmap(BRect(0.0, 0.0,
Fractal->Width - 1, Fractal->Height - 1), 0, B_RGB32);
delete Fractal->markBitmap;
Fractal->markBitmap = new (nothrow) BBitmap(BRect(0.0, 0.0,
Fractal->Width - 1,
Fractal->Height - 1),
0,
B_GRAY8);
// Allocation checked
if (Fractal->bitmap != NULL && Fractal->bitmap->IsValid()) {
Fractal->markBitmap = new BBitmap(BRect(0.0, 0.0,
Fractal->Width - 1, Fractal->Height - 1), 0, B_GRAY8);
// allocation checked
if (Fractal->bitmap != NULL && Fractal->bitmap->IsValid())
memset(Fractal->bitmap->Bits(), 0, Fractal->bitmap->BitsLength());
} else {
else {
delete Fractal->bitmap;
Fractal->bitmap = NULL;
}
if (Fractal->markBitmap != NULL && Fractal->markBitmap->IsValid()) {
memset(Fractal->markBitmap->Bits(), 0, Fractal->markBitmap->BitsLength());
memset(Fractal->markBitmap->Bits(), 0,
Fractal->markBitmap->BitsLength());
} else {
delete Fractal->markBitmap;
Fractal->markBitmap = NULL;
}
}
// destructor
IFS::~IFS()
{
if (fRoot != NULL) {
_FreeIFS(fRoot);
free((void*) fRoot);
free((void*)fRoot);
}
}
// Draw
void
IFS::Draw(BView* view, const buffer_info* info, int32 frames)
{
//bigtime_t now = system_time();
int i;
float u, uu, v, vv, u0, u1, u2, u3;
SIMI *S, *S1, *S2, *S3, *S4;
FRACTAL *F;
int i;
float u;
float uu;
float v;
float vv;
float u0;
float u1;
float u2;
float u3;
SIMILITUDE* S;
SIMILITUDE* S1;
SIMILITUDE* S2;
SIMILITUDE* S3;
SIMILITUDE* S4;
FRACTAL* F;
if (fRoot == NULL)
return;
F = fRoot;
if (F->buffer1 == NULL)
return;
//if (frames > 1)
// printf("skipping %ld frames\n", frames);
// do this as many times as necessary to calculate the missing frames
// so the animation doesn't jerk when we miss a few frames
for (int32 frame = 0; frame < frames; frame++) {
u = (float) (F->Count) * (float) (F->Speed) / 1000.0;
uu = u * u;
v = 1.0 - u;
@@ -301,14 +315,14 @@ IFS::Draw(BView* view, const buffer_info* info, int32 frames)
u1 = 3.0 * vv * u;
u2 = 3.0 * v * uu;
u3 = u * uu;
S = F->Components;
S1 = S + F->Nb_Simi;
S2 = S1 + F->Nb_Simi;
S3 = S2 + F->Nb_Simi;
S4 = S3 + F->Nb_Simi;
for (i = F->Nb_Simi; i; --i, S++, S1++, S2++, S3++, S4++) {
S1 = S + F->SimilitudeCount;
S2 = S1 + F->SimilitudeCount;
S3 = S2 + F->SimilitudeCount;
S4 = S3 + F->SimilitudeCount;
for (i = F->SimilitudeCount; i; --i, S++, S1++, S2++, S3++, S4++) {
S->c_x = u0 * S1->c_x + u1 * S2->c_x + u2 * S3->c_x + u3 * S4->c_x;
S->c_y = u0 * S1->c_y + u1 * S2->c_y + u2 * S3->c_y + u3 * S4->c_y;
S->r = u0 * S1->r + u1 * S2->r + u2 * S3->r + u3 * S4->r;
@@ -316,53 +330,46 @@ IFS::Draw(BView* view, const buffer_info* info, int32 frames)
S->A = u0 * S1->A + u1 * S2->A + u2 * S3->A + u3 * S4->A;
S->A2 = u0 * S1->A2 + u1 * S2->A2 + u2 * S3->A2 + u3 * S4->A2;
}
//bigtime_t beforeDraw = system_time();
if (frame == frames - 1)
_DrawFractal(view, info);
//bigtime_t draw = system_time() - beforeDraw;
if (F->Count >= 1000 / F->Speed) {
S = F->Components;
S1 = S + F->Nb_Simi;
S2 = S1 + F->Nb_Simi;
S3 = S2 + F->Nb_Simi;
S4 = S3 + F->Nb_Simi;
S1 = S + F->SimilitudeCount;
S2 = S1 + F->SimilitudeCount;
S3 = S2 + F->SimilitudeCount;
S4 = S3 + F->SimilitudeCount;
for (i = F->Nb_Simi; i; --i, S++, S1++, S2++, S3++, S4++) {
for (i = F->SimilitudeCount; i; --i, S++, S1++, S2++, S3++, S4++) {
S2->c_x = 2.0 * S4->c_x - S3->c_x;
S2->c_y = 2.0 * S4->c_y - S3->c_y;
S2->r = 2.0 * S4->r - S3->r;
S2->r2 = 2.0 * S4->r2 - S3->r2;
S2->A = 2.0 * S4->A - S3->A;
S2->A2 = 2.0 * S4->A2 - S3->A2;
*S1 = *S4;
}
_RandomSimis(F, F->Components + 3 * F->Nb_Simi, F->Nb_Simi);
_RandomSimis(F, F->Components + 4 * F->Nb_Simi, F->Nb_Simi);
_RandomSimilitudes(F, F->Components + 3 * F->SimilitudeCount,
F->SimilitudeCount);
_RandomSimilitudes(F, F->Components + 4 * F->SimilitudeCount,
F->SimilitudeCount);
F->Count = 0;
} else
F->Count++;
// F->Col++;
//bigtime_t finish = (system_time() - now) - draw;
//if (info)
//printf("draw: %lld\nnon-draw: %lld\n\n", draw, finish);
}
}
// SetAdditive
void
IFS::SetAdditive(bool additive)
{
fAdditive = additive;
}
// SetSpeed
void
IFS::SetSpeed(int32 speed)
{
@@ -370,39 +377,45 @@ IFS::SetSpeed(int32 speed)
fRoot->Speed = speed;
}
// Draw
void
IFS::_DrawFractal(BView* view, const buffer_info* info)
{
FRACTAL* F = fRoot;
int i, j;
int32 x, y, xo, yo;
SIMI* Cur, *Simi;
int i;
int j;
int32 x;
int32 y;
int32 xo;
int32 yo;
SIMILITUDE* Current;
SIMILITUDE* Similitude;
for (Cur = F->Components, i = F->Nb_Simi; i; --i, Cur++) {
Cur->Cx = FLOAT_TO_INT(Cur->c_x);
Cur->Cy = FLOAT_TO_INT(Cur->c_y);
for (Current = F->Components, i = F->SimilitudeCount; i; --i, Current++) {
Current->Cx = FLOAT_TO_INT(Current->c_x);
Current->Cy = FLOAT_TO_INT(Current->c_y);
Cur->Ct = FLOAT_TO_INT(cos(Cur->A));
Cur->St = FLOAT_TO_INT(sin(Cur->A));
Cur->Ct2 = FLOAT_TO_INT(cos(Cur->A2));
Cur->St2 = FLOAT_TO_INT(sin(Cur->A2));
Current->Ct = FLOAT_TO_INT(cos(Current->A));
Current->St = FLOAT_TO_INT(sin(Current->A));
Current->Ct2 = FLOAT_TO_INT(cos(Current->A2));
Current->St2 = FLOAT_TO_INT(sin(Current->A2));
Cur->R = FLOAT_TO_INT(Cur->r);
Cur->R2 = FLOAT_TO_INT(Cur->r2);
Current->R = FLOAT_TO_INT(Current->r);
Current->R2 = FLOAT_TO_INT(Current->r2);
}
fCurrentPoint = 0;
fCurrentFractal = F;
fPointBuffer = F->buffer2;
for (Cur = F->Components, i = F->Nb_Simi; i; --i, Cur++) {
xo = Cur->Cx;
yo = Cur->Cy;
for (Simi = F->Components, j = F->Nb_Simi; j; --j, Simi++) {
if (Simi == Cur)
for (Current = F->Components, i = F->SimilitudeCount; i; --i, Current++) {
xo = Current->Cx;
yo = Current->Cy;
for (Similitude = F->Components, j = F->SimilitudeCount; j;
--j, Similitude++) {
if (Similitude == Current)
continue;
transform(Simi, xo, yo, &x, &y);
transform(Similitude, xo, yo, &x, &y);
_Trace(F, x, y);
}
}
@@ -419,9 +432,9 @@ IFS::_DrawFractal(BView* view, const buffer_info* info)
// Erase previous dots from bitmap,
// but only if we're not in BDirectWindow mode,
// since the dots will have been erased already
if (!info) {
if (F->Cur_Pt) {
for (int32 i = 0; i < F->Cur_Pt; i++) {
if (info == NULL) {
if (F->CurrentPoint) {
for (int32 i = 0; i < F->CurrentPoint; i++) {
Point p = F->buffer1[i];
if (p.x >= 0 && p.x < F->Width
&& p.y >= 0 && p.y < F->Height) {
@@ -429,10 +442,13 @@ IFS::_DrawFractal(BView* view, const buffer_info* info)
*(uint32*)&bits[offset] = 0;
if (minX > p.x)
minX = p.x;
if (minY > p.y)
minY = p.y;
if (maxX < p.x)
maxX = p.x;
if (maxY < p.y)
maxY = p.y;
}
@@ -441,7 +457,7 @@ IFS::_DrawFractal(BView* view, const buffer_info* info)
}
// draw the new dots into the bitmap
if (fCurrentPoint) {
if (info) {
if (info != NULL) {
for (int32 i = 0; i < fCurrentPoint; i++) {
Point p = F->buffer2[i];
if (p.x >= 0 && p.x < F->Width
@@ -453,9 +469,8 @@ IFS::_DrawFractal(BView* view, const buffer_info* info)
bits[offset + 1] += 51;
bits[offset + 2] += 51;
}
} else {
} else
*(uint32*)&bits[offset] = 0xffffffff;
}
}
}
} else {
@@ -471,9 +486,9 @@ IFS::_DrawFractal(BView* view, const buffer_info* info)
bits[offset + 1] += 15;
bits[offset + 2] += 15;
}
} else {
} else
*(uint32*)&bits[offset] = 0xffffffff;
}
if (minX > p.x)
minX = p.x;
if (minY > p.y)
@@ -486,102 +501,113 @@ IFS::_DrawFractal(BView* view, const buffer_info* info)
}
}
}
if (info && info->bits) {
if (info != NULL && info->bits != NULL) {
uint8* screenBits = (uint8*)info->bits;
uint32 screenBPR = info->bytesPerRow;
int32 left = info->bounds.left;
int32 top = info->bounds.top;
int32 bpp = info->bits_per_pixel;
screenBits += left * bpp + top * bpr;
int32 screenWidth = info->bounds.right - left;
int32 screenHeight = info->bounds.bottom - top;
//printf("using BDirectWindow (%p %dx%d)\n", this, F->Width, F->Height);
// redraw the previous points on screen
// with the contents of the current bitmap
//
// draw the new points, erasing the bitmap as we go
int32 maxPoints = max_c(F->Cur_Pt, fCurrentPoint);
if (maxPoints > 0) {
// BScreen screen(B_MAIN_SCREEN_ID);
// screen.WaitForRetrace();
int32 maxPoints = max_c(F->CurrentPoint, fCurrentPoint);
if (maxPoints > 0) {
for (int32 i = 0; i < maxPoints; i++) {
// copy previous points (black)
if (i < F->Cur_Pt) {
if (i < F->CurrentPoint) {
Point p = F->buffer1[i];
if (p.x >= 0 && p.x < F->Width && p.x < screenWidth
&& p.y >= 0 && p.y < F->Height && p.y < screenHeight) {
&& p.y >= 0 && p.y < F->Height
&& p.y < screenHeight) {
int32 markOffset = markBPR * p.y + p.x;
if (markBits[markOffset] != fCurrentMarkValue) {
int32 offset = bpr * p.y + p.x * 4;
// copy the pixel to the screen
uint32* src = (uint32*)&bits[offset];
if (bpp == 32) {
int32 screenOffset = screenBPR * p.y + p.x * 4;
int32 screenOffset = screenBPR * p.y
+ p.x * 4;
*(uint32*)&screenBits[screenOffset] = *src;
} else if (bpp == 16) {
int32 screenOffset = screenBPR * p.y + p.x * 2;
int32 screenOffset = screenBPR * p.y
+ p.x * 2;
*(uint16*)&screenBits[screenOffset] =
(uint16)(((bits[offset + 2] & 0xf8) << 8) |
((bits[offset + 1] & 0xfc) << 3) |
(bits[offset] >> 3));
(uint16)(((bits[offset + 2] & 0xf8)
<< 8)
| ((bits[offset + 1] & 0xfc) << 3)
| (bits[offset] >> 3));
} else if (bpp == 15) {
int32 screenOffset = screenBPR * p.y + p.x * 2;
int32 screenOffset = screenBPR * p.y
+ p.x * 2;
*(uint16*)&screenBits[screenOffset] =
(uint16)(((bits[offset + 2] & 0xf8) << 7) |
((bits[offset + 1] & 0xf8) << 2) |
(bits[offset] >> 3));
(uint16)(((bits[offset + 2] & 0xf8)
<< 7)
| ((bits[offset + 1] & 0xf8) << 2)
| (bits[offset] >> 3));
} else if (bpp == 8) {
int32 screenOffset = screenBPR * p.y + p.x;
screenBits[screenOffset] = bits[offset];
}
*src = 0;
markBits[markOffset] = fCurrentMarkValue;
} // else it means the pixel has been copied already
}
// else it means the pixel has been copied already
}
}
// copy current points (white) and erase them from the bitmap
// copy current points (white) and erase them from the
// bitmap
if (i < fCurrentPoint) {
Point p = F->buffer2[i];
if (p.x >= 0 && p.x < F->Width && p.x < screenWidth
&& p.y >= 0 && p.y < F->Height && p.y < screenHeight) {
&& p.y >= 0 && p.y < F->Height
&& p.y < screenHeight) {
int32 markOffset = markBPR * p.y + p.x;
int32 offset = bpr * p.y + p.x * 4;
// copy the pixel to the screen
uint32* src = (uint32*)&bits[offset];
if (markBits[markOffset] != fCurrentMarkValue) {
if (bpp == 32) {
int32 screenOffset = screenBPR * p.y + p.x * 4;
int32 screenOffset = screenBPR * p.y
+ p.x * 4;
*(uint32*)&screenBits[screenOffset] = *src;
} else if (bpp == 16) {
int32 screenOffset = screenBPR * p.y + p.x * 2;
int32 screenOffset = screenBPR * p.y
+ p.x * 2;
*(uint16*)&screenBits[screenOffset] =
(uint16)(((bits[offset + 2] & 0xf8) << 8) |
((bits[offset + 1] & 0xfc) << 3) |
(bits[offset] >> 3));
(uint16)(((bits[offset + 2] & 0xf8)
<< 8)
| ((bits[offset + 1] & 0xfc) << 3)
| (bits[offset] >> 3));
} else if (bpp == 15) {
int32 screenOffset = screenBPR * p.y + p.x * 2;
int32 screenOffset = screenBPR * p.y
+ p.x * 2;
*(uint16*)&screenBits[screenOffset] =
(uint16)(((bits[offset + 2] & 0xf8) << 7) |
((bits[offset + 1] & 0xf8) << 2) |
(bits[offset] >> 3));
(uint16)(((bits[offset + 2] & 0xf8)
<< 7)
| ((bits[offset + 1] & 0xf8) << 2)
| (bits[offset] >> 3));
} else if (bpp == 1) {
int32 screenOffset = screenBPR * p.y + p.x;
screenBits[screenOffset] = bits[offset];
}
markBits[markOffset] = fCurrentMarkValue;
} // else it means the pixel has been copied already
}
// else it means the pixel has been copied already
*src = 0;
}
}
}
}
} else {
//printf("using BView (%p %dx%d)\n", this, F->Width, F->Height);
// if not in BDirectWindow mode, draw the bitmap
BRect b(minX, minY, maxX, maxY);
view->DrawBitmapAsync(F->bitmap, b, b);
@@ -589,28 +615,28 @@ IFS::_DrawFractal(BView* view, const buffer_info* info)
}
// flip buffers
F->Cur_Pt = fCurrentPoint;
F->CurrentPoint = fCurrentPoint;
fPointBuffer = F->buffer1;
F->buffer1 = F->buffer2;
F->buffer2 = fPointBuffer;
if (fCurrentMarkValue == 255)
fCurrentMarkValue = 0;
else
fCurrentMarkValue++;
}
// _Trace
void
IFS::_Trace(FRACTAL* F, int32 xo, int32 yo)
{
int32 x, y, i;
SIMI* Cur;
int32 x;
int32 y;
SIMILITUDE* Current;
Cur = fCurrentFractal->Components;
for (i = fCurrentFractal->Nb_Simi; i; --i, Cur++) {
transform(Cur, xo, yo, &x, &y);
// fPointBuffer->x = F->Lx + (x * F->Lx / (UNIT * 2));
// fPointBuffer->y = F->Ly - (y * F->Ly / (UNIT * 2));
Current = fCurrentFractal->Components;
for (int32 i = fCurrentFractal->SimilitudeCount; i; --i, Current++) {
transform(Current, xo, yo, &x, &y);
fPointBuffer->x = (UNIT * 2 + x) * F->Lx / (UNIT * 2);
fPointBuffer->y = (UNIT * 2 - y) * F->Ly / (UNIT * 2);
fPointBuffer++;
@@ -624,36 +650,37 @@ IFS::_Trace(FRACTAL* F, int32 xo, int32 yo)
}
}
// _RandomSimis
void
IFS::_RandomSimis(FRACTAL* f, SIMI* cur, int i) const
IFS::_RandomSimilitudes(FRACTAL* fractal, SIMILITUDE* current, int i) const
{
while (i--) {
cur->c_x = gauss_rand(0.0, .8, 4.0);
cur->c_y = gauss_rand(0.0, .8, 4.0);
cur->r = gauss_rand(f->r_mean, f->dr_mean, 3.0);
cur->r2 = half_gauss_rand(0.0,f->dr2_mean, 2.0);
cur->A = gauss_rand(0.0, 360.0, 4.0) * (M_PI / 180.0);
cur->A2 = gauss_rand(0.0, 360.0, 4.0) * (M_PI / 180.0);
cur++;
while (i-- > 0) {
current->c_x = gauss_rand(0.0, .8, 4.0);
current->c_y = gauss_rand(0.0, .8, 4.0);
current->r = gauss_rand(fractal->r_mean, fractal->dr_mean, 3.0);
current->r2 = half_gauss_rand(0.0,fractal->dr2_mean, 2.0);
current->A = gauss_rand(0.0, 360.0, 4.0) * (M_PI / 180.0);
current->A2 = gauss_rand(0.0, 360.0, 4.0) * (M_PI / 180.0);
current++;
}
}
// _FreeBuffers
void
IFS::_FreeBuffers(FRACTAL *f)
IFS::_FreeBuffers(FRACTAL* f)
{
if (f->buffer1) {
free((void*) f->buffer1);
free((void*)f->buffer1);
f->buffer1 = (Point*)NULL;
}
if (f->buffer2) {
free((void*) f->buffer2);
free((void*)f->buffer2);
f->buffer2 = (Point*)NULL;
}
}
// _FreeIFS
void
IFS::_FreeIFS(FRACTAL* f)
{
@@ -663,6 +690,3 @@ IFS::_FreeIFS(FRACTAL* f)
delete f->markBitmap;
f->markBitmap = NULL;
}
+56 -33
View File
@@ -1,20 +1,28 @@
// Polygon.h
/*
* Copyright (c) 1997 by Massimino Pascal <[email protected]>
* Copyright 2006-2014, Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus, [email protected]
* Massimino Pascal, [email protected]
* John Scipione, [email protected]
*/
#ifndef IFS_H
#define IFS_H
#include <Screen.h>
#include <Point.h>
#include <Rect.h>
#include <Region.h>
#include <View.h>
typedef struct Similitude_Struct SIMI;
typedef struct Fractal_Struct FRACTAL;
#define FIX 12
#define UNIT ( 1<<FIX )
#define MAX_SIMI 6
#define MAX_SIMILITUDE 6
// settings for a PC 120Mhz...
#define MAX_DEPTH_2 10
@@ -22,6 +30,7 @@ typedef struct Fractal_Struct FRACTAL;
#define MAX_DEPTH_4 4
#define MAX_DEPTH_5 3
struct buffer_info {
void* bits;
uint32 bytesPerRow;
@@ -35,32 +44,48 @@ struct Point {
int32 y;
};
struct Similitude_Struct {
typedef struct Similitude {
float c_x;
float c_y;
float r;
float r2;
float A;
float A2;
int32 Ct;
int32 St;
int32 Ct2;
int32 St2;
int32 Cx;
int32 Cy;
int32 R;
int32 R2;
} SIMILITUDE;
float c_x, c_y;
float r, r2, A, A2;
int32 Ct, St, Ct2, St2;
int32 Cx, Cy;
int32 R, R2;
};
struct Fractal_Struct {
int Nb_Simi;
SIMI Components[5 * MAX_SIMI];
int Depth, Col;
int Count, Speed;
int Width, Height, Lx, Ly;
float r_mean, dr_mean, dr2_mean;
int Cur_Pt, Max_Pt;
typedef struct Fractal {
int SimilitudeCount;
SIMILITUDE Components[5 * MAX_SIMILITUDE];
int Depth;
int Col;
int Count;
int Speed;
int Width;
int Height;
int Lx;
int Ly;
float r_mean;
float dr_mean;
float dr2_mean;
int CurrentPoint;
int MaxPoint;
Point* buffer1;
Point* buffer2;
BBitmap* bitmap;
BBitmap* markBitmap;
};
} FRACTAL;
class IFS {
public:
public:
IFS(BRect bounds);
virtual ~IFS();
@@ -70,18 +95,15 @@ class IFS {
void SetAdditive(bool additive);
void SetSpeed(int32 speed);
private:
private:
void _DrawFractal(BView* view,
const buffer_info* info);
void _Trace(FRACTAL* F,
int32 xo, int32 yo);
void _RandomSimis(FRACTAL* f,
SIMI* cur,
int i) const;
const buffer_info* info);
void _Trace(FRACTAL* F, int32 xo, int32 yo);
void _RandomSimilitudes(FRACTAL* f, SIMILITUDE* cur,
int i) const;
void _FreeBuffers(FRACTAL* f);
void _FreeIFS(FRACTAL* f);
FRACTAL* fRoot;
FRACTAL* fCurrentFractal;
Point* fPointBuffer;
@@ -91,4 +113,5 @@ class IFS {
uint8 fCurrentMarkValue;
};
#endif // ABOUT_VPOLYGON_HIEW_H
#endif // IFS_H
+73 -72
View File
@@ -1,6 +1,14 @@
// IFSSaver.cpp
/*
* Copyright (c) 1997 by Massimino Pascal <[email protected]>
* Copyright 2006-2014, Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus, [email protected]
* Massimino Pascal, [email protected]
* John Scipione, [email protected]
*/
#include <math.h>
#include <stdio.h>
@@ -11,56 +19,63 @@
#include "IFSSaver.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Screensaver IFS"
enum {
MSG_TOGGLE_ADDITIVE = 'tgad',
MSG_SET_SPEED = 'stsp',
};
static const uint32 kMsgToggleAdditive = 'tgad';
static const uint32 kMsgSetSpeed = 'stsp';
// #pragma mark - Instantiation function
// MAIN INSTANTIATION FUNCTION
extern "C" _EXPORT BScreenSaver*
instantiate_screen_saver(BMessage *message, image_id image)
{
return new IFSSaver(message, image);
}
// constructor
IFSSaver::IFSSaver(BMessage *message, image_id id)
: BScreenSaver(message, id),
BHandler("IFS Saver"),
fIFS(NULL),
fIsPreview(false),
fBounds(0.0, 0.0, -1.0, -1.0),
fLastDrawnFrame(0),
fAdditive(false),
fSpeed(6)
// #pragma mark - IFSSaver
IFSSaver::IFSSaver(BMessage* message, image_id id)
:
BScreenSaver(message, id),
BHandler("IFS Saver"),
fIFS(NULL),
fIsPreview(false),
fBounds(0.0, 0.0, -1.0, -1.0),
fLastDrawnFrame(0),
fAdditive(false),
fSpeed(6)
{
fDirectInfo.bits = NULL;
fDirectInfo.bytesPerRow = 0;
if (message) {
if (message->FindBool("IFS additive", &fAdditive) < B_OK)
fAdditive = false;
if (message->FindInt32("IFS speed", &fSpeed) < B_OK)
fSpeed = 6;
}
if (message == NULL)
return;
if (message->FindBool("IFS additive", &fAdditive) != B_OK)
fAdditive = false;
if (message->FindInt32("IFS speed", &fSpeed) != B_OK)
fSpeed = 6;
}
// destructor
IFSSaver::~IFSSaver()
{
if (Looper()) {
if (Looper() != NULL) {
Looper()->RemoveHandler(this);
}
_Cleanup();
}
// StartConfig
void
IFSSaver::StartConfig(BView *view)
IFSSaver::StartConfig(BView* view)
{
BRect bounds = view->Bounds();
bounds.InsetBy(10.0, 10.0);
@@ -68,9 +83,8 @@ IFSSaver::StartConfig(BView *view)
// the additive check box
fAdditiveCB = new BCheckBox(frame, "additive setting",
B_TRANSLATE("Render dots additive"),
new BMessage(MSG_TOGGLE_ADDITIVE),
B_FOLLOW_LEFT | B_FOLLOW_BOTTOM);
B_TRANSLATE("Render dots additive"), new BMessage(kMsgToggleAdditive),
B_FOLLOW_LEFT | B_FOLLOW_BOTTOM);
fAdditiveCB->SetValue(fAdditive);
@@ -82,10 +96,8 @@ IFSSaver::StartConfig(BView *view)
// the additive check box
fSpeedS = new BSlider(frame, "speed setting",
B_TRANSLATE("Morphing speed:"),
new BMessage(MSG_SET_SPEED),
1, 12, B_BLOCK_THUMB,
B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM);
B_TRANSLATE("Morphing speed:"), new BMessage(kMsgSetSpeed), 1, 12,
B_BLOCK_THUMB, B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM);
fSpeedS->SetValue(fSpeed);
fSpeedS->SetHashMarks(B_HASH_MARKS_BOTTOM);
@@ -101,13 +113,13 @@ IFSSaver::StartConfig(BView *view)
BRect textRect = bounds;
textRect.OffsetTo(0.0, 0.0);
BTextView* textView = new BTextView(bounds, B_EMPTY_STRING, textRect,
B_FOLLOW_ALL, B_WILL_DRAW);
B_FOLLOW_ALL, B_WILL_DRAW);
textView->SetViewColor(view->ViewColor());
BString aboutScreenSaver(B_TRANSLATE("%screenSaverName%\n\n"
""B_UTF8_COPYRIGHT" 1997 Massimino Pascal\n\n"
"xscreensaver port by Stephan Aßmus\n"
"<[email protected]>"));
""B_UTF8_COPYRIGHT" 1997 Massimino Pascal\n\n"
"xscreensaver port by Stephan Aßmus\n"
"<[email protected]>"));
BString screenSaverName(B_TRANSLATE("Iterated Function System"));
aboutScreenSaver.ReplaceFirst("%screenSaverName%", screenSaverName);
@@ -115,7 +127,6 @@ IFSSaver::StartConfig(BView *view)
textView->SetStylable(true);
textView->SetFontAndColor(0, screenSaverName.Length(), be_bold_font);
// textView->SetFontAndColor(25, 255, be_plain_font);
textView->MakeEditable(false);
@@ -129,9 +140,9 @@ IFSSaver::StartConfig(BView *view)
fSpeedS->SetTarget(this);
}
// StartSaver
status_t
IFSSaver::StartSaver(BView *v, bool preview)
IFSSaver::StartSaver(BView* view, bool preview)
{
display_mode mode;
BScreen screen(B_MAIN_SCREEN_ID);
@@ -139,11 +150,10 @@ IFSSaver::StartSaver(BView *v, bool preview)
float totalSize = mode.timing.h_total * mode.timing.v_total;
float fps = mode.timing.pixel_clock * 1000.0 / totalSize;
//printf("ticks per frame: %lldµs\n", (int64)floor(1000000.0 / fps + 0.5));
SetTickSize((int64)floor(1000000.0 / fps + 0.5));
fIsPreview = preview;
fBounds = v->Bounds();
fBounds = view->Bounds();
_Init(fBounds);
fIFS->SetAdditive(fIsPreview || fAdditive);
@@ -152,53 +162,44 @@ IFSSaver::StartSaver(BView *v, bool preview)
return B_OK;
}
// StopSaver
void
IFSSaver::StopSaver()
{
_Cleanup();
}
// DirectConnected
void
IFSSaver::DirectConnected(direct_buffer_info* info)
{
//printf("IFSSaver::DirectConnected()\n");
int32 request = info->buffer_state & B_DIRECT_MODE_MASK;
switch (request) {
case B_DIRECT_START:
//printf("B_DIRECT_START\n");
//printf(" bits_per_pixel: %ld\n", info->bits_per_pixel);
//printf(" bytes_per_row: %ld\n", info->bytes_per_row);
//printf(" pixel_format: %d\n", info->pixel_format);
//printf(" window_bounds: (%ld, %ld, %ld, %ld)\n",
// info->window_bounds.left, info->window_bounds.top,
// info->window_bounds.right, info->window_bounds.bottom);
fDirectInfo.bits = info->bits;
fDirectInfo.bytesPerRow = info->bytes_per_row;
fDirectInfo.bits_per_pixel = info->bits_per_pixel;
fDirectInfo.format = info->pixel_format;
fDirectInfo.bounds = info->window_bounds;
break;
case B_DIRECT_STOP:
fDirectInfo.bits = NULL;
break;
default:
break;
}
//printf("bits: %p\n", fDirectInfo.bits);
}
// Draw
void
IFSSaver::Draw(BView *view, int32 frame)
IFSSaver::Draw(BView* view, int32 frame)
{
//printf("IFSSaver::Draw(%ld) (%ldx%ld)\n", frame, view->Bounds().IntegerWidth() + 1, view->Bounds().IntegerHeight() + 1);
if (frame == 0) {
fLastDrawnFrame = -1;
view->SetHighColor(0, 0, 0);
view->FillRect(view->Bounds());
}
int32 frames = frame - fLastDrawnFrame;
if ((fIsPreview || fDirectInfo.bits == NULL) && fLocker.Lock()) {
fIFS->Draw(view, NULL, frames);
@@ -208,23 +209,22 @@ IFSSaver::Draw(BView *view, int32 frame)
}
}
// DirectDraw
void
IFSSaver::DirectDraw(int32 frame)
{
//bigtime_t now = system_time();
//printf("IFSSaver::DirectDraw(%ld)\n", frame);
if (frame == 0)
fLastDrawnFrame = -1;
int32 frames = frame - fLastDrawnFrame;
if (fDirectInfo.bits) {
fIFS->Draw(NULL, &fDirectInfo, frames);
//printf("DirectDraw(): %lldµs\n", system_time() - now);
fLastDrawnFrame = frame;
}
}
// SaveState
status_t
IFSSaver::SaveState(BMessage* into) const
{
@@ -237,31 +237,33 @@ IFSSaver::SaveState(BMessage* into) const
return ret;
}
// MessageReceived
void
IFSSaver::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_TOGGLE_ADDITIVE:
case kMsgToggleAdditive:
if (fLocker.Lock() && fIFS) {
fAdditive = fAdditiveCB->Value() == B_CONTROL_ON;
fIFS->SetAdditive(fAdditive || fIsPreview);
fLocker.Unlock();
}
break;
case MSG_SET_SPEED:
case kMsgSetSpeed:
if (fLocker.Lock() && fIFS) {
fSpeed = fSpeedS->Value();
fIFS->SetSpeed(fSpeed);
fLocker.Unlock();
}
break;
default:
BHandler::MessageReceived(message);
}
}
// _Init
void
IFSSaver::_Init(BRect bounds)
{
@@ -272,7 +274,7 @@ IFSSaver::_Init(BRect bounds)
}
}
// _Cleanup
void
IFSSaver::_Cleanup()
{
@@ -282,4 +284,3 @@ IFSSaver::_Cleanup()
fLocker.Unlock();
}
}
+15 -18
View File
@@ -1,5 +1,3 @@
// IFSSaver.h
#ifndef IFS_SAVER_H
#define IFS_SAVER_H
@@ -10,16 +8,17 @@
#include "IFS.h"
class BCheckBox;
class BSlider;
class IFSSaver : public BScreenSaver, public BHandler {
public:
public:
IFSSaver(BMessage *message,
image_id image);
virtual ~IFSSaver();
// BScreenSaver
virtual void StartConfig(BView *view);
virtual status_t StartSaver(BView *view, bool preview);
virtual void StopSaver();
@@ -30,30 +29,28 @@ class IFSSaver : public BScreenSaver, public BHandler {
virtual status_t SaveState(BMessage* into) const;
// BHandler
virtual void MessageReceived(BMessage* message);
private:
private:
void _Init(BRect bounds);
void _Cleanup();
IFS* fIFS;
IFS* fIFS;
bool fIsPreview;
BRect fBounds;
bool fIsPreview;
BRect fBounds;
BLocker fLocker;
BLocker fLocker;
BCheckBox* fAdditiveCB;
BSlider* fSpeedS;
BCheckBox* fAdditiveCB;
BSlider* fSpeedS;
buffer_info fDirectInfo;
int32 fLastDrawnFrame;
buffer_info fDirectInfo;
int32 fLastDrawnFrame;
// config settings
bool fAdditive;
int32 fSpeed;
bool fAdditive;
int32 fSpeed;
};
#endif // IFS_SAVER_H
#endif // IFS_SAVER_H