Hey, just joined, hi! :)

Ok, well, I'll start of with, I'm fairly familiar with c++ i think... Well kind of. Theres a lot i don't know, but im familiar with classes and stuff. Hopefully that'll give you an idea.

Anyway, my problem! I was working on a simple game using the Allegro SDK, and its like a Zelda type game, you have the top down view, and i want to place objects down. I was working on a map editor to start with, but my problem was, how do i store the X and Y coordinates of so many items? I'll have house sprites, tree sprites, etc etc, LOTS! And, well first i thought, a simple X and Y array for every object. But, for every object? That would get annoying, but possible i guess. How should i do this?

If you could kind of comment on any code you paste, that would be great to help me! :)

Thanks in advance!

Aaron :)

Usually one would use Vectors to store the positions. Like so :

struct Vector{
 float posX;
 float posY;

And if your Tree sprite class is something like this :

class Tree{
 private : 
  Vector position;

You would use composition inside your Tree class. Then if you want
500 trees, you can use an array like so :

Tree allTrees[500];

Now each tree has an x and y position. All you need to do now is
strategically place the trees in position, perhaps using loops or other

Check out gamedev.net. There are a lot of tutorials out there for how to do this kind of thing. I'm not sure what Allegro contains, but you may want to look into some tile engine development stuff. With a tile engine, you could make a separate application to create maps with different tile textures and properties and even multiple layers to get that hide behind object effect. This would also alleviate the need to give exact X & Y coords for each tree/house/etc...

Wow, quick reply, thanks!

I should have a class for each item? Didn't think of that...
I'm not quite understanding:
Vector position;
You would use composition inside your Tree class.

Sorry, hope I not asking of too much haha ;p



@ Sodabread, replied after i refreshed lol. I Will do that also, Cheers!

Composition just means, use another class object inside a class.

You don't have to make a Tree class, if you want you can do something like this :

class Sprite{
 Vector position;
 Vector velocity;
 Image img;

The point is that use Vector to store the position of an object by
using composition, in other words, by creating an Vector object inside
our Sprite class. As you see, you can even store the velocity( speed and direction) of the sprite easily. Here is an example that might help :

#include <iostream>
#include <ctime>
using namespace std;
struct Vector{
 float posX;
 float posY;
 Vector() : posX(0.0f) , posY(0.0f){}
 Vector(float x, float y) : posX(x) , posY(y){}
class Sprite{
 Vector position;
 Vector velocity;

 Sprite(float posX, float posY, float spdX, float spdY){
  position = Vector(posX,posY);
  velocity = Vector(spdX,spdY);

int main(){
 const int NUM_OF_TREES = 10;
 Sprite sprite[NUM_OF_TREES];

 for(int elem = 0; elem != NUM_OF_TREES; ++elem){
  sprite.velocity.x = rand() % 10;
  sprite.velocity.y = rand() % 10;
  sprite.position.x = rand() % 10;
  sprite.position.y = rand() % 10;

Now their maybe error because the above code is not compiled. But
the point is the main idea. I did not have to create a posX and posY
variable for each Tree sprite. Instead I let the for loop
do the work, and let it generate random speed and position for
each sprite.

Hey, That looks good, thanks. I'll get back to tell you guys how it goes.

Cheers :)

Hey people :)

Ok, I was still kind of confused by the stuff here. But I did this, and was wondering if you could tell me if its OK to use.

First I created a class called Sprite, in it I have 2 arrays, x and y.
You create, like, Sprite tree; for instance, then call set_variables() (is 'call' the correct word?...) That will set the stuff in it to what it needs to be. Then, you can just call, for example:


And it adds it to the array.

Then, in one of the loops, to draw it, I just call, for example again:


Buffer, being the buffer, and 1, being the item to draw. The cool thing to me is that it's all in 1 class! Because ive never had it this clean before. I don't know if you guys would still find this messy or inefficient? I hope not.

Anyway, once again, any information, bad or good, is highly appreciated.

Cheers! :D

Generally you do not want a class doing all of that. A good idea would
be to split up the work so that a class has its minimal objective to fill.
So instead of having the class do everything, split the jobs up. This makes
it more readable and cleaner.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.