- ///////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////
- LRESULT CALLBACK WindowProc(HWND hwnd,
- UINT msg,
- WPARAM wparam,
- LPARAM lparam)
- {
- // this is the main message handler of the system
- PAINTSTRUCT ps; // used in WM_PAINT
- HDC hdc; // handle to a device context
- char buffer[80]; // used to print strings
- // what is the message
- switch(msg)
- {
- case WM_CREATE:
- {
- // do initialization stuff here
- // return success
- return(0);
- } break;
- case WM_PAINT:
- {
- // simply validate the window
- hdc = BeginPaint(hwnd,&ps);
- // end painting
- EndPaint(hwnd,&ps);
- // return success
- return(0);
- } break;
- case WM_DESTROY:
- {
- // kill the application, this sends a WM_QUIT message
- PostQuitMessage(0);
- // return success
- return(0);
- } break;
- default:break;
- } // end switch
- // process any messages that we didn't take care of
- return (DefWindowProc(hwnd, msg, wparam, lparam));
- } // end WinProc
- ///////////////////////////////////////////////////////////
- int Game_Main(void *parms = NULL, int num_parms = 0)
- {
- // 这是游戏的主循环,作你的处理
- //查找正确的行走顺序
- static int anmation_seq[4]={0,1,0,2};
- int index;//循环变量
- // 确保这不是再次执行
- if (window_closed)
- return(0);
- if (KEYDOWN(VK_ESCAPE))
- {
- PostMessage(main_window_handle,WM_CLOSE,0,0);
- window_closed = 1;
- }
- //复制背景到后备缓冲区
- DDraw_Draw_Surface(lpddsbackground,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,lpddsback,0);
- //移动物体
- for (index=0;index<2;index++)
- {
- //使对象向右移动
- aliens[index].x++;
- //测试是否进入屏幕的边缘,设置循环
- if(aliens[index].x>SCREEN_WIDTH)
- aliens[index].x=-80;
- //机器人动画
- if (++aliens[index].counter >=(8-aliens[index].velocity))
- {
- //复位计数器
- aliens[index].counter=0;//动画时间为0
- //前进到下一帧
- if (++aliens[index].current_frame>2)//当前帧大于3
- {
- aliens[index].current_frame=0;
- }
- }
- //角色控制
- if (KEYDOWN(VK_RIGHT))
- {
- aliens[2].x++;
- //测试是否进入屏幕的边缘,设置循环
- if(aliens[2].x>SCREEN_WIDTH)
- aliens[2].x=-80;
- if (++aliens[2].counter >=(8-aliens[2].velocity))
- {
- //复位计数器
- aliens[2].counter=0;//动画时间为0
- //前进到下一帧
- if (++aliens[2].current_frame>2)//当前帧大于3
- {
- aliens[2].current_frame=0;
- }
- }
- }
- }//结束index循环
- //绘制全部机器人
- for (index =0;index<3;index++)
- {
- //绘制对象
- DDraw_Draw_Surface(aliens[index].frames[anmation_seq[aliens[index].current_frame]],
- aliens[index].x,aliens[index].y,
- 72,80,
- lpddsback);
- }
- while(FAILED(lpddsprimary->Flip(NULL,DDFLIP_WAIT)));
- Sleep(30);
- return(1);
- } // end Game_Main
- ////////////////////////////////////////////////////////////
- int Game_Init(void *parms = NULL, int num_parms = 0)
- {
- // this is called once after the initial window is created and
- // before the main event loop is entered, do all your initialization
- // here
- // create IDirectDraw interface 7.0 object and test for error
- if (FAILED(DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)))
- return(0);
- // set cooperation to full screen
- if (FAILED(lpdd->SetCooperativeLevel(main_window_handle,
- DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
- DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
- return(0);
- // set display mode to 640x480x8
- if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,0,0)))
- return(0);
- // we need a complex surface system with a primary and backbuffer
- // clear ddsd and set size
- DDRAW_INIT_STRUCT(ddsd);
- // enable valid fields
- ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
- // set the backbuffer count field to 1, use 2 for triple buffering
- ddsd.dwBackBufferCount = 1;
- // request a complex, flippable
- ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
- // create the primary surface
- if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL)))
- return(0);
- // now query for attached surface from the primary surface
- // this line is needed by the call
- ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
- // get the attached back buffer surface
- if (FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsback)))
- return(0);
- // set clipper up on back buffer since that's where well clip
- RECT screen_rect= {0,0,SCREEN_WIDTH-1,SCREEN_HEIGHT-1};
- lpddclipper = DDraw_Attach_Clipper(lpddsback,1,&screen_rect);
- // load the 8-bit p_w_picpath
- if (!Load_Bitmap_File(&bitmap,"1.bmp"))
- return(0);
- // clean the surfaces
- DDraw_Fill_Surface(lpddsprimary,0);
- DDraw_Fill_Surface(lpddsback,0);
- // create the buffer to hold the background
- lpddsbackground = DDraw_Create_Surface(640,480,0);
- // copy the background bitmap p_w_picpath to the background surface
- // lock the surface
- lpddsbackground->Lock(NULL,&ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
- // get video pointer to primary surfce
- DWORD *p_w_picpath_buffer = (DWORD *)ddsd.lpSurface;
- // process each line and copy it into the primary buffer
- for (int index_y = 0; index_y < SCREEN_HEIGHT; index_y++)
- {
- for (int index_x = 0; index_x < SCREEN_WIDTH; index_x++)
- {
- // get BGR values
- UCHAR blue = (bitmap.buffer[index_y*SCREEN_WIDTH*4 + index_x*4 + 0]),
- green = (bitmap.buffer[index_y*SCREEN_WIDTH*4 + index_x*4 + 1]),
- red = (bitmap.buffer[index_y*SCREEN_WIDTH*4 + index_x*4 + 2]);
- // this builds a 32 bit color value in A.8.8.8 format (8-bit alpha mode)
- DWORD pixel = _RGB32BIT(0,red,green,blue);
- // write the pixel
- p_w_picpath_buffer[index_x + (index_y*ddsd.lPitch >> 2)] = pixel;
- } // end for index_x
- } // end for index_y
- // now unlock the primary surface
- if (FAILED(lpddsbackground->Unlock(NULL)))
- return(0);
- // unload the bitmap file, we no longer need it
- Unload_Bitmap_File(&bitmap);
- // seed random number generator
- srand(GetTickCount());
- // initialize all the aliens
- // alien on level 1 of complex
- aliens[0].x = rand()%SCREEN_WIDTH;
- aliens[0].y = 116 - 72;
- aliens[0].velocity = 2+rand()%4;
- aliens[0].current_frame = 0;
- aliens[0].counter = 0;
- // alien on level 2 of complex
- aliens[1].x = rand()%SCREEN_WIDTH;
- aliens[1].y = 246 - 72;
- aliens[1].velocity = 2+rand()%4;
- aliens[1].current_frame = 0;
- aliens[1].counter = 0;
- // alien on level 3 of complex
- aliens[2].x = rand()%SCREEN_WIDTH;
- aliens[2].y = 382 - 72;
- aliens[2].velocity = 2+rand()%4;
- aliens[2].current_frame = 0;
- aliens[2].counter = 0;
- // now load the bitmap containing the alien p_w_picpathry
- // then scan the p_w_picpaths out into the surfaces of alien[0]
- // and copy then into the other two, be careful of reference counts!
- // load the 8-bit p_w_picpath
- if (!Load_Bitmap_File(&bitmap,"3.bmp"))
- return(0);
- // create each surface and load bits
- for (int index = 0; index < 2; index++)
- {
- // create surface to hold p_w_picpath
- aliens[0].frames[index] = DDraw_Create_Surface(72,80,0);
- // now load bits...
- Scan_Image_Bitmap(&bitmap, // bitmap file to scan p_w_picpath data from
- aliens[0].frames[index], // surface to hold data
- index, 2); // cell to scan p_w_picpath from
- } // end for index
- // unload the bitmap file, we no longer need it
- Unload_Bitmap_File(&bitmap);
- // now for the tricky part. There is no need to create more surfaces with the same
- // data, so I'm going to copy the surface pointers member for member to each alien
- // however, be careful, since the reference counts do NOT go up, you still only need
- // to release() each surface once!
- for (int index = 0; index < 2; index++)
- aliens[1].frames[index] = aliens[2].frames[index] = aliens[0].frames[index];
- // return success or failure or your own return code here
- return(1);
- } // end Game_Init
- /////////////////////////////////////////////////////////////
- int Game_Shutdown(void *parms = NULL, int num_parms = 0)
- {
- // this is called after the game is exited and the main event
- // loop while is exited, do all you cleanup and shutdown here
- // first the palette
- if (lpddpal)
- {
- lpddpal->Release();
- lpddpal = NULL;
- } // end if
- // now the primary surface
- if (lpddsprimary)
- {
- lpddsprimary->Release();
- lpddsprimary = NULL;
- } // end if
- // now blow away the IDirectDraw4 interface
- if (lpdd)
- {
- lpdd->Release();
- lpdd = NULL;
- } // end if
- // unload the bitmap file, we no longer need it
- Unload_Bitmap_File(&bitmap);
- // return success or failure or your own return code here
- return(1);
- } // end Game_Shutdown
- // WINMAIN ////////////////////////////////////////////////
- int WINAPI WinMain( HINSTANCE hinstance,
- HINSTANCE hprevinstance,
- LPSTR lpcmdline,
- int ncmdshow)
- {
- WNDCLASSEX winclass; // this will hold the class we create
- HWND hwnd; // generic window handle
- MSG msg; // generic message
- HDC hdc; // graphics device context
- // first fill in the window class stucture
- winclass.cbSize = sizeof(WNDCLASSEX);
- winclass.style = CS_DBLCLKS | CS_OWNDC |
- CS_HREDRAW | CS_VREDRAW;
- winclass.lpfnWndProc = WindowProc;
- winclass.cbClsExtra = 0;
- winclass.cbWndExtra = 0;
- winclass.hInstance = hinstance;
- winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
- winclass.lpszMenuName = NULL;
- winclass.lpszClassName = WINDOW_CLASS_NAME;
- winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- // save hinstance in global
- hinstance_app = hinstance;
- // register the window class
- if (!RegisterClassEx(&winclass))
- return(0);
- // create the window
- if (!(hwnd = CreateWindowEx(NULL, // extended style
- WINDOW_CLASS_NAME, // class
- TEXT("DirectDraw 8-Bit Bitmap Loading"), // title
- WS_POPUP | WS_VISIBLE,
- 0,0, // initial x,y
- SCREEN_WIDTH,SCREEN_HEIGHT, // initial width, height
- NULL, // handle to parent
- NULL, // handle to menu
- hinstance,// instance of this application
- NULL))) // extra creation parms
- return(0);
- // save main window handle
- main_window_handle = hwnd;
- // initialize game here
- Game_Init();
- // enter main event loop
- while(TRUE)
- {
- // test if there is a message in queue, if so get it
- if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
- {
- // test if this is a quit
- if (msg.message == WM_QUIT)
- break;
- // translate any accelerator keys
- TranslateMessage(&msg);
- // send the message to the window proc
- DispatchMessage(&msg);
- } // end if
- // main game processing goes here
- Game_Main();
- } // end while
- // closedown game here
- Game_Shutdown();
- // return to Windows like this
- return(msg.wParam);
- } // end WinMain
- ///////////////////////////////////////////////////////////