Jump to content
  • Advertisement
Sign in to follow this  
AnthonyVelazquez

Screen Resolution and Sprites

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

[color="#16334B"]Ok, I have two problems.
1. I place a sprite that is 1024*768 and it actually doesnt cover the entire screen. There is an excess and it gets cropped out. The image is 1024*768 as well.

2. How would I exactly change from the main menu to another sprite. Do I have to clear it before I change or something?


This is my code:
#include "Global.h"

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
D3DPRESENT_PARAMETERS d3dpp;
//Main Menu
LPD3DXSPRITE sprite;
LPDIRECT3DTEXTURE9 mainmenutex;
LPDIRECT3DTEXTURE9 battletex;

RECT rct;


void StartD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = 1024; // set the width of the buffer
d3dpp.BackBufferHeight = 768; // set the height of the buffer
d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&d3ddev);
//MainMenu Stuff
D3DXCreateSprite(d3ddev, &sprite);
D3DXCreateTextureFromFile(d3ddev, L"Menu.jpg", &mainmenutex);
D3DXCreateTextureFromFile(d3ddev, L"Battle.png", &battletex);

}

void BeginRender(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(255,255,255,255), 1.0f, 0);
d3ddev->BeginScene();
sprite->Begin(NULL);
}

void EndRender(void)
{
sprite->End();
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}

void StopD3D(void)
{
d3ddev->Release();
d3d->Release();
sprite->Release();
}

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

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

int MainMenu(void)
{
rct.left=0;
rct.right=1024;
rct.top=0;
rct.bottom=768;
sprite->Draw(mainmenutex, &rct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));
if(GetAsyncKeyState(VK_ESCAPE)&1)
{return 0;}
if(GetAsyncKeyState(VK_RETURN)&1)
{return 1;}

}

void Game(void)
{
}


void GameLoop(void)
{
BeginRender();
/////////////
if(MainMenu() == 0)
{
return;
}
else if(MainMenu() == 1)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(255,255,255,255), 1.0f, 0);
sprite->Draw(battletex, &rct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));
}

////////////
EndRender();
}
Basically, if MainMenu returns 0, that means they wanted to close the game, if it returns 1, then it means they want to play. Its not exactly working though.

Share this post


Link to post
Share on other sites
Advertisement
1) At a guess, your graphics card doesn't support non-power-of-2 sized textures so the 768 is a no-no. If my wild guess is correct, you'll need to create a 1024x1024 texture, load your image into the top part then select the appropriate source rectangle when you render. It may help if you could post us a screenshot of exactly what the result on the screen is.

2) You just pass a different texture to ID3DXSprite::Draw() I assume. ID3DXSprite is a confusingly named beast as it does not in anyway represent a specific sprite. It is an interface for drawing sprites and, as far as I know, does not retain any state after ID3DXSprite::End() is called but I'll defer to people who use it on this.

Share this post


Link to post
Share on other sites
I changed it to this:

#include "Global.h"

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
D3DPRESENT_PARAMETERS d3dpp;
//Main Menu
LPD3DXSPRITE sprite;
LPDIRECT3DTEXTURE9 mainmenutex;
LPDIRECT3DTEXTURE9 battletex;

RECT rct;



void StartD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = 1024; // set the width of the buffer
d3dpp.BackBufferHeight = 768; // set the height of the buffer
d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&d3ddev);
//MainMenu Stuff
D3DXCreateSprite(d3ddev, &sprite);
D3DXCreateTextureFromFile(d3ddev, L"Menu.jpg", &mainmenutex);
D3DXCreateTextureFromFile(d3ddev, L"Battle.png", &battletex);

}

void BeginRender(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(255,255,255,255), 1.0f, 0);
d3ddev->BeginScene();
sprite->Begin(D3DXSPRITE_ALPHABLEND);
}

void EndRender(void)
{
sprite->End();
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}

void StopD3D(void)
{
d3ddev->Release();
d3d->Release();
sprite->Release();
mainmenutex->Release();
battletex->Release();
}

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

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

int MainMenu(void)
{
rct.left=0;
rct.right=1024;
rct.top=0;
rct.bottom=768;
sprite->Draw(mainmenutex, &rct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));
if(GetAsyncKeyState(VK_ESCAPE)&1)
{sprite->Draw(battletex, &rct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));
if(GetAsyncKeyState(VK_INSERT)&1)
{return 0;
}
}
return 0;
}

void Game(void)
{
}


void GameLoop(void)
{
BeginRender();
/////////////
MainMenu();
////////////
EndRender();
}




The picture shows perfectly now but I had to change the dimensions of the picture from 1024 X 768 to 1024 X 588, something around there...I basically opened the picture in paint and shrinked it until it fit.


