aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/texture/TextureManager.java
blob: afe5da3591e9eb59bb8d810e3358a700335f1c40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ru.olamedia.texture;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import javax.media.opengl.GLProfile;

import ru.olamedia.asset.AssetManager;
import ru.olamedia.asset.AssetNotFoundException;
import ru.olamedia.asset.Sprite;
import ru.olamedia.asset.SpriteRectangle;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;
import com.jogamp.opengl.util.texture.awt.AWTTextureIO;

public class TextureManager {
	private static HashMap<String, Texture> list = new HashMap<String, Texture>();
	private static HashMap<String, Sprite> currentSpriteList = new HashMap<String, Sprite>();
	private static HashMap<String, SpriteRectangle> offsets = new HashMap<String, SpriteRectangle>();
	private static Sprite currentSprite = new Sprite(256, 256, 32, 32); // 64
																		// tiles

	public static void writeSprite(String filename) {
		currentSprite.write(filename);
	}

	public static Texture get(String filename) {
		if (!list.containsKey(filename)) {
			try {
				return get(filename, AssetManager.getAsset(filename).getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			} catch (AssetNotFoundException e) {
				e.printStackTrace();
				return null;
			}
		}
		return list.get(filename);
	}

	public static Texture get(String key, InputStream stream) {
		if (!list.containsKey(key)) {
			try {
				list.put(key, TextureIO.newTexture(stream, true, "PNG"));
			} catch (IOException e) {
				list.put(key, null);
				e.printStackTrace();
			}
		}
		return list.get(key);
	}

	public static Texture get(String key, InputStream stream, String format) {
		if (!list.containsKey(key)) {
			try {
				list.put(key, TextureIO.newTexture(stream, true, format));
			} catch (IOException e) {
				list.put(key, null);
				e.printStackTrace();
			}
		}
		return list.get(key);
	}

	public static Texture makeTextureFromBufferedImage(BufferedImage b) {
		return null;
	}

	public static Texture getSprite(String filename) {
		if (!list.containsKey(filename)) {
			try {
				SpriteRectangle offset = currentSprite.addImage(filename);
				offsets.put(filename, offset);
				currentSpriteList.put(filename, currentSprite);
			} catch (AssetNotFoundException e) {
				e.printStackTrace();
			}
			return get(filename);
		}
		return list.get(filename);
	}

	public static SpriteRectangle getSpriteOffset(String filename) {
		if (!offsets.containsKey(filename)) {
			getSprite(filename);
			if (!offsets.containsKey(filename)) {
				return null;
			}
		}
		return offsets.get(filename);
	}

	public static void finishSprite() {
		GLProfile gp = GLProfile.getGL2ES2();
		Texture texture = AWTTextureIO.newTexture(gp, currentSprite.getImage(), true);
		currentSprite.dispose();
		for (String key : currentSpriteList.keySet()) {
			list.put(key, texture);
		}
		currentSpriteList.clear();
	}
}