• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.badlogic.gdx.tests;
18 
19 import java.util.ArrayList;
20 import java.util.List;
21 
22 import com.badlogic.gdx.Gdx;
23 import com.badlogic.gdx.graphics.Color;
24 import com.badlogic.gdx.graphics.GL20;
25 import com.badlogic.gdx.graphics.Pixmap.Format;
26 import com.badlogic.gdx.graphics.Texture;
27 import com.badlogic.gdx.graphics.g2d.Gdx2DPixmap;
28 import com.badlogic.gdx.graphics.g2d.Sprite;
29 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
30 import com.badlogic.gdx.tests.utils.GdxTest;
31 
32 public class Gdx2DTest extends GdxTest {
33 	SpriteBatch batch;
34 	List<Sprite> sprites;
35 
textureFromPixmap(Gdx2DPixmap pixmap)36 	Texture textureFromPixmap (Gdx2DPixmap pixmap) {
37 		Texture texture = new Texture(pixmap.getWidth(), pixmap.getHeight(), Format.RGB565);
38 		texture.bind();
39 		Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_2D, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0,
40 			pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());
41 		return texture;
42 	}
43 
drawToPixmap(Gdx2DPixmap pixmap)44 	void drawToPixmap (Gdx2DPixmap pixmap) {
45 		pixmap.clear(Color.rgba8888(1, 0, 0, 0.1f));
46 		pixmap.setPixel(16, 16, Color.rgba8888(0, 0, 1, 1));
47 		int clearColor = 0;
48 		int pixelColor = 0;
49 		switch (pixmap.getFormat()) {
50 		case Gdx2DPixmap.GDX2D_FORMAT_ALPHA:
51 			clearColor = Color.rgba8888(1, 1, 1, 0.1f);
52 			pixelColor = Color.rgba8888(1, 1, 1, 1);
53 			break;
54 		case Gdx2DPixmap.GDX2D_FORMAT_LUMINANCE_ALPHA:
55 			clearColor = 0x36363619; // Color.rgba8888(1, 1, 1, 0.1f);
56 			pixelColor = 0xffffff12;
57 			break;
58 		case Gdx2DPixmap.GDX2D_FORMAT_RGB565:
59 			clearColor = Color.rgba8888(1, 0, 0, 1);
60 			pixelColor = Color.rgba8888(0, 0, 1, 1);
61 			break;
62 		case Gdx2DPixmap.GDX2D_FORMAT_RGB888:
63 			clearColor = Color.rgba8888(1, 0, 0, 1);
64 			pixelColor = Color.rgba8888(0, 0, 1, 1);
65 			break;
66 		case Gdx2DPixmap.GDX2D_FORMAT_RGBA4444:
67 			clearColor = 0xff000011;
68 			pixelColor = Color.rgba8888(0, 0, 1, 1);
69 			break;
70 		case Gdx2DPixmap.GDX2D_FORMAT_RGBA8888:
71 			clearColor = Color.rgba8888(1, 0, 0, 0.1f);
72 			pixelColor = Color.rgba8888(0, 0, 1, 1);
73 
74 		}
75 		if (pixmap.getPixel(15, 16) != clearColor) throw new RuntimeException("error clear: " + pixmap.getFormatString());
76 		if (pixmap.getPixel(16, 16) != pixelColor) throw new RuntimeException("error pixel: " + pixmap.getFormatString());
77 		pixmap.drawLine(0, 0, 31, 31, Color.rgba8888(1, 1, 1, 1));
78 		pixmap.drawRect(10, 10, 5, 7, Color.rgba8888(1, 1, 0, 0.5f));
79 		pixmap.fillRect(20, 10, 5, 7, Color.rgba8888(0, 1, 1, 0.5f));
80 		pixmap.drawCircle(16, 16, 10, Color.rgba8888(1, 0, 1, 1));
81 		pixmap.fillCircle(16, 16, 6, Color.rgba8888(0, 1, 0, 0.5f));
82 		pixmap.fillTriangle(16, 16, 18, 18, 20, 14, Color.rgba8888(0, 0.5f, 0, 0.5f));
83 		pixmap.drawLine(0, -1, 0, 0, Color.rgba8888(1, 1, 0, 1));
84 		pixmap.drawLine(41, -10, 31, 0, Color.rgba8888(1, 1, 0, 1));
85 		pixmap.drawLine(10, 41, 0, 31, Color.rgba8888(0, 1, 1, 1));
86 		pixmap.drawLine(41, 41, 31, 31, Color.rgba8888(0, 1, 1, 1));
87 
88 		pixmap.drawRect(-10, -10, 20, 20, Color.rgba8888(0, 1, 1, 1));
89 		pixmap.drawRect(21, -10, 20, 20, Color.rgba8888(0, 1, 1, 1));
90 		pixmap.drawRect(-10, 21, 20, 20, Color.rgba8888(0, 1, 1, 1));
91 		pixmap.drawRect(21, 21, 20, 20, Color.rgba8888(0, 1, 1, 1));
92 
93 		pixmap.fillRect(-10, -10, 20, 20, Color.rgba8888(0, 1, 1, 0.5f));
94 		pixmap.fillRect(21, -10, 20, 20, Color.rgba8888(0, 1, 1, 0.5f));
95 		pixmap.fillRect(-10, 21, 20, 20, Color.rgba8888(0, 1, 1, 0.5f));
96 		pixmap.fillRect(21, 21, 20, 20, Color.rgba8888(0, 1, 1, 0.5f));
97 	}
98 
testPixmaps()99 	Gdx2DPixmap[] testPixmaps () {
100 		int[] formats = {Gdx2DPixmap.GDX2D_FORMAT_ALPHA, Gdx2DPixmap.GDX2D_FORMAT_LUMINANCE_ALPHA, Gdx2DPixmap.GDX2D_FORMAT_RGB565,
101 			Gdx2DPixmap.GDX2D_FORMAT_RGB888, Gdx2DPixmap.GDX2D_FORMAT_RGBA4444, Gdx2DPixmap.GDX2D_FORMAT_RGBA8888};
102 
103 		Gdx2DPixmap[] pixmaps = new Gdx2DPixmap[formats.length];
104 		for (int i = 0; i < pixmaps.length; i++) {
105 			Gdx2DPixmap pixmap = new Gdx2DPixmap(64, 32, formats[i]);
106 			drawToPixmap(pixmap);
107 			pixmaps[i] = pixmap;
108 		}
109 		return pixmaps;
110 	}
111 
112 	@Override
create()113 	public void create () {
114 		batch = new SpriteBatch();
115 		sprites = new ArrayList<Sprite>();
116 		Gdx2DPixmap[] pixmaps = testPixmaps();
117 
118 		Gdx2DPixmap composite = new Gdx2DPixmap(512, 256, Gdx2DPixmap.GDX2D_FORMAT_RGBA8888);
119 		composite.clear(0);
120 		Gdx2DPixmap.setBlend(Gdx2DPixmap.GDX2D_BLEND_NONE);
121 		for (int i = 0; i < pixmaps.length; i++) {
122 			Gdx2DPixmap.setScale(Gdx2DPixmap.GDX2D_SCALE_NEAREST);
123 			composite.drawPixmap(pixmaps[i], 0, 0, 32, 32, i * 64, 0, 64, 64);
124 			composite.drawPixmap(pixmaps[i], 0, 0, 32, 32, i * 64, 64, 16, 16);
125 			composite.drawPixmap(pixmaps[i], 0, 0, 32, 32, i * 64, 0, 64, 64);
126 			composite.drawPixmap(pixmaps[i], 0, 0, 32, 32, i * 64, 64, 16, 16);
127 			Gdx2DPixmap.setScale(Gdx2DPixmap.GDX2D_SCALE_LINEAR);
128 			composite.drawPixmap(pixmaps[i], 0, 0, 32, 32, i * 64, 100, 64, 64);
129 			composite.drawPixmap(pixmaps[i], 0, 0, 32, 32, i * 64, 164, 16, 16);
130 			composite.drawPixmap(pixmaps[i], 0, 0, 32, 32, i * 64, 100, 64, 64);
131 			composite.drawPixmap(pixmaps[i], 0, 0, 32, 32, i * 64, 164, 16, 16);
132 			Sprite sprite = new Sprite(textureFromPixmap(pixmaps[i]));
133 			sprite.setPosition(10 + i * 32, 10);
134 			sprites.add(sprite);
135 		}
136 
137 		Sprite sprite = new Sprite(textureFromPixmap(composite));
138 		sprite.setPosition(10, 50);
139 		sprites.add(sprite);
140 	}
141 
142 	@Override
render()143 	public void render () {
144 		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
145 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
146 		batch.begin();
147 		for (int i = 0; i < sprites.size(); i++) {
148 			sprites.get(i).draw(batch);
149 		}
150 		batch.end();
151 	}
152 }
153