# Another c++/directx match 3 puzzle question

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

## Recommended Posts

You guys are probably sick of me by now, but I'm not through with you, not by a long shot! :) Ok, Im going to get laughed at I know, but please, if all you can think of to answer this is to mock or say, woa, what a noob, I already know that, so just don't post. Thanks. The question is really about optimization, and since I like to program the engine code, puzzle game logic is all new to me. Here we go. I load the sprites like this: sprite1_pic=LoadTexture("Resources/whitesprite.bmp",D3DCOLOR_XRGB(255,255,255)); sprite2_pic=LoadTexture("Resources/bluesprite.bmp",D3DCOLOR_XRGB(255,255,255)); etc.. I declare their properties like this: sprite_1.x=150; sprite_1.y=120; sprite_1.width=100; sprite_1.height=100; etc... I go to the render function and do this: std::vector<LPDIRECT3DTEXTURE9>sprite1; sprite1.push_back(sprite5_pic); sprite1.push_back(sprite2_pic); sprite1.push_back(sprite3_pic); sprite1.push_back(sprite4_pic); sprite1.push_back(sprite1_pic); std::vector<D3DXVECTOR3>spritepos1; spritepos1.push_back(pos1); spritepos1.push_back(pos2); spritepos1.push_back(pos3); spritepos1.push_back(pos4); spritepos1.push_back(pos5); then draw like this: for(int i=0;i<sprite1.size();i++) { Sprite_Handler->Draw( sprite1, NULL, NULL, &spritepos1, D3DCOLOR_XRGB(255,255,255)); } Now, all this is fine if this was the WHOLE GAME, but obviously, its not. First if all, this code is only to draw ONE ROW of sprites, and there needs to be 5 rows on the board at a time, so I would need to declare 5 sprite vectors, 5 d3dxvector vectors, and a whopping 50 pos's!!! I am very willing to do this, but I am afraid of the consequences to the framerate. I am doing it this way, because, in my noob head, one of the only ways I can think of to check for triple matches is with sprite based collision, and using sprite based collision, each sprite has to have its own defined position. So, for the 5 blue sprites scattered around the board, I will have to say something like: if(sidecollision(b_sprite1,b_sprite2)&& sidecollsion(b_sprite2,b_sprite3) { a match has been made }...FOR ALL THE POSSIBLE COMBOS OF ALL 5 COLORS!!! Again, I am willing to do this, but I am afraid this will totally kill the performance. So.. Am I right, am I wrong, What do you think, what should I do?

##### Share on other sites
woa, what a noob! :) just kidding.

Your approach is very linear. I would suggest taking a step back and trying to make it more object oriented. For instance, you have a list of dx 9 textures and a list of positions (which seem to coorilate one-to-one to eachother). So, I would then create a Sprite object:
class SpriteObj{public:   LPDIRECT3DTEXTURE9 Texture;   D3DXVECTOR3 Position;}//So you can now just have one list:std::vector<SpriteObj> spriteList;

I am guessing you have a 5x5 board (25 sprites). So when you render your sprites, you will only draw 25 per frame.

I don't quite get your side collision, but you should be able to loop through your objects and test them:

//starting one in and ending one less so we can test -1,0,1;for(int i = 1;i < spriteList.Count()-1;i++){   if (CheckCollision(spriteList[i-1],spriteList,spriteList[i+1]);   {   //you collided and have spriteList in the middle.. DO SOMETHING!   }}

Hope this helps :)

Jeff.

##### Share on other sites
Thanks very much Jeff! But since I am new to this particular kind of logic, I am afraid I will have to ask you to expound a bit more. for instance, Is your CheckCollision function a user-defined function, or is it from STL or similar? Also, in your suggested std::vector<SpriteObj> spritelist, how do I assign a specific texture and position to each sprite in the list? You will have to be very patient with me, as my linear thinking is outweighing my better reason! Sample code helps too.

Thanks!

##### Share on other sites

Not a problem. First, my CheckCollision function is a user-defined function that would probably encapsulate your sidecollision function. Assuming you already have sidecollision, you could do something like this:

void CheckCollision(SpriteObj* left, SpriteObj* mid, SpriteObj* right){   //validate pointers   assert(left);   assert(mid);   assert(right);   // your code you pasted   if(sidecollision(left->Position,mid->Position)&& sidecollsion(mid->Position,right->Position))   {   //a match has been made   };}

Get it? Again, I'm not sure what your sidecollision does (I am guessing it checkes a collision against the two x,y,w,h positions.

Quote:
 Also, in your suggested std::vector spritelist, how do I assign a specific texture and position to each sprite in the list?

My SpriteObj is pretty primitive. Meaning, you may want to put accessor functions and other functions for position, etc. If you used my example, you could just do something like:

//not listing all the parametersD3DXCreateTextureFromFileEx(..., &spriteList[0].Texture);D3DXCreateTextureFromFileEx(..., &spriteList[1].Texture);//etc.

Jeff.

##### Share on other sites
Ok, I kind of get what you're saying, but here are the problems:

First of all, you're assuming I'm much smarter than I am. :) My side collision creates a Rect that passes throught the center of the sprite and extends on both sides, see?

int SideCollision(SPRITE sprite1,SPRITE sprite2)
{
RECT rect1;
rect1.left=sprite1.x+10;
rect1.top=sprite1.y+5;
rect1.right=sprite1.x+sprite1.width+15;
rect1.bottom=sprite1.y+sprite1.height-20;

RECT rect2;
rect2.left=sprite2.x+10;
rect2.top=sprite2.y+5;
rect2.right=sprite2.x+sprite2.width+15;
rect2.bottom=sprite2.y+sprite2.height-20;

RECT dest;
return IntersectRect(&dest,&rect1,&rect2);
}

The problem with this of course, is that it only works with 2 sprites. By the way, my sprite struct:

struct SPRITE {
int x,y;
int width,height;
int movex,movey;
int curframe,lastframe;
int animdelay,animcount;
int scalex, scaley;
int rotation, rotaterate;
};

Again, a very primitive way of doing things. Also as you have probably figured out by now, my framework is not object-oriented. It was my first engine, and I slowly added to it, mainly for fun. I would use object oriented framework, but unfortunately, I'm on a deadline, which is why I was using this one since it is at least functional.

Please instruct me on how to do the 3-way collision with pointers, as your check collision function seems to be suggesting, or just help me figure out where to go from here, as now I'm feeling really lost!

Thanks Again!

P.S. Also, doing it the way I was doing in my first post, would there be a signifigant framerate collapse, or is it just taking the long,linear route, which is not neccessarily bad for beginners?

##### Share on other sites
After looking at it again, I am not quite so lost. Since My framework isn't object oriented, I could just make the SpriteObject Class into a struct, right? As for the collision, I am still in the dark about how to detect matches of 3 same colors, both horizontally and vertically. Perhaps you could still help me out there?

Thanks again,again!

-Brandon

##### Share on other sites
look into using graphs

1. 1
Rutin
23
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 16
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002151
×