Skip to content

Commit

Permalink
screensaver: Add gravity based on GCI 2012 work
Browse files Browse the repository at this point in the history
* A modified version of TriEdgeAI's original work.
* Cleaned up style problems
* Some virtual functions didn't mesh properly with
  ones defined in the OpenGL kit (Draw for example)
* Wrote Jamfile
* Add missing include cstdlib for rand
* Not in image yet as it still needs gcc2 testing
  • Loading branch information
kallisti5 committed Dec 27, 2012
1 parent f736d30 commit 93f1242
Show file tree
Hide file tree
Showing 14 changed files with 786 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/add-ons/screen_savers/Jamfile
Expand Up @@ -4,6 +4,7 @@ SubInclude HAIKU_TOP src add-ons screen_savers butterfly ;
SubInclude HAIKU_TOP src add-ons screen_savers debugnow ;
SubInclude HAIKU_TOP src add-ons screen_savers flurry ;
SubInclude HAIKU_TOP src add-ons screen_savers glife ;
SubInclude HAIKU_TOP src add-ons screen_savers gravity ;
SubInclude HAIKU_TOP src add-ons screen_savers haiku ;
SubInclude HAIKU_TOP src add-ons screen_savers icons ;
SubInclude HAIKU_TOP src add-ons screen_savers ifs ;
Expand Down
17 changes: 17 additions & 0 deletions src/add-ons/screen_savers/gravity/Gravity.cpp
@@ -0,0 +1,17 @@
/*
* Copyright 2012, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tri-Edge AI <triedgeai@gmail.com>
*/


#include "GravityScreenSaver.hpp"


extern "C" _EXPORT BScreenSaver*
instantiate_screen_saver(BMessage* pbmPrefs, image_id iidImage)
{
return new GravityScreenSaver(pbmPrefs, iidImage);
}
14 changes: 14 additions & 0 deletions src/add-ons/screen_savers/gravity/Gravity.rdef
@@ -0,0 +1,14 @@
resource app_signature "application/x-vnd.Haiku-GravityScreenSaver";

resource app_name_catalog_entry "x-vnd.Haiku-GravityScreenSaver:System name:Gravity";

resource app_version
{
major = 1,
middle = 0,
minor = 0,
variety = 0,
internal = 0,
short_info = "1.0",
long_info = "The screensaver simulates a gravity hole effect with particles, utilizing the OpenGL interface for Haiku."
};
94 changes: 94 additions & 0 deletions src/add-ons/screen_savers/gravity/GravityConfigView.cpp
@@ -0,0 +1,94 @@
/*
* Copyright 2012, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tri-Edge AI <triedgeai@gmail.com>
*/


#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);
}
}
42 changes: 42 additions & 0 deletions src/add-ons/screen_savers/gravity/GravityConfigView.hpp
@@ -0,0 +1,42 @@
/*
* Copyright 2012, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tri-Edge AI <triedgeai@gmail.com>
*/
#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_ */
79 changes: 79 additions & 0 deletions src/add-ons/screen_savers/gravity/GravityHole.cpp
@@ -0,0 +1,79 @@
/*
* Copyright 2012, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tri-Edge AI <triedgeai@gmail.com>
*/

#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()
{
//...
}
35 changes: 35 additions & 0 deletions src/add-ons/screen_savers/gravity/GravityHole.hpp
@@ -0,0 +1,35 @@
/*
* Copyright 2012, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tri-Edge AI <triedgeai@gmail.com>
*/

#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 */
95 changes: 95 additions & 0 deletions src/add-ons/screen_savers/gravity/GravityScreenSaver.cpp
@@ -0,0 +1,95 @@
/*
* Copyright 2012, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tri-Edge AI <triedgeai@gmail.com>
*/


#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);
}

0 comments on commit 93f1242

Please sign in to comment.