0

I'm fairly new to WINAPI, and I need some help doing text output. I have an array of pixels that I write to with functions and then periodically blit onto the screen using the following functions:

DWORD WINAPI tickThreadProc(HANDLE handle) {
ShowWindow( hwnd, SW_SHOW );
HDC hdc = GetDC( hwnd );
hdcMem = CreateCompatibleDC( hdc );
HBITMAP hbmOld = (HBITMAP)SelectObject( hdcMem, hbmp );
int delay = 1000 / fps;
InitPhys();
LoadIMGs();
for ( ;; ) {
    onFrame( pixels );

    BitBlt( hdc, gLeft, gTop, width, height, hdcMem, 0, 0, SRCCOPY );
    // Wait
    Sleep( delay );
    // Physics
    SimPhys();  
}
SelectObject( hdcMem, hbmOld );
DeleteDC( hdc );
return 0;
}


void MakeSurface(HWND hwnd) {
BITMAPINFO bmi;
bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight =  -height; // Order pixels from top to bottom
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32; // last byte not used, 32 bit for alignment
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
bmi.bmiHeader.biXPelsPerMeter = 0;
bmi.bmiHeader.biYPelsPerMeter = 0;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;
bmi.bmiColors[0].rgbBlue = 0;
bmi.bmiColors[0].rgbGreen = 0;
bmi.bmiColors[0].rgbRed = 0;
bmi.bmiColors[0].rgbReserved = 0;
HDC hdc = GetDC( hwnd );
// Create DIB section to always give direct access to pixels
hbmp = CreateDIBSection( hdc, &bmi, DIB_RGB_COLORS, (void**)&pixels, NULL, 0 );
DeleteDC( hdc );
// Create a new thread to use as a timer
hTickThread = CreateThread( NULL, 0, &tickThreadProc, NULL,0, NULL );
}

This is modified off some code I found on the internet. The pixel struct has 4 ints for r, g, b, and a.

I need to do text output and loading a picture for text is impractical. Any help?

3
  • 3
    Like the TextOut function? Commented Jun 20, 2013 at 16:06
  • 1
    Yes, sort of. But I can't use the PAINSTRUCT stuff. Commented Jun 20, 2013 at 17:08
  • +1 for 'PAINSTRUCT' :) Commented Jun 20, 2013 at 19:07

1 Answer 1

1

First of all, if you use GetDC to get a handle to device context, you must use ReleaseDC when you're done with it. DeleteDC is only for device contexts that you created.

To draw text to this window, you can use functions like TextOut or DrawText using that DC (before you release it).

PAINTSTRUCT is for handling WM_PAINT messages (which is the more common way to draw to a Window). It looks like you're instead trying to draw directly from another thread on a regular basis. GDI isn't very good at dealing with multiple threads, so you might have some problems with this approach. But if your BitBlts are working, then a TextOut should work as well.

Sign up to request clarification or add additional context in comments.

2 Comments

The loop in the thread is infinite; the deleteDC will never be called. I'm not sure why it's there (adapted this code). When the program exits, the loop does.
@Yuri Kahn: There's a DeleteDC in MakeSurface, near the botton of the code sample, just before it creates the thread. That should be a ReleaseDC. The DeleteDC in the thread proc was probably intended to clean up hdcMem rather than hdc, but, you're right that it will never be called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.