[haiku-commits] haiku: hrev45111 - src/add-ons/screen_savers/gravity

  • From: Gerasim Troeglazov <3deyes@xxxxxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Mon, 31 Dec 2012 08:54:52 +0100 (CET)

hrev45111 adds 1 changeset to branch 'master'
old head: 776c58b2b56d8bcf33638a2ecb6c697f95a1cbf3
new head: 8cd10c710fcecc63e86ac1d9db3db25e176d0c3e
overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=8cd10c7+%5E776c58b

----------------------------------------------------------------------------

8cd10c7: Gravity: Big code changes
  
  * Reworked code style completely.
  * Improved the behaviour of the particles.
  * Made particles smaller (2,5x performance gain).
  * Cleaned unnecessary includes.
  * Tried improving performance with glCallList - performance dropped even more.
  * Tried improving performance with glDrawArrays - no noticeable performance 
changes.

                                       [ Tri-Edge AI <triedgeai@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev45111
Commit:      8cd10c710fcecc63e86ac1d9db3db25e176d0c3e
URL:         http://cgit.haiku-os.org/haiku/commit/?id=8cd10c7
Author:      Tri-Edge AI <triedgeai@xxxxxxxxx>
Date:        Mon Dec 31 07:42:51 2012 UTC
Committer:   threedeyes <3dEyes@xxxxxxxxx>
Commit-Date: Mon Dec 31 07:42:51 2012 UTC

----------------------------------------------------------------------------

21 files changed, 589 insertions(+), 609 deletions(-)
src/add-ons/screen_savers/gravity/ConfigView.cpp | 104 +++++++++++++++++
src/add-ons/screen_savers/gravity/ConfigView.h   |  41 +++++++
src/add-ons/screen_savers/gravity/Constants.h    |  15 +++
src/add-ons/screen_savers/gravity/Gravity.cpp    |  98 ++++++++++++++--
src/add-ons/screen_savers/gravity/Gravity.h      |  43 +++++++
.../screen_savers/gravity/GravityConfigView.cpp  |  94 ---------------
.../screen_savers/gravity/GravityConfigView.hpp  |  42 -------
.../screen_savers/gravity/GravityHole.cpp        |  79 -------------
.../screen_savers/gravity/GravityHole.hpp        |  35 ------
.../screen_savers/gravity/GravityScreenSaver.cpp |  95 ---------------
.../screen_savers/gravity/GravityScreenSaver.hpp |  53 ---------
.../screen_savers/gravity/GravitySource.cpp      |  74 ++++++++++++
.../screen_savers/gravity/GravitySource.h        |  34 ++++++
.../screen_savers/gravity/GravityView.cpp        |  83 ++++++-------
src/add-ons/screen_savers/gravity/GravityView.h  |  32 +++++
.../screen_savers/gravity/GravityView.hpp        |  40 -------
src/add-ons/screen_savers/gravity/Jamfile        |   8 +-
src/add-ons/screen_savers/gravity/Particle.cpp   | 116 ++++++++++---------
src/add-ons/screen_savers/gravity/Particle.h     |  44 +++++++
src/add-ons/screen_savers/gravity/Particle.hpp   |  54 ---------
src/add-ons/screen_savers/gravity/main.cpp       |  14 +++

----------------------------------------------------------------------------

diff --git a/src/add-ons/screen_savers/gravity/ConfigView.cpp 
b/src/add-ons/screen_savers/gravity/ConfigView.cpp
new file mode 100644
index 0000000..3a4fe18
--- /dev/null
+++ b/src/add-ons/screen_savers/gravity/ConfigView.cpp
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
+
+
+#include "ConfigView.h"
+
+#include "Constants.h"
+#include "Gravity.h"
+
+#include <GroupLayoutBuilder.h>
+#include <ListView.h>
+#include <ScrollView.h>
+#include <Slider.h>
+#include <StringView.h>
+#include <View.h>
+
+
+ConfigView::ConfigView(Gravity* parent, BRect rect)
+       :
+       BView(rect, B_EMPTY_STRING, B_FOLLOW_ALL_SIDES, B_WILL_DRAW)
+{
+       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->AddItem(new BStringItem("Red"));
+       fShadeList->AddItem(new BStringItem("Green"));
+       fShadeList->AddItem(new BStringItem("Blue"));
+       fShadeList->AddItem(new BStringItem("Orange"));
+       fShadeList->AddItem(new BStringItem("Purple"));
+       fShadeList->AddItem(new BStringItem("White"));
+       fShadeList->AddItem(new BStringItem("Rainbow"));
+
+       fShadeList->Select(parent->Config.ShadeID);
+
+       fShadeScroll = new BScrollView(B_EMPTY_STRING, fShadeList,
+               B_WILL_DRAW | B_FRAME_EVENTS, false, true);
+
+       fCountSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
+       fCountSlider->SetHashMarkCount(5);
+       fCountSlider->SetLimitLabels("128", "2048");
+
+       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)
+       );
+}
+
+
+void
+ConfigView::AttachedToWindow()
+{
+       fShadeList->SetTarget(this);
+       fCountSlider->SetTarget(this);
+}
+
+
+void
+ConfigView::MessageReceived(BMessage* msg)
+{
+       switch (msg->what) {
+               case MSG_COUNT:
+                       fParent->Config.ParticleCount = fCountSlider->Value();
+                       break;
+
+               case MSG_SHADE:
+                       fParent->Config.ShadeID = 
fShadeList->CurrentSelection();
+                       break;
+
+               default:
+                       BView::MessageReceived(msg);
+       }
+}
diff --git a/src/add-ons/screen_savers/gravity/ConfigView.h 
b/src/add-ons/screen_savers/gravity/ConfigView.h
new file mode 100644
index 0000000..adfa77c
--- /dev/null
+++ b/src/add-ons/screen_savers/gravity/ConfigView.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
+#ifndef GRAVITY_CONFIG_VIEW_H
+#define GRAVITY_CONFIG_VIEW_H
+
+
+#include <View.h>
+
+class Gravity;
+
+class BListView;
+class BScrollView;
+class BSlider;
+class BStringView;
+
+class ConfigView : public BView
+{
+public:
+                                       ConfigView(Gravity* parent, BRect rect);
+
+       void                    AttachedToWindow();
+       void                    MessageReceived(BMessage* pbmMessage);
+
+private:
+       Gravity*                fParent;
+
+       BStringView*    fTitleString;
+       BStringView*    fAuthorString;
+
+       BListView*              fShadeList;
+       BStringView*    fShadeString;
+       BScrollView*    fShadeScroll;
+
+       BSlider*                fCountSlider;
+
+};
+
+
+#endif
diff --git a/src/add-ons/screen_savers/gravity/Constants.h 
b/src/add-ons/screen_savers/gravity/Constants.h
new file mode 100644
index 0000000..15df98d
--- /dev/null
+++ b/src/add-ons/screen_savers/gravity/Constants.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * 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
diff --git a/src/add-ons/screen_savers/gravity/Gravity.cpp 
b/src/add-ons/screen_savers/gravity/Gravity.cpp
index 5010f5c..afef0fe 100644
--- a/src/add-ons/screen_savers/gravity/Gravity.cpp
+++ b/src/add-ons/screen_savers/gravity/Gravity.cpp
@@ -1,17 +1,91 @@
-/* 
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- *
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */ 
- 
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
 