int MainMenu(void)
{
rct.left=0;
rct.right=1024;
rct.top=0;
rct.bottom=768;
sprite->Draw(mainmenutex, &rct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));
if(GetAsyncKeyState(VK_ESCAPE)&1)
{sprite->Draw(battletex, &rct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));
if(GetAsyncKeyState(VK_INSERT)&1)
{return 0;
}
}
return 0;
}


Now my issue with this part is that when I press Escape, the image doesnt stay, it only appears when I press escape.

Share this post


Link to post
Share on other sites
Well, that is to be expected. Every loop you draw the first image, then draw the second image only if Escape is pressed, so what you describe is the expected behaviour. I think you maybe trying to run before you can walk here.

Hint:


LPDIRECT3DTEXTURE9 CurrentTexture=mainmenutex;

void MainMenu()
{
sprite->Draw(CurrentTexture, &rct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255))

if(GetAsyncKeyState(VK_ESCAPE) & 0x8000) CurrentTexture=battletex;
}


Grossly oversimplified, but in essence you need to somehow preserve your current state outside of the MainMenu function.

HTH

Share this post


Link to post
Share on other sites
I updated it:
#include "Global.h"

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
D3DPRESENT_PARAMETERS d3dpp;
//Main Menu
LPD3DXSPRITE sprite;
LPDIRECT3DTEXTURE9 mainmenutex;
LPDIRECT3DTEXTURE9 battletex;
bool battle = false;
RECT rct;



void StartD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = 1024; // set the width of the buffer
d3dpp.BackBufferHeight = 768; // set the height of the buffer
d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&d3ddev);
//MainMenu Stuff
D3DXCreateSprite(d3ddev, &sprite);
D3DXCreateTextureFromFile(d3ddev, L"Menu.jpg", &mainmenutex);
D3DXCreateTextureFromFile(d3ddev, L"Battle.png", &battletex);

}

void BeginRender(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(255,255,255,255), 1.0f, 0);
d3ddev->BeginScene();
sprite->Begin(D3DXSPRITE_ALPHABLEND);
}

void EndRender(void)
{
sprite->End();
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}

void StopD3D(void)
{
d3ddev->Release();
d3d->Release();
sprite->Release();
mainmenutex->Release();
battletex->Release();
}

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

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

int MainMenu(void)
{
rct.left=0;
rct.right=1024;
rct.top=0;
rct.bottom=768;
if(battle == false)
{sprite->Draw(mainmenutex, &rct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));}
if(battle == true)
{sprite->Draw(battletex, &rct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));}
if(GetAsyncKeyState(VK_INSERT)&1)
{battle = !battle;
}
return 0;
}

void Game(void)
{
}


void GameLoop(void)
{
BeginRender();
/////////////
MainMenu();
////////////
EndRender();
}



Would this work?

[color="#16334B"][color="#000088"][color="#000088"][color="#000088"]if[color="#666600"][color="#666600"][color="#666600"]([color="#660066"][color="#660066"][color="#660066"]MainMenu[color="#666600"][color="#666600"][color="#666600"]()[color="#000000"][color="#000000"] [color="#666600"][color="#666600"][color="#666600"]==[color="#000000"][color="#000000"] [color="#006666"][color="#006666"][color="#006666"]0[color="#666600"][color="#666600"][color="#666600"])[color="#000000"][color="#000000"] [color="#666600"][color="#666600"][color="#666600"]{[color="#000000"][color="#000000"] [color="#000088"][color="#000088"][color="#000088"]return[color="#666600"][color="#666600"][color="#666600"];[color="#000000"][color="#000000"] [color="#666600"][color="#666600"][color="#666600"]}[color="#000000"][color="#000000"] [color="#000088"][color="#000088"][color="#000088"]else[color="#000000"][color="#000000"] [color="#000088"][color="#000088"][color="#000088"]if[color="#666600"][color="#666600"][color="#666600"]([color="#660066"][color="#660066"][color="#660066"]MainMenu[color="#666600"][color="#666600"][color="#666600"]()[color="#000000"][color="#000000"] [color="#666600"][color="#666600"][color="#666600"]==[color="#000000"][color="#000000"] [color="#006666"][color="#006666"][color="#006666"]1[color="#666600"][color="#666600"][color="#666600"])[color="#000000"][color="#000000"] [color="#666600"][color="#666600"][color="#666600"]{[color="#000000"][color="#000000"][color="#000000"]
sprite[color="#666600"][color="#666600"][color="#666600"]->[color="#660066"][color="#660066"][color="#660066"]Draw[color="#666600"][color="#666600"][color="#666600"]([color="#000000"][color="#000000"][color="#000000"]battletex[color="#666600"][color="#666600"][color="#666600"],[color="#000000"][color="#000000"] [color="#666600"][color="#666600"][color="#666600"]&[color="#000000"][color="#000000"][color="#000000"]rct[color="#666600"][color="#666600"][color="#666600"],[color="#000000"][color="#000000"][color="#000000"] NULL[color="#666600"][color="#666600"][color="#666600"],[color="#000000"][color="#000000"][color="#000000"]NULL[color="#666600"][color="#666600"][color="#666600"],[color="#000000"][color="#000000"][color="#000000"] D3DCOLOR_XRGB[color="#666600"][color="#666600"][color="#666600"]([color="#006666"][color="#006666"][color="#006666"]255[color="#666600"][color="#666600"][color="#666600"],[color="#000000"][color="#000000"] [color="#006666"][color="#006666"][color="#006666"]255[color="#666600"][color="#666600"][color="#666600"],[color="#000000"][color="#000000"] [color="#006666"][color="#006666"][color="#006666"]255[color="#666600"][color="#666600"][color="#666600"]));[color="#000000"][color="#000000"] [color="#666600"][color="#666600"][color="#666600"]}
[color="#1C2837"][color="#16334B"]Basically, if MainMenu returns 0, that means they wanted to close the game, if it returns 1, then it means they want to play.

