PocketMatrix forums

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

Blitting a surface on top of another (OpenGL)

Hi! I need to add detail to an existing 2D surface by blitting another surface on top of it. Up to this point, I haven't been using OpenGL and managed to get good results by calling the BltFast method from the destination surface.

I've now changed my code to make use of OpenGL (I'm working on Windows) but, unfortunately, the BltFast() method I use to compose the surface is returning E_LOCKFAILED and I don't know how to solve it.

I'm attaching a Visual Studio solution with a very simple example so that you can try it out and tell me what's missing. The call to BltFast that is failing is located in the line 194 of main.cpp

Many thanks!

--Nacho
Attachments:
SurfIntoSurf.rar (168 KB)
Hi Nacho,

When a surface has been uploaded to OpenGL, it will reside in video memory and the pixel data can't be changed. Blitting to and from the surface also doesn't work (unless the target is the display).

If you want to change a surface dynamically you can do the following:
- Before uploading to OpenGL create and adjust the surface until it can be used in the game, then finally upload it (this works for images that are built dynamically, but won't change while the game runs)
- When uploading, set the 'cleardata' parameter to false. When surface data has been changed, re-upload to display the changes
- Use render-to-texture to alter a texture dynamically, this may not work on all devices. Implementing render-to-texture is not a trivial matter
Hi edge,

Thanks for your reply. The method you suggested worked fine, but now I'm having problems porting some other parts of the code.

For example, I added the following snippet of code in the OnNextFrame method in order to draw a red filled rectangle at the top left corner of the screen, but the rectangle doesn't show up (I cannot determine if it's a problem with the method either, since its return value is "void").

Code:

RECT r;
r.left = r.top = 0;
r.right = 100; r.bottom = 200;
ecd.dsp->FillRect(&r;,E_RGBX(255,0,0));


As a side question: how can I (easily) draw a regular (ie. non-filled) rectangle on an OpenGL display? When I used to blit to the backbuffer directly, I made use of the DrawRect method, but there doesn't seem to be an equivalent one in ClassEDisplay.

Once again, thanks for your support.

--Nacho
Hi Nacho,

The rectangle isn't filled because the E_RGBX macro doesn't specify opacity information. The correct way is to use E_RGBA, for example: E_RGBA(255, 0, 0, 255) for a red color.

Unfortunately it isn't possible to draw rectangles and lines easily. At the moment, the best way to do this is to use an E3DSurface object containing your rectangle, or use OpenGL directly.
Hi edge,

Many thanks for your reply! The filled rectangle worked great. I'll see if I can implement the non-filled rectangle using OpenGL during the weekend.

--Nacho
Hi edge,

I tried drawing a rectangle by using the OpenGL functions but I'm afraid I must be doing something wrong since I'm getting a green rectangle instead of the red rectangle I'm after (the rectangle gets rendered in the correct place, though).

Here's the rendering code:

Code:

int iWidth, iHeight;

iWidth = lpRect->right - lpRect->left;
iHeight = lpRect->bottom - lpRect->top;

glBegin(GL_LINE_LOOP);
glColor3f(1.0f,0.0f,0.0f);
glVertex2d(lpRect->left,lpRect->top);
glVertex2d(lpRect->left+iWidth,lpRect->top);
glVertex2d(lpRect->left+iWidth,lpRect->top+iHeight);
glVertex2d(lpRect->left,lpRect->top+iHeight);
glEnd();


Do you have any suggestions so that I can fix this issue?

Thanks in advance for your answer!

--Nacho
Hi Nacho,

The lines are probably green because texturing is still enabled. Also, did you know you can't use glBegin/End calls on many mobile devices?

To fix this you need to call glDisable(GL_TEXTURE2D) and disable some OpenGL client states: texture coordinate, color and normal arrays.