BREW Breakout - 3 / 9 -
Preparation
SophiaFramework AppWizard
Use SophiaFramework AppWizard to configure the following items:
- BREW SDK : Version 2.1 ( or higher )
- GUI framework : Not to be used
- Generate the following event handlers:
- SFEVT_APP_START
- SFEVT_APP_RESUME
- SFEVT_APP_SUSPEND
- SFEVT_KEY
Other items do not need to be modified.
Adding Member Variables
The Block class is very specific and rather limited to this particular application. Therefore, the programming of this class did not use object-oriented design techniques. But the class for collision detection, Overlap, could be useful in other game applications and hence was coded in an object-oriented way.
Now that the Block class has been generated by the SophiaFramework AppWizard, definitions for its application variables must be added.
First, structures to retain properties for the ball, racket and blocks are defined in the top part of Block.hpp. ( See Block.hpp )
- Block.hpp - ... // Structure for saving ball properties SFMTYPEDEFSTRUCT(BallData) struct BallData { SFXCircle ball; // Ball SFXRGBColor color; // Color SFXGrid velocity; // Speed }; // Structure for retainintg racket properties SFMTYPEDEFSTRUCT(RacketData) struct RacketData { SFXRectangle racket; // Racket SFXRGBColor color; // Color SFXGrid velocity; // Speed }; // Structure for retaining block properties SFMTYPEDEFSTRUCT(BlockData) struct BlockData { UInt16 type; // Block type SInt16 life; // Block life (how many balls can it stand) SInt16 score; // Score SFXRectangle block; // Block SFXRGBColor color; // Color }; ...
Then define variables used for game processing as private member variables of the Block class.
- Block.hpp - ... SFMTYPEDEFCLASS(Block) class Block : public SFCApplication { ... private: ... // Variables for Breakout SFXGraphicsPtr _pgraphics; // Graphics interface SFXRectangle _gameArea; // Game Area SFXRectangle _messageArea; // Area for message display SFXRectangle _basicBlock; // Standard block, // other blocks copy it BallData _usingBall; // Ball used to play Bool _ballPassing; // Is the ball passing // through the block? BallData _spareBalls[SPARE_BALLS]; // Spare balls RacketData _racket; // Racket BlockData _blocks[BLOCK_H_NUM * BLOCK_V_NUM]; // Block SInt32 _score; // Score SInt16 _spareBallNumber; // Number of spare balls SInt16 _blockNumber; // Number of blocks // remaining Bool _blockMotion; // Stage where // block moves? SInt16 _nowStage; // Current stage SInt32 _drawInterval; // Redrawing interval // (Unit: msec) SInt16 _ballYSpeed; // Ball speed in // y direction // (Dots per frame) SInt16 _racketSpeed; // Racket speed // (Dots per frame) UInt16 _status; // Current app status ...
Adding Member Functions
Next, define the functions required to execute the game. These functions will not be invoked from outside the class, so they can be defined as private functions, except for the callback function which will be used in the timer.
- Block.hpp - ... SFMTYPEDEFCLASS(Block) class Block : public SFCApplication { ... private: ... // Functions only for Breakout // Basic initializing function Void Initialize(Void); // Draw nth block (if flag = false, then delete) Void DrawBlock(SInt16 n = -1, Bool flag = true); // Draw Raquet (if flag = false, then delete) Void DrawRacket(Bool flag = true); // Draw ball (if flag = false, then delete) Void DrawBall(Bool flag = true); // Display message (Msg, number of spare balls) Void DrawMessage(SFXWideStringConstRef msg); // Display message(Score and Spare ball number) Void DrawMessage(Void); // Generate stage n Void CreateStage(SInt16 n); // Finished stage ? UInt16 CheckEnd(Void); // Game over Void ShowGameOver(Void); // Game clear ! Void ShowGameClear(Void); }; Void CBMoveBlocks(VoidPtr pdata); // Move whole block (for timer) Void CBAnimate(VoidPtr pdata); // Collision detection and animation // (for timer). ...
Definitions of parameter
Constants that are very likely to be changed, such as ball color and speed should be grouped together in the Parameters.hpp header file.
- Parameters.hpp - #ifndef __PARAMETERS_HPP #define __PARAMETERS_HPP // Background color is black #define BGCOLOR SFXRGBColor(0, 0, 0, 0) // Ball is white #define BALL_COLOR SFXRGBColor(255, 255, 255, 0) // Racket is read #define RACKET_COLOR SFXRGBColor(255, 0, 0, 0) // Message color is white #define MESSAGE_COLOR SFXRGBColor(255, 255, 255, 0) // Maximum number of blocks(horizontal) #define BLOCK_H_NUM 5 // Maximum number of blocks(vertical) #define BLOCK_V_NUM 6 // Number of spare balls #define SPARE_BALLS 2 // Racket width to block width (numerator) #define RACKET_WIDTH2BLOCK_NUM 3 // Racket width to block width (denometer) #define RACKET_WIDTH2BLOCK_DEN 4 // Racket width to block height (numerator) #define RACKET_HEIGHT2BLOCK_NUM 1 // Racket width to block height (denometer) #define RACKET_HEIGHT2BLOCK_DEN 2 // Racket speed to ball speed (numerator) #define RACKET_SPEED2BALL_NUM 16 // Racket speed to ball speed (denometer) #define RACKET_SPEED2BALL_DEN 10 // Radius of the racket corners #define RACKET_CORNER 4 // How many miliseconds would it take for a ball to // go from the bottom to the top. #define TRAVEL_TIME 1400 // Redraw rate per second #define DRAW_RATE 50 // Time until block begins movement (Unit: msec) #define BLOCK_MOVEMENT_TIME 10000 // Interval for block movement (unit is milisecond) #define BLOCK_MOVEMENT_INTERVAL 4000 // No block #define BLOCK_TYPE_NONE 0 // Standard block #define BLOCK_TYPE_NORMAL 1 // Invisible block #define BLOCK_TYPE_INVISIBLE 2 // Do nothing #define APP_STATUS_NOTHING 0 // Playing game #define APP_STATUS_PLAYING 1 // Waiting for user(When game begins) #define APP_STATUS_READY 2 // Waiting for user(to restart) #define APP_STATUS_WAIT_RESTART 3 // Stage continue #define STAGE_END_CONTINUE 0 // Game over #define STAGE_END_MISS 1 // Stage clear #define STAGE_END_CLEAR 2 // Number of stages #define MAX_STAGE 4 #endif
Fractions are used in some values because floating-point operations are very slow in the ARM processor. Whenever possible, integer operations should be substituted in place of floating point operations.