Share this post


Link to post
Share on other sites

Would this work?


I have no idea I'm afraid. It would require you to return different values from MainMenu() for a start.

My basic structure looks a bit like this:


void main() // or whatever
{
SetEverythingUp();

while(MessageLoop())
{
if(!UpdateGame()) PostMessage(Hw,WM_CLOSE,0,0);

Render(); // try to separate rendering out from logic where possible
}

ShutEverythingDown();
}

bool UpdateGate()
{
GetInputFromSomewhere();
if(UserHasChosenToQuit()) return false;

UpdateStateOfEverything();

return true;
}

void Render()
{
DrawEverythingBasedOnItsCurrentState();
}


Again, grossly oversimplified but I'd advise you to separate the logic from rendering. Logical section of the code should deal with input and modify state accordingly (e.g. which menu option is selected, which image are we displaying on the background). State needs to be held elsewhere, either as globals in this simple example or as part of a class in a more sophisticated system.

Rendering should not be concerned with modifying state but just drawing things based on their current state.

Simple menu example in psuedocode:


int menuSelected=0;

void Update()
{
if(UpKeyPressed() && menuSelected>0) --menuSelected;
if(DownKeyPressed() && menuSelected<maxMenuItems) ++menuSelected;
}

void Render()
{
foreach(MenuItem m in menuItems)
{
if(index_of(m)==menuSelected) DrawAllSparkly(m);
else DrawNormally(m);
}
}


You would also probably be better to respond to WM_KEYDOWN messages rather than using GetAsyncKeyState() for this sort of thing as you only really want one notification when the key is first pressed, not a constant stream of notifications each frame that the key is down.

HTH

Share this post


Link to post
Share on other sites
I made my character with this
sprite->Draw(chartex, &charrct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));


The rectangle is:

charrct.left=0;
charrct.right=64;
charrct.top=0;
charrct.bottom=64;



I am trying to make the sprite move when I use the UP,DOWN,LEFT,RIGHT arrow keys, but how would I exactly do that?
Im thinking it has to do with the rectangle it is in but Im not sure.

Share this post


Link to post
Share on other sites
Okay, you are without question running before you can walk now. This is going to be a very long, painful process for you if you don't get some fundamental basics down before you start trying to write a Direct3D application, in my humble opinion.

Look at the documentation for ID3DXSprite::Draw(). What's the 4th parameter? The rectangle is clearly documented as being for defining the source rectangle that the image is taken from and has nothing to do with where on the screen it is drawn. The 4th parameter, quite helpfully called 'position' is the one you want to move the sprite around.

Opinion: You need to take a bit of a step back and learn some fundamental programming concepts in a simpler environment. Have you written any text-based games? Have you tried using a more user-friendly graphics library like SDL, SFML or Allegro? Have you developed the skills needed to research API calls using documentation?

Trying to get through this stuff by posting every question on an internet forum is fine, and people will always be happy to help, but it will take you literally years to achieve anything.

But, in essence:


float x=100,y=100;

void Update()
{
if(KeyDown(LEFT)) x-=5;
if(KeyDown(RIGHT)) x+=5;
if(KeyDown(UP)) y-=5;
if(KeyDown(DOWN)) y+=5;
}

void Render()
{
sprite->Draw(texture,&sourcerect,NULL,D3DXVECTOR3(x,y,0),color);
}


What that will do will of course depend upon what view and projection matrices you have set up but given your previous examples of drawing the static images, I'm assuming that you are (0,0,0) equals top left of screen mode.

