• 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.io.InputStream;
20 import java.net.HttpURLConnection;
21 import java.net.URL;
22 
23 import com.badlogic.gdx.Gdx;
24 import com.badlogic.gdx.graphics.GL20;
25 import com.badlogic.gdx.graphics.Pixmap;
26 import com.badlogic.gdx.graphics.Texture;
27 import com.badlogic.gdx.graphics.g2d.BitmapFont;
28 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
29 import com.badlogic.gdx.graphics.g2d.TextureRegion;
30 import com.badlogic.gdx.math.MathUtils;
31 import com.badlogic.gdx.tests.utils.GdxTest;
32 import com.badlogic.gdx.utils.StreamUtils;
33 
34 public class TextureDownloadTest extends GdxTest {
35 	TextureRegion image;
36 	BitmapFont font;
37 	SpriteBatch batch;
38 
39 	@Override
create()40 	public void create () {
41 		new Thread(new Runnable() {
42 			/** Downloads the content of the specified url to the array. The array has to be big enough. */
43 			private int download (byte[] out, String url) {
44 				InputStream in = null;
45 				try {
46 					HttpURLConnection conn = null;
47 					conn = (HttpURLConnection)new URL(url).openConnection();
48 					conn.setDoInput(true);
49 					conn.setDoOutput(false);
50 					conn.setUseCaches(true);
51 					conn.connect();
52 					in = conn.getInputStream();
53 					int readBytes = 0;
54 					while (true) {
55 						int length = in.read(out, readBytes, out.length - readBytes);
56 						if (length == -1) break;
57 						readBytes += length;
58 					}
59 					return readBytes;
60 				} catch (Exception ex) {
61 					return 0;
62 				} finally {
63 					StreamUtils.closeQuietly(in);
64 				}
65 			}
66 
67 			@Override
68 			public void run () {
69 				byte[] bytes = new byte[200 * 1024]; // assuming the content is not bigger than 200kb.
70 				int numBytes = download(bytes, "http://www.badlogicgames.com/wordpress/wp-content/uploads/2012/01/badlogic-new.png");
71 				if (numBytes != 0) {
72 					// load the pixmap, make it a power of two if necessary (not needed for GL ES 2.0!)
73 					Pixmap pixmap = new Pixmap(bytes, 0, numBytes);
74 					final int originalWidth = pixmap.getWidth();
75 					final int originalHeight = pixmap.getHeight();
76 					int width = MathUtils.nextPowerOfTwo(pixmap.getWidth());
77 					int height = MathUtils.nextPowerOfTwo(pixmap.getHeight());
78 					final Pixmap potPixmap = new Pixmap(width, height, pixmap.getFormat());
79 					potPixmap.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight());
80 					pixmap.dispose();
81 					Gdx.app.postRunnable(new Runnable() {
82 						@Override
83 						public void run () {
84 							image = new TextureRegion(new Texture(potPixmap), 0, 0, originalWidth, originalHeight);
85 						}
86 					});
87 				}
88 			}
89 		}).start();
90 
91 		font = new BitmapFont();
92 		batch = new SpriteBatch();
93 	}
94 
95 	@Override
render()96 	public void render () {
97 		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
98 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
99 
100 		if (image != null) {
101 			batch.begin();
102 			batch.draw(image, 100, 100);
103 			batch.end();
104 		} else {
105 			batch.begin();
106 			font.draw(batch, "Downloading...", 100, 100);
107 			batch.end();
108 		}
109 	}
110 }
111