Gravity: Live update settings and cleanup

Now that this screensaver shows a preview, update the settings live
so that the display updates immediately.

Since this didn't work before this screensaver took a shortcut and only
updated the settings when the saver started.

Some other changes include:
* Update copyright header, add myself to the authors list.
* Convert Particle from using a BList to a BObjectList and remove casts.
* Use switch statement to set particle color that has a default case for blue.
* Initialize member variables to a default value in the constructor.
* Remove Constants.h, the remaining constants are in ConfigView.cpp
* Convert deprecated GroupLayoutBuilder to a LayoutBuilder template instance
* Convert GravitySource from a class to a struct since all members are public.
* Simplify realCount calculation to just a single left shift.
* A bunch of style fixes
  - lots of whitespace fixes
  - rename rect to frame in GravityView and ConfigView
  - reorder the frame parameter first in GravityView and ConfigView
  - curly brace goes on same line as class or struct declaration

This turned out to be a bigger change than I originally intended to make.
This commit is contained in:
John Scipione 2014-02-23 03:50:15 -05:00
parent 60f8c91355
commit 8e727810a7
11 changed files with 332 additions and 241 deletions

View File

@ -1,47 +1,48 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT license.
*
* Authors:
* Tri-Edge AI
* John Scipione, jscipione@gmail.com
*/
#include "ConfigView.h"
#include "Constants.h"
#include "Gravity.h"
#include <GroupLayoutBuilder.h>
#include <LayoutBuilder.h>
#include <ListView.h>
#include <ScrollView.h>
#include <Slider.h>
#include <StringView.h>
#include <View.h>
#include "Gravity.h"
ConfigView::ConfigView(Gravity* parent, BRect rect)
static const int32 kMsgSize = 'size';
static const int32 kMsgShade = 'shad';
ConfigView::ConfigView(BRect frame, Gravity* parent)
:
BView(rect, B_EMPTY_STRING, B_FOLLOW_ALL_SIDES, B_WILL_DRAW)
BView(frame, B_EMPTY_STRING, B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
fParent(parent),
fTitleString(new BStringView(B_EMPTY_STRING, "OpenGL Gravity Effect")),
fAuthorString(new BStringView(B_EMPTY_STRING, "by Tri-Edge AI")),
fCountSlider(new BSlider(B_EMPTY_STRING, "Particle Count: ",
new BMessage(kMsgSize), 0, 4, B_HORIZONTAL, B_BLOCK_THUMB,
B_NAVIGABLE | B_WILL_DRAW)),
fShadeString(new BStringView(B_EMPTY_STRING, "Shade: ")),
fShadeList(new BListView(B_EMPTY_STRING, B_SINGLE_SELECTION_LIST,
B_WILL_DRAW | B_NAVIGABLE))
{
fParent = parent;
SetLayout(new BGroupLayout(B_HORIZONTAL));
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fTitleString = new BStringView(RECT_0, B_EMPTY_STRING,
"OpenGL Gravity Effect", B_FOLLOW_LEFT);
fAuthorString = new BStringView(RECT_0, B_EMPTY_STRING,
"by Tri-Edge AI", B_FOLLOW_LEFT);
fCountSlider = new BSlider(RECT_0, B_EMPTY_STRING, "Particle Count: ",
new BMessage(MSG_COUNT), 0, 4, B_BLOCK_THUMB);
fShadeString = new BStringView(RECT_0, B_EMPTY_STRING, "Shade: ",
B_FOLLOW_LEFT);
fShadeList = new BListView(RECT_0, B_EMPTY_STRING, B_SINGLE_SELECTION_LIST,
B_FOLLOW_ALL);
fShadeList->SetSelectionMessage(new BMessage(MSG_SHADE));
fShadeList->SetSelectionMessage(new BMessage(kMsgShade));
fShadeList->AddItem(new BStringItem("Red"));
fShadeList->AddItem(new BStringItem("Green"));
@ -59,22 +60,18 @@ ConfigView::ConfigView(Gravity* parent, BRect rect)
fCountSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
fCountSlider->SetHashMarkCount(5);
fCountSlider->SetLimitLabels("128", "2048");
fCountSlider->SetModificationMessage(new BMessage(kMsgSize));
fCountSlider->SetValue(parent->Config.ParticleCount);
AddChild(BGroupLayoutBuilder(B_VERTICAL, B_USE_DEFAULT_SPACING)
.Add(BGroupLayoutBuilder(B_VERTICAL, 0)
.Add(fTitleString)
.Add(fAuthorString)
)
.Add(fShadeString)
.Add(fShadeScroll)
.Add(fCountSlider)
.SetInsets(B_USE_DEFAULT_SPACING,
B_USE_DEFAULT_SPACING,
B_USE_DEFAULT_SPACING,
B_USE_DEFAULT_SPACING)
);
AddChild(BLayoutBuilder::Group<>(B_VERTICAL, B_USE_DEFAULT_SPACING)
.AddGroup(B_VERTICAL, 0)
.Add(fTitleString)
.Add(fAuthorString)
.End()
.Add(fShadeString)
.Add(fShadeScroll)
.Add(fCountSlider)
.SetInsets(B_USE_DEFAULT_SPACING));
}
@ -90,11 +87,11 @@ void
ConfigView::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_COUNT:
case kMsgSize:
fParent->Config.ParticleCount = fCountSlider->Value();
break;
case MSG_SHADE:
case kMsgShade:
fParent->Config.ShadeID = fShadeList->CurrentSelection();
break;

View File

@ -1,6 +1,12 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT license.
*
* Authors:
* Tri-Edge AI
* John Scipione, jscipione@gmail.com
*/
#ifndef GRAVITY_CONFIG_VIEW_H
#define GRAVITY_CONFIG_VIEW_H
@ -8,6 +14,7 @@
#include <View.h>
class Gravity;
class BListView;
@ -15,27 +22,26 @@ class BScrollView;
class BSlider;
class BStringView;
class ConfigView : public BView
{
public:
ConfigView(Gravity* parent, BRect rect);
void AttachedToWindow();
void MessageReceived(BMessage* message);
class ConfigView : public BView {
public:
ConfigView(BRect frame, Gravity* parent);
void AttachedToWindow();
void MessageReceived(BMessage* message);
private:
Gravity* fParent;
Gravity* fParent;
BStringView* fTitleString;
BStringView* fAuthorString;
BStringView* fTitleString;
BStringView* fAuthorString;
BListView* fShadeList;
BStringView* fShadeString;
BScrollView* fShadeScroll;
BSlider* fCountSlider;
BSlider* fCountSlider;
BStringView* fShadeString;
BListView* fShadeList;
BScrollView* fShadeScroll;
};
#endif
#endif // GRAVITY_CONFIG_VIEW_H

View File

@ -1,15 +0,0 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef CONSTANTS_H
#define CONSTANTS_H
#define MSG_SHADE 'm000'
#define MSG_COUNT 'm001'
#define RECT_0 BRect(0, 0, 0, 0)
#endif

View File

@ -1,6 +1,12 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT license.
*
* Authors:
* Tri-Edge AI
* John Scipione, jscipione@gmail.com
*/
@ -14,9 +20,12 @@
#include <stdlib.h>
Gravity::Gravity(BMessage* prefs, image_id imageID)
:
BScreenSaver(prefs, imageID)
BScreenSaver(prefs, imageID),
fGravityView(NULL),
fConfigView(NULL)
{
srand(time(NULL));
@ -45,7 +54,8 @@ Gravity::SaveState(BMessage* prefs) const
void
Gravity::StartConfig(BView* view)
{
view->AddChild(new ConfigView(this, view->Bounds()));
fConfigView = new ConfigView(view->Bounds(), this);
view->AddChild(fConfigView);
}
@ -54,8 +64,8 @@ Gravity::StartSaver(BView* view, bool preview)
{
SetTickSize((1000 / 20) * 1000);
// ~20 FPS
fView = new GravityView(this, view->Bounds());
view->AddChild(fView);
fGravityView = new GravityView(view->Bounds(), this);
view->AddChild(fGravityView);
return B_OK;
}
@ -64,17 +74,17 @@ Gravity::StartSaver(BView* view, bool preview)
void
Gravity::StopSaver()
{
if (fView != NULL)
fView->EnableDirectMode(false);
if (fGravityView != NULL)
fGravityView->EnableDirectMode(false);
}
void
Gravity::DirectConnected(direct_buffer_info* info)
{
if (fView != NULL) {
fView->DirectConnected(info);
fView->EnableDirectMode(true);
if (fGravityView != NULL) {
fGravityView->DirectConnected(info);
fGravityView->EnableDirectMode(true);
}
}
@ -82,5 +92,5 @@ Gravity::DirectConnected(direct_buffer_info* info)
void
Gravity::DirectDraw(int32 frame)
{
fView->DirectDraw();
fGravityView->DirectDraw();
}

View File

@ -1,6 +1,12 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT license.
*
* Authors:
* Tri-Edge AI
* John Scipione, jscipione@gmail.com
*/
#ifndef GRAVITY_SCREEN_SAVER_H
#define GRAVITY_SCREEN_SAVER_H
@ -8,36 +14,37 @@
#include <ScreenSaver.h>
class GravityView;
class BMessage;
class BView;
class ConfigView;
class GravityView;
class Gravity : public BScreenSaver
{
class Gravity : public BScreenSaver {
public:
struct
{
struct {
int32 ShadeID;
int32 ParticleCount;
} Config;
Gravity(BMessage* prefs, image_id imageID);
Gravity(BMessage* prefs, image_id imageID);
status_t SaveState(BMessage* prefs) const;
status_t SaveState(BMessage* prefs) const;
void StartConfig(BView* view);
void StartConfig(BView* view);
status_t StartSaver(BView* view, bool preview);
void StopSaver();
status_t StartSaver(BView* view, bool preview);
void StopSaver();
void DirectConnected(direct_buffer_info* info);
void DirectDraw(int32 frame);
void DirectConnected(direct_buffer_info* info);
void DirectDraw(int32 frame);
private:
GravityView* fView;
GravityView* fGravityView;
ConfigView* fConfigView;
};
#endif
#endif // GRAVITY_SCREEN_SAVER_H

View File

@ -1,9 +1,16 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT license.
*
* Authors:
* Tri-Edge AI
* John Scipione, jscipione@gmail.com
*/
#include "GravitySource.h"
#include "Particle.h"
@ -59,7 +66,7 @@ GravitySource::Tick()
}
for (int32 i = 0; i < Particle::list->CountItems(); i++) {
Particle* p = (Particle*)Particle::list->ItemAt(i);
Particle* p = Particle::list->ItemAt(i);
dx = x - p->x;
dy = y - p->y;
dz = z - p->z;

View File

@ -1,34 +1,36 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT license.
*
* Authors:
* Tri-Edge AI
* John Scipione, jscipione@gmail.com
*/
#ifndef GRAVITY_SOURCE_H
#define GRAVITY_SOURCE_H
class GravitySource
{
public:
float x;
float y;
float z;
struct GravitySource {
float x;
float y;
float z;
float r;
float vx;
float vy;
float vz;
float vx;
float vy;
float vz;
float tx;
float ty;
float tz;
float tx;
float ty;
float tz;
GravitySource();
void Tick();
private:
void Tick();
};
#endif
#endif // GRAVITY_SOURCE_H

View File

@ -1,6 +1,12 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT license.
*
* Authors:
* Tri-Edge AI
* John Scipione, jscipione@gmail.com
*/
@ -13,29 +19,16 @@
#include <GL/glu.h>
GravityView::GravityView(Gravity* parent, BRect rect)
GravityView::GravityView(BRect frame, Gravity* parent)
:
BGLView(rect, B_EMPTY_STRING, B_FOLLOW_NONE, 0,
BGL_RGB | BGL_DEPTH | BGL_DOUBLE)
BGLView(frame, B_EMPTY_STRING, B_FOLLOW_NONE, 0,
BGL_RGB | BGL_DEPTH | BGL_DOUBLE),
fParent(parent),
fGravitySource(new GravitySource()),
fSize(128 << parent->Config.ParticleCount),
fShade(parent->Config.ShadeID)
{
fParent = parent;
int realCount;
if (parent->Config.ParticleCount == 0)
realCount = 128;
else if (parent->Config.ParticleCount == 1)
realCount = 256;
else if (parent->Config.ParticleCount == 2)
realCount = 512;
else if (parent->Config.ParticleCount == 3)
realCount = 1024;
else if (parent->Config.ParticleCount == 4)
realCount = 2048;
else
realCount = 128;
Particle::Initialize(realCount, parent->Config.ShadeID);
Particle::Initialize(fSize, fShade);
LockGL();
@ -46,7 +39,7 @@ GravityView::GravityView(Gravity* parent, BRect rect)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, rect.Width() / rect.Height(), 2.0f, 20000.0f);
gluPerspective(45.0f, frame.Width() / frame.Height(), 2.0f, 20000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
@ -55,15 +48,13 @@ GravityView::GravityView(Gravity* parent, BRect rect)
glDepthMask(GL_FALSE);
UnlockGL();
fGravSource = new GravitySource();
}
GravityView::~GravityView()
{
Particle::Terminate();
delete fGravSource;
delete fGravitySource;
}
@ -79,13 +70,29 @@ GravityView::AttachedToWindow()
void
GravityView::DirectDraw()
{
int32 size = 128 << fParent->Config.ParticleCount;
int32 shade = fParent->Config.ShadeID;
// resize particle list if needed
if (size > fSize)
Particle::AddParticles(size, shade);
else if (size < fSize)
Particle::RemoveParticles(size, shade);
// recolor particles if needed
if (shade != fShade)
Particle::ColorParticles(size, shade);
fSize = size;
fShade = shade;
LockGL();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Particle::Tick();
fGravSource->Tick();
fGravitySource->Tick();
SwapBuffers();

View File

@ -1,6 +1,12 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT license.
*
* Authors:
* Tri-Edge AI
* John Scipione, jscipione@gmail.com
*/
#ifndef GRAVITY_VIEW_H
#define GRAVITY_VIEW_H
@ -12,21 +18,23 @@
class Gravity;
class GravitySource;
class GravityView : public BGLView
{
class GravityView : public BGLView {
public:
GravityView(Gravity* parent, BRect rect);
~GravityView();
GravityView(BRect frame, Gravity* parent);
~GravityView();
void AttachedToWindow();
void AttachedToWindow();
void DirectDraw();
void DirectDraw();
private:
Gravity* fParent;
GravitySource* fGravSource;
Gravity* fParent;
GravitySource* fGravitySource;
int32 fSize;
int32 fShade;
};
#endif
#endif // GRAVITY_VIEW_H

View File

@ -1,101 +1,81 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT license.
*
* Authors:
* Tri-Edge AI
* John Scipione, jscipione@gmail.com
*/
#include "Particle.h"
#include <List.h>
#include <stdlib.h>
#define frand() ((float)rand() / (float)RAND_MAX)
BList* Particle::list;
BObjectList<Particle>* Particle::list;
void
/*static*/ void
Particle::Initialize(int32 size, int32 shade)
{
list = new BList();
list = new BObjectList<Particle>(2048);
for (int32 i = 0; i < size; i++) {
Particle* p = new Particle();
p->x = frand() * 30.0f - 15.0f;
p->y = frand() * 30.0f - 15.0f;
p->z = frand() * 5.0f;
p->r = frand() * 360.0f;
p->vx = frand() - 0.5f;
p->vy = frand() - 0.5f;
p->vz = frand() - 0.5f;
p->vr = (frand() - 0.5f) * 180.0f;
if (shade == 0) {
// Red
p->red = 0.1f + frand() * 0.2f;
p->green = 0.0f;
p->blue = frand() * 0.05f;
} else if (shade == 1) {
// Green
p->red = 0;
p->green = 0.1f + frand() * 0.2f;
p->blue = frand() * 0.05f;
} else if (shade == 2) {
// Blue
p->red = 0;
p->green = frand() * 0.05f;
p->blue = 0.1f + frand() * 0.2f;
} else if (shade == 3) {
// Orange
p->red = 0.1f + frand() * 0.1f;
p->green = 0.05f + frand() * 0.1f;
p->blue = 0.0f;
} else if (shade == 4) {
// Purple
p->red = 0.1f + frand() * 0.2f;
p->green = 0.0f;
p->blue = 0.1f + frand() * 0.2f;
} else if (shade == 5) {
// White
p->red = p->green = p->blue = 0.1f + frand() * 0.2f;
} else if (shade == 6) {
// Rainbow
p->red = 0.1f + frand() * 0.2f;
p->green = 0.1f + frand() * 0.2f;
p->blue = 0.1f + frand() * 0.2f;
} else {
// Man, this shouldn't even happen.. Blue.
p->red = 0;
p->green = frand() * 0.05f;
p->blue = 0.1f + frand() * 0.2f;
}
Particle::_FillParticle(p, size, shade);
list->AddItem(p);
}
}
void
/*static*/ void
Particle::AddParticles(int32 size, int32 shade)
{
for (int32 i = list->CountItems(); i < size; i++) {
Particle* p = new Particle();
Particle::_FillParticle(p, size, shade);
list->AddItem(p);
}
}
/*static*/ void
Particle::RemoveParticles(int32 size, int32 shade)
{
while (list->CountItems() > size)
delete list->RemoveItemAt(list->CountItems() - 1);
}
/*static*/ void
Particle::ColorParticles(int32 size, int32 shade)
{
for (int32 i = 0; i < size; i++) {
Particle* p = list->ItemAt(i);
Particle::_ColorParticle(p, size, shade);
}
}
/*static*/ void
Particle::Terminate()
{
for (int32 i = 0; i < list->CountItems(); i++)
delete (Particle*)list->ItemAt(i);
list->MakeEmpty();
delete list;
}
void
/*static*/ void
Particle::Tick()
{
for (int32 i = 0; i < list->CountItems(); i++) {
Particle* p = (Particle*)list->ItemAt(i);
Particle* p = list->ItemAt(i);
p->_Logic();
p->_Render();
}
@ -134,3 +114,74 @@ Particle::_Render() const
glEnd();
glPopMatrix();
}
/*static*/ void
Particle::_FillParticle(Particle* p, int32 size, int32 shade)
{
p->x = frand() * 30.0f - 15.0f;
p->y = frand() * 30.0f - 15.0f;
p->z = frand() * 5.0f;
p->r = frand() * 360.0f;
p->vx = frand() - 0.5f;
p->vy = frand() - 0.5f;
p->vz = frand() - 0.5f;
p->vr = (frand() - 0.5f) * 180.0f;
Particle::_ColorParticle(p, size, shade);
}
/*static*/ void
Particle::_ColorParticle(Particle* p, int32 size, int32 shade)
{
switch(shade) {
case 0:
// Red
p->red = 0.1f + frand() * 0.2f;
p->green = 0.0f;
p->blue = frand() * 0.05f;
break;
case 1:
// Green
p->red = 0;
p->green = 0.1f + frand() * 0.2f;
p->blue = frand() * 0.05f;
break;
case 2:
default:
// Blue
p->red = 0;
p->green = frand() * 0.05f;
p->blue = 0.1f + frand() * 0.2f;
break;
case 3:
// Orange
p->red = 0.1f + frand() * 0.1f;
p->green = 0.05f + frand() * 0.1f;
p->blue = 0.0f;
break;
case 4:
// Purple
p->red = 0.1f + frand() * 0.2f;
p->green = 0.0f;
p->blue = 0.1f + frand() * 0.2f;
break;
case 5:
// White
p->red = p->green = p->blue = 0.1f + frand() * 0.2f;
case 6:
// Rainbow
p->red = 0.1f + frand() * 0.2f;
p->green = 0.1f + frand() * 0.2f;
p->blue = 0.1f + frand() * 0.2f;
break;
}
}

View File

@ -1,44 +1,55 @@
/*
* Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT license.
*
* Authors:
* Tri-Edge AI
* John Scipione, jscipione@gmail.com
*/
#ifndef PARTICLE_H
#define PARTICLE_H
#include <ObjectList.h>
#include <GLView.h>
class BList;
class Particle
{
class Particle {
public:
static BList* list;
static BObjectList<Particle>* list;
static void Initialize(int32 size, int32 shade);
static void Terminate();
static void Tick();
static void Initialize(int32 size, int32 shade);
static void AddParticles(int32 size, int32 shade);
static void RemoveParticles(int32 size, int32 shade);
static void ColorParticles(int32 size, int32 shade);
static void Terminate();
static void Tick();
float x;
float y;
float z;
float r;
float x;
float y;
float z;
float r;
float vx;
float vy;
float vz;
float vr;
float vx;
float vy;
float vz;
float vr;
float red;
float green;
float blue;
float red;
float green;
float blue;
private:
void _Logic();
void _Render() const;
void _Logic();
void _Render() const;
static void _FillParticle(Particle* p, int32 size,
int32 shade);
static void _ColorParticle(Particle* p, int32 size,
int32 shade);
};
#endif
#endif // PARTICLE_H