-#include "GravityScreenSaver.hpp"
 
+#include "Gravity.h"
 
-extern "C" _EXPORT BScreenSaver* 
-instantiate_screen_saver(BMessage* pbmPrefs, image_id iidImage)
+#include "ConfigView.h"
+#include "GravityView.h"
+
+#include <ScreenSaver.h>
+#include <View.h>
+
+#include <stdlib.h>
+
+Gravity::Gravity(BMessage* prefs, image_id imageID)
+       :
+       BScreenSaver(prefs, imageID)
+{
+       srand(time(NULL));
+
+       if (prefs->IsEmpty()) {
+               Config.ParticleCount = 1;
+               Config.ShadeID = 2;
+       } else {
+               if (prefs->FindInt32("ParticleCount", &Config.ParticleCount) != 
B_OK)
+                       Config.ParticleCount = 1;
+
+               if (prefs->FindInt32("ShadeID", &Config.ShadeID) != B_OK)
+                       Config.ShadeID = 2;
+       }
+}
+
+
+status_t
+Gravity::SaveState(BMessage* prefs) const
+{
+       prefs->AddInt32("ParticleCount", Config.ParticleCount);
+       prefs->AddInt32("ShadeID", Config.ShadeID);
+       return B_OK;
+}
+
+
+void
+Gravity::StartConfig(BView* view)
+{
+       view->AddChild(new ConfigView(this, view->Bounds()));
+}
+
+
+status_t
+Gravity::StartSaver(BView* view, bool preview)
+{
+       if (preview) {
+               fView = NULL;
+               return B_ERROR;
+       } else {
+               SetTickSize((1000 / 20) * 1000);
+                       // ~20 FPS
+               fView = new GravityView(this, view->Bounds());
+               view->AddChild(fView);
+               return B_OK;
+       }
+}
+
+
+void
+Gravity::StopSaver()
+{
+       if (fView != NULL)
+               fView->EnableDirectMode(false);
+}
+
+
+void
+Gravity::DirectConnected(direct_buffer_info* info)
+{
+       if (fView != NULL) {
+               // TODO: Find out why I had to uncomment this.
+               // view->DirectConnected(pdbiInfo);
+               // view->EnableDirectMode(true);
+       }
+}
+
+
+void
+Gravity::DirectDraw(int32 frame)
 {
-       return new GravityScreenSaver(pbmPrefs, iidImage);
+       fView->DirectDraw();
 }
