Jump to content
  • Advertisement
Sign in to follow this  
msjkus

CDXInput: Help needed

This topic is 4959 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, this is my second post on gamedev.net I am havin problem with CDXInput. I previously posted this message in the beginner section but no body reesponded it so i m posting it here again. My post was as follows Started using the CDX to develop my game but having a problem with CDXInput. I looked to the sample game of CDX (blaster) and tried to use the same code in my game. In the sample the input works fine but in my game it works like i m playin game on pentium100. What i did that i wanted to move my sprite with the arrow keys . Initially I placed the sprite at position X=50, Y=335 but when i press the right arrow key the sprite vanishes and the X location is something like 3274. I dont know why it incrementing the X in thousands ? Hope that someone understands my problem and have solution Here is my code
//////////////////////////////////////////////////////////////////////////////////////////////////
// TIMER.H
//////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef _TF_TIMER_H_
#define _TF_TIMER_H_

#include <windows.h> // for using timeGetTime and use

class TIMER
{
public:
virtual void reset();
virtual bool elapsed(const int &duration, BOOL reschedule=true);
virtual int time();
virtual void start();
TIMER();
virtual ~TIMER();

protected:
int start_time;
};

#endif

//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// GAME.H
//////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _TF_GAME_H_
#define _TF_GAME_H_

#define CDXINCLUDEALL
#include <CDX.h>
#include "TIMER.H"

#ifdef _TF_GAME_MAIN_
#define DECLARE
#else
#define DECLARE extern
#endif

//////////////////////////////////////////////////////////////////////////////////////////////////
// Defines
//////////////////////////////////////////////////////////////////////////////////////////////////
#define TITLE "Time Fight"
#define WIDTH 800
#define HEIGHT 600
#define DEPTH 24
#define FPS 10
#define SPRITE_WIDTH 160
#define SPRITE_HEIGHT 215
#define TRANS_COLOR RGB(255,0,255)
#define BACKGROUND_FILE "bg.bmp"
#define MEM CDXMEM_SYSTEMONLY

//////////////////////////////////////////////////////////////////////////////////////////////////
// Global Variables
//////////////////////////////////////////////////////////////////////////////////////////////////
DECLARE HWND hWnd;
DECLARE BOOL gActive;

DECLARE CDXScreen* Screen;
DECLARE CDXSurface* WorkSurface;
DECLARE CDXSurface* Background;
DECLARE CDXSprite* Player1;
DECLARE CDXInput* Input;
DECLARE TIMER* Timer;
DECLARE TIMER* ITimer;

//////////////////////////////////////////////////////////////////////////////////////////////////
// Functions
//////////////////////////////////////////////////////////////////////////////////////////////////
void InitGame();
void UpdateInput();
void UpdateObjects();
void Render();
void FinishGame();

#endif

//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// TIMER.CPP
//////////////////////////////////////////////////////////////////////////////////////////////////

#include "TIMER.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TIMER::TIMER()
{
this->start();
}

TIMER::~TIMER()
{
}

void TIMER::start()
{
this->start_time = timeGetTime();
}

int TIMER::time()
{
return timeGetTime() - this->start_time;
}

bool TIMER::elapsed(const int &duration, BOOL reschedule)
{
int _time = timeGetTime();
if( (_time - this->start_time) >= duration )
{
if( reschedule )
{
this->reset();
}
return true;
}
return false;
}

void TIMER::reset()
{
this->start_time = timeGetTime();
}

//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// MAIN.CPP
//////////////////////////////////////////////////////////////////////////////////////////////////

#define _TF_GAME_MAIN_

#include "GAME.H"
#include <windows.h>

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_ACTIVATEAPP:
gActive = wParam;
break;

case WM_CREATE:
break;

case WM_SETCURSOR:
SetCursor(NULL);
return TRUE;

case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
Screen->FadeToBlack(2500);
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
break;

case WM_DESTROY:
FinishGame();
PostQuitMessage(0);
break;
}

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

