game
Class CGameCanvas

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Canvas
          extended by game.CGameCanvas
All Implemented Interfaces:
IGame, IGameDisplay, java.awt.event.MouseListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible

public class CGameCanvas
extends java.awt.Canvas
implements IGame, IGameDisplay, java.awt.event.MouseListener

The game Canvas, implementing the IGame and IGameDisplay interfaces. Responds to user actions through controllers as well.

Author:
sroccaserra
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
static java.awt.Color DEFAULT_COLOR
           
static float FPS
           
static java.awt.Color GAME_BG_COLOR
           
static int H
           
private  java.awt.Graphics m_backBuffer
           
private  CAnimatedSprite m_ball
           
private  java.awt.Image m_bgImage
           
private  CBitmapFont m_bmpBlueFont
           
private  CBitmapFont m_bmpBoldFont_9
           
private  CBitmapFont m_bmpFont_13
           
private  CBitmapFont m_bmpGreenFont
           
private  CBitmapFont m_bmpRedFont
           
private  boolean m_cantAnimate
           
private  boolean m_cantDraw
           
private  CGraphicLayer m_cBackgroundLayer
           
private  CCriticalLayer m_cCriticalLayer
           
private  CEphemereLayer m_cEphemereLayer
           
private  CEphemereMessage m_criticalWarning
           
private  CGraphicLayer m_cTextLayer
           
private  IGameApplication m_gameApplication
           
private  CGameController m_gameController1
           
private  CGameLoop m_gameLoop
           
private  CHighscoreTable m_highscoreTable
           
private  java.awt.Image m_introImage
           
private  boolean m_isDrawing
           
private  CMouseController m_mouseController
           
private  long m_msStartTime
           
private  java.awt.Image m_offscreenImage
           
private  COnscreenMessage m_pressStart
           
private  CStaticSprite m_scoreBoxSprite
           
private  CScoreManager m_scoreManager
           
private  boolean m_showDebugText
           
static java.awt.Color SHADOW_COLOR
           
static java.lang.String STR_DATE
           
static java.lang.String STR_NAME
           
static java.lang.String STR_VERSION
           
static java.awt.Color TITLE_BG_COLOR
           
static int W
           
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
private CGameCanvas()
          Basic constructor (if no resource - gfx or sound - needs to be loaded).
  CGameCanvas(IGameApplication gameApplication)
          Resource loading constructor.
 
Method Summary
private  void addHitText(int nbHits, int increment, int multiplier, float x, float y)
           
private  void addRandomBall()
           
private  void addRandomRect()
           
private  void addStaticData()
          (Re)inits internal data.
 void animate(float dt)
          Animation function called by the game loop.
private  void checkState()
          Checks controller inputs.
 void clear()
          Clears internal data.
 void draw()
          The drawing function called by the game loop.
private  void draw(java.awt.Graphics g)
          Internal drawing function.
private  java.awt.Image generateBGImage()
          Computes the background image.
private  java.awt.Image generateIntroImage()
          Computes the intro (credits) image.
private  void generateSparkles(CVector2D position, CVector2D velocity)
          Generates sparkles.
 CBitmapFont getBlueFont()
           
 CBitmapFont getBoldFont()
           
 CBitmapFont getDefaultFont()
           
 CBitmapFont getGreenFont()
           
 CBitmapFont getRedFont()
           
 int getX()
          IGameDisplay implementation.
 int getY()
          IGameDisplay implementation.
private  void init()
          (Re)inits internal data.
 void mouseClicked(java.awt.event.MouseEvent arg0)
           
 void mouseEntered(java.awt.event.MouseEvent arg0)
           
 void mouseExited(java.awt.event.MouseEvent arg0)
           
 void mousePressed(java.awt.event.MouseEvent arg0)
           
 void mouseReleased(java.awt.event.MouseEvent arg0)
           
 void paint(java.awt.Graphics g)
          Canvas override.
private  void pause()
          Pauses the animation.
private  void plugController(CGameController gc)
          Game controller declaration.
private  void plugMouseController(CMouseController mc)
          Mouse controller declaration.
private  void popNHitGraphicObject(IGraphicObject go)
          Pops an object and registers a hit.
private  void recordControllerStates()
          Records current controllers state.
private  void restart()
          Restarts the game.
 void setBounds(int arg0, int arg1, int arg2, int arg3)
          Component override.
 void startLoop()
          Called by the Applet or main application to start the game loop.
 void stopLoop()
          Called by the Applet or main application to stop the game loop.
private  void unpause()
          Unpauses the animation.
 void update(java.awt.Graphics g)
          Canvas override.
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface game.IGameDisplay
getHeight, getWidth
 

Field Detail

STR_NAME

public static final java.lang.String STR_NAME
See Also:
Constant Field Values

STR_VERSION

public static final java.lang.String STR_VERSION
See Also:
Constant Field Values

STR_DATE

public static final java.lang.String STR_DATE
See Also:
Constant Field Values

FPS

public static final float FPS
See Also:
Constant Field Values

m_msStartTime

private long m_msStartTime

m_gameLoop

private CGameLoop m_gameLoop

m_showDebugText

private boolean m_showDebugText

W

public static final int W
See Also:
Constant Field Values

H

public static final int H
See Also:
Constant Field Values

DEFAULT_COLOR

public static final java.awt.Color DEFAULT_COLOR

SHADOW_COLOR

public static final java.awt.Color SHADOW_COLOR

TITLE_BG_COLOR

public static final java.awt.Color TITLE_BG_COLOR

GAME_BG_COLOR

public static final java.awt.Color GAME_BG_COLOR

m_cantDraw

private volatile boolean m_cantDraw

m_isDrawing

private volatile boolean m_isDrawing

m_cantAnimate

