Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

W32SAL.CPP

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  w32sal.cpp                                                                          */
00003 /*                                                                                      */
00004 /*  Description:  GDI dib handling code                                                 */
00005 /*                                                                                      */
00006 /*                Code fragments contributed by John Miles                              */
00007 /*                                                                                      */
00008 /*  The contents of this file are subject to the Genesis3D Public License               */
00009 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00010 /*  compliance with the License. You may obtain a copy of the License at                */
00011 /*  http://www.genesis3d.com                                                            */
00012 /*                                                                                      */
00013 /*  Software distributed under the License is distributed on an "AS IS"                 */
00014 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00015 /*  the License for the specific language governing rights and limitations              */
00016 /*  under the License.                                                                  */
00017 /*                                                                                      */
00018 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00019 /*Genesis3D Version 1.1 released November 15, 1999                            */
00020 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
00021 /*                                                                                      */
00022 /****************************************************************************************/
00023 
00024 
00025 // Mike's notes:
00026 // not very much of this module is being used, and what is being used is only
00027 // being used by the software rasterizer.  This needs to be trimmed.  it would be
00028 // best to take out the superclassing system and replace it with some basic utility 
00029 // functions called from the client.
00030 // I think these would do it:
00031 //     paint()  to repaint
00032 //     activate()  to inform the engine about activation/deactivation
00033 //     fullscreen_toggle() 
00034 //
00035 //
00036 //  for the moment, I've disabled most of the superclassing behavior, so that it
00037 // does not interfere with the application's window (if it is in windowed mode)
00038 // I think this will work (there may be problems with full-screen software 
00039 // rasterization mode)
00040 #define DISABLED_BEHAVIOR
00041 
00042 
00043 #define WIN32_LEAN_AND_MEAN
00044 #include <windows.h>
00045 #include <windowsx.h>
00046 #include <winuser.h>
00047 #include <mmsystem.h>
00048 #include <Assert.h>
00049 
00050 #include "sal.h"
00051 
00052 #include <stdio.h>
00053 #include <dos.h>
00054 #include <malloc.h>
00055 #include <direct.h>
00056 #include <io.h>
00057 
00058 
00059 //#ifdef SAL_USE_D3D
00060 //      #include "d3dhal.h"
00061 //#endif
00062 
00063 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00064 //лл                                                                        лл
00065 //лл SAL statics and globals                                                лл
00066 //лл                                                                        лл
00067 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00068 
00069 //
00070 // Common variables
00071 //
00072 
00073 static  S32        desktop_w;                 // Windows desktop width, height
00074 static  S32        desktop_h;
00075 
00076 static  S32        cursor_state;              // Copy of Windows mouse hide/show state
00077 static  S32        show_count;                // System mouse cursor show count
00078 
00079 static  char       debug_log_filename[256];
00080 static  S32        log_to_file;               // TRUE if user wants debug log
00081 
00082 static  S32        app_minimized;             // TRUE if this SAL app has been minimized
00083 static  S32        app_active;                // TRUE if this SAL app has input focus
00084 static  S32        app_terminated;            // TRUE if app has received its quit message
00085 
00086 static  SALEXITCB  exit_callback;             // Address of mandatory exit callback
00087 static  SALFOCUSCB focus_callback;            // Address of focus callback, if any
00088 static  WNDPROC    window_callback;           // Address of WNDPROC, if any
00089 static  WNDPROC    OldWindowProc;
00090 static  HANDLE     hSem;                      // Semaphore to limit instances
00091 
00092 static  S32        mode_change_request;       // TRUE to toggle window/fullscreen
00093 
00094 static  HINSTANCE  hAppInstance;              // Application instance handle
00095 static  HINSTANCE  hDLLInstance;              // DLL instance handle
00096 static  char       szMyAppName[512];          // Application name
00097 static  HWND       hWnd;                      // Handle to application window
00098 static  HANDLE     hHook;                     // Handle to Windows hook object
00099 
00100 static  S32 SAL_preference[N_SAL_PREFS];      // Preferences array
00101 
00102 static  S32        mode_change_allowed;       // TRUE if window/fullscreen toggle allowed
00103 static  S32        current_window_mode;       // SAL_FULLSCREEN / SAL_WINDOW
00104 static  S32        current_bpp;               // Mode info set by last call to 
00105 static  S32        current_size_X;            //  SAL_set_display_mode
00106 static  S32        current_size_Y;
00107 
00108 static  LOGPALETTE       *pLogPal;            // LOGPALETTE structure
00109 static  BITMAPINFO       *pbmi;               // BITMAPINFO structure
00110 static  BITMAPINFOHEADER *pbmih;              // Pointer to pbmi header
00111 
00112 static  SAL_RGB32  palette_state[256];        // Current state of all DAC registers
00113 
00114 //
00115 // CreateDIBSection() (windowed mode) variables and related data
00116 //
00117 
00118 static  SAL_WINAREA area;                     // Location/size of window client area
00119 
00120 static  S32         desktop_bpp;              // Windows video mode BPP
00121 static  U32         desktop_R_bitmask;        // Desktop high-color pixel format
00122 static  U32         desktop_G_bitmask;        
00123 static  U32         desktop_B_bitmask;
00124 
00125 static  U32         DIB_R_bitmask;            // DIB high-color pixel format
00126 static  U32         DIB_G_bitmask;
00127 static  U32         DIB_B_bitmask;
00128 
00129 static  U32         DIB_R_Shift;
00130 static  U32         DIB_G_Shift;
00131 static  U32         DIB_B_Shift;
00132 
00133 static  S32         DIB_active;               // CreateDIBSection() active if TRUE
00134 
00135 static  U8         *lpDIBBuffer;              // DIB image buffer
00136 static  HBITMAP     hDIB;                     // Handle returned from CreateDIBSection
00137 static  HPALETTE    hPalette;                 // Handle to palette if 8-bit mode in use
00138 
00139 static  S32         palette_change_request;   // TRUE to signal palette update
00140 
00141 static  RECT        original_window_rect;     // Default size of DIB window
00142 static  SAL_WINAREA original_area;            // Default size of client area
00143 static  LONG            OldWindowLong_GWL_STYLE;        // Old Window GWL_STYLE 
00144 static  LONG            OldWindowLong_GWL_EXSTYLE;      // Old Window GWL_STYLE 
00145 
00146 static  RECT       unconstrained_rect;        // Default area of mouse constraint
00147 static  S32        constrain_state;           // 1 if mouse limited to window area
00148 static  S32        constrain_request;         // 1 if mouse should be constrained at next movement
00149 
00150 static  HDC Context;
00151 static  HBITMAP DefaultBitmap;
00152 
00153 extern "C" long FAR PASCAL MyWndProc(HWND   hWindow, UINT   message,   //)
00154                        WPARAM wParam,  LPARAM lParam);
00155 
00156 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00157 //лл                                                                        лл
00158 //лл DLLMain() function to acquire DLL instance handle, etc.                лл
00159 //лл                                                                        лл
00160 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00161 
00162 BOOL WINAPI DllMain(HINSTANCE hinstDLL,//)
00163                     DWORD     fdwReason,
00164                     LPVOID    lpvReserved)
00165 {
00166    if (fdwReason == DLL_PROCESS_ATTACH)
00167       {
00168       hDLLInstance = hinstDLL;
00169       }
00170 
00171    return TRUE;
00172 }
00173 
00174 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00175 //лл                                                                        лл
00176 //лл Serve Windows message queue, returning 0 if WM_QUIT message received   лл
00177 //лл                                                                        лл
00178 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00179 
00180 void serve_queue(void)
00181 {
00182         /*
00183         MSG msg;
00184 
00185         if (app_terminated)
00186         {
00187                 return;
00188         }
00189 
00190         //
00191         //      Serve message queue
00192         //
00193 
00194         while (PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE))
00195         {
00196                 if (!GetMessage(&msg, NULL, 0, 0 ))
00197                 {
00198                         app_active     = FALSE;
00199                         app_terminated = TRUE;
00200 
00201                         (exit_callback)();
00202 
00203                         //
00204                         // Return statement should not be reached
00205                         //
00206 
00207                         return;
00208                 }
00209 
00210                 TranslateMessage(&msg); 
00211                 DispatchMessage(&msg);
00212         }
00213         */
00214 }
00215 
00216 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00217 //лл                                                                        лл
00218 //лл Return rectangle containing client-area boundaries in screenspace      лл
00219 //лл                                                                        лл
00220 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00221 
00222 RECT *client_screen_rect(void)
00223 {
00224    static RECT  rect;
00225    POINT        ul,lr;
00226 
00227    GetClientRect(hWnd, &rect);
00228 
00229    ul.x = rect.left; 
00230    ul.y = rect.top; 
00231    lr.x = rect.right; 
00232    lr.y = rect.bottom; 
00233 
00234    ClientToScreen(hWnd, &ul); 
00235    ClientToScreen(hWnd, &lr); 
00236 
00237    SetRect(&rect, ul.x, ul.y, 
00238                   lr.x-1, lr.y-1); 
00239 
00240    return &rect;
00241 }
00242 
00243 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00244 //лл                                                                        лл
00245 //лл Mouse/keyboard event management                                        лл
00246 //лл                                                                        лл
00247 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00248 
00249 void MOUSE_event(unsigned long wParam, unsigned long lParam)
00250 {
00251 }
00252 
00253 void KEYDOWN_event(unsigned long wParam, unsigned long lParam)
00254 {
00255 }
00256 
00257 void KEYUP_event(unsigned long wParam, unsigned long lParam)
00258 {
00259 }
00260 
00261 void CHAR_event(unsigned long wParam, unsigned long lParam)
00262 {
00263 }
00264 
00265 
00266 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00267 //лл                                                                        лл
00268 //лл Shut down CreateDIBSection() services                                  лл
00269 //лл                                                                        лл
00270 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00271 
00272 void DIB_shutdown(void)
00273 {
00274    if (constrain_state)
00275       {
00276       #ifndef DISABLED_BEHAVIOR
00277                 ClipCursor(&unconstrained_rect);
00278           #endif
00279       constrain_request = 0;
00280       }
00281 
00282    if (DIB_active && SAL_is_app_active())
00283       {
00284       SAL_wipe_surface(SAL_BACK_SURFACE, 0);
00285       SAL_flip_surface();
00286       }
00287 
00288    if (hDIB != NULL)
00289       {
00290       DeleteObject(hDIB);
00291       hDIB = NULL;
00292       }
00293 
00294    if (hPalette != NULL)
00295       {
00296       DeleteObject(hPalette);
00297       hPalette = NULL;
00298       }
00299                 /*
00300                 //
00301                 //      Make sure we restore the old hWnd parms
00302                 //
00303                 SetWindowLong(hWnd, 
00304                              GWL_STYLE, 
00305                              OldWindowLong_GWL_STYLE);
00306                 SetWindowLong(hWnd, 
00307                              GWL_EXSTYLE, 
00308                              OldWindowLong_GWL_EXSTYLE);
00309                 SetWindowPos(hWnd, 
00310                             HWND_TOP, 
00311                             original_area.x,
00312                             original_area.y,
00313                                         original_area.width,
00314                             original_area.height,
00315                             SWP_NOCOPYBITS | SWP_NOZORDER);
00316                 
00317                 ShowWindow(hWnd, SW_SHOWNORMAL);
00318                 */
00319    DIB_active = 0;
00320 }
00321 
00322 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00323 //лл                                                                        лл
00324 //лл Start up CreateDIBSection() services for windowed display              лл
00325 //лл                                                                        лл
00326 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00327 
00328 S32 DIB_startup(S32   display_size_X,//)
00329                 S32   display_size_Y,
00330                 S32   display_bpp)
00331 {
00332         HDC      hdc;
00333         COLORREF color,save;
00334         RECT     window_rect;
00335 
00336         area.x                  = 0;
00337         area.y                  = 0;
00338         area.width              = display_size_X;
00339         area.height             = display_size_Y;
00340         original_area   = area;
00341         
00342         GetWindowRect(hWnd, &window_rect);
00343         original_window_rect = window_rect;
00344         
00345         OldWindowLong_GWL_STYLE = GetWindowLong(hWnd, GWL_STYLE);
00346         OldWindowLong_GWL_EXSTYLE = GetWindowLong(hWnd, GWL_EXSTYLE);
00347 
00348         display_size_X = area.width;
00349         display_size_Y = area.height;
00350 
00351         // Get desktop size
00352         //
00353         /*
00354         desktop_w = GetSystemMetrics(SM_CXSCREEN);
00355         desktop_h = GetSystemMetrics(SM_CYSCREEN);
00356 
00357         //
00358         // Enable caption menu and user preferences
00359         //
00360 
00361         SetWindowLong(hWnd, 
00362                  GWL_STYLE, 
00363                  GetWindowLong(hWnd, GWL_STYLE) & ~WS_POPUP);
00364 
00365         SetWindowLong(hWnd, 
00366                  GWL_STYLE, 
00367                  GetWindowLong(hWnd, GWL_STYLE) | (WS_OVERLAPPED  | 
00368                                                    WS_CAPTION     | 
00369                                                    WS_SYSMENU     | 
00370                                                    WS_MINIMIZEBOX));
00371 
00372         if (SAL_get_preference(SAL_ALLOW_WINDOW_RESIZE))
00373         {
00374                 SetWindowLong(hWnd, 
00375                     GWL_STYLE, 
00376                     GetWindowLong(hWnd, GWL_STYLE) | WS_THICKFRAME |
00377                                                      WS_MAXIMIZEBOX);
00378         }
00379 
00380         if (SAL_get_preference(SAL_ALWAYS_ON_TOP))
00381         {
00382                 SetWindowLong(hWnd, 
00383                     GWL_EXSTYLE, 
00384                     GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_TOPMOST);
00385         }
00386         else
00387         {
00388                 SetWindowLong(hWnd, 
00389                     GWL_EXSTYLE, 
00390                     GetWindowLong(hWnd, GWL_EXSTYLE) & ~WS_EX_TOPMOST);
00391         }
00392 
00393         //
00394         // If area not already established, center window's client area on
00395         // desktop, and size it to correspond to the display size for optimum 
00396         // performance (no stretching needed)
00397         //
00398 
00399         //if (area.width == -1)
00400         {
00401                 area.width  = display_size_X;
00402                 area.height = display_size_Y;
00403 
00404                 area.x = ((desktop_w - area.width ) / 2);
00405                 area.y = ((desktop_h - area.height) / 2);
00406         }
00407 
00408         //
00409         // Calculate adjusted position of window
00410         //
00411         // Do not allow overall window size to exceed desktop size; keep 
00412         // dividing height and width by 2 until entire window fits
00413         //
00414         // If window is offscreen (or almost entirely offscreen), center it
00415         //
00416 
00417         do
00418         {
00419                 retry = 0;
00420 
00421                 window_rect.left   = area.x;
00422                 window_rect.right  = area.x + area.width - 1;
00423                 window_rect.top    = area.y;
00424                 window_rect.bottom = area.y + area.height - 1;
00425 
00426                 AdjustWindowRectEx(&window_rect,
00427                           GetWindowLong(hWnd, GWL_STYLE),
00428                          (GetMenu(hWnd) != NULL),
00429                           GetWindowLong(hWnd, GWL_EXSTYLE));
00430 
00431                 if ((window_rect.right - window_rect.left + 1) > desktop_w)
00432                 {
00433                         area.width >>= 1;
00434                         area.x = ((desktop_w - area.width ) / 2);
00435                         retry = 1;
00436                 }
00437 
00438                 if ((window_rect.bottom - window_rect.top + 1) > desktop_h)
00439                 {
00440                         area.height >>= 1;
00441                         area.y = ((desktop_h - area.height) / 2);
00442                         retry = 1;
00443                 }
00444 
00445                 if ((window_rect.left   >= (desktop_w-16)) ||
00446                         (window_rect.top    >= (desktop_h-16)) ||
00447                         (window_rect.right  <= 16)          ||
00448                         (window_rect.bottom <= 16))
00449                 {
00450                         area.x = ((desktop_w - area.width ) / 2);
00451                         area.y = ((desktop_h - area.height) / 2);
00452                         retry = 1;
00453                 }
00454         }
00455    while (retry);
00456 
00457    //
00458    // Save window and client areas for restoration if maximize button pressed
00459    //
00460 
00461    original_window_rect = window_rect;
00462    original_area        = area;
00463 
00464    //
00465    // Set window size and position
00466    //
00467 
00468    SetWindowPos(hWnd, 
00469                 HWND_TOP, 
00470                 window_rect.left,
00471                 window_rect.top,
00472                 window_rect.right  - window_rect.left + 1,
00473                 window_rect.bottom - window_rect.top  + 1,
00474                 SWP_NOCOPYBITS | SWP_NOZORDER);
00475 
00476    GetClientRect(hWnd, 
00477                 &client_rect);
00478 
00479    SAL_debug_printf("SAL: Window at (%d,%d), client size = (%d,%d)\n",
00480       window_rect.left,
00481       window_rect.top,
00482       client_rect.right,
00483       client_rect.bottom);
00484 
00485    //
00486    // Make window visible
00487    //
00488 
00489    ShowWindow(hWnd, SW_SHOWNORMAL);
00490 
00491    //
00492    // If mouse is constrained, limit its travel to the window area
00493    //
00494 
00495    */
00496 
00497    if (constrain_state)    
00498       {
00499       constrain_request = 1;
00500       }
00501 
00502    //
00503    // Init DIB globals
00504    //
00505 
00506    hDIB        = NULL;
00507    lpDIBBuffer = NULL;
00508    hPalette    = NULL;
00509 
00510    pbmih->biSize          =  sizeof(BITMAPINFOHEADER);
00511    pbmih->biWidth         =  (display_size_X);
00512    pbmih->biHeight        = -(display_size_Y);
00513    pbmih->biPlanes        =  1;
00514    pbmih->biBitCount      =  (U16) display_bpp;
00515    pbmih->biSizeImage     =  0;
00516    pbmih->biXPelsPerMeter =  0;
00517    pbmih->biYPelsPerMeter =  0;
00518    pbmih->biClrUsed       =  0;
00519    pbmih->biClrImportant  =  0;
00520 
00521    //
00522    // Get Windows desktop display format (and masks, in high-color modes)
00523    //
00524 
00525    hdc = GetDC(hWnd);
00526 
00527    desktop_bpp = GetDeviceCaps(hdc, BITSPIXEL) * 
00528                  GetDeviceCaps(hdc, PLANES);
00529 
00530    SAL_debug_printf("SAL: Desktop = %dx%d, %d BPP\n",desktop_w, 
00531                                                      desktop_h, 
00532                                                      desktop_bpp);
00533 
00534    if(desktop_bpp > 8)
00535    {
00536            HBITMAP                      TempBmp;
00537            BITMAPINFO           TempInfo;
00538            OSVERSIONINFO        WinVer;
00539 
00540            memset(&WinVer, 0, sizeof(OSVERSIONINFO));
00541            WinVer.dwOSVersionInfoSize   =sizeof(OSVERSIONINFO);
00542 
00543            GetVersionEx(&WinVer);
00544            if(WinVer.dwPlatformId == VER_PLATFORM_WIN32_NT)
00545            {
00546                    TempBmp      =CreateCompatibleBitmap(hdc, 8, 8);
00547                    if(TempBmp)
00548                    {
00549                            memset(&TempInfo, 0, sizeof(BITMAPINFO));
00550                            TempInfo.bmiHeader.biSize            =sizeof(BITMAPINFO);
00551                            TempInfo.bmiHeader.biBitCount        =(U16)desktop_bpp;
00552                            TempInfo.bmiHeader.biCompression     =BI_BITFIELDS;
00553 
00554                            if(GetDIBits(hdc, TempBmp, 0, 0, NULL, &TempInfo, DIB_RGB_COLORS))
00555                            {
00556                                    desktop_R_bitmask    =*((U32 *)&TempInfo.bmiColors[0]);
00557                                    desktop_G_bitmask    =*((U32 *)&TempInfo.bmiColors[1]);
00558                                    desktop_B_bitmask    =*((U32 *)&TempInfo.bmiColors[2]);
00559 
00560                                    SAL_debug_printf("(%x-%x-%x)\n", desktop_R_bitmask, desktop_G_bitmask, desktop_B_bitmask);
00561                            }
00562                            DeleteObject(TempBmp);
00563                    }
00564            }
00565            else
00566            {
00567                    save = GetPixel(hdc,0,0);
00568 
00569                   SetPixel(hdc,0,0,RGB(0x08,0x08,0x08));
00570 
00571                   color = GetPixel(hdc,0,0) & 0xffffff;
00572 
00573                   SAL_debug_printf("SAL: Desktop pixel format = 0x%X ",color);
00574 
00575                   switch (color)
00576                          {
00577                          //
00578                          // 0x000000 = 5-5-5
00579                          //
00580 
00581                          case 0x000000:
00582             
00583                                 SAL_debug_printf("(5-5-5)\n");
00584 
00585                                 desktop_R_bitmask = 0x007c00;
00586                                 desktop_G_bitmask = 0x0003e0;
00587                                 desktop_B_bitmask = 0x00001f;
00588                                 break;
00589 
00590                          //
00591                          // 0x000800 = 5-6-5
00592                          //
00593 
00594                          case 0x000800:
00595             
00596                                 SAL_debug_printf("(5-6-5)\n");
00597 
00598                                 desktop_R_bitmask = 0x00f800;
00599                                 desktop_G_bitmask = 0x0007e0;
00600                                 desktop_B_bitmask = 0x00001f;
00601                                 break;
00602 
00603                          //
00604                          // 0x080808 = 8-8-8
00605                          //
00606 
00607                          case 0x080808:
00608 
00609                                 SAL_debug_printf("(8-8-8)\n");
00610 
00611                                 desktop_R_bitmask = 0xff0000;
00612                                 desktop_G_bitmask = 0x00ff00;
00613                                 desktop_B_bitmask = 0x0000ff;
00614                                 break;
00615 
00616                          default:
00617 
00618                                 SAL_debug_printf("(Unsupported, using 5-6-5)\n");
00619 
00620                                 if ((desktop_bpp == 15) || (desktop_bpp == 16))
00621                                    {
00622                                    desktop_R_bitmask = 0x00f800;
00623                                    desktop_G_bitmask = 0x0007e0;
00624                                    desktop_B_bitmask = 0x00001f;
00625                                    }
00626                                 break;
00627                          }
00628 
00629                   SetPixel(hdc,0,0,save);
00630                   }
00631    }
00632    ReleaseDC(hWnd, hdc);
00633 
00634    //
00635    // If DIB and desktop are both in 15/16-BPP mode, set DIB to desktop
00636    // pixel format for maximum throughput
00637    // 
00638    // Otherwise, set DIB to 5-6-5 mode if 16BPP DIB requested, or 8-8-8 mode
00639    // if 24BPP DIB requested
00640    //
00641    // Finally, if 8BPP DIB requested, create GDI palette object based on 
00642    // current logical palette
00643    //
00644 
00645    switch (display_bpp)
00646       {
00647       case 8:
00648    
00649          pbmih->biCompression = BI_RGB;
00650 
00651          hPalette = CreatePalette(pLogPal);
00652          palette_change_request = TRUE;
00653          break;
00654 
00655       case 16:
00656 
00657          pbmih->biCompression = BI_BITFIELDS;
00658 
00659          if ((desktop_bpp == 15) || (desktop_bpp == 16))
00660             {
00661             *(U32 *) (&(pbmi->bmiColors[0])) = desktop_R_bitmask;
00662             *(U32 *) (&(pbmi->bmiColors[1])) = desktop_G_bitmask;
00663             *(U32 *) (&(pbmi->bmiColors[2])) = desktop_B_bitmask;
00664             DIB_R_bitmask = desktop_R_bitmask;
00665             DIB_G_bitmask = desktop_G_bitmask;
00666             DIB_B_bitmask = desktop_B_bitmask;
00667 
00668             }
00669          else
00670             {
00671             *(U32 *) (&(pbmi->bmiColors[0])) = 0x00f800;
00672             *(U32 *) (&(pbmi->bmiColors[1])) = 0x0007e0;
00673             *(U32 *) (&(pbmi->bmiColors[2])) = 0x00001f;
00674             DIB_R_bitmask = 0x00f800;
00675             DIB_G_bitmask = 0x0007e0;
00676             DIB_B_bitmask = 0x00001f;
00677             }
00678          break;
00679 
00680       case 24:
00681 
00682          pbmih->biCompression = BI_BITFIELDS;
00683 
00684          *(U32 *) (&(pbmi->bmiColors[0])) = 0xff0000;
00685          *(U32 *) (&(pbmi->bmiColors[1])) = 0x00ff00;
00686          *(U32 *) (&(pbmi->bmiColors[2])) = 0x0000ff;
00687          DIB_R_bitmask = 0xff0000;
00688          DIB_G_bitmask = 0x00ff00;
00689          DIB_B_bitmask = 0x0000ff;
00690          break;
00691       }
00692 
00693    //
00694    // Allocate the DIB section ("back buffer")
00695    //
00696 
00697    hdc = GetDC(hWnd);
00698 
00699    hDIB = CreateDIBSection(hdc,             // Device context
00700                            pbmi,            // BITMAPINFO structure
00701                            DIB_RGB_COLORS,  // Color data type
00702                 (void **) &lpDIBBuffer,     // Address of image map pointer
00703                            NULL,            // File
00704                            0);              // Bitmap file offset
00705 
00706    ReleaseDC(hWnd, hdc);
00707    
00708    if (hDIB == NULL)
00709       {
00710       SAL_error_box(NULL,"CreateDIBSection() failed\n");
00711       DIB_shutdown();
00712       return FALSE;
00713       }
00714    
00715    //
00716    // Set global flag to indicate CreateDIBSection() is active
00717    // 
00718 
00719    DIB_active = 1;
00720 
00721    return TRUE;
00722 }
00723 
00724 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00725 //лл                                                                        лл
00726 //лл Copy current contents of DIB buffer to output window                   лл
00727 //лл                                                                        лл
00728 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00729 
00730 void DIB_refresh_surface(void)
00731 {
00732    HDC hdc;
00733 
00734    hdc = GetDC(hWnd);
00735 
00736    //
00737    // Select palette if desktop running in 8-bit mode
00738    //
00739    // If palette has changed, realize it
00740    //
00741 
00742    if (desktop_bpp == 8)
00743       {
00744       SelectPalette(hdc, 
00745                     hPalette, 
00746                     0);
00747 
00748       if (palette_change_request)
00749          {
00750          palette_change_request = 0;
00751          RealizePalette(hdc);
00752          }
00753       }
00754 
00755    //
00756    // Disable Boolean operations during stretching
00757    //
00758 
00759    SetStretchBltMode(hdc, COLORONCOLOR);
00760 
00761    //
00762    // Stretch bitmap to conform to the size of the output window
00763    //
00764    /*
00765    StretchDIBits(hdc,            // Destination DC
00766                  0,              // Destination X
00767                  0,              // Destination Y
00768                  area.width,     // Destination (client area) width
00769                  area.height,    // Destination (client area) height 
00770                  0,              // Source X
00771                  0,              // Source Y
00772                  current_size_X, // Source (back buffer) width
00773                  current_size_Y, // Source (back buffer) height
00774                  lpDIBBuffer,    // Pointer to source (back buffer)
00775                  pbmi,           // Bitmap info for back buffer
00776                  DIB_RGB_COLORS, // Bitmap contains index values
00777                  SRCCOPY);       // Do normal copy with stretching
00778    */
00779    StretchDIBits(hdc,            // Destination DC
00780                  0,              // Destination X
00781                  0,              // Destination Y
00782                  current_size_X,     // Destination (client area) width
00783                  current_size_Y,    // Destination (client area) height 
00784                  0,              // Source X
00785                  0,              // Source Y
00786                  current_size_X, // Source (back buffer) width
00787                  current_size_Y, // Source (back buffer) height
00788                  lpDIBBuffer,    // Pointer to source (back buffer)
00789                  pbmi,           // Bitmap info for back buffer
00790                  DIB_RGB_COLORS, // Bitmap contains index values
00791                  SRCCOPY);       // Do normal copy with stretching
00792    
00793    ReleaseDC(hWnd, hdc);
00794 }
00795 
00796 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00797 //лл                                                                        лл
00798 //лл Flip CreateDIBSection() surface (simulated by StretchDIB())            лл
00799 //лл                                                                        лл
00800 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00801 
00802 void DIB_flip_surface(void)
00803 {
00804         //while (1)
00805         //{
00806                 //serve_queue();
00807 
00808                 if (!SAL_is_app_active())
00809                 {
00810                         Sleep(10);
00811                 }
00812                 else
00813                 {
00814                         DIB_refresh_surface();
00815                     //break;
00816                 }
00817         //} 
00818 }
00819 
00820 DXDEF void WINAPI SAL_blit_surface(void)
00821 {
00822         DIB_flip_surface();
00823 }
00824 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00825 //лл                                                                        лл
00826 //лл Wipe CreateDIBSection() surface                                        лл
00827 //лл                                                                        лл
00828 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00829 
00830 void DIB_wipe_surface           (S32        surface,//)
00831                                  U32        color)
00832 {
00833    memset(lpDIBBuffer,
00834           color,
00835           current_size_X * current_size_Y * (current_bpp / 8));
00836 }
00837 
00838 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00839 //лл                                                                        лл
00840 //лл Return pointer to CreateDIBSection() surface                           лл
00841 //лл                                                                        лл
00842 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00843 
00844 void DIB_lock_surface         (S32        surface,//)
00845                                U8       **ptr,
00846                                S32       *pitch)
00847 {
00848    if (ptr != NULL)
00849       {
00850       *ptr = lpDIBBuffer;
00851       }
00852 
00853    if (pitch != NULL)
00854       {
00855       *pitch = current_size_X * (current_bpp / 8);
00856       }
00857 }
00858 
00859 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00860 //лл                                                                        лл
00861 //лл Release pointer to CreateDIBSection() surface                          лл
00862 //лл                                                                        лл
00863 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00864 
00865 void DIB_release_surface        (S32        surface,//)
00866                                  S32        perform_flip)
00867 {
00868    if (perform_flip)
00869       {
00870       SAL_flip_surface();
00871       }
00872 }
00873 
00874 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00875 //лл                                                                        лл
00876 //лл Start up SAL services                                                  лл
00877 //лл                                                                        лл
00878 //лл Initialize preferences                                                 лл
00879 //лл                                                                        лл
00880 //лл Non-zero: Success                                                      лл
00881 //лл        0: Failure                                                      лл
00882 //лл       -1: Attempt to launch multiple instances                         лл
00883 //лл                                                                        лл
00884 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00885 
00886 DXDEF S32 WINAPI SAL_startup(BOOL FileLog)
00887 {
00888    log_to_file = FileLog;
00889    strcpy(debug_log_filename, "debug.txt");
00890 
00891    app_active             =  FALSE;  // App not activated yet
00892    app_terminated         =  FALSE;  // App not terminated yet
00893    app_minimized          =  FALSE;  // App not minimized by default
00894 
00895    show_count             =  0;      // Mouse cursor show count
00896    cursor_state           =  1;      // (Windows cursor is on by default)
00897    constrain_state        =  0;      // Mouse is not constrained to window
00898    constrain_request      =  0;      // Mouse should not be constrained at next movement
00899 
00900    area.width             = -1;
00901 
00902    focus_callback         =  NULL;
00903    window_callback        =  NULL;
00904    hWnd                   =  NULL;
00905    hHook                  =  NULL;
00906    mode_change_request    =  FALSE;
00907    hSem                   =  NULL;
00908 
00909    DIB_active             =  FALSE;
00910 
00911    mode_change_allowed    =  FALSE;
00912    current_window_mode    =  SAL_FULLSCREEN;
00913    current_bpp            =  0;
00914    current_size_X         =  0;
00915    current_size_Y         =  0;
00916 
00917    palette_change_request = FALSE;
00918 
00919    //
00920    // Get original mouse constraint area (normally entire screen)
00921    //
00922         #ifndef DISABLED_BEHAVIOR
00923           GetClipCursor(&unconstrained_rect);
00924         #endif
00925    //
00926    // Allocate palette and bitmap structures at startup time to avoid
00927    // memory fragmentation during mode switches
00928    //
00929    // Clear palette structure to black to avoid screen flash when
00930    // setting initial mode
00931    //
00932 
00933    pLogPal = (LOGPALETTE *) 
00934              malloc (sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256));
00935 
00936    if (pLogPal == NULL)
00937       {
00938       return 0;
00939       }
00940 
00941    memset(pLogPal,0, sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256));
00942 
00943    pLogPal->palVersion    = 0x300;
00944    pLogPal->palNumEntries = 256;
00945 
00946    pbmi = (BITMAPINFO *) 
00947           malloc(sizeof (BITMAPINFOHEADER) + (sizeof (RGBQUAD) * 256));
00948 
00949    if (pbmi == NULL)
00950       {
00951       free(pLogPal);
00952       return 0;
00953       }
00954  
00955    memset(pbmi, 0, sizeof (BITMAPINFOHEADER) + (sizeof (RGBQUAD) * 256));
00956 
00957    pbmih = &(pbmi->bmiHeader);
00958 
00959    return TRUE;
00960 }
00961 
00962 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00963 //лл                                                                        лл
00964 //лл System Abstraction Layer exit function                                 лл
00965 //лл                                                                        лл
00966 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00967 
00968 DXDEF void WINAPI SAL_shutdown(void)
00969 {
00970 //      if (OldWindowProc)
00971 //      {
00972 //              SetWindowLong( hWnd, GWL_WNDPROC, (LONG)OldWindowProc);
00973 //              OldWindowProc = NULL;
00974 //      }
00975 
00976    if (DIB_active)
00977       {
00978       DIB_shutdown();
00979       }
00980 
00981    if (pbmi != NULL)
00982       {
00983       free(pbmi);
00984       pbmi = NULL;
00985       }
00986 
00987    if (pLogPal != NULL)
00988       {
00989       free(pLogPal);
00990       pLogPal = NULL;
00991       }
00992 }
00993 
00994 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
00995 //лл                                                                        лл
00996 //лл Set SAL operational preferences and policies                           лл
00997 //лл                                                                        лл
00998 //лл May be called by applications which need to alter the default          лл
00999 //лл behavior of the SAL system                                             лл
01000 //лл                                                                        лл
01001 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01002 
01003 DXDEF S32 WINAPI SAL_set_preference(U32 number, S32 value)
01004 {
01005    S32 old;
01006 
01007    old = SAL_preference[number];
01008 
01009    SAL_preference[number] = value;
01010 
01011    return old;
01012 }
01013 
01014 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01015 //лл                                                                        лл
01016 //лл Get SAL operational preferences and policies                           лл
01017 //лл                                                                        лл
01018 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01019 
01020 DXDEF S32 WINAPI SAL_get_preference(U32 number)
01021 {
01022    return SAL_preference[number];
01023 }
01024 
01025 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01026 //лл                                                                        лл
01027 //лл Monochrome debug printf() function                                     лл
01028 //лл                                                                        лл
01029 //лл Text written to logfile if WIN.INI / DOS environment option enabled    лл
01030 //лл                                                                        лл
01031 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01032 
01033 DXDEF void __cdecl SAL_debug_printf(char *fmt, ...)
01034 {
01035    static char work_string[4096];
01036    FILE       *log;
01037 
01038    if ((fmt == NULL) || (strlen(fmt) > sizeof(work_string)))
01039       {
01040       strcpy(work_string, "(String missing or too large)");
01041       }
01042    else
01043       {
01044       va_list ap;
01045 
01046       va_start(ap, 
01047                fmt);
01048 
01049       vsprintf(work_string, 
01050                fmt, 
01051                ap);
01052 
01053       va_end  (ap);
01054       }
01055 
01056    if (log_to_file)
01057       {
01058       log = fopen(debug_log_filename,"a+t");
01059       
01060       if (log != NULL)
01061          {
01062          fprintf(log,"%s\n",work_string);
01063          fclose(log);
01064          }
01065       }
01066 
01067    OutputDebugString(work_string);
01068 }
01069 
01070 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01071 //лл                                                                        лл
01072 //лл Message box display function                                           лл
01073 //лл                                                                        лл
01074 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01075 
01076 DXDEF void __cdecl SAL_error_box(C8 *caption, C8 *fmt, ...)
01077 {
01078    static char work_string[4096];
01079    S32         restore_cursor;
01080 
01081    restore_cursor = cursor_state;
01082 
01083    if (!cursor_state)
01084       {
01085                 #ifndef DISABLED_BEHAVIOR
01086                         ShowCursor(1);
01087                 #endif
01088       }
01089 
01090    if ((fmt == NULL) || (strlen(fmt) > sizeof(work_string)))
01091       {
01092       strcpy(work_string, "(String missing or too large)");
01093       }
01094    else
01095       {
01096       va_list ap;
01097 
01098       va_start(ap, 
01099                fmt);
01100 
01101       vsprintf(work_string, 
01102                fmt, 
01103                ap);
01104 
01105       va_end  (ap);
01106       }
01107 
01108    SAL_debug_printf("%s\n",work_string);
01109 
01110    //
01111    // If DirectDraw active, display GDI surface for dialog box
01112    // 
01113 
01114    //
01115    // If in 8-bpp mode, save current palette and switch to standard palette
01116    // 
01117 
01118    if (caption == NULL)
01119       {
01120       MessageBox(hWnd, 
01121                  work_string,
01122                  "SAL Error", 
01123                  MB_OK);
01124       }
01125    else
01126       {
01127       MessageBox(hWnd, 
01128                  work_string,
01129                  caption,
01130                  MB_OK);
01131       }
01132 
01133    if (!restore_cursor)
01134       {
01135                 #ifndef DISABLED_BEHAVIOR
01136                         ShowCursor(0);
01137                 #endif
01138       }
01139 }
01140 
01141 DXDEF BOOL WINAPI SAL_set_main_window(HWND hWindow)
01142 {
01143         hWnd = hWindow;
01144 
01145         app_active = TRUE;
01146 
01147 //      OldWindowProc = (WNDPROC)SetWindowLong( hWnd, GWL_WNDPROC, (LONG)MyWndProc);
01148 
01149 //      if (!OldWindowProc)
01150 //              return FALSE;
01151 
01152         return TRUE;
01153 }
01154 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01155 //лл                                                                        лл
01156 //лл Set display mode and window size                                       лл
01157 //лл                                                                        лл
01158 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01159 
01160 DXDEF S32        WINAPI SAL_set_display_mode        (S32   display_size_X,//)
01161                                                      S32   display_size_Y,
01162                                                      S32   display_bpp,
01163                                                      S32   initial_window_mode,
01164                                                      S32   allow_mode_switch)
01165 {
01166    S32 result;
01167 
01168    //
01169    // Shut down current video mode
01170    //
01171 
01172    if (DIB_active)
01173    {
01174                   DIB_shutdown();
01175    }
01176 
01177    //
01178    // Hide or show mouse cursor, as appropriate
01179    //
01180 
01181    if (show_count < 1)
01182       {
01183       //
01184       // Hide mouse cursor if it's currently visible
01185       //
01186 
01187       if (cursor_state)
01188          {
01189          cursor_state = 0;
01190                         #ifndef DISABLED_BEHAVIOR
01191                                 ShowCursor(0);
01192                         #endif
01193                         
01194          }
01195       }
01196    else
01197       {
01198       //
01199       // Show mouse cursor if it's currently hidden
01200       //
01201 
01202       if (!cursor_state)
01203          {
01204          cursor_state = 1;
01205                         #ifndef DISABLED_BEHAVIOR
01206                                 ShowCursor(1);
01207                         #endif
01208          }
01209       }
01210 
01211    //
01212    // Start up new video mode
01213    //
01214 
01215    if (initial_window_mode == SAL_FULLSCREEN)
01216       {
01217       //
01218       // SAL_FULLSCREEN: Set up fullscreen video mode using DirectDraw
01219       //
01220 
01221       }
01222    else if (initial_window_mode == SAL_WINDOW)
01223       {
01224       //
01225       // SAL_WINDOW: Set up window using CreateDIBSection()
01226       //
01227 
01228       result = DIB_startup(display_size_X,
01229                            display_size_Y,
01230                            display_bpp);
01231         }
01232         else if (initial_window_mode == SAL_TRY_FULLSCREEN)
01233         {
01234                 assert(0);
01235 
01236       //
01237       // SAL_TRY_FULLSCREEN: Use DirectDraw if possible/available, otherwise
01238       //                     fall back automatically to CreateDIBSection()
01239       //
01240 
01241       }
01242    else
01243       {
01244       //
01245       // Unknown window mode, return failure
01246       //
01247 
01248       return FALSE;
01249       }
01250 
01251    //
01252    // If successful, update global variables to reflect new video mode
01253    //
01254 
01255    if (result)
01256       {
01257       mode_change_allowed = allow_mode_switch;
01258       current_window_mode = initial_window_mode;
01259       current_bpp         = display_bpp;
01260       current_size_X      = display_size_X;
01261       current_size_Y      = display_size_Y;
01262       }
01263 
01264    return result;
01265 }
01266 
01267 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01268 //лл                                                                        лл
01269 //лл Return current window mode                                             лл
01270 //лл                                                                        лл
01271 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01272 
01273 DXDEF S32 WINAPI  SAL_window_status           (void)
01274 {
01275    return current_window_mode;
01276 }
01277 
01278 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01279 //лл                                                                        лл
01280 //лл Return area of client window in screen coordinates                     лл
01281 //лл                                                                        лл
01282 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01283 
01284 DXDEF void WINAPI SAL_client_area             (SAL_WINAREA *area)
01285 {
01286    RECT *rect;
01287 
01288    if (DIB_active)
01289       {
01290       rect = client_screen_rect();
01291 
01292       area->x      = rect->left;
01293       area->y      = rect->top;
01294       area->width  = rect->right  - rect->left + 1;
01295       area->height = rect->bottom - rect->top  + 1;
01296       }
01297 
01298 }
01299 
01300 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01301 //лл                                                                        лл
01302 //лл Return area of entire window in screen coordinates                     лл
01303 //лл                                                                        лл
01304 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01305 
01306 DXDEF void WINAPI SAL_window_area             (SAL_WINAREA *area)
01307 {
01308    RECT window_rect;
01309 
01310    if (DIB_active)
01311       {
01312       GetWindowRect(hWnd, &window_rect);
01313 
01314       area->x      = window_rect.left;
01315       area->y      = window_rect.top;
01316       area->width  = window_rect.right  - window_rect.left + 1;
01317       area->height = window_rect.bottom - window_rect.top  + 1;
01318       }
01319 
01320 }
01321 
01322 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01323 //лл                                                                        лл
01324 //лл Return TRUE if app has input focus                                     лл
01325 //лл                                                                        лл
01326 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01327 
01328 DXDEF S32 WINAPI  SAL_is_app_active           (void)
01329 {
01330    return app_active;
01331 }
01332 
01333 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01334 //лл                                                                        лл
01335 //лл Register app function to be called when focus gained or lost           лл
01336 //лл                                                                        лл
01337 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01338 
01339 DXDEF SALFOCUSCB WINAPI SAL_register_focus_callback (SALFOCUSCB fn)
01340 {
01341    SALFOCUSCB old;
01342 
01343    old = focus_callback;
01344 
01345    focus_callback = fn;
01346 
01347    return old;
01348 }
01349 
01350 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01351 //лл                                                                        лл
01352 //лл Register application WNDPROC function                                  лл
01353 //лл                                                                        лл
01354 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01355 
01356 DXDEF WNDPROC WINAPI SAL_register_WNDPROC     (WNDPROC fn)
01357 {
01358    WNDPROC old;
01359 
01360    old = window_callback;
01361 
01362    window_callback = fn;
01363 
01364    return old;
01365 }
01366 
01367 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01368 //лл                                                                        лл
01369 //лл Service Windows message queue, handling any mode change request        лл
01370 //лл                                                                        лл
01371 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01372 
01373 DXDEF void WINAPI SAL_serve_message_queue(void)
01374 {
01375    S32 result;
01376 
01377    serve_queue();
01378 
01379    if (mode_change_request && mode_change_allowed)
01380       {
01381       mode_change_request = 0;   
01382 
01383       result = SAL_set_display_mode(
01384                   current_size_X,
01385                   current_size_Y,
01386                   current_bpp,
01387                   current_window_mode ^ (SAL_FULLSCREEN ^ SAL_WINDOW),
01388                   TRUE);
01389 
01390       //
01391       // If window mode toggle did not succeed, restore original mode
01392       //
01393 
01394       if (!result)
01395          {
01396          SAL_debug_printf("SAL: SAL_set_display_mode() failed, restoring old mode\n");
01397 
01398          SAL_set_display_mode(
01399             current_size_X,
01400             current_size_Y,
01401             current_bpp,
01402             current_window_mode,
01403             TRUE);
01404          }
01405       }
01406 }
01407 
01408 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01409 //лл                                                                        лл
01410 //лл Set a single palette entry                                             лл
01411 //лл                                                                        лл
01412 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01413 
01414 DXDEF void WINAPI SAL_set_palette_entry      (S32        index,//)
01415                                               SAL_RGB32 *entry,
01416                                               S32        wait_flag)
01417 {
01418    //
01419    // If we're not in 8BPP mode, bail out
01420    // 
01421 
01422    if (current_bpp != 8)
01423       {
01424       return;
01425       }
01426 
01427    //
01428    // Update global palette state
01429    //
01430 
01431    palette_state[index] = *entry;
01432 
01433    pLogPal->palPalEntry[index].peRed   = pbmi->bmiColors[index].rgbRed   = (unsigned char) entry->r;
01434    pLogPal->palPalEntry[index].peGreen = pbmi->bmiColors[index].rgbGreen = (unsigned char) entry->g;
01435    pLogPal->palPalEntry[index].peBlue  = pbmi->bmiColors[index].rgbBlue  = (unsigned char) entry->b;
01436    pLogPal->palPalEntry[index].peFlags = NULL;
01437 
01438    //
01439    // Update DirectDraw palette, if appropriate
01440    //
01441 
01442 
01443    if (DIB_active)
01444       {
01445       if (hPalette != NULL)
01446          {
01447          DeleteObject(hPalette);
01448          }
01449 
01450       hPalette = CreatePalette(pLogPal);
01451 
01452       palette_change_request = TRUE;
01453       }
01454 }
01455 
01456 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01457 //лл                                                                        лл
01458 //лл Retrieve a single palette entry                                        лл
01459 //лл                                                                        лл
01460 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01461 
01462 DXDEF void WINAPI SAL_get_palette_entry      (S32        index,//)
01463                                               SAL_RGB32 *entry)
01464 {
01465    *entry = palette_state[index];
01466 }
01467 
01468 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01469 //лл                                                                        лл
01470 //лл Set a range of palette entries                                         лл
01471 //лл                                                                        лл
01472 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01473 
01474 DXDEF void WINAPI SAL_set_palette_range      (S32        index,//)
01475                                               S32        num_entries,
01476                                               SAL_RGB32 *entry_list,
01477                                               S32        wait_flag)
01478 {
01479    S32 i;
01480    S32 j;
01481 
01482    //
01483    // If we're not in 8BPP mode, bail out
01484    // 
01485 
01486    if (current_bpp != 8)
01487       {
01488       return;
01489       }
01490 
01491    //
01492    // Update global palette state
01493    //
01494 
01495    for (i=0; i < num_entries; i++)
01496       {
01497       palette_state[index+i] = entry_list[i];
01498       }
01499 
01500    for (i=0, j=index; i < num_entries; i++, j++)
01501       {
01502       pLogPal->palPalEntry[j].peRed   = pbmi->bmiColors[j].rgbRed   = (unsigned char) entry_list[i].r;
01503       pLogPal->palPalEntry[j].peGreen = pbmi->bmiColors[j].rgbGreen = (unsigned char) entry_list[i].g;
01504       pLogPal->palPalEntry[j].peBlue  = pbmi->bmiColors[j].rgbBlue  = (unsigned char) entry_list[i].b;
01505       pLogPal->palPalEntry[j].peFlags = NULL;
01506       }
01507 
01508    //
01509    // Update DirectDraw palette, if appropriate
01510    //
01511 
01512 
01513    if (DIB_active)
01514       {
01515       if (hPalette != NULL)
01516          {
01517          DeleteObject(hPalette);
01518          }
01519 
01520       hPalette = CreatePalette(pLogPal);
01521 
01522       palette_change_request = TRUE;
01523       }
01524 }
01525                                    
01526 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01527 //лл                                                                        лл
01528 //лл Retrieve a range of palette entries                                    лл
01529 //лл                                                                        лл
01530 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01531 
01532 DXDEF void WINAPI SAL_get_palette_range      (S32        index,//)
01533                                               S32        num_entries,
01534                                               SAL_RGB32 *entry_list)
01535 {
01536    for (int i=0; i < num_entries; i++)
01537       {
01538       entry_list[i] = palette_state[index+i];
01539       }
01540 }
01541 
01542 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01543 //лл                                                                        лл
01544 //лл Make back surface visible                                              лл
01545 //лл                                                                        лл
01546 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01547 
01548 DXDEF void WINAPI SAL_flip_surface           (void)
01549 {
01550         if (DIB_active)
01551       {
01552       DIB_flip_surface();
01553       }
01554 }
01555 
01556 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01557 //лл                                                                        лл
01558 //лл Clear a surface to a desired color                                     лл
01559 //лл                                                                        лл
01560 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01561 
01562 DXDEF void WINAPI SAL_wipe_surface           (S32        surface,//)
01563                                               U32        color)
01564 {
01565         if (DIB_active)
01566       {
01567       DIB_wipe_surface(surface, color);
01568       }
01569 }
01570 
01571 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01572 //лл                                                                        лл
01573 //лл Request a pointer to a surface's memory block                          лл
01574 //лл                                                                        лл
01575 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01576 
01577 DXDEF void WINAPI SAL_lock_surface           (S32        surface,//)
01578                                               U8       **ptr,
01579                                               S32       *pitch)
01580 {
01581         if (DIB_active)
01582       {
01583       DIB_lock_surface(surface, ptr, pitch);
01584       }
01585 }
01586 
01587 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01588 //лл                                                                        лл
01589 //лл Release surface memory pointer, optionally performing page flip        лл
01590 //лл                                                                        лл
01591 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01592 
01593 DXDEF void WINAPI SAL_release_surface        (S32        surface,//)
01594                                               S32        perform_flip)
01595 {
01596         if (DIB_active)
01597       {
01598       DIB_release_surface(surface, perform_flip);
01599       }
01600 }
01601 
01602 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01603 //лл                                                                        лл
01604 //лл Request a pointer to a region of surface memory                        лл
01605 //лл                                                                        лл
01606 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01607 
01608 DXDEF void WINAPI SAL_lock_region            (S32        surface,//)
01609                                               SAL_REGION region,
01610                                               U8       **ptr,
01611                                               S32       *pitch)
01612 {
01613 
01614 }
01615 
01616 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01617 //лл                                                                        лл
01618 //лл Release surface region pointer                                         лл
01619 //лл                                                                        лл
01620 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01621 
01622 DXDEF void WINAPI SAL_release_region         (S32        surface,//)
01623                                               SAL_REGION region)
01624 {
01625 }
01626 
01627 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01628 //лл                                                                        лл
01629 //лл Show system mouse cursor                                               лл
01630 //лл                                                                        лл
01631 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01632 
01633 DXDEF void WINAPI SAL_show_system_mouse      (void)
01634 {
01635    ++show_count;
01636 
01637    if (show_count == 1)
01638       {
01639       //
01640       // Show count has become 1, so show mouse cursor if it's currently 
01641       // hidden
01642       //
01643 
01644       if (!cursor_state)
01645          {
01646          cursor_state = 1;
01647                         #ifndef DISABLED_BEHAVIOR
01648                                 ShowCursor(1);
01649                         #endif
01650          }
01651       }
01652 }
01653 
01654 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01655 //лл                                                                        лл
01656 //лл Hide system mouse cursor                                               лл
01657 //лл                                                                        лл
01658 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01659 
01660 DXDEF void WINAPI SAL_hide_system_mouse      (void)
01661 {
01662    --show_count;
01663 
01664    if (show_count == 0)
01665       {
01666       //
01667       // Show count has become 0, so hide mouse cursor if it's currently
01668       // visible
01669       //
01670 
01671       if (cursor_state)
01672          {
01673          cursor_state = 0;
01674                         #ifndef DISABLED_BEHAVIOR
01675                                 ShowCursor(0);
01676                         #endif
01677          }
01678       }
01679 }
01680 
01681 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01682 //лл                                                                        лл
01683 //лл Constrain mouse to limits of client area window in DIB mode            лл
01684 //лл                                                                        лл
01685 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01686 
01687 DXDEF void WINAPI SAL_constrain_mouse(void)
01688 {
01689    constrain_state = 1;
01690 
01691    if (DIB_active)
01692       {
01693       constrain_request = 1;
01694       }
01695 }
01696 
01697 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01698 //лл                                                                        лл
01699 //лл Unconstrain mouse                                                      лл
01700 //лл                                                                        лл
01701 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01702 
01703 DXDEF void WINAPI SAL_unconstrain_mouse(void)
01704 {
01705    constrain_state = 0;
01706 
01707    if (DIB_active)
01708       {
01709       #ifndef DISABLED_BEHAVIOR
01710                 ClipCursor(&unconstrained_rect);
01711           #endif
01712       constrain_request = 0;
01713       }
01714 }
01715 
01716 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01717 //лл                                                                        лл
01718 //лл Get pixel format in current mode                                       лл
01719 //лл                                                                        лл
01720 //лллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл
01721 
01722 DXDEF void WINAPI SAL_get_pixel_format(S32 *pixel_pitch, //)
01723                                        S32 *bytes_per_pixel,
01724                                        S32 *R_shift,
01725                                        U32 *R_mask,
01726                                        S32 *R_width,
01727                                        S32 *G_shift,
01728                                        U32 *G_mask,
01729                                        S32 *G_width,
01730                                        S32 *B_shift,
01731                                        U32 *B_mask,
01732                                        S32 *B_width)
01733 
01734 {
01735    S32 red_shift;
01736    U32 red_mask;
01737    S32 red_width;
01738    S32 grn_shift;
01739    U32 grn_mask;
01740    S32 grn_width;
01741    S32 blu_shift;
01742    U32 blu_mask;
01743    S32 blu_width;
01744    S32 i;
01745 
01746    //
01747    // Handle palettized (8 BPP) modes
01748    //
01749    // Write 8-bit pixel pitch and visible bytes/pixel fields
01750    // If we're in 8BPP mode, exit without altering RGB fields
01751    //
01752 
01753    if (current_bpp == 8)
01754       {
01755       if (pixel_pitch     != NULL) *pixel_pitch     = 1;
01756       if (bytes_per_pixel != NULL) *bytes_per_pixel = 1;
01757 
01758       return;
01759       }
01760 
01761    //
01762    // Handle hi-color (16+ BPP) modes
01763    //
01764    // If using DirectDraw, do a GetPixelFormat() call
01765    // 
01766    // If using CreateDIBSection(), return mask values used to create DIB
01767    // 
01768 
01769    if (DIB_active)
01770       {
01771       if (pixel_pitch     != NULL) *pixel_pitch     = (current_bpp / 8);
01772       if (bytes_per_pixel != NULL) *bytes_per_pixel = (current_bpp / 8);
01773 
01774       red_mask = DIB_R_bitmask;
01775       grn_mask = DIB_G_bitmask;
01776       blu_mask = DIB_B_bitmask;
01777       }
01778 
01779    //
01780    // Derive shift, width values from masks
01781    //
01782 
01783    for (i=31; i >= 0; i--)
01784       {
01785       if (red_mask & (1 << i))
01786          {
01787          red_shift = i;
01788          }
01789 
01790       if (grn_mask & (1 << i))
01791          {
01792          grn_shift = i;
01793          }
01794 
01795       if (blu_mask & (1 << i))
01796          {
01797          blu_shift = i;
01798          }
01799       }
01800 
01801    for (i=0; i <= 31; i++)
01802       {
01803       if (red_mask & (1 << i))
01804          {
01805          red_width = i - red_shift + 1;
01806          }
01807 
01808       if (grn_mask & (1 << i))
01809          {
01810          grn_width = i - grn_shift + 1;
01811          }
01812 
01813       if (blu_mask & (1 << i))
01814          {
01815          blu_width = i - blu_shift + 1;
01816          }
01817       }
01818    //
01819    // Pass all requested values back to the caller
01820    //
01821 
01822    if (R_shift != NULL) *R_shift = red_shift;
01823    if (G_shift != NULL) *G_shift = grn_shift;
01824    if (B_shift != NULL) *B_shift = blu_shift;
01825 
01826    if (R_mask  != NULL) *R_mask  = red_mask;
01827    if (G_mask  != NULL) *G_mask  = grn_mask;
01828    if (B_mask  != NULL) *B_mask  = blu_mask;
01829 
01830    if (R_width != NULL) *R_width = red_width;
01831    if (G_width != NULL) *G_width = grn_width;
01832    if (B_width != NULL) *B_width = blu_width;
01833 }
01834 
01835 DXDEF HBITMAP WINAPI GetDIBHandle(void)
01836 {
01837    return hDIB;
01838 }
01839 
01840 DXDEF void WINAPI SAL_GetBackBufferDC(HDC *dc)
01841 {
01842         if (DIB_active) 
01843         {
01844                 *dc = CreateCompatibleDC ( 0 );
01845                 DefaultBitmap = SelectBitmap ( *dc, hDIB );
01846         }
01847 }
01848 
01849 DXDEF void WINAPI SAL_ReleaseBackBufferDC(HDC dc)
01850 {
01851 
01852         if (DIB_active) 
01853         {
01854                 SelectBitmap ( dc, DefaultBitmap );
01855                 DeleteDC ( dc );
01856                 DeleteObject ( DefaultBitmap );
01857     }
01858 }

Generated on Tue Sep 30 12:36:38 2003 for GTestAndEngine by doxygen 1.3.2