xref: /haiku/src/add-ons/screen_savers/gravity/Gravity.cpp (revision 56430ad8002b8fd1ac69b590e9cc130de6d9e852)
1 /*
2  * Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
3  * Copyright 2014 Haiku, Inc. All rights reserved.
4  *
5  * Distributed under the terms of the MIT license.
6  *
7  * Authors:
8  *		Tri-Edge AI
9  *		John Scipione, jscipione@gmail.com
10  */
11 
12 
13 #include "Gravity.h"
14 
15 #include "ConfigView.h"
16 #include "GravityView.h"
17 
18 #include <ScreenSaver.h>
19 #include <View.h>
20 
21 #include <stdlib.h>
22 
23 
24 Gravity::Gravity(BMessage* prefs, image_id imageID)
25 	:
26 	BScreenSaver(prefs, imageID),
27 	fGravityView(NULL),
28 	fConfigView(NULL)
29 {
30 	srand(time(NULL));
31 
32 	if (prefs->IsEmpty()) {
33 		Config.ParticleCount = 1;
34 		Config.ShadeID = 2;
35 	} else {
36 		if (prefs->FindInt32("ParticleCount", &Config.ParticleCount) != B_OK)
37 			Config.ParticleCount = 1;
38 
39 		if (prefs->FindInt32("ShadeID", &Config.ShadeID) != B_OK)
40 			Config.ShadeID = 2;
41 	}
42 }
43 
44 
45 status_t
46 Gravity::SaveState(BMessage* prefs) const
47 {
48 	prefs->AddInt32("ParticleCount", Config.ParticleCount);
49 	prefs->AddInt32("ShadeID", Config.ShadeID);
50 	return B_OK;
51 }
52 
53 
54 void
55 Gravity::StartConfig(BView* view)
56 {
57 	fConfigView = new ConfigView(view->Bounds(), this);
58 	view->AddChild(fConfigView);
59 }
60 
61 
62 status_t
63 Gravity::StartSaver(BView* view, bool preview)
64 {
65 	SetTickSize((1000 / 20) * 1000);
66 		// ~20 FPS
67 	fGravityView = new GravityView(view->Bounds(), this);
68 	view->AddChild(fGravityView);
69 
70 	return B_OK;
71 }
72 
73 
74 void
75 Gravity::StopSaver()
76 {
77 	if (fGravityView != NULL)
78 		fGravityView->EnableDirectMode(false);
79 }
80 
81 
82 void
83 Gravity::DirectConnected(direct_buffer_info* info)
84 {
85 	if (fGravityView != NULL) {
86 		fGravityView->DirectConnected(info);
87 		fGravityView->EnableDirectMode(true);
88 	}
89 }
90 
91 
92 void
93 Gravity::Draw(BView*, int32 frame)
94 {
95 	fGravityView->DirectDraw();
96 }
97