PocketMatrix forums

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

Help with SetSurface

Hi all,

I'm trying out some of the game api stuff. I have a small app where I want to draw lines on a new surface and then display the surface in the WM_PAINT.

I'm getting a syntax error on the SetSurface lines though.

I've simplified my app to the point where it's just the IDE's simple game api project, with the help files NewSurface example and I still get the syntax error.

I notice the DrawSurface must be in a drawproc. Must the SetSurface calls be in a certain place?

Code below, any help appreciated :?

Code:

/*

Program name: Bitten.ppl
Author : apw
Description : bitmap editor

*/
// Based on:
// Default Advanced Game API skeleton

#include "GameAPI.ppl"

func mainproc(hWnd$, Msg$, wParam$, lParam$)
ok$ = true;
case(Msg$)
WM_CLOSE:
ShutGameAPI(hWnd$);

WM_KEYDOWN:
KeysPressed$[wParam$] = 1;

WM_KEYUP:
KeysPressed$[wParam$] = 0;

end;
return(ok$);
end;

func GameProc (hWnd$, Msg$, wParam$, lParam$)
case (Msg$)
WM_PAINT:
G_Clear(G_RGB (255,255,255));
//bitmap
G_DRAWRECT (1,1,129,129,G_RGB(255,0,0));

//draw
//G_DRAWRECT (1,139,1+238,139+171,G_RGB(255,0,0));
//DrawSurface(gPad$,1,139,238,171,0,0,-1,0,0);


WM_TIMER:
if (KeysPressed$[keys.vkUp$])
end;
if (KeysPressed$[keys.vkDown$])
end;
if (KeysPressed$[keys.vkLeft$])
end;
if (KeysPressed$[keys.vkRight$])
end;
if (KeysPressed$[keys.vkA$])
PostMessage(hWnd$, WM_CLOSE, 0, 0);
end;


end;
return (true);
end;

func WinMain
h$=newform({GAPI}, {GAPIClass}, &mainproc;);
ShowWindow(h$, SW_SHOW);

ShowFPS(true, G_RGB(255,255,255));
InitGameAPI(h$, &GameProc;, false);

global(surface$,gWidth$,gHeight$,gPad$,gPadMultiply$,gBitmap$,gPallet$);

gWidth$=32;
gHeight$=32;
gPadMultiply$=4;

surface$ = NewSurface(100, 100);
SetSurface(surface$);
g_textout(null, "Test", DVT_NONE, 0, 0, G_RGB(255, 255, 255));
SaveSurface(surface$, "\My Documents\Test.bmp");
SetSurface(NULL);
delete surface$;

return(true);
end;
Try putting G_BEGINSCENE() before the first SetSurface amd G_UPDATE() after the last SetSurface?

I think the back buffer is only taken when a G_BEGINSCENE() is done. In the WM_PAINT it is done automatically.
I applied G_BEGINSCENE() and G_UPDATE() as you suggested but it made no difference. I still get a syntax error from the first SetSurface call.

Could it be the NewSurface call failing and then the handle variable is wrong? What value should the NewSurface call return?
It will return this from PocketFrog:

Frog::Surface* b = m_display->CreateSurface(Width, Height);

It returns a pointer to a new Surface.

I will do a search in the PocketFrog forums.
Ok I found the problem, the SetSurface function was not defined in the list of exported functions. :roll: My bad, sorry. I also fixed the SaveSurface() function definition internally.

Here is a demo that will work perfectly in the next build:

Code:
// Bounce demo written by Alain Deschenes (c) 2004

#include "GameAPI.ppl"

func mainproc(hWnd$, Msg$, wParam$, lParam$)
ok$ = true;
case (Msg$)
WM_CLOSE:
ShutGameAPI(hWnd$);
end;
return (ok$);
end;

func GameProc(hWnd$, Msg$, wParam$, lParam$)
case (Msg$)
WM_PAINT:
G_Clear(0);
end;
return (true);
end;

func WinMain
h$ = newform({GAPI}, {GAPIClass}, &mainproc;);
ShowWindow(h$, SW_SHOW);

ShowFPS(true, G_RGB(255, 255, 255));
InitGameAPI(h$, &GameProc;, 1);

s$ = NewSurface(320, 100, newrast$);
SetSurface(s$, newrast$, oldrast$);
g_beginscene;
g_Clear(0);
g_textout(NULL, "Surface Demo", DVT_CENTER, 0, 2, g_rgb(255, 255, 255));
g_SetPixel(10, 10, G_RGB(255, 255, 255));
g_update;
SaveSurface(s$, "c:\\test");
SetSurface(NULL, oldrast$, oldrast$);
g_beginscene;
DrawSurface(s$, 1, 1, 100, 100, 0, -1, -1, -1, -1);
g_update;
deleteobject(s$, newrast$);

return (true);
end;


There are few more parameters required for the SetSurface and NewSurface. I have update the help file with the changes and fixed the example.
Thanks for looking into this kornalius and thanks for the demo code - I'll look forward to the new build. Btw what are the new parameters for? oldrast$, newrast$ Something to do with raster but uh? :oops:
Yeah, most drawing functions are done using rasterizers in PocketFrog and not directly to the surface. I forgot about those when implementing the NewSurface function.
Kornalius,

Interesting new build. I like the look of the new menu's in the PPC editor for inserting functions etc...

