Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

BitmapScreen Class Reference

A VScreen implemented as a bitmap. More...

#include <BitmapScreen.h>

Inherits VScreen.

List of all members.

Public Methods

DIBitmapgetWriteBuffer ()
bool DoneWriting ()
const DIBitmapgetReadBuffer ()
void DoneReading ()
void paint (HDC)
 BitmapScreen (const int, const int, const int, const int)

Private Attributes

DIBitmap buffer [2]
 Two DIBitmaps forming a double buffer.

HANDLE writeMutex
 A Handle to a Mutex used for serialising calls to getWriteBuffer().

CSWMRG currentBufferLock
 A single writer multiple reader guard protecting the switch between buffers.

volatile int currentBuffer
 An index into the array of DIBitmaps.


Detailed Description

Provides a double buffered di bitmap 'screen' with read and write locks. The bitmap being read is the 'currentBuffer', many simultaneous reads can be carried out. Writes are performed on the other buffer, writes are serialised and requests are blocking until reads have completed.

Definition at line 25 of file BitmapScreen.h.


Constructor & Destructor Documentation

BitmapScreen::BitmapScreen const int    tlX,
const int    tlY,
const int    w,
const int    h
 

Constructor - Takes the same parameters as VScreen

Definition at line 16 of file BitmapScreen.cpp.

References buffer, DIBitmap::createCompatible(), currentBuffer, and writeMutex.

00017 : VScreen(tlX, tlY, w, h)
00018 {
00019     currentBuffer = 0;
00020     buffer[0].createCompatible(w, h, 8);
00021     buffer[1].createCompatible(w, h, 8);
00022 
00023     writeMutex = CreateMutex(NULL, false, NULL);
00024 }


Member Function Documentation

void BitmapScreen::DoneReading  
 

Releases the read lock. This most be called for each read lock obtained or a write lock will never suceed and block any other lock attempts.

Definition at line 70 of file BitmapScreen.cpp.

References currentBufferLock, and CSWMRG::Done().

Referenced by Cls_OnCommand(), Cls_OnCreate(), and UpdateHandler::update().

00071 {
00072     currentBufferLock.Done();
00073 }

bool BitmapScreen::DoneWriting  
 

After checking the validity of the updated buffer a write lock is obtained the buffers are switched and the lock released. The Mutex serialising writes is also released.

Returns:
bool : true iff the buffer written to is a bitmap compatible with this screen

Definition at line 94 of file BitmapScreen.cpp.

References buffer, currentBuffer, currentBufferLock, CSWMRG::Done(), VScreen::getHeight(), DIBitmap::getPBITMAPINFOHEADER(), VScreen::getWidth(), CSWMRG::WaitToWrite(), and writeMutex.

Referenced by Cls_OnCreate(), and UpdateHandler::update().

00095 {
00096     int otherBuffer = 0 == currentBuffer ? 1 : 0;
00097     
00098     if( abs(buffer[otherBuffer].getPBITMAPINFOHEADER()->biHeight) != abs(VScreen::getHeight())
00099             || buffer[otherBuffer].getPBITMAPINFOHEADER()->biWidth != VScreen::getWidth() ){
00100         assert( ReleaseMutex(writeMutex) );
00101         return false;
00102     }
00103 
00104     currentBufferLock.WaitToWrite();
00105 
00106     currentBuffer = otherBuffer;
00107 
00108     currentBufferLock.Done();
00109 
00110     assert( ReleaseMutex(writeMutex) );
00111     return true;
00112 }

const DIBitmap * BitmapScreen::getReadBuffer  
 

Obtains a read lock and returns a pointer to a const DIBitmap. The pointer should be considered invalid after calling DoneReading.

Definition at line 60 of file BitmapScreen.cpp.

References buffer, currentBuffer, currentBufferLock, and CSWMRG::WaitToRead().

Referenced by Cls_OnCommand(), Cls_OnCreate(), and UpdateHandler::update().

00061 {
00062     currentBufferLock.WaitToRead();
00063     return &buffer[currentBuffer];
00064 }

DIBitmap * BitmapScreen::getWriteBuffer  
 

Returns a pointer to a writable DIBitmap. This function is serialised using the writeMutex. The pointer should be considered invalid after calling DoneWriting.

Definition at line 80 of file BitmapScreen.cpp.

References buffer, currentBuffer, and writeMutex.

Referenced by Cls_OnCreate(), and UpdateHandler::update().

00081 {
00082     WaitForSingleObject(writeMutex, INFINITE);
00083     return &buffer[0 == currentBuffer ? 1 : 0];
00084 }

void BitmapScreen::paint HDC    hDC [virtual]
 

Requests a read lock and then sets the dibs bits to the specified DC. Releases the read lock before returning.

Parameters:
hDC  : Handle to a device context.

Reimplemented from VScreen.

Definition at line 35 of file BitmapScreen.cpp.

References buffer, currentBuffer, currentBufferLock, CSWMRG::Done(), VScreen::getRgnRect(), and CSWMRG::WaitToRead().

Referenced by Cls_OnPaint().

00036 {
00037     currentBufferLock.WaitToRead();
00038 
00039     RECT regionR = VScreen::getRgnRect();
00040 
00041     SetDIBitsToDevice(
00042         hDC,
00043         regionR.left, regionR.top,
00044         buffer[currentBuffer].getPBITMAPINFOHEADER()->biWidth,
00045         abs(buffer[currentBuffer].getPBITMAPINFOHEADER()->biHeight),
00046         0, 0,
00047         0,
00048         abs(buffer[currentBuffer].getPBITMAPINFOHEADER()->biHeight),
00049         buffer[currentBuffer].getPBytes(),
00050         buffer[currentBuffer].getPBITMAPINFO(),
00051         DIB_RGB_COLORS);
00052 
00053     currentBufferLock.Done();
00054 }


The documentation for this class was generated from the following files:
Generated on Mon Mar 25 06:29:59 2002 for Window Dressing by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001