xref: /haiku/src/add-ons/screen_savers/gravity/GravityView.cpp (revision bff26905875330ea486e316f9c4279556917bfb5)
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 "GravityView.h"
14 
15 #include "Gravity.h"
16 #include "GravitySource.h"
17 #include "Particle.h"
18 
19 #include <GL/glu.h>
20 
21 
22 GravityView::GravityView(BRect frame, Gravity* parent)
23 	:
24 	BGLView(frame, B_EMPTY_STRING, B_FOLLOW_NONE, 0,
25 		BGL_RGB | BGL_DEPTH | BGL_DOUBLE),
26 	fParent(parent),
27 	fGravitySource(new GravitySource()),
28 	fSize(128 << parent->Config.ParticleCount),
29 	fShade(parent->Config.ShadeID)
30 {
31 	Particle::Initialize(fSize, fShade);
32 
33 	LockGL();
34 
35 	glClearDepth(1.0f);
36 
37 	glEnable(GL_BLEND);
38 	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
39 
40 	glMatrixMode(GL_PROJECTION);
41 	glLoadIdentity();
42 	gluPerspective(45.0f, frame.Width() / frame.Height(), 2.0f, 20000.0f);
43 	glMatrixMode(GL_MODELVIEW);
44 	glLoadIdentity();
45 
46 	glTranslatef(0.0f, 0.0f, -30.0f);
47 
48 	glDepthMask(GL_FALSE);
49 
50 	UnlockGL();
51 }
52 
53 
54 GravityView::~GravityView()
55 {
56 	Particle::Terminate();
57 	delete fGravitySource;
58 }
59 
60 
61 void
62 GravityView::AttachedToWindow()
63 {
64 	LockGL();
65 	BGLView::AttachedToWindow();
66 	UnlockGL();
67 }
68 
69 
70 void
71 GravityView::DirectDraw()
72 {
73 	int32 size = 128 << fParent->Config.ParticleCount;
74 	int32 shade = fParent->Config.ShadeID;
75 
76 	// resize particle list if needed
77 	if (size > fSize)
78 		Particle::AddParticles(size, shade);
79 	else if (size < fSize)
80 		Particle::RemoveParticles(size, shade);
81 
82 	// recolor particles if needed
83 	if (shade != fShade)
84 		Particle::ColorParticles(size, shade);
85 
86 	fSize = size;
87 	fShade = shade;
88 
89 	LockGL();
90 
91 	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
92 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
93 
94 	Particle::Tick();
95 	fGravitySource->Tick();
96 
97 	SwapBuffers();
98 
99 	UnlockGL();
100 }
101 
102