how are you..
i'm here again to ask for some advice and a little hlep too...
i've been working with one 3d chess on irrlicht but i don know how to move the pieces..
my program is supossed to receive the coordenates written by the user, from where to where he wants to move.. the you press the button.. and the piece is moved.
i don't really know how to do that... i hope you can help me..
this is my code and the pieces... you can download it from here:
my code + media - megaupload
my code + media+ gigasize
thank you very much for reading
Code: Select all
#include <irrlicht.h>
#include <iostream>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;//para agregar botones ventanas etc
using namespace std;
#pragma comment(lib, "Irrlicht.lib")
#ifdef _IRR_WINDOWS_
#endif
scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;
scene::ISceneNode* cursor =0;
IGUIEnvironment* env;
IGUIWindow* window;
IGUIWindow* win1;
IGUIWindow* win2;
IGUIWindow* win3;
IGUIWindow* win4;
s32 cnt=0;
int board[8][8] = {
{ -1, -3, -5, -8, -7, -6, -4, -2},
{-10,-11,-12,-13,-14,-15,-16,-17},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 10, 11, 12, 13, 14, 15, 16, 17},
{ 1, 3, 5, 8, 7, 6, 4, 2}
};
int x;
int y;
int z;
void CreateBoard(ISceneManager* smgr,IVideoDriver* driver,int board[8][8],int x,int y,int z)
{
for(int i=0;i < 8;i++)
{
for(int j=0;j < 8;j++)
{
if(board[j][i] == 10)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8; //25*i+7;
y=1; //1*j-6;
z=57; //9*j;
scene::IAnimatedMeshSceneNode* anms10 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anms10->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms10->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == 11)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8; //25*i+7;
y=1; //1*j-6;
z=57; //9*j;
scene::IAnimatedMeshSceneNode* anms11 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anms11->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms11->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == 12)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8; //25*i+7;
y=1; //1*j-6;
z=57; //9*j;
scene::IAnimatedMeshSceneNode* anms12 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anms12->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms12->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == 13)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8; //25*i+7;
y=1; //1*j-6;
z=57; //9*j;
scene::IAnimatedMeshSceneNode* anms13 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anms13->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms13->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == 14)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8; //25*i+7;
y=1; //1*j-6;
z=57; //9*j;
scene::IAnimatedMeshSceneNode* anms14 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anms14->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms14->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == 15)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8; //25*i+7;
y=1; //1*j-6;
z=57; //9*j;
scene::IAnimatedMeshSceneNode* anms15 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anms15->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms15->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == 16)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8; //25*i+7;
y=1; //1*j-6;
z=57; //9*j;
scene::IAnimatedMeshSceneNode* anms16 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anms16->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms16->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == 17)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8; //25*i+7;
y=1; //1*j-6;
z=57; //9*j;
scene::IAnimatedMeshSceneNode* anms17 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anms17->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms17->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == 1)
{//26 para mover 1 en x; 24 para mover 1 en z
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8;
y=1;
z=30;//4*j
scene::IAnimatedMeshSceneNode* anms1 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/torre.ms3d"));
anms1->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms1->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == 2)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8;
y=1;
z=30;
scene::IAnimatedMeshSceneNode* anms2 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/torre.ms3d"));
anms2->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms2->setPosition(core::vector3df(x,y,z));
anms2->setRotation(core::vector3df(0,-180,0));
}
else if(board[j][i] == 3)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8;
y=1;
z=30;
scene::IAnimatedMeshSceneNode* anms3 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/caballo.ms3d"));
anms3->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms3->setPosition(core::vector3df(x,y,z));
anms3->setRotation(core::vector3df(0,-180,0));
}
else if(board[j][i] == 4)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8;
y=1;
z=30;
scene::IAnimatedMeshSceneNode* anms4 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/caballo.ms3d"));
anms4->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms4->setPosition(core::vector3df(x,y,z));
anms4->setRotation(core::vector3df(0,-180,0));
}
else if(board[j][i] == 5)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8;
y=1;
z=30;
scene::IAnimatedMeshSceneNode* anms5 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/alfil.ms3d"));
anms5->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms5->setPosition(core::vector3df(x,y,z));
anms5->setRotation(core::vector3df(0,-40,0));
}
else if(board[j][i] == 6)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8;
y=1;
z=30;
scene::IAnimatedMeshSceneNode* anms6 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/alfil.ms3d"));
anms6->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms6->setPosition(core::vector3df(x,y,z));
anms6->setRotation(core::vector3df(0,-40,0));
}
else if(board[j][i] == 7)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8;
y=1;
z=30;
scene::IAnimatedMeshSceneNode* anms7 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/reina.ms3d"));
anms7->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms7->setPosition(core::vector3df(x,y,z));
anms7->setRotation(core::vector3df(0,-40,0));
}
else if(board[j][i] == 8)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+8;
y=1;
z=30;
scene::IAnimatedMeshSceneNode* anms8 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/rey.ms3d"));
anms8->setMaterialTexture(0, driver->getTexture("../../media/ccc.bmp"));
anms8->setPosition(core::vector3df(x,y,z));
anms8->setRotation(core::vector3df(0,-5,0));
}
else if(board[j][i] == -10)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7; //35*i+7;
y=1; //5*j-4;
z=175*j; //175*j;
scene::IAnimatedMeshSceneNode* anmsM10 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anmsM10->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM10->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -11)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7; //35*i+7;
y=1; //5*j-4;
z=175*j; //175*j;
scene::IAnimatedMeshSceneNode* anmsM11 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anmsM11->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM11->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -12)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7; //35*i+7;
y=1; //5*j-4;
z=175*j; //175*j;
scene::IAnimatedMeshSceneNode* anmsM12 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anmsM12->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM12->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -13)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7; //35*i+7;
y=1; //5*j-4;
z=175*j; //175*j;
scene::IAnimatedMeshSceneNode* anmsM13 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anmsM13->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM13->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -14)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7; //35*i+7;
y=1; //5*j-4;
z=175*j; //175*j;
scene::IAnimatedMeshSceneNode* anmsM14 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anmsM14->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM14->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -15)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7; //35*i+7;
y=1; //5*j-4;
z=175*j; //175*j;
scene::IAnimatedMeshSceneNode* anmsM15 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anmsM15->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM15->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -16)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7; //35*i+7;
y=1; //5*j-4;
z=175*j; //175*j;
scene::IAnimatedMeshSceneNode* anmsM16 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anmsM16->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM16->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -17)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7; //35*i+7;
y=1; //5*j-4;
z=175*j; //175*j;
scene::IAnimatedMeshSceneNode* anmsM17 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/peon.ms3d"));
anmsM17->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM17->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -1)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7;
y=1;
z=203;
scene::IAnimatedMeshSceneNode* anmsM1 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/torre.ms3d"));
anmsM1->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM1->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -2)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7;
y=1;
z=203;
scene::IAnimatedMeshSceneNode* anmsM2 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/torre.ms3d"));
anmsM2->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM2->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -3)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7;
y=1;
z=203;
scene::IAnimatedMeshSceneNode* anmsM3 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/caballo.ms3d"));
anmsM3->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM3->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -4)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7;
y=1;
z=203;
scene::IAnimatedMeshSceneNode* anmsM4 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/caballo.ms3d"));
anmsM4->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM4->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -5)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7;
y=1;
z=203;
scene::IAnimatedMeshSceneNode* anmsM5 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/alfil.ms3d"));
anmsM5->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM5->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -6)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7;
y=1;
z=203;
scene::IAnimatedMeshSceneNode* anmsM6 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/alfil.ms3d"));
anmsM6->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM6->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -7)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7;
y=1;
z=203;
scene::IAnimatedMeshSceneNode* anmsM7 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/reina.ms3d"));
anmsM7->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM7->setPosition(core::vector3df(x,y,z));
}
else if(board[j][i] == -8)
{
//Ir_a_xy(5*i+7,3*j+2);
x=25*i+7;
y=1;
z=203;
scene::IAnimatedMeshSceneNode* anmsM7 = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/rey.ms3d"));
anmsM7->setMaterialTexture(0, driver->getTexture("../../media/ccc2.bmp"));
anmsM7->setPosition(core::vector3df(x,y,z));
}
}
}
}
class MyEventReceiver : public IEventReceiver
{
public:
bool touched;
core::vector3df v;
virtual bool OnEvent(SEvent event)
{
/*if (event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
IGUIEnvironment* env = device->getGUIEnvironment();
switch(event.GUIEvent.EventType)
{
case EGET_BUTTON_CLICKED:
if (id == 101)
{
win1->getElementFromId(1);
win2->getElementFromId(2);
win3->getElementFromId(3);
win4->getElementFromId(4);
board[win1][win2]=board[win3][win4];
return true;
}
break;
}
}
return false;
*/
if (cursor != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&!event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_KEY_W:
case KEY_KEY_S:
{
v = cursor->getPosition();
v.Z += event.KeyInput.Key == KEY_KEY_W ? 24.0f : -24.0f;
if(v.Z >= 30 && v.Z <= 203)
{
cursor->setPosition(v);
}
}
return true;
}
switch(event.KeyInput.Key)
{
case KEY_KEY_A:
{
v = cursor->getPosition();
v.X += event.KeyInput.Key == KEY_KEY_W ? 24.0f : -24.0f;
if(v.X >= 9)
{
cursor->setPosition(v);
}
}
return true;
}
switch(event.KeyInput.Key)
{
case KEY_KEY_D:
{
v = cursor->getPosition();
v.X += event.KeyInput.Key == KEY_KEY_W ? -24.0f : 24.0f;;//-25 25
if(v.X <= 182)
{
cursor->setPosition(v);
}
}
return true;
}
switch(event.KeyInput.Key)
{
case KEY_KEY_F:
{
touched = true;
}
return true;
}
}//endif
return false;
}
};
class Position
{
public :
Position();
void setX(int x)
{
fila = x;
}
void setZ(int z)
{
columna = z;
}
int getX(){return fila;}
int getZ(){return columna;}
private :
int fila;
int columna;
};
class Cursor
{
public :
int piece;
int posx;
int posz;
Cursor();
void Select()
{
for(int a=0;a < 8;a++)
{
for(int b=0;b < 8;b++)
{
if(mer.touched == true && (mer.v.X >=(25*a+7) && mer.v.X <=(25*a+32)) && (mer.v.Z >=(9*b) && mer.v.Z <= (9*b+24)))
{
piece = board[b][a];
posx=(25*a+7);
posz=(9*b);
}
}
}
}
Position getSelected()
{
pos.setX(posx);
pos.setZ(posz);
return pos;
}
//Position getActual();
private :
Position pos;
ISceneNode* node;
MyEventReceiver mer;
SEvent event;
};
int main()
{
// create device
MyEventReceiver receiver;
IrrlichtDevice* device = createDevice(video::EDT_DIRECT3D9, core::dimension2d<s32>(1000, 700),//640,480
16, false, false, false, &receiver);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* env = device->getGUIEnvironment();
node = smgr->addCubeSceneNode(11,0,-1,core::vector3df(0,-90,0),core::vector3df(0,-90,0),core::vector3df(20.0f,0.2f,20.0f));
node->setPosition(core::vector3df(95,0,117));//0,0,
node->setMaterialTexture(0, driver->getTexture("../../media/aje1.jpg"));//para cargar foto30
node->setMaterialFlag(video::EMF_LIGHTING, false);//esta false porke no ha metido luces
cursor = smgr->addCubeSceneNode(1,0,-1,core::vector3df(0,-90,0),core::vector3df(0,-90,0),core::vector3df(20.0f,0.2f,20.0f));
cursor->setPosition(core::vector3df(10,2,55));//0,0,
cursor->setMaterialTexture(0, driver->getTexture("../../media/white.jpg"));//para cargar foto30
cursor->setMaterialFlag(video::EMF_LIGHTING, false);//esta false porke no ha metido luces
ILightSceneNode *luz = smgr->addLightSceneNode();//para efecto de la luz lo vamos a ocupar para el proyecto
luz->getLightData().Radius=300;//aumentar el radio de la luz
luz->setPosition(vector3df(10,10,10));//la luz gira alrededor del modelo
scene::ISceneNodeAnimator* anim=smgr->createFlyCircleAnimator(core::vector3df(0,0,0),100.0f,0.0015f); //VELOCIDAD DE TRASLACION
luz->addAnimator(anim);
env->addStaticText(L"MOVE FROM X : ", rect<s32>(5,20,350,40), false);
env->addStaticText(L"MOVE FROM Z : ", rect<s32>(150,20,350,40), false);
env->addEditBox(L"",rect<s32>(40,40,70,70),true,0,1);
env->addEditBox(L"",rect<s32>(190,40,220,70),true,0,2);
env->addStaticText(L"MOVE TO X : ", rect<s32>(5,90,350,110), false);
env->addStaticText(L"MOVE TO Z : ", rect<s32>(150,90,350,110), false);
env->addEditBox(L"",rect<s32>(40,110,70,140),true,0,3);
env->addEditBox(L"",rect<s32>(190,110,220,140),true,0,4);
//con proposito para tirar mensajes de error en los movimientos no validos
//window = env->addWindow(rect<s32>(70 + cnt, 260 + cnt, 200 + cnt, 330 + cnt),false,L" ERROR!!!");
//env->addStaticText(L"ACCION NO VALIDA",rect<s32>(25,40,130,50),false,false,window);
env->addButton(rect<s32>(90,170,170,200), 0,101, L"MOVE");
//int boton = env-
device->getCursorControl()->setVisible(true);//para ke no se mire el mouse
scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeMaya();
camera->setPosition(core::vector3df(96.375f ,189.57f,100.81091f));
camera->setTarget(core::vector3df(95.783f ,-180.764f ,153.80769f));
CreateBoard(smgr,driver,board,x,y,z);
int lastFPS = -1;
while(device->run())//mientras la ventana este corriendo
{
driver->beginScene(true, true, video::SColor(255,113,113,133));
smgr->drawAll(); // draw the 3d scene
device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)
driver->endScene();
//todo esto de abajo calcula cuantas veses corre el while por segundo
// si esta arriba de 30 es jugable
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw tmp(L"Ultimate 3D chess[");
tmp += driver->getName();
tmp += L"] fps: ";
tmp += fps;
device->setWindowCaption(tmp.c_str());
lastFPS = fps;
}
}
/*
In the end, delete the Irrlicht device.
*/
device->drop();
return 0;
}