temporary_user_name
temporary_user_name

Reputation: 37068

Creating a cursor in Allegro?

I'm writing a basic game.

I want a cursor image with an actual proper outline for collision detection. This is NOT for the mouse-- I want little cursors flying around in my game, and I don't want their bounding box to be a rectangle.

But I'm new to Allegro, and graphical programming. My first idea was to use al_draw_polygon to create a standard cursor look-a-like, but then I realized I have absolutely no idea how to figure out what vertices to feed it. Is there a better way of doing this? Also, what is the standard method for figuring out the coordinates of vertices relative to one another when drawing a polygon? Surely you don't draw up a graph on paper and calculate it out every time?

The other way I thought of was to load an image of a mouse, but then it'd be rectangular. How do I do this properly?

Note: C++. Figure that was a given, but just in case.

Upvotes: 2

Views: 537

Answers (2)

Apprentice Coder
Apprentice Coder

Reputation: 17

It's hard to understand what you mean, but you may; Want to add the mouse into your program, add it into a loop(normally on a timer so things refresh even when no input is given) and use the coords you get from the cursor to then draw your cursor.

There are 2 methods of Collision Detection- There's the pixel perfect method, checking for transparancies and There's the hit box method, where you check if the x / y attempts to move beyond a box, and if so move it back, or deduct health, whatever you intend it to do.

Both of these methods are covered in the Allegro 5 wiki. I hope I was of some help. To create a bunch of cursors flying in different directions create a random direction int using something like srand();, then add a velocity, the result would be a vector, next each cursor has to call a collision function, to keep it from exiting the sides of the game window for example I would create a windowheight and windowwidth integer and then check to see if the x or y of the object is out of bounds.If so, "bounce" the cursor back, or change the direction of it. Then you need to do the same except with the "player", comparing to see if the current x/y is not overlapping the other x/y and if so, then do something.

Upvotes: 1

Matthew
Matthew

Reputation: 48294

You are asking both how to draw the cursors and how to do collision detection. While related, those are two separate topics.

If you are just starting off, then I would recommend loading 32-bit RGBA PNG files for the cursors. The alpha channel will allow you to draw non-rectangular images. You can also rotate and/or scale the images.

Then you have to worry about collision detection. That really has nothing to do with Allegro. You could use any 2D physics library or just look up formulas on how to check if a line collides with a triangle, etc. It's easiest to just start by approximating a box around the vital area of the object. You can always build more advanced collision detection later.

Upvotes: 1

Related Questions