diff --git a/src/add-ons/screen_savers/gravity/Gravity.h 
b/src/add-ons/screen_savers/gravity/Gravity.h
new file mode 100644
index 0000000..6fc25f6
--- /dev/null
+++ b/src/add-ons/screen_savers/gravity/Gravity.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
+#ifndef GRAVITY_SCREEN_SAVER_H
+#define GRAVITY_SCREEN_SAVER_H
+
+
+#include <ScreenSaver.h>
+
+class GravityView;
+
+class BMessage;
+class BView;
+
+
+class Gravity : public BScreenSaver
+{
+public:
+       struct
+       {
+               int32 ShadeID;
+               int32 ParticleCount;
+       } Config;
+
+                                       Gravity(BMessage* prefs, image_id 
imageID);
+
+       status_t                SaveState(BMessage* prefs) const;
+
+       void                    StartConfig(BView* view);
+
+       status_t                StartSaver(BView* view, bool preview);
+       void                    StopSaver();
+
+       void                    DirectConnected(direct_buffer_info* info);
+       void                    DirectDraw(int32 frame);
+
+private:
+       GravityView*    fView;
+};
+
+
+#endif
diff --git a/src/add-ons/screen_savers/gravity/GravityConfigView.cpp 
b/src/add-ons/screen_savers/gravity/GravityConfigView.cpp
deleted file mode 100644
index 1a12fa9..0000000
--- a/src/add-ons/screen_savers/gravity/GravityConfigView.cpp
+++ /dev/null
@@ -1,94 +0,0 @@
-/* 
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- * 
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */ 
-
-
-#include "GravityConfigView.hpp"
-
-
-class GravityScreenSaver;
-
-GravityConfigView::GravityConfigView(GravityScreenSaver* parent, BRect frame)
-       :
-       BView(frame, "", B_FOLLOW_ALL_SIDES, B_WILL_DRAW)
-{
-       this->parent = parent;
-       
-       SetLayout(new BGroupLayout(B_HORIZONTAL));
-       SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
-       
-       BStringView* pbsvTitle = new BStringView(frame, B_EMPTY_STRING, 
-               "OpenGL Gravity Effect", B_FOLLOW_LEFT);
-                                                                               
                                
-       BStringView* pbsvAuthor = new BStringView(frame, B_EMPTY_STRING, 
-               "by Tri-Edge AI", B_FOLLOW_LEFT);
-       
-       pbsParticleCount = new BSlider(frame, B_EMPTY_STRING, "Particle Count: 
",
-               new BMessage('pcnt'), 0, 4, B_BLOCK_THUMB);
-       
-       pbsvShadeText = new BStringView(frame, B_EMPTY_STRING, "Shade: ", 
-               B_FOLLOW_LEFT);
-       
-       pblvShade = new BListView(frame, B_EMPTY_STRING, 
B_SINGLE_SELECTION_LIST,
-               B_FOLLOW_ALL);
-                                                                       
-       pblvShade->SetSelectionMessage(new BMessage('shds'));
-       
-       pblvShade->AddItem(new BStringItem("Red"));
-       pblvShade->AddItem(new BStringItem("Green"));
-       pblvShade->AddItem(new BStringItem("Blue"));
-       pblvShade->AddItem(new BStringItem("Orange"));
-       pblvShade->AddItem(new BStringItem("Purple"));
-       pblvShade->AddItem(new BStringItem("White"));
-       pblvShade->AddItem(new BStringItem("Rainbow"));
-       
-       pblvShade->Select(parent->Config.ShadeID);
-       
-       BScrollView* scroll = new BScrollView(B_EMPTY_STRING, pblvShade, 
-               B_WILL_DRAW | B_FRAME_EVENTS, false, true);
-       
-       pbsParticleCount->SetHashMarks(B_HASH_MARKS_BOTTOM);
-       pbsParticleCount->SetHashMarkCount(5);
-       pbsParticleCount->SetLimitLabels("128", "2048");
-       
-       pbsParticleCount->SetValue(parent->Config.ParticleCount);
-       
-       AddChild(BGroupLayoutBuilder(B_VERTICAL, B_USE_DEFAULT_SPACING)
-                       .Add(BGroupLayoutBuilder(B_VERTICAL, 0)
-                               .Add(pbsvTitle)
-                               .Add(pbsvAuthor)
-                       )
-                       .Add(pbsvShadeText)
-                       .Add(scroll)
-                       .Add(pbsParticleCount)
-                       .SetInsets(B_USE_DEFAULT_SPACING, 
-                               B_USE_DEFAULT_SPACING, 
-                               B_USE_DEFAULT_SPACING, 
-                               B_USE_DEFAULT_SPACING)
-       );
-}
-
-
-void
-GravityConfigView::AttachedToWindow()
-{
-       pblvShade->SetTarget(this);
-       pbsParticleCount->SetTarget(this);      
-}
-
-
-void
-GravityConfigView::MessageReceived(BMessage* pbmMessage)
-{
-       if (pbmMessage->what == 'pcnt') {
-               parent->Config.ParticleCount = pbsParticleCount->Value();
-       } else if (pbmMessage->what == 'shds') {
-               parent->Config.ShadeID = pblvShade->CurrentSelection(); 
-       } else {
-               BView::MessageReceived(pbmMessage);     
-       }
-}
diff --git a/src/add-ons/screen_savers/gravity/GravityConfigView.hpp 
b/src/add-ons/screen_savers/gravity/GravityConfigView.hpp
deleted file mode 100644
index b8f63fa..0000000
--- a/src/add-ons/screen_savers/gravity/GravityConfigView.hpp
+++ /dev/null
@@ -1,42 +0,0 @@
-/* 
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- * 
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */ 
-#ifndef _GRAVITY_CONFIG_VIEW_HPP_
-#define _GRAVITY_CONFIG_VIEW_HPP_
-
-
-#include "GravityScreenSaver.hpp"
-
-#include <GroupLayout.h>
-#include <GroupLayoutBuilder.h>
-#include <ListView.h>
-#include <ScrollView.h>
-#include <Slider.h>
-#include <StringItem.h>
-#include <StringView.h>
-#include <View.h>
-
-
-class GravityScreenSaver;
-
-class GravityConfigView : public BView
-{
-public:
-       GravityConfigView(GravityScreenSaver* parent, BRect frame);
-       
-       void AttachedToWindow();
-       void MessageReceived(BMessage* pbmMessage);
-       
-private:       
-       GravityScreenSaver* parent;
-       BListView* pblvShade;
-       BStringView* pbsvShadeText;
-       BSlider* pbsParticleCount;
-};
-
-
-#endif /* _GRAVITY_CONFIG_VIEW_HPP_ */
diff --git a/src/add-ons/screen_savers/gravity/GravityHole.cpp 
b/src/add-ons/screen_savers/gravity/GravityHole.cpp
deleted file mode 100644
index e71a0b7..0000000
--- a/src/add-ons/screen_savers/gravity/GravityHole.cpp
+++ /dev/null
@@ -1,79 +0,0 @@
-/* 
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- * 
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */ 
-
-#include "Particle.hpp"
-#include "GravityHole.hpp"
-
-#include <math.h>
-#include <stdlib.h>
-
-#define frand() ((float)rand() / (float)RAND_MAX)
-
-GravityHole::GravityHole()
-{
-       x = 0.0f;
-       y = 0.0f;
-       z = 0.0f;
-       
-       vx = 0.0f;
-       vy = 0.0f;
-       vz = 0.0f;
-       
-       ax = frand() * 30.0f - 15.0f;
-       ay = frand() * 30.0f - 15.0f;
-       az = frand() * 10.0f - 5.0f;
-}
-
-
-void
-GravityHole::Run()
-{
-       float dx = ax - x;
-       float dy = ay - y;
-       float dz = az - z;
-       
-       float d = dx * dx + dy * dy + dz * dz;
-       
-       vx += dx * 0.005f;
-       vy += dy * 0.005f;
-       vz += dz * 0.005f;
-       
-       x += vx;
-       y += vy;
-       z += vz;
-       
-       vx *= 0.95f;
-       vy *= 0.95f;
-       vz *= 0.95f;
-       
-       if (dx * dx + dy * dy + dz * dz < 10.0f) {
-               ax = frand() * 30.0f - 15.0f;
-               ay = frand() * 30.0f - 15.0f;
-               az = frand() * 10.0f - 5.0f;
-       }
-       
-       for (uint32 i = 0; i < Particle::list.size(); i++) {
-               dx = x - Particle::list[i]->x;
-               dy = y - Particle::list[i]->y;
-               dz = z - Particle::list[i]->z;
-               
-               d = dx * dx + dy * dy + dz * dz;
-               
-               Particle::list[i]->vx += dx / d * 0.5f;
-               Particle::list[i]->vy += dy / d * 0.5f;
-               Particle::list[i]->vz += dz / d * 0.5f;
-               Particle::list[i]->vr += 1.0f / d;
-       }
-}
-
-
-void
-GravityHole::Draw()
-{
-       //...
-}
diff --git a/src/add-ons/screen_savers/gravity/GravityHole.hpp 
b/src/add-ons/screen_savers/gravity/GravityHole.hpp
deleted file mode 100644
index 9ede10f..0000000
--- a/src/add-ons/screen_savers/gravity/GravityHole.hpp
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- *
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */
-
-#ifndef _GRAVITY_HOLE_HPP_
-#define _GRAVITY_HOLE_HPP_
-
-#include <GLView.h>
-
-class GravityHole
-{
-public:
-       float x;
-       float y;
-       float z;
-       
-       float vx;
-       float vy;
-       float vz;
-       
-       float ax;
-       float ay;
-       float az;
-       
-       GravityHole();
-       
-       void Run();
-       void Draw();
-};
-
-#endif /* _GRAVITY_HOLE_HPP */
diff --git a/src/add-ons/screen_savers/gravity/GravityScreenSaver.cpp 
b/src/add-ons/screen_savers/gravity/GravityScreenSaver.cpp
deleted file mode 100644
index 1524b0a..0000000
--- a/src/add-ons/screen_savers/gravity/GravityScreenSaver.cpp
+++ /dev/null
@@ -1,95 +0,0 @@
-/* 
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- * 
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */ 
-
-
-#include <View.h>
-#include <StringView.h>
-
-#include "GravityScreenSaver.hpp"
-
-
-GravityScreenSaver::GravityScreenSaver(BMessage* pbmPrefs, image_id iidImage) 
-       :
-       BScreenSaver(pbmPrefs, iidImage)
-{
-       srand(time(NULL));
-       
-       if (pbmPrefs->IsEmpty()) {
-               Config.ParticleCount = 1;
-               Config.ShadeID = 2;
-       } else {
-               if (pbmPrefs->FindInt32("ParticleCount", &Config.ParticleCount) 
!= B_OK)
-                       Config.ParticleCount = 1;       
-               
-               if (pbmPrefs->FindInt32("ShadeID", &Config.ShadeID) != B_OK)
-                       Config.ShadeID = 2;     
-       }
-}
-
-
-status_t
-GravityScreenSaver::SaveState(BMessage* pbmPrefs) const
-{
-       pbmPrefs->AddInt32("ParticleCount", Config.ParticleCount);
-       pbmPrefs->AddInt32("ShadeID", Config.ShadeID);
-       return B_OK;    
-}
-
-
-void
-GravityScreenSaver::StartConfig(BView* pbvView)
-{
-       pbvView->AddChild(new GravityConfigView(this, pbvView->Bounds()));
-}
-
-
-status_t
-GravityScreenSaver::StartSaver(BView* pbvView, bool bPreview)
-{
-       if (bPreview) {
-               view = NULL;
-               return B_ERROR;
-       } else {
-               SetTickSize((1000 / 30) * 1000); // ~30 FPS
-       
-               view = new GravityView(this, pbvView->Bounds());
-               pbvView->AddChild(view);
-       
-               return B_OK;
-       }
-}
-
-
-void
-GravityScreenSaver::StopSaver()
-{
-       if (view != NULL) {
-               view->EnableDirectMode(false);
-       }
-}
-
-
-void
-GravityScreenSaver::DirectConnected(direct_buffer_info* pdbiInfo)
-{
-       if (view != NULL) {
-               // TODO: Find out why I had to uncomment this.
-               // view->DirectConnected(pdbiInfo);
-               // view->EnableDirectMode(true);
-       }
-}
-
-
-void
-GravityScreenSaver::DirectDraw(int32 iFrame)
-{
-       view->Run();
-       // Dummy rect
-       BRect rect;
-       view->Draw(rect);
-}
diff --git a/src/add-ons/screen_savers/gravity/GravityScreenSaver.hpp 
b/src/add-ons/screen_savers/gravity/GravityScreenSaver.hpp
deleted file mode 100644
index 40900e2..0000000
--- a/src/add-ons/screen_savers/gravity/GravityScreenSaver.hpp
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- *
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */
-#ifndef _GRAVITY_SCREEN_SAVER_HPP_
-#define _GRAVITY_SCREEN_SAVER_HPP_
- 
-
-#include "GravityConfigView.hpp"
-#include "GravityView.hpp"
- 
-#include <OS.h>
-#include <ScreenSaver.h>
-#include <View.h>
-#include <GLView.h>
-
-#include <stdlib.h>
-#include <time.h>
-#include <math.h>
-  
-
-class GravityView;
- 
-class GravityScreenSaver : public BScreenSaver
-{
-public:
-       struct 
-       {
-               int32 ShadeID;
-               int32 ParticleCount;
-       } Config;
-       
-       GravityScreenSaver(BMessage* pbmPrefs, image_id iidImage);
-
-       status_t SaveState(BMessage* pbmPrefs) const;
-
-       void StartConfig(BView* pbvView);
-       
-       status_t StartSaver(BView* pbvView, bool bPreview);
-       void StopSaver();
-               
-       void DirectConnected(direct_buffer_info* pdbiInfo);
-       void DirectDraw(int32 iFrame);
-       
-private:
-       GravityView* view;
-};
-
-
-#endif /* _GRAVITY_SCREEN_SAVER_HPP_ */
diff --git a/src/add-ons/screen_savers/gravity/GravitySource.cpp 
b/src/add-ons/screen_savers/gravity/GravitySource.cpp
new file mode 100644
index 0000000..0348768
--- /dev/null
+++ b/src/add-ons/screen_savers/gravity/GravitySource.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
+
+
+#include "GravitySource.h"
+
+#include "Particle.h"
+
+#include <stdlib.h>
+
+
+#define frand() ((float)rand() / (float)RAND_MAX)
+
+
+GravitySource::GravitySource()
+{
+       x = 0.0f;
+       y = 0.0f;
+       z = 0.0f;
+       r = 0.0f;
+
+       vx = 0.0f;
+       vy = 0.0f;
+       vz = 0.0f;
+
+       tx = frand() * 30.0f - 15.0f;
+       ty = frand() * 30.0f - 15.0f;
+       tz = frand() * 10.0f - 5.0f;
+}
+
+
+void
+GravitySource::Tick()
+{
+       float dx = tx - x;
+       float dy = ty - y;
+       float dz = tz - z;
+
+       float d = dx * dx + dy * dy + dz * dz;
+
+       vx += dx * 0.003f;
+       vy += dy * 0.003f;
+       vz += dz * 0.003f;
+
+       x += vx;
+       y += vy;
+       z += vz;
+
+       vx *= 0.98f;
+       vy *= 0.98f;
+       vz *= 0.98f;
+
+       if (dx * dx + dy * dy + dz * dz < 1.0f) {
+               tx = frand() * 20.0f - 10.0f;
+               ty = frand() * 20.0f - 10.0f;
+               tz = frand() * 10.0f - 5.0f;
+       }
+
+       for (int32 i = 0; i < Particle::list->CountItems(); i++) {
+               Particle* p = (Particle*)Particle::list->ItemAt(i);
+               dx = x - p->x;
+               dy = y - p->y;
+               dz = z - p->z;
+
+               d = dx * dx + dy * dy + dz * dz;
+
+               p->vx += dx / d * 0.25f;
+               p->vy += dy / d * 0.25f;
+               p->vz += dz / d * 0.25f;
+               p->vr += 1.0f / d;
+       }
+}
diff --git a/src/add-ons/screen_savers/gravity/GravitySource.h 
b/src/add-ons/screen_savers/gravity/GravitySource.h
new file mode 100644
index 0000000..0a44ef7
--- /dev/null
+++ b/src/add-ons/screen_savers/gravity/GravitySource.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
+#ifndef GRAVITY_SOURCE_H
+#define GRAVITY_SOURCE_H
+
+
+class GravitySource
+{
+public:
+       float   x;
+       float   y;
+       float   z;
+       float   r;
+
+       float   vx;
+       float   vy;
+       float   vz;
+
+       float   tx;
+       float   ty;
+       float   tz;
+
+                       GravitySource();
+
+       void    Tick();
+
+private:
+
+};
+
+
+#endif
diff --git a/src/add-ons/screen_savers/gravity/GravityView.cpp 
b/src/add-ons/screen_savers/gravity/GravityView.cpp
index 880ecf1..3ae454d 100644
--- a/src/add-ons/screen_savers/gravity/GravityView.cpp
+++ b/src/add-ons/screen_savers/gravity/GravityView.cpp
@@ -1,70 +1,69 @@
-/* 
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- * 
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */ 
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
 
 
-#include "GravityView.hpp"
+#include "GravityView.h"
+
+#include "Gravity.h"
+#include "GravitySource.h"
+#include "Particle.h"
 
 #include <GL/glu.h>
 
 
