Problems with my program crashing
<p>Hi Im trying to get my player to shoot bullets in my game. I have tested firing bullets at set x coordinates and they work fine. So now I have coded the bullets to spawn at the players current position. The bullet now loads fine and moves correctly, but spawns at 0(x) and not the players x position which a want it to spawn at. If you have any ideas then please reply. I have included the entire code, as people have asked me to do this, it is very long, and most of it is irrelavant, but here it is. Any relevant suggestions would be really helpfull. PS. please do not reply if your comment is unrelated to the question.</p>

<pre><code>if(c != Sprite-&gt;key )
    {PutPixel( x + xoff,y + yoff,c);}}}
}

void D3DGraphics::DrawChar(char c,int xoff,int yoff,Font* font,D3DCOLOR color)
{
    if( c &lt; ' ' || c &gt; '~' )
    return;

    const int sheetIndex = c - ' ';
    const int sheetcol = sheetIndex % font-&gt;nCharsPerRow;
    const int sheetRow = sheetIndex / font-&gt;nCharsPerRow;
    const int xStart = sheetcol * font-&gt;charWidth;
    const int yStart = sheetRow * font-&gt;charHeight;
    const int xEnd = xStart + font-&gt;charWidth;
    const int yEnd = yStart + font-&gt;charHeight;
    const int surfacewidth = font-&gt;charWidth *font-&gt;nCharsPerRow;
    for( int y = yStart; y &lt; yEnd; y++)
    {for(int x = xStart; x &lt; xEnd; x++ )
    {
    if(font-&gt;surface[ x + y * surfacewidth ] == D3DCOLOR_XRGB( 0,0,0))
    {PutPixel( x + xoff - xStart,y + yoff - yStart,color);}}}
}

void D3DGraphics::DrawString(const char* string,int xoff,int yoff,Font* font,D3DCOLOR color)
{
    for(int index = 0; string[ index ] != '\0'; index++ )
    {DrawChar(string[ index ],xoff + index * font-&gt;charWidth,yoff,font,color);}
}

GAME.H
#pragma once

#include "D3DGraphics.h"
#include "Keyboard.h"
#include "Mouse.h"
#include "Sound.h"
#include &lt;time.h&gt;
#include "Timer.h"

#define DUDEWIDTH 50
#define DUDEHEIGHT 50
#define DUDENFRAMES 7
#define DUDEFRAMETIME (60 / DUDENFRAMES)
#define DUDEKEYCOLOR D3DCOLOR_XRGB(255,255,255)
#define DUDEFILENAME "leon\\leon"
#define DUDESPEED 1.70f
#define NDUDES 1
class Game
{

public:
struct AnimatedSpriteTemplate
    {Sprite* frames;
    int nframes;
    int frameduration;};

struct  AnimatedSpriteInstance
    {
        AnimatedSpriteTemplate* templat;
        int currentframe;
        int currentframeexposure;
        float x,y;
    };

public:
    Game( HWND hWnd,const KeyboardServer&amp; kServer,const MouseServer&amp; mServer );
    ~Game();
    void LoadAnimatedSprite( AnimatedSpriteTemplate* templat,
    const char* basename,int width,int height,D3DCOLOR key,int nframes,int frameduration);
    void FreeAnimatedSprite(AnimatedSpriteTemplate* templat);
    void CreateSpriteInstance(AnimatedSpriteTemplate* templat,AnimatedSpriteInstance* instance);
    void UpdateAnimation(AnimatedSpriteInstance* instance);
    void DrawSpriteInstance(AnimatedSpriteInstance* instance);
    void gun();
    void Go();

private:
    void ComposeFrame();
    /********************************/
    /*  User Functions              */

    /********************************/
private:
    D3DGraphics gfx;
    KeyboardClient kbd;
    MouseClient mouse;
    DSound audio;
    /********************************/
    /*  User Variables              */
    Sprite backgroundSprite;
    Sprite foregroundSprite;
    Sprite bulletSprite;
    AnimatedSpriteTemplate dudetemplate;

