Archived

This topic is now archived and is closed to further replies.

Someone Help me please!

This topic is 5649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having a really hard time with my project. I am trying to get response from the keyboard but I can''t! I am not even sure if my program''s main loop is working. Can someone please just give me a few minutes of help. Here is my program. I was trying to get the cursor to move as the keys are pressed up and down. But... they are not. Can someone help me use KEYDOWN ... oh goodness... /code: /*--------------------------------------------------------------------------*/ // SUPER SMASH PONG // Programmed by Jesse Barksdale // Art by Matthew Herms // June 2002 /*--------------------------------------------------------------------------*/ #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif //------ Include Files ------// #include "stdafx.h" #define INITGUID #include <ddraw.h> #include <dinput.h> //------ Image Loading and Initialization Flags ------// BOOL bInit=FALSE; //------ DirectDraw Pointers ------// LPDIRECTDRAW4 lpDD=NULL; // DirectDraw object LPDIRECTDRAWSURFACE4 lpDDSPrimary=NULL; // DirectDraw primary surface LPDIRECTDRAWSURFACE4 lpDDSBack=NULL; // DirectDraw back buffer LPDIRECTDRAWCLIPPER lpClip=NULL; // Directray Clipper Object //------ DirectInput Object and Devices ------// //------ Game surfaces ------------------// LPDIRECTDRAWSURFACE4 lpTitleScreen=NULL; // Titlescreen background LPDIRECTDRAWSURFACE4 lpMenuCursor=NULL; // The menu cursor //------ Window Class Information ------// static char szClass[] = "XmplHr2Class"; static char szCaption[] = "SUPER SMASH PONG"; //------ Menu Variables ------------// #define MENU_MAIN_START 1 //main menu selections #define MENU_MAIN_OPTIONS 2 #define MENU_MAIN_DIRECTIONS 3 #define MENU_MAIN_ABOUT 4 #define MENU_MAIN_EXIT 5 //coordinates to draw the menu cursor #define MENU_START_X 32 #define MENU_START_Y 230 #define MENU_STYLE_ONEONONE 1 //game style menu selections #define MENU_STYLE_ONEONCOMP 2 #define MENU_PADDLE_NORMAL 0 #define MENU_PADDLE_FAST 1 #define MENU_PADDLE_STRONG 2 int game_quit = 0; // Tells whether or not the game has been quit, 1 = game has been quit int menu_main_cursor = MENU_MAIN_START; //keeps track of the location of the main menu cursor int menu_game_style = MENU_STYLE_ONEONONE; //keeps track of the cursor location for selecting game style (1on1, 1onCOMP) int game_style; //holds info telling whether the game is 1on1 or 1onCOMP (1 = 1on1, 2 = 1onCOMP) int menu_paddle_select = MENU_PADDLE_NORMAL; int player_one_paddle;//holds the player paddle types int player_two_paddle; int menu_options_cursor = 1; //keeps track of the options cursor location int player_one_move=0; // Keeps track of all movement (throughout menus, game, etc) up and down buttons int player_two_move=0; // Keeps track of all movement of player 2 like above A and Z buttons int player_one_cursor_count =0; //tracks cursor movement int player_two_cursor_count =0; //------ Game variables ------------// #define GAME_STATE_MAIN_MENU 0 //the main menu with all the selections #define GAME_STATE_OPTIONS 1 //options screen #define GAME_STATE_ABOUT 2 //about us screen #define GAME_STATE_DIRECTIONS 3 //directions screen #define GAME_STATE_EXIT 4 //shows an exit screen with info about us, etc #define GAME_STATE_PLAY_STYLE 5 //1on1 or 1onCOMP? #define GAME_STATE_PADDLE_SELECT 6 //choose your paddle #define GAME_STATE_PLAY 7 //the actual game #define GAME_STATE_SCORE 8 //the end game score screen int game_state = GAME_STATE_MAIN_MENU; int current_level = 1;//The current level being played int player_one_score = 0;//player scores for the current game. Scores are reset every level int player_two_score = 0; //------ Error Return String ------// const char *ErrStr; //------ Error Messages ------// const char Err_Reg_Class[] = "Error Registering Window Class"; const char Err_Create_Win[] = "Error Creating Window"; const char Err_DirectDrawCreate[] = "DirectDrawCreate FAILED"; const char Err_Query[] = "QueryInterface FAILED"; const char Err_Coop[] = "SetCooperativeLevel FAILED"; const char Err_CreateClip[] = "CreateClip FAILED"; const char Err_CreatePrimSurf[] = "CreateSurface Primary FAILED"; const char Err_CreateBackSurf[] = "CreateSurface Back FAILED"; const char Err_LoadTitleScreenBMP[] = "Error Loading TitleScreen Image"; const char Err_LoadMenuCursorBMP[] = "Error Loading MenuCursor Image"; const char Err_DirectInputCreate[] = "Error Creating DI Keyboard"; const char Err_CreateDevice[] = "Error Creating DI Device"; //------ Cleanup Function to Release Objects ------// #define SafeRelease(x) if (x) { x->Release(); x=NULL; } void Cleanup(void) { // release the interfaces SafeRelease(lpTitleScreen); SafeRelease(lpDDSPrimary); SafeRelease(lpClip); SafeRelease(lpDD); // display error if one thrown if (ErrStr) MessageBox(NULL, ErrStr, szCaption, MB_OK); } //------ Function to Load a Bitmap into a DirectDraw Surface ------// LPDIRECTDRAWSURFACE4 bitmap_surface(LPCTSTR file_name) { HDC hdc; HBITMAP bit; LPDIRECTDRAWSURFACE4 surf; // load the interface bitmap bit=(HBITMAP) LoadImage(NULL,file_name,IMAGE_BITMAP,0,0, LR_DEFAULTSIZE|LR_LOADFROMFILE); if (!bit) // failed to load, return failure to caller return NULL; // get bitmap dimensions BITMAP bitmap; GetObject( bit, sizeof(BITMAP), &bitmap ); int surf_width=bitmap.bmWidth; int surf_height=bitmap.bmHeight; // create surface HRESULT ddrval; DDSURFACEDESC2 ddsd; ZeroMemory(&ddsd,sizeof(ddsd)); ddsd.dwSize = sizeof(DDSURFACEDESC2); ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT ; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN|DDSCAPS_SYSTEMMEMORY; ddsd.dwWidth = surf_width; ddsd.dwHeight = surf_height; // attempt to create surface ddrval=lpDD->CreateSurface(&ddsd,&surf,NULL); // created ok? if (ddrval!=DD_OK) { // no, release the bitmap and return failure to caller DeleteObject(bit); return NULL; } else { // yes, get a DC for the surface surf->GetDC(&hdc); // generate a compatible DC HDC bit_dc=CreateCompatibleDC(hdc); // blit the interface to the surface SelectObject(bit_dc,bit); BitBlt(hdc,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY); // release the DCs surf->ReleaseDC(hdc); DeleteDC(bit_dc); } // clear bitmap DeleteObject(bit); // return pointer to caller return surf; } //------ Draw the Bitmap to the Primary Surface ------// void DrawImage() { } //------ Windows Message Handler ------// LRESULT CALLBACK WindowProc(HWND hWnd, unsigned uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_PAINT: PAINTSTRUCT ps; BeginPaint(hWnd, &ps); DrawImage(); EndPaint(hWnd, &ps); break; case WM_DESTROY: Cleanup(); PostQuitMessage(0); break; case WM_KEYDOWN: switch (wParam) { case VK_UP: { player_one_move +=1; break; } case VK_DOWN: { player_one_move -=1; break; } case VK_ESCAPE: { // exit the program on escape DestroyWindow(hWnd); break; } // Process other non-character keystrokes. default: break; } default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0L; } //------ Function to Initialize DirectDraw, DirectInput and the Application ------// static BOOL Init(HINSTANCE hInstance, int nCmdShow) { WNDCLASS wc; HRESULT hRet; DDSURFACEDESC2 ddsd; DDSCAPS2 ddscaps; LPDIRECTDRAW pDD; // Set up and register window class wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC) WindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = sizeof(DWORD); wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = szClass; if (!RegisterClass(&wc)) { ErrStr=Err_Reg_Class; return FALSE; } // Get dimensions of display int ScreenWidth = GetSystemMetrics(SM_CXSCREEN); int ScreenHeight = GetSystemMetrics(SM_CYSCREEN); // Create a window and display HWND hWnd; hWnd = CreateWindow(szClass, // class szCaption, // caption WS_VISIBLE|WS_POPUP, // style 0, // left 0, // top ScreenWidth, // width ScreenHeight, // height NULL, // parent window NULL, // menu hInstance, // instance NULL); // parms if (!hWnd) { ErrStr=Err_Create_Win; return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // Create the main DirectDraw object hRet = DirectDrawCreate(NULL, &pDD, NULL); if (hRet != DD_OK) { ErrStr=Err_DirectDrawCreate; return FALSE; } // Fetch DirectDraw4 interface hRet = pDD->QueryInterface(IID_IDirectDraw4, (LPVOID *) & lpDD); if (hRet != DD_OK) { ErrStr=Err_Query; return FALSE; } // Set our cooperative level hRet = lpDD->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN ); if (hRet != DD_OK) { ErrStr=Err_Coop; return FALSE; } // Set the display mode hRet = lpDD->SetDisplayMode( 640, 480, 16, 0, 0); if (hRet !=DD_OK) { return FALSE; } // Create the primary surface with 1 back buffer 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; hRet = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL ); if (hRet!=DD_OK) { ErrStr=Err_CreatePrimSurf; return FALSE; } // Fetch back buffer interface ddscaps.dwCaps=DDSCAPS_BACKBUFFER; hRet=lpDDSPrimary->GetAttachedSurface(&ddscaps,&lpDDSBack); if (hRet!=DD_OK) { ErrStr=Err_CreateBackSurf; return FALSE; } // flag initialization as completed bInit=TRUE; // load the game bitmaps lpTitleScreen=bitmap_surface("TitleScreen.bmp"); if (!lpTitleScreen) { ErrStr=Err_LoadTitleScreenBMP; return FALSE; } lpMenuCursor=bitmap_surface("MenuCursor.bmp"); if (!lpMenuCursor) { ErrStr=Err_LoadMenuCursorBMP; return FALSE; } return TRUE; } //------ Application Loop ------// int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; // windows message structure // initialize the application, exit on failure if (!Init(hInstance, nCmdShow)) { Cleanup(); return FALSE; } //================================================================================// //------------------------------------------------------------------------// //-------------------------------MAIN GAME LOOP-----------------------------------// //------------------------------------------------------------------------// //================================================================================// while (game_quit==0) { switch (game_state) { case (GAME_STATE_MAIN_MENU): // Main menu { // draw the background image lpDDSBack->Blt(NULL,lpTitleScreen,NULL,DDBLT_WAIT,NULL); //////////////////////////////////////////////////////////////////////// ///// MAIN MENU CURSOR //////////////////////////////////////////////////////////////////////// switch (menu_main_cursor) // Keeps track of the main menu cursor { case (MENU_MAIN_START): { lpDDSBack->BltFast(40,230,lpMenuCursor,NULL,DDBLTFAST_WAIT); // Draw Cursor if(player_one_move > player_one_cursor_count) // See if cursor moved up { menu_main_cursor = MENU_MAIN_EXIT; } if(player_one_move > player_one_cursor_count) // See if cursor moved down { menu_main_cursor ++; } break; } case (MENU_MAIN_OPTIONS): { lpDDSBack->BltFast(40,267,lpMenuCursor,NULL,DDBLTFAST_WAIT); // Draw Cursor break; } case (MENU_MAIN_ABOUT): { lpDDSBack->BltFast(40,297,lpMenuCursor,NULL,DDBLTFAST_WAIT); // Draw Cursor break; } case (MENU_MAIN_DIRECTIONS): { lpDDSBack->BltFast(40,336,lpMenuCursor,NULL,DDBLTFAST_WAIT); // Draw Cursor break; } case (MENU_MAIN_EXIT): { lpDDSBack->BltFast(40,374,lpMenuCursor,NULL,DDBLTFAST_WAIT); // Draw Cursor break; } } // End main_menu_cursor switch //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// break; } case (GAME_STATE_OPTIONS): { break; } case (GAME_STATE_ABOUT): { break; } case (GAME_STATE_DIRECTIONS): { break; } case (GAME_STATE_EXIT): { break; } } // End game_state switch // Render the game scene lpDDSPrimary->Flip(NULL,DDFLIP_WAIT); //================================================================================// //================================================================================// //================================================================================// // handle the message loop till we exit player_one_cursor_count=player_one_move; // Set equal so we can see if a button was pressed player_two_cursor_count=player_two_move; while (GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } } // exit returning final message return (msg.wParam); } thanks...

Share this post


Link to post
Share on other sites
P.S.

I have gotten my display stuff working correctly but I really need help with the input stuff. I don''t think any of my input is getting to my program loop. Could someone please advise me on who to do some good input?

Thanks for the time

Share this post


Link to post
Share on other sites
I've heard from somewhere that you shouldn't use wParam, use message. Not sure though...

I meant switch (wParam) should be switch (uMsg). Just a guess

______________________________________________
You know your game is in trouble when your AI says, in a calm, soothing voice, "I'm afraid I can't let you do that, Dave"

[edited by - Zeraan on June 25, 2002 10:03:27 PM]

Share this post


Link to post
Share on other sites