| CODE |
| # include <windows.h> # include <winuser.h> # include <stdio.h> # include <stdlib.h> # include <time.h> # define APPTITLE "Game loop" // function prototypes LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM); ATOM MyRegisterClass(HINSTANCE); bool InitInstance(HINSTANCE, int); void DrawBitmap(HDC, char*, int, int); void Game_Init(); void Game_Run(); void Game_End(); // local variables HWND global_hwnd; HDC global_hdc; LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { global_hwnd=hWnd; global_hdc=GetDC(hWnd); switch(message) { case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hWnd, message, wParam, lParam); } ATOM MyRegisterClass(HINSTANCE hInstance) { //create the claass structure WNDCLASSEX wc; wc.cbSize=sizeof(WNDCLASSEX); //fillt the struct with info wc.style= CS_HREDRAW|CS_VREDRAW; wc.lpfnWndProc=(WNDPROC)WinProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance= hInstance; wc.hIcon= NULL; wc.hCursor= LoadCursor(NULL,IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wc.lpszMenuName= NULL; wc.lpszClassName= "APPTITLE"; wc.hIconSm = NULL; // set up the window wtih the class info return RegisterClassEx(&wc); } bool InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; // create a new window hWnd= CreateWindow( "APPTITLE",//window class "GL",//title bar WS_OVERLAPPEDWINDOW | //window style WS_VISIBLE | WS_SYSMENU,//window style CW_USEDEFAULT,//x positon CW_USEDEFAULT,//y postion 500,//width of window 400,//height of window NULL,//parent window NULL,//menu hInstance,// application instance NULL);//window parameter //if erreor creating window if(!hWnd) return false; //display the window ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); return true; } //entry point for int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){ // declare variables MSG msg; // register the class MyRegisterClass(hInstance); // intialize application if (!InitInstance(hInstance,nCmdShow)) return false; // intialize the game Game_Init(); int done=0; while(done!=1) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { //look for quit message if(msg.message==WM_QUIT) done=1; //decode and pass the messages on to WndProc TranslateMessage(&msg); DispatchMessage(&msg); } } // do game cleanup Game_End(); msg.wParam; } void Game_Init() { // load bitmaps srand(time(NULL)); } void Game_Run() { int x=0, y=0; RECT rect; GetClientRect(global_hwnd, &rect); if(rect.right>0) { x= rand()%(rect.right-rect.left); y= rand()%(rect.bottom-rect.top); DrawBitmap(global_hdc, "c.bmp", x, y); } } void Game_End() { } void DrawBitmap(HDC hdcDest, char *filename, int x, int y) { HBITMAP image; BITMAP bm; HDC hdcMem; //load the btimap image image= LoadImage(0, "c.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); //read the bitmap's properties GetObject(image, sizeof(BITMAP), &bm); //crate a device context fot the bitmap hdcMem=CreateCompatibleDC(global_hdc); SelectObject(hdcMem, image); //draw the bitmap to the window BitBlt( global_hdc,//destination device context x,y,//x and y destination bm.bmWidth, bm.bmHeight,//widht, height of bitmap hdcMem,//source bitmap context 0, 0,// start x,y on soure bitmap SRCCOPY);//blit method //delete the context and bitmap DeleteDC(hdcMem); DelteObject((HBITMAP)image); } |