summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/PuzzleApplication.java
blob: 13758cb206699e5db4de29753a6c818690a429ed (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fourisland.fourpuzzle;

import com.fourisland.fourpuzzle.database.Database;
import com.fourisland.fourpuzzle.database.Vocabulary;
import com.fourisland.fourpuzzle.gamestate.TitleScreenGameState;
import com.fourisland.fourpuzzle.util.Interval;
import com.fourisland.fourpuzzle.window.SystemGraphic;
import java.awt.Container;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.Semaphore;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.jdesktop.application.Application;

/**
 *
 * @author hatkirby
 */
public class PuzzleApplication extends Application {

    public static PuzzleApplication INSTANCE;
    public static boolean debugSpeed = false;
    public static boolean stretchScreen = true;
    public static boolean gameSleep = false;
    private static JDialog gameDialog = new JDialog(new JFrame(), false);
    private static Semaphore gameDialogHandler = new Semaphore(1);
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(PuzzleApplication.class, args);
    }
    
    private void initGameDialog(boolean undecorated)
    {
        gameDialogHandler.acquireUninterruptibly();
        
        gameDialog.setVisible(false);
        Container contentPane = gameDialog.getContentPane();
        gameDialog = new JDialog(new JFrame(), false);
        gameDialog.setContentPane(contentPane);
        gameDialog.setTitle(Database.getVocab(Vocabulary.Title));
        gameDialog.setSize(Game.WIDTH * 2, Game.HEIGHT * 2);
        gameDialog.setResizable(false);
        gameDialog.setUndecorated(undecorated);
        gameDialog.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().x-Game.WIDTH, GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().y-Game.HEIGHT);
        gameDialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            @Override
            public void windowActivated(WindowEvent e) {
                gameSleep = false;
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                gameSleep = true;
            }
        });
        gameDialog.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e)
            {
                if (e.getKeyCode() == KeyEvent.VK_F4)
                {
                    stretchScreen = !stretchScreen;

                    if (stretchScreen)
                    {
                        initGameDialog(true);
                        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(gameDialog);
                    } else {
                        initGameDialog(false);
                        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null);
                    }
                } else if (e.getKeyCode() == KeyEvent.VK_SHIFT)
                {
                    if (INSTANCE.getContext().getResourceMap().getBoolean("debugMode"))
                    {
                        debugSpeed = true;
                    }
                } else {
                    KeyboardInput.getKey().keyInput(e);
                }
            }

            @Override
            public void keyReleased(KeyEvent e)
            {
                if (e.getKeyCode() == KeyEvent.VK_SHIFT)
                {
                    debugSpeed = false;
                } else {
                    KeyboardInput.getKey().letGo();
                }
            }
        });
        gameDialog.setVisible(true);
        
        gameDialogHandler.release();
    }

    @Override
    protected void startup() {
        INSTANCE = this;
        
        initGameDialog(true);
        
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(gameDialog);

        new Thread(new Runnable() {
            public void run() {
                try {
                    Audio.init();
                    SystemGraphic.initalize();
                    
                    Game.setGameState(new TitleScreenGameState());
                    
                    Interval in = Interval.createTickInterval(1);
                    while (true)
                    {
                        if ((debugSpeed || in.isElapsed()) && !gameSleep)
                        {
                            if (!Display.isTransitionRunning())
                            {
                                KeyboardInput.processInput();

                                Game.getGameState().doGameCycle();
                            }

                            gameDialogHandler.acquireUninterruptibly();
                            Display.render(gameDialog.getContentPane());
                            gameDialogHandler.release();
                        }
                    }
                } catch (RuntimeException ex) {
                    reportError(ex);
                } catch (Error ex) {
                    reportError(ex);
                }
            }
        },"GameCycle").start();
    }
    
    public String getGamePackage()
    {
        return INSTANCE.getContext().getResourceMap().getString("Application.package");
    }
    
    public void reportError(Throwable ex)
    {
        if ((ex instanceof Exception) && !(ex instanceof RuntimeException))
        {
            return;
        }
        
        JFrame errorBox = new JFrame(ex.getClass().getSimpleName());
        JLabel text = new JLabel();
        text.setText("<HTML><CENTER>I'm sorry, but " + Database.getVocab(Vocabulary.Title) +
                " has run into an error and been forced to quit.<BR>Your save file has not been kept. The error was:<BR><BR>" +
                ex.getMessage() + "</CENTER>");
        if (ex instanceof Error)
        {
            text.setText(text.getText() + "<P><CENTER>We have identified this problem as a serious error in the game.");
        }
        errorBox.add(text);
        errorBox.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        errorBox.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        errorBox.pack();
        errorBox.setVisible(true);
        ex.printStackTrace();
    }
}