Original Post
I'm putting together a job application for a generalist programmer position, and I'm in the process of selecting a code sample for my review, and I would very much like to hear any feedback before sending it off. Too simple, could be written better... ect. Whatever comes to mind if you were handed it as a potential applicant.
As always, thanks for the help.
[source lang="cpp"]
/*
Core game class. Manages the elements, start and end of a game. Instantiated
by main.cpp
*/
#include "Game.h"
Game::Game()
{
// Set default values
gameInProgress = false;
navigating = false;
selectedNode = NULL;
player = NULL;
lastCoreTransform = NULL;
}
Game::~Game()
{
if( gameInProgress )
endGame();
delete lastCoreTransform;
}
// Starts a new game, if one is not already running
void Game::startGame( int levelID )
{
// Break if a game is already in progress
if( gameInProgress ) return;
gameInProgress = true;
player = new Player();
// Create default cameras
cameras.push_back( new Camera() ); //player camera
cameras.push_back( new Camera() ); //fixed cam
// Set camera offset, and set it to follow the player's ship
currentCamera = cameras[0];
currentCamera->translate( 0, -30, -100.0f );
currentCamera->rotate ( 0.0f, 0.0f, 0.0f );
currentCamera->camTarget = player;
// Offset the static camera
cameras[1]->translate( 0, 0, -100.0f );
setCamera(0);
// Generate the level, based on the passed in ID
GameLevelData::createLevel( levelID );
}
// Ends the curent game, and deletes all non texture assets
void Game::endGame()
{
gameInProgress = false;
if( player != NULL )
delete player;
player = NULL;
// Delete all Nodes, Actors and Cameras
while( Node::getNodes().size() > 0 )
delete (*Node::getNodes().begin());
while( Actor::getActors().size() > 0 )
delete (*Actor::getActors().begin());
for( int i = cameras.size()-1; i >= 0; i-- )
delete cameras;
}
// Function to be run, each time the frame updates
void Game::update( int timePassed )
{
handleInput(); // Process user input
updateActors( timePassed ); // Update all actors, AI, and physics
updateCamera( timePassed ); // Update the current camera
drawScene(); // Draw scene data
drawHud(); // Draw 2d HUD elements
}
// Function to generate the view matrix
void Game::updateCamera()
{
// If a game is not running, or a camera is not set, view matrix is just an identity matrix
if( !gameInProgress || currentCamera == NULL )
{
iGLEngine->viewMatrix->setMatrix( mat4(1.0f) );
}
else if( currentCamera->camTarget == NULL )
{
// Camera has no target, so retun just the cameras transform matrix
iGLEngine->viewMatrix->setMatrix( *currentCamera->getTransform()->getMatrix() );
}
else
{
// We have a camera target. Interpolate the last camera position, a set percentage, towards
// the transform of the camera target.
lastCoreTransform->interpolate(currentCamera->camTarget->getCoreTransform(),
0.5f, //Rot multiplier
0.25f ); //Pos Multiplier
// Apply the camera's offset, to the interpolated transformation, and set it as the current view Matrix
iGLEngine->viewMatrix->setMatrix( (*currentCamera->getTransform()->getMatrix()) *
glm::inverse( *lastCoreTransform->getMatrix() ) );
}
}
// Handles user input at the game level
void Game::handleInput(){
// Get a pointer to the current input state via a static accessor
InputState *inputState = InputState::getInputState();
// Toggle between the two cameras with the number keys
if( inputState->keyDown( SDL_SCANCODE_1 ) ) setCamera( 0 );
if( inputState->keyDown( SDL_SCANCODE_2 ) ) setCamera( 1 );
}
// Updates all actors, processes AI (if any), and physics
void Game::updateActors( int timePassed )
{
for ( list::iterator it = Actor::getActors().begin(); it != Actor::getActors().end(); it++ )
{
(*it)->update( timePassed );
}
}
// Draws the scene elements
void Game::drawScene()
{
for ( list::iterator it = Node::getNodes().begin(); it != Node::getNodes().end(); it++ )
{
Mesh *mesh = static_cast(*it);
if( mesh && mesh->getParent() == NULL )
{
mesh->drawMesh();
}
}
}
// Recursive function, to delete a node, and all it's children.
void Game::deleteNode( Node *node )
{
list children = node->getChildren();
for ( list::iterator it = children.begin(); it != children.end(); it++ )
{
deleteNode( (*it) );
}
delete node;
}
// Sets the current camera
void Game::setCamera( int index )
{
// Verify that the passed in index is within boundaries
index = ( index < 0 ) ? 0 : index;
index = ( index > (int)cameras.size() ) ? (int)cameras.size() -1 : index;
currentCamera = cameras[index];
// If the camera has a target, make sure to set the data for the 'lastCoreTransform'
if( currentCamera->camTarget != NULL )
{
// Create the lastCoreTransform if it does not already exist
if( lastCoreTransform == NULL )
lastCoreTransform = new Transform();
lastCoreTransform->setMatrix( *currentCamera->camTarget->getCoreTransform()->getMatrix() );
}
}
[/source]
As always, thanks for the help.
[source lang="cpp"]
/*
Core game class. Manages the elements, start and end of a game. Instantiated
by main.cpp
*/
#include "Game.h"
Game::Game()
{
// Set default values
gameInProgress = false;
navigating = false;
selectedNode = NULL;
player = NULL;
lastCoreTransform = NULL;
}
Game::~Game()
{
if( gameInProgress )
endGame();
delete lastCoreTransform;
}
// Starts a new game, if one is not already running
void Game::startGame( int levelID )
{
// Break if a game is already in progress
if( gameInProgress ) return;
gameInProgress = true;
player = new Player();
// Create default cameras
cameras.push_back( new Camera() ); //player camera
cameras.push_back( new Camera() ); //fixed cam
// Set camera offset, and set it to follow the player's ship
currentCamera = cameras[0];
currentCamera->translate( 0, -30, -100.0f );
currentCamera->rotate ( 0.0f, 0.0f, 0.0f );
currentCamera->camTarget = player;
// Offset the static camera
cameras[1]->translate( 0, 0, -100.0f );
setCamera(0);
// Generate the level, based on the passed in ID
GameLevelData::createLevel( levelID );
}
// Ends the curent game, and deletes all non texture assets
void Game::endGame()
{
gameInProgress = false;
if( player != NULL )
delete player;
player = NULL;
// Delete all Nodes, Actors and Cameras
while( Node::getNodes().size() > 0 )
delete (*Node::getNodes().begin());
while( Actor::getActors().size() > 0 )
delete (*Actor::getActors().begin());
for( int i = cameras.size()-1; i >= 0; i-- )
delete cameras;
}
// Function to be run, each time the frame updates
void Game::update( int timePassed )
{
handleInput(); // Process user input
updateActors( timePassed ); // Update all actors, AI, and physics
updateCamera( timePassed ); // Update the current camera
drawScene(); // Draw scene data
drawHud(); // Draw 2d HUD elements
}
// Function to generate the view matrix
void Game::updateCamera()
{
// If a game is not running, or a camera is not set, view matrix is just an identity matrix
if( !gameInProgress || currentCamera == NULL )
{
iGLEngine->viewMatrix->setMatrix( mat4(1.0f) );
}
else if( currentCamera->camTarget == NULL )
{
// Camera has no target, so retun just the cameras transform matrix
iGLEngine->viewMatrix->setMatrix( *currentCamera->getTransform()->getMatrix() );
}
else
{
// We have a camera target. Interpolate the last camera position, a set percentage, towards
// the transform of the camera target.
lastCoreTransform->interpolate(currentCamera->camTarget->getCoreTransform(),
0.5f, //Rot multiplier
0.25f ); //Pos Multiplier
// Apply the camera's offset, to the interpolated transformation, and set it as the current view Matrix
iGLEngine->viewMatrix->setMatrix( (*currentCamera->getTransform()->getMatrix()) *
glm::inverse( *lastCoreTransform->getMatrix() ) );
}
}
// Handles user input at the game level
void Game::handleInput(){
// Get a pointer to the current input state via a static accessor
InputState *inputState = InputState::getInputState();
// Toggle between the two cameras with the number keys
if( inputState->keyDown( SDL_SCANCODE_1 ) ) setCamera( 0 );
if( inputState->keyDown( SDL_SCANCODE_2 ) ) setCamera( 1 );
}
// Updates all actors, processes AI (if any), and physics
void Game::updateActors( int timePassed )
{
for ( list
{
(*it)->update( timePassed );
}
}
// Draws the scene elements
void Game::drawScene()
{
for ( list
{
Mesh *mesh = static_cast
if( mesh && mesh->getParent() == NULL )
{
mesh->drawMesh();
}
}
}
// Recursive function, to delete a node, and all it's children.
void Game::deleteNode( Node *node )
{
list
for ( list
{
deleteNode( (*it) );
}
delete node;
}
// Sets the current camera
void Game::setCamera( int index )
{
// Verify that the passed in index is within boundaries
index = ( index < 0 ) ? 0 : index;
index = ( index > (int)cameras.size() ) ? (int)cameras.size() -1 : index;
currentCamera = cameras[index];
// If the camera has a target, make sure to set the data for the 'lastCoreTransform'
if( currentCamera->camTarget != NULL )
{
// Create the lastCoreTransform if it does not already exist
if( lastCoreTransform == NULL )
lastCoreTransform = new Transform();
lastCoreTransform->setMatrix( *currentCamera->camTarget->getCoreTransform()->getMatrix() );
}
}
[/source]