Thread: this code compiles, but doesn't work how it should

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    this code compiles, but doesn't work how it should

    here is my main.cpp from my direct x project. moving the bitmap left and right works good, but it still doesn't move up and down. someone look at this and tell me whats wrong? (i only included the important parts.

    Code:
    enum Actions 
    {
    	ACTIONS_LEFTRIGHT,
    	ACTIONS_UPDOWN,
    	ACTIONS_LEFT,
    	ACTIONS_RIGHT,
    	ACTIONS_UP,
    	ACTIONS_DOWN,
    	ACTIONS_QUIT,
    	ACTIONS_HELP,
    	ACTIONS_CONFIGURE,
    	ACTIONS_MUSIC,
    	ACTIONS_PAUSE,
    	ACTIONS_NONE
    };
    
    bool	g_bQuit, g_bShowHelp, g_bPaused, g_bDoConfig;
    bool	g_bLeft, g_bRight, g_bUp, g_bDown;
    
    DIACTION g_rgActions[] =
    {
    	// Genre-defined virtual axes
    	{ ACTIONS_LEFTRIGHT,	DIAXIS_ARCADES_LATERAL,				0,	"Left/Right" },
    	{ ACTIONS_UPDOWN,		DIAXIS_ARCADES_MOVE,				0,	"Up/Down" },
    
    	// Genre-defined virtual buttons
    	{ ACTIONS_LEFT,			DIBUTTON_ARCADES_LEFT_LINK,			0,	"Left" },
    	{ ACTIONS_RIGHT,		DIBUTTON_ARCADES_RIGHT_LINK,		0,	"Right" },
    	{ ACTIONS_UP,			DIBUTTON_ARCADES_FORWARD_LINK,		0,	"Up" },
    	{ ACTIONS_DOWN,			DIBUTTON_ARCADES_BACK_LINK,			0,	"Down" },
    	{ ACTIONS_PAUSE,		DIBUTTON_ARCADES_PAUSE,				0,	"Pause" },
    
    	// Keys
    	{ ACTIONS_LEFT,			DIKEYBOARD_LEFT,					0,	"Left" },
    	{ ACTIONS_LEFT,			DIKEYBOARD_NUMPAD4,					0,	"Left" },
    	{ ACTIONS_RIGHT,		DIKEYBOARD_RIGHT,					0,	"Right" },
    	{ ACTIONS_RIGHT,		DIKEYBOARD_NUMPAD6,					0,	"Right" },
    	{ ACTIONS_UP,			DIKEYBOARD_UP,						0,	"Up" },
    	{ ACTIONS_UP,			DIKEYBOARD_NUMPAD8,					0,	"Up" },
    	{ ACTIONS_DOWN,			DIKEYBOARD_DOWN,					0,	"Down" },
    	{ ACTIONS_DOWN,			DIKEYBOARD_NUMPAD2,					0,	"Down" },
    	{ ACTIONS_PAUSE,		DIKEYBOARD_P,			DIA_APPFIXED,	"Pause" },
    	{ ACTIONS_MUSIC,		DIKEYBOARD_M,			DIA_APPFIXED,	"Music on/off" },
    	{ ACTIONS_HELP,			DIKEYBOARD_F1,			DIA_APPFIXED,	"Help" },
    	{ ACTIONS_CONFIGURE,	DIKEYBOARD_F2,			DIA_APPFIXED,	"Configure" },
    	{ ACTIONS_QUIT,			DIKEYBOARD_ESCAPE,		DIA_APPFIXED,	"Quit" },
    };
    
    //skip all this blah blah
    
    void HandleAction(UINT nAction, DWORD dwData)
    {
    	int	nAxisPos = (int)dwData;
    
    	switch (nAction)
    	{
    	case ACTIONS_LEFTRIGHT:
    		if (nAxisPos < -AXIS_THRESHOLD)
    		{
    			g_bLeft = true;
    			g_bRight = false;
    		}
    		else if (nAxisPos > AXIS_THRESHOLD)
    		{
    			g_bRight = true;
    			g_bLeft = false;
    		}
    		else
    		{
    			g_bLeft = g_bRight = false;
    		}
    		break;
    
    	case ACTIONS_UPDOWN:
    		if (nAxisPos < -AXIS_THRESHOLD)
    		{
    			g_bUp = true;
    			g_bDown = false;
    		}
    		else if (nAxisPos > AXIS_THRESHOLD)
    		{
    			g_bDown = true;
    			g_bUp = false;
    		}
    		else
    		{
    			g_bUp = g_bDown = false;
    		}
    		break;
    	
    	case ACTIONS_LEFT:
    		g_bLeft = (dwData != 0);
    		break;
    
    	case ACTIONS_RIGHT:
    		g_bRight = (dwData != 0);
    		break;
    
    	case ACTIONS_UP:
    		g_bUp = (dwData != 0);
    		break;
    
    	case ACTIONS_DOWN:
    		g_bDown = (dwData != 0);
    		break;
    
    	case ACTIONS_PAUSE:
    		if(dwData != 0)
    			g_bPaused = !g_bPaused;
    		break;
    
    	case ACTIONS_QUIT:
    		g_bQuit = true;
    		break;
    	
    	default:
    		break;
    	}
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    // Idle-time processing function.
    
    void OnIdle(void)
    {
        if (g_lpPrimary == NULL)
            return;
    
        if (!g_bActive)
            return;
    
        CheckInput();
    
        if (g_bQuit)
        {
            PostMessage(hWndMain, WM_CLOSE, 0, 0);
            return;
        }
    
        RECT    r;
        
        // Draw the background to the screen.
        r.left = 0;
        r.top = 0;
        r.right = 640;
        r.bottom = 480;
        BackBlt(0, 0, lpA, &r, DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
    
        // Move a sprite around with the input.
        static int xSprite;
    	static int ySprite;
    
    	if(!g_bPaused)
    	{
            if (g_bLeft)
                xSprite--;
            if (g_bRight)
                xSprite++;
    	    if (g_bUp)
    		    ySprite--;
    	    if(g_bDown)
    		    ySprite++;
    
            r.left = 64;
            r.right = 128;
            r.top = 0;
            r.bottom = 64;
    
            BackBlt(288 + xSprite, 150 + ySprite, lpSprites, &r, DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT); 
    
            g_lpPrimary->Flip(NULL, DDFLIP_WAIT);
    	}
    }

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    ...whoaaaaaa, slow down with the posts guys (sarcasm)

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    you expected a response in less than 36 minutes?
    Away.

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    //skip all this blah blah
    Would that be where your DInput code is, because without seeing when/how you're setting your direction enum var's, there's no telling what's wrong.

    What code populates the nAction variable? Are you sure that it's being populated properly or at least with the keystroke you expect?

    All you've shown us is an enum, your blit routine and something that looks like an extra unneeded layer of abstraction sitting on top of a wrapper for DI. And impatiently bumping your thread with sarcastic remarks after only half an hour isn't exactly going to get people to flock to help you.

  5. #5
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    here is input.cpp, also I attached my VC++ project:
    Code:
    ////////////////////////////////////////////////////////////////////////////////
    // Input.cpp
    //
    //  DirectInput handling.
    
    #include "stdhdr.h"
    
    #include "Input.h"
    
    // Force the inclusion of the libraries we need
    #pragma comment(lib, "kernel32.lib")
    #pragma comment(lib, "user32.lib")
    #pragma comment(lib, "gdi32.lib")
    #pragma comment(lib, "dxguid.lib")
    #pragma comment(lib, "dinput8.lib")
    
    ////////////////////////////////////////////////////////////////////////////////
    // Input support
    
    // DirectInput object
    IDirectInput8		*g_pDI;
    // Array of devices that we will be using
    LPDIRECTINPUTDEVICE8	*g_pDeviceArray = NULL;
    // Number of devices to handle
    int						g_nDevices = 0;
    
    // Global structure for enumeration
    static	DIACTIONFORMAT			g_diaf;
    
    BOOL CALLBACK DIEnumDevicesBySemanticsCallback(LPCDIDEVICEINSTANCE lpddi,  
    	IDirectInputDevice8	*lpdid, DWORD dwFlags, DWORD dwRemaining, LPVOID pvRef);
    
    // Initialise DirectInput
    BOOL InitDirectInput()
    {
    	HRESULT	h;
    
    	// Create an IDirectInput8 object that we will use to create devices.
    	h = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8,
    		(void **)&g_pDI, NULL);
    	if (FAILED(h))
    		return FALSE;
    
    	ZeroMemory(&g_diaf, sizeof(DIACTIONFORMAT));
    	g_diaf.dwSize = sizeof(DIACTIONFORMAT);
    	g_diaf.dwActionSize = sizeof(DIACTION);
    	g_diaf.rgoAction = g_rgActions;
    	g_diaf.dwNumActions = g_nActions;
    	g_diaf.dwDataSize = g_diaf.dwNumActions * sizeof(DWORD);
    	g_diaf.guidActionMap = g_guidApp;
    	g_diaf.dwGenre = g_dwGenre;
    	g_diaf.dwBufferSize = 16;
    	g_diaf.lAxisMin = -100;
    	g_diaf.lAxisMax = 100;
    	_tcscpy(g_diaf.tszActionMap, g_tszActionMapName);
    	h = g_pDI->EnumDevicesBySemantics(NULL, &g_diaf, 
    		DIEnumDevicesBySemanticsCallback,
    		NULL, DIEDBSFL_ATTACHEDONLY);
    	if (FAILED(h))
    		return FALSE;
    
    	return TRUE;
    }
    
    // Shut down DirectInput
    void ExitDirectInput()
    {
    	for (int iDevice = 0; iDevice < g_nDevices; iDevice++)
    		g_pDeviceArray[iDevice]->Release();
    
    	delete [] g_pDeviceArray;
    	g_pDeviceArray = NULL;
    	g_nDevices = 0;
    
    	if (g_pDI != NULL)
    	{
    		g_pDI->Release();
    		g_pDI = NULL;
    	}
    }
    
    // Acquire (obtain control of) the devices
    void AcquireDevices()
    {
    	for (int iDevice = 0; iDevice < g_nDevices; iDevice++)
    		g_pDeviceArray[iDevice]->Acquire();
    }
    
    // Unacquire (release control of) the devices
    void UnacquireDevices()
    {
    	if (g_pDeviceArray == NULL)
    		return;
    
    	for (int iDevice = 0; iDevice < g_nDevices; iDevice++)
    		g_pDeviceArray[iDevice]->Unacquire();
    }
    
    // Callback function to map appropriate devices
    BOOL CALLBACK DIEnumDevicesBySemanticsCallback(LPCDIDEVICEINSTANCE lpddi,  
    	IDirectInputDevice8	*lpdid, DWORD dwFlags, DWORD dwRemaining, LPVOID pvRef)
    {
    	HRESULT		h;
    
        // Devices of type DI8DEVTYPE_DEVICECTRL are specialized devices not generally
        // considered appropriate to control game actions. We just ignore these.
        if (GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_DEVICECTRL)
            return DIENUM_CONTINUE;
    
    	// Assign exclusive control of this device to us.
        h = lpdid->SetCooperativeLevel(hWndMain, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
    	if (FAILED(h))
    		return DIENUM_CONTINUE;
    
    	// Build the action map for the device. This will map each action to
    	//  the most appropriate function on the device.
    	h = lpdid->BuildActionMap(&g_diaf, NULL, DIDBAM_DEFAULT);
    	if (FAILED(h))
    		return DIENUM_CONTINUE;
    
    	for (DWORD i = 0; i < g_diaf.dwNumActions; i++)
    	{
    		if (g_diaf.rgoAction[i].dwHow != DIAH_UNMAPPED)
    			break;
    	}
    	if (i < g_diaf.dwNumActions)
    	{
    		// If any controls were mapped, we will be using this device,
    		//  so set the action map on this device.
    		h = lpdid->SetActionMap(&g_diaf, NULL, DIDSAM_DEFAULT);
    		if (FAILED(h))
    			return DIENUM_CONTINUE;
    
    		if (g_pDeviceArray == NULL)
    			g_pDeviceArray = new LPDIRECTINPUTDEVICE8[dwRemaining + 1];
    
    		g_pDeviceArray[g_nDevices] = lpdid;
    		g_nDevices++;
    
    		// Add a reference to this device, since DirectInput will 
    		//  release the device when we return.
    		lpdid->AddRef();
    	}
    	return DIENUM_CONTINUE;
    }
    
    #define	INPUT_DATA_LIMIT	20
    
    // Check each device for actions
    void CheckInput()
    {
    	DIDEVICEOBJECTDATA	pdidod[INPUT_DATA_LIMIT];
    	DWORD				dwObjCount;
    
    	if (g_pDeviceArray == NULL)
    		return;
    
    	for (int iDevice = 0; iDevice < g_nDevices; iDevice++)
    	{
    		// Poll the device for data. 
    		g_pDeviceArray[iDevice]->Poll(); 
     
        	// Retrieve the data.
    		dwObjCount = INPUT_DATA_LIMIT;
    		g_pDeviceArray[iDevice]->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),
    											   pdidod,
    											   &dwObjCount, 0);
    		for (DWORD i = 0; i < dwObjCount; i++)
    			// Handle the actions regardless of what device returned them.
    			HandleAction(pdidod[i].uAppData, pdidod[i].dwData);
    	}
    }

  6. #6
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    bump

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    if ( conduct yourself better == FALSE )
    {
            for ( Counter = 0; Counter < Endless; Counter ++ )
            {
                    cout<< "Flame" << endl;
            }
    }
    Don't blatently bump threads.
    The world is waiting. I must leave you now.

  8. #8
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    i bump it when it gets to the bottom of the page. if you're not going to help, don't reply.

    Code:
    if ( postmeaninglessreplies == TRUE )
    {
            for ( Counter = 0; Counter < Endless; Counter ++ )
            {
                    cout<< "Don't post if you are not helping" << endl;
            }
    }
    
    if ( knowhowtospell == FALSE )
    {
            for ( Counter = 0; Counter < Endless; Counter ++ )
            {
                    cout<< "blatAnt" << endl;
            }
    }
    
    if ( knowswhatblatAntmeans == FALSE )
    {
            for ( Counter = 0; Counter < Endless; Counter ++ )
            {
                    cout<< "does bumping offend you? is it noisy?" << endl;
            }
    }
    learn how to spellhttp://www.tuxedo.org/~esr/faqs/smar...html#id2753205
    Last edited by Leeman_s; 09-10-2002 at 01:18 PM.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i bump it when it gets to the bottom of the page. if you're not going to help, don't reply.

    Rule #6. It's not that hard to follow.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    so, can anyone help me?

  11. #11
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > i bump it when it gets to the bottom of the page
    Don't do it at all.

    > learn how to spell
    Wow, that's shocking for me. (serious)

    > can anyone help me?
    That's not the question.

    > if you're not going to help, don't reply
    I will take my right to ignore this request.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why doesn't the code work on right part of tree?
    By lastrial in forum C Programming
    Replies: 1
    Last Post: 05-16-2007, 06:42 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM