-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
108 lines (91 loc) · 3.51 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* @author Clément Petit (282626)
* @author Yanis Berkani (271348)
*/
package ch.epfl.gameboj.gui;
import java.io.File;
import java.util.List;
import java.util.Map;
import ch.epfl.gameboj.GameBoy;
import ch.epfl.gameboj.component.Joypad.Key;
import ch.epfl.gameboj.component.cartridge.Cartridge;
import ch.epfl.gameboj.component.lcd.LcdController;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.layout.BorderPane;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
/**
* contains the simulator's main program.
*/
public final class Main extends Application {
private final static int ENLARGEMENT_FACTOR = 2;
private static Map<KeyCode, Key> keys = Map.of(KeyCode.D, Key.RIGHT,
KeyCode.A, Key.LEFT, KeyCode.W, Key.UP, KeyCode.S, Key.DOWN,
KeyCode.O, Key.A, KeyCode.K, Key.B, KeyCode.B, Key.SELECT,
KeyCode.N, Key.START);
/**
* calls the method launch of Application with the given argument.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
/**
* Checks that only one argument has been passed to the program (the name of
* a ROM file) and finish the execution otherwise.
* Creates a Game Boy whose cartridge is obtained from the ROM file passed in argument.
* Creates the graphical interface and then displays it on the screen.
* Simulates the Game Boy by periodically updating the image displayed on
* the screen and reacting to the key presses corresponding to those of the Game Boy.
*/
@Override
public void start(Stage stage) throws Exception {
List<String> argList = getParameters().getRaw();
if (argList.size() != 1)
System.exit(1);
File romFile = new File(argList.get(0));
GameBoy gb = new GameBoy(Cartridge.ofFile(romFile));
ImageView imageView = new ImageView();
imageView.setFitWidth(LcdController.LCD_WIDTH * ENLARGEMENT_FACTOR);
imageView.setFitHeight(LcdController.LCD_HEIGHT * ENLARGEMENT_FACTOR);
imageView.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (keys.get(event.getCode()) != null)
gb.joypad().keyPressed(keys.get(event.getCode()));
}
});
imageView.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (keys.get(event.getCode()) != null)
gb.joypad().keyReleased(keys.get(event.getCode()));
}
});
BorderPane borderPane = new BorderPane(imageView);
stage.setScene(new Scene(borderPane));
stage.setTitle("Gameboj");
stage.show();
imageView.requestFocus();
long start = System.nanoTime();
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
gb.runUntil(
(long) ((now - start) * GameBoy.CYCLES_PER_NANOSECOND));
Image image = ImageConverter
.convert(gb.lcdController().currentImage());
imageView.setImage(image);
}
};
timer.start();
}
}