PocketMatrix forums

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

Pointer related question

Sorry, this might not be the right place for it, but it's involving gapidraw, so why not? :) (Wish there was a general programming forum here).

Pointers seem to always get the best of me. Everytime I think I get it, they suprise me or do soemthing unexpected.

I was able to get a sprite drawn and moving around the screen in gapidraw... which I felt was a good start.
So I intended to add some functionality to it... I would create a class for a "ball", and create a few instances of it.
With that in mind, each ball would in essence draw itself, and track it's own coordinates, etc... nothing too complex, I'd venture.

On my CGapiApplications ProcessNextFrame(), I placed the following code:

Code:

pBackbuffer->FillRect(NULL, RGB(128, 128, 128), 0, NULL);
ball1.move(&pBackbuffer;);
return S_OK;


ball1 had been created, so no worries there. The basic ball code was as follows:

Code:

class Ball
{
public:
int x;
int y;
int speed;
CGapiSurface graphic;

Ball()
{
x = 0;
y = 0;
speed = 1;
graphic.CreateSurface(GDSURFACE_SYSTEMMEMORY, TEXT("\\ballGraphic.bmp"));
}

int move(CGapiSurface* pBackbuffer)
{
x += speed;
y += speed;

pBackbuffer->BltFast(x, y, & graphic, NULL, 0, NULL);
}
~Ball()
{
graphic.FreeSurface();
}
}


So there it is basically. It compiles, but does nothing.

the line that says:
ball1.move(&pBackbuffer;);

I changed around to try a number of things, since I felt more and more confused about the pointer system, the more it failed.
I tried:
ball1.move(pBackbuffer); // Assuming pBackbuffer contained the address of the buffer
and
ball1.move(&pBackbuffer;); // Assuming I needed to pass the address.

So can anyone help out w/ this? I'd appreciate it a lot! Sorry if this in the total wrong location, but I am trying to get a handle on using gapidraw :)
BTW, there was a little additional functionality of course. incrementing x and y by speed each time will just move it diagonally until it reaches the border of the screen, I nkow. But I'm mostly concerned in getting anything on the screen working, and so excluded the code the control that.
Hello

You get a pointer to the backbuffer in ProcessNextFrame, so you should remove the & in your function call.

I guess the problem with your approach is that you create your Ball objects before you opened the display. Try calling the constructor of your ball objects in CreateSurfaces.

A good idea when encountering problems like this is to check the return values of the GapiDraw operations. If the problem in this case is what I think it is, CreateSurface in your constructor returns GDERR_INCOMPATIBLEPRIMARY. All GapiDraw return codes can be found in the file gapidraw.h.

//Erik
Volte6 wrote:Sorry, this might not be the right place for it, but it's involving gapidraw, so why not? :) (Wish there was a general programming forum here).


Um...http://www.pocketmatrix.com/forums/viewforum.php?f=5
Thanks Pam :) The name/description just didn't seem very coding oriented for some reason ;)

Thanks for the suggestion ruis, i'll try that when I get home. For some reason I felt like I tried nearly every combo (with &, without, etc.), and after I realized I was just randomly typing crap out, gave up :)
From a quick look, it would seem that you have the basic call set up correclty (your ::move function). However there are other factors at play that you may (or may not) have considered.

After initializing the display, create all your surfaces, objects etc.. you will need to perform a "flip" after blitting your stuff to the backbuffer.

example:

....
CGapiSurface* pBackBuffer = pDisplay->GetBackBuffer( );
BlitObject( pBackBuffer );
..
..
pDisplay->Flip( );

One thing though, I would never have a 'CreateSurface' function or anything like that in the class constructor. Simply because you have no decent recourse should the call fail.

Think of the app like this: You're a painter and you must paint a full frame each tick. Every frame is a new picture. So you have to draw/blit all of your objects/surfaces unto the backbuffer and then when you're ready you simply do a swap on the buffers (eg. Flip()). Then you begin a brand new picture.

Without calling "Flip()" you will simply be drawing on the buffers without revealing anything to the display.

Inside "BlitObject" you may have something like:

void BlitObject( CGapiSurface* pBackBuffer )
{
pBackBuffer->BltFast( x, y, m_pSomeSurface, NULL, 0UL, NULL );
}

Where 'm_pSomeSurface' is a color buffer which would contain a sprite or something.

It seems the basic syntax you have is correct, but what your not showing is the rest of your app. So it's hard to say what the real problem is. There could be other things you may (or may not) have missed....it's hard to say within the forum.

I would ensure that you're correctly initializing the display, loading your surfaces, calling flip, not doing something like clearing the backbuffer after you've drawn on it - stuff like that.

I hope that something I've written can be of some help. Good luck!
Well, I'm using the CGapiApplication framework, as suggested in Creating a new project.

In theory it SHOULD work, I'd think. I still feel like it has something to do with the way i'm passing the backbuffer pointer to the move() method in the ball object.

