#include <windows.h>

struct megaimage_frame {
 HWND win;
 HDC dc;
 HDC bitmapdc;
 BITMAPINFO bitmapinfo;
 HBITMAP bitmap;
 int width;
 int height;
} w[1];

typedef unsigned char byte;

void create_bitmap (HWND win, byte *rgba, int width, int height) {
 w->win = win;
 w->dc = GetDC (win);
 w->bitmapdc = CreateCompatibleDC (w->dc);


 w->bitmapinfo.bmiHeader.biSize = sizeof (w->bitmapinfo);
 w->bitmapinfo.bmiHeader.biWidth = width;
 w->bitmapinfo.bmiHeader.biHeight = height;
 w->bitmapinfo.bmiHeader.biPlanes = 1;
 w->bitmapinfo.bmiHeader.biBitCount = 32;
 w->bitmapinfo.bmiHeader.biCompression = BI_RGB;
 w->bitmapinfo.bmiHeader.biSizeImage = 0;
 w->bitmapinfo.bmiHeader.biXPelsPerMeter = 0;
 w->bitmapinfo.bmiHeader.biYPelsPerMeter = 0; 
 w->bitmapinfo.bmiHeader.biClrImportant = 0;
 w->bitmapinfo.bmiHeader.biClrUsed = 0;

 w->bitmap = CreateDIBitmap (w->dc, &w->bitmapinfo.bmiHeader, CBM_INIT,
   rgba, &w->bitmapinfo, DIB_RGB_COLORS);

 if (!w->bitmap) {
  MessageBox (NULL, "You're screwed by the bitmap", "HMM", MB_OK);
  return;
 }

 w->bitmap = SelectObject (w->bitmapdc, w->bitmap);
 w->width = width;
 w->height = height;
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {
 PAINTSTRUCT ps;
 HDC         hdc;


 switch ( iMsg ) {
  case WM_PAINT:
   hdc = BeginPaint(hwnd, &ps);
   TextOut(hdc, 100, 100, "Hello, world!", 13);

   BitBlt (w->dc, /*destx*/ 0, /*desty*/ 0, w->width, w->height, w->bitmapdc,
    /*srcx*/ 0, /*srcy*/ 0, SRCCOPY);

   EndPaint(hwnd, &ps);
   return 0;

  case WM_DESTROY:
   PostQuitMessage(0);
   return 0;
 }

 return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
 char appname[] = "Windows Tk9";
 HWND wnd;
 MSG msg;
 WNDCLASSEX wndclass;
 byte *p;
 int width, height;


 wndclass.cbSize         = sizeof (wndclass);
 wndclass.style          = CS_HREDRAW | CS_VREDRAW;
 wndclass.lpfnWndProc    = WndProc;
 wndclass.cbClsExtra     = 0;
 wndclass.cbWndExtra     = 0;
 wndclass.hInstance      = hInstance;
 wndclass.hIcon          = LoadIcon (NULL, IDI_APPLICATION);
 wndclass.hIconSm        = LoadIcon (NULL, IDI_APPLICATION);
 wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
 wndclass.hbrBackground  = (HBRUSH) GetStockObject (WHITE_BRUSH);
 wndclass.lpszClassName  = appname;
 wndclass.lpszMenuName   = NULL;

 RegisterClassEx(&wndclass);


 wnd = CreateWindow (appname, "Windows Tk9",
	WS_OVERLAPPEDWINDOW,
	CW_USEDEFAULT, CW_USEDEFAULT,
	CW_USEDEFAULT, CW_USEDEFAULT,
	NULL, NULL, hInstance, NULL);


 width = 200;
 height = 200;

 p = HeapAlloc (GetProcessHeap(), (DWORD)0, (DWORD)width * height * 4);

 memset (p, 127, width * height * 4);
 create_bitmap (wnd, p, width, height);


 ShowWindow (wnd, iCmdShow);
 UpdateWindow (wnd);


 while (GetMessage (&msg, NULL, 0, 0)) {
  TranslateMessage(&msg); 
  DispatchMessage(&msg); 
 }

 /*
  *  Exit with the status from the WM_QUIT message.
  */

 return msg.wParam;
}
