coreBugZJ

此 blog 已弃。

游戏 切水果

本程序是模仿流行的水果忍者游戏,移动鼠标,来切除空中运动的水果,水果为随机生成,作自由上抛落体运动。被切到的水果将裂为两半,自由落下。切除一个水果加一分,分数在右上角显示。炸弹会混在水果中抛出,一但切到炸弹,炸弹会爆炸,且分数归零。






/*
Fruit.c

Copyright (C) 2011, coreBugZJ, all rights reserved.

游戏 切水果。
使用 自己的 ImageZ 图形库 加载显示图像 ( ImageZ_201112291853 )。
支持 Win32 和 Linux 平台。
Win32 平台下使用 API 做界面。
Linux 平台下使用 Xlib 做界面。
ImageZ 本身跨平台。Win32 下提供 ImageZ.lib 和 ImageZ.dll。Linux 下提供 libImageZ.a。

玩法:鼠标移动即可切水果,有加分。切到雷,则分数归零。

版本:201112292200
*/


        /* 此宏若定义,则 Linux 平台;否则 Win32 平台 */
/* #define  FRUIT_LINUX */


#ifdef  FRUIT_LINUX
/* Linux */

#include <X11/Xlib.h>
#include <sys/time.h>
#include <signal.h>

#else /* FRUIT_LINUX */
/* Win32 */

#include <Windows.h>
#pragma comment( lib, "ImageZ.lib" )

#endif /* FRUIT_LINUX */

#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include "ImageZ.h"


        /* 游戏区域宽高 */
#define  BOARD_WIDTH   640
#define  BOARD_HEIGHT  480

        /* 窗口宽高 */
#define  WND_WIDTH     (BOARD_WIDTH)
#define  WND_HEIGHT    (BOARD_HEIGHT)

        /* 水果种类数,即 水果信息结构体 数组大小 */
#define  NUM_FRUIT    10
        /* 空中未被切的水果数最大值 */
#define  NUM_INFO     3
        /* 空中所有的物体数最大值,即 空中物体信息结构体 数组大小 */
#define  NUM_INFO_H   (NUM_INFO + 256)
        /* 切割时的折线段点数最大值 */
#define  NUM_POINT    1024
        /* 数字 */
#define  NUM_NUMBER   10


/* 空中物体信息结构体 标志值 */
        /* 未使用,可分配 */
#define  FLAG_NOT_USE      0
        /* 水果 */
#define  FLAG_FRUIT        1
        /* 左半水果 */
#define  FLAG_HALF_LEFT    2
        /* 右半水果 */
#define  FLAG_HALF_RIGHT   3
        /* 雷 */
#define  FLAG_THUNDER      4
        /* 爆炸 */
#define  FLAG_CRASH        5
        /* 本结构体正在处理中 */
#define  FLAG_DEAL         6


/* 游戏状态 */
        /* 停止 */
#define  STATE_STOP        0
        /* 运行中 */
#define  STATE_RUNNING     1


        /* 生成的水果是雷的概率 */
#define  THUNDER_PROB      9
        /* 雷 一帧区域 */
#define  THUNDER_WIDTH      64
#define  THUNDER_HEIGHT     64
        /* 雷一行帧数 */
#define  THUNDER_LINE_NUM   16
        /* 雷总帧数 */
#define  THUNDER_FRAME_NUM  16

        /* 爆炸一帧区域 */
#define  CRASH_WIDTH       440
#define  CRASH_HEIGHT      300
        /* 爆炸一行帧数 */
#define  CRASH_LINE_NUM    4
        /* 爆炸总帧数 */
#define  CRASH_FRAME_NUM   8

        /* 帧间隔,单位 MS */
#define  DELTA_TIME        100

        /* 垂直加速度,单位 像素/帧/帧 */
#define  ACCEL_Y           4
        /* 新水果 垂直速度 最大值,单位 像素/帧 */
#define  MAX_VY            55
        /* 新水果 垂直速度 最小值,单位 像素/帧 */
#define  MIN_VY            40
        /* 新水果 水平速度 最大值的2倍,单位 像素/帧 */
#define  MAX_VX            30
        /* 水果切开后,左半 水平速度,单位 像素/帧 */