-GravityView::GravityView(GravityScreenSaver* parent, BRect rect)
+GravityView::GravityView(Gravity* parent, BRect rect)
        :
-       BGLView(rect, B_EMPTY_STRING, B_FOLLOW_NONE, 0, 
-               BGL_RGB | BGL_DEPTH | BGL_DOUBLE),
-       fRect(rect)
+       BGLView(rect, B_EMPTY_STRING, B_FOLLOW_NONE, 0,
+               BGL_RGB | BGL_DEPTH | BGL_DOUBLE)
 {
-       this->parent = parent;
-       
+       fParent = parent;
+
        int realCount;
-       
+
        if (parent->Config.ParticleCount == 0)
                realCount = 128;
        else if (parent->Config.ParticleCount == 1)
-               realCount = 256;        
+               realCount = 256;
        else if (parent->Config.ParticleCount == 2)
-               realCount = 512;        
+               realCount = 512;
        else if (parent->Config.ParticleCount == 3)
-               realCount = 1024;       
+               realCount = 1024;
        else if (parent->Config.ParticleCount == 4)
-               realCount = 2048;       
+               realCount = 2048;
        else
-               realCount = 128; // This shouldn't be happening either. 
-       
+               realCount = 128;
+
        Particle::Initialize(realCount, parent->Config.ShadeID);
-       
+
        LockGL();
 
        glClearDepth(1.0f);
-       
-       glEnable(GL_TEXTURE_2D);
-       
+
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE);
-       
+
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, rect.Width() / rect.Height(), 2.0f, 20000.0f);
+       glMatrixMode(GL_MODELVIEW);
+       glLoadIdentity();
+
        glTranslatef(0.0f, 0.0f, -30.0f);
