home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------*\
- | qa.c - A template for a Windows application |
- | |
- | History: |
- | 01/01/88 toddla Created |
- | |
- \*----------------------------------------------------------------------------*/
-
- #include <windows.h>
- #include <windowsx.h>
- #include <commdlg.h>
- #include <mmsystem.h> // for timeGetTime()
- #include <stdlib.h> // for rand()
- #include "qa.h"
- #include "dib.h"
-
- #ifdef WIN32
- // unless you have Win32s 1.1 timeGetTime() does not work
- // #define timeGetTime() GetTickCount()
- #endif
-
- /*----------------------------------------------------------------------------*\
- | |
- | g l o b a l v a r i a b l e s |
- | |
- \*----------------------------------------------------------------------------*/
- static char szAppName[]="Blt App";
- static char szAppFilter[]="Bitmaps\0*.bmp;*.dib\0All Files\0*.*\0";
-
- static HINSTANCE hInstApp;
- static HWND hwndApp;
- static HACCEL hAccelApp;
- static HPALETTE hpalApp;
- static HPALETTE hpalBack;
- static BOOL fAppActive;
- static BOOL gfBackPal;
- static BOOL gfClipping;
- static BOOL gfStretch;
- static BOOL gfMirror;
-
- #ifdef WIN32
- #define _export
- #else
- #define SetPixelV SetPixel
- #endif
-
- struct {
- BITMAPINFOHEADER bi;
- RGBQUAD argb[256];
- } biApp;
-
- static LPBITMAPINFOHEADER lpbiApp;
- static HBITMAP hbmApp;
- static HBITMAP hbmDS; // from CreateDIBSection
- static HDC hdcApp;
- static HDC hdcDS;
-
- static BITMAP bm;
- static DWORD SizeImage;
- static DWORD OffsetScan0;
- static LPBYTE lpBitmapBits;
- static LPVOID lpDibBits;
- static LPBYTE BitmapTranslate;
-
- typedef void (*PDraw)(HDC hdc, int x, int y, int dx, int dy);
-
- void DrawRGB(HDC hdc, int x, int y, int dx, int dy);
- void DrawPAL(HDC hdc, int x, int y, int dx, int dy);
- void DrawIDX(HDC hdc, int x, int y, int dx, int dy);
-
- void DrawDRGB(HDC hdc, int x, int y, int dx, int dy);
- void DrawDPAL(HDC hdc, int x, int y, int dx, int dy);
- void DrawDIDX(HDC hdc, int x, int y, int dx, int dy);
-
- void DrawBLTDS(HDC hdc, int x, int y, int dx, int dy);
- void DrawBLT(HDC hdc, int x, int y, int dx, int dy);
- void DrawSBDS(HDC hdc, int x, int y, int dx, int dy);
- void DrawSB(HDC hdc, int x, int y, int dx, int dy);
- void DrawDEV(HDC hdc, int x, int y, int dx, int dy);
- void DrawSETBLT(HDC hdc, int x, int y, int dx, int dy);
- void DrawSETDIB(HDC hdc, int x, int y, int dx, int dy);
- void DrawXLATBLT(HDC hdc, int x, int y, int dx, int dy);
- void DrawCOPYBLT(HDC hdc, int x, int y, int dx, int dy);
-
- BOOL IsWin32s()
- {
- return LOBYTE(GetVersion()) == 3 && (GetVersion() & 0x80000000l);
- }
-
- //
- // are we able to use DIB_PAL_INDICES
- //
- BOOL InitIDX() {return !IsWin32s();}
-
- //
- // stuff to test CreateDIBSection
- //
- static HBITMAP (WINAPI *XCreateDIBSection)(HDC, LPBITMAPINFO, UINT, LPVOID FAR *, HANDLE, DWORD);
- #define CreateDIBSection XCreateDIBSection
-
- #ifdef WIN32
- #define GDIMOD "GDI32"
- #else
- #define GDIMOD "GDI"
- #endif
-
- static BOOL InitDS()
- {
- (FARPROC&)XCreateDIBSection = GetProcAddress(GetModuleHandle(GDIMOD), "CreateDIBSection");
- return XCreateDIBSection != NULL;
- }
-
- static UINT RandomSeed;
-
- struct {
- PDraw Draw;
- BOOL (*Init)(void);
- char * szName;
- DWORD time;
- int count;
- } aDraw[] = {
- //
- // BitBlt and StretchBlt for comparing.
- //
- DrawBLT,NULL, "BitBlt ",0,0,
- DrawSB, NULL, "StretchBlt ",0,0,
-
- //
- // CreateDIBSection.
- //
- DrawBLTDS, InitDS, "BitBlt (DIBSection) ",0,0,
- DrawSBDS, InitDS, "StretchBlt (DIBSection) ",0,0,
-
- //
- // SetDIBitsToDevice
- //
- DrawRGB,NULL, "DibToDevice (RGB) ",0,0,
- DrawPAL,NULL, "DibToDevice (PAL) ",0,0,
- #ifdef WIN32
- DrawIDX,InitIDX, "DibToDevice (IDX) ",0,0,
- #endif
- //
- // StretchDIBits, should be the same
- //
- DrawDRGB,NULL, "StretchDIBits (RGB) ",0,0,
- DrawDPAL,NULL, "StretchDIBits (PAL) ",0,0,
- #ifdef WIN32
- DrawDIDX,InitIDX,"StretchDIBits (IDX) ",0,0,
- #endif
-
- //
- // versions of set+BitBlt
- //
- #ifndef WIN32
- // uses .ASM code
- DrawXLATBLT,NULL,"XlatBits + BitBlt",0,0,
- DrawCOPYBLT,NULL,"CopyBits + BitBlt",0,0,
- #endif
- DrawSETBLT,NULL, "SetBitmapBits + BitBlt ",0,0,
- DrawSETDIB,NULL, "SetDIBits + BitBlt ",0,0,
- };
-
- #define NUM_DRAW (sizeof(aDraw) / sizeof(aDraw[0]))
-
- /*----------------------------------------------------------------------------*\
- \*----------------------------------------------------------------------------*/
-
- static int iDraw;
- static PDraw Draw;
-
- void SetDraw(int i)
- {
- char ach[128];
-
- if (aDraw[i].Draw == NULL)
- return;
-
- iDraw = i;
- Draw = aDraw[i].Draw;
- wsprintf(ach, "%s - %s", (LPSTR)szAppName, (LPSTR)aDraw[i].szName);
- SetWindowText(hwndApp, ach);
- }
-
- /*----------------------------------------------------------------------------*\
- | |
- | f u n c t i o n d e f i n i t i o n s |
- | |
- \*----------------------------------------------------------------------------*/
-
- LONG FAR PASCAL _export AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
- int ErrMsg (LPSTR sz,...);
- LONG AppCommand (HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
-
- void AppExit(void);
- BOOL AppIdle(void);
- void AppOpenFile(HWND hwnd, LPSTR szFileName);
-
- HPALETTE Create666Palette(void);
-
- /*----------------------------------------------------------------------------*\
- | AppAbout( hDlg, uiMessage, wParam, lParam ) |
- | |
- | Description: |
- | This function handles messages belonging to the "About" dialog box. |
- | The only message that it looks for is WM_COMMAND, indicating the use |
- | has pressed the "OK" button. When this happens, it takes down |
- | the dialog box. |
- | |
- | Arguments: |
- | hDlg window handle of about dialog window |
- | uiMessage message number |
- | wParam message-dependent |
- | lParam message-dependent |
- | |
- | Returns: |
- | TRUE if message has been processed, else FALSE |
- | |
- \*----------------------------------------------------------------------------*/
- BOOL FAR PASCAL _export AppAbout(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
- {
- switch (msg)
- {
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK)
- {
- EndDialog(hwnd,TRUE);
- }
- break;
-
- case WM_INITDIALOG:
- return TRUE;
- }
- return FALSE;
- }
-
- /*----------------------------------------------------------------------------*\
- | AppInit( hInst, hPrev) |
- | |
- | Description: |
- | This is called when the application is first loaded into |
- | memory. It performs all initialization that doesn't need to be done |
- | once per instance. |
- | |
- | Arguments: |
- | hInstance instance handle of current instance |
- | hPrev instance handle of previous instance |
- | |
- | Returns: |
- | TRUE if successful, FALSE if not |
- | |
- \*----------------------------------------------------------------------------*/
- BOOL AppInit(HINSTANCE hInst,HINSTANCE hPrev,int sw,LPSTR szCmdLine)
- {
- WNDCLASS cls;
- int dx,dy;
- int i;
-
- /* Save instance handle for DialogBoxs */
- hInstApp = hInst;
-
- hAccelApp = LoadAccelerators(hInst, "AppAccel");
-
- if (!hPrev)
- {
- /*
- * Register a class for the main application window
- */
- cls.hCursor = LoadCursor(NULL,IDC_ARROW);
- cls.hIcon = LoadIcon(hInst,"AppIcon");
- cls.lpszMenuName = "AppMenu";
- cls.lpszClassName = szAppName;
- cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- cls.hInstance = hInst;
- cls.style = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
- cls.lpfnWndProc = (WNDPROC)AppWndProc;
- cls.cbWndExtra = 0;
- cls.cbClsExtra = 0;
-
- if (!RegisterClass(&cls))
- return FALSE;
- }
-
- dx = GetSystemMetrics (SM_CXSCREEN) / 2;
- dy = GetSystemMetrics (SM_CYSCREEN) / 2;
-
- hwndApp = CreateWindow (szAppName, // Class name
- szAppName, // Caption
- WS_OVERLAPPEDWINDOW, // Style bits
- CW_USEDEFAULT, 0, // Position
- dx,dy, // Size
- (HWND)NULL, // Parent window (no parent)
- (HMENU)NULL, // use class menu
- hInst, // handle to window instance
- (LPSTR)NULL // no params to pass on
- );
- ShowWindow(hwndApp,sw);
-
- //
- // init every thing.
- //
- for (i=0; i<NUM_DRAW; i++)
- {
- if (aDraw[i].Init && !aDraw[i].Init())
- aDraw[i].Draw = NULL;
- }
-
- if (*szCmdLine)
- AppOpenFile(hwndApp, szCmdLine);
- else
- AppOpenFile(hwndApp, "Herman");
-
- RandomSeed = (UINT)timeGetTime();
- srand(RandomSeed);
-
- hpalBack = Create666Palette();
-
- //
- // build the draw menu.
- //
- HMENU hmenu = GetSubMenu(GetMenu(hwndApp), 1);
- DeleteMenu(hmenu, MENU_DRAW, MF_BYCOMMAND);
-
- for (i=0; i<NUM_DRAW; i++)
- {
- AppendMenu(hmenu, 0, MENU_DRAW+i, aDraw[i].szName);
- }
-
- AppendMenu(hmenu, MF_SEPARATOR, -1, NULL);
- AppendMenu(hmenu, 0, MENU_BACKPAL, "Background Pal");
- AppendMenu(hmenu, 0, MENU_CLIP, "Clipping");
- AppendMenu(hmenu, 0, MENU_STRETCH, "Stretch");
- AppendMenu(hmenu, 0, MENU_MIRROR, "Mirror");
-
- // pick a default.
- SetDraw(0);
-
- return TRUE;
- }
-
-
- /*----------------------------------------------------------------------------*\
- | AppExit() |
- | |
- | Description: |
- | app is just about to exit, cleanup |
- | |
- \*----------------------------------------------------------------------------*/
- void AppExit()
- {
- DeleteObject(hpalBack);
- AppOpenFile(hwndApp, NULL);
- }
-
- /*----------------------------------------------------------------------------*\
- \*----------------------------------------------------------------------------*/
-
- #define RandRGB() RGB(rand() % 256, rand() % 256, rand() % 256)
- #define RandPT(pt, rc) ((pt).x = rc.left + (rand() % (rc.right-rc.left))), ((pt).y = rc.top + (rand() % (rc.bottom-rc.top)))
-
- RECT rcRand;
- RECT rcApp;
- RECT rcApp2;
-
- void InitDC(HDC hdc)
- {
- if (gfBackPal)
- {
- SelectPalette(hdc, hpalBack, FALSE);
- RealizePalette(hdc);
-
- SelectPalette(hdc, hpalApp, TRUE);
- RealizePalette(hdc);
- }
- else
- {
- SelectPalette(hdc, hpalApp, FALSE);
- RealizePalette(hdc);
- }
-
- SetStretchBltMode(hdc, COLORONCOLOR);
- }
-
- void DrawRandom(HDC hdc)
- {
- BOOL f;
- POINT pt;
- POINT ptW;
- int dx,dy;
-
- if (f = (hdc == NULL))
- {
- hdc = GetDC(hwndApp);
- InitDC(hdc);
- }
-
- if (gfStretch)
- {
- RandPT(pt, rcApp);
-
- if (gfClipping)
- RandPT(ptW, rcApp2);
- else
- RandPT(ptW, rcApp);
-
- dx = ptW.x - pt.x;
- dy = ptW.y - pt.y;
-
- if (!gfMirror)
- {
- if (dx < 0)
- pt.x = ptW.x, dx = -dx;
-
- if (dy < 0)
- pt.y = ptW.y, dy = -dy;
- }
- }
- else
- {
- RandPT(pt, rcRand);
-
- dx = bm.bmWidth;
- dy = bm.bmHeight;
- }
-
- Draw(hdc, pt.x, pt.y, dx, dy);
-
- if (f)
- ReleaseDC(hwndApp, hdc);
- }
-
- void CopyText(LPSTR pch)
- {
- HANDLE h = GlobalAlloc(GHND, lstrlen(pch+1));
- lstrcpy((LPSTR)GlobalLock(h), pch);
- OpenClipboard(hwndApp);
- EmptyClipboard();
- SetClipboardData(CF_TEXT, h);
- CloseClipboard();
- }
-
- /*----------------------------------------------------------------------------*\
- | TimeIt() |
- \*----------------------------------------------------------------------------*/
-
- #define N 100
-
- #define FPS(time,n) \
- time ? (1000l * n / time) : 0, \
- time ? (1000000l * n / time) % 1000: 0
-
- char achMsg[2048];
-
- void TimeIt()
- {
- HDC hdc;
- LONG time;
- int i,n;
- char *pch;
- HCURSOR hcur;
- int iDrawSave;
-
- InvalidateRect(hwndApp, NULL, TRUE);
- UpdateWindow(hwndApp);
-
- hdc = GetDC(hwndApp);
- InitDC(hdc);
-
- hcur = SetCursor(NULL);
-
- iDrawSave = iDraw;
-
- for (n=0; n<NUM_DRAW; n++)
- {
- if (aDraw[n].Draw == NULL)
- continue;
-
- SetDraw(n);
-
- srand(RandomSeed);
-
- time = timeGetTime();
-
- for (i=0; i<N; i++)
- {
- DrawRandom(hdc);
- }
-
- time = timeGetTime() - time;
-
- aDraw[n].time = time;
- aDraw[n].count = N;
- }
-
- SetDraw(iDrawSave);
- SetCursor(hcur);
- ReleaseDC(hwndApp, hdc);
-
- pch = achMsg;
-
- for (n=0; n<NUM_DRAW; n++)
- {
- if (aDraw[n].Draw == NULL)
- continue;
-
- pch += wsprintf(pch, "%-40s\t%3ld.%03ld fps\r\n", (LPSTR)aDraw[n].szName, FPS(aDraw[n].time,N));
- }
-
- CopyText(achMsg);
-
- ErrMsg(achMsg);
- }
-
- /*----------------------------------------------------------------------------*\
- | WinMain( hInst, hPrev, lpszCmdLine, cmdShow ) |
- | |
- | Description: |
- | The main procedure for the App. After initializing, it just goes |
- | into a message-processing loop until it gets a WM_QUIT message |
- | (meaning the app was closed). |
- | |
- | Arguments: |
- | hInst instance handle of this instance of the app |
- | hPrev instance handle of previous instance, NULL if first |
- | szCmdLine ->null-terminated command line |
- | cmdShow specifies how the window is initially displayed |
- | |
- | Returns: |
- | The exit code as specified in the WM_QUIT message. |
- | |
- \*----------------------------------------------------------------------------*/
- int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
- {
- MSG msg;
-
- /* Call initialization procedure */
- if (!AppInit(hInst,hPrev,sw,szCmdLine))
- return FALSE;
-
- /*
- * Polling messages from event queue
- */
- for (;;)
- {
- if (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
- {
- if (msg.message == WM_QUIT)
- break;
-
- if (hAccelApp && TranslateAccelerator(hwndApp, hAccelApp, &msg))
- continue;
-
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- if (AppIdle())
- WaitMessage();
- }
- }
-
- AppExit();
- return msg.wParam;
- }
-
- /*----------------------------------------------------------------------------*\
- | AppIdle() |
- | |
- | Description: |
- | place to do idle time stuff. |
- | |
- | Returns: |
- | RETURN TRUE IF YOU HAVE NOTHING TO DO OTHERWISE YOUR APP WILL BE A |
- | CPU PIG! |
- \*----------------------------------------------------------------------------*/
- BOOL AppIdle()
- {
- if (fAppActive)
- {
- //
- // we are the foreground app.
- //
- if (lpbiApp)
- {
- DrawRandom(NULL);
- return FALSE;
- }
-
- return TRUE; // nothing to do.
- }
- else
- {
- //
- // we are a background app.
- //
- return TRUE; // nothing to do.
- }
- }
-
- /*----------------------------------------------------------------------------*\
- | AppOpenFile() |
- | |
- | Description: |
- | open a file stupid |
- | |
- \*----------------------------------------------------------------------------*/
- void AppOpenFile(HWND hwnd, LPSTR szFileName)
- {
- HDC hdc;
- LPVOID lpBits;
-
- if (hpalApp) DeleteObject(hpalApp);
- if (hdcApp) DeleteObject(hdcApp);
- if (hbmApp) DeleteObject(hbmApp);
- if (hdcDS) DeleteObject(hdcDS);
- if (hbmDS) DeleteObject(hbmDS);
- if (lpbiApp) DibFree(lpbiApp);
-
- hpalApp = NULL;
- lpbiApp = NULL;
- hbmApp = NULL;
- hdcApp = NULL;
-
- if (szFileName == NULL)
- return;
-
- lpbiApp = DibOpenFile(szFileName);
-
- if (lpbiApp == NULL)
- {
- ErrMsg("Cant open %s", szFileName);
- return;
- }
-
- //
- // get the palette
- //
- hpalApp = DibCreatePalette(lpbiApp);
-
- //
- // make this a identity palette for fast drawing.
- //
- MakeIdentityPalette(hpalApp);
- DibMapToPalette(lpbiApp, hpalApp);
-
- //
- // convert to a bitmap
- //
- hbmApp = BitmapFromDib(lpbiApp, hpalApp, DIB_RGB_COLORS);
-
- //
- // bitmap stuff.
- //
- GetObject(hbmApp, sizeof(bm), (LPVOID)&bm);
- SizeImage = (DWORD)(UINT)bm.bmHeight * (DWORD)(UINT)bm.bmWidthBytes;
- lpBitmapBits = (LPBYTE)GlobalAllocPtr(GHND, SizeImage);
- BitmapTranslate = (LPBYTE)GlobalAllocPtr(GHND, 256);
-
- OffsetScan0 = SizeImage - bm.bmWidthBytes;
- GetBitmapBits(hbmApp, SizeImage, lpBitmapBits);
-
- lpDibBits = DibPtr(lpbiApp);
-
- //
- // we will use DIB_PAL_COLORS to draw DIBs
- //
- hmemcpy(&biApp, lpbiApp, sizeof(biApp)); //save RGB colors
- DibSetUsage(lpbiApp, hpalApp, DIB_PAL_COLORS);
-
- //
- // make memory DCs for easy access.
- //
- hdc = GetDC(NULL);
-
- //
- // make a DIBSECTION
- //
- if (XCreateDIBSection != NULL)
- {
- hbmDS = CreateDIBSection(hdc, (LPBITMAPINFO)&biApp, DIB_RGB_COLORS, &lpBits, NULL, 0);
- //// hbmDS = CreateDIBSection(hdc, (LPBITMAPINFO)&biApp, DIB_PAL_INDICIES, &lpBits, NULL, 0);
- //// hbmDS = CreateDIBSection(hdc, (LPBITMAPINFO)lpbiApp, DIB_PAL_COLORS, &lpBits, NULL, 0);
- GetObject(hbmDS, sizeof(bm), (LPVOID)&bm);
-
- hdcDS = CreateCompatibleDC(hdc);
-
- //
- // copy the bits.
- //
- SetDIBits(hdcDS, hbmDS, 0, bm.bmHeight,
- DibPtr(lpbiApp), (LPBITMAPINFO)&biApp, DIB_RGB_COLORS);
-
- SelectObject(hdcDS, hbmDS);
- }
-
- for (int i=0; i<256; i++)
- BitmapTranslate[i] = i;
-
- hdcApp = CreateCompatibleDC(hdc);
- ReleaseDC(NULL, hdc);
-
- SelectObject(hdcApp, hbmApp);
- SelectPalette(hdcApp, hpalApp, FALSE);
- RealizePalette(hdcApp);
-
- SendMessage(hwndApp, WM_SIZE, 0, 0);
- }
-
- /*----------------------------------------------------------------------------*\
- | AppPaint(hwnd, hdc) |
- | |
- | Description: |
- | The paint function. Right now this does nothing. |
- | |
- | Arguments: |
- | hwnd window painting into |
- | hdc display context to paint to |
- | |
- | Returns: |
- | nothing |
- | |
- \*----------------------------------------------------------------------------*/
- AppPaint (HWND hwnd, HDC hdc)
- {
- return TRUE;
- }
-
- /*----------------------------------------------------------------------------*\
- | AppWndProc( hwnd, uiMessage, wParam, lParam ) |
- | |
- | Description: |
- | The window proc for the app's main (tiled) window. This processes all |
- | of the parent window's messages. |
- | |
- \*----------------------------------------------------------------------------*/
- LONG FAR PASCAL _export AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hdc;
- BOOL f;
- int i;
-
- switch (msg)
- {
- case WM_CREATE:
- break;
-
- case WM_ACTIVATEAPP:
- fAppActive = (BOOL)wParam;
- break;
-
- case WM_TIMER:
- break;
-
- case WM_ERASEBKGND:
- break;
-
- case WM_INITMENU:
- for (i=0; i<NUM_DRAW; i++)
- {
- EnableMenuItem((HMENU)wParam, MENU_DRAW + i, aDraw[i].Draw ? MF_ENABLED : MF_GRAYED);
- CheckMenuItem((HMENU)wParam, MENU_DRAW + i, iDraw == i ? MF_CHECKED : MF_UNCHECKED);
- }
-
- CheckMenuItem((HMENU)wParam, MENU_BACKPAL, gfBackPal ? MF_CHECKED : MF_UNCHECKED);
- CheckMenuItem((HMENU)wParam, MENU_CLIP, gfClipping ? MF_CHECKED : MF_UNCHECKED);
- CheckMenuItem((HMENU)wParam, MENU_MIRROR, gfMirror ? MF_CHECKED : MF_UNCHECKED);
- CheckMenuItem((HMENU)wParam, MENU_STRETCH, gfStretch ? MF_CHECKED : MF_UNCHECKED);
- break;
-
- case WM_COMMAND:
- return AppCommand(hwnd,msg,wParam,lParam);
-
- case WM_DESTROY:
- hAccelApp = NULL;
- PostQuitMessage(0);
- break;
-
- case WM_SIZE:
- InvalidateRect(hwnd, NULL, TRUE);
-
- GetClientRect(hwnd, &rcApp);
- GetClientRect(hwnd, &rcApp2);
- GetClientRect(hwnd, &rcRand);
-
- if (lpbiApp == NULL)
- return 0L;
-
- if (!gfClipping)
- {
- if ((int)DibWidth(lpbiApp) < rcRand.right)
- rcRand.right -= DibWidth(lpbiApp);
-
- if ((int)DibHeight(lpbiApp) < rcRand.bottom)
- rcRand.bottom -= DibHeight(lpbiApp);
- }
- else
- {
- rcRand.left -= DibWidth(lpbiApp);
- rcRand.right += DibWidth(lpbiApp);
- rcRand.top -= DibHeight(lpbiApp);
- rcRand.bottom += DibHeight(lpbiApp);
-
- rcApp2 = rcRand;
- }
- break;
-
- case WM_CLOSE:
- break;
-
- case WM_PALETTECHANGED:
- if ((HWND)wParam == hwnd)
- break;
-
- // fall through to WM_QUERYNEWPALETTE
-
- case WM_QUERYNEWPALETTE:
- hdc = GetDC(hwnd);
-
- if (hpalApp)
- SelectPalette(hdc, hpalApp, FALSE);
-
- f = RealizePalette(hdc);
- ReleaseDC(hwnd,hdc);
-
- if (f)
- InvalidateRect(hwnd,NULL,TRUE);
-
- return f;
-
- case WM_PAINT:
- hdc = BeginPaint(hwnd,&ps);
- AppPaint (hwnd,hdc);
- EndPaint(hwnd,&ps);
- return 0L;
- }
- return DefWindowProc(hwnd,msg,wParam,lParam);
- }
-
- /*----------------------------------------------------------------------------*\
- | AppCommand(hwnd, msg, wParam, lParam ) |
- | |
- | Description: |
- | handles WM_COMMAND messages for the main window (hwndApp) |
- | of the parent window's messages. |
- | |
- \*----------------------------------------------------------------------------*/
- LONG AppCommand (HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
- {
- char achFileName[128];
- OPENFILENAME ofn;
- int i;
-
- switch(LOWORD(wParam))
- {
- case MENU_ABOUT:
- DialogBox(hInstApp,"AppAbout",hwnd,AppAbout);
- break;
-
- case MENU_OPEN:
- achFileName[0] = 0;
-
- /* prompt user for file to open */
- ofn.lStructSize = sizeof(OPENFILENAME);
- ofn.hwndOwner = hwnd;
- ofn.hInstance = NULL;
- ofn.lpstrFilter = szAppFilter;
- ofn.lpstrCustomFilter = NULL;
- ofn.nMaxCustFilter = 0;
- ofn.nFilterIndex = 0;
- ofn.lpstrFile = achFileName;
- ofn.nMaxFile = sizeof(achFileName);
- ofn.lpstrFileTitle = NULL;
- ofn.nMaxFileTitle = 0;
- ofn.lpstrInitialDir = NULL;
- ofn.lpstrTitle = NULL;
- ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
- ofn.nFileOffset = 0;
- ofn.nFileExtension = 0;
- ofn.lpstrDefExt = NULL;
- ofn.lCustData = 0;
- ofn.lpfnHook = NULL;
- ofn.lpTemplateName = NULL;
-
- if (GetOpenFileName(&ofn))
- {
- AppOpenFile(hwnd,achFileName);
- }
-
- break;
-
- case MENU_EXIT:
- PostMessage(hwnd,WM_CLOSE,0,0L);
- break;
-
- case MENU_TIME:
- TimeIt();
- break;
-
- case MENU_BACKPAL:
- gfBackPal = !gfBackPal;
- PostMessage(hwnd, WM_SIZE, 0, 0);
- break;
-
- case MENU_CLIP:
- gfClipping = !gfClipping;
- PostMessage(hwnd, WM_SIZE, 0, 0);
- break;
-
- case MENU_MIRROR:
- gfMirror = !gfMirror;
- PostMessage(hwnd, WM_SIZE, 0, 0);
- break;
-
- case MENU_STRETCH:
- gfStretch = !gfStretch;
- PostMessage(hwnd, WM_SIZE, 0, 0);
- break;
-
- default:
- i = (int)LOWORD(wParam) - MENU_DRAW;
-
- if (i >= 0 && i < NUM_DRAW && aDraw[i].Draw)
- SetDraw(i);
-
- InvalidateRect(hwnd, NULL, TRUE);
- break;
- }
- return 0L;
- }
-
- /*----------------------------------------------------------------------------*\
- | ErrMsg - Opens a Message box with a error message in it. The user can |
- | select the OK button to continue |
- \*----------------------------------------------------------------------------*/
- int ErrMsg (LPSTR sz,...)
- {
- static char ach[4096];
-
- wvsprintf (ach,sz,(LPSTR)(&sz+1)); /* Format the string */
- MessageBox(hwndApp,ach,szAppName,MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL);
- return FALSE;
- }
-
- typedef void (FAR PASCAL CONVERTPROC)(
- LPVOID pd, // --> dst.
- LONG dd, // offset to start at
- LONG nd, // dst_next_scan.
- LPVOID ps, // --> source.
- LONG ds, // offset to start at
- LONG ns, // src_next_scan.
- LONG dx, // pixel count.
- LONG dy, // scan count.
- LPVOID pc); // pixel convert table.
-
- extern "C" CONVERTPROC copy_8_8, convert_8_8;
-
- void DrawDRGB(HDC hdc, int x, int y, int dx, int dy)
- {
- StretchDIBits(
- hdc,
- x,y,dx,dy,
- 0,0,
- DibWidth(lpbiApp),
- DibHeight(lpbiApp),
- DibPtr(lpbiApp), (LPBITMAPINFO)&biApp,
- DIB_RGB_COLORS,SRCCOPY);
- }
-
- void DrawDPAL(HDC hdc, int x, int y, int dx, int dy)
- {
- StretchDIBits(
- hdc,
- x,y,dx,dy,
- 0,0,
- DibWidth(lpbiApp),
- DibHeight(lpbiApp),
- DibPtr(lpbiApp), (LPBITMAPINFO)lpbiApp,
- DIB_PAL_COLORS,SRCCOPY);
- }
-
- #ifdef WIN32
- void DrawDIDX(HDC hdc, int x, int y, int dx, int dy)
- {
- StretchDIBits(
- hdc,
- x,y,dx,dy,
- 0,0,
- DibWidth(lpbiApp),
- DibHeight(lpbiApp),
- DibPtr(lpbiApp), (LPBITMAPINFO)&biApp,
- DIB_PAL_INDICES,SRCCOPY);
- }
- #endif
-
- void DrawRGB(HDC hdc, int x, int y, int dx, int dy)
- {
- SetDIBitsToDevice(hdc, x, y, dx, dy,
- 0, 0, 0, DibHeight(lpbiApp), DibPtr(lpbiApp),
- (LPBITMAPINFO)&biApp, DIB_RGB_COLORS);
- }
-
- void DrawPAL(HDC hdc, int x, int y, int dx, int dy)
- {
- SetDIBitsToDevice(hdc, x, y, dx, dy,
- 0, 0, 0, DibHeight(lpbiApp), DibPtr(lpbiApp),
- (LPBITMAPINFO)lpbiApp, DIB_PAL_COLORS);
- }
-
- #ifdef WIN32
- void DrawIDX(HDC hdc, int x, int y, int dx, int dy)
- {
- SetDIBitsToDevice(hdc, x, y, dx, dy,
- 0, 0, 0, DibHeight(lpbiApp), DibPtr(lpbiApp),
- (LPBITMAPINFO)&biApp, DIB_PAL_INDICES);
- }
- #endif
-
- void DrawBLT(HDC hdc, int x, int y, int dx, int dy)
- {
- // Flush cached bitmaps
- SetPixelV(hdcApp, 0, 0, 0L);
- BitBlt(hdc, x, y, dx, dy, hdcApp, 0, 0, SRCCOPY);
- }
-
- void DrawSB(HDC hdc, int x, int y, int dx, int dy)
- {
- // Flush cached bitmaps
- SetPixelV(hdcApp, 0, 0, 0L);
- StretchBlt(hdc, x, y, dx, dy,
- hdcApp, 0, 0,
- bm.bmWidth, bm.bmHeight,
- SRCCOPY);
- }
-
-
- void DrawBLTDS(HDC hdc, int x, int y, int dx, int dy)
- {
- BitBlt(hdc, x, y, dx, dy,hdcDS, 0, 0, SRCCOPY);
- }
-
- void DrawSBDS(HDC hdc, int x, int y, int dx, int dy)
- {
- StretchBlt(hdc, x, y, dx, dy,
- hdcDS, 0, 0,bm.bmWidth, bm.bmHeight,SRCCOPY);
- }
-
- void DrawSETBLT(HDC hdc, int x, int y, int dx, int dy)
- {
- SetBitmapBits(hbmApp, SizeImage, lpBitmapBits);
-
- StretchBlt(hdc, x, y, dx, dy,
- hdcApp, 0, 0,
- bm.bmWidth, bm.bmHeight,
- SRCCOPY);
- }
-
- void DrawSETDIB(HDC hdc, int x, int y, int dx, int dy)
- {
- SetDIBits(hdc, hbmApp, 0, DibHeight(lpbiApp),
- DibPtr(lpbiApp), (LPBITMAPINFO)lpbiApp, DIB_PAL_COLORS);
-
- StretchBlt(hdc, x, y, dx, dy,
- hdcApp, 0, 0,
- bm.bmWidth, bm.bmHeight,
- SRCCOPY);
- }
-
- #ifndef WIN32
- void DrawCOPYBLT(HDC hdc, int x, int y, int dx, int dy)
- {
- copy_8_8(lpBitmapBits, 0, bm.bmWidthBytes,
- lpBitmapBits, OffsetScan0, -(int)bm.bmWidthBytes,
- bm.bmWidth, bm.bmHeight, BitmapTranslate);
-
- SetBitmapBits(hbmApp, SizeImage, lpBitmapBits);
-
- StretchBlt(hdc, x, y, dx, dy,
- hdcApp, 0, 0,
- bm.bmWidth, bm.bmHeight,
- SRCCOPY);
- }
-
- void DrawXLATBLT(HDC hdc, int x, int y, int dx, int dy)
- {
- convert_8_8(lpBitmapBits, 0, bm.bmWidthBytes,
- lpBitmapBits, OffsetScan0, -(int)bm.bmWidthBytes,
- bm.bmWidth, bm.bmHeight, BitmapTranslate);
-
- SetBitmapBits(hbmApp, SizeImage, lpBitmapBits);
-
- StretchBlt(hdc, x, y, dx, dy,
- hdcApp, 0, 0,
- bm.bmWidth, bm.bmHeight,
- SRCCOPY);
- }
- #endif
-
- //
- // create a 6*6*6 rainbow palette
- //
- HPALETTE Create666Palette()
- {
- LOGPALETTE *pPal;
- PALETTEENTRY *ppe;
- HPALETTE hpal = NULL;
- WORD nNumColors;
- BYTE r,g,b;
-
- nNumColors = 6*6*6;
- pPal = (LOGPALETTE*)LocalAlloc(LPTR,sizeof(LOGPALETTE) + nNumColors * sizeof(PALETTEENTRY));
-
- if (!pPal)
- goto exit;
-
- pPal->palNumEntries = nNumColors;
- pPal->palVersion = 0x300;
-
- ppe = pPal->palPalEntry;
-
- for (r=0; r<6; r++)
- for (g=0; g<6; g++)
- for (b=0; b<6; b++)
- {
- ppe->peRed = (BYTE)((UINT)r * 255 / 6);
- ppe->peGreen = (BYTE)((UINT)g * 255 / 6);
- ppe->peBlue = (BYTE)((UINT)b * 255 / 6);
- ppe->peFlags = (BYTE)0;
- ppe++;
- }
-
- hpal = CreatePalette(pPal);
- LocalFree((HANDLE)pPal);
-
- exit:
- return hpal;
- }
-
- /*****************************************************************************
- *
- * dprintf() is called by the DPF macro if DEBUG is defined at compile time.
- *
- * The messages will be send to COM1: like any debug message. To
- * enable debug output, add the following to WIN.INI :
- *
- * [debug]
- * QA=1
- *
- ****************************************************************************/
-
- #ifdef DEBUG
-
- #define MODNAME "QA"
-
- #ifndef WIN32
- #define lstrcatA lstrcat
- #define lstrcpyA lstrcpy
- #define lstrlenA lstrlen
- #define wvsprintfA wvsprintf
- #define GetProfileIntA GetProfileInt
- #define OutputDebugStringA OutputDebugString
- #endif
-
- #define _WINDLL
- #include <stdarg.h>
-
- void FAR CDECL dprintf(LPSTR szFormat, ...)
- {
- char ach[128];
- va_list va;
-
- static BOOL fDebug = -1;
-
- if (fDebug == -1)
- fDebug = GetProfileIntA("Debug", MODNAME, TRUE);
-
- if (!fDebug)
- return;
-
- lstrcpyA(ach, MODNAME ": ");
- va_start(va, szFormat);
- wvsprintfA(ach+lstrlenA(ach),szFormat,(LPSTR)va);
- va_end(va);
- lstrcatA(ach, "\r\n");
-
- OutputDebugStringA(ach);
- }
-
- #endif
-