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