PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Surfaces not loading when using vector?

Hi, I'm having a problem (of course...).
I load a few different surfaces, for example... a background and foreground. In between the two I want to paint a number of objects, all of which are stored in a vector container.

It seems like they are being stored in the vector just fine, but the surfaces never load. I've done a few tests, like the width of the surface etc., and it returns less than 1 every time.
Other surfaces (background/foreground) work fine. The vector stored surfaces do not.

Anybody have a clue why this would be? Here is a largely trimmed down version of the code. both classes. I just loop through the vector and check each .graphic and/or blit it.

Thanks, hope you guys can help!

Code:

class Object
{
public:
CGapiSurface graphic;
double xPos;
double yPos;

int objectType;

Object()
{
xPos = 160;
yPos = 120;
};
void loadGraphic(int objType, const TCHAR* pImageFile)
{
objectType = objType;

graphic.CreateSurface(GDSURFACE_SYSTEMMEMORY, pImageFile);

COLORREF controlPixel;
graphic.GetPixel(0, 0, &controlPixel);
graphic.SetColorKey(controlPixel);
};
~Object()
{
graphic.FreeSurface();
};
};

class ObjectManager
{
public:
int howManyObjects;
int maxObjs;
std::vector<Object> objectList;

ObjectManager()
{
howManyObjects = 0;
maxObjs = 2;

for (int i=0; i<maxObjs; i++)
{
spawnObj(P_TYPE_ONE, TEXT("\\graphicTemplate.bmp"));
}
};
int spawnObj(int objType, const TCHAR* pImageFile)
{
Object newObj;
newObj.loadGraphic(objType, pImageFile);

howManyObjects += 1;
objectList.push_back(newObj);
return howManyObjects;

return 0;
};
~ObjectManager(){};
};
I can access objects stored in a vector container like so, correct?

Code:

for (int i=0; i < objectManager.objectList.size(); i++)
{
rasterizer.drawObject(&objectManager.objectList[i].graphic);
}
I'll bet you didn't check those return codes did you? :wink:

Honestly, that would be where I would start debugging this. Make sure CreateSurface isn't failing.
Yeah, I've managed to create a class that will finally let me see any error codes. I'll toy with that tonight, i hope.

Even so, there isn't any reason it should fail is there? I sure can't think of one.
You'll want to verify the path information for your image file. Based on what you have listed, the image is located in the root directory.
Yeah, everythign runs from the root.

Plus, I load at least 3 other surfaces, with no problem.

It has somethign to do with the way i'm doing it... i'm sure of it. But I can't possibly guess WHAT i'm doing wrong.

I may end up instantiating the object with "new" and just passing a pointer into the vector container. I have a feeling that will work, for some reason. I hate to do it. And i'm gonna have to worry about deleting them too. Le argh.

Anybody ever try to do something like this, pushing an object with a surface into a vector? Problems? No problems?

Thanks
I've not used vector, but have used arrays of surfaces with no problem. Using a vector shouldn't be an issue. Without seeing the actual code, I can only guess at what the problem may be. I would first check those return codes...
That's pretty much the code, actually...

I mean, you can use that same code to do whatever is needed...

I'll check the return codes when I can though. thanks.
Pejo Software - Per · 05 Mar 2004, 22:15 UTC
When you add you Object to the vector you are making a copy and since CGapiSurface doesn't have a copy constructor that copies the image data the Object in the vector will not contain the image data.
Ah! Excellent, thanks! I thought it might be doing something like that, though I don't know what a 'copy constructor' is.

So I must first create the object, copy it to the vector, THEN load the surface, eh?

Thanks!

Anyone wanna explain what a copy constructor is? :)
When defining classes where the datafields include pointers, and memory is allocated dynamically, you really need to also define a copy constructor. This is a function that specifies how you should copy the fields. If you don't specify this, then whenever a class is copied (e.g., when using it as a call-by-value argument) it may not be copied right; datafields may point to an original version of some data, rather than a copy. This may cause weird errors.

When an object is copied, space is allocated for the contents of each datafield, so in the copy we'd have space for str and s. But no new space would be allocated for what str points to, and indeed that is not copied. This is called "shallow'' copying, as only the top level is copied; the copied pointers in datafields will still point to the exact same bit of memory.

This is probably not what we want. And to avoid this we need to define a copy constructor that does what's called a "deep" copy. Here's an example:

Code:

MyType::MyType(const MyType& another) {
int i;
s = another.s;
str = new char[s];
for(i = 0; i<size; i++)
str[i] = another.str[i];
}


This may be particularly important if we are going to use arguments of MyType as call-by-value arguments to functions:

Code:

myfn(MyType x) {
some stuff
}


This is legal, but much more often you'd be dealing with pointers to objects of type MyType, or use call by reference.

Anyway, if you WERE using call-by-value then copies of the object would be created when the function was called. If you want call by value to behave as you would expect (not resulting in the parameter being modified when the fn exits) then you'd have to define a copy constructor.
Still not working.

I dunno what format to specify that an HRESULT is, so I'm using %0u for _stprintf to display the value.

That gives me: 3362128032

How do I find out what that sort of thing means?
Compare the result to the constants in GD. For instance, you can generally do:

HRESULT hr;
hr = graphic.CreateSurface(GDSURFACE_SYSTEMMEMORY, pImageFile);
if (hr != GD_OK) {
// Do some sort of error handling
}

You could do something as simple as:

if (hr == GDERR_BITMAPNOTFOUND) { // Pop error message }
if (hr == GDERR_INCOMPATIBLEPRIMARY { // Pop error message }
if (hr == GDERR_INVALIDBITMAP { // Pop error message }
if (hr == GDERR_INVALIDPARAMS { // Pop error message }
if (hr == GDERR_INVALIDSURFACETYPE { // Pop error message }
if (hr == GDERR_LOCKEDSURFACES { // Pop error message }
if (hr == GDERR_OUTOFMEMORY { // Pop error message }
if (hr == GDERR_SURFACELOST { // Pop error message }

Those are all the errors defined for this function to return.

Personally, I have a simple wrapper function that I call in place of CreateSurface that handles this so I don't have a bunch of repeated code. I suspect everyone does a very similar thing too.
Okay, it's this:

GDERR_INCOMPATIBLEPRIMARY

What does it mean, and how do I fix it? :(
And finally, where can I find a list describing these error messages? Thanks!
It's in GapiDraw.h. GDERR_INCOMPATIBLEPRIMARY means that you have not yet opened the display, which is crucial since displays use lots of pixel formats such as 444 (common on symbian), 555 (win95), and 565 (pocket pcs).. First call CGapiDisplay::OpenDisplay and then create your surfaces.
Aha! So, checking your return codes would have saved you alot of time. :wink:
G.R. likes when his oft-repeated "check the return codes" mantra is validated! :)

It would probably be annoying as much as he writes it in this forums, if it wasn't for the fact that HE'S 100% RIGHT!! :)
Yeah. Even so, it took a bit before I knew what the code meant ;)

But thanks, good advice.