rename vtm-gdx-dektop to vtm-desktop

This commit is contained in:
Hannes Janetzek
2014-03-21 03:29:41 +01:00
parent 211efc4d5a
commit f224486e70
8 changed files with 1 additions and 1 deletions

View File

@@ -0,0 +1,138 @@
/*
* Copyright 2013 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.awt;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.IntBuffer;
import javax.imageio.ImageIO;
import org.oscim.backend.canvas.Bitmap;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.utils.BufferUtils;
public class AwtBitmap implements Bitmap {
BufferedImage bitmap;
int width;
int height;
boolean internal;
public AwtBitmap(int width, int height, int format) {
bitmap = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
this.width = width;
this.height = height;
internal = true;
// if (!this.bitmap.isAlphaPremultiplied())
// this.bitmap.coerceData(true);
}
AwtBitmap(InputStream inputStream) throws IOException {
this.bitmap = ImageIO.read(inputStream);
this.width = this.bitmap.getWidth();
this.height = this.bitmap.getHeight();
if (!this.bitmap.isAlphaPremultiplied()
&& this.bitmap.getType() == BufferedImage.TYPE_INT_ARGB)
this.bitmap.coerceData(true);
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
@Override
public int[] getPixels() {
return null;
}
@Override
public void eraseColor(int transparent) {
}
private final static IntBuffer tmpBuffer = BufferUtils.newIntBuffer(512 * 256);
private final static int[] tmpPixel = new int[512 * 256];
private final static boolean WRITE_TEX = false;
private int dbgCnt;
@Override
public void uploadToTexture(boolean replace) {
int[] pixels;
IntBuffer buffer;
if (width * height < 512 * 256) {
pixels = tmpPixel;
buffer = tmpBuffer;
buffer.clear();
} else {
pixels = new int[width * height];
buffer = BufferUtils.newIntBuffer(width * height);
}
// FIXME dont convert to argb when there data is greyscale
bitmap.getRGB(0, 0, width, height, pixels, 0, width);
if (WRITE_TEX) {
try {
boolean ok = ImageIO.write(bitmap, "png", new File("texture_" + dbgCnt + ".png"));
System.out.println("write tex " + ok + " " + dbgCnt);
dbgCnt++;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int i = 0, n = width * height; i < n; i++) {
int c = pixels[i];
float alpha = (c >>> 24) / 255f;
int r = (int) ((c & 0x000000ff) * alpha);
int b = (int) (((c & 0x00ff0000) >>> 16) * alpha);
int g = (int) (((c & 0x0000ff00) >>> 8) * alpha);
pixels[i] = (c & 0xff000000) | r << 16 | g << 8 | b;
}
buffer.put(pixels, 0, width * height);
buffer.flip();
Gdx.gl20.glTexImage2D(GL20.GL_TEXTURE_2D, 0, GL20.GL_RGBA, width,
height, 0, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, buffer);
}
@Override
public void recycle() {
}
@Override
public boolean isValid() {
return true;
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
* Copyright 2013 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.awt;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.backend.canvas.Canvas;
import org.oscim.backend.canvas.Paint;
public class AwtCanvas implements Canvas {
Graphics2D canvas;
public AwtCanvas() {
}
@Override
public void setBitmap(Bitmap bitmap) {
if (canvas != null)
canvas.dispose();
AwtBitmap awtBitamp = (AwtBitmap) bitmap;
canvas = awtBitamp.bitmap.createGraphics();
canvas.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0));
canvas.fillRect(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
canvas.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//canvas.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
canvas.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
canvas.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
@Override
public void drawText(String text, float x, float y, Paint paint) {
// if (paint.isTransparent()) {
// return;
// }
AwtPaint awtPaint = (AwtPaint) paint;
//AwtPaint awtPaint = AwtGraphicFactory.getAwtPaint(paint);
if (awtPaint.stroke == null) {
canvas.setColor(awtPaint.color);
canvas.setFont(awtPaint.font);
canvas.drawString(text, x + 2, y);
} else {
setColorAndStroke(awtPaint);
TextLayout textLayout = new TextLayout(text,
awtPaint.font,
canvas.getFontRenderContext());
AffineTransform affineTransform = new AffineTransform();
affineTransform.translate(x + 2, y);
canvas.draw(textLayout.getOutline(affineTransform));
}
}
private void setColorAndStroke(AwtPaint awtPaint) {
canvas.setColor(awtPaint.color);
if (awtPaint.stroke != null) {
canvas.setStroke(awtPaint.stroke);
}
}
// @Override
// public void drawText(String string, float x, float y, Paint stroke) {
// AwtPaint p = (AwtPaint)stroke;
//
// canvas.setFont(p.font);
// canvas.setColor(p.color);
//
// canvas.drawString(string, (int)x, (int)y);
// }
@Override
public void drawBitmap(Bitmap bitmap, float x, float y) {
// TODO Auto-generated method stub
throw new UnknownError("not implemented");
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2013 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.awt;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import org.oscim.backend.CanvasAdapter;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.backend.canvas.Canvas;
import org.oscim.backend.canvas.Paint;
public class AwtGraphics extends CanvasAdapter {
private static final AwtGraphics INSTANCE = new AwtGraphics();
public static final AwtGraphics get() {
return INSTANCE;
}
private AwtGraphics() {
// do nothing
}
@Override
public Paint getPaint() {
return new AwtPaint();
}
@Override
public Bitmap getBitmap(int width, int height, int format) {
return new AwtBitmap(width, height, format);
}
@Override
public Canvas getCanvas() {
return new AwtCanvas();
}
static final BufferedImage image;
static final Graphics2D canvas;
static {
image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
canvas = image.createGraphics();
canvas.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//canvas.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
//canvas.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
}
static synchronized FontMetrics getFontMetrics(Font font) {
canvas.setFont(font);
// get character measurements
FontMetrics fm = canvas.getFontMetrics();
// int ascent = fm.getMaxAscent();
// int descent = fm.getMaxDescent();
// int advance = fm.charWidth('W'); // width of widest char, more
// reliable than getMaxAdvance();
// int leading = fm.getLeading();
//
return fm;
}
static synchronized float getTextWidth(FontMetrics fm, String text) {
//return (float)fm.getStringBounds(text, canvas).getWidth();
return fm.stringWidth(text);
}
@Override
public Bitmap decodeBitmap(InputStream inputStream) {
try {
return new AwtBitmap(inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public Bitmap loadBitmapAsset(String fileName) {
try {
return createBitmap(fileName);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

View File

@@ -0,0 +1,181 @@
/*
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
* Copyright 2013 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.awt;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Stroke;
import java.awt.font.TextAttribute;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.HashMap;
import java.util.Map;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.backend.canvas.Paint;
public class AwtPaint implements Paint {
private static int getCap(Cap cap) {
switch (cap) {
case BUTT:
return BasicStroke.CAP_BUTT;
case ROUND:
return BasicStroke.CAP_ROUND;
case SQUARE:
return BasicStroke.CAP_SQUARE;
}
throw new IllegalArgumentException("unknown cap: " + cap);
}
static final Font defaultFont;
static {
Map<Attribute, Object> textAttributes = new HashMap<Attribute, Object>();
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
textAttributes.put(TextAttribute.FAMILY, "Arial");
textAttributes.put(TextAttribute.SIZE, 14);
defaultFont = Font.getFont(textAttributes);
}
Font font = defaultFont; // new Font("Default", Font.PLAIN, 13);
Stroke stroke;
FontMetrics fm;
Color color = new Color(0.1f, 0.1f, 0.1f, 1);
private int cap;
private float strokeWidth;
//private Align mAlign;
@Override
public int getColor() {
return 0;
}
@Override
public int getTextHeight(String text) {
return 0;
}
@Override
public int getTextWidth(String text) {
return 0;
}
@Override
public void setBitmapShader(Bitmap bitmap) {
}
@Override
public void setColor(int c) {
color = new Color(((c >> 16) & 0xff) / 255f,
((c >> 8) & 0xff) / 255f,
((c >> 0) & 0xff) / 255f,
((c >> 24) & 0xff) / 255f);
}
@Override
public void setDashPathEffect(float[] strokeDasharray) {
// TODO Auto-generated method stub
}
@Override
public void setStrokeCap(Cap cap) {
this.cap = getCap(cap);
createStroke();
}
@Override
public void setStrokeWidth(float width) {
strokeWidth = width;
createStroke();
// int size = font.getSize();
// font = font.deriveFont(size + width * 4);
// TODO Auto-generated method stub
}
@Override
public void setStyle(Style style) {
// TODO Auto-generated method stub
}
@Override
public void setTextAlign(Align align) {
//mAlign = align;
}
@Override
public void setTextSize(float textSize) {
font = font.deriveFont(textSize - 2);
}
@Override
public void setTypeface(FontFamily fontFamily, FontStyle fontStyle) {
// TODO Auto-generated method stub
}
@Override
public float measureText(String text) {
if (fm == null)
fm = AwtGraphics.getFontMetrics(this.font);
float w = AwtGraphics.getTextWidth(fm, text);
//Gdx.app.log("text width:", text + " " + w);
return w + 4;
// return fm.getStringBounds(text, A).getWidth();
// return AwtGraphics.getTextWidth(fm, text);
// return fm.stringWidth(text);
}
@Override
public float getFontHeight() {
if (fm == null)
fm = AwtGraphics.getFontMetrics(this.font);
float height = fm.getHeight();
return height;
}
@Override
public float getFontDescent() {
if (fm == null)
fm = AwtGraphics.getFontMetrics(this.font);
float desc = fm.getDescent();
return desc;
}
private void createStroke() {
if (strokeWidth <= 0) {
return;
}
stroke = new BasicStroke(strokeWidth, cap, BasicStroke.JOIN_MITER, 1, null, 0);
}
}