#define  LEFT_VX           (-15)
        /* 水果切开后,右半 水平速度,单位 像素/帧 */
#define  RIGHT_VX          15
        /* 水果旋转因子,多少帧旋转90度 */
#define  ROTATE_FACTOR     1

        /* 水果半径,单位 像素。水果一律以圆形建模 */
#define  RADIUS       28


        /* 水果信息结构体,用于管理水果图片 */
struct _Fruit
{
        int     radius;              /* 半径 */
        ImageZ  img, imgLe, imgRi;   /* 图像 整个,左半,右半 */
};
typedef  struct _Fruit  Fruit;

        /* 空中物体信息结构体,用于管理空中物体 */
struct _Info
{
        int     px, py;    /* 位置,像素 */
        int     vx, vy;    /* 速度,像素/帧 */
        int     flag;      /* 标志 */
        int     type;      /* 类型,即 水果信息结构体 数组下标 */
        ImageZ  img;       /* 对应图片 */
};
typedef  struct _Info  Info;

        /* 显示图片,平台相关,分别实现 */
#ifdef  FRUIT_LINUX
/* Linux */

        /* 显示器信息结构体 */
struct _Disp
{
        Display  *dsp;
        Window   wnd;
        GC       gc;
        Visual   *vis;
};
typedef  struct _Disp  Disp;
Disp  disp;

        /* 在游戏初始化前,初始化显示器 */
void preInitGame( Display *dsp, Window wnd, GC gc, Visual *vis ) {
        disp.dsp = dsp;
        disp.wnd = wnd;
        disp.gc  = gc;
        disp.vis = vis;
}

        /* 将图像子区域显示在窗口子区域 */
void showImage( int dispLeft, int dispTop,
        int width, int height,
        ImageZ img, int imgLeft, int imgTop  ) {
        R32 res = displayImageZ( disp.dsp, disp.wnd,
                        disp.gc, disp.vis,
                        dispLeft, dispTop,
                        width, height,
                        img, imgLeft, imgTop  );
}

#else /* FRUIT_LINUX */
/* Win32 */

        /* 显示器信息结构体 */
struct _Disp
{
        HWND hWnd;
};
typedef  struct _Disp  Disp;
Disp  disp;

        /* 在游戏初始化前,初始化显示器 */
void preInitGame( HWND hWnd ) {
        disp.hWnd = hWnd;
}

        /* 将图像子区域显示在窗口子区域 */
void showImage( int dispLeft, int dispTop,
        int width, int height,
        ImageZ img, int imgLeft, int imgTop  ) {
                HDC hdc = GetDC( disp.hWnd );
                R32 res = displayImageZ( hdc, dispLeft, dispTop, width, height, img, imgLeft, imgTop );
                ReleaseDC( disp.hWnd, hdc );
}

#endif /* FRUIT_LINUX */


        /* 是否正在运行 */
int     isRunning;
        /* 游戏状态 */
int     state;
        /* 得分 */
int     score;
        /* 水果信息结构体 数组,用于管理水果图片 */
Fruit   fruit[ NUM_FRUIT ];
        /* 空中物体信息结构体 数组,用于管理空中物体 */
Info    info[ NUM_INFO_H ];
        /* 切割时的折线段中的点 */
int     ptX[ NUM_POINT ], ptY[ NUM_POINT ], ptNum;
        /* 背景图 */
ImageZ  imgBk;
        /* 帧缓存 */
ImageZ  imgFrame;
        /* 数字 */
ImageZ  imgNum[ NUM_NUMBER ];
        /* 爆炸,此时,空中物体信息结构体 中 type 表示爆炸帧下标 */
ImageZ  imgCrash;
        /* 雷,此时,空中物体信息结构体 中 type 表示爆炸帧下标 */
ImageZ  imgThunder;
        /* 刀 */
ImageZ  imgSword;


void updateGame( int sig );

        /* 启动定时器 */
