head	1.4;
access;
symbols;
locks; strict;
comment	@# @;


1.4
date	2002.09.07.01.50.41;	author curt;	state Exp;
branches;
next	1.3;

1.3
date	2002.08.31.19.01.59;	author curt;	state Exp;
branches;
next	1.2;

1.2
date	2002.08.31.18.54.05;	author curt;	state Exp;
branches;
next	1.1;

1.1
date	2002.02.15.17.22.03;	author curt;	state Exp;
branches;
next	;


desc
@@


1.4
log
@Updated doxygen docs.
@
text
@<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>win32-printer.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.16 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; </center>
<hr><h1>win32-printer.h</h1><div class="fragment"><pre>00001 
00002 <font class="keyword">class </font>CGlPrinter
00003 {
00004 <font class="keyword">public</font>:
00005         <font class="keyword">typedef</font> <font class="keyword">enum</font> { RENDER_TO_PRINTER, RENDER_TO_BITMAP, READ_BITMAP, PRINT_BITMAP } PRINTMODE;
00006 <font class="keyword">public</font>:
00007         <font class="keyword">inline</font> CGlPrinter( PRINTMODE mode = READ_BITMAP );
00008         <font class="keyword">inline</font> ~CGlPrinter();
00009         <font class="keyword">inline</font> <font class="keywordtype">bool</font> Begin( <font class="keywordtype">char</font> *title, <font class="keywordtype">int</font> w=0, <font class="keywordtype">int</font> h=0 );
00010         <font class="keyword">inline</font> <font class="keywordtype">void</font> End( GLubyte *bm=NULL );
00011         <font class="keyword">inline</font> <font class="keywordtype">double</font> GetAspect();
00012         <font class="keyword">inline</font> <font class="keywordtype">int</font> GetHorzRes();
00013         <font class="keyword">inline</font> <font class="keywordtype">int</font> GetVertRes();
00014 
00015 <font class="keyword">private</font>:
00016         PRINTMODE m_printMode;
00017         PRINTDLG m_printDLG;
00018         BITMAPINFO m_bitmapInfo;
00019         <font class="keywordtype">void</font> *m_bitmap;
00020         HBITMAP bitmapHandle;
00021         HDC m_bitmapDC;
00022         HGLRC m_printerHGLRC;
00023         HGLRC m_bitmapHGLRC;
00024         <font class="keywordtype">int</font> m_pageWidth;                                <font class="comment">// Dimension of printer page (x)</font>
00025         <font class="keywordtype">int</font> m_pageHeight;                               <font class="comment">// Dimension of printer page (y)</font>
00026 };
00027 
00028 <font class="keyword">inline</font> <font class="keywordtype">double</font> CGlPrinter::GetAspect()
00029 {
00030         <font class="keywordflow">return</font> (double)m_pageWidth/(double)m_pageHeight;
00031 }
00032 
00033 <font class="keyword">inline</font> <font class="keywordtype">int</font> CGlPrinter::GetHorzRes()
00034 {
00035         <font class="keywordflow">return</font> m_pageWidth;
00036 }
00037 
00038 <font class="keyword">inline</font> <font class="keywordtype">int</font> CGlPrinter::GetVertRes()
00039 {
00040         <font class="keywordflow">return</font> m_pageHeight;
00041 }
00042 
00043 <font class="keyword">inline</font> CGlPrinter::CGlPrinter( PRINTMODE mode ) :
00044         m_printerHGLRC( 0 ), m_bitmapHGLRC( 0 ), m_printMode( mode ), m_bitmap( NULL )
00045 {
00046 }
00047 
00048 <font class="keyword">inline</font> CGlPrinter::~CGlPrinter()
00049 {
00050 }
00051 
00052 <font class="keyword">inline</font> <font class="keywordtype">bool</font> CGlPrinter::Begin( <font class="keywordtype">char</font> *title, <font class="keywordtype">int</font> w, <font class="keywordtype">int</font> h  )
00053 {
00054         <font class="comment">// Pixel format for Printer Device context</font>
00055         <font class="keyword">static</font> PIXELFORMATDESCRIPTOR pPrintfd = {
00056                 <font class="keyword">sizeof</font>(PIXELFORMATDESCRIPTOR),                  <font class="comment">// Size of this structure</font>
00057                 1,                                      <font class="comment">// Version of this structure    </font>
00058                 PFD_DRAW_TO_WINDOW |                    <font class="comment">// Draw to Window (not to m_bitmap)</font>
00059                 PFD_SUPPORT_OPENGL |                    <font class="comment">// Support OpenGL calls </font>
00060                 PFD_SUPPORT_GDI |                       <font class="comment">// Allow GDI drawing in window too</font>
00061                 PFD_DEPTH_DONTCARE,                                             <font class="comment">// Don't care about depth buffering</font>
00062                 PFD_TYPE_RGBA,                          <font class="comment">// RGBA Color mode</font>
00063                 24,                                     <font class="comment">// Want 24bit color </font>
00064                 0,0,0,0,0,0,                            <font class="comment">// Not used to select mode</font>
00065                 0,0,                                    <font class="comment">// Not used to select mode</font>
00066                 0,0,0,0,0,                              <font class="comment">// Not used to select mode</font>
00067                 0,                                      <font class="comment">// Size of depth buffer</font>
00068                 0,                                      <font class="comment">// Not used to select mode</font>
00069                 0,                                      <font class="comment">// Not used to select mode</font>
00070                 0,                                                      <font class="comment">// Not used to select mode</font>
00071                 0,                                      <font class="comment">// Not used to select mode</font>
00072                 0,0,0 };                                <font class="comment">// Not used to select mode</font>
00073 
00074         DOCINFO docInfo;        <font class="comment">// Document Info</font>
00075         <font class="keywordtype">int</font> nPixelFormat;               <font class="comment">// Pixel format requested</font>
00076 
00077         <font class="comment">// Get printer information</font>
00078         memset(&amp;m_printDLG,0,<font class="keyword">sizeof</font>(PRINTDLG));
00079         m_printDLG.lStructSize = <font class="keyword">sizeof</font>(PRINTDLG);
00080         m_printDLG.hwndOwner = GetForegroundWindow();
00081         m_printDLG.hDevMode = NULL;
00082         m_printDLG.hDevNames = NULL;
00083         m_printDLG.Flags = PD_RETURNDC | PD_ALLPAGES;
00084 
00085         <font class="comment">// Display printer dialog</font>
00086         <font class="keywordflow">if</font>(!PrintDlg(&amp;m_printDLG))
00087         {
00088                 printf( <font class="stringliteral">"PrintDlg() failed %lx\n"</font>, GetLastError() );
00089                 <font class="keywordflow">return</font> <font class="keyword">false</font>;
00090         }
00091 
00092         <font class="comment">// Get the dimensions of the page</font>
00093         m_pageWidth = GetDeviceCaps(m_printDLG.hDC, HORZRES);
00094         m_pageHeight = GetDeviceCaps(m_printDLG.hDC, VERTRES);
00095 
00096         <font class="comment">// Initialize DocInfo structure</font>
00097         docInfo.cbSize = <font class="keyword">sizeof</font>(DOCINFO);
00098         docInfo.lpszDocName = title;
00099         docInfo.lpszOutput = NULL;
00100 
00101 
00102         <font class="comment">// Choose a pixel format that best matches that described in pfd</font>
00103         nPixelFormat = ChoosePixelFormat(m_printDLG.hDC, &amp;pPrintfd);
00104         <font class="comment">// Watch for no pixel format available for this printer</font>
00105         <font class="keywordflow">if</font>(nPixelFormat == 0)
00106                 {
00107                 <font class="comment">// Delete the printer context</font>
00108                 DeleteDC(m_printDLG.hDC);
00109 
00110                 printf( <font class="stringliteral">"ChoosePixelFormat() failed %lx\n"</font>, GetLastError() );
00111                 <font class="keywordflow">return</font> <font class="keyword">false</font>;
00112                 }
00113 
00114         <font class="comment">// Start the document and page</font>
00115         StartDoc(m_printDLG.hDC, &amp;docInfo);
00116         StartPage(m_printDLG.hDC);
00117         
00118         <font class="comment">// Set the pixel format for the device context, but watch for failure</font>
00119         <font class="keywordflow">if</font>(!SetPixelFormat(m_printDLG.hDC, nPixelFormat, &amp;pPrintfd))
00120                 {
00121                 <font class="comment">// Delete the printer context</font>
00122                 DeleteDC(m_printDLG.hDC);
00123 
00124                 printf( <font class="stringliteral">"SetPixelFormat() failed %lx\n"</font>, GetLastError() );
00125                 <font class="keywordflow">return</font> <font class="keyword">false</font>;
00126                 }
00127 
00128         <font class="comment">// Create the Rendering context and make it current</font>
00129         <font class="keywordflow">if</font> ( m_printMode == RENDER_TO_PRINTER )
00130         {
00131                 m_printerHGLRC = wglCreateContext(m_printDLG.hDC);
00132                 wglMakeCurrent(m_printDLG.hDC, m_printerHGLRC);
00133         }
00134         <font class="keywordflow">else</font> 
00135         {
00136                 memset( &amp;m_bitmapInfo, 0, <font class="keyword">sizeof</font>(BITMAPINFO) );
00137                 m_bitmapInfo.bmiHeader.biSize = <font class="keyword">sizeof</font>( BITMAPINFOHEADER );
00138                 <font class="keywordflow">if</font> ( m_printMode == READ_BITMAP )
00139                 {
00140                         GLint vp[4];
00141                         glGetIntegerv( GL_VIEWPORT, vp );
00142                         m_bitmapInfo.bmiHeader.biWidth = vp[2];
00143                         m_bitmapInfo.bmiHeader.biHeight = (vp[3] + 3) &amp; ~3;
00144                 }
00145                 <font class="keywordflow">else</font>
00146                 {
00147                         <font class="keywordflow">if</font> ( m_printMode == RENDER_TO_BITMAP )
00148                         {
00149                                 m_bitmapInfo.bmiHeader.biWidth = m_pageWidth;
00150                                 m_bitmapInfo.bmiHeader.biHeight = m_pageHeight;
00151                         }
00152                         <font class="keywordflow">else</font>
00153                         {
00154                                 <font class="comment">// PRINT_BITMAP</font>
00155                                 m_bitmapInfo.bmiHeader.biWidth = w;
00156                                 m_bitmapInfo.bmiHeader.biHeight = h;
00157                         }
00158                 }
00159                 m_bitmapInfo.bmiHeader.biPlanes = 1;
00160                 m_bitmapInfo.bmiHeader.biBitCount = 24;
00161                 m_bitmapInfo.bmiHeader.biCompression = BI_RGB;
00162                 m_bitmapInfo.bmiHeader.biSizeImage = m_bitmapInfo.bmiHeader.biWidth*m_bitmapInfo.bmiHeader.biHeight*3;
00163                 m_bitmapInfo.bmiHeader.biXPelsPerMeter = 2952; <font class="comment">// 75dpi</font>
00164                 m_bitmapInfo.bmiHeader.biYPelsPerMeter = 2952; <font class="comment">// 75dpi</font>
00165                 m_bitmapInfo.bmiHeader.biClrUsed = 0;
00166                 m_bitmapInfo.bmiHeader.biClrImportant = 0;
00167                 bitmapHandle = CreateDIBSection( NULL, &amp;m_bitmapInfo, DIB_RGB_COLORS, &amp;m_bitmap, NULL, 0);
00168                 m_bitmapDC = CreateCompatibleDC( NULL );
00169                 <font class="keywordflow">if</font> ( m_bitmapDC == NULL )
00170                 {
00171                         DeleteDC(m_printDLG.hDC);
00172                         printf( <font class="stringliteral">"CreateCompatibleDC() failed %lx\n"</font>, GetLastError() );
00173                         <font class="keywordflow">return</font> <font class="keyword">false</font>;
00174                 }
00175                 <font class="keywordflow">if</font> ( SelectObject( m_bitmapDC, bitmapHandle ) == NULL )
00176                 {
00177                         DeleteDC(m_printDLG.hDC);
00178                         DeleteDC(m_bitmapDC);
00179                         printf( <font class="stringliteral">"SelectObject() failed %lx\n"</font>, GetLastError() );
00180                         <font class="keywordflow">return</font> <font class="keyword">false</font>;
00181                 }
00182                 PIXELFORMATDESCRIPTOR pfd;
00183                 memset( &amp;pfd, 0, <font class="keyword">sizeof</font>(PIXELFORMATDESCRIPTOR) );
00184                 pfd.nVersion = 1 ;
00185                 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
00186                 pfd.iPixelType = PFD_TYPE_RGBA ; 
00187                 pfd.cColorBits = 24;
00188                 pfd.iLayerType = PFD_MAIN_PLANE;
00189                 <font class="keywordtype">int</font> pixelFormat = ::ChoosePixelFormat(m_bitmapDC, &amp;pfd);
00190                 SetPixelFormat (m_bitmapDC, pixelFormat, &amp;pfd);
00191                 <font class="keywordflow">if</font> ( m_printMode == RENDER_TO_BITMAP )
00192                 {
00193                         m_bitmapHGLRC = wglCreateContext( m_bitmapDC );
00194                         wglMakeCurrent( m_bitmapDC, m_bitmapHGLRC );
00195                 }
00196         }
00197 
00198         <font class="keywordflow">if</font> ( m_printMode == RENDER_TO_PRINTER || m_printMode == RENDER_TO_BITMAP )
00199         {
00200                 <font class="comment">// Set viewing volume info</font>
00201                 <font class="comment">//glViewport(0,0,m_pageWidth/2,m_pageHeight/2); // Put this in to restrict area of page.</font>
00202 
00203                 GLfloat nRange = 100.0f;
00204 
00205                 <font class="comment">// Reset projection matrix stack</font>
00206                 glMatrixMode(GL_PROJECTION);
00207                 glLoadIdentity();
00208 
00209                 <font class="comment">// Establish clipping volume (left, right, bottom, top, near, far)</font>
00210                 <font class="comment">// This keeps the perspective square regardless of window or page size</font>
00211 
00212                 <font class="keywordflow">if</font> (m_pageHeight &lt;= m_pageWidth) 
00213                 {
00214                         glOrtho (-nRange, nRange, -nRange*m_pageHeight/m_pageWidth, nRange*m_pageHeight/m_pageWidth, -nRange, nRange);
00215                 }
00216                 <font class="keywordflow">else</font> 
00217                 {
00218                         glOrtho (-nRange*m_pageWidth/m_pageHeight, nRange*m_pageWidth/m_pageHeight, -nRange, nRange, -nRange, nRange);
00219                 }
00220 
00221                 <font class="comment">// Reset Model view matrix stack</font>
00222                 glMatrixMode(GL_MODELVIEW);
00223                 glLoadIdentity();
00224                 glRotatef(270.0f, 1.0, 0.0, 0.0);
00225         }
00226 
00227         <font class="keywordflow">return</font> <font class="keyword">true</font>;
00228 }
00229 
00230 <font class="keyword">inline</font> <font class="keywordtype">void</font> CGlPrinter::End( GLubyte *bm )
00231 {
00232         <font class="keywordflow">if</font> ( m_printDLG.hDC )
00233         {
00234                 glFinish();
00235 
00236                 <font class="keywordflow">if</font> ( m_printMode == RENDER_TO_PRINTER )
00237                 {
00238                         <font class="keywordflow">if</font> ( m_printerHGLRC )
00239                         {
00240                                 wglDeleteContext( m_printerHGLRC );
00241                         }
00242                 }
00243                 <font class="keywordflow">else</font>
00244                 {
00245                         <font class="keywordflow">if</font> ( m_printMode == RENDER_TO_BITMAP )
00246                         {
00247                                 BitBlt( m_printDLG.hDC, 0,0, m_pageWidth,m_pageHeight, m_bitmapDC, 0,0, SRCCOPY );
00248                         }
00249                         <font class="keywordflow">else</font>
00250                         {
00251                                 <font class="keywordflow">if</font> ( m_printMode == READ_BITMAP )
00252                                 {
00253                                         glPixelStorei( GL_PACK_ALIGNMENT, 4 );
00254                                         glPixelStorei( GL_PACK_ROW_LENGTH, 0 );
00255                                         glPixelStorei( GL_PACK_SKIP_ROWS, 0 );
00256                                         glPixelStorei( GL_PACK_SKIP_PIXELS, 0 );
00257                                         <font class="comment">//GLubyte *tempbitmap = (GLubyte *) malloc(m_bitmapInfo.bmiHeader.biWidth*m_bitmapInfo.bmiHeader.biHeight*4);</font>
00258                                         glReadPixels( 0,0, m_bitmapInfo.bmiHeader.biWidth,m_bitmapInfo.bmiHeader.biHeight,
00259                                                 GL_RGB, GL_UNSIGNED_BYTE, m_bitmap );
00260                                 }
00261                                 <font class="keywordflow">else</font>
00262                                 {
00263                                         <font class="comment">//PRINT_BITMAP</font>
00264                                         memcpy( m_bitmap, bm, m_bitmapInfo.bmiHeader.biSizeImage );
00265                                 }
00266                                 <font class="keywordtype">int</font> i,j;
00267                                 GLubyte *rgb, temp;
00268                                 <font class="keywordflow">for</font> ( i = 0; i &lt; m_bitmapInfo.bmiHeader.biHeight; i++ )
00269                                 {
00270                                         <font class="keywordflow">for</font> ( j = 0, rgb = ((GLubyte *)m_bitmap) + i * m_bitmapInfo.bmiHeader.biWidth * 3;
00271                                         j &lt; m_bitmapInfo.bmiHeader.biWidth;
00272                                         j++, rgb +=3 )
00273                                         {
00274                                                 temp = rgb[0];
00275                                                 rgb[0] = rgb[2];
00276                                                 rgb[2] = temp;
00277                                         }
00278                                 }
00279                                 <font class="keywordtype">long</font> width = m_pageWidth;
00280                                 <font class="keywordtype">long</font> height = width * m_bitmapInfo.bmiHeader.biHeight / m_bitmapInfo.bmiHeader.biWidth;
00281                                 <font class="keywordflow">if</font> ( height &gt; m_pageHeight )
00282                                 {
00283                                         height = m_pageHeight;
00284                                         width = height * m_bitmapInfo.bmiHeader.biWidth / m_bitmapInfo.bmiHeader.biHeight;
00285                                 }
00286                                 <font class="keywordtype">long</font> xoffset = (m_pageWidth - width) / 2;
00287                                 <font class="keywordtype">long</font> yoffset = (m_pageHeight - height) / 2;
00288                                 StretchBlt( m_printDLG.hDC, xoffset, yoffset, width, height, m_bitmapDC, 0, 0,
00289                                         m_bitmapInfo.bmiHeader.biWidth, m_bitmapInfo.bmiHeader.biHeight, SRCCOPY );
00290                         }
00291                         <font class="keywordflow">if</font> ( m_bitmapDC ) 
00292                         {
00293                                 DeleteDC( m_bitmapDC );
00294                         }
00295                         <font class="keywordflow">if</font> ( bitmapHandle )
00296                         {
00297                                 DeleteObject( bitmapHandle );
00298                         }
00299                         <font class="keywordflow">if</font> ( m_bitmapHGLRC )
00300                         {
00301                                 wglDeleteContext( m_bitmapHGLRC );
00302                         }
00303                 }
00304 
00305                 <font class="comment">/* EndPage... */</font>
00306                 <font class="keywordflow">if</font> ( EndPage( m_printDLG.hDC ) &lt;=0 )
00307                 {
00308                         printf( <font class="stringliteral">"EndPage() failed\n"</font> );
00309                 }
00310 
00311                 <font class="comment">/* EndDoc... */</font>
00312                 <font class="keywordflow">if</font> ( EndDoc( m_printDLG.hDC ) &lt;=0 )
00313                 {
00314                         printf( <font class="stringliteral">"EndDoc() failed\n"</font> );
00315                 }
00316 
00317                 <font class="comment">// Delete the printer context when done with it</font>
00318                 DeleteDC(m_printDLG.hDC);
00319                 
00320                 <font class="comment">// Restore window rendering context</font>
00321                 <font class="comment">//wglMakeCurrent(hDC, hRC);</font>
00322                 wglMakeCurrent( NULL, NULL );
00323 
00324         }
00325 }
</pre></div><hr><address align="right"><small>Generated on Fri Sep 6 20:46:37 2002 for SimGear by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.16 </small></address>
</body>
</html>
@


1.3
log
@Oops, try again.
@
text
@d334 1
a334 1
</pre></div><hr><address align="right"><small>Generated on Sat Aug 31 13:59:40 2002 for SimGear by
@


1.2
log
@Regenerated doxygen files.
@
text
@d334 1
a334 1
</pre></div><hr><address align="right"><small>Generated on Sat Aug 31 13:45:11 2002 for SimGear by
@


1.1
log
@More web page updates (added doxygen docs to repository.)
@
text
@d5 2
a6 2
</head><body bgcolor="#ffffff">
<!-- Generated by Doxygen 1.2.12 -->
d334 1
a334 1
</pre></div><hr><address><small>Generated on Fri Feb 15 11:15:33 2002 for SimGear by
d336 2
a337 3
<img src="doxygen.gif" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.12 written by <a href="mailto:dimitri@@stack.nl">Dimitri van Heesch</a>,
 &copy;&nbsp;1997-2001</small></address>
@

