Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

Ovgabuf.cpp Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

Ovgabuf.cpp

Go to the documentation of this file.
00001 //Filename    : OVGABUF.CPP
00002 //Description : OVGABUF direct draw surface class
00003 
00004 #include <OVGABUF.H>
00005 #include <OVGA.H>
00006 #include <OCOLTBL.H>
00007 #include <ALL.H>
00008 #include <OMOUSE.H>
00009 #include <IMGFUN.H>
00010 #include <OSYS.H>
00011 #include <OWORLD.H>
00012 #include <OBITMAP.H>
00013 #include <OBITMAPW.H>
00014 
00015 short *VgaBuf::default_remap_table;
00016 
00017 //-------- Begin of function VgaBuf::VgaBuf ----------//
00018 
00019 VgaBuf::VgaBuf() {
00020     memset( this, 0, sizeof(VgaBuf) );
00021     set_color( VGA_GRAY );
00022 }
00023 
00024 //-------- End of function VgaBuf::VgaBuf ----------//
00025 
00026 //-------- Begin of function VgaBuf::~VgaBuf ----------//
00027 
00028 VgaBuf::~VgaBuf() {
00029     deinit();
00030 }
00031 
00032 //-------- End of function VgaBuf::~VgaBuf ----------//
00033 
00034 //-------- Begin of function VgaBuf::init_front ----------//
00038 void VgaBuf::init_front(LPDIRECTDRAW4 ddPtr) {
00039     DDSURFACEDESC2       ddsd;
00040     HRESULT             rc;
00041     DDCAPS              ddcaps;
00042 
00043     //------ Get Direct Draw capacity info --------//
00044 
00045     ddcaps.dwSize = sizeof( ddcaps );
00046 
00047     if( ddPtr->GetCaps( &ddcaps, NULL ) != DD_OK )
00048         err.run( "Error creating Direct Draw front surface!" );
00049 
00050     //---------------------------------------------//
00051     // Create the Front Buffer
00052     //---------------------------------------------//
00053 
00054     ZeroMemory( &ddsd, sizeof(ddsd) );
00055     ddsd.dwSize = sizeof( ddsd );
00056 
00057     ddsd.dwFlags = DDSD_CAPS;
00058     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
00059 
00060     //  LPDIRECTDRAWSURFACE dd1Buf;
00061     rc = ddPtr->CreateSurface( &ddsd, &dd_buf, NULL );
00062     if( rc != DD_OK )
00063         err.run( "Error creating Direct Draw front surface!" );
00064 
00065     //  rc = dd1Buf->QueryInterface(IID_IDirectDrawSurface2, (void **)&dd_buf);
00066     //  if( rc != DD_OK )
00067     //  {
00068     //          dd1Buf->Release();
00069     //          err.run ( "Error creating Direct Draw front surface!!" );
00070     //  }
00071 
00072     //  dd1Buf->Release();
00073 
00074     lock_bit_stack = 0;
00075     lock_stack_count = 0;
00076 
00077     default_remap_table = vga.default_remap_table;  // new for 16-bit
00078 
00079     is_front = 1;
00080 }
00081 
00082 //-------- End of function VgaBuf::init_front ----------//
00083 
00084 //-------- Begin of function VgaBuf::init_back ----------//
00091 void VgaBuf::init_back( LPDIRECTDRAW4 ddPtr, DWORD w, DWORD h ) {
00092     DDSURFACEDESC2       ddsd;
00093     HRESULT             rc;
00094 
00095     //--------- fill in surface desc -----------//
00096 
00097     memset( &ddsd, 0, sizeof( ddsd ) );
00098     ddsd.dwSize = sizeof( ddsd );
00099     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT |DDSD_WIDTH;
00100 
00101     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
00102 
00103     ddsd.dwWidth  = w ? w : VGA_WIDTH;
00104     ddsd.dwHeight = h ? h : VGA_HEIGHT;
00105 
00106     //  LPDIRECTDRAWSURFACE dd1Buf;
00107     rc = ddPtr->CreateSurface( &ddsd, &dd_buf, NULL );
00108     if( rc != DD_OK )
00109         err.run( "Error creating direct draw back surface!" );
00110 
00111     //  rc = dd1Buf->QueryInterface(IID_IDirectDrawSurface2, (void **)&dd_buf);
00112     //  if( rc != DD_OK )
00113     //  {
00114     //          dd1Buf->Release();
00115     //          err.run( "Error creating direct draw back surface!!" );
00116     //  }
00117 
00118     //  dd1Buf->Release();
00119 
00120     lock_bit_stack = 0;
00121     lock_stack_count = 0;
00122 
00123     default_remap_table = vga.default_remap_table;  // new for 16-bit
00124 }
00125 
00126 //-------- End of function VgaBuf::init_back ----------//
00127 
00128 //------ Begin of function VgaBuf::deinit --------//
00129 
00130 void VgaBuf::deinit() {
00131     if( dd_buf ) {
00132         dd_buf->Release();
00133         dd_buf = NULL;
00134     }
00135 }
00136 
00137 //-------- End of function VgaBuf::deinit ----------//
00138 
00139 //-------- Begin of function VgaBuf::activate_pal ----------//
00143 void VgaBuf::activate_pal(LPDIRECTDRAWPALETTE ddPalPtr) {
00144     return;                                         // no need for 16-bit
00145 
00146     err_when(!ddPalPtr || !dd_buf);
00147 
00148     HRESULT rc = dd_buf->SetPalette(ddPalPtr);
00149 
00150     if( rc == DDERR_SURFACELOST ) {
00151         dd_buf->Restore();
00152         rc = dd_buf->SetPalette(ddPalPtr);
00153     }
00154 
00155 #ifdef DEBUG
00156     if( rc != DD_OK )
00157         debug_msg( "VgaBuf::activate_pal(), failed activating the palette" );
00158 #endif
00159 }
00160 
00161 //--------- End of function VgaBuf::activate_pal ----------//
00162 
00163 //-------- Begin of function VgaBuf::color_match ----------//
00164 
00165 DWORD VgaBuf::color_match(COLORREF rgb) {
00166     COLORREF    rgbT;
00167     HDC         hdc;
00168     DWORD       dw = CLR_INVALID;
00169     DDSURFACEDESC2  ddsd;
00170     HRESULT       hres;
00171 
00172     if( dd_buf->GetDC(&hdc) == DD_OK ) {
00173         rgbT = GetPixel(hdc, 0, 0);
00174         SetPixel(hdc, 0, 0, rgb);
00175         dd_buf->ReleaseDC(hdc);
00176     }
00177 
00178     ddsd.dwSize = sizeof(ddsd);
00179     hres = dd_buf->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL);
00180 
00181     if (hres == DD_OK) {
00182         dw  = *(DWORD *)ddsd.lpSurface;
00183         dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount)-1;
00184         dd_buf->Unlock(NULL);
00185     }
00186 
00187     if (dd_buf->GetDC(&hdc) == DD_OK) {
00188         SetPixel(hdc, 0, 0, rgbT);
00189         dd_buf->ReleaseDC(hdc);
00190     }
00191 
00192     return dw;
00193 }
00194 
00195 //-------- End Endof function VgaBuf::color_match ----------//
00196 
00197 //-------- Begin of function VgaBuf::is_buf_lost ----------//
00199 BOOL VgaBuf::is_buf_lost() {
00200     return dd_buf && dd_buf->IsLost() == DDERR_SURFACELOST;
00201 }
00202 
00203 //--------- End of function VgaBuf::is_buf_lost ----------//
00204 
00205 //-------- Begin of function VgaBuf::restore_buf ----------//
00209 BOOL VgaBuf::restore_buf() {
00210     if( dd_buf == NULL || dd_buf->Restore() != DD_OK ) {
00211 #ifdef DEBUG
00212         debug_msg("Error restoring direct draw buffer");
00213 #endif
00214         return FALSE;
00215     }
00216 
00217     return TRUE;
00218 }
00219 
00220 //--------- End of function VgaBuf::restore_buf ----------//
00221 
00222 //------------- Begin of function VgaBuf::lock_buf --------------//
00223 
00224 void VgaBuf::lock_buf() {
00225     err_if( buf_locked )
00226         err_now( "VgaBuf::lock_buf() error, buffer already locked." );
00227 
00228     memset( &buf_des, 0, sizeof(buf_des) );
00229 
00230     buf_des.dwSize = sizeof(buf_des);
00231 
00232     int rc = dd_buf->Lock(NULL, &buf_des, DDLOCK_WAIT, NULL);
00233 
00234     cur_buf_ptr = (short *) buf_des.lpSurface;
00235 
00236     //--------------------------------------//
00237 
00238     if( rc==DD_OK )
00239         buf_locked = TRUE;
00240     else {
00241         if( is_front )
00242             err_now( "VgaBuf::lock_buf() locking front buffer failed." );
00243         else
00244             err_now( "VgaBuf::lock_buf() locking back buffer failed." );
00245 
00246 #ifdef DEBUG
00247         debug_msg( "Failed to lock the buffer." );
00248 #endif
00249     }
00250 }
00251 
00252 //--------------- End of function VgaBuf::lock_buf --------------//
00253 
00254 //------------- Begin of function VgaBuf::unlock_buf --------------//
00255 
00256 void VgaBuf::unlock_buf() {
00257     // ####### begin Gilbert 16/9 #####//
00258     if( !dd_buf )
00259         return;
00260     // ####### end Gilbert 16/9 #####//
00261 
00262     err_when( !buf_locked );
00263 
00264     int rc = dd_buf->Unlock(NULL);
00265 
00266     if( rc==DD_OK )
00267         buf_locked = FALSE;
00268     else {
00269         switch(rc) {
00270         case DDERR_INVALIDOBJECT:
00271             err_now( "VgaBuf::unlock_buf error: DDERR_INVALIDOBJECT" );
00272 
00273         case DDERR_INVALIDPARAMS:
00274             err_now( "VgaBuf::unlock_buf error: DDERR_INVALIDPARAMS" );
00275 
00276         case DDERR_INVALIDRECT:
00277             err_now( "VgaBuf::unlock_buf error: DDERR_INVALIDRECT" );
00278 
00279         case DDERR_NOTLOCKED:
00280             err_now( "VgaBuf::unlock_buf error: DDERR_NOTLOCKED" );
00281 
00282         case DDERR_SURFACELOST:
00283             err_now( "VgaBuf::unlock_buf error: DDERR_SURFACELOST" );
00284         }
00285 
00286         if( is_front )
00287             err_now( "VgaBuf::unlock_buf() unlocking front buffer failed." );
00288         else
00289             err_now( "VgaBuf::unlock_buf() unlocking back buffer failed." );
00290 
00291 #ifdef DEBUG
00292         debug_msg( "Failed to unlock the buffer." );
00293 #endif
00294     }
00295 }
00296 
00297 //--------------- End of function VgaBuf::unlock_buf --------------//
00298 
00299 //------------- Begin of function VgaBuf::temp_unlock --------------//
00303 void VgaBuf::temp_unlock() {
00304     // push buf_locked
00305     err_when(lock_stack_count >= sizeof(lock_bit_stack)*8);
00306     err_when(buf_locked != 0 && buf_locked != 1);   // 0 or 1
00307     lock_bit_stack = (lock_bit_stack << 1) | buf_locked;
00308     ++lock_stack_count;
00309 
00310     if( buf_locked )
00311         unlock_buf();
00312 
00313     err_when( buf_locked );
00314 }
00315 
00316 //--------------- End of function VgaBuf::temp_unlock --------------//
00317 
00318 //------------- Begin of function VgaBuf::temp_restore_lock --------------//
00323 void VgaBuf::temp_restore_lock() {
00324     // pop buf_locked
00325     err_when(lock_stack_count==0);
00326     DWORD newBufLocked = lock_bit_stack & 1;
00327     lock_bit_stack >>= 1;
00328     lock_stack_count--;
00329 
00330     if( newBufLocked )
00331         lock_buf();
00332 }
00333 
00334 //--------------- End of function VgaBuf::temp_restore_lock --------------//
00335 
00336 //------------- Begin of function VgaBuf::temp_lock --------------//
00340 void VgaBuf::temp_lock() {
00341     // push buf_locked
00342     err_when(lock_stack_count >= sizeof(lock_bit_stack)*8);
00343     err_when(buf_locked != 0 && buf_locked != 1);   // 0 or 1
00344     lock_bit_stack = (lock_bit_stack << 1) | buf_locked;
00345     ++lock_stack_count;
00346 
00347     if( !buf_locked )
00348         lock_buf();
00349     err_when( !buf_locked );
00350 }
00351 
00352 //------------- End of function VgaBuf::temp_lock --------------//
00353 
00354 //------------- Begin of function VgaBuf::temp_restore_unlock --------------//
00359 void VgaBuf::temp_restore_unlock() {
00360     // pop buf_locked
00361     err_when(lock_stack_count==0);
00362     DWORD newBufLocked = lock_bit_stack & 1;
00363     lock_bit_stack >>= 1;
00364     lock_stack_count--;
00365 
00366     if( !newBufLocked )
00367         unlock_buf();
00368 }
00369 
00370 //------------- End of function VgaBuf::temp_restore_unlock --------------//
00371 
00372 //------------- Begin of function VgaBuf::put_bitmap --------------//
00376 void VgaBuf::put_bitmap(int x,int y,char* bitmapPtr) {
00377     err_when( !buf_locked );
00378 
00379     if( is_front )
00380         mouse.hide_area( x, y, x+*((short*)bitmapPtr)-1, y+*(((short*)bitmapPtr)+1)-1 );
00381 
00382     //  ##chwg1130
00383     if((bitmapPtr[0]==-1)&&(bitmapPtr[1]==-1)) {
00384         //create new 16bit palette
00385         short newPalette[256];
00386         for(int i=0,p=2;i<256;i++,p+=sizeof(RGBColor))
00387             newPalette[i] = (short)vga.make_pixel(
00388                 (BYTE)bitmapPtr[p] << 2,
00389                 (BYTE)bitmapPtr[p+1] << 2,
00390                 (BYTE)bitmapPtr[p+2] << 2);
00391         //                newPalette[i] = (short)vga.make_pixel( (RGBColor *)(bitmapPtr + p) );
00392         put_bitmap_remap(x,y,bitmapPtr+p,newPalette);
00393     }
00394     else
00395         IMGbltRemap(buf_ptr(), buf_true_pitch(), x, y, bitmapPtr, default_remap_table);
00396 
00397     if( is_front )
00398         mouse.show_area();
00399 }
00400 
00401 //--------------- End of function VgaBuf::put_bitmap --------------//
00402 
00403 //------- Begin of function VgaBuf::put_bitmap_trans --------//
00407 void VgaBuf::put_bitmap_trans(int x,int y,char* bitmapPtr) {
00408     err_when( !buf_locked );
00409 
00410     if( is_front )
00411         mouse.hide_area( x, y, x+*((short*)bitmapPtr)-1, y+*(((short*)bitmapPtr)+1)-1 );
00412 
00413     //  ##chwg1130
00414     if((bitmapPtr[0]==-1)&&(bitmapPtr[1]==-1)) {
00415         //create new 16bit palette
00416         short newPalette[256];
00417         for(int i=0,p=2;i<256;i++,p+=sizeof(RGBColor))
00418             newPalette[i] = (short)vga.make_pixel(
00419                 (BYTE)bitmapPtr[p] << 2,
00420                 (BYTE)bitmapPtr[p+1] << 2,
00421                 (BYTE)bitmapPtr[p+2] << 2);
00422         IMGbltTransRemap(buf_ptr(), buf_true_pitch(), x, y, bitmapPtr+p,newPalette);
00423     }
00424     else
00425         IMGbltTransRemap(buf_ptr(), buf_true_pitch(), x, y, bitmapPtr, default_remap_table);
00426 
00427     if( is_front )
00428         mouse.show_area();
00429 }
00430 
00431 //--------- End of function VgaBuf::put_bitmap_trans --------//
00432 
00433 //------- Begin of function VgaBuf::put_bitmap_remap --------//
00437 void VgaBuf::put_bitmap_remap(int x,int y,char* bitmapPtr,short *colorTable) {
00438     err_when( !buf_locked );
00439 
00440     if( is_front )
00441         mouse.hide_area( x, y, x+((Bitmap *)bitmapPtr)->get_width()-1, y+((Bitmap*)bitmapPtr)->get_height()-1 );
00442 
00443     IMGbltRemap(buf_ptr(), buf_true_pitch(), x, y, bitmapPtr, colorTable);
00444 
00445     if( is_front )
00446         mouse.show_area();
00447 }
00448 
00449 //--------- End of function VgaBuf::put_bitmap_remap --------//
00450 
00451 //---------- Begin of function VgaBuf::save_area_common_buf ----------//
00455 void VgaBuf::save_area_common_buf(int x1, int y1, int x2, int y2) {
00456     err_when( x1>x2 || y1>y2 || x1<0 || y1<0 || x2>=VGA_WIDTH || y2>=VGA_HEIGHT );
00457 
00458     long saveSize = sizeof(short)*4 + BitmapW::size(x2-x1+1, y2-y1+1);
00459 
00460     err_if( saveSize > COMMON_DATA_BUF_SIZE )
00461         err_now( "VgaBuf::save_area_common_buf()" );
00462 
00463     short* shortPtr = (short*) sys.common_data_buf;
00464 
00465     *shortPtr++ = x1;
00466     *shortPtr++ = y1;
00467     *shortPtr++ = x2;
00468     *shortPtr++ = y2;
00469 
00470     //-------- read screen ---------//
00471 
00472     if( is_front )
00473         mouse.hide_area( x1,y1,x2,y2 );               // if the mouse cursor is in that area, hide it
00474 
00475     read_bitmapW( x1,y1,x2,y2, shortPtr );
00476 
00477     if( is_front )
00478         mouse.show_area();
00479 }
00480 
00481 //------------ End of function VgaBuf::save_area_common_buf ----------//
00482 
00483 //---------- Begin of function VgaBuf::rest_area_common_buf ----------//
00488 void VgaBuf::rest_area_common_buf() {
00489     short* shortPtr = (short*) sys.common_data_buf;
00490 
00491     int x1 = *shortPtr++;
00492     int y1 = *shortPtr++;
00493     int x2 = *shortPtr++;
00494     int y2 = *shortPtr++;
00495 
00496     put_bitmapW( x1, y1, shortPtr );
00497 }
00498 
00499 //------------ End of function VgaBuf::rest_area_common_buf ----------//
00500 
00501 //---------- Begin of function VgaBuf::save_area ---------//
00512 short* VgaBuf::save_area(int x1, int y1, int x2, int y2, short* saveScr ) {
00513     err_when( x1>x2 || y1>y2 || x1<0 || y1<0 || x2>=VGA_WIDTH || y2>=VGA_HEIGHT );
00514 
00515     long newSize = sizeof(short)*4 + BitmapW::size((x2-x1+1),(y2-y1+1));
00516 
00517     saveScr = (short *)mem_resize( saveScr, newSize );
00518 
00519     short* shortPtr = (short*) saveScr;
00520 
00521     *shortPtr++ = x1;
00522     *shortPtr++ = y1;
00523     *shortPtr++ = x2;
00524     *shortPtr++ = y2;
00525 
00526     if( is_front )
00527         mouse.hide_area( x1,y1,x2,y2 );               // if the mouse cursor is in that area, hide it
00528 
00529     read_bitmapW( x1,y1,x2,y2, shortPtr );
00530 
00531     if( is_front )
00532         mouse.show_area();
00533 
00534     return saveScr;
00535 }
00536 
00537 //------------ End of function VgaBuf::save_area ---------//
00538 
00539 // ---  begin  ##chwg1021 --- //
00540 //----------- Begin of function VgaBuf::rest_area_with_clipping --------//
00553 void VgaBuf::rest_area_with_clipping(short* saveScr,
00554                                      int cx1, int cy1,
00555                                      int cx2, int cy2,
00556                                      int releaseFlag, int transparentFlag) {
00557     int x1,y1,x2,y2;
00558 
00559     if( saveScr == NULL )
00560         return;
00561 
00562     short* shortPtr = (short*) saveScr;
00563 
00564     x1 = *shortPtr++;
00565     y1 = *shortPtr++;
00566     x2 = *shortPtr++;
00567     y2 = *shortPtr++;
00568 
00569     err_when( x1>x2 || y1>y2 || x1<0 || y1<0 || x2>=VGA_WIDTH || y2>=VGA_HEIGHT );
00570 
00571     if( transparentFlag )
00572         put_bitmapW_area_trans( x1, y1, shortPtr, cx1, cy1, cx2, cy2);
00573     else
00574         put_bitmapW_area( x1, y1, shortPtr, cx1, cy1, cx2, cy2);
00575 
00576     if( releaseFlag )
00577         mem_del( saveScr );
00578 }
00579 
00580 //------------ End of function VgaBuf::rest_area_with_clipping ----------//
00581 // ---  end  ##chwg1021 --- //
00582 
00583 //----------- Begin of function VgaBuf::rest_area --------//
00592 void VgaBuf::rest_area(short* saveScr, int releaseFlag, int transparentFlag) {
00593     int  x1,y1,x2,y2;
00594 
00595     if( saveScr == NULL )
00596         return;
00597 
00598     short* shortPtr = (short*) saveScr;
00599 
00600     x1 = *shortPtr++;
00601     y1 = *shortPtr++;
00602     x2 = *shortPtr++;
00603     y2 = *shortPtr++;
00604 
00605     err_when( x1>x2 || y1>y2 || x1<0 || y1<0 || x2>=VGA_WIDTH || y2>=VGA_HEIGHT );
00606 
00607     if( transparentFlag )
00608         put_bitmapW_trans( x1, y1, shortPtr );
00609     else
00610         put_bitmapW( x1, y1, shortPtr );
00611 
00612     if( releaseFlag )
00613         mem_del( saveScr );
00614 }
00615 
00616 //------------ End of function VgaBuf::rest_area ----------//
00617 
00619 // old version 8 bit //
00621 //------------ Begin of function VgaBuf::write_bmp_file --------------//
00629 /*
00630 int VgaBuf::write_bmp_file(char* fileName)
00631 {
00632    File                         bmpFile;
00633    BITMAPINFO*  bmpInfoPtr = NULL;
00634    char*                        bitmapPtr = NULL;
00635 
00636    bmpFile.file_create(fileName, 1, 0);         // 1-handle error, 0-disable variable file size
00637 
00638    //------------ Write the file header ------------//
00639 
00640 BITMAPFILEHEADER bmpFileHdr;
00641 
00642 bmpFileHdr.bfType               = 0x4D42;                       // set the type to "BM"
00643 bmpFileHdr.bfSize               = buf_size();
00644 bmpFileHdr.bfReserved1 = 0;
00645 bmpFileHdr.bfReserved2 = 0;
00646 bmpFileHdr.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256;
00647 
00648 bmpFile.file_write(&bmpFileHdr, sizeof(bmpFileHdr));
00649 
00650 //------------ Write in the info header -----------//
00651 
00652 BITMAPINFOHEADER bmpInfoHdr;
00653 
00654 bmpInfoHdr.biSize                        = sizeof(BITMAPINFOHEADER);
00655 bmpInfoHdr.biWidth                       = buf_des.dwWidth;
00656 bmpInfoHdr.biHeight                      = buf_des.dwHeight;
00657 bmpInfoHdr.biPlanes                      = 1;
00658 bmpInfoHdr.biBitCount            = 8;
00659 bmpInfoHdr.biCompression         = BI_RGB;
00660 bmpInfoHdr.biSizeImage      = buf_size();
00661 bmpInfoHdr.biXPelsPerMeter = 0;
00662 bmpInfoHdr.biYPelsPerMeter = 0;
00663 bmpInfoHdr.biClrUsed             = 0;
00664 bmpInfoHdr.biClrImportant  = 0;
00665 
00666 bmpFile.file_write(&bmpInfoHdr, sizeof(bmpInfoHdr));
00667 
00668 //------------ write the color table -----------//
00669 
00670 LPDIRECTDRAWPALETTE ddPalettePtr;                               // get the direct draw surface's palette
00671 dd_buf->GetPalette(&ddPalettePtr);
00672 
00673 PALETTEENTRY *palEntries = (PALETTEENTRY*) mem_add( sizeof(PALETTEENTRY)*256 );
00674 ddPalettePtr->GetEntries(0, 0, 256, palEntries);
00675 
00676 RGBQUAD *colorTable = (RGBQUAD*) mem_add( sizeof(RGBQUAD)*256 );                // allocate a color table with 256 entries
00677 
00678 for( int i=0 ; i<256 ; i++ )
00679 {
00680 colorTable[i].rgbBlue  = palEntries[i].peBlue;
00681 colorTable[i].rgbGreen = palEntries[i].peGreen;
00682 colorTable[i].rgbRed   = palEntries[i].peRed;
00683 colorTable[i].rgbReserved = 0;
00684 }
00685 
00686 bmpFile.file_write(colorTable, sizeof(RGBQUAD)*256);
00687 
00688 mem_del(palEntries);
00689 mem_del(colorTable);
00690 
00691 //----------- write the bitmap ----------//
00692 
00693 for( int y=buf_height()-1 ; y>=0 ; y-- )                                        // write in reversed order as DIB's vertical order is reversed
00694 bmpFile.file_write(buf_ptr(0,y), buf_width());
00695 
00696 //------------ close the file -----------//
00697 
00698 bmpFile.file_close();
00699 
00700 return 1;
00701 }
00702 //------------ End of function VgaBuf::write_bmp_file --------------//
00703 */
00704 
00705 //------------ Begin of function VgaBuf::write_bmp_file --------------//
00713 int VgaBuf::write_bmp_file(char* fileName) {
00714     File       bmpFile;
00715     BITMAPINFO*  bmpInfoPtr = NULL;
00716     char*      bitmapPtr = NULL;
00717 
00718     int        hasPaletteFlag = 0;
00719 
00720     bmpFile.file_create(fileName, 1, 0);            // 1-handle error, 0-disable variable file size
00721 
00722     //------------ Write the file header ------------//
00723 
00724     BITMAPFILEHEADER bmpFileHdr;
00725 
00726     bmpFileHdr.bfType    = 0x4D42;                  // set the type to "BM"
00727     bmpFileHdr.bfSize    = buf_size();
00728     bmpFileHdr.bfReserved1 = 0;
00729     bmpFileHdr.bfReserved2 = 0;
00730     bmpFileHdr.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
00731     if( hasPaletteFlag )
00732         bmpFileHdr.bfOffBits += sizeof(RGBQUAD)*256;
00733 
00734     bmpFile.file_write(&bmpFileHdr, sizeof(bmpFileHdr));
00735 
00736     //------------ Write in the info header -----------//
00737 
00738     BITMAPINFOHEADER bmpInfoHdr;
00739 
00740     bmpInfoHdr.biSize       = sizeof(BITMAPINFOHEADER);
00741     bmpInfoHdr.biWidth      = buf_des.dwWidth;
00742     bmpInfoHdr.biHeight       = buf_des.dwHeight;
00743     bmpInfoHdr.biPlanes       = 1;
00744     bmpInfoHdr.biBitCount     = 24;
00745     bmpInfoHdr.biCompression   = BI_RGB;
00746     bmpInfoHdr.biSizeImage     = bmpInfoHdr.biWidth * bmpInfoHdr.biHeight * bmpInfoHdr.biBitCount / 8;
00747     bmpInfoHdr.biXPelsPerMeter = 0;
00748     bmpInfoHdr.biYPelsPerMeter = 0;
00749     bmpInfoHdr.biClrUsed    = 0;
00750     bmpInfoHdr.biClrImportant  = 0;
00751 
00752     bmpFile.file_write(&bmpInfoHdr, sizeof(bmpInfoHdr));
00753 
00754     //------------ write the color table -----------//
00755 
00756     if( hasPaletteFlag ) {
00757         LPDIRECTDRAWPALETTE ddPalettePtr;             // get the direct draw surface's palette
00758         dd_buf->GetPalette(&ddPalettePtr);
00759 
00760         PALETTEENTRY *palEntries = (PALETTEENTRY*) mem_add( sizeof(PALETTEENTRY)*256 );
00761         ddPalettePtr->GetEntries(0, 0, 256, palEntries);
00762 
00763         // allocate a color table with 256 entries
00764         RGBQUAD *colorTable = (RGBQUAD*) mem_add( sizeof(RGBQUAD)*256 );
00765 
00766         for( int i=0 ; i<256 ; i++ ) {
00767             colorTable[i].rgbBlue  = palEntries[i].peBlue;
00768             colorTable[i].rgbGreen = palEntries[i].peGreen;
00769             colorTable[i].rgbRed   = palEntries[i].peRed;
00770             colorTable[i].rgbReserved = 0;
00771         }
00772 
00773         bmpFile.file_write(colorTable, sizeof(RGBQUAD)*256);
00774 
00775         mem_del(palEntries);
00776         mem_del(colorTable);
00777     }
00778 
00779     //----------- write the bitmap ----------//
00780 
00781     if( bmpInfoHdr.biBitCount == 8 ) {
00782         for( int y=buf_height()-1 ; y>=0 ; y-- )      // write in reversed order as DIB's vertical order is reversed
00783             bmpFile.file_write(buf_ptr(0,y), buf_width());
00784     }
00785     else if( bmpInfoHdr.biBitCount == 24 ) {
00786         int lineBufferSize = sizeof(RGBColor) * bmpInfoHdr.biWidth;
00787         RGBColor *lineBuffer = (RGBColor *)mem_add( lineBufferSize );
00788         for( int y = buf_height()-1; y>=0 ; --y ) {
00789             register short *pixelPtr = buf_ptr( 0, y );
00790             register RGBColor *lineBufPtr = lineBuffer;
00791             for( int x = buf_width()-1; x >= 0; --x, ++pixelPtr, ++lineBufPtr) {
00792                 vga.decode_pixel( *pixelPtr, lineBufPtr );
00793                 if(lineBufPtr->blue>=0xf8)                // to make the printing output clearer
00794                     lineBufPtr->blue=0xff;                  // chwg0410
00795                 if(lineBufPtr->green>=0xf8)               // to make the printing output clearer
00796                     lineBufPtr->green=0xff;                 // chwg0410
00797                 if(lineBufPtr->red>=0xf8)                 // to make the printing output clearer
00798                     lineBufPtr->red=0xff;                   // chwg0410
00799                 // exchange Red and blue
00800                 BYTE r = lineBufPtr->red;
00801                 lineBufPtr->red = lineBufPtr->blue;
00802                 lineBufPtr->blue = r;
00803             }
00804             bmpFile.file_write(lineBuffer, lineBufferSize );
00805         }
00806         mem_del(lineBuffer);
00807     }
00808     else {
00809         err_here();
00810     }
00811 
00812     //------------ close the file -----------//
00813 
00814     bmpFile.file_close();
00815 
00816     return 1;
00817 }
00818 
00819 //------------ End of function VgaBuf::write_bmp_file --------------//
00820 
00821 //------------ Begin of function VgaBuf::write_bmp_file_area --------------//
00829 int VgaBuf::write_bmp_file_area(char* fileName, int x1, int y1, int x2, int y2) {
00830     File       bmpFile;
00831     BITMAPINFO*  bmpInfoPtr = NULL;
00832     char*      bitmapPtr = NULL;
00833 
00834     int        hasPaletteFlag = 0;
00835     int        bytePerPixel = sizeof(RGBColor);
00836 
00837     bmpFile.file_create(fileName, 1, 0);            // 1-handle error, 0-disable variable file size
00838 
00839     //------------ Write the file header ------------//
00840 
00841     BITMAPFILEHEADER bmpFileHdr;
00842 
00843     bmpFileHdr.bfType    = 0x4D42;                  // set the type to "BM"
00844     // buf_size();
00845     bmpFileHdr.bfSize    = (x2-x1+1)*(y2-y1+1)*bytePerPixel;
00846     bmpFileHdr.bfReserved1 = 0;
00847     bmpFileHdr.bfReserved2 = 0;
00848     bmpFileHdr.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
00849     if( hasPaletteFlag )
00850         bmpFileHdr.bfOffBits += sizeof(RGBQUAD)*256;
00851 
00852     bmpFile.file_write(&bmpFileHdr, sizeof(bmpFileHdr));
00853 
00854     //------------ Write in the info header -----------//
00855 
00856     BITMAPINFOHEADER bmpInfoHdr;
00857 
00858     bmpInfoHdr.biSize       = sizeof(BITMAPINFOHEADER);
00859     bmpInfoHdr.biWidth      = (x2-x1+1);            // buf_des.dwWidth;
00860     bmpInfoHdr.biHeight       = (y2-y1+1);          // buf_des.dwHeight;
00861     bmpInfoHdr.biPlanes       = 1;
00862     bmpInfoHdr.biBitCount     = bytePerPixel*8;
00863     bmpInfoHdr.biCompression   = BI_RGB;
00864     bmpInfoHdr.biSizeImage     = bmpInfoHdr.biWidth * bmpInfoHdr.biHeight * bmpInfoHdr.biBitCount / 8;
00865     bmpInfoHdr.biXPelsPerMeter = 0;
00866     bmpInfoHdr.biYPelsPerMeter = 0;
00867     bmpInfoHdr.biClrUsed    = 0;
00868     bmpInfoHdr.biClrImportant  = 0;
00869 
00870     bmpFile.file_write(&bmpInfoHdr, sizeof(bmpInfoHdr));
00871 
00872     //------------ write the color table -----------//
00873 
00874     if( hasPaletteFlag ) {
00875         LPDIRECTDRAWPALETTE ddPalettePtr;             // get the direct draw surface's palette
00876         dd_buf->GetPalette(&ddPalettePtr);
00877 
00878         PALETTEENTRY *palEntries = (PALETTEENTRY*) mem_add( sizeof(PALETTEENTRY)*256 );
00879         ddPalettePtr->GetEntries(0, 0, 256, palEntries);
00880 
00881         // allocate a color table with 256 entries
00882         RGBQUAD *colorTable = (RGBQUAD*) mem_add( sizeof(RGBQUAD)*256 );
00883 
00884         for( int i=0 ; i<256 ; i++ ) {
00885             colorTable[i].rgbBlue  = palEntries[i].peBlue;
00886             colorTable[i].rgbGreen = palEntries[i].peGreen;
00887             colorTable[i].rgbRed   = palEntries[i].peRed;
00888             colorTable[i].rgbReserved = 0;
00889         }
00890 
00891         bmpFile.file_write(colorTable, sizeof(RGBQUAD)*256);
00892 
00893         mem_del(palEntries);
00894         mem_del(colorTable);
00895     }
00896 
00897     //----------- write the bitmap ----------//
00898 
00899     if( bmpInfoHdr.biBitCount == 8 ) {
00900         for( int y=buf_height()-1 ; y>=0 ; y-- )      // write in reversed order as DIB's vertical order is reversed
00901             bmpFile.file_write(buf_ptr(0,y), buf_width());
00902     }
00903     else if( bmpInfoHdr.biBitCount == 24 ) {
00904         int lineBufferSize = sizeof(RGBColor) * bmpInfoHdr.biWidth;
00905         RGBColor *lineBuffer = (RGBColor *)mem_add( lineBufferSize );
00906         for( int y = y2; y>=y1 ; --y ) {
00907             short *pixelPtr = buf_ptr( x1, y );
00908             RGBColor *lineBufPtr = lineBuffer;
00909             for( int x = x1; x <= x2; ++x, ++pixelPtr, ++lineBufPtr) {
00910                 vga.decode_pixel( *pixelPtr, lineBufPtr );
00911                 if(lineBufPtr->blue>=0xf8)                // to make the printing output clearer
00912                     lineBufPtr->blue=0xff;                  // chwg0410
00913                 if(lineBufPtr->green>=0xf8)               // to make the printing output clearer
00914                     lineBufPtr->green=0xff;                 // chwg0410
00915                 if(lineBufPtr->red>=0xf8)                 // to make the printing output clearer
00916                     lineBufPtr->red=0xff;                   // chwg0410
00917                 // exchange Red and blue
00918                 BYTE r = lineBufPtr->red;
00919                 lineBufPtr->red = lineBufPtr->blue;
00920                 lineBufPtr->blue = r;
00921             }
00922             bmpFile.file_write(lineBuffer, lineBufferSize );
00923         }
00924         mem_del(lineBuffer);
00925     }
00926     else {
00927         err_here();
00928     }
00929 
00930     //------------ close the file -----------//
00931 
00932     bmpFile.file_close();
00933 
00934     return 1;
00935 }
00936 
00937 //------------ End of function VgaBuf::write_bmp_file --------------//
00938 
00939 //---------- Begin of function VgaBuf::put_large_bitmap ---------//
00957 
00958 void VgaBuf::put_large_bitmap(int x1, int y1, File* filePtr, short *colorRemapTable) {
00959     if( filePtr == NULL )
00960         return;
00961 
00962     if( !colorRemapTable )
00963         colorRemapTable = default_remap_table;
00964 
00965     int pictWidth = filePtr->file_get_short();
00966     int hasPalette=0;
00967 
00968     //------ read in bitmap and display it --------//
00969 
00970     int pictHeight = filePtr->file_get_short();
00971     int x2 = x1 + pictWidth  - 1;
00972     int y2 = y1 + pictHeight - 1;
00973 
00974     long pictSize = (long) pictWidth * pictHeight;
00975 
00976     err_when( x1>x2 || y1>y2 || x1<0 || y1<0 || x2>=VGA_WIDTH || y2>=VGA_HEIGHT );
00977 
00978     //---- if pict size less than 64K, read in the picture in one step ----//
00979 
00980     if( Bitmap::size(pictWidth, pictHeight) <= COMMON_DATA_BUF_SIZE ) {
00981         ((Bitmap *)sys.common_data_buf)->init(pictWidth, pictHeight);
00982 
00983         filePtr->file_read( ((Bitmap *)sys.common_data_buf)->bitmap, pictSize );
00984 
00985         if( is_front )
00986             mouse.hide_area( x1,y1,x2,y2 );             // if the mouse cursor is in that area, hide it
00987 
00988         put_bitmap_remap_fast( x1, y1, (char *)sys.common_data_buf, colorRemapTable );
00989 
00990         if( is_front )
00991             mouse.show_area();
00992     }
00993     else {                                          //----- if the picture size > 64K, read in line by line -----//
00994         // max. no. of lines can be in the buffer
00995         int bufferLine = (COMMON_DATA_BUF_SIZE - 2*sizeof(short) )/ pictWidth;
00996         int ty=y1+bufferLine-1;
00997 
00998         if( ty> y2 )
00999             ty=y2;
01000 
01001         while( y1<=y2 ) {
01002             ((Bitmap *)sys.common_data_buf)->init( pictWidth, (ty-y1+1) );
01003             filePtr->file_read( ((Bitmap *)sys.common_data_buf)->bitmap, (unsigned)pictWidth * (ty-y1+1) );
01004 
01005             if( is_front )
01006                 mouse.hide_area( x1,y1,x2,ty );           // if the mouse cursor is in that area, hide it
01007 
01008             put_bitmap_remap_fast( x1, y1, sys.common_data_buf, colorRemapTable );
01009 
01010             if( is_front )
01011                 mouse.show_area();
01012 
01013             y1 += bufferLine;
01014 
01015             if( (ty+=bufferLine) > y2 )
01016                 ty=y2;
01017         }
01018     }
01019 }
01020 
01021 //----------- End of function VgaBuf::put_large_bitmap --------//
01022 
01023 //---------- Begin of function VgaBuf::put_large_bitmapW ---------//
01041 
01042 void VgaBuf::put_large_bitmapW(int x1, int y1, File* filePtr) {
01043     if( filePtr == NULL )
01044         return;
01045 
01046     int pictWidth = filePtr->file_get_short();
01047 
01048     //------ read in bitmap and display it --------//
01049 
01050     int pictHeight = filePtr->file_get_short();
01051     int x2 = x1 + pictWidth  - 1;
01052     int y2 = y1 + pictHeight - 1;
01053 
01054     long pictSize = (long) pictWidth * pictHeight * sizeof(short);
01055 
01056     err_when( x1>x2 || y1>y2 || x1<0 || y1<0 || x2>=VGA_WIDTH || y2>=VGA_HEIGHT );
01057 
01058     //---- if pict size less than 64K, read in the picture in one step ----//
01059 
01060     if( BitmapW::size(pictWidth, pictHeight) <= COMMON_DATA_BUF_SIZE ) {
01061         ((BitmapW *)sys.common_data_buf)->init(pictWidth, pictHeight);
01062 
01063         filePtr->file_read( ((BitmapW *)sys.common_data_buf)->bitmap, pictSize );
01064 
01065         if( is_front )
01066             mouse.hide_area( x1,y1,x2,y2 );             // if the mouse cursor is in that area, hide it
01067 
01068         put_bitmapW( x1, y1, (short *)sys.common_data_buf );
01069 
01070         if( is_front )
01071             mouse.show_area();
01072     }
01073     else {                                          //----- if the picture size > 64K, read in line by line -----//
01074         // max. no. of lines can be in the buffer
01075         int bufferLine = (COMMON_DATA_BUF_SIZE - 2*sizeof(short) )/ pictWidth / sizeof(short);
01076         int ty=y1+bufferLine-1;
01077 
01078         if( ty> y2 )
01079             ty=y2;
01080 
01081         while( y1<=y2 ) {
01082             ((BitmapW *)sys.common_data_buf)->init( pictWidth, (ty-y1+1) );
01083             filePtr->file_read( ((BitmapW *)sys.common_data_buf)->bitmap, (unsigned)pictWidth * (ty-y1+1) * sizeof(short) );
01084 
01085             if( is_front )
01086                 mouse.hide_area( x1,y1,x2,ty );           // if the mouse cursor is in that area, hide it
01087 
01088             put_bitmapW( x1, y1, (short *)sys.common_data_buf);
01089 
01090             if( is_front )
01091                 mouse.show_area();
01092 
01093             y1 += bufferLine;
01094 
01095             if( (ty+=bufferLine) > y2 )
01096                 ty=y2;
01097         }
01098     }
01099 }
01100 
01101 //----------- End of function VgaBuf::put_large_bitmapW --------//
01102 
01103 //----------- Begin of function VgaBuf::convert_gray ----------//
01107 void VgaBuf::convert_gray(int x1, int y1, int x2, int y2) {
01108     // remap_bar(x1, y1, x2, y2, vga.gray_remap_table);
01109 }
01110 
01111 //--------- End of function VgaBuf::convert_gray -----------//

Generated on Fri Aug 23 01:38:34 2002 for VirtualU by doxygen1.2.17