shootgal.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 1,095 行 · 第 1/3 页

C
1,095
字号
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shootgal.h"

static char ShootGalClass[32]="ShootGalClass";
HWND        MessageWnd = NULL;
HWND        ScoreWnd = NULL;
BOOL        MessagesOn = FALSE;

int PASCAL WinMain( HINSTANCE, HINSTANCE, LPSTR, int );
static BOOL FirstInstance( HINSTANCE );
static BOOL AnyInstance( HINSTANCE, int );
BOOL _EXPORT FAR PASCAL About( HWND, unsigned, WORD, LONG );
BOOL _EXPORT FAR PASCAL SpeedDlgProc( HWND, unsigned, WORD, LONG );
long _EXPORT FAR PASCAL WindowProc( HWND, unsigned, WORD, LONG );
BOOL TurnMessageWindowOn( HWND );
BOOL TurnScoreWindowOn( HWND );
BOOL _EXPORT FAR PASCAL MessageWindowProc( HWND, unsigned, WORD, LONG );
BOOL _EXPORT FAR PASCAL ScoreProc( HWND, unsigned, WORD, LONG );
static void CheckHit( HDC, POINT );
static void DrawBitmap( HDC, HBITMAP, short, short );
POINT RandPoint( RECT, POINT );
void _EXPORT FAR PASCAL DrawBolt( short, short, LPSTR );
static void ShootBolt( HWND );
static void BoomSound();
static void BoltSound();

/*
 * WinMain - initialization, message loop
 */
int PASCAL WinMain( HINSTANCE this_inst, HINSTANCE prev_inst, LPSTR cmdline,
                    int cmdshow )
/*******************************/
{
    MSG         msg;

    cmdline = cmdline;          /* shut up compiler warning */
    prev_inst = prev_inst;

#ifdef __WINDOWS_386__
    sprintf( ShootGalClass,"ShootGalClass%d", this_inst );
#else
    if( !prev_inst )
#endif
        if( !FirstInstance( this_inst ) ) return( FALSE );

    if( !AnyInstance( this_inst, cmdshow ) ) return( FALSE );

    while( GetMessage( &msg, NULL, NULL, NULL ) ) {
        /*
         * check to see if any of the messages are for a modeless dialog box,
         */
        if( ( MessageWnd == NULL || !IsDialogMessage( MessageWnd, &msg ) ) &&
            ( ScoreWnd == NULL || !IsDialogMessage( ScoreWnd, &msg ) ) ) {

            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }

    }

    return( msg.wParam );

} /* WinMain */

/*
 * FirstInstance - register window class for the application,
 *                 and do any other application initialization
 */
static BOOL FirstInstance( HINSTANCE this_inst )
/*******************************************/
{
    WNDCLASS    wc;
    BOOL        rc;

    /*
     * set up and register window classes
     */
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (LPVOID) WindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = sizeof( DWORD );
    wc.hInstance = this_inst;
    wc.hIcon = LoadIcon( this_inst, "ShootGalIcon" );
    wc.hCursor = LoadCursor( this_inst, "guncursor" );
    wc.hbrBackground = GetStockObject( WHITE_BRUSH );
    wc.lpszMenuName = "ShootGalMenu";
    wc.lpszClassName = ShootGalClass;
    rc = RegisterClass( &wc );

    return( rc );

} /* FirstInstance */

/*
 * AnyInstance - do work required for every instance of the application:
 *                create the window, initialize data
 */