Back to SetSurface though. I have tried your sample code with the latest build and it still doesn't seem to work. No errors this time, it doesn't produce any output file (and it doesn't terminate either :oops: )

I've added some terminate code I found on one of the samples and added my own file name to the SaveSurface call. Code is below.

Btw I did try my own program which doesn't use SaveSurface and that didn't work either - no error's though!

Any ideas?

Code:

#include "GameAPI.ppl"

func mainproc(hWnd$, Msg$, wParam$, lParam$)
ok$ = true;
case (Msg$)
WM_CLOSE:
ShutGameAPI(hWnd$);
WM_KEYDOWN:
KeysPressed$[wParam$] = 1;
WM_KEYUP:
KeysPressed$[wParam$] = 0;

end;
return (ok$);
end;

func GameProc(hWnd$, Msg$, wParam$, lParam$)
case (Msg$)
WM_PAINT:
G_Clear(0);
WM_TIMER:
if (KeysPressed$[keys.vkA$])
PostMessage(hWnd$, WM_CLOSE, 0, 0);
end;

end;
return (true);
end;

func WinMain
h$ = newform({GAPI}, {GAPIClass}, &mainproc;);
ShowWindow(h$, SW_SHOW);

ShowFPS(true, G_RGB(255, 255, 255));
InitGameAPI(h$, &GameProc;, 1);

s$ = NewSurface(320, 100, newrast$);
SetSurface(s$, newrast$, oldrast$);
g_beginscene;
g_Clear(0);
g_textout(NULL, "Surface Demo", DVT_CENTER, 0, 2, g_rgb(255, 255, 255));
g_SetPixel(10, 10, G_RGB(255, 255, 255));
g_update;
SaveSurface(s$, "\MY DOCUMENTS\apw99");
SetSurface(NULL, oldrast$, oldrast$);
g_beginscene;
DrawSurface(s$, 1, 1, 100, 100, 0, -1, -1, -1, -1);
g_update;
deleteobject(s$, newrast$);

return (true);
end;
The image is certainly being displayed, you just don't see it, it's too fast.

You should put you drawimage code within the WM_PAINT. Then you should see the display.
Kornalius,

Sorry! Your update is working fine. My drawsurface was in the WM_PAINT event but I'm not going to tell you why it wasn't working :oops: :oops: :oops:
No problem, as long as I helped you out!
Hi,

It would be very handy to be able draw only a subset of a drawsurface, ie a window of one, something like:

DRAWSURFACESUBSET (Surface, screenX, screenY, surfaceX, surfaceY, SurfaceWidth, SurfaceHeight, Angle, Alpha, Tint, TintLevel, Light)

Unless there is some other way of acheiving this?

EDIT:
Can I use G_SETCLIPPING in some way for this? Does this just overwrite the outside of the rectangle or does it prevent the outside area being drawn?
Hi,

I will fix the routine to add this feature.

The new one will look something like this:

DRAWSURFACE (Surface, X, Y, SurfaceX, SurfaceY, Width, Height, Angle, Alpha, Tint, TintLevel, Light, OffsetX, OffsetY)

Edit: Here is the latest from the help file:

DRAWSURFACE (Surface, DestX, DestY, DestWidth, DestHeight, SourceX, SourceY, SourceWidth, SourceHeight, Angle, Alpha, Tint, TintLevel, Light, OffsetX, OffsetY)

Draw surface (surface) at position (SourceX, SourceY) using the current size (DestWidth, DestHeight).

You can rotate the surface with the (angle) parameter and you can also change the blending of the surface by using (alpha).

If you specify a tint other than -1, the surface will be drawn with this color using the Tintlevel for level of tinting.

You can also specify the amount of light the surface should be painted with. Light ranges from 0 to 255, a value of -1 doesn't use light.

You can also only draw a portion of the source surface on the screen using (SourceX, SourceY, SourceWidth, SourceHeight).

OffsetX and OffsetY draw the surface scrolled either horizontally or vertically.

NB: This function should be used inside a DrawProc procedure.

Example:

surface$ = LoadSurface("\\My Documents\\Bitmap.bmp", -1);
DrawSurface(surface$, 10, 10, 30, 30, 0, 0, 0, 0, 0, 50, 0, 0, 0, 0);
Kornalius,

Excellent! Its working a treat now! :D
Kornalius,

My app is coming along nicely but I do have a problem. Every third time I run it (from the PPC editor) it crashes on startup and I have to reset. I've tried compiling to an exe and that runs each time without a problem.

I've put this on my SetSurface thread as I thought it may be related to the way I'm using the GAMEAPI stuff.

I can send you the code if that would help.
Yeah, might be a memory leak or something like this. Are you freeing the surfaces each time?

Send me the code, I will take a look.
Hi,

I just took a look at your program, real nice! I like it a lot.

The deleteobject was not freeing the surfaces and rasterizers properly. So yes there was a major memory leak (4 megs every run). :cry:

I just realized deleteobject wouldn't work like I thought. So I had to create a bunch of freexxxxxxx routines.

FreeSurface(Handle, [...]);
FreeRasterizer(Handle, [...]);

Just replace the deleteobject functions you have with these two functions, make sure you don't mix up rasterizers and surfaces though.
Kornalius,

Glad you like my app :D
Its always nice when your work is appreciated!

I like your fix :D Now I can continue development without the test/test/test/reset/... cycle. I have a very simple pallete working now...

PPL is becoming so HOT! How's the VFB coming on - I know it can't be fun working on that module...
Right now, 100% of my work is geared towards the physic engine. I want to finish this before I work on something else.

As for the VFB, bmanske is working on it. It is slowly coming along together. I hope to finish it for the official 0.8 beta.