-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainApplication.java
95 lines (60 loc) · 2.15 KB
/
MainApplication.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
// INTERNAL JAVA CLASSES IMPORT:
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.event.*;
import java.io.InputStream;
// CUSTOM CLASSES IMPORT:
import gamesource.main.*;
public class MainApplication extends Application
{
public static final int SCREEN_WIDTH = 1280;
public static final int SCREEN_HEIGHT = 854;
private Pane _myPane = new Pane();
private Scene _myScene;
private Canvas _myCanvas;
private GraphicsContext _myContext;
private CustomAnimationTimer _animationTimer;
private GameManager _gameManager;
public static void main(String[]args) {
launch(args);
}
public void start(Stage primaryStage)
{
_myScene = new Scene(_myPane);
_myCanvas = new Canvas(SCREEN_WIDTH, SCREEN_HEIGHT);
_myContext = _myCanvas.getGraphicsContext2D();
_myPane.getChildren().add(_myCanvas);
// REGISTER MOUSE & KEYBOARD INPUT:
_myPane.addEventHandler(MouseEvent.MOUSE_PRESSED, Input.OnMousePressed);
_myPane.addEventHandler(MouseEvent.MOUSE_RELEASED, Input.OnMouseReleased);
_myPane.addEventHandler(MouseEvent.MOUSE_MOVED, Input.OnMouseMove);
_myPane.addEventHandler(MouseEvent.MOUSE_DRAGGED, Input.OnMouseMove);
_myPane.addEventHandler(KeyEvent.KEY_PRESSED, Input.OnKeyPressed);
_myPane.addEventHandler(KeyEvent.KEY_RELEASED, Input.OnKeyReleased);
_myPane.requestFocus();
_gameManager = new GameManager(_myPane);
primaryStage.setScene(_myScene);
primaryStage.setTitle("Dynavoid 0.1.1");
primaryStage.setResizable(false);
primaryStage.sizeToScene();
primaryStage.show();
_animationTimer = new CustomAnimationTimer(this::GameLoop);
_animationTimer.start();
}
public void GameLoop(long currentNanoTime)
{
// UPDATE THE GAME STATE:
_gameManager.Update();
// RESET THE CURRENT PIXEL BUFFER, THEN DRAW THE CURRENT STATE OF THE GAME:
_myContext.clearRect(0, 0, SCREEN_WIDTH, SCREEN_WIDTH);
_gameManager.Draw();
_gameManager.DrawGUI();
// CLEAR RELEASE INPUT FOR NEXT ITERATION:
Input.KEYS_RELEASED.clear();
}
}