1/*******************************************************************************
2 * Copyright 2011 See AUTHORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16
17package com.badlogic.gdx.tests;
18
19import com.badlogic.gdx.Gdx;
20import com.badlogic.gdx.Input;
21import com.badlogic.gdx.InputProcessor;
22import com.badlogic.gdx.graphics.GL20;
23import com.badlogic.gdx.graphics.g2d.ParticleEffect;
24import com.badlogic.gdx.graphics.g2d.ParticleEmitter;
25import com.badlogic.gdx.graphics.g2d.SpriteBatch;
26import com.badlogic.gdx.tests.utils.GdxTest;
27import com.badlogic.gdx.utils.Array;
28
29public class ParticleEmitterTest extends GdxTest {
30	private SpriteBatch spriteBatch;
31	ParticleEffect effect;
32	int emitterIndex;
33	Array<ParticleEmitter> emitters;
34	int particleCount = 10;
35	float fpsCounter;
36	InputProcessor inputProcessor;
37
38	@Override
39	public void create () {
40		spriteBatch = new SpriteBatch();
41
42		effect = new ParticleEffect();
43		effect.load(Gdx.files.internal("data/test.p"), Gdx.files.internal("data"));
44		effect.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
45		// Of course, a ParticleEffect is normally just used, without messing around with its emitters.
46		emitters = new Array(effect.getEmitters());
47		effect.getEmitters().clear();
48		effect.getEmitters().add(emitters.get(0));
49
50		inputProcessor = new InputProcessor() {
51			public boolean touchUp (int x, int y, int pointer, int button) {
52				return false;
53			}
54
55			public boolean touchDragged (int x, int y, int pointer) {
56				effect.setPosition(x, Gdx.graphics.getHeight() - y);
57				return false;
58			}
59
60			public boolean touchDown (int x, int y, int pointer, int newParam) {
61				// effect.setPosition(x, Gdx.graphics.getHeight() - y);
62				ParticleEmitter emitter = emitters.get(emitterIndex);
63				particleCount += 100;
64				System.out.println(particleCount);
65				particleCount = Math.max(0, particleCount);
66				if (particleCount > emitter.getMaxParticleCount()) emitter.setMaxParticleCount(particleCount * 2);
67				emitter.getEmission().setHigh(particleCount / emitter.getLife().getHighMax() * 1000);
68				effect.getEmitters().clear();
69				effect.getEmitters().add(emitter);
70				return false;
71			}
72
73			public boolean keyUp (int keycode) {
74				return false;
75			}
76
77			public boolean keyTyped (char character) {
78				return false;
79			}
80
81			public boolean keyDown (int keycode) {
82				ParticleEmitter emitter = emitters.get(emitterIndex);
83				if (keycode == Input.Keys.DPAD_UP)
84					particleCount += 5;
85				else if (keycode == Input.Keys.DPAD_DOWN)
86					particleCount -= 5;
87				else if (keycode == Input.Keys.SPACE) {
88					emitterIndex = (emitterIndex + 1) % emitters.size;
89					emitter = emitters.get(emitterIndex);
90
91					// if we've previously stopped the emitter reset it
92					if (emitter.isComplete()) emitter.reset();
93					particleCount = (int)(emitter.getEmission().getHighMax() * emitter.getLife().getHighMax() / 1000f);
94				} else if (keycode == Input.Keys.ENTER) {
95					emitter = emitters.get(emitterIndex);
96					if (emitter.isComplete())
97						emitter.reset();
98					else
99						emitter.allowCompletion();
100				} else
101					return false;
102				particleCount = Math.max(0, particleCount);
103				if (particleCount > emitter.getMaxParticleCount()) emitter.setMaxParticleCount(particleCount * 2);
104				emitter.getEmission().setHigh(particleCount / emitter.getLife().getHighMax() * 1000);
105				effect.getEmitters().clear();
106				effect.getEmitters().add(emitter);
107				return false;
108			}
109
110			@Override
111			public boolean mouseMoved (int x, int y) {
112				return false;
113			}
114
115			@Override
116			public boolean scrolled (int amount) {
117				return false;
118			}
119		};
120
121		Gdx.input.setInputProcessor(inputProcessor);
122	}
123
124	@Override
125	public void dispose () {
126		spriteBatch.dispose();
127		effect.dispose();
128	}
129
130	public void render () {
131		spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
132		float delta = Gdx.graphics.getDeltaTime();
133		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
134		spriteBatch.begin();
135		effect.draw(spriteBatch, delta);
136		spriteBatch.end();
137		fpsCounter += delta;
138		if (fpsCounter > 3) {
139			fpsCounter = 0;
140			int activeCount = emitters.get(emitterIndex).getActiveCount();
141			Gdx.app.log("libgdx", activeCount + "/" + particleCount + " particles, FPS: " + Gdx.graphics.getFramesPerSecond());
142		}
143	}
144
145	public boolean needsGL20 () {
146		return false;
147	}
148}
149