void initTimer() {
#ifdef  FRUIT_LINUX
/* Linux */

        struct itimerval  new_timeset;
        long   n_sec, n_usecs;

        signal( SIGALRM, updateGame );

        n_sec   = DELTA_TIME / 1000;
        n_usecs = ( DELTA_TIME % 1000 ) * 1000L;

        new_timeset.it_interval.tv_sec  = n_sec;
        new_timeset.it_interval.tv_usec = n_usecs;
        new_timeset.it_value.tv_sec     = n_sec;
        new_timeset.it_value.tv_usec    = n_usecs;

        setitimer( ITIMER_REAL, &new_timeset, NULL );

#else /* FRUIT_LINUX */
/* Win32 */

        SetTimer( disp.hWnd, 1, DELTA_TIME, NULL );
       
#endif /* FRUIT_LINUX */
}

        /* 关闭定时器 */
void cancelTimer() {
#ifdef  FRUIT_LINUX
/* Linux */

        /* defs */

#else /* FRUIT_LINUX */
/* Win32 */

        KillTimer( disp.hWnd, 1 );

#endif /* FRUIT_LINUX */
}

        /* 限定范围的随机数 */
int randMod( int mod ) {
        if ( 1 > mod ) mod = 1;
        return rand() % mod;
}

        /* 初始化图像 Alpha 域,因 bmp 不支持透明,视纯白为透明色 */
void initAlpha( ImageZ img ) {
        U32 x, y, w, h, r, g, b, a;
        U08 *ptrimg;

#define  THRE_H  240
#define  THRE_L  120

        if ( (!isImageValidZ(img)) || (isImagePaletteUsedZ(img)) ) {
                return;
        }

        w = getImageWidthZ( img );
        h = getImageHeightZ( img );

        for ( y = 0; y < h; ++y ) {
                ptrimg = img->pPixel + y * img->linePitch;
                for ( x = 0; x < w; ++x ) {
                        b = ptrimg[ IMAGEZ_OFFSET_BLUE_Z  ];
                        g = ptrimg[ IMAGEZ_OFFSET_GREEN_Z ];
                        r = ptrimg[ IMAGEZ_OFFSET_RED_Z   ];
                        if ( (THRE_H < b) && (THRE_H < g) && (THRE_H < r) ) {
                                a = 0;
                        }
                        else {
                                a = 255;
                        }

                        ptrimg[ IMAGEZ_OFFSET_ALPHA_Z ] = (U08)(a);
                        ptrimg += IMAGEZ_COLOR_SIZE_Z;
                }
        }
#undef  THRE_L
#undef  THRE_H
}

        /* 初始化游戏 */
void initGame() {
        int i;
        char base[ 30 ] = "./Res/fruit";
        char ext[ 30 ]  = ".png";
        char lef[ 30 ]  = "left";
        char rig[ 30 ]  = "right";
        char num[ 30 ];
        char name[ 90 ] = "";

        srand( (unsigned int)time( NULL ) );

        imgBk      = createImageFromFilePngZ( "./Res/imgBk.png" );
        imgFrame   = createImageFromImageInfoZ( imgBk );
        imgCrash   = createImageFromFilePngZ( "./Res/crash.png" );
        imgThunder = createImageFromFilePngZ( "./Res/thunder.png" );
        imgSword   = createImageFromFilePngZ( "./Res/sword.png" );

        score = 0;

        ptNum = 0;

        memset( info, 0, sizeof(info) );

        memset( fruit, 0, sizeof(fruit) );
        for ( i = 0; i < NUM_FRUIT; ++i ) {
                fruit[ i ].radius = RADIUS;

                strcpy( name, base );
                sprintf( num, "%d", i );
                strcat( name, num );
                strcat( name, ext );
                fruit[ i ].img = createImageFromFilePngZ( name );

                strcpy( name, base );
                sprintf( num, "%d", i );
                strcat( name, num );
                strcat( name, lef );
                strcat( name, ext );
                fruit[ i ].imgLe = createImageFromFilePngZ( name );

                strcpy( name, base );
                sprintf( num, "%d", i );
                strcat( name, num );
                strcat( name, rig );
                strcat( name, ext );
                fruit[ i ].imgRi = createImageFromFilePngZ( name );
        }

        for ( i = 0; i < NUM_NUMBER; ++i ) {
                sprintf( name, "./Res/%d.bmp", i );
                imgNum[ i ] = createImageFromFileBmpZ( name );
                initAlpha( imgNum[ i ] );
        }

        state = STATE_RUNNING;
        isRunning = 1;
        initTimer();
}

        /* 新水果初始化 */