    AnimatedSpriteInstance dude;
    D3DCOLOR fontsurf[ 512 * 84 ];
    Font fixedsys;

    int bulletx;
    Timer timer;
    float timesum;
    float timemin;
    float timemax;
    int frameCount;
    float lastMin;
    float lastMax;
    float lastAvg;
    bool gunisfired;
    int getx();
    int gety();
    /********************************/
};
GAME.CPP
#include "Game.h"
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
#include "Timer.h"
#include &lt;float.h&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;


Game::Game( HWND hWnd,const KeyboardServer&amp; kServer,const MouseServer&amp; mServer )
:   gfx( hWnd ),
    audio( hWnd ),
    kbd( kServer ),
    mouse( mServer ),
    timemin( FLT_MAX),
    timemax(0.0f),
    timesum(0.0f),
    lastMin(0.0f),
    lastMax(0.0f),
    lastAvg(0.0f),
    frameCount(0),
    gunisfired(false)

{
    srand(time(NULL));
    LoadAnimatedSprite(&amp;dudetemplate,
    DUDEFILENAME,DUDEWIDTH,DUDEHEIGHT,DUDEKEYCOLOR,DUDENFRAMES,DUDEFRAMETIME);

    for(int index = 0; index &lt; NDUDES; index++)
    {CreateSpriteInstance(&amp;dudetemplate,&amp;dude);
    dude.x = 20;
    dude.y = 500;
    dude.currentframe = 1% dudetemplate.nframes;}


    LoadFont( &amp;fixedsys,fontsurf,"Fixedsys16x28.bmp",16,28,32);

    LoadSprite( &amp;backgroundSprite,"background.bmp",800,440,D3DCOLOR_XRGB(255,255,255));     
    LoadSprite( &amp;foregroundSprite,"foreground.bmp",800,189,D3DCOLOR_XRGB(255,255,255)); 
    LoadSprite( &amp;bulletSprite,"bullet.bmp",10,10,D3DCOLOR_XRGB(255,255,255));   

}
Game::~Game()
{
    FreeSprite(&amp;backgroundSprite);
    FreeSprite(&amp;foregroundSprite);
    FreeSprite(&amp;bulletSprite);
    FreeAnimatedSprite(&amp;dudetemplate);
}

void Game::LoadAnimatedSprite( AnimatedSpriteTemplate* templat,
    const char* basename,int width,int height,D3DCOLOR key,int nframes,int frameduration)
{
    templat-&gt;frames = (Sprite*)malloc(sizeof(Sprite) * nframes);

    for(int index = 0;index &lt; nframes; index++)
    {   
        char filenamebuffer[ 64 ];
        sprintf(filenamebuffer,"%s%.2d.bmp",basename,index);
        LoadSprite(&amp;templat-&gt;frames[index],filenamebuffer,width,height,key);
    }

    templat-&gt;nframes = nframes;
    templat-&gt;frameduration = frameduration;

}

void Game::FreeAnimatedSprite(AnimatedSpriteTemplate* templat)
{
    for(int index = 0;index &lt; templat-&gt;nframes; index++)
    {FreeSprite(&amp;templat-&gt;frames[ index ]);}
    free(templat-&gt;frames);
}
void Game::CreateSpriteInstance(AnimatedSpriteTemplate* templat,AnimatedSpriteInstance* instance)
{
    instance-&gt; templat = templat;
    instance-&gt;currentframeexposure = 0;
    instance-&gt;currentframe = 0;
    instance-&gt; x = 0.0f;
    instance-&gt; y = 0.0f;
}



void Game::Go()
{
    if(!kbd.SpaceIsPressed())
    {gunisfired = false;}
    else if(kbd.SpaceIsPressed())
    {gunisfired = true;}
    for(int index = 0;index &lt; NDUDES;index++)
    {if(kbd.RightIsPressed())
    {dude.x += DUDESPEED;}
    if(dude.x + DUDEWIDTH &gt;= 790)
    {dude.x = 0;}
    UpdateAnimation(&amp;dude);}
    gfx.BeginFrame();
    ComposeFrame();
    gfx.EndFrame();
}




