resizable window + GDI Drawing.

Miscellaneous Forums/Win32 Discussion/resizable window + GDI Drawing.

heres a c file that opens a window and lets you draw to it and access the pixels without going thru opengl, only windows native stuff.

#include <windows.h>

HWND    g_win_hwnd;
HBITMAP g_win_dib;
byte *  g_win_pixels;
HDC     g_win_memdc;

HFONT  g_win_font;
HBRUSH g_win_hbrush;
HPEN g_win_hpen;

int g_win_width;
int g_win_height;
int g_win_dibwidth;
int g_win_dibheight;

BOOL g_win_mdown[10];
BOOL g_win_mx;
BOOL g_win_my;
BOOL g_win_quit = FALSE;

#define    BWIN_NONE 0
#define    BWIN_RESIZE 1
#define    BWIN_CLOSE 2
#define    BWIN_MOUSE 3

int g_win_event = 0;

/*************************************************************************
DibCreateBuffer() - Creates th DIB and returns pointer to DibBits
 *************************************************************************/
byte *
DibCreateBuffer (
HWND    hwnd,				// INPUT : hwnd to window
int		iWidth,			// INPUT : Width OF DIB
int		iHeight			// INPUT : Height of DIB
)
{
	HDC				    hdc;
	BITMAPINFOHEADER   bmi;
   byte * dest;
	
   g_win_dibwidth = iWidth;
   g_win_dibheight = iHeight;

	bmi.biSize 			    = sizeof ( BITMAPINFOHEADER );
	bmi.biWidth 			= iWidth;
	bmi.biHeight 			= -iHeight;
	bmi.biPlanes 			= 1;
	bmi.biBitCount 		    = 32;
	bmi.biCompression 		= 0;
	bmi.biSizeImage 		= iWidth * iHeight * 3;
	bmi.biXPelsPerMeter 	= 0;
	bmi.biYPelsPerMeter 	= 0;
	bmi.biClrUsed 			= 0;
	bmi.biClrImportant 	    = 0;

	SetFocus ( hwnd );
	SetForegroundWindow ( hwnd );
	hdc = GetDC ( hwnd );

	dest = NULL;
	
	g_win_dib = CreateDIBSection ( hdc, (BITMAPINFO*)&bmi, DIB_RGB_COLORS, (VOID**)&dest, NULL, 0 );			
	
	ReleaseDC ( hwnd, hdc );

   SelectObject ( g_win_memdc, g_win_dib );

	return ( dest );
}

/*************************************************************************
WinMainFunc() - Function for Main Window
 *************************************************************************/
LRESULT CALLBACK
WinMainFunc (
HWND hwnd, 
UINT msg, 
WPARAM wParam, 
LPARAM lParam
)
{
	HDC					hdc;
	PAINTSTRUCT			paintstruct;
	RECT				rect;

   switch (msg)
   {
       case WM_RBUTTONDOWN:
           g_win_mdown[2] = 1;
			return(0L);	

       case WM_RBUTTONUP:
           g_win_mdown[2] = 0;
			return(0L);	

       case WM_LBUTTONDOWN:
           g_win_mdown[1] = 1;
			return(0L);	

       case WM_LBUTTONUP:
           g_win_mdown[1] = 0;
			return(0L);	

       case WM_MOUSEMOVE:
           g_win_mx = LOWORD(lParam);
           g_win_my = HIWORD(lParam);
			return(0L);	

		case WM_SIZE:
           g_win_width =  LOWORD(lParam);
           g_win_height = HIWORD(lParam);
           g_win_event = BWIN_RESIZE;
			return(0L);	

		case WM_CHAR:

		case WM_KEYUP:
		
		case WM_SYSKEYUP:

       case WM_CREATE:
           hdc = GetDC ( hwnd );
           g_win_memdc = CreateCompatibleDC(hdc);
			return(0L);	

		case WM_PAINT:
			hdc = BeginPaint ( hwnd, &paintstruct );
//           SelectObject ( g_win_memdc, g_win_dib );
		    BitBlt ( hdc, 
               paintstruct.rcPaint.left, 
               paintstruct.rcPaint.top, 
               paintstruct.rcPaint.right-paintstruct.rcPaint.left+1, 
               paintstruct.rcPaint.bottom-paintstruct.rcPaint.top+1, 
               g_win_memdc, 
               paintstruct.rcPaint.left, 
               paintstruct.rcPaint.top, 
               SRCCOPY ); 
			EndPaint ( hwnd, &paintstruct );
			return(0L);	

       case WM_DESTROY:
           g_win_quit = TRUE;
           if ( g_win_hbrush != NULL ) 
               DeleteObject ( g_win_hbrush );
   
           if ( g_win_hpen != NULL ) 
               DeleteObject ( g_win_hpen );
			PostQuitMessage(0);
           g_win_event = BWIN_CLOSE;
           break;    
   }


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

/*========================================================================
   
 ========================================================================*/
int WinPoll ( void )
{
	MSG					msg;
	while ( PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE )  )
	{
		if ( GetMessage ( &msg, NULL, 0, 0 ) == FALSE )
		{
			return ( 1 );
		}
		
		TranslateMessage ( &msg );
		DispatchMessage ( &msg );
	}

	return ( 0 );
}

