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