void initFruitInfo( Info *pInf ) {
        if ( NULL == pInf ) {
                return;
        }
        pInf->flag = FLAG_DEAL;

        destroyImageZ( pInf->img );
        pInf->img = NULL;

        pInf->px   = randMod( BOARD_WIDTH / 3 ) + BOARD_WIDTH / 6;
        pInf->py   = BOARD_HEIGHT - MAX_VY;
        pInf->vx   = randMod( MAX_VX ) - MAX_VX / 2;
        pInf->vy   = -randMod( MAX_VY - MIN_VY ) - MIN_VY;

        if ( randMod( THUNDER_PROB ) != 0 ) {
                pInf->type = randMod( NUM_FRUIT );
                pInf->img  = createImageFromImageZ( fruit[ pInf->type ].img );
                pInf->flag = FLAG_FRUIT;
        }
        else {
                pInf->type = 0;
                pInf->img  = imgThunder;
                pInf->flag = FLAG_THUNDER;
        }
}

        /* 切掉水果 */
void cutFruitInfo( Info *pInf ) {
        int i, j;

        if ( NULL == pInf ) {
                return;
        }

                /* 雷 */
        if ( FLAG_THUNDER == pInf->flag ) {
                pInf->flag = FLAG_DEAL;

                pInf->img   = imgCrash;
                pInf->px   -= CRASH_WIDTH / 2;
                pInf->py   -= CRASH_HEIGHT / 2;
                pInf->type  = 0;
                pInf->flag  = FLAG_CRASH;

                score = 0;
                return;
        }

                /* 不是水果 */
        if ( FLAG_FRUIT != pInf->flag ) {
                return;
        }

        pInf->flag = FLAG_DEAL;

        i = NUM_INFO;
        while ( (NUM_INFO_H > i) && (FLAG_NOT_USE != info[ i ].flag) ) {
                ++i;
        }
        info[ i ].flag = FLAG_DEAL;
        j = i + 1;
        while ( (NUM_INFO_H > j) && (FLAG_NOT_USE != info[ j ].flag) ) {
                ++j;
        }
        info[ j ].flag = FLAG_DEAL;

        if ( NUM_INFO_H > j ) {
                destroyImageZ( info[ i ].img );
                info[ i ].img  = createImageFromImageZ( fruit[ pInf->type ].imgLe );
                info[ i ].px   = pInf->px;
                info[ i ].py   = pInf->py;
                info[ i ].type = pInf->type;
                info[ i ].vx   = LEFT_VX;
                info[ i ].vy   = 0;
                info[ i ].flag = FLAG_HALF_LEFT;

                destroyImageZ( info[ j ].img );
                info[ j ].img  = createImageFromImageZ( fruit[ pInf->type ].imgRi );
                info[ j ].px   = pInf->px + getImageWidthZ(info[ i ].img);
                info[ j ].py   = pInf->py;
                info[ j ].type = pInf->type;
                info[ j ].vx   = RIGHT_VX;
                info[ j ].vy   = 0;
                info[ j ].flag = FLAG_HALF_RIGHT;
        }
        else {
                info[ i ].flag = FLAG_NOT_USE;
                info[ j ].flag = FLAG_NOT_USE;
        }

        pInf->flag = FLAG_NOT_USE;
        ++score;
}

        /* 检查是否切到 */
int checkCut( Info *pInf ) {
        int cx, cy, r, r2, i;

        if ( NULL == pInf ) {
                return 0;
        }

        if ( FLAG_THUNDER == pInf->flag ) {
                cx = THUNDER_WIDTH  / 2 + pInf->px;
                cy = THUNDER_HEIGHT / 2 + pInf->py;
                r  = RADIUS;
        }
        else if ( FLAG_FRUIT == pInf->flag ) {
                cx = getImageWidthZ(pInf->img)  / 2 + pInf->px;
                cy = getImageHeightZ(pInf->img) / 2 + pInf->py;
                r  = fruit[ pInf->type ].radius;
        }
        else {
                return 0;
        }
        r2 = r * r;

        for ( i = 0; i < ptNum; ++i ) {
                if ( (ptX[i]-cx) * (ptX[i]-cx) + (ptY[i]-cy) * (ptY[i]-cy) < r2 ) {
                        return 1;
                }
        }

        return 0;
}

        /* 定时器回调函数,一帧,更新游戏数据 */