Share this post


Link to post
Share on other sites
Its not working...

I fixed the menu issue, and I got my character to use blending. The position isnt working though....
I made a function called Input() and this is what is inside

if(GetAsyncKeyState(VK_RETURN)&1)
{position.x--;
}



Then of course I Render(), but my character just stays in the corner....

#include "Global.h"

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
D3DPRESENT_PARAMETERS d3dpp;
//Main Menu
LPD3DXSPRITE sprite;
LPDIRECT3DTEXTURE9 mainmenutex;
LPDIRECT3DTEXTURE9 battletex;
LPDIRECT3DTEXTURE9 chartex;
float charx=0.0f,chary=0.0f;
D3DXVECTOR3 position;
D3DXVECTOR3 center;
bool battle = false;
int menu = 1;
RECT imagerct;
RECT charrct;



void StartD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = 1024; // set the width of the buffer
d3dpp.BackBufferHeight = 768; // set the height of the buffer
d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&d3ddev);
//MainMenu Stuff
D3DXCreateSprite(d3ddev, &sprite);
D3DXCreateTextureFromFile(d3ddev, L"Menu.jpg", &mainmenutex);
D3DXCreateTextureFromFile(d3ddev, L"Battle.png", &battletex);
D3DXCreateTextureFromFileEx(d3ddev, // the device pointer
L"trainer.png", // the new file name
D3DX_DEFAULT, // default width
D3DX_DEFAULT, // default height
D3DX_DEFAULT, // no mip mapping
NULL, // regular usage
D3DFMT_A8R8G8B8, // 32-bit pixels with alpha
D3DPOOL_MANAGED, // typical memory handling
D3DX_DEFAULT, // no filtering
D3DX_DEFAULT, // no mip filtering
D3DCOLOR_XRGB(255, 255, 255), // the hot-pink color key
NULL, // no image info struct
NULL, // not using 256 colors
&chartex); // load to sprite
}

void BeginRender(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(255,255,255,255), 1.0f, 0);
d3ddev->BeginScene();
sprite->Begin(D3DXSPRITE_ALPHABLEND);
}

void EndRender(void)
{
sprite->End();
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}

void StopD3D(void)
{
d3ddev->Release();
d3d->Release();
sprite->Release();
mainmenutex->Release();
battletex->Release();
chartex->Release();
}

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

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

int MainMenu(void)
{
imagerct.left=0;
imagerct.right=1024;
imagerct.top=0;
imagerct.bottom=768;

sprite->Draw(mainmenutex, &imagerct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));
if(GetAsyncKeyState(VK_RETURN)&1)
{menu = 2;
}
if(GetAsyncKeyState(VK_ESCAPE)&1)
{menu = 0;
}


return 0;
}

void Game(void)
{
charrct.left=0;
charrct.right=64;
charrct.top=0;
charrct.bottom=64;

imagerct.left=0;
imagerct.right=1024;
imagerct.top=0;
imagerct.bottom=768;

position.x=0.0f;
position.y=0.0f;
position.z=0.0f;


center.x=0.0f;
center.y=0.0f;
center.z=0.0f;

sprite->Draw(battletex, &imagerct, NULL,NULL, D3DCOLOR_XRGB(255, 255, 255));
sprite->Draw(chartex, &charrct, &center,&position, D3DCOLOR_XRGB(255, 255, 255));
if(GetAsyncKeyState(VK_RETURN)&1)
{menu = 1;
}
if(GetAsyncKeyState(VK_ESCAPE)&1)
{menu = 0;
}
}
void Input(void)
{
if(GetAsyncKeyState(VK_RETURN)&1)
{position.x--;
}
return;
}
void GameLoop(void)
{
Input();
BeginRender();
/////////////
if(menu == 1)
{MainMenu();}
else if(menu == 0)
{return;}
else
{Game();}
////////////
EndRender();}




Share this post


Link to post
Share on other sites

position.x=0.0f;
position.y=0.0f;
position.z=0.0f;


This is what you are passing in as the position parameter every frame. Regardless of what you do in Input() you are resetting position to (0,0,0) in Game() before your draw call every frame. What do you expect to happen?

You've made position a global, but you are resetting it to (0,0,0) literally the line before you call Draw(). This makes me suspect that continuing to advise on this is a bit pointless.

With the greatest respect and with the best intentions in the world, you simply can't blunder and guess your way through something as complex as a Direct3D-based graphics application. You need to step back and get some more fundamental programming concepts in your mind through simpler applications first.

I hope that isn't offensive in anyway. I don't mean it to be. I just think you've clearly tried to learn to swim by jumping from a helicopter into the middle of the pacific. In programming, it rarely works out that way but I wish you luck.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!