-       
+
        glDepthMask(GL_FALSE);
-       
-       glMatrixMode(GL_MODELVIEW);
-       
+
        UnlockGL();
-       
-       ghole = new GravityHole();
+
+       fGravSource = new GravitySource();
 }
 
 
 GravityView::~GravityView()
 {
-       delete ghole;
        Particle::Terminate();
+       delete fGravSource;
 }
 
 
@@ -78,24 +77,18 @@ GravityView::AttachedToWindow()
 
 
 void
-GravityView::Draw(BRect rect)
+GravityView::DirectDraw()
 {
        LockGL();
-       
+
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-       
-       Particle::DrawAll();
-       ghole->Draw();
+
+       Particle::Tick();
+       fGravSource->Tick();
 
        SwapBuffers();
+
        UnlockGL();
 }
 
-
-void
-GravityView::Run()
-{
-       Particle::RunAll();
-       ghole->Run();
-}
diff --git a/src/add-ons/screen_savers/gravity/GravityView.h 
b/src/add-ons/screen_savers/gravity/GravityView.h
new file mode 100644
index 0000000..4557b60
--- /dev/null
+++ b/src/add-ons/screen_savers/gravity/GravityView.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
+#ifndef GRAVITY_VIEW_H
+#define GRAVITY_VIEW_H
+
+
+#include <GLView.h>
+
+
+class Gravity;
+class GravitySource;
+
+class GravityView : public BGLView
+{
+public:
+                                       GravityView(Gravity* parent, BRect 
rect);
+                                       ~GravityView();
+
+       void                    AttachedToWindow();
+
+       void                    DirectDraw();
+
+private:
+       Gravity*                fParent;
+       GravitySource*  fGravSource;
+
+};
+
+
+#endif
diff --git a/src/add-ons/screen_savers/gravity/GravityView.hpp 
b/src/add-ons/screen_savers/gravity/GravityView.hpp
deleted file mode 100644
index 4938437..0000000
--- a/src/add-ons/screen_savers/gravity/GravityView.hpp
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- *
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */
-#ifndef _GRAVITY_VIEW_HPP_
-#define _GRAVITY_VIEW_HPP_
-
-
-#include <GLView.h>
-
-#include "Particle.hpp"
-#include "GravityHole.hpp"
-#include "GravityScreenSaver.hpp"
-
-
-class GravityScreenSaver;
-
-class GravityView : public BGLView
-{
-public:
-                                                       
GravityView(GravityScreenSaver* parent, BRect rect);
-                                                       ~GravityView();
-       
-                       void                    AttachedToWindow();
-       
-                       void                    Draw(BRect rect);
-                       void                    Run();
-       
-private:
-                       BRect                   fRect;
-
-                       GravityScreenSaver* parent;
-                       GravityHole*    ghole;
-};
-
-
-#endif /* _GRAVITY_VIEW_HPP_ */
diff --git a/src/add-ons/screen_savers/gravity/Jamfile 
b/src/add-ons/screen_savers/gravity/Jamfile
index c6f3238..426435b 100644
--- a/src/add-ons/screen_savers/gravity/Jamfile
+++ b/src/add-ons/screen_savers/gravity/Jamfile
@@ -3,19 +3,19 @@ SubDirSysHdrs $(HAIKU_GLU_HEADERS) ;
 SubDirSysHdrs $(HAIKU_MESA_HEADERS) ;
 
 # For GCC2