void Game::UpdateAnimation(AnimatedSpriteInstance* instance)
{

    if(kbd.RightIsPressed())
    {instance-&gt;currentframeexposure++;
    if( instance-&gt;currentframeexposure &gt;= instance-&gt;templat-&gt;frameduration)
    {instance-&gt;currentframe++;
    instance-&gt;currentframe %= instance-&gt;templat-&gt;nframes;
    instance-&gt;currentframeexposure = 0;}}
}

void Game::DrawSpriteInstance(AnimatedSpriteInstance* instance)
{
    gfx.DrawSprite((int)instance-&gt;x,(int)instance-&gt;y,
        &amp;instance-&gt;templat-&gt;frames[ instance-&gt;currentframe ]);
}

int Game::getx()
{return dude.x;
{bulletx = dude.x;}}

void Game::ComposeFrame()
{
    gfx.DrawSprite(0,125,&amp;backgroundSprite);
    gfx.DrawSprite(0,411,&amp;foregroundSprite);

    timer.StartWatch();
    {for(int index = 0;index &lt; NDUDES;index++)
    {DrawSpriteInstance(&amp;dude);}
    timer.StopWatch();}

    const float frametime = timer.GetTimeMilli();
    timesum += frametime;
    timemin = min(timemin,frametime);
    timemax = max(timemax,frametime);
    frameCount++;

    if(frameCount &gt;= 60)

    {lastAvg = timemin / 60.0f;
    lastMin = timemin;
    lastMax = timemax;
    timesum = 0.0f;
    timemin = FLT_MAX;
    timesum = 0.0f;
    frameCount = 0;}


    char buffer[ 64 ];
    sprintf( buffer,"",NDUDES,lastAvg);
    gfx.DrawString(buffer,0,0,&amp;fixedsys,D3DCOLOR_XRGB(255,0,0));
    sprintf( buffer,"",lastMin,lastMax);
    gfx.DrawString(buffer,0,28,&amp;fixedsys,D3DCOLOR_XRGB(255,0,0));


    if(gunisfired)
    {gfx.DrawSprite(bulletx,516,&amp;bulletSprite);}
KEYBOARD.H
#pragma once

class KeyboardServer;

class KeyboardClient
{
public:
    KeyboardClient( const KeyboardServer&amp; kServer );
    bool UpIsPressed() const;
    bool DownIsPressed() const;
    bool LeftIsPressed() const;
    bool RightIsPressed() const;
    bool SpaceIsPressed() const;
    bool EnterIsPressed() const;
private:
    const KeyboardServer&amp; server;
};

class KeyboardServer
{
    friend KeyboardClient;
public:
    KeyboardServer();

    void OnUpPressed();
    void OnDownPressed();
    void OnLeftPressed();
    void OnRightPressed();
    void OnSpacePressed();
    void OnEnterPressed();

    void OnUpReleased();
    void OnDownReleased();
    void OnLeftReleased();
    void OnRightReleased();
    void OnSpaceReleased();
    void OnEnterReleased();
private:
    bool upIsPressed;
    bool downIsPressed;
    bool leftIsPressed;
    bool rightIsPressed;
    bool spaceIsPressed;
    bool enterIsPressed;
};
KEYBOARD.CPP
#include "Keyboard.h"


KeyboardClient::KeyboardClient( const KeyboardServer&amp; kServer )
    : server( kServer )
{}

bool KeyboardClient::UpIsPressed() const
{
    return server.upIsPressed;
}
bool KeyboardClient::DownIsPressed() const
{
    return server.downIsPressed;
}
bool KeyboardClient::LeftIsPressed() const
{
    return server.leftIsPressed;
}
bool KeyboardClient::RightIsPressed() const
{
    return server.rightIsPressed;
}
bool KeyboardClient::SpaceIsPressed() const
{
    return server.spaceIsPressed;
}
bool KeyboardClient::EnterIsPressed() const
{
    return server.enterIsPressed;
}



KeyboardServer::KeyboardServer()
:   upIsPressed( false ),
    downIsPressed( false ),
    leftIsPressed( false ),
    rightIsPressed( false ),
    spaceIsPressed( false ),
    enterIsPressed( false )
{}

void KeyboardServer::OnUpPressed()
{
    upIsPressed = true;
}
void KeyboardServer::OnDownPressed()
{
    downIsPressed = true;
}
void KeyboardServer::OnLeftPressed()
{
    leftIsPressed = true;
}
void KeyboardServer::OnRightPressed()
{
    rightIsPressed = true;
}
void KeyboardServer::OnSpacePressed()
{
    spaceIsPressed = true;
}
void KeyboardServer::OnEnterPressed()
{
    enterIsPressed = true;
}

void KeyboardServer::OnUpReleased()
{
    upIsPressed = false;
}
void KeyboardServer::OnDownReleased()
{
    downIsPressed = false;
}
void KeyboardServer::OnLeftReleased()
{
    leftIsPressed = false;
}
void KeyboardServer::OnRightReleased()
{
    rightIsPressed = false;
}
void KeyboardServer::OnSpaceReleased()
{
    spaceIsPressed = false;
}
void KeyboardServer::OnEnterReleased()
{
    enterIsPressed = false;
}
#pragma once

TIME.H
#include &lt;windows.h&gt;

class Timer
{
public:
    Timer();
    void StartWatch();
    void StopWatch();
    float GetTimeMilli() const;
private:
    float invFreqMilli;
    bool watchStopped;
    __int64 currentCount;
    __int64 startCount;
};
TIME.CPP
#include "Timer.h"

Timer::Timer()
{
    __int64 frequency;
    QueryPerformanceFrequency( (LARGE_INTEGER*)&amp;frequency );
    invFreqMilli = 1.0f / (float)((double)frequency / 1000.0);
    StartWatch();
}

void Timer::StopWatch()
{
    if( !watchStopped )
    {
        QueryPerformanceCounter( (LARGE_INTEGER*)&amp;currentCount );
        watchStopped = true;
    }
}

void Timer::StartWatch()
{
    watchStopped = false;
    QueryPerformanceCounter( (LARGE_INTEGER*)&amp;startCount );
}

float Timer::GetTimeMilli() const
{
    if( !watchStopped )
    {
        QueryPerformanceCounter( (LARGE_INTEGER*)&amp;currentCount );
        return (float)(currentCount - startCount) * invFreqMilli;
    }
    else
    {
        return (float)(currentCount - startCount) * invFreqMilli;
    }
}
WINDOWS.CPP
#include &lt;Windows.h&gt;
#include &lt;wchar.h&gt;
#include "Game.h"
#include "resource.h"
#include "Mouse.h"

static KeyboardServer kServ;
static MouseServer mServ;

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            PostQuitMessage( 0 );
            break;

        // ************ KEYBOARD MESSAGES ************ //
        case WM_KEYDOWN:
            switch( wParam )
            {
            case VK_UP:
                kServ.OnUpPressed();
                break;
            case VK_DOWN:
                kServ.OnDownPressed();
                break;
            case VK_LEFT:
                kServ.OnLeftPressed();
                break;
            case VK_RIGHT:
                kServ.OnRightPressed();
                break;
            case VK_SPACE:
                kServ.OnSpacePressed();
                break;
            case VK_RETURN:
                kServ.OnEnterPressed();
                break;
            }
            break;
        case WM_KEYUP:
            switch( wParam )
            {
            case VK_UP:
                kServ.OnUpReleased();
                break;
            case VK_DOWN:
                kServ.OnDownReleased();
                break;
            case VK_LEFT:
                kServ.OnLeftReleased();
                break;
            case VK_RIGHT:
                kServ.OnRightReleased();
                break;
            case VK_SPACE:
                kServ.OnSpaceReleased();
                break;
            case VK_RETURN:
                kServ.OnEnterReleased();
                break;
            }
            break;
        // ************ END KEYBOARD MESSAGES ************ //

        // ************ MOUSE MESSAGES ************ //
        case WM_MOUSEMOVE:
            {
                int x = (short)LOWORD( lParam );
                int y = (short)HIWORD( lParam );
                if( x &gt; 0 &amp;&amp; x &lt; 800 &amp;&amp; y &gt; 0 &amp;&amp; y &lt; 600 )
                {
                    mServ.OnMouseMove( x,y );
                    if( !mServ.IsInWindow() )
                    {
                        SetCapture( hWnd );
                        mServ.OnMouseEnter();
                    }
                }
                else
                {
                    if( wParam &amp; (MK_LBUTTON | MK_RBUTTON) )
                    {
                        x = max( 0,x );
                        x = min( 799,x );
                        y = max( 0,y );
                        y = min( 599,y );
                        mServ.OnMouseMove( x,y );
                    }
                    else
                    {
                        ReleaseCapture();
                        mServ.OnMouseLeave();
                        mServ.OnLeftReleased();
                        mServ.OnRightReleased();
                    }
                }
            }
            break;
        case WM_LBUTTONDOWN:
            mServ.OnLeftPressed();
            break;
        case WM_RBUTTONDOWN:
            mServ.OnRightPressed();
            break;
        case WM_LBUTTONUP:
            mServ.OnLeftReleased();
            break;
        case WM_RBUTTONUP:
            mServ.OnRightReleased();
            break;
        // ************ END MOUSE MESSAGES ************ //
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}