I kept getting weird errors that I didn't understand though, saying things like it couldn't convert a **CGapiSurface to a *GapiSurface or something like that. I'll be sure to cut/paste the exact errors when I get back to my home PC, though.
If you're doing all of your drawing from within ProcessNextFrame(), the Flip is don't automagically for you.

Rather than passing a pointer to the backbuffer each call, I would recommend that you store the Display pointer when you create your ball object and then acquire the backbuffer from Display on each call. That way you are sure you get the correct backbuffer each call.

Also, always get the result code on your GD calls. I'm sure I don't need to remind everyone that they are there for a reason. For example:

HRESULT hr = pBackbuffer->BltFast(...); if(hr != GD_OK) return hr;
Okay, fair enough, but since i'm using CGapiApplication, how would I go about that? Both the primary and secondary surfaces are created out of my hands.
myApp.m_pDisplay gives me the primary surface, apprently... how do I get the backbuffer?

The only reason I know it in this case is because the processNextFrame method provides it.
You can get a pointer to the backbuffer using:

CGapiSurface* CGapiDisplay::GetBackBuffer()


//Erik
ppcStudios wrote:Rather than passing a pointer to the backbuffer each call, I would recommend that you store the Display pointer when you create your ball object and then acquire the backbuffer from Display on each call. That way you are sure you get the correct backbuffer each call.


The reason I say this is that your Display pointer will never change, but you're not guaranteed that your backbuffer will be the same. This won't be a problem on older (or probably even current) devices, but it may be on future devices.
ruis wrote:You can get a pointer to the backbuffer using:

CGapiSurface* CGapiDisplay::GetBackBuffer()


//Erik


So:

Code:


CGapiSurface* surfacePointer;
CGapiSurface* bufferPointer;

surfacePointer = &myApplication;.m_pDisplay;

bufferPointer = surfacePointer->GetBackBuffer();


??

A lot of this seems weird and not right. But i'm slightly confusled ;)
Thanks for help thus far.
In CreateVideoSurfaces ( ) you will be passed a pointer to the display as the first parameter. Pass this to your ball object when you create its surfaces and store it for later use (say as CGapiDisplay * m_display). Now, when you go to blt your ball object, do:

Code:

CGapiSurface * backbuffer = m_display->GetBackBuffer(); if(!backbuffer) return E_FAIL;


You will now have a valid backbuffer to blt to.
With pointers you must be careful in all cases. For instance, with the compiler, it's not so informative when it comes to describing pointer errors.

I think you need to have two things solved before you can really continue. First, is the understanding of pointers and the second, an understanding of the way GD does surfaces, blits, flips etc..

For pointers, it can be confusing. But think of it this way, when you have a single pointer it's like a road sign pointing to a stop sign. When you have a double pointer , it's like a road sign pointing to the next road sign which points to the stop sign ahead.

Using your examples:

surfacePointer = &myApplication.m;_pDisplay; (WRONG)

This is going to give you the address of the pDisplay pointer - which is far from what you want.

To get a backbuffer pointer you would do this:

surfacePointer = myApplication.m_pDisplay->GetBackBuffer( );
(which returns a pointer to the backbuffer - ready to use)
Here, you get a pointer (from the CGapiDisplay object) to the backbuffer object. This is NOT the actual backbuffer but rather an object that wraps the backbuffer in a neat C++ object. This is all you need for the moment.

Then having a pointer (which points to the BackBuffer object) you can blit a surface:

pBackbuffer->BltFast( x, y , pSurface, NULL, 0UL, NULL );

You would get pSurface from :

CGapiSurface* pSurface = new CGapiSurface( );
pSurface->CreateSurface( ... ); // load your image unto your surface

OR if you don't make your surface a pointer but instead do this:

CGapiSurface m_Surface;

Then you'd simply pass this in the argument like this:

pBackBuffer->BltFast( x,y, &m;_Surface, NULL, 0UL, NULL );

Hopefully this will help clear some fog :)
Great, tons of terrific info! Thanks!

I'll experiment with all this before nagging with more questions, thanks! :)

ppcStudios, Thanks, I was under the impression CreateVidSurfaces( ) didn't pass anything, so I was confused ;)
Okay, everything went great, thanks for your help.
A large part of the problem was that I was instantiating the Ball class in the start of the program... I hadn't realized it was absolutely necessary to do it later (in the initVidMemory). Once I did that, it properly loaded the bitmap. I was able to get a couple small physics demos going.
So, yeah, thanks! :)
Another question...

Right now i'm passing the backbuffer regularly each time in the main loop... can I set this once, or would it be better to do it every frame?

Furthermore, when setting the clipper for the backbuffer, can I do it once at the start of th eprogram, or do I have to do it each frame?

Thanks! I'm learning a lot from you guys... I'll have a game up and running in no time ;)
You can set the clipper once and forget about it. You should reset the clipper if you lose your surface (ie minimize) though to be safe. If you have various viewports (something I do in our RaptureEngine) you can set clippers for each one.