Advertisement

a dynamic array of pointers

Started by January 20, 2001 12:55 AM
2 comments, last by cow_in_the_well 24 years ago
for my sprite system i''m making a dynamic array. since the bitmap type has to be a pointer is has to be a pointer to a pointer. here is my code that adds a frame to the end of the array:
  
// p_jiBitmap is a pointer to a jiBitmap

// Sprite->frame is declared as a p_jiBitmap *frame;


//=================================================================================

// Adds a frame to a sprite from a bitmap to end of animation

//=================================================================================

jiBoolean jiSprite_AddFrame (jiSprite *Sprite, jiBitmap *Bitmap)
{
    int w,h,bpp;
    p_jiBitmap *NewList = NULL;

    // Check pointers

    if (!Sprite)
		return FALSE;
    
    if (!Bitmap)
		return FALSE;

    // check specs on new frame

    jiBitmap_GetSize (Bitmap, &w, &h);
    bpp = jiBitmap_GetColorDepth (Bitmap);

    if (Sprite->frame_count == 0) // none, just use this size for all

    {
		Sprite->width = w;
		Sprite->height = h;
    }
    else // make sure is same size as the others

    	if (w != Sprite->width || h != Sprite->height)
			return FALSE;

	// Increase frame count

	Sprite->frame_count++;

    // Allocate new list

    NewList = new p_jiBitmap[Sprite->frame_count];
    NewList[Sprite->frame_count-1] = Bitmap;

    // replace old list

    if (Sprite->frame_count > 1)
		delete Sprite->frame;
    Sprite->frame = NewList;

	if (Sprite->frame_count == 1)
		Sprite->current_frame = 1;

    return TRUE;
}
  
whats the best way of adding an item to a dynamic array (of pointers )? thanks thomas ----------------------------- -cow_in_the_well ''When in doubt, empty your magazine.'' - Murphy''s Combat Law

- Thomas Cowellwebsite | journal | engine video

maybe there are more opportunities,but what about linked lists?!

Edited by - castor on January 20, 2001 1:33:53 PM
Advertisement
I don''t know that linked lists are appropriate. I presume that when you create this container of frames, you''d be doing it all at once and in-order. That is, you aren''t going to be inserting or deleting frames from anywhere but the end of the list. Since the list is a list of 32-bit pointers, which are really small and easy to move, you should be able to do it with an STL vector.

  struct Sprite{  //...  vector<jiBitmap*> bmpVec;  //...};// at the end of your function, after you validate the bitmap:  //... your code...  if (w != Sprite->width || h != Sprite->height)    return FALSE;  Sprite->bmpVec.push_back (Bitmap);  return TRUE;}  


There''s no need to store the frame count--that''s m_bmpVec.size (). If you want to loop through the bitmaps, just do this:
for (int i=0; ibmpVec.size (); i++){  jiBitmap *frame = Sprite->bmpVec;  // do whatever you want with frame} 
erfff....been away a few days and had to dig to find this again

anyway...

castor: i just thought that linked lists would be overkill, im just adding to the end not pulling from the middle and i didn''t want to have to search through from the beginning to find a certain frame.

Stoffel: that looks really cool, i should look into the STL a bit more . anyway i fixed my function up so it does it manually. I had just forgotten to copy the bitmap pointers from the old to the new lists.

  ...// Increase frame count	Sprite->frame_count++;    // Allocate new list    NewList = new p_jiBitmap[Sprite->frame_count];        // Copy any old pointers we want to keep	if (Sprite->frame_count > 1)	{		for (int i = 0; i < Sprite->frame_count-1; i++)			NewList[i] = Sprite->frame[i];	}	// Put new pointer at end of list	NewList[Sprite->frame_count-1] = Bitmap;	// Free old memory    if (Sprite->frame_count > 1)		delete Sprite->frame;		Sprite->frame = NewList;  


works so i won''t bother change it

thanks

-----------------------------
-cow_in_the_well

''When in doubt, empty your magazine.'' - Murphy''s Combat Law

- Thomas Cowellwebsite | journal | engine video

This topic is closed to new replies.

Advertisement