0
\$\begingroup\$

Hello i'm doing a game in WPF, 2D version (top view)

I have the map and i'm trying to check for color collision in pixel each time the timer is activated

Well...with 1 object it works fine but with more objects at same time it just slow down the process

This is my code every time the dispatchertimer activates and i loop to check for collision on every object

CroppedBitmap cb = new CroppedBitmap(source, new Int32Rect(x, y, 1, 1));
int px = 1 * (source.Format.BitsPerPixel / 8);
byte[] pixels = new byte[px * 1];
cb.CopyPixels(pixels, px, 0);
Color c = Color.FromArgb(pixels[3], pixels[2], pixels[1], pixels[0]);
if (c.R == 255 && c.B == 255 && c.G == 255) {
   pixels = null;
   return true;
}

Any help to simplify the process? I really want to do it in WPF

PS: The map is 1018x731

\$\endgroup\$
0
\$\begingroup\$

I think your main problem here is that you continuously allocate new memory. Instead of creating small buffers for each pixel you want to read, why not create a buffer from all the image pixels and the read directly from there?

Here's how I would modify your code. Disclaimer: I've never used WPF before, so take this with a grain of salt. I don't know how the pixel data is ordered in the array etc.

// -- Don't do this inside the loop, but somewhere else 
// and keep a pointer to the pixels array
// read the entire image into a buffer
CroppedBitmap cb = new CroppedBitmap(source, new Int32Rect(0, 0, 1018, 731));
int px = 1018 * 731 * (source.Format.BitsPerPixel / 8);
byte[] pixels = new byte[px];
cb.CopyPixels(pixels, px, 0);

// -- do this inside your loop
// calculate the offset into the array
int offset = (y * 1018 + x) * 4;
Color c = Color.FromArgb(
    pixels[offset + 3], pixels[offset + 2], pixels[offset + 1], pixels[offset]);

if (c.R == 255 && c.B == 255 && c.G == 255) {
   pixels = null;
   return true;
}
\$\endgroup\$
  • \$\begingroup\$ Thanks a lot it worked. The only problem is that the offset throws a exception but i will take care of it, apart of that is awesome :) Well i have one other question, Is appropriate to loop for 51 items and check the collision for each one? Is there any alternative? Because I already did the dumb test and the application don't move :P Well, i'm just new at game development (graphical interface) \$\endgroup\$ – GameDev Dec 9 '13 at 21:53
  • \$\begingroup\$ @GameDev checking 51 items per update should not be a problem. But that really depends on what your check actually does... Just implement it and if it's slow, then profile it to find the problem. That's all the advice I can give you. \$\endgroup\$ – bummzack Dec 9 '13 at 23:10
  • \$\begingroup\$ should the problem be wpf and to not be a good plataform to develop such a game or it's programming problems like above? \$\endgroup\$ – GameDev Dec 9 '13 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.