gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > vectors

#7664 - hnager - Sun Jun 22, 2003 7:06 pm

I have a project in mind which will work best with the vector template, but I'm having trouble compiling with g++.

This is the error I get:

main.cpp:3: syntax error before `;' token
main.cpp: In function `int main()':
main.cpp:7: `EmptyVector' undeclared (first use this function)
main.cpp:7: (Each undeclared identifier is reported only once for each function
it appears in.)
main.cpp:7: parse error before `;' token


when trying to compile this tiny test to see if I can use vectors:


#include <vector>

typedef vector<int> EmptyVector;

int main(){

EmptyVector testVector;

while(1){}
return 0;
}


Are vectors not supported? If not (or frowned upon) how else would I have an array of sprites(for example) where only active sprites are in the list. If I have 10 onscreen enemies and 2 are eliminated then I want to remove them from the list. This is just a basic example of what I'm thinking of doing...can that be done (efficiently) with a normal array? pushing and removing elements at runtime.

#7688 - tepples - Sun Jun 22, 2003 11:57 pm

If you're not experienced with the STL, try writing your own vector class or bringing up your issues on [url=news:comp.lang.c++]comp.lang.c++[/url].

If you want to allocate efficiently from a pool of up to 32 elements, store a 32-bit "bitmap" of which registers are free and then do a search for a free element. The following untested code should find the first set bit in a 32-bit integer:
Code:
int bitfield_find_first_one(u32 free_map)
{
  int free_idx = 0;

  if(!free_map)  /* if there are no set bits, fail */
    return -1;
  if(!(free_map & 0x0000ffff))  /* if there are no set bits in lower 16, then shift */
  {
    free_idx += 16;
    free_map >>= 16;
  }
  if(!(free_map & 0x000000ff))  /* if there are no set bits in lower 8, then shift */
  {
    free_idx += 8;
    free_map >>= 8;
  }
  if(!(free_map & 0x0000000f))  /* you get the idea */
  {
    free_idx += 4;
    free_map >>= 4;
  }
  if(!(free_map & 0x00000003))
  {
    free_idx += 2;
    free_map >>= 2;
  }
  if(!(free_map & 0x00000001))
  {
    free_idx += 1;
    free_map >>= 1;
  }
  return free_idx;
}

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#7691 - hnager - Mon Jun 23, 2003 12:31 am

Are we talking about the same thing? Im looking for a vector template as an alternative to using the standard array because of the fact that I may not know how many elements I need until ruintime aswell as the neccesity to remove() elements at runtime.

#7692 - hnager - Mon Jun 23, 2003 12:40 am

Ah - found the answer - looks as if vector is defined in the 'std'
namespace (not global). So :

typedef std::vector<int> EmptyVector;

Thanks again - does anybody use vectors in their gba dev?

#7693 - hnager - Mon Jun 23, 2003 1:04 am

not so fast...I'm getting all kinds of errors :

unsupported relocation error
undefined reference to `write'

and that's when trying to simply push_back() !!

If anybody has working examples of vectors in gba development, let me know (especially if you are willing to let me have a peek at working source).

thanks.

#7695 - tepples - Mon Jun 23, 2003 2:18 am

hnager wrote:
not so fast...I'm getting all kinds of errors :

unsupported relocation error
undefined reference to `write'

The 'undefined reference to write' is discussed in the "C and C++" section of this board's FAQ. The 'unsupported relocation error' is a linker bug that may have been fixed in the version of Binutils used in DKA R5b3.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#7697 - hnager - Mon Jun 23, 2003 3:51 am

Thanks and thanks ;)

#7710 - Sweex - Mon Jun 23, 2003 11:37 am

I did get STLPort working for the GBA, however, I decided to write my own vector class to have a bit more control over it than using STL.

#7914 - hnager - Sat Jun 28, 2003 1:38 pm

I got vectors included without compile errors - however, push_back() causes an unexpected result - the screen (in visualboy advance) turns all white...without that line the screen displays what it's supposed to - in this case I'm using Lupin's DMA Text to just display text. Below is the relevant snippet. thanks again.

#include <vector>
#include "gba.h"

typedef std::vector<int> INTVECTOR;

int main(){

...

INTVECTOR theVector;

theVector.push_back(5);

...

while(1){}
return 0;
}