BOOL InitApp(HINSTANCE hInst, int nCmdShow)
{
WNDCLASS WndClass;
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpfnWndProc = WinProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInst;
WndClass.hIcon = LoadIcon(hInst, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = NULL;
WndClass.lpszMenuName = TITLE;
WndClass.lpszClassName = TITLE;
RegisterClass(&WndClass);

// create the main window
hWnd = CreateWindowEx(
WS_EX_TOPMOST,
TITLE,
TITLE,
WS_POPUP,
0,0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hInst,
NULL);

if(!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

InitGame();
if( FAILED(Input->Create(hInst, hWnd)) )
{
CDXError(Screen, "Can't create input");
}
Input->SetActiveDevices(FALSE, TRUE, FALSE);

return TRUE;
}

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;

if(!InitApp(hInst, nCmdShow))
{
return FALSE;
}

while(true)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if(!GetMessage(&msg, NULL, 0, 0 ))
{
return msg.wParam;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if(gActive)
{
UpdateInput();
UpdateObjects();
Render();
}
else WaitMessage();
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// GAME.CPP
//////////////////////////////////////////////////////////////////////////////////////////////////

#include "GAME.H"

void InitGame()
{
Screen = new CDXScreen;
if( FAILED(Screen->CreateFullScreen(hWnd, WIDTH, HEIGHT, DEPTH)) )
{

CDXError(Screen, "Can't create screen");
}

WorkSurface = new CDXSurface;
if( FAILED(WorkSurface->Create(Screen, WIDTH, HEIGHT, MEM)) )
{
CDXError(Screen, "Can't create work surface");
}

Input = new CDXInput;

char str[100]="";
sprintf(str, "Background ...");
WorkSurface->TextXY(0, 0, RGB(255, 255, 255), str);
WorkSurface->DrawBlk(Screen->GetBack(), 0, 0);
Screen->Flip();
::Sleep(1000);

Background = new CDXSurface;
if( FAILED(Background->Create(Screen, BACKGROUND_FILE, MEM)) )
{
CDXError(Screen, "Can't create/load background");
}

sprintf(str, "Sprite ...");
WorkSurface->TextXY(0, 15, RGB(255, 255, 255), str);
WorkSurface->DrawBlk(Screen->GetBack(), 0, 0);
Screen->Flip();
::Sleep(1000);

Player1 = new CDXSprite;
if( FAILED(Player1->Create(Screen, "p1.bmp", SPRITE_WIDTH, SPRITE_HEIGHT, 1, MEM)) )
{
CDXError(Screen, "Can't create/load sprite 1");
}

Player1->SetPosX(50);
Player1->SetPosY(HEIGHT-SPRITE_HEIGHT-50);

sprintf(str, "Timer ...");
WorkSurface->TextXY(0, 30, RGB(255, 255, 255), str);
WorkSurface->DrawBlk(Screen->GetBack(), 0, 0);
Screen->Flip();
::Sleep(1000);

Timer = new TIMER;
Timer->start();

ITimer = new TIMER;
ITimer->start();

Background->DrawBlk(WorkSurface, 0, 0);
WorkSurface->DrawBlk(Screen->GetBack(), 0, 0);
Screen->FadeToSource(2000);
}

void FinishGame()
{
gActive = FALSE;

if( ITimer )
{
delete ITimer; ITimer = 0;
}
if( Timer )
{
delete Timer; Timer = 0;
}
if( Player1 )
{
delete Player1; Player1 = 0;
}
if( Background )
{
delete Background; Background = 0;
}
if( WorkSurface )
{
delete WorkSurface; WorkSurface = 0;
}
if( Input )
{
delete Input; Input = 0;
}
if( Screen )
{
delete Screen; Screen = 0;
}
}

void UpdateInput()
{
Input->Update();
if( Input->GetKeyState(CDXKEY_UPARROW) )
{
Player1->SetPosY(Player1->GetPosY()-1);
}
if( Input->GetKeyState(CDXKEY_DOWNARROW) )
{
Player1->SetPosY(Player1->GetPosY()+1);
}
if( Input->GetKeyState(CDXKEY_LEFTARROW) )
{
Player1->SetPosX(Player1->GetPosX()-1);
}
if( Input->GetKeyState(CDXKEY_RIGHTARROW) )
{
Player1->SetPosX(Player1->GetPosX()+1);
}
}

void UpdateObjects()
{
}

void Render()
{
if( gActive )
{
if( Timer->elapsed((1000/FPS)) )
{
Background->DrawBlk(WorkSurface, 0, 0);

char str[100]="";
sprintf(str, "FPS:%d X:%d, Y:%d", FPS, Player1->GetPosX(), Player1->GetPosY());

WorkSurface->TextXY(0, 0, RGB(255, 255, 255), str);

Player1->SetColorKey(TRANS_COLOR);
Player1->SetShadowOffset(10, 15);
Player1->Draw(WorkSurface, 0, 0, CDXBLT_TRANSSHADOWFAST);
Player1->Draw(WorkSurface, 0, 0, CDXBLT_TRANSALPHA);

WorkSurface->DrawBlk(Screen->GetBack(), 0, 0);
Screen->Flip();
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////

[Edited by - Coder on March 14, 2005 1:11:35 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by msjkus
wow one of the biggest forum on the internet and no response for my little post.

[lol] 1 Week! That sucks. Well let me tell you why, people don't like reading a lot of stuff, I just had a huge post to which I had to write an article on and write something 1/10 the size of what I had posted before people would reply.

Anyways, I've never heard of CDX nor do I used DX, but I'll give it a shot.

First off, using the set functions are not a good idea:
Player1->SetPosY(Player1->GetPosY()-1);
Instead what you need to do is make a ChangePosY function that takes the amount you want to change by and modifies the current Y position. This way you can just call:
Player1->ChangePosY(-1); and that's it! Much simpler and more efficient. Do the same thing for X.

I went thorugh the code you posted and could not see anything else that could be causing a problem like that.
Quote:
In the sample the input works fine but in my game it works like i m playin game on pentium100

Well you have FPS defined as 10:
#define FPS 10
So that could be one reason.

To counter from moving the object too much, in your new functions that I suggested you make, add in variable checking so that if X + W > 640 then X = 640 and limit the bounds for the Y direction as well using the correct values. If it is still too fast after that, then that means that your input updating coding is going way to fast.

I am not familar with the CDXInput class, but you will need to do some more comparing of the sample to see what's different.

- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by msjkus
so you mean that my input in the game very fast ?


Yes. I think what is happening is that either:

1. Your functions to move are off, as to why I suggested making a better one
or
2. Your game is updating so fast that it processes input at least once per frame, but in a way that is consistant with windows updates - ie. if you press down a key, you will get *A LOT* of keydown events.

See if you can reset the state of the key after you process it, so like 'clear' it somehow.

Share this post


Link to post
Share on other sites
great

i did it by changing
#define FPS 10 ---->>>> #define FPS 40
and changed the function
void UpdateInput()
{
if( ipsTimer->elapsed((1000/FPS)) )
{
Input->Update();
if( Input->GetKeyState(CDXKEY_UPARROW) )
{
Player1->SetPosY(Player1->GetPosY()-10);
if( Player1->GetPosY()<0 )
{
Player1->SetPosY(0);
}
}
if( Input->GetKeyState(CDXKEY_DOWNARROW) )
{
Player1->SetPosY(Player1->GetPosY()+10);
if( Player1->GetPosY()>(HEIGHT-SPRITE_HEIGHT) )
{
Player1->SetPosY((HEIGHT-SPRITE_HEIGHT));
}
}
if( Input->GetKeyState(CDXKEY_LEFTARROW) )
{
Player1->SetPosX(Player1->GetPosX()-10);
if( Player1->GetPosX()<0 )
{
Player1->SetPosX(0);
}
}
if( Input->GetKeyState(CDXKEY_RIGHTARROW) )
{
Player1->SetPosX(Player1->GetPosX()+10);
if( Player1->GetPosX()>(WIDTH-SPRITE_WIDTH) )
{
Player1->SetPosX((WIDTH-SPRITE_WIDTH));
}
}
}
}

Thanks dude what i did i just slowed up the input procedure for the game and it worked. Actually i was processing frames very slowley and there was no time any other constraint on the input of the game.

Thanks again for your help

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!