void updateGame( int sig ) {
        static  int  rf = 0;
        int  i, j, w, h, le;

                /* 已经结束 */
        if ( 0 == isRunning ) {
                return;
        }

                /* 绘制背景 */
        paintImageZ( imgFrame, 0, 0,
                imgBk, 0, 0,
                getImageWidthZ(imgBk), getImageHeightZ(imgBk) );

                /* 绘制刀 */
#define  SWORD_POINT  12
        if ( SWORD_POINT <= ptNum ) {
                ImageZ img = NULL;
                int    lef, top, dx, dy, xh, xt, yh, yt;

                img = createImageCropZ( imgSword, 0, 0, 100, 100 );

                xh = yh = xt = yt = 0;
                for ( i = 1; i <= SWORD_POINT; ++i ) {
                        if ( SWORD_POINT / 2 >= i ) {
                                xt += ptX[ ptNum - i ];
                                yt += ptY[ ptNum - i ];
                        }
                        else {
                                xh += ptX[ ptNum - i ];
                                yh += ptY[ ptNum - i ];
                        }
                }
                xh /= SWORD_POINT / 2;
                yh /= SWORD_POINT / 2;
                xt /= SWORD_POINT / 2;
                yt /= SWORD_POINT / 2;

                dx = xt - xh;
                dy = yt - yh;

                rotateImageZ( img,
                        atan2( (double)(-dy), (double)dx ),
                        ITP_BILINEAR_Z, NULL );
                cropImageZ( img, (getImageWidthZ(img)-100)/2, (getImageHeightZ(img)-100)/2, 100, 100 );

                /*
                xt = ptX[ ptNum - 1 ];
                yt = ptY[ ptNum - 1 ];
                if ( 0 <= dx ) {
                        if ( 0 <= dy ) {
                                lef = xt - 55;
                                top = yt - 55;
                        }
                        else {
                                lef = xt - 55;
                                top = yt - 35;
                        }
                }
                else {
                        if ( 0 <= dy ) {
                                lef = xt - 35;
                                top = yt - 55;
                        }
                        else {
                                lef = xt - 35;
                                top = yt - 35;
                        }
                }
                */

                lef = ptX[ ptNum - 1 ] - ( (0 <= dx) ? 55 : 35 );
                top = ptY[ ptNum - 1 ] - ( (0 <= dy) ? 55 : 35 );

                paintImageZ( imgFrame, lef, top,
                        img, 0, 0, 100, 100 );
                destroyImageZ( img );
                img = NULL;
        }

                /* 检查切割 */
        for ( i = ptNum-1; i > 0; --i ) {
                ptX[ ptNum ] = ( ptX[ i ] + ptX[ i - 1 ] ) / 2;
                ptY[ ptNum ] = ( ptY[ i ] + ptY[ i - 1 ] ) / 2;
                if ( NUM_POINT > ptNum ) {
                        ++ptNum;
                }
        }
        for ( i = 0; i < NUM_INFO; ++i ) {
                if ( (FLAG_NOT_USE == info[ i ].flag) || (FLAG_DEAL == info[ i ].flag) ) {
                        continue;
                }
                if ( checkCut( &(info[ i ]) ) ) {
                        cutFruitInfo( &(info[ i ]) );
                }
        }
        ptNum = 0;

                /* 自由落体 */
        for ( i = 0; i < NUM_INFO_H; ++i ) {
                if ( (FLAG_NOT_USE == info[ i ].flag) || (FLAG_DEAL == info[ i ].flag) ) {
                        continue;
                }
                info[ i ].px += info[ i ].vx;
                info[ i ].py += info[ i ].vy;
                info[ i ].vy += ACCEL_Y;
                w = getImageWidthZ( info[ i ].img );
                h = getImageHeightZ( info[ i ].img );
                if ( (BOARD_HEIGHT < info[ i ].py + 5) ||
                     (BOARD_WIDTH  < info[ i ].px + 5) || (-w+5 > info[ i ].px) ) {
                        if ( (FLAG_THUNDER == info[ i ].flag) || (FLAG_CRASH == info[ i ].flag) ) {
                                info[ i ].img = NULL;
                        }
                        info[ i ].flag = FLAG_NOT_USE;
                }
        }

                /* 新水果 */
        for ( i = 0; i < NUM_INFO; ++i ) {
                if ( FLAG_NOT_USE == info[ i ].flag ) {
                        initFruitInfo( &(info[ i ]) );
                }
        }

                /* 绘制得分 */
        i  = score;
        le = BOARD_WIDTH - 60;
        do {
                j  = i % NUM_NUMBER;
                i /= NUM_NUMBER;
                paintImageZ( imgFrame, le, 7,
                        imgNum[ j ], 0, 0,
                        getImageWidthZ(imgNum[ j ]), getImageHeightZ(imgNum[ j ])  );
                le -= getImageWidthZ(imgNum[ j ]) - 4;
        } while ( 0 != i );

                /* 绘制空中物体 */
        rf = ( rf + 1 ) % ROTATE_FACTOR;
        for ( i = 0; i < NUM_INFO_H; ++i ) {
                if ( (FLAG_NOT_USE == info[ i ].flag) || (FLAG_DEAL == info[ i ].flag) ) {
                        continue;
                }

                if ( FLAG_THUNDER == info[ i ].flag ) {
                        paintImageZ( imgFrame, info[ i ].px, info[ i ].py,
                                info[ i ].img, info[ i ].type % THUNDER_LINE_NUM * THUNDER_WIDTH,
                                info[ i ].type / THUNDER_LINE_NUM * THUNDER_HEIGHT,
                                THUNDER_WIDTH, THUNDER_HEIGHT   );
                        info[ i ].type = ( info[ i ].type + 3 ) % THUNDER_FRAME_NUM;
                }
                else if ( FLAG_CRASH == info[ i ].flag ) {
                        if ( CRASH_FRAME_NUM > info[ i ].type ) {
                                paintImageZ( imgFrame, info[ i ].px, info[ i ].py,
                                        info[ i ].img, info[ i ].type % CRASH_LINE_NUM * CRASH_WIDTH,
                                        info[ i ].type / CRASH_LINE_NUM * CRASH_HEIGHT,
                                        CRASH_WIDTH, CRASH_HEIGHT   );
                                ++(info[ i ].type);
                        }
                }
                else {
                        if ( 0 == rf ) {
                                rotateImage90degreeZ( info[ i ].img, FALSE );
                        }
                        paintImageZ( imgFrame, info[ i ].px, info[ i ].py,
                                info[ i ].img, 0, 0,
                                getImageWidthZ(info[ i ].img), getImageHeightZ(info[ i ].img) );
                }
        }

                /* 显示 */
        showImage( 0, 0,
                getImageWidthZ(imgFrame), getImageHeightZ(imgFrame),
                imgFrame, 0, 0 );
}

        /* 处理鼠标移动消息 */
