• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
gbMike

Code Sample Review Request

5 posts in this topic

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[i];
}

// 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<Actor *>::iterator it = Actor::getActors().begin(); it != Actor::getActors().end(); it++ )
{
(*it)->update( timePassed );
}
}

// Draws the scene elements
void Game::drawScene()
{
for ( list<Node *>::iterator it = Node::getNodes().begin(); it != Node::getNodes().end(); it++ )
{
Mesh *mesh = static_cast<Mesh *>(*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<Node *> children = node->getChildren();
for ( list<Node *>::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]
0

Share this post


Link to post
Share on other sites
[CODE]
for ( list::iterator it = Actor::getActors().begin(); it != Actor::getActors().end(); it++ )
{
(*it)->update( timePassed );
}
[/CODE]

[CODE]
++it
[/CODE]

The postfix increment, it++, creates a copy of the object prior to the operation for comparison of the old value, and it isn't necessary here, the prefix increment doesn't do this. so:

[CODE]
for ( list::iterator it = Actor::getActors().begin(); it != Actor::getActors().end(); ++it )
{
(*it)->update( timePassed );
}
[/CODE]

Don't compare against a function call in a loop, where you have

[CODE]
it != Actor::getActors().end();
[/CODE]

Change it so you create a local iterator value called end, so you end up with:
[CODE]
for ( list::iterator it = Actor::getActors().begin(), end = Actor::getActors().end(); it != end; ++it )
{
(*it)->update( timePassed );
}
[/CODE]

And lastly (*it)-> looks like a bug. Without the class to look at to see that the *it operator is a dereference for the iterator and the class for actor to look at to see that -> is a valid reference for that class I'm just having alarms shoot off here. If that is valid syntax you need to put a comment in there to explain why you're dereferencing twice.

[CODE]
for ( list::iterator it = Actor::getActors().begin(), end = Actor::getActors().end(); it != end; ++it )
{
// The Actor class overloads -> because it is stored as a shared pointer (example)
(*it)->update( timePassed );
}
[/CODE]

[CODE]
// 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[i];
[/CODE]

Are you sure you want to delete the result of .begin() ? Once again I don't know how you're code works but this looks like an infinite loop to me.

[CODE]

for( int i = cameras.size()-1; i >= 0; i-- )
delete cameras[i];

[/CODE]

[CODE]
while( cameras.size() > 0 ) {
cam = cameras.pop_back();
delete cam;
}
[/CODE]

you might want to leave your camera delete in to demonstrate that you know how to do a counting downwards for loop though

Otherwise the general quality of the code is better than what I had for my code samples when I started. Edited by Kyall
1

Share this post


Link to post
Share on other sites
I appreciate the feedback. I've made some adjustments, per your advice. In particular, the loops have been updated with reduced function overhead, and comments have been added to unclear sections. Just to be sure, I've re-reviewed and tested the loops during memory cleanup as well, just to verify that they do work as intended.
0

Share this post


Link to post
Share on other sites
I hate to be a spoilsport on this sort of thing, but I feel like posting your code for review like that is a bit dishonest.

The whole point of a code sample is to see how you really code.

Instead you are presenting a code sample of how your peer-review writes code.


Asking for a code review so you can learn and benefit in your coding style is one thing. Asking for a code review for an employment opportunity is quite another.
2

Share this post


Link to post
Share on other sites
That is a good point. I hadn't thought about it like that.

I'll send it out as is, and see about a code review some other time.
0

Share this post


Link to post
Share on other sites
Isn't code peer-reviewed when you work: [url="http://www.youtube.com/watch?v=Bz6tc9hG9AU"]http://www.youtube.com/watch?v=Bz6tc9hG9AU[/url]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0