summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/Display.java
blob: 6fab3ea8b1082f99f92e8bb17b5a0bd221508b61 (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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle;

import com.fourisland.fourpuzzle.transition.Transition;
import com.fourisland.fourpuzzle.transition.TransitionCallbackThread;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.VolatileImage;
import javax.swing.JDialog;

/**
 *
 * @author hatkirby
 */
public class Display {
    
    public static int tileAnimationFrame = 0;

    public static void render(JDialog gameFrame)
    {
        if (enabled)
        {
            VolatileImage vImg = gameFrame.createVolatileImage(Game.WIDTH, Game.HEIGHT);
            render(gameFrame, vImg);

            Image img = null;
            do
            {
                int returnCode = vImg.validate(gameFrame.getGraphicsConfiguration());
                if (returnCode == VolatileImage.IMAGE_RESTORED)
                {
                    render(gameFrame, vImg);
                } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE)
                {
                    vImg = gameFrame.createVolatileImage(Game.WIDTH, Game.HEIGHT);
                    render(gameFrame, vImg);
                }

                img = vImg;
            } while (vImg.contentsLost());

            gameFrame.getContentPane().getGraphics().drawImage(img, 0, 0, gameFrame.getContentPane().getWidth(), gameFrame.getContentPane().getHeight(), gameFrame);
            img.flush();
            Toolkit.getDefaultToolkit().sync();

            if (tileAnimationFrame == 15)
            {
                tileAnimationFrame = 0;
            } else {
                tileAnimationFrame++;
            }
        }
    }

    private static void render(JDialog gameFrame, VolatileImage vImg)
    {
        if (vImg.validate(gameFrame.getGraphicsConfiguration()) == VolatileImage.IMAGE_INCOMPATIBLE)
        {
            vImg = gameFrame.createVolatileImage(Game.WIDTH, Game.HEIGHT);
        }

        Graphics2D g = vImg.createGraphics();
        
        if (transition != null)
        {
            transition.render(g);
        }
        
        Game.getGameState().render(g);
        g.dispose();
    }
    
    public static void transition(Transition transition, Runnable callback)
    {
        setTransition(transition);
        
        new Thread(new TransitionCallbackThread(callback)).start();
    }
    
    private static Transition transition;
    public static Transition getTransition()
    {
        return transition;
    }
    public static void setTransition(Transition transition)
    {
        Display.transition = transition;
    }
    
    private static boolean enabled = true;
    public static boolean isEnabled()
    {
        return enabled;
    }
    public static void setEnabled(boolean aEnabled)
    {
        enabled = aEnabled;
    }
    
}