void moveMouse( int x, int y ) {
        ptX[ ptNum ] = x;
        ptY[ ptNum ] = y;
        ++ptNum;
}

        /* 游戏结束,释放资源 */
void finishGame() {
        int i;
        isRunning = 0;
        cancelTimer();
        for ( i = 0; i < NUM_FRUIT; ++i ) {
                destroyImageZ( fruit[ i ].img );
                destroyImageZ( fruit[ i ].imgLe );
                destroyImageZ( fruit[ i ].imgRi );
                fruit[ i ].img   = NULL;
                fruit[ i ].imgLe = NULL;
                fruit[ i ].imgRi = NULL;
        }
        for ( i = 0; i < NUM_NUMBER; ++i ) {
                destroyImageZ( imgNum[ i ] );
                imgNum[ i ] = NULL;
        }
        for ( i = 0; i < NUM_INFO_H; ++i ) {
                if ( (FLAG_THUNDER != info[ i ].flag) && (FLAG_CRASH != info[ i ].flag) ) {
                        destroyImageZ( info[ i ].img );
                }
                info[ i ].img = NULL;
        }
        destroyImageZ( imgBk );
        imgBk = NULL;
        destroyImageZ( imgFrame );
        imgFrame = NULL;
        destroyImageZ( imgCrash );
        imgCrash = NULL;
        destroyImageZ( imgThunder );
        imgThunder = NULL;
        destroyImageZ( imgSword );
        imgSword = NULL;
}


