about summary refs log tree commit diff stats
path: root/src/com
diff options
context:
space:
mode:
Diffstat (limited to 'src/com')
-rw-r--r--src/com/fourisland/frigidearth/GameState.java14
-rw-r--r--src/com/fourisland/frigidearth/Main.java119
-rw-r--r--src/com/fourisland/frigidearth/Renderable.java16
-rw-r--r--src/com/fourisland/frigidearth/TileGameState.java54
4 files changed, 152 insertions, 51 deletions
diff --git a/src/com/fourisland/frigidearth/GameState.java b/src/com/fourisland/frigidearth/GameState.java new file mode 100644 index 0000000..335760a --- /dev/null +++ b/src/com/fourisland/frigidearth/GameState.java
@@ -0,0 +1,14 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.fourisland.frigidearth;
6
7/**
8 *
9 * @author hatkirby
10 */
11public interface GameState extends Renderable
12{
13 public void tick();
14}
diff --git a/src/com/fourisland/frigidearth/Main.java b/src/com/fourisland/frigidearth/Main.java index 2e320cc..ab4b8b6 100644 --- a/src/com/fourisland/frigidearth/Main.java +++ b/src/com/fourisland/frigidearth/Main.java
@@ -4,11 +4,19 @@
4 */ 4 */
5package com.fourisland.frigidearth; 5package com.fourisland.frigidearth;
6 6
7import java.awt.Canvas;
7import java.awt.Color; 8import java.awt.Color;
8import java.awt.Graphics; 9import java.awt.Graphics;
9import java.awt.event.ComponentAdapter; 10import java.awt.Graphics2D;
10import java.awt.event.ComponentEvent; 11import java.awt.GraphicsConfiguration;
11import java.util.Random; 12import java.awt.GraphicsDevice;
13import java.awt.GraphicsEnvironment;
14import java.awt.Toolkit;
15import java.awt.Transparency;
16import java.awt.image.BufferStrategy;
17import java.awt.image.BufferedImage;
18import java.util.List;
19import java.util.concurrent.CopyOnWriteArrayList;
12import javax.swing.JFrame; 20import javax.swing.JFrame;
13 21
14/** 22/**
@@ -17,75 +25,84 @@ import javax.swing.JFrame;
17 */ 25 */
18public class Main 26public class Main
19{ 27{
20 static final int GAME_WIDTH = 15; 28 static final int GAME_WIDTH = 320;
21 static final int GAME_HEIGHT = 10; 29 static final int GAME_HEIGHT = 240;
22 static int TILE_WIDTH = 32;
23 static int TILE_HEIGHT = 32;
24 30
25 private static JFrame mainWindow; 31 private static JFrame mainWindow;
26 private static Color[][] grid; 32 private static Color[][] grid;
27 private static int drawOffsetX = 0; 33 private static int drawOffsetX = 0;
28 private static int drawOffsetY = 0; 34 private static int drawOffsetY = 0;
35 private static Canvas gameCanvas;
36 private static List<Renderable> renderables = new CopyOnWriteArrayList<Renderable>();
37 private static GameState gameState;
29 38
30 public static void main(String[] args) 39 public static void main(String[] args)
31 { 40 {
32 mainWindow = new JFrame("Frigid Earth"); 41 gameCanvas = new Canvas();
33 mainWindow.setSize(GAME_WIDTH*TILE_WIDTH, GAME_HEIGHT*TILE_HEIGHT + mainWindow.getInsets().top); 42
43 mainWindow = new JFrame();
44 mainWindow.setTitle("Frigid Earth");
45 mainWindow.setSize(GAME_WIDTH*2, GAME_HEIGHT*2);
46 mainWindow.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().x-GAME_WIDTH, GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().y-GAME_HEIGHT);
34 mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 47 mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
35 mainWindow.addComponentListener(new ComponentAdapter() { 48 mainWindow.add(gameCanvas);
36 @Override
37 public void componentResized(ComponentEvent ce)
38 {
39 drawOffsetX = 0;
40 drawOffsetY = 0;
41
42 TILE_WIDTH = mainWindow.getContentPane().getWidth() / GAME_WIDTH;
43 drawOffsetX = mainWindow.getWidth() % GAME_WIDTH / 2;
44 TILE_HEIGHT = mainWindow.getContentPane().getHeight() / GAME_HEIGHT;
45 drawOffsetY = mainWindow.getHeight() % GAME_HEIGHT / 2;
46
47 if (TILE_WIDTH > TILE_HEIGHT)
48 {
49 TILE_WIDTH = TILE_HEIGHT;
50 drawOffsetX = (mainWindow.getContentPane().getWidth() - (GAME_WIDTH * TILE_WIDTH)) / 2;
51 } else if (TILE_HEIGHT > TILE_WIDTH)
52 {
53 TILE_HEIGHT = TILE_WIDTH;
54 drawOffsetY = (mainWindow.getContentPane().getHeight() - (GAME_HEIGHT * TILE_HEIGHT)) / 2;
55 }
56
57 redrawScreen();
58 }
59 });
60 mainWindow.setVisible(true); 49 mainWindow.setVisible(true);
61 50
62 Random r = new Random(); 51 gameCanvas.createBufferStrategy(2);
63 grid = new Color[GAME_WIDTH][GAME_HEIGHT]; 52
53 gameState = new TileGameState();
54 renderables.add(gameState);
64 55
65 for (int x=0; x<GAME_WIDTH; x++) 56 gameState.tick();
57
58 for (;;)
66 { 59 {
67 for (int y=0; y<GAME_HEIGHT; y++) 60 render(gameCanvas);
61
62 try
63 {
64 Thread.sleep(10);
65 } catch (InterruptedException ex)
68 { 66 {
69 grid[x][y] = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); 67 //Nothing
70 } 68 }
71 } 69 }
72
73 redrawScreen();
74 } 70 }
75 71
76 private static void redrawScreen() 72 public static void render(Canvas gameCanvas)
77 { 73 {
78 Graphics g = mainWindow.getGraphics(); 74 BufferStrategy buffer = gameCanvas.getBufferStrategy();
79 g.setColor(Color.black); 75 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
80 g.fillRect(0, 0, mainWindow.getWidth(), mainWindow.getHeight()); 76 GraphicsDevice device = env.getDefaultScreenDevice();
77 GraphicsConfiguration config = device.getDefaultConfiguration();
78 BufferedImage vImg = config.createCompatibleImage(GAME_WIDTH, GAME_HEIGHT, Transparency.TRANSLUCENT);
79 Graphics2D g = vImg.createGraphics();
81 80
82 for (int x=0; x<GAME_WIDTH; x++) 81 for (Renderable renderable : renderables)
83 { 82 {
84 for (int y=0; y<GAME_HEIGHT; y++) 83 renderable.render(g);
85 {
86 g.setColor(grid[x][y]);
87 g.fillRect(x*TILE_WIDTH + drawOffsetX, y*TILE_HEIGHT + drawOffsetY + mainWindow.getInsets().top, TILE_WIDTH, TILE_HEIGHT);
88 }
89 } 84 }
85
86 do {
87 do {
88 float wt = mainWindow.getWidth() / (float) GAME_WIDTH;
89 float ht = (mainWindow.getHeight() - mainWindow.getInsets().top) / (float) GAME_HEIGHT;
90 int renderWidth = Math.round(Math.min(wt, ht) * GAME_WIDTH);
91 int renderHeight = Math.round(Math.min(wt, ht) * GAME_HEIGHT);
92 int renderX = (mainWindow.getWidth()/2)-(renderWidth/2);
93 int renderY = ((mainWindow.getHeight()-mainWindow.getInsets().top)/2)-(renderHeight/2);
94
95 Graphics g2 = buffer.getDrawGraphics();
96 g2.setColor(Color.BLACK);
97 g2.fillRect(0, 0, mainWindow.getWidth(), mainWindow.getHeight());
98 g2.drawImage(vImg, renderX, renderY, renderWidth, renderHeight, gameCanvas);
99 g2.dispose();
100 } while (buffer.contentsRestored());
101
102 buffer.show();
103 } while (buffer.contentsLost());
104
105
106 Toolkit.getDefaultToolkit().sync();
90 } 107 }
91} 108}
diff --git a/src/com/fourisland/frigidearth/Renderable.java b/src/com/fourisland/frigidearth/Renderable.java new file mode 100644 index 0000000..5b49cc0 --- /dev/null +++ b/src/com/fourisland/frigidearth/Renderable.java
@@ -0,0 +1,16 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.fourisland.frigidearth;
6
7import java.awt.Graphics2D;
8
9/**
10 *
11 * @author hatkirby
12 */
13public interface Renderable
14{
15 public void render(Graphics2D g);
16}
diff --git a/src/com/fourisland/frigidearth/TileGameState.java b/src/com/fourisland/frigidearth/TileGameState.java new file mode 100644 index 0000000..b64e551 --- /dev/null +++ b/src/com/fourisland/frigidearth/TileGameState.java
@@ -0,0 +1,54 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.fourisland.frigidearth;
6
7import java.awt.Color;
8import java.awt.Graphics2D;
9import java.util.Random;
10
11/**
12 *
13 * @author hatkirby
14 */
15public class TileGameState implements GameState
16{
17 private final int TILE_WIDTH = 16;
18 private final int TILE_HEIGHT = 16;
19 private final int GAME_WIDTH = Main.GAME_WIDTH / TILE_WIDTH;
20 private final int GAME_HEIGHT = Main.GAME_HEIGHT / TILE_HEIGHT;
21 private Color[][] grid;
22
23 public TileGameState()
24 {
25 Random r = new Random();
26 grid = new Color[GAME_WIDTH][GAME_HEIGHT];
27
28 for (int x=0; x<GAME_WIDTH; x++)
29 {
30 for (int y=0; y<GAME_HEIGHT; y++)
31 {
32 grid[x][y] = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
33 }
34 }
35 }
36
37 public void tick()
38 {
39
40 }
41
42 public void render(Graphics2D g)
43 {
44 for (int x=0; x<GAME_WIDTH; x++)
45 {
46 for (int y=0; y<GAME_HEIGHT; y++)
47 {
48 g.setColor(grid[x][y]);
49 g.fillRect(x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
50 }
51 }
52 }
53
54}