xref: /haiku/src/add-ons/screen_savers/gravity/GravityView.cpp (revision a5a3b2d9a3d95cbae71eaf371708c73a1780ac0d)
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 
34 
35 GravityView::~GravityView()
36 {
37 	Particle::Terminate();
38 	delete fGravitySource;
39 }
40 
41 
42 void
43 GravityView::AttachedToWindow()
44 {
45 	LockGL();
46 	BGLView::AttachedToWindow();
47 
48 	glClearDepth(1.0f);
49 
50 	glEnable(GL_BLEND);
51 	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
52 
53 	glMatrixMode(GL_PROJECTION);
54 	glLoadIdentity();
55 	gluPerspective(45.0f, Bounds().Width() / Bounds().Height(), 2.0f, 20000.0f);
56 	glMatrixMode(GL_MODELVIEW);
57 	glLoadIdentity();
58 
59 	glTranslatef(0.0f, 0.0f, -30.0f);
60 
61 	glDepthMask(GL_FALSE);
62 
63 	UnlockGL();
64 }
65 
66 
67 void
68 GravityView::DirectDraw()
69 {
70 	int32 size = 128 << fParent->Config.ParticleCount;
71 	int32 shade = fParent->Config.ShadeID;
72 
73 	// resize particle list if needed
74 	if (size > fSize)
75 		Particle::AddParticles(size, shade);
76 	else if (size < fSize)
77 		Particle::RemoveParticles(size, shade);
78 
79 	// recolor particles if needed
80 	if (shade != fShade)
81 		Particle::ColorParticles(size, shade);
82 
83 	fSize = size;
84 	fShade = shade;
85 
86 	LockGL();
87 
88 	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
89 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
90 
91 	Particle::Tick();
92 	fGravitySource->Tick();
93 
94 	SwapBuffers();
95 
96 	UnlockGL();
97 }
98 
99