#ifdef  FRUIT_LINUX
/* Linux */

int main() {
        Display  *disp;
        int      scr;
        Window   wnd;
        GC       gc;
        Visual   *vis;
        XEvent   e;

        disp = XOpenDisplay( NULL );
        if ( NULL == disp ) {
                printf( "XOpenDisplay Failed!\n" );
                return 0;
        }
        scr = XDefaultScreen( disp );
        gc  = XDefaultGC( disp, scr );
        vis = XDefaultVisual( disp, scr );
        wnd = XCreateSimpleWindow( disp, RootWindow( disp, scr ),
                200, 200, WND_WIDTH, WND_HEIGHT, 3,
                BlackPixel( disp, scr ), WhitePixel( disp, scr )   );

        XSelectInput( disp, wnd, ExposureMask | KeyPressMask | PointerMotionMask );

        XMapWindow( disp, wnd );

        preInitGame( disp, wnd, gc, vis );
        initGame();

        while ( 1 ) {
                XNextEvent( disp, &e );

                if ( Expose == e.type ) {
                }

                if ( MotionNotify == e.type  ) {
                        moveMouse( e.xmotion.x, e.xmotion.y );
                }

                /*
                if ( KeyPress == e.type )
                        break;
                */
        }

        finishGame();

        XCloseDisplay( disp );

        return 0;
}

#else /* FRUIT_LINUX */
/* Win32 */

LRESULT  CALLBACK  WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
        PAINTSTRUCT ps;
        HDC         hdc;

        switch ( uMsg ) {
        case WM_TIMER :
                updateGame( 0 );
                return 0;

        case WM_MOUSEMOVE :
                moveMouse( LOWORD(lParam), HIWORD(lParam) );
                return 0;

        case WM_CREATE :
                preInitGame( hWnd );
                initGame();
                return 0;

        case WM_PAINT :
                hdc = BeginPaint( hWnd, &ps );
                EndPaint( hWnd, &ps );
                return 0;

        case WM_DESTROY :
                finishGame();
                PostQuitMessage( 0 );
                return 0;
        }
        return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

INT  APIENTRY  WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR szCmdLine, INT iShow ) {
        static TCHAR  className[] = TEXT("Fruit");
        static TCHAR  wndName[]   = TEXT("Fruit");

        WNDCLASSEX wc;
        HWND       hWnd;
        MSG        msg;

        wc.cbClsExtra = 0;
        wc.cbSize     = sizeof(wc);
        wc.cbWndExtra = 0;
        wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
        wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
        wc.hIcon         = LoadIcon( NULL, IDI_APPLICATION );
        wc.hIconSm       = LoadIcon( NULL, IDI_APPLICATION );
        wc.hInstance     = hInst;
        wc.lpfnWndProc   = WndProc;
        wc.lpszClassName = className;
        wc.lpszMenuName  = NULL;
        wc.style         = 0;
        RegisterClassEx( &wc );

        hWnd = CreateWindowEx( 0, className, wndName,
                WS_OVERLAPPEDWINDOW,
                100, 100, WND_WIDTH, WND_HEIGHT,
                NULL, NULL, wc.hInstance, NULL );

        ShowWindow( hWnd, iShow );
        UpdateWindow( hWnd );

        while ( GetMessage( &msg, NULL, 0, 0 ) ) {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
        }

        return msg.wParam;
}

#endif /* FRUIT_LINUX */

posted on 2011-12-31 16:53 coreBugZJ 阅读(4091) 评论(2)  编辑 收藏 引用 所属分类: 课内作业娱乐

Feedback

# re: 游戏 切水果 2012-01-02 18:27 林羽飞扬

虽然看不懂,但是感觉很牛逼的样子  回复  更多评论   

# re: 游戏 切水果 2013-07-26 15:43 chezai

ImageZ.lib哪里来的?  回复  更多评论   



只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理