-if $(HAIKU_GCC_VERSION[1]) < 3 { 
+if $(HAIKU_GCC_VERSION[1]) < 3 {
     SubDirC++Flags --no-warnings ;
 }
 
 AddResources Gravity : Gravity.rdef ;
 
 local sources =
+       ConfigView.cpp
        Gravity.cpp
-       GravityConfigView.cpp
-       GravityHole.cpp
-       GravityScreenSaver.cpp
+       GravitySource.cpp
        GravityView.cpp
        Particle.cpp
+       main.cpp
 ;
 
 Includes [ FGristFiles $(sources) ] : $(HAIKU_MESA_HEADERS_DEPENDENCY) ;
diff --git a/src/add-ons/screen_savers/gravity/Particle.cpp 
b/src/add-ons/screen_savers/gravity/Particle.cpp
index f5d3e62..93cea67 100644
--- a/src/add-ons/screen_savers/gravity/Particle.cpp
+++ b/src/add-ons/screen_savers/gravity/Particle.cpp
@@ -1,66 +1,81 @@
-/* 
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- * 
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */ 
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
 
 
-#include "Particle.hpp"
+#include "Particle.h"
+
+#include <List.h>
+
+#include <stdlib.h>
 
 
 #define frand() ((float)rand() / (float)RAND_MAX)
 
-vector<Particle*> Particle::list;
+
+BList* Particle::list;
 
 
 void
 Particle::Initialize(int32 size, int32 shade)
-{      
+{
+       list = new BList();
+
        for (int32 i = 0; i < size; i++) {
-               Particle* p = new Particle(frand() * 30.0f - 15.0f, 
-                       frand() * 30.0f - 15.0f, frand() * 5.0f, frand() * 
360.0f);
-               
+               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
+
+               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->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
+               } 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
+               } 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
+               } 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 
+               } else if (shade == 5) {
+                       // White
                        p->red = p->green = p->blue = 0.1f + frand() * 0.2f;
-               } else if (shade == 6) { // Rainbow
+               } 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.
+                       // Man, this shouldn't even happen.. Blue.
                        p->red = 0;
                        p->green = frand() * 0.05f;
                        p->blue = 0.1f + frand() * 0.2f;
                }