/*========================================================================
   
 ========================================================================*/
void WinCreate ( int x, int y, int width, int height, char *title )
{
	static char	WinName[] = "GameClass1";
	WNDCLASS			wcl;
	RECT				rect;


	// Define a Window Class ===================
//   wcl.hInstance 		= hThisInst;
	wcl.lpszClassName	= WinName;
	wcl.lpfnWndProc		= WinMainFunc;
	wcl.style			= 0;
	wcl.hCursor 		= NULL;
	wcl.lpszMenuName 	= NULL;
	wcl.cbClsExtra 		= 0;
	wcl.cbWndExtra 		= 0;
	wcl.hbrBackground   = (HBRUSH)GetStockObject ( BLACK_BRUSH );

	// Resgister Class ==========================
	if ( !RegisterClass ( &wcl ) )
		return ( 0 );

	SetRect ( &rect, 0, 0, width, height );
	AdjustWindowRectEx ( &rect, WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX, FALSE, WS_EX_APPWINDOW );

	g_win_hwnd = CreateWindowEx ( WS_EX_APPWINDOW,
						  WinName, 
						  title,
						  WS_CAPTION | WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX, 
						  CW_USEDEFAULT,
						  CW_USEDEFAULT,
						  rect.right - rect.left,
						  rect.bottom - rect.top,
						  (HWND)NULL,
						  (HMENU)NULL,
						  0,
						  (LPVOID)NULL
						  );

   SystemParametersInfo ( SPI_GETWORKAREA, 0, &rect, 0 );

   g_win_pixels = DibCreateBuffer ( g_win_hwnd, rect.right - rect.left, rect.bottom - rect.top );

   WinPoll();
}

/*========================================================================
   
 ========================================================================*/
void WinLine ( int ix1, int iy1, int ix2, int iy2 )
{
   RECT rect;

   MoveToEx ( g_win_memdc, ix1, iy1, NULL );
   LineTo (g_win_memdc, ix2, iy2 );

   rect.left = ix1;
   rect.top = iy1;
   rect.right = ix2;
   rect.bottom = iy2;

   InvalidateRect ( g_win_hwnd, &rect, 0 );
}

/*========================================================================
   
 ========================================================================*/
void WinRect ( int x, int y, int width, int height, int solid )
{
   RECT rect;
   int x2, y2;

   x2 = x + width-1;
   y2 = y + height-1;

   if ( solid ) 
   {
       SelectObject ( g_win_memdc, g_win_hbrush );

       Rectangle ( g_win_memdc, x, y, x2+1, y2+1 );
   }
   else
   {
       MoveToEx ( g_win_memdc, x, y, NULL );
       LineTo (g_win_memdc, x2, y );
       LineTo (g_win_memdc, x2, y2 );
       LineTo (g_win_memdc, x, y2 );
       LineTo (g_win_memdc, x, y );
   }

   rect.top = y;
   rect.left = x;
   rect.bottom = y+height;
   rect.right = x+width;

   InvalidateRect ( g_win_hwnd, &rect, 0 );
}

/*========================================================================
   
 ========================================================================*/
void WinText ( int x, int y, char * msg, int centerx, int center )
{
   TextOut ( g_win_memdc, x, y, msg, strlen ( msg ) );   
}

/*========================================================================
   
 ========================================================================*/
void WinSetColor ( int r, int g, int b )
{
   if ( g_win_hbrush != NULL ) 
       DeleteObject ( g_win_hbrush );
   
   if ( g_win_hpen != NULL ) 
       DeleteObject ( g_win_hpen );

   g_win_hbrush = CreateSolidBrush ( RGB ( r,g,b ) );
   g_win_hpen = CreatePen ( PS_SOLID, 1, RGB ( r,g,b ) );

   SelectObject ( g_win_memdc, g_win_hpen );
}

/*========================================================================
   
 ========================================================================*/
void WinSetUpdateRect ( int ix1, int iy1, int ix2, int iy2 )
{
   RECT rect;

   rect.left = ix1;
   rect.top = iy1;
   rect.right = ix2;
   rect.bottom = iy2;

   InvalidateRect ( g_win_hwnd, &rect, 0 );
}

/*========================================================================
   
 ========================================================================*/
void WinCls ( int x, int y, int x2, int y2 )
{
   PatBlt ( g_win_memdc, x, y, x2-x+1, y2-y+1, BLACKNESS );
}

/*========================================================================
   
 ========================================================================*/
void WinFlip ( void )
{
   UpdateWindow ( g_win_hwnd );
}

/*========================================================================
   
 ========================================================================*/
void WinSetTextColor ( byte r, byte g, byte b )
{
   SetTextColor ( g_win_memdc, RGB ( r,g,b ) );
   SetBkMode ( g_win_memdc, TRANSPARENT );
}

/*========================================================================
   
 ========================================================================*/
