xref: /haiku/src/add-ons/screen_savers/gravity/GravityView.cpp (revision a085e81e62d7a860f809b4fb7c7bf5654c396985)
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 "GravityView.h"
8 
9 #include "Gravity.h"
10 #include "GravitySource.h"
11 #include "Particle.h"
12 
13 #include <GL/glu.h>
14 
15 
16 GravityView::GravityView(Gravity* parent, BRect rect)
17 	:
18 	BGLView(rect, B_EMPTY_STRING, B_FOLLOW_NONE, 0,
19 		BGL_RGB | BGL_DEPTH | BGL_DOUBLE)
20 {
21 	fParent = parent;
22 
23 	int realCount;
24 
25 	if (parent->Config.ParticleCount == 0)
26 		realCount = 128;
27 	else if (parent->Config.ParticleCount == 1)
28 		realCount = 256;
29 	else if (parent->Config.ParticleCount == 2)
30 		realCount = 512;
31 	else if (parent->Config.ParticleCount == 3)
32 		realCount = 1024;
33 	else if (parent->Config.ParticleCount == 4)
34 		realCount = 2048;
35 	else
36 		realCount = 128;
37 
38 	Particle::Initialize(realCount, parent->Config.ShadeID);
39 
40 	LockGL();
41 
42 	glClearDepth(1.0f);
43 
44 	glEnable(GL_BLEND);
45 	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
46 
47 	glMatrixMode(GL_PROJECTION);
48 	glLoadIdentity();
49 	gluPerspective(45.0f, rect.Width() / rect.Height(), 2.0f, 20000.0f);
50 	glMatrixMode(GL_MODELVIEW);
51 	glLoadIdentity();
52 
53 	glTranslatef(0.0f, 0.0f, -30.0f);
54 
55 	glDepthMask(GL_FALSE);
56 
57 	UnlockGL();
58 
59 	fGravSource = new GravitySource();
60 }
61 
62 
63 GravityView::~GravityView()
64 {
65 	Particle::Terminate();
66 	delete fGravSource;
67 }
68 
69 
70 void
71 GravityView::AttachedToWindow()
72 {
73 	LockGL();
74 	BGLView::AttachedToWindow();
75 	UnlockGL();
76 }
77 
78 
79 void
80 GravityView::DirectDraw()
81 {
82 	LockGL();
83 
84 	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
85 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
86 
87 	Particle::Tick();
88 	fGravSource->Tick();
89 
90 	SwapBuffers();
91 
92 	UnlockGL();
93 }
94 
95