-               
-               list.push_back(p);
+
+               list->AddItem(p);
        }
 }
 
@@ -68,46 +83,35 @@ Particle::Initialize(int32 size, int32 shade)
 void
 Particle::Terminate()
 {
-       for (uint32 i = 0; i < list.size(); i++)
-               delete list[i]; 
-       
-       list.clear();
-}
+       for (int32 i = 0; i < list->CountItems(); i++)
+               delete (Particle*)list->ItemAt(i);
 
-
-void
-Particle::RunAll()
-{
-       for (uint32 i = 0; i < list.size(); i++)
-               list[i]->Run();
+       list->MakeEmpty();
+       delete list;
 }
 
 
 void
-Particle::DrawAll()
+Particle::Tick()
 {
-       for (uint32 i = 0; i < list.size(); i++)
-               list[i]->Draw();
-}
-
-
-Particle::Particle(float x, float y, float z, float r)
-{
-       this->x = x;
-       this->y = y;
-       this->z = z;
-       this->r = r;
+       for (int32 i = 0; i < list->CountItems(); i++) {
+               Particle* p = (Particle*)list->ItemAt(i);
+               p->_Logic();
+               p->_Render();
+       }
 }
 
 
 void
-Particle::Run()
+Particle::_Logic()
 {
+       // Motion
        x += vx;
        y += vy;
        z += vz;
        r += vr;
-       
+
+       // Friction
        vx *= 0.98f;
        vy *= 0.98f;
        vz *= 0.98f;
@@ -116,17 +120,17 @@ Particle::Run()
 
 
 void
-Particle::Draw() const
+Particle::_Render() const
 {
        glPushMatrix();
        glTranslatef(x, y, z);
        glRotatef(r, 0.0f, 0.0f, 1.0f);
-       glBegin(GL_QUADS);
        glColor3f(red, green, blue);
-       glVertex3f(-0.5f,  0.5f,  0.0f);
-       glVertex3f(-0.5f, -0.5f,  0.0f);
-       glVertex3f( 0.5f, -0.5f,  0.0f);
-       glVertex3f( 0.5f,  0.5f,  0.0f);
+       glBegin(GL_QUADS);
+       glVertex3f(-0.25f,  0.25f,  0.0f);
+       glVertex3f(-0.25f, -0.25f,  0.0f);
+       glVertex3f( 0.25f, -0.25f,  0.0f);
+       glVertex3f( 0.25f,  0.25f,  0.0f);
        glEnd();
        glPopMatrix();
 }
diff --git a/src/add-ons/screen_savers/gravity/Particle.h 
b/src/add-ons/screen_savers/gravity/Particle.h
new file mode 100644
index 0000000..1d7be18
--- /dev/null
+++ b/src/add-ons/screen_savers/gravity/Particle.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
+#ifndef PARTICLE_H
+#define PARTICLE_H
+
+
+#include <GLView.h>
+
+class BList;
+
+
+class Particle
+{
+public:
+       static  BList*  list;
+
+       static  void    Initialize(int32 size, int32 shade);
+       static  void    Terminate();
+       static  void    Tick();
+
+                       float   x;
+                       float   y;
+                       float   z;
+                       float   r;
+
+                       float   vx;
+                       float   vy;
+                       float   vz;
+                       float   vr;
+
+                       float   red;
+                       float   green;
+                       float   blue;
+
+private:
+                       void    _Logic();
+                       void    _Render() const;
+
+};
+
+
+#endif
diff --git a/src/add-ons/screen_savers/gravity/Particle.hpp 
b/src/add-ons/screen_savers/gravity/Particle.hpp
deleted file mode 100644
index bc8f204..0000000
--- a/src/add-ons/screen_savers/gravity/Particle.hpp
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright 2012, Haiku, Inc.
- * Distributed under the terms of the MIT License.
- *
- * Authors:
- *             Tri-Edge AI <triedgeai@xxxxxxxxx>
- */
-#ifndef _PARTICLE_HPP_
-#define _PARTICLE_HPP_
-
-
-#include <GLView.h>
-
-#include <math.h>
-#include <vector>
-#include <cstdlib>
-
-
-using namespace std;
-
-
-class Particle
-{
-public:
-       static vector<Particle*> list;
-       
-       static void Initialize(int32 size, int32 shade);
-       static void Terminate();
-       static void RunAll();
-       static void DrawAll();
-
-       float x;
-       float y;
-       float z;
-       float r;
-       
-       float vx;
-       float vy;
-       float vz;
-       float vr;
-       
-       float red;
-       float green;
-       float blue;
-       
-       Particle(float x, float y, float z, float r);
-       
-private:
-       void Run();
-       void Draw() const;
-};
-
-
-#endif /* _PARTICLE_HPP_ */
diff --git a/src/add-ons/screen_savers/gravity/main.cpp 
b/src/add-ons/screen_savers/gravity/main.cpp
new file mode 100644
index 0000000..9b93e91
--- /dev/null
+++ b/src/add-ons/screen_savers/gravity/main.cpp
@@ -0,0 +1,14 @@
+/*
+ * Copyright 2012-2013 Tri-Edge AI <triedgeai@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
+
+
+#include "Gravity.h"
+
+
+extern "C" _EXPORT BScreenSaver*
+instantiate_screen_saver(BMessage* prefs, image_id imageID)
+{
+       return new Gravity(prefs, imageID);
+}


Other related posts:

  • » [haiku-commits] haiku: hrev45111 - src/add-ons/screen_savers/gravity - Gerasim Troeglazov