int WINAPI wWinMain( HINSTANCE hInst,HINSTANCE,LPWSTR,INT )
{
    WNDCLASSEX wc = { sizeof( WNDCLASSEX ),CS_CLASSDC,MsgProc,0,0,
                      GetModuleHandle( NULL ),NULL,NULL,NULL,NULL,
                      L"Chili DirectX Framework Window",NULL };
    wc.hIconSm = (HICON)LoadImage( hInst,MAKEINTRESOURCE( IDI_APPICON16 ),IMAGE_ICON,16,16,0 );
    wc.hIcon   = (HICON)LoadImage( hInst,MAKEINTRESOURCE( IDI_APPICON32 ),IMAGE_ICON,32,32,0 );
    wc.hCursor = LoadCursor( NULL,IDC_ARROW );
    RegisterClassEx( &amp;wc );

    RECT wr;
    wr.left = 650;
    wr.right = 800 + wr.left;
    wr.top = 150;
    wr.bottom = 600 + wr.top;
    AdjustWindowRect( &amp;wr,WS_OVERLAPPEDWINDOW,FALSE );
    HWND hWnd = CreateWindowW( L"Chili DirectX Framework Window",L"Chili DirectX Framework",
                              WS_OVERLAPPEDWINDOW,wr.left,wr.top,wr.right-wr.left,wr.bottom-wr.top,
                              NULL,NULL,wc.hInstance,NULL );

    ShowWindow( hWnd,SW_SHOWDEFAULT );
    UpdateWindow( hWnd );

    Game theGame( hWnd,kServ,mServ );

    MSG msg;
    ZeroMemory( &amp;msg,sizeof( msg ) );
    while( msg.message != WM_QUIT )
    {
        if( PeekMessage( &amp;msg,NULL,0,0,PM_REMOVE ) )
        {
            TranslateMessage( &amp;msg );
            DispatchMessage( &amp;msg );
        }
        else
        {
            theGame.Go();
        }
    }

    UnregisterClass( L"Chili DirectX Framework Window",wc.hInstance );
    return 0;
}
</code></pre>