static BOOL AnyInstance( HINSTANCE this_inst, int cmdshow )
/******************************************************/
{
    HWND        window_handle;
    BITMAP      bitmapbuff;
    extra_data  *edata_ptr;

    /*
     * create main window
     */
    window_handle = CreateWindow(
        ShootGalClass,           /* class */
        "Open WATCOM Shooting Gallery - Sample Application",   /* caption */
        WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,    /* style */
        CW_USEDEFAULT,          /* init. x pos */
        CW_USEDEFAULT,          /* init. y pos */
        CW_USEDEFAULT,          /* init. x size */
        CW_USEDEFAULT,          /* init. y size */
        NULL,                   /* parent window */
        NULL,                   /* menu handle */
        this_inst,              /* program handle */
        NULL                    /* create parms */
        );

    if( !window_handle ) return( FALSE );

    /*
     * get a timer
     */
    while( !SetTimer( window_handle, TARGET_TIMER, STD_TARGET_SPEED,
                                                 ( FARPROC ) NULL ) ) {
        if( MessageBox( window_handle, "Too many timers in use!", NULL,
                        MB_ICONEXCLAMATION | MB_RETRYCANCEL ) == IDCANCEL ) {
            return( FALSE );
        }
    }

    /*
     * initialize data
     */
    edata_ptr = calloc( 1, sizeof( extra_data ) );
    if( edata_ptr == NULL ) return( FALSE );

    edata_ptr->target_bmp = LoadBitmap( this_inst, "target" );
    GetObject( edata_ptr->target_bmp, sizeof( BITMAP ), (LPSTR) &bitmapbuff);

    edata_ptr->sound_on = FALSE;
    edata_ptr->score_on = FALSE;
    edata_ptr->target.x = 0;
    edata_ptr->target.y = 0;
    edata_ptr->size.x = bitmapbuff.bmWidth;
    edata_ptr->size.y = bitmapbuff.bmHeight;
    edata_ptr->aim = edata_ptr->target;
    edata_ptr->bolt = edata_ptr->aim;
    edata_ptr->target_speed = STD_TARGET_SPEED;
    edata_ptr->bolt_speed = STD_BOLT_SPEED;
    GetClientRect( window_handle, &edata_ptr->client_rect );
    edata_ptr->message_window_proc =
             MakeProcInstance( (FARPROC)MessageWindowProc, this_inst );
    edata_ptr->score_window_proc =
             MakeProcInstance( (FARPROC)ScoreProc, this_inst );
    edata_ptr->bolt_icon = LoadIcon( this_inst, "Bolt" );

    /*
     * put a pointer to the above structure in the main window structure
     */
    SetWindowLong( window_handle, EXTRA_DATA_OFFSET, (DWORD) edata_ptr );

    /*
     * display window
     */
    ShowWindow( window_handle, cmdshow );
    UpdateWindow( window_handle );

    return( TRUE );

} /* AnyInstance */

/*
 * About -  processes messages for the about dialogue.
 */
BOOL _EXPORT FAR PASCAL About( HWND window_handle, unsigned msg,
                                WORD wparam, LONG lparam )
/********************************************************/
{
    lparam = lparam;                    /* turn off warning */
    window_handle = window_handle;

    switch( msg ) {
    case WM_INITDIALOG:
        return( TRUE );

    case WM_COMMAND:
        if( wparam == IDOK ) {
            EndDialog( window_handle, TRUE );
            return( TRUE );
        }
        break;
    }
    return( FALSE );

} /* About */

/*
 * SpeedDlgProc - processes messages for the speed dialogue box.
 *
 * Allows the user to select speeds for the target or lightning bolt,
 * depending on which selection was chosen.
 */
BOOL _EXPORT FAR PASCAL SpeedDlgProc( HWND dialog_wnd, unsigned msg,
                                WORD wparam, LONG lparam )
