Jump to content
  • Advertisement
Sign in to follow this  
Motoky12

Moving sprites in a 2D Coordinate System

This topic is 2521 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

Hey, I have learning how to program in a few languages for about a year and a half now. I have been learning from multiple books over this year and a half. I have a basic graps of programming, I can make a reasonable amount of applications, maybe with a bit of help from msdn with functions and such.

Anyway, I can't seem to get my head around how 2D Objects, Sprites, etc can be moved around on the screen. I have a simple program which is from the Beginning DirectX book. I did this before Christmas and kind of added to it a bit.


#include <Windows.h>
#include "dxManager.h"
#include "spriteStruct.h"

HINSTANCE appInstance;
HWND hWnd;

IDirect3DSurface9* pBitmap;
dxManager* dxMgr = new dxManager();

LARGE_INTEGER timeStart;
LARGE_INTEGER timeEnd;
LARGE_INTEGER timeFreq;
float anim_rate;

#define SPRITE_WIDTH 100
#define SPRITE_HEIGHT 100
#define SCRN_WIDTH 640
#define SCRN_HEIGHT 480

bool initWindow(HINSTANCE hInstance);
bool initSprites(void);
void drawSprite(int frame);
void moveSprite(int frame);
void getFramesPerSecond(float time);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR nCmdLine, int nCmdShow)
{
if(!initWindow(appInstance))
return false;

if(!dxMgr->initDirect3D(hWnd))
return false;

if(!initSprites())
return false;

QueryPerformanceFrequency(&timeFreq);
QueryPerformanceCounter(&timeEnd);

MSG msg;
ZeroMemory(&msg, sizeof(msg));

while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
QueryPerformanceCounter(&timeStart);

anim_rate = ((float)timeStart.QuadPart - (float)timeEnd.QuadPart) / timeFreq.QuadPart;

getFramesPerSecond(anim_rate);

dxMgr->endRender();

timeEnd = timeStart;
}
}
dxMgr->Release();

return (int) msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(wcex);
wcex.style = CS_VREDRAW | CS_HREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(0, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = "DirectXExample";
wcex.lpszClassName = "DirectXExample";
wcex.hIconSm = 0;
RegisterClassEx(&wcex);

hWnd = CreateWindow(wcex.lpszClassName, "DirectXExample",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
SCRN_WIDTH, SCRN_HEIGHT, 0, 0, hInstance, 0);

if(!hWnd)
return false;

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

return true;
}

bool initSprites(void)
{
pBitmap = dxMgr->getSurfaceFromBitmap("Ichigo.bmp", 1100, 61);

if(pBitmap == NULL)
return false;

for(int i = 0; i < MAX_SPRITES; i++)
{
spriteStruct.srcRect.top = 0;
spriteStruct.srcRect.left = i * 100;
spriteStruct.srcRect.right = spriteStruct.srcRect.left + 100;
spriteStruct.srcRect.bottom = 61;

spriteStruct.posX = 0;
spriteStruct.posY = 0;

spriteStruct.currFrame = 0;
spriteStruct.numFrames = MAX_SPRITES;
}
return true;
}

void drawSprite(int frame)
{
if(spriteStruct[frame].currFrame < spriteStruct[frame].numFrames-1)
spriteStruct[frame].currFrame++;
else
spriteStruct[frame].currFrame = 0;

spriteStruct[frame].srcRect.left = spriteStruct[frame].currFrame * 100;
spriteStruct[frame].srcRect.right = spriteStruct[frame].srcRect.left + 100;

spriteStruct[frame].posX += spriteStruct[frame].moveX * anim_rate;
spriteStruct[frame].posY += spriteStruct[frame].moveY * anim_rate;

if(spriteStruct[frame].posX > SCRN_WIDTH - 100)
spriteStruct[frame].moveX = 0;
else if(spriteStruct[frame].posX < 0)
spriteStruct[frame].moveX = 0;
if(spriteStruct[frame].posY > SCRN_HEIGHT - 100)
spriteStruct[frame].moveY = 0;
else if(spriteStruct[frame].posY < 0)
spriteStruct[frame].moveY = 0;

RECT dest;
dest.top = int(spriteStruct[frame].posY);
dest.left = int(spriteStruct[frame].posX);
dest.right = dest.left + 100;
dest.bottom = dest.top + 61;

dxMgr->blitToBitmap(pBitmap, &spriteStruct[frame].srcRect, &dest);
}
void moveSprite(int frame)
{

static UCHAR gKeyBuffer[256];

GetKeyboardState(gKeyBuffer);

if(gKeyBuffer[VK_UP]) spriteStruct[frame].moveY = -100.0f;
if(gKeyBuffer[VK_DOWN]) spriteStruct[frame].moveY = 100.0f;
if(gKeyBuffer[VK_RIGHT]) spriteStruct[frame].moveX = -100.0f;
if(gKeyBuffer[VK_LEFT]) spriteStruct[frame].moveX = 100.0f;
}

