PocketMatrix forums

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

CGApiSurface as attribute of a class

Hi,

I am rather new to programming C++, mostly I program in C# and VB. If this is a stupid question, sorry for that. I always try to solve the problems I face by myself, but I find this problem a bit harder to solve.

What I am trying is to make a game with GapiDraw with a object oriented framework I want to build. For the GUI, I made a button-class. lButtonImage is of type CGapiSurface and is a property of CMensButton. All is going well, but when I try to initialize the surface, I get error -932839296, Surface not initialized. Here is the code. I am aware of the fact that I do not use standard naming conventions. I will change that soon, first I want to get the basic things right.

Code:

class CMensButton
{
RECT ButtonSize;
long srcX;
long srcY;
long srcWidth;
long srcHeight;

public:
__declspec(property(get = _getLButtonImage, put = _setLButtonImage)) CGapiSurface lButtonImage;
CMensButton( )
{
//nError = imgMain->CreateSurface(TEXT("mainmenu.gfx"));
_lButtonImage=new CGapiSurface();
};

virtual ~CMensButton() {};

CGapiSurface _setLButtonImage( TCHAR* anImagePath )
{
_lButtonImage->CreateSurface( anImagePath );

return *_lButtonImage;
}

CGapiSurface _getLButtonImage()
{
return *_lButtonImage;
}

private:
CGapiSurface *_lButtonImage;

};


I use this in my calls:

Code:

// This is in my class as local variable
CMensButton *appButton[4];

// This is in my initGFX() function, which is called in the
// CreateSurfaces() function.

appButton[0] = new CMensButton();
appButton[0]->ButtonSize.left = 0;
appButton[0]->ButtonSize.right = 120;
appButton[0]->ButtonSize.top = 0;
appButton[0]->ButtonSize.bottom = 30;
appButton[0]->lButtonImage = TEXT("mainbuttons.gfx");


If someone can help me out with this, I would be very grateful. Thanks in Advance!

Hiu.
Here are some excerpts from the help file that may help. Sorry if you've already been over this.

Supported image file types are BMP, GIF, JPG and PNG on all devices. BMP images can be RLE-encoded (1, 4, 8, and 24-bit BMPs are supported).

I'm not sure what kind of file your *.gfx files are. I also don't know if GapiDraw goes by extension when determining files or if it interrogates the file itself to determine its type.

When images are loaded from file, GapiDraw will use an absolute file path if pImageFile begins with "\" or "x:\". If pImageFile does not begin with "\" or "x:\", GapiDraw will use the application path of your executable file and append the image file name (e.g. if your executable is "\Program Files\Games\MyGame\MyGame.exe" and you call CreateSurface with pImageFile set to "MyPicture.png", GapiDraw will try to load the image as "\Program Files\Games\MyGame\MyPicture.png").

Do you have your graphics files in the same directory as your exe file?

By the way, are you using C++ Builder? I never really worked with it, but that's what it looks like to me. I was more of a Delphi person, myself. :) I'm using Visual C++ for GapiDraw.
Hi,

Thanks for your reply. I am able to show a bitmap on the backbuffer, I have a mainscreen. What I want to accomplish is to make a class "button" which will draw itself on the surface you want.
I think it has to do with pointers etc. I guess.
Leave it up to MS to make some odd langauge modification, and have it used.

I assume the error you speak of appears when you uncomment that line in the constructor? Or when that line is uncommented you still don't see the button?


Question for you, why even start using names that aren't allowed? (Its not that they are non-standard, they are reserved for the compiler's use.) Which could be your problem now, some name conflict somewhere, its a long shot.

It doesn't appear to be a pointer problem, as far as I can tell. There seems to be quite a bit of needless complication going on though, why make your surface a pointer when you will just dynamically alloc space for it in your constructor. (And is bug-prone as evidenced by your code, which never deletes that space.)

Another thing that comes to mind is that the CreateSurface call with no width and height supposedly erases the surface, which I assume to be the default values.
The problem could also be in your update function which wasn't present so I can't comment.

The Surface class has a save function which can write the surface to a file, try using that to see if that surface is valid after you try to load the button image.


HTH
Hi,

Thanks for your reply. To test what is going on, I restructured my class a bit. I returned the class to some bare functionality. To do this, I have a function "Draw", which I put in the same application class:

Code:

HRESULT Draw( CGapiSurface& aBuffer )
{
aBuffer.DrawText(100, 100, TEXT("TESTTEST!"), RGB(255, 255, 255),
GDDRAWTEXT_CENTER | GDDRAWTEXT_BORDER, 0, NULL);

return S_OK;
};


This works. But when I put it in another class:

Code:

HRESULT CMensButton::Draw( CGapiSurface& aBuffer )
{
aBuffer.DrawText(50, 100, TEXT("TESTTEST!"), RGB(255, 255, 255),
GDDRAWTEXT_CENTER | GDDRAWTEXT_BORDER, 0, NULL);

return S_OK;
};


It refuses to draw the text on the buffer. The functions are identical. The call to these functions are also the same. The only difference is, one is in a seperate class and one in the application class.

The seperate class itself seems to work. I can fill a property, a RECT, without hassles. And I can retrieve the values.

I am a bit puzzled...
I understand the problem as I've run into it before as well. Try this - instead of passing the surface buffer, let gapidraw determine what that buffer really is (the backbuffer can change!). You can do this by telling your class what your CGapiDisplay pointer is at surface creation time (and saving that pointer), then using that pointer when you want to draw your button(s) to determine what your backbuffer is -

CGapiSurface backbuffer;
hr = m_display->GetBackBuffer(&backbuffer);
if(hr != GD_OK) return hr;

Now you have the backbuffer which you can draw your button on. This will work everytime.

-- GR