private volatile boolean m_cantAnimate

m_cBackgroundLayer

private CGraphicLayer m_cBackgroundLayer

m_cCriticalLayer

private CCriticalLayer m_cCriticalLayer

m_cEphemereLayer

private CEphemereLayer m_cEphemereLayer

m_cTextLayer

private CGraphicLayer m_cTextLayer

m_offscreenImage

private java.awt.Image m_offscreenImage

m_backBuffer

private java.awt.Graphics m_backBuffer

m_scoreBoxSprite

private CStaticSprite m_scoreBoxSprite

m_ball

private CAnimatedSprite m_ball

m_introImage

private java.awt.Image m_introImage

m_bgImage

private java.awt.Image m_bgImage

m_criticalWarning

private CEphemereMessage m_criticalWarning

m_pressStart

private COnscreenMessage m_pressStart

m_bmpFont_13

private CBitmapFont m_bmpFont_13

m_bmpBoldFont_9

private CBitmapFont m_bmpBoldFont_9

m_bmpRedFont

private CBitmapFont m_bmpRedFont

m_bmpGreenFont

private CBitmapFont m_bmpGreenFont

m_bmpBlueFont

private CBitmapFont m_bmpBlueFont

m_gameController1

private CGameController m_gameController1

m_mouseController

private CMouseController m_mouseController

m_scoreManager

private CScoreManager m_scoreManager

m_highscoreTable

private CHighscoreTable m_highscoreTable

m_gameApplication

private IGameApplication m_gameApplication
Constructor Detail

CGameCanvas

private CGameCanvas()
Basic constructor (if no resource - gfx or sound - needs to be loaded).


CGameCanvas

public CGameCanvas(IGameApplication gameApplication)
Resource loading constructor.

Parameters:
gameApplication - : the object that can access and load resources.
Typicaly an Applet derivate or the main app.
Method Detail

addStaticData

private void addStaticData()
(Re)inits internal data.


plugController

private void plugController(CGameController gc)
Game controller declaration.


plugMouseController

private void plugMouseController(CMouseController mc)
Mouse controller declaration.


init

private void init()
(Re)inits internal data.


clear

public void clear()
Clears internal data.


restart

private void restart()
Restarts the game.


startLoop

public void startLoop()
Called by the Applet or main application to start the game loop.


stopLoop

public void stopLoop()
Called by the Applet or main application to stop the game loop.


pause

private void pause()
Pauses the animation.


unpause

private void unpause()
Unpauses the animation.


checkState

private void checkState()
Checks controller inputs.


recordControllerStates

private void recordControllerStates()
Records current controllers state.


addHitText

private void addHitText(int nbHits,
                        int increment,
                        int multiplier,
                        float x,
                        float y)

popNHitGraphicObject

private final void popNHitGraphicObject(IGraphicObject go)
Pops an object and registers a hit. Sparkles are generated at object center position.

Parameters:
go - : the IGraphicObject to pop.

generateSparkles

private final void generateSparkles(CVector2D position,
                                    CVector2D velocity)
Generates sparkles.

Parameters:
position - : origin of the sparkles
velocity - : average speed of the sparkles

animate

public void animate(float dt)
Animation function called by the game loop.

Specified by:
animate in interface IGame
Parameters:
dt - : the amount of game time since last frame. Can be fixed (frame based anim.) or variable (real time based anim.).

draw

public final void draw()
The drawing function called by the game loop. The drawing is double buffered.

Specified by:
draw in interface IGame

draw

private final void draw(java.awt.Graphics g)
Internal drawing function.

Parameters:
g - : the Graphics to draw to.

addRandomRect

private void addRandomRect()

addRandomBall

private void addRandomBall()

paint

public final void paint(java.awt.Graphics g)
Canvas override. If the backbuffer image is available, simply draw it to g. Otherwise, calls super.paint().

Overrides:
paint in class java.awt.Canvas
Parameters:
g -

update

public final void update(java.awt.Graphics g)
Canvas override. Avoids flicker.

Overrides:
update in class java.awt.Canvas
Parameters:
g -

setBounds

public void setBounds(int arg0,
                      int arg1,
                      int arg2,
                      int arg3)
Component override. Calls super and resets the backbuffer since size has changed.

Overrides:
setBounds in class java.awt.Component

getX

public final int getX()
IGameDisplay implementation. Can be used in later versions to translate the whole game surface.

Specified by:
getX in interface IGameDisplay
Overrides:
getX in class java.awt.Component
Returns:
the x position of the game surface

getY

public final int getY()
IGameDisplay implementation. Can be used in later versions to translate the whole game surface.

Specified by:
getY in interface IGameDisplay
Overrides:
getY in class java.awt.Component
Returns:
the y position of the game surface

getDefaultFont

public final CBitmapFont getDefaultFont()
Specified by:
getDefaultFont in interface IGameDisplay

getBoldFont

public final CBitmapFont getBoldFont()
Specified by:
getBoldFont in interface IGameDisplay

getRedFont

public final CBitmapFont getRedFont()
Specified by:
getRedFont in interface IGameDisplay

getGreenFont

public final CBitmapFont getGreenFont()
Specified by:
getGreenFont in interface IGameDisplay

getBlueFont

public final CBitmapFont getBlueFont()
Specified by:
getBlueFont in interface IGameDisplay

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent arg0)
Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent arg0)
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent arg0)
Specified by:
mouseExited in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent arg0)
Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent arg0)
Specified by:
mouseReleased in interface java.awt.event.MouseListener

generateBGImage

private java.awt.Image generateBGImage()
Computes the background image.

Returns:
the generated background image.

generateIntroImage

private java.awt.Image generateIntroImage()
Computes the intro (credits) image.

Returns:
the generated intro image.