LRESULT CALLBACK WindowFunc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { // Identify the event switch(message) { case WM_ACTIVATE: g_bActive = wParam; return 0L; case WM_DESTROY: // Shut down DirectDraw (in case not already done so) DD_Shutdown(); // Terminate System PostQuitMessage(0); return 0L; } // Event not recognised - Return default message procedure return DefWindowProc(hwnd,message,wParam,lParam); }
LRESULT CALLBACK WindowFunc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_ACTIVATE: g_bActive = wParam; return 0L; case WM_SETCURSOR: // Turn off the cursor since this is a full-screen app SetCursor(NULL); return TRUE; case WM_DESTROY: DD_Shutdown(); PostQuitMessage(0); return 0L; break; } return DefWindowProc(hwnd,message,wParam,lParam); }
int DD_Init(HWND hwnd) { int index; if(DirectDrawCreate(NULL,&pdd,NULL)!=DD_OK) { DD_Shutdown(); return 0; } if(pdd->QueryInterface(IID_IDirectDraw4, (LPVOID *) & lpdd ) != DD_OK) { DD_Shutdown(); return 0; } if(lpdd->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_FPUSETUP) != DD_OK ) { DD_Shutdown(); return 0; } if(lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,0,0) != DD_OK) { DD_Shutdown(); return 0; } ZeroMemory(&ddsd, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL) != DD_OK) { DD_Shutdown(); return 0; } ddscaps.dwCaps = DDSCAPS_BACKBUFFER; if (lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback) != DD_OK) { DD_Shutdown(); return 0; } int temp; for(index = 0; index < 256; index++ ) { // Swap blue and red components (Bitmap file stores in incorrect order) temp = colour_palette[index].peRed; colour_palette[index].peRed = colour_palette[index].peBlue; colour_palette[index].peBlue = temp; // colour_palette[index].peGreen = index; colour_palette[index].peFlags = PC_NOCOLLAPSE; } if(lpdd->CreatePalette((DDPCAPS_8BIT | DDPCAPS_INITIALIZE | DDPCAPS_ALLOW256 ),colour_palette,&lpddpal,NULL)!=DD_OK) { DD_Shutdown(); return 0; } if( lpddsprimary->SetPalette(lpddpal) != DD_OK ) { DD_Shutdown(); return 0; } for(index = 0; index < 256; index++ ) { // Swap blue and red components (Bitmap file stores in incorrect order) temp = colour_palette[index].peRed; colour_palette[index].peRed = colour_palette[index].peBlue; colour_palette[index].peBlue = temp; colour_palette[index].peFlags = PC_EXPLICIT; } return 1; } // end DD_Init
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode) { HWND hwnd; MSG msg; WNDCLASS wcl; HDC hdc; char buffer[] = "hello"; wcl.hInstance = hThisInst; wcl.lpszClassName = WINDOW_CLASS_NAME; wcl.lpfnWndProc = WindowFunc; wcl.style = CS_HREDRAW | CS_VREDRAW; wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION); //wcl.hIconSm = LoadIcon(NULL, IDI_WINLOGO); wcl.hCursor = LoadCursor(NULL, IDC_ARROW); wcl.lpszMenuName = WINDOW_CLASS_NAME; wcl.cbClsExtra = 0; wcl.cbWndExtra = 0; wcl.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH); if(!RegisterClass(&wcl)) return 0; if(!(hwnd = CreateWindowEx ( WS_EX_TOPMOST, WINDOW_CLASS_NAME, "My First Proper Thing 2", WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hThisInst, NULL ))) return 0; ShowWindow(hwnd, nWinMode); UpdateWindow(hwnd); SetFocus(hwnd); ShowCursor(0); main_window_handle = hwnd; LoadBitmap(&colour_bmp_file,"texture2.bmp"); LoadBitmap(&height_bmp_file,"heightd2.bmp"); if (!DD_Init(hwnd)) { DestroyWindow(hwnd); return 0; } ofstream fout("myfile.dem"); int i,j,n,index = 0; for ( i = 0; i < 512; i++) { for ( j = 0; j < 512; j++) { // heights[i][j] = height_bmp_file.buffer[index++] * 8; // fout << heights[i][j] << " "; heights[i][j] = 0; } fout << "\n"; } index = 0; for ( i = 0; i < 1024; i++) { for ( j = 0; j < 1024; j++) { if ((colours[i][j] = colour_bmp_file.buffer[index++])>250) colours[i][j] = 250; } fout << "\n"; } LoadDEM(); fout.close(); LoadDEM(); while (1) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message == WM_QUIT) { DD_Shutdown(); break; } TranslateMessage(&msg); DispatchMessage(&msg); } else { memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); while ( lpddsback->Lock(NULL,&ddsd,DDLOCK_SURFACEMEMORYPTR,NULL) != DD_OK); video_buffer = (UCHAR *) ddsd.lpSurface; memset(video_buffer,0,SCREEN_WIDTH*SCREEN_HEIGHT); GameMain(); lpddsback->Unlock(NULL); sprintf(buffer,"%d",poo); if (lpddsback->GetDC(&hdc) == DD_OK) { SetBkColor(hdc, RGB(0, 0, 255)); SetTextColor(hdc, RGB(255, 255, 0)); TextOut(hdc, 0, 0, buffer, lstrlen(buffer)); lpddsback->ReleaseDC(hdc); } //Z+=10; if (Z>3840.0) Z -=3840.0; while (TRUE) { ddrval = lpddsprimary->Flip(NULL, 0); if (ddrval == DD_OK) break; if (ddrval == DDERR_SURFACELOST) { ddrval = lpddsprimary->Restore(); if (ddrval != DD_OK) break; } if (ddrval != DDERR_WASSTILLDRAWING) break; } if (KEY_DOWN(VK_ESCAPE)) { DD_Shutdown(); PostMessage(main_window_handle,WM_CLOSE,0,0); } } } DD_Shutdown(); return(msg.wParam); }
int DD_Init(HWND hwnd) { int index; if(DirectDrawCreate(NULL,&pdd,NULL)!=DD_OK) { DD_Shutdown(); return 0; } if(pdd->QueryInterface(IID_IDirectDraw4, (LPVOID *) & lpdd ) != DD_OK) { DD_Shutdown(); return 0; } if(lpdd->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN ) != DD_OK ) { DD_Shutdown(); return 0; } if(lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,0,0) != DD_OK) { DD_Shutdown(); return 0; } ZeroMemory(&ddsd, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL) != DD_OK) { DD_Shutdown(); return 0; } ddscaps.dwCaps = DDSCAPS_BACKBUFFER; if (lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback) != DD_OK) { DD_Shutdown(); return 0; } memset(colour_palette,0,256*sizeof(PALETTEENTRY)); for(index = 0; index < 256; index++ ) { colour_palette[index].peRed = colour_bmp_file.palette[index].peBlue ; colour_palette[index].peBlue = colour_bmp_file.palette[index].peRed; colour_palette[index].peGreen = colour_bmp_file.palette[index].peGreen; colour_palette[index].peFlags = PC_NOCOLLAPSE; } if(lpdd->CreatePalette((DDPCAPS_8BIT | DDPCAPS_INITIALIZE),colour_palette,&lpddpal,NULL)!=DD_OK) { DD_Shutdown(); return 0; } lpddsprimary->SetPalette(lpddpal); // lpddsback->SetPalette(lpddpal); return 1; } // end DD_Init
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode) { HWND hwnd; MSG msg; HDC hdc; char buffer[20]; // Un-comment these variables if a frame rate is to be calculated // clock_t start, finish; // double duration; // Obtain paramaters from command line arguments char height_file[40]=""; char colour_file[40]=""; int resolution; sscanf(lpszArgs, "%s %s %d %d %d %d", colour_file, height_file, &MAP_HEIGHT, &MAP_WIDTH, &MAX_STEPS, &resolution); // Set screen resolution based on command line argument switch(resolution) { case _320x240: SCREEN_WIDTH = 320; SCREEN_HEIGHT = 240; break; case _640x480: SCREEN_WIDTH = 640; SCREEN_HEIGHT = 480; break; case _800x600: SCREEN_WIDTH = 800; SCREEN_HEIGHT = 600; break; case _1024x768: SCREEN_WIDTH = 1024; SCREEN_HEIGHT = 768; break; case _1280x1024: SCREEN_WIDTH = 1280; SCREEN_HEIGHT = 1024; break; default: return 0; } // Check Map Size argument values if (!MAP_HEIGHT || !MAP_WIDTH) return 0; // Allocate Memory for Height and Colour Maps heights = new int*[MAP_HEIGHT]; colours = new UCHAR*[MAP_HEIGHT]; for (int index = 0; index < MAP_HEIGHT; index++ ) heights[index] = new int[MAP_WIDTH], colours[index] = new UCHAR[MAP_WIDTH]; // Set initial map heights to zero for ( int i = 0; i < MAP_HEIGHT; i++) for ( int j = 0; j < MAP_WIDTH; j++) heights[i][j] = 0; // Load in Colour Map if(strlen(colour_file)==0) return 0; else LoadBitmap(colour_file); // Load in Height Map if(strlen(height_file)==0) return 0; else LoadDEM(height_file); // Set up initial viewing paramters HALF_SCREEN_WIDTH = SCREEN_WIDTH / 2; ANGLE_RANGE = (SCREEN_WIDTH * 360 / FOV ); pitch = -(SCREEN_HEIGHT / 2); toRadians = 2.0 * PIE / (double) ANGLE_RANGE; // Initialise Application Window if ( !(hwnd = InitWindows(hThisInst, hPrevInst, nWinMode, hwnd)) ) return 0; // Initialise DirectDraw if (!DD_Init(hwnd)) { DestroyWindow(hwnd); return 0; } // Enter main loop // 1. Deal with windows events // 2. Do asynchronous processing while (1) { // Check windows messages if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message == WM_QUIT) { DD_Shutdown(); break; } TranslateMessage(&msg); DispatchMessage(&msg); } else { // Main Routines // Obtain pointer to display buffer memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); while ( lpddsback->Lock(NULL,&ddsd,DDLOCK_SURFACEMEMORYPTR,NULL) != DD_OK); video_buffer = (UCHAR *) ddsd.lpSurface; // Clear screen memset(video_buffer,250,SCREEN_WIDTH*SCREEN_HEIGHT); // start = clock(); // Render current view of landscape RenderView(); // finish = clock(); /* Un-comment this section if screen shots are wanted To save a screenshot press F12 if (KEY_DOWN(VK_F12)) { SaveScreen(); } */ // Release pointer to display memory lpddsback->Unlock(NULL); // Flip back surface to front while (TRUE) { ddrval = lpddsprimary->Flip(NULL, 0); if (ddrval == DD_OK) break; if (ddrval == DDERR_SURFACELOST) { ddrval = lpddsprimary->Restore(); if (ddrval != DD_OK) break; } if (ddrval != DDERR_WASSTILLDRAWING) break; } // Check to see if user pressed Escape if (KEY_DOWN(VK_ESCAPE)) { // Shut down DirectDraw DD_Shutdown(); // Send Quit event to Windows event handler PostMessage(main_window_handle,WM_CLOSE,0,0); } } // Un-comment these for frame rates // buffer contains the current frame rate // duration = (double) (finish - start); // sprintf(buffer,"%2.4f", (1000.0 / (float)(finish - start))); } // We should never get here return(msg.wParam); }
int FreakOut::Game_Main(void *parms,HWND main_window_handle) { // this is the workhorse of your game it will be called // continuously in real-time this is like main() in C // all the calls for you game go here! char buffer[80]; // used to print text // what state is the game in? if (game_state == GAME_STATE_INIT) { // initialize everything here graphics DD_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP); // seed the random number generator // so game is different each play srand(Start_Clock()); // set the paddle position here to the middle bottom paddle_x = PADDLE_START_X; paddle_y = PADDLE_START_Y; // set ball position and velocity ball_x = 8+rand()%(SCREEN_WIDTH-16); ball_y = BALL_START_Y; ball_dx = -4 + rand()%(8+1); ball_dy = 6 + rand()%2; // transition to start level state game_state = GAME_STATE_START_LEVEL; } // end if //////////////////////////////////////////////////////////////// else if (game_state == GAME_STATE_START_LEVEL) { // get a new level ready to run // initialize the blocks Init_Blocks(); // reset block counter blocks_hit = 0; // transition to run state game_state = GAME_STATE_RUN; } // end if /////////////////////////////////////////////////////////////// else if (game_state == GAME_STATE_RUN) { // start the timing clock Start_Clock(); // clear drawing surface for the next frame of animation Draw_Rectangle(0,0,SCREEN_WIDTH-1, SCREEN_HEIGHT-1,200); // move the paddle if (KEY_DOWN(VK_RIGHT)) { // move paddle to right paddle_x+=8; // make sure paddle doesn't go off screen if (paddle_x > (SCREEN_WIDTH-PADDLE_WIDTH)) paddle_x = SCREEN_WIDTH-PADDLE_WIDTH; } // end if else if (KEY_DOWN(VK_LEFT)) { // move paddle to right paddle_x-=8; // make sure paddle doesn't go off screen if (paddle_x < 0) paddle_x = 0; } // end if // draw blocks Draw_Blocks(); // move the ball ball_x+=ball_dx; ball_y+=ball_dy; // keep ball on screen, if the ball hits the edge of // screen then bounce it by reflecting its velocity if (ball_x > (SCREEN_WIDTH - BALL_SIZE) || ball_x < 0) { // reflect x-axis velocity ball_dx=-ball_dx; // update position ball_x+=ball_dx; } // end if // now y-axis if (ball_y < 0) { // reflect y-axis velocity ball_dy=-ball_dy; // update position ball_y+=ball_dy; } // end if else // penalize player for missing the ball if (ball_y > (SCREEN_HEIGHT - BALL_SIZE)) { // reflect y-axis velocity ball_dy=-ball_dy; // update position ball_y+=ball_dy; // minus the score score-=100; } // end if // next watch out for ball velocity getting out of hand if (ball_dx > 8) ball_dx = 8; else if (ball_dx < -8) ball_dx = -8; // test if ball hit any blocks or the paddle Process_Ball(); // draw the paddle and shadow Draw_Rectangle(paddle_x-8, paddle_y+8, paddle_x+PADDLE_WIDTH-8, paddle_y+PADDLE_HEIGHT+8,0); Draw_Rectangle(paddle_x, paddle_y, paddle_x+PADDLE_WIDTH, paddle_y+PADDLE_HEIGHT,PADDLE_COLOR); // draw the ball Draw_Rectangle(ball_x-4, ball_y+4, ball_x+BALL_SIZE-4, ball_y+BALL_SIZE+4, 0); Draw_Rectangle(ball_x, ball_y, ball_x+BALL_SIZE, ball_y+BALL_SIZE, 255); // draw the info sprintf(buffer,"F R E A K O U T Score %d Level %d",score,level); Draw_Text_GDI(buffer, 8,SCREEN_HEIGHT-16, 127); // flip the surfaces DD_Flip(); // sync to 33ish fps Wait_Clock(30); // check of user is trying to exit if (KEY_DOWN(VK_ESCAPE)) { // send message to windows to exit PostMessage(main_window_handle, WM_DESTROY,0,0); // set exit state game_state = GAME_STATE_SHUTDOWN; } // end if } // end if /////////////////////////////////////////////////////////////// else if (game_state == GAME_STATE_SHUTDOWN) { // in this state shut everything down and release resources DD_Shutdown(); // switch to exit state game_state = GAME_STATE_EXIT; } // end if // return success return(1); } // end Game_Main