Another game of mine...
I am working on another game, and have mostly completed, but I need help on the collision (sorta) detection. When the guy swings his sword, I want the program to know if he hits the monster. I've gotten the upwards swing one mostly in (it still messes up, try swinging it to the right of the monster, but nowhere near him), and I'm try to put in the downwards swing one, but so far, it hasn't worked.
(The zip attached has all of the .bmps, .cpps, and for those trusting folks out there, a .exe. But you can compile it if you have allegro and the Visual C++.)[FONT=arial][FONT=arial][FONT=arial]
Where's the zip? I'm probably being stupid, but I can't figure out how it is attached..
Hmm, seems the attachment didn't work. Try this:
Alright, a couple comments.
First, the following lines are repeditive in what they do:
BITMAP *guy = create_bitmap(90, 90)
guy = load_bmp("cdown.bmp", default_palette);
The load_bmp function will allocate the memory for the *guy BITMAP, so ya only need to call it.
Then, at the end of the proggy, don't forget to deallocate it with the destroy_bitmap() function. allegro_exit() may do this for you, but destroying the bitmap is the #1 way to ensure you don't cause a memory leak.
Now then, the collision detection:
if ((ex - sx) < 90 && (sy - ey) < 90 && (ey - sy) < 90) ...
This is a tad confusing, but if I understand correctly, you are trying to say, if the enemy x is within 90 pixels of the sword x and the enemy y is also within 90 pixels of the sword y, then a collision has occured. Here would be the way I would do this:
if(ex > (sx - 45) && ex < (sx + 45) && ey > (sy - 45) && ey < (sy + 45) ...
This makes a nice neat box of collision detection, and you aren't worried about negative values and such.
Hope that helps. (Kind of a late reply, so you may have figured that out already.) :p
P.S. Looking good so far. You'll have the next best RPG/Action Adventure going in no time at all. (If you are having a problem with flickering and are running 2K, see this post on some of the quirks of DirectX in full screen.)
Just a quick question related to that BITMAP thing. How about functions that return a BITMAP - ie. how to destroy those ones?
blit(foo(), screen, 0, 0, 0, 0, x, y);
So foo() creates a bitmap and then returns a pointer to it, right? Probably, allegro_exit() will clear that up, but if you want to be sure, you'd better have a pointer waiting in bar() to pick it up.
blit(Temp, screen, 0, 0, 0, 0, x, y);
But both of them still go to blit as `source':
> BITMAP *Temp=foo();
> blit(Temp, screen, 0, 0, 0, 0, x, y);
void blit(BITMAP *source, BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height);
And when it's the same BITMAP all day long it's actually just wasting time to create that `Temp' (!!! since when was that written with capital first letter :-), won't you say?
Anyway, perhaps it ultimately better - for the sake of a readable source and memory handling - not to return those bitmaps....
It probably doesn't hurt to do it the way you were, so maybe the motto "if it ain't broke, don't fix it" applies here. Try your way, and if you run into any memory leaks, you'll know how to fix 'em. :)