Open GL Super Bible

Previous Table of Contents Next


Listing 22-8 Code to realize TPalette for the TWindowView class

// Handles WM_QUERYNEWPALETTE message
bool TOwlglWindowView::EvQueryNewPalette()
        {
        bool result;

        // Only if palette was created
        if(m_pPalette != NULL)
                {
                int nRet;

                // Select the palette into the current device context
                if(SelectPalette(m_hDC, m_pPalette->GetHandle(),FALSE)
                == NULL)
                        ::MessageBox(NULL,"Cannot select Palette","Error",
                        MB_OK);

                // Map entries from the currently selected palette to
                // the system palette.  The return value is the number
                // of palette entries modified.
                nRet = RealizePalette(m_hDC);

                if(nRet == 0)
                        ::MessageBox(NULL,"Cannot realize Palette",
                        "Error",MB_OK);

                // Repaint, forces remap of palette in current window
                Invalidate();

                return nRet;
                }

        // Call default function
        result = TWindowView::EvQueryNewPalette();
        return result;
        }

// Handles WM_PALETTECHANGED message
void TOwlglWindowView::EvPaletteChanged(THandle hWndPalChg)
        {
        // Only if palette created, or not this window
        if((m_pPalette != NULL) && (hWndPalChg != this->HWindow))
                {
                // Select the palette into the device context
                ::SelectPalette(m_hDC,m_pPalette->GetHandle(),FALSE);

                // Map entries to system palette
                ::RealizePalette(m_hDC);

                // Remap the current colors to the newly realized palette
                ::UpdateColors(m_hDC);
                return;
                }

         // Call default handler
         TWindowView::EvPaletteChanged(hWndPalChg);
         }

The code to realize the palette is very much like that in Chapter 8. Here, though, Windows does not send these messages to the TWindowView-derived class directly, but rather to the application’s class TDecoratedFrame (SDIDecFrame, for our example). This is because Windows only sends palette messages to the application’s main window. It is this window’s responsibility to route the messages to any child windows that need to be notified.

So use the Class Expert once again to add the two palette messages to the SDIDecFrame class. These message handlers, shown in Listing 22-9, simply find the child TWindowView and post the palette messages to it unchanged, allowing the window to respond as needed.

Listing 22-9 CMainFrame code to route palette-handling messages to the view

// Route WM_QUERYNEWPALETTE to child
bool SDIDecFrame::EvQueryNewPalette()
        {
        bool result;
        TWindow *pGLWindow;

        // Get the child SDI window
        pGLWindow = GetClientWindow();

        // Send the message
        if(pGLWindow)
                pGLWindow->SendMessage(WM_QUERYNEWPALETTE,0,0);

        return TRUE;
        }

// Route the WM_PALETTECHANGES to child
void SDIDecFrame::EvPaletteChanged(THandle hWndPalChg)
        {
        TWindow *pGLWindow;

        // Get the child SDI window
        pGLWindow = GetClientWindow();

        // Send the message
        if(pGLWindow)
                pGLWindow->SendMessage(WM_PALETTECHANGED, (UINT)
                hWndPalChg, (UINT)0);
        }

Summary

This chapter covered the specific mechanics of using OpenGL from an OWL-based program, demonstrating where to set the Windows styles required for OpenGL, where and when to set the pixel format, and creation of the rendering context. The example program also illustrates when and where to make the rendering context current, and how to realize an OWL TPalette when needed.

You should be able to take the sample application from this chapter and easily add your own custom OpenGL code. In addition, the framework—with all the OpenGL code in the glcode.c module—makes it easy to port existing C/OpenGL samples to our OWL shell program. You can study additional examples in many of the sample programs in this book, which are implemented in C, and in C++ using OWL and MFC (see Chapter 21).


Previous Table of Contents Next