void getFramesPerSecond(float time)
{
static float timeElapsed = 0.0f;
static int frameCnt = 0;
static char buffer[256];

frameCnt++;
timeElapsed += time;

if(timeElapsed >= 0.1f)
{
dxMgr->render();

for(int i = 0; i < MAX_SPRITES; i++)
{
drawSprite(i);
moveSprite(i);
}

sprintf_s(buffer, "DirectXExample -- Frames Per Second -- %d", frameCnt);
SetWindowText(hWnd, buffer);

frameCnt = 0;
timeElapsed = 0;
}
}

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

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



#define MAX_SPRITES 11
struct spriteStruct {
RECT srcRect;
float posX;
float posY;
float moveX;
float moveY;
int numFrames;
int currFrame;
} spriteStruct[MAX_SPRITES];



I don't really need to include the other two files. I just wanted to know how my image is able to move with the posX, posY, moveX, and moveY. How does C++ know that posX and posY are the X and Y coordinates in the program? I didn't assign these two variables to anything. I know that the moveX and moveY are the variables used for the speed and are added onto the posX and posY, and multiplied by the difference in time from the previous and current times.

I bet it's really simple to understand, but at the moment I am looking through my code and just cannot see how it works.

Share this post


Link to post
Share on other sites
Advertisement
The function that seems to be doing the rendering is the dxMgr->blitToBitmap(..). I'm not familiar with the windows API, but it increments the sprite position in moveSprite and pass each sprite to the directx manager in drawSprite function.

Share this post


Link to post
Share on other sites
Here is the blitToBitmap function:


void dxManager::blitToBitmap(IDirect3DSurface9* surface, const RECT* src, const RECT* dest)
{
pD3DDevice->StretchRect(surface, src, getBackBuffer(), dest, D3DTEXF_NONE);
}


I don't see any coordinates or anything on the MSDN in the StretchRect function. I've checked each parameter and couldn't find anything lol. Programming is confusing sometimes ;_;

Share this post


Link to post
Share on other sites
The coordinates are on RECT* dest


RECT dest;
dest.top = int(spriteStruct[frame].posY);
dest.left = int(spriteStruct[frame].posX);
dest.right = dest.left + 100;
dest.bottom = dest.top + 61;

Share this post


Link to post
Share on other sites
I think you're confused. You adjust the moveX and moveY in movesprite, and you adjust the posX and posY in drawSprite.

Here's the specific parts, and what they do:

void moveSprite(int frame)
{

static UCHAR gKeyBuffer[256];

GetKeyboardState(gKeyBuffer);

if(gKeyBuffer[VK_UP]) spriteStruct[frame].moveY = -100.0f;
if(gKeyBuffer[VK_DOWN]) spriteStruct[frame].moveY = 100.0f;
if(gKeyBuffer[VK_RIGHT]) spriteStruct[frame].moveX = -100.0f;
if(gKeyBuffer[VK_LEFT]) spriteStruct[frame].moveX = 100.0f;
}


This function checks if the up, down, left or right key is down. if so, it set the X or Y speed. Then, in drawsprite, there's this.


void drawSprite(int frame)
{
.
.
.
spriteStruct[frame].posX += spriteStruct[frame].moveX * anim_rate;
spriteStruct[frame].posY += spriteStruct[frame].moveY * anim_rate;

if(spriteStruct[frame].posX > SCRN_WIDTH - 100)
spriteStruct[frame].moveX = 0;
else if(spriteStruct[frame].posX < 0)
spriteStruct[frame].moveX = 0;
if(spriteStruct[frame].posY > SCRN_HEIGHT - 100)
spriteStruct[frame].moveY = 0;
else if(spriteStruct[frame].posY < 0)
spriteStruct[frame].moveY = 0;
.
.
.
}

Which adds moveX and moveY (time some time delta) to the current position (posX and posY). It then check if posY or posX is at the edge of the screen, and if so, it sets moveX and moveY to 0.

Actually, that check (that posX and Y are at the edge of the screen) should happen before it applies moveX and Y to posX and Y.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!