Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
screensaver: Add gravity based on GCI 2012 work
* 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
Showing
14 changed files
with
786 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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." | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
{ | ||
//... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Oops, something went wrong.