package chip8emu;
/*
This file is part of JavaCHIP8.
Copyright 2004 Kustaa Nyholm / SpareTimeLabs
JavaCHIP8 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
JavaCHIP8 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with JavaCHIP8; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import javax.microedition.media.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class Chip8Screen
extends GameCanvas
implements Chip8IO, CommandListener {
public int width;
public int height;
private Command exitCommand;
private Command resetCommand;
private Player beepPlayer;
private Chip8MIDlet midlet;
private Graphics graphics;
private int screenLeft;
private int screenTop;
private int keyMap[] = {
-1, 1, 2, 3, 4, 5, 6, 7, 8, 9};
public void drawPixels(int[] pixels) {
graphics.drawRGB(pixels, 0, 128, screenLeft, screenTop, 128, 64, false);
flushGraphics();
}
public Chip8Screen(Chip8MIDlet let) {
super(false);
addCommand(exitCommand = new Command("Exit", Command.BACK, 1));
addCommand(resetCommand = new Command("Restart", Command.SCREEN, 2));
midlet = let;
setCommandListener(this);
beepPlayer = beepPlayer = Chip8MIDlet.createPlayer("beep.wav",
"audio/x-wav");
beep();
setFullScreenMode(true);
graphics = getGraphics();
width = getWidth();
height = getHeight();
screenLeft = (width - 128) / 2;
screenTop = (height - 64) / 2;
graphics.setColor(0);
graphics.fillRect(0, 0, width, height);
}
public void beep() {
Chip8MIDlet.startPlayer(beepPlayer);
}
public void configureKeys(String c) {
int l = c.length();
for (int i = 1; i <= 9; ++i) {
keyMap[i] = i;
}
if (l > 0) {
for (int i = 1; i <= 9; ++i) {
keyMap[i] = i;
if (i < l) {
char ch = c.charAt(i - 1);
keyMap[i] = (ch == '.') ? -1 : ((ch < 'A') ? ch - '0' : ch - 'A' + 10);;
System.out.println("Map " + i + " => " + keyMap[i]);
}
}
}
}
public int getKeys() {
int s = getKeyStates();
int k = 0;
if ( (s & 512) != 0) {
k = 1;
}
if ( (s & 2) != 0) {
k = 2;
}
if ( (s & 1024) != 0) {
k = 3;
}
if ( (s & 4) != 0) {
k = 4;
}
if ( (s & 256) != 0) {
k = 5;
}
if ( (s & 32) != 0) {
k = 6;
}
if ( (s & 2048) != 0) {
k = 7;
}
if ( (s & 64) != 0) {
k = 8;
}
if ( (s & 4096) != 0) {
k = 9;
}
return keyMap[k];
}
public Graphics getGraphics() {
return super.getGraphics();
}
public void flushPixels() {
super.flushGraphics();
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
midlet.emulator.stop();
midlet.gotoGamesScreen();
}
if (c == resetCommand) {
midlet.emulator.start(0);
}
}
}
|