import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.nio.*;
import java.util.*;
import javax.imageio.ImageIO;
import net.java.games.jogl.*;
import samuuo.witchlet.*;
public class Yaa extends Frame implements GLEventListener, WonderSwanListener {
private WonderSwan wonderSwan;
private Animator animator;
private int textureName;
private double eyex = 0.0;
private double eyey = 10.0;
private double eyez = 2.0;
private double centerY = -500.0;
private float rotate;
private double sinr;
private double cosr = 1.0;
private static final float MAP_WIDTH = 300.0f;
private static final float MAP_HEIGHT = 300.0f;
public Yaa(Properties props) throws Exception {
super("やあ");
wonderSwan = new WonderSwan(props);
GLCapabilities glc = new GLCapabilities();
Button openButton = new Button("Open");
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open();
}
});
Button closeButton = new Button("Close");
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
close();
}
});
Panel buttonPanel = new Panel(new FlowLayout());
buttonPanel.add(openButton);
buttonPanel.add(closeButton);
GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(glc);
canvas.setSize(800, 600);
canvas.addGLEventListener(this);
setLayout(new BorderLayout());
add(BorderLayout.CENTER, canvas);
add(BorderLayout.SOUTH, buttonPanel);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
quit();
}
});
pack();
setVisible(true);
animator = new Animator(canvas);
animator.start();
}
private void open() {
try {
wonderSwan.open();
wonderSwan.addWonderSwanKeyListener(this);
System.out.println("Serial port has opened");
} catch (WonderSwanException e) {
if (e.getTargetException() != null) {
e.getTargetException().printStackTrace();
} else {
e.printStackTrace();
}
}
}
private void close() {
if (wonderSwan != null) {
wonderSwan.removeWonderSwanKeyListener(this);
wonderSwan.close();
System.out.println("Serial port has closed");
}
}
private void quit() {
close();
animator.stop();
System.exit(0);
}
public void keyPressed(int key) {
float rotate = this.rotate;
double eyex = this.eyex;
double eyey = this.eyey;
double eyez = this.eyez;
double centerY = this.centerY;
double sinr = Math.sin(Math.PI * rotate);
double cosr = Math.cos(Math.PI * rotate);
boolean isFast = (key & WonderSwan.KEY_A) != 0;
if ((key & WonderSwan.KEY_B) != 0) {
}
if ((key & WonderSwan.KEY_Y1) != 0) {
eyey += 0.1;
} else if ((key & WonderSwan.KEY_Y3) != 0) {
eyey -= 0.1;
}
if ((key & WonderSwan.KEY_Y2) != 0) {
centerY -= 4.0;
} else if ((key & WonderSwan.KEY_Y4) != 0) {
centerY += 4.0;
}
if ((key & WonderSwan.KEY_X1) != 0) {
if (isFast) {
eyex -= 0.4 * sinr;
eyez -= 0.4 * cosr;
} else {
eyex -= 0.1 * sinr;
eyez -= 0.1 * cosr;
}
} else if ((key & WonderSwan.KEY_X3) != 0) {
if (isFast) {
eyex += 0.4 * sinr;
eyez += 0.4 * cosr;
} else {
eyex += 0.1 * sinr;
eyez += 0.1 * cosr;
}
}
if ((key & WonderSwan.KEY_X2) != 0) {
rotate -= 0.01;
} else if ((key & WonderSwan.KEY_X4) != 0) {
rotate += 0.01;
}
this.rotate = rotate;
this.sinr = Math.sin(Math.PI * rotate);
this.cosr = Math.cos(Math.PI * rotate);
if (eyex > MAP_WIDTH / 2) {
eyex = MAP_WIDTH / 2;
} else if (eyex < - MAP_WIDTH / 2) {
eyex = - MAP_WIDTH / 2;
}
if (eyez > MAP_HEIGHT / 2) {
eyez = MAP_HEIGHT / 2;
} else if (eyez < - MAP_HEIGHT / 2) {
eyez = - MAP_HEIGHT / 2;
}
this.eyex = eyex;
this.eyey = eyey;
this.eyez = eyez;
this.centerY = centerY;
}
public void display(GLDrawable drawable) {
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(eyex, eyey, eyez,
-1000 * sinr, centerY, -1000 * cosr, 0.0, 1.0, 0.0);
if (textureName == 0) {
try {
prepareTexture(gl, "yaa.gif");
} catch (Exception e) {
e.printStackTrace();
}
}
gl.glBindTexture(GL.GL_TEXTURE_2D, textureName);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-MAP_WIDTH / 2, 0.0f, MAP_HEIGHT / 2);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f( MAP_WIDTH / 2, 0.0f, MAP_HEIGHT / 2);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f( MAP_WIDTH / 2, 0.0f, -MAP_HEIGHT / 2);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-MAP_WIDTH / 2, 0.0f, -MAP_HEIGHT / 2);
gl.glEnd();
gl.glFlush();
}
public void displayChanged(
GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
public void init(GLDrawable drawable) {
GL gl = drawable.getGL();
gl.glClearColor(0.0f, 0.4f, 0.75f, 1.0f);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glTexEnvf(
GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
}
public void reshape(
GLDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(90, (double)width / height, 0.1, 30);
}
private static ColorModel GL_ALPHA_COLOR_MODEL = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 },
true, false, ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
private static ColorModel GL_COLOR_MODEL = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 0 },
false, false, ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
private void prepareTexture(GL gl, String name) throws Exception {
BufferedImage image = ImageIO.read(Yaa.class.getResource('/' + name));
int texWidth = 2;
int texHeight = 2;
while (texWidth < image.getWidth()) {
texWidth *= 2;
}
while (texHeight < image.getHeight()) {
texHeight *= 2;
}
BufferedImage texImage;
int format;
if (image.getColorModel().hasAlpha()) {
WritableRaster raster = Raster.createInterleavedRaster(
DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
texImage = new BufferedImage(
GL_ALPHA_COLOR_MODEL, raster, false, new Hashtable());
format = GL.GL_RGBA;
} else {
WritableRaster raster = Raster.createInterleavedRaster(
DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null);
texImage = new BufferedImage(
GL_COLOR_MODEL, raster, false, new Hashtable());
format = GL.GL_RGB;
}
Graphics g = texImage.getGraphics();
g.setColor(new Color(0.0f, 0.0f, 0.0f, 0.0f));
g.fillRect(0, 0, texWidth, texHeight);
g.drawImage(image, 0, 0, null);
g.dispose();
byte[] data =
((DataBufferByte)texImage.getRaster().getDataBuffer()).getData();
texImage.flush();
ByteBuffer imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
int[] tmp = new int[1];
gl.glGenTextures(1, tmp);
textureName = tmp[0];
gl.glBindTexture(GL.GL_TEXTURE_2D, textureName);
gl.glTexParameteri(
GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
gl.glTexParameteri(
GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
gl.glTexParameteri(
GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
gl.glTexParameteri(
GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA,
image.getWidth(), image.getHeight(),
0, format, GL.GL_UNSIGNED_BYTE, imageBuffer);
}
public static void main(String[] args) throws Exception {
InputStream in = Yaa.class.getResourceAsStream("/witchlet.properties");
Properties props = new Properties();
props.load(in);
in.close();
Yaa applet = new Yaa(props);
}
}
|
最近のコメント