/********************************************************/
{
    extra_data              *edata_ptr;
    short                   speed;
    static short            old_speed = 0; /* in case the user hits CANCEL */
    static short            what_are_we_setting = 0;
    static short            fastest_speed = 0;
    static short            slowest_speed = 0;
    HWND                    scrollbar;

    edata_ptr = (extra_data *) GetWindowLong(
                 GetWindow( dialog_wnd, GW_OWNER ), EXTRA_DATA_OFFSET );

    switch( msg ) {
    case WM_INITDIALOG:
        what_are_we_setting = (short)lparam;
        if( what_are_we_setting == SET_TARGET_SPEED ) {
            SetDlgItemText( dialog_wnd, SET_WHAT, (LPSTR)"Set Target Speed" );
            old_speed = edata_ptr->target_speed;
            fastest_speed = FASTEST_TARGET_SPEED;
            slowest_speed = SLOWEST_TARGET_SPEED;
        } else {
            SetDlgItemText( dialog_wnd, SET_WHAT, (LPSTR)"Set Lightning Bolt Speed" );
            SetDlgItemText( dialog_wnd, TEST, (LPSTR)"Click RIGHT mouse button to test." );
            /*
             * we want fastest on left, slowest on right,
             * so use negative numbers for bolt speed
             */
            old_speed = -edata_ptr->bolt_speed;
            fastest_speed = -FASTEST_BOLT_SPEED;
            slowest_speed = -SLOWEST_BOLT_SPEED;
        }
        scrollbar = GetDlgItem( dialog_wnd, SPEED_SCROLL );
        SetScrollRange( scrollbar, SB_CTL, fastest_speed, slowest_speed, FALSE );
        SetScrollPos( scrollbar, SB_CTL, old_speed, FALSE );
        return( TRUE );

    case WM_RBUTTONDOWN:
        if( what_are_we_setting == SET_BOLT_SPEED ) {
            /*
             * shoot bolt diagonally accross the screen as a test
             */
            edata_ptr->aim.x = edata_ptr->client_rect.left;
            edata_ptr->aim.y = edata_ptr->client_rect.bottom;
            ShootBolt( GetWindow( dialog_wnd, GW_OWNER ) );
        }
        break;
    case WM_HSCROLL:
        speed = ( what_are_we_setting == SET_TARGET_SPEED )
                ? edata_ptr->target_speed : -edata_ptr->bolt_speed;
        scrollbar = GetDlgItem( dialog_wnd, SPEED_SCROLL );
        switch( wparam ) {
        case SB_PAGEDOWN:
            speed += 15;    /* note - no break - flow through to next case */
        case SB_LINEDOWN:
            speed = min( slowest_speed, ++speed );
            break;
        case SB_PAGEUP:
            speed -= 15;    /* note - no break - flow through to next case */
        case SB_LINEUP:
            speed = max( fastest_speed, --speed );
            break;
        case SB_TOP:
            speed = fastest_speed;
            break;
        case SB_BOTTOM:
            speed = slowest_speed;
            break;
        case SB_THUMBPOSITION:
        case SB_THUMBTRACK:
            speed = LOWORD( lparam );
            break;
        default:
            return( FALSE );
        }
        SetScrollPos( scrollbar, SB_CTL, speed , TRUE );
        if( what_are_we_setting == SET_TARGET_SPEED ) {
            edata_ptr->target_speed = speed;

            /* restart timer at new speed */
            KillTimer( GetWindow( dialog_wnd, GW_OWNER), TARGET_TIMER );
            SetTimer( GetWindow( dialog_wnd, GW_OWNER), TARGET_TIMER,
                      speed, ( FARPROC ) NULL );
        } else {
            /* change speed back to positive value */
            edata_ptr-> bolt_speed = -speed;
        }
        break;
    case WM_COMMAND:
        switch( wparam ) {
        case IDOK:
            EndDialog( dialog_wnd, TRUE );
            return( TRUE );
        case IDCANCEL:
            /* change speed back to old value */
            if( what_are_we_setting == SET_TARGET_SPEED ) {
                edata_ptr->target_speed = old_speed;
            } else {
                edata_ptr->bolt_speed = -old_speed;
            }
            EndDialog( dialog_wnd, TRUE );
            return( TRUE );
        break;
        }
    }
    return( FALSE );

} /* SpeedDlgProc */

/*
 * WindowProc - handle messages for the main application window
 */
LONG _EXPORT FAR PASCAL WindowProc( HWND window_handle, unsigned msg,
                                     WORD wparam, LONG lparam )
/*************************************************************/
{
    FARPROC             proc;
    HANDLE              inst_handle;
    extra_data          *edata_ptr;
    HDC                 hdc;
    PAINTSTRUCT         ps;
    RECT                rect;
    HBRUSH              brush;

    /*
     * if the message window is ON, send all messages we want to display to the
     * message window, so that we can see what is happening
     * ( before we actually process the message )
     */
    if( MessagesOn ) {
        switch( msg ) {
        case WM_COMMAND:
        case WM_MOUSEMOVE:
        case WM_LBUTTONUP:
        case WM_LBUTTONDOWN:
        case WM_LBUTTONDBLCLK:
        case WM_RBUTTONUP:
        case WM_RBUTTONDOWN:
        case WM_RBUTTONDBLCLK:
        case WM_KEYDOWN:
        case WM_KEYUP:
        case WM_CHAR:
        case WM_TIMER:
        case WM_HSCROLL:
        case WM_VSCROLL:
            SendMessage( MessageWnd, msg, wparam, lparam );
            break;
        }
    }

    /*
     * now process the message
     */
    switch( msg ) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?