From dd6b3c43d2a60f2aa8887b782ed8e19b28f11845 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sat, 2 Jun 2012 16:12:21 -0400 Subject: Created GameState mechanism I'm using a rendering system based off of FourPuzzle's, but less ugly. This one comes with built in variable window size, so the last commit was a bit unnecessary. TileGameState is just a dummy that does what the last commit did: display randomly colored tiles. 320x240 for game size is also just a placeholder for now. --- src/com/fourisland/frigidearth/GameState.java | 14 +++ src/com/fourisland/frigidearth/Main.java | 119 ++++++++++++---------- src/com/fourisland/frigidearth/Renderable.java | 16 +++ src/com/fourisland/frigidearth/TileGameState.java | 54 ++++++++++ 4 files changed, 152 insertions(+), 51 deletions(-) create mode 100644 src/com/fourisland/frigidearth/GameState.java create mode 100644 src/com/fourisland/frigidearth/Renderable.java create mode 100644 src/com/fourisland/frigidearth/TileGameState.java 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 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.frigidearth; + +/** + * + * @author hatkirby + */ +public interface GameState extends Renderable +{ + public void tick(); +} 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 @@ */ package com.fourisland.frigidearth; +import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; -import java.util.Random; +import java.awt.Graphics2D; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.Toolkit; +import java.awt.Transparency; +import java.awt.image.BufferStrategy; +import java.awt.image.BufferedImage; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import javax.swing.JFrame; /** @@ -17,75 +25,84 @@ import javax.swing.JFrame; */ public class Main { - static final int GAME_WIDTH = 15; - static final int GAME_HEIGHT = 10; - static int TILE_WIDTH = 32; - static int TILE_HEIGHT = 32; + static final int GAME_WIDTH = 320; + static final int GAME_HEIGHT = 240; private static JFrame mainWindow; private static Color[][] grid; private static int drawOffsetX = 0; private static int drawOffsetY = 0; + private static Canvas gameCanvas; + private static List renderables = new CopyOnWriteArrayList(); + private static GameState gameState; public static void main(String[] args) { - mainWindow = new JFrame("Frigid Earth"); - mainWindow.setSize(GAME_WIDTH*TILE_WIDTH, GAME_HEIGHT*TILE_HEIGHT + mainWindow.getInsets().top); + gameCanvas = new Canvas(); + + mainWindow = new JFrame(); + mainWindow.setTitle("Frigid Earth"); + mainWindow.setSize(GAME_WIDTH*2, GAME_HEIGHT*2); + mainWindow.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().x-GAME_WIDTH, GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint().y-GAME_HEIGHT); mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - mainWindow.addComponentListener(new ComponentAdapter() { - @Override - public void componentResized(ComponentEvent ce) - { - drawOffsetX = 0; - drawOffsetY = 0; - - TILE_WIDTH = mainWindow.getContentPane().getWidth() / GAME_WIDTH; - drawOffsetX = mainWindow.getWidth() % GAME_WIDTH / 2; - TILE_HEIGHT = mainWindow.getContentPane().getHeight() / GAME_HEIGHT; - drawOffsetY = mainWindow.getHeight() % GAME_HEIGHT / 2; - - if (TILE_WIDTH > TILE_HEIGHT) - { - TILE_WIDTH = TILE_HEIGHT; - drawOffsetX = (mainWindow.getContentPane().getWidth() - (GAME_WIDTH * TILE_WIDTH)) / 2; - } else if (TILE_HEIGHT > TILE_WIDTH) - { - TILE_HEIGHT = TILE_WIDTH; - drawOffsetY = (mainWindow.getContentPane().getHeight() - (GAME_HEIGHT * TILE_HEIGHT)) / 2; - } - - redrawScreen(); - } - }); + mainWindow.add(gameCanvas); mainWindow.setVisible(true); - Random r = new Random(); - grid = new Color[GAME_WIDTH][GAME_HEIGHT]; + gameCanvas.createBufferStrategy(2); + + gameState = new TileGameState(); + renderables.add(gameState); - for (int x=0; x