delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/22/22:27:26

To: djgpp AT delorie DOT com
Subject: Re: PLEASE help with vectors!
References: <353d29bc DOT 235411 AT news DOT ziplink DOT net> <353D613B DOT D502B628 AT home DOT com> <353e6e9e DOT 405303 AT news DOT ziplink DOT net>
From: M DOT A DOT Bukin AT inp DOT nsk DOT su
Date: 23 Apr 1998 09:01:09 +0700
In-Reply-To: dmt@bigfoot.com's message of "Wed, 22 Apr 1998 22:29:38 GMT"
Message-ID: <20u37lthy2.fsf@Sky.inp.nsk.su>
Lines: 123

dmt AT bigfoot DOT com (Jeff W./DMT) writes:

> NOTE: MapStruct is the main structure, i.e. instead of having DATA as
> type int, it should be type MapStruct.  Sooooo, the problem becomes
> this: When you use the [] operator, how can I make it so that I know
> what field to access??  I.e. what I would ideally like to be able to
> do is to say:
> AMap[40][2].base.tileno = 5;  or something similar.  

---
typedef struct BaseType
{
 //base layer of tiles
 int tileno; //which tile number to load from the datafile
 int opaque; //can you see past the tile? For line of sight algorithm
 int collision; //for collision detection, see #defines
 int flag; //misc flag
};

typedef struct FringeType
//non-pickupable objects
{
 int tileno;
 int flag;
};

typedef struct ObjType
//normal objects
{
 int tileno;
 int active; //shouldit be blitted???
 int flag;
};

typedef struct RoofType
//for the roof of buildings/etc
{
 int tileno;
 int flag;
};

typedef struct MapStruct
//just contains all the other structures into 1
{
 BaseType base;
 FringeType fringe;
 ObjType obj;
 RoofType roof;
};

class TiledMap
{
public:
  TiledMap (void)
  {
    width = 20; //arbitrary starting map size of 20x15
    height = 15;
    size = width * height;
    data = new MapStruct[size];
  }
  ~TiledMap (void)
  {
    delete[] data;
  }

  int resize (const int _width, const int _height)
  {
    if ((_width <= 0) || (_height <= 0))
      return -1;
    width = _width;
    height = _height;
    if ((width * height) > size)
      {
	delete[] data;
	size = width * height;
	data = new MapStruct[size];
      }
    return 0;
  }

  MapStruct& tile (const int _x, const int _y)
  {
    /* FIXME: test for input out of range.  */
    return data[_y * width + _x];
  }

  /* operator[] exhibits implementation details
   * (that tiles on each line are in one array).
   * But it can be faster to access than `tile' above,
   * if indexes values are known at compile time. */
  MapStruct* operator[] (const int _y)
  {
    /* FIXME: test for input out of range.  */
    return &data[_y * width];
  }

private:
  int width;
  int height;
  int size;
  MapStruct* data;
};

int
main (void)
{
  TiledMap map;

  map.resize (1024, 512);
  if (map[123][456].obj.active)
    {
      /* Do something.  */
      map[123][456].obj.flag |= 1;
    }
  else if (map.tile (567, 234).roof.flag)
    {
      /* etc.  */
      map.tile (567, 234).obj.active = 1;
    }

  return 0;
}
---

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019