• Advertisement
Sign in to follow this  

Something is causing this to crash

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

I've been trying to find the bug, but I need help. Am I doing something wrong? code: #include "player.h" player::player() { } player::~player() { } void player::player_set_device(LPDIRECT3DDEVICE9 device) { D3DDEVICE9=device; D3DDEVICE9->AddRef(); } void player::player_set_start_position(float x, float y) { start_x=x; start_y=y; z=0; } void player::player_create(int player) { x=0; y=0; z=0; player_index=player; x_speed=0; y_speed=0; max_x_speed=5; //for now max_y_speed=3; angle=0; angle_holder=0; action=0; animation=0; switch (player_index) { case 0: player_sprite_dir="img/test.bmp"; break; case 1: player_sprite_dir="img/tails.png"; break; case 2: player_sprite_dir="img/knuckles.png"; break; } } void player::player_animation() { //if(player_image_index <=146){player_image_index+=1;}else{player_image_index=0;} } void player::player_step() { player_animation(); } struct VERTEX { float x, y, z; float tu, tv; }; VERTEX player_quad[]= { {-1.0f, 1.0f, 0.0f, 0.0f,0.0f }, { 1.0f, 1.0f, 0.0f, 1.0f,0.0f }, {-1.0f,-1.0f, 0.0f, 0.0f,1.0f }, { 1.0f,-1.0f, 0.0f, 1.0f,1.0f } }; void player::player_draw() { //// sprite loading //// D3DXIMAGE_INFO d3dxImageInfo; D3DXGetImageInfoFromFile(player_sprite_dir, &d3dxImageInfo); player_sprite_width=d3dxImageInfo.Width; player_sprite_height=d3dxImageInfo.Height; D3DXCreateTextureFromFileEx( D3DDEVICE9, player_sprite_dir, D3DPOOL_DEFAULT, // I had to set width manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE. player_sprite_width, // I had to set height manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE. player_sprite_height, // Don't create mip-maps when you plan on using D3DXSPRITE. It throws off the pixel math for sprite animation. D3DPOOL_DEFAULT, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), &d3dxImageInfo, NULL, &player_texture ); D3DDEVICE9->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); D3DDEVICE9->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); D3DXCreateSprite( D3DDEVICE9, &player_sprite ); srcRect.top = 0; srcRect.left = 0; srcRect.bottom = srcRect.top+64; srcRect.right = srcRect.left+(player_image_index*64); D3DXVECTOR3 vCenter( 0.5f, 0.5f, 0.0f ); D3DXVECTOR3 vPosition( x, y, z ); player_sprite->Begin( D3DXSPRITE_ALPHABLEND ); player_sprite->Draw( player_texture, &srcRect, &vCenter, &vPosition, D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,1.0f) ); player_sprite->End(); }

Share this post


Link to post
Share on other sites
Advertisement
Some things to remember:
1. Post the code between source tags as I did below, easier to read.
2. Indent source code, easier to read.
3. Always gives variable definition/declaration so we can know the type or at least use some variable naming conventions so we know if it's a class member, global, etc.
3. Use a debugger and point out the exact line in which the crash happens, with values of variables and stack call, with this information you will probably find your error alone.

Just glancing through this piece of code, everything seems fine. My guess it that there's a problem with other pieces of code using this one. Or maybe some data file can't be found on the hard drive and it crash creating the texture (player_sprite_dir).


#include "player.h"

player::player()
{

}

player::~player()
{

}

void player::player_set_device(LPDIRECT3DDEVICE9 device)
{
D3DDEVICE9=device;
D3DDEVICE9->AddRef();
}

void player::player_set_start_position(float x, float y)
{
start_x=x;
start_y=y;
z=0;
}

void player::player_create(int player)
{
x=0;
y=0;
z=0;

player_index=player;

x_speed=0;
y_speed=0;

max_x_speed=5; //for now
max_y_speed=3;

angle=0;
angle_holder=0;

action=0;

animation=0;

switch (player_index)
{
case 0:
player_sprite_dir="img/test.bmp";
break;
case 1:
player_sprite_dir="img/tails.png";
break;
case 2:
player_sprite_dir="img/knuckles.png";
break;
}

}

void player::player_animation()
{
//if(player_image_index <=146){player_image_index+=1;}else{player_image_index=0;}
}

void player::player_step()
{
player_animation();

}

struct VERTEX
{
float x,
y,
z;

float tu,
tv;
};

VERTEX player_quad[]=
{
{-1.0f, 1.0f, 0.0f, 0.0f,0.0f },
{ 1.0f, 1.0f, 0.0f, 1.0f,0.0f },
{-1.0f,-1.0f, 0.0f, 0.0f,1.0f },
{ 1.0f,-1.0f, 0.0f, 1.0f,1.0f }

};

void player::player_draw()
{

//// sprite loading ////

D3DXIMAGE_INFO d3dxImageInfo;

D3DXGetImageInfoFromFile(player_sprite_dir, &d3dxImageInfo);
player_sprite_width=d3dxImageInfo.Width;
player_sprite_height=d3dxImageInfo.Height;

D3DXCreateTextureFromFileEx( D3DDEVICE9,
player_sprite_dir,
D3DPOOL_DEFAULT, // I had to set width manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE.
player_sprite_width, // I had to set height manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE.
player_sprite_height, // Don't create mip-maps when you plan on using D3DXSPRITE. It throws off the pixel math for sprite animation.
D3DPOOL_DEFAULT,
D3DFMT_UNKNOWN,
D3DPOOL_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f),
&d3dxImageInfo,
NULL,
&player_texture );

D3DDEVICE9->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
D3DDEVICE9->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

D3DXCreateSprite( D3DDEVICE9, &player_sprite );

srcRect.top = 0;
srcRect.left = 0;
srcRect.bottom = srcRect.top+64;
srcRect.right = srcRect.left+(player_image_index*64);

D3DXVECTOR3 vCenter( 0.5f, 0.5f, 0.0f );
D3DXVECTOR3 vPosition( x, y, z );

player_sprite->Begin( D3DXSPRITE_ALPHABLEND );

player_sprite->Draw( player_texture,
&srcRect,
&vCenter,
&vPosition,
D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,1.0f) );

player_sprite->End();
}



Share this post


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

  • Advertisement