void WinSetFont ( char * fname )
{
   g_win_font = CreateFont ( 13,0,0,0, FW_THIN,0,0,0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fname );
   SelectObject ( g_win_memdc, g_win_font );
}


/*========================================================================
   
 ========================================================================*/
void WinDrawPixmap ( byte * src, int x, int y, int width, int height, int pitch, int format )
{
   byte * dest;
   RECT rect;
   int bw;
   byte r,g,b,a;
   int loopx;

   if ( format == 3 || format == 4 ) 
       bw = 3;
   else
       bw = 4;

   if ( width <=0 ) return;
   if ( height <=0 ) return;
   if ( x >= g_win_dibwidth ) return;
   if ( y >= g_win_dibheight ) return;
   if ( x + width <= 0 ) return;
   if ( y + height <= 0 ) return;
  
   if ( y < 0 )
   {
      src += (-y * pitch); 
      height += y;
      y = 0;
   }

   if ( y + height > g_win_dibheight )
   {
      height = g_win_dibheight - y;
   }
  
   if ( x < 0 )
   {
      src += (-x*bw); 
      width += x;
      x = 0;
   }

   if ( x + width > g_win_dibwidth )
   {
      width = g_win_dibwidth - x;
   }

   dest = g_win_pixels + (x*4) + ( y * g_win_dibwidth );

   switch (format) 
   {
       case 3:
           while (height-->0) 
           {
               for (loopx = 0; loopx < width; ++loopx ) 
               {
                   r = *src++;
                   g = *src++;
                   b = *src++;

                   *dest++ = b;
                   *dest++ = g;
                   *dest++ = r;
                   *dest++ = 255;
               }
               dest += ((g_win_dibwidth<<2)-(width<<2));
               src += pitch-(width*3);
           }
       break;
       
       case 4:
           while (height-->0) 
           {
               for (loopx = 0; loopx < width; ++loopx ) 
               {
                   b = *src++;
                   g = *src++;
                   r = *src++;

                   *dest++ = b;
                   *dest++ = g;
                   *dest++ = r;
                   *dest++ = 255;
               }
               dest += ((g_win_dibwidth<<2)-(width<<2));
               src += pitch-(width*3);
           }
       break;    

       case 5:
           while (height-->0) 
           {
               memcpy ( dest, src, width<<2 );
               dest += (g_win_dibwidth<<2);
               src += pitch;
           }
       break;    

       case 6:
           while (height-->0) 
           {
               for (loopx = 0; loopx < width; ++loopx ) 
               {
                   r = *src++;
                   g = *src++;
                   b = *src++;
                   a = *src++;

                   *dest++ = b;
                   *dest++ = g;
                   *dest++ = r;
                   *dest++ = a;
               }
               dest += ((g_win_dibwidth<<2)-(width<<2));
               src += pitch-(width<<2);
           }
       break;    

       default:
           return;
       break;
   }

   rect.top = y;
   rect.left = x;
   rect.bottom = y+height;
   rect.right = x+width;

   InvalidateRect ( g_win_hwnd, &rect, 0 );

}


/*========================================================================
   
 ========================================================================*/
int WinMouseDown ( int num )
{
   return ( g_win_mdown[num] );
}

/*========================================================================
   
 ========================================================================*/
int WinMouseX ( void )
{
   return ( g_win_mx );
}


/*========================================================================
   
 ========================================================================*/
int WinMouseY ( void )
{
   return ( g_win_my );
}

/*========================================================================
   
 ========================================================================*/
int WinIsQuit ( void )
{
   return(g_win_quit);
}

/*========================================================================
   
 ========================================================================*/
int WinEvent ( void )
{
   int rc = g_win_event;
   g_win_event = BWIN_NONE;
   return(rc);
}

/*========================================================================
   
 ========================================================================*/
int WinGetWidth ( void )
{
   return(g_win_width);
}

/*========================================================================
   
 ========================================================================*/
int WinGetHeight ( void )
{
   return(g_win_height);
}



Here are the extern things to add to your code
Extern
Function GfxInit()
Function WinCreate( x, y, width, height, title$z )
Function WinSetColor ( r, g, b )
Function WinSetTextColor ( r, g, b )
Function WinRect ( x, y, width, height, solid )
Function WinFlip()
Function WinCls(x,y,x2,y2)
Function WinSetFont ( fname$z )
Function WinText ( x, y, msg$z, centerx, centery )
Function WinLine ( x1, y1, x2, y2 )
Function WinDrawPixmap ( src:byte ptr, x, y, w, h, p, format )
Function WinMouseX()
Function WinMouseY()
Function WinMouseDown(num)
Function WinPoll()
Function WinSetUpdateRect ( ix1, iy1, ix2, iy2 )
Function WinEvent()
Function WinIsQuit()
Function WinGetWidth()
Function WinGetHeight()
End Extern


just Import"win32.c" ( first file )

then include the Externs in your code.

Also bmax images seem to be mode 6 ( for mac i think )
the draw image routine flips the bytes around right for PC's