Jump to content

Hello there people. I'm currently developing a game for one of my classes. It's a 2D rpg game inspired by the Final Fantasy series. (obviously the old ones from the snes) I'm having trouble figuring out how to check for a key press. The key is question is the C key. I need to check for the C key being pressed and if it is pressed I need to load a credits .BMP image I created. I already have everything set up and the game loads the main .BMP image for my game (the opening title screen) I'm lost here. I have already incorporated a check for the ESC key being pressed and if it is pressed the game ends and closes. I just don't know how to check for the C key. I thought it might be similar but I can't get it to work. This is all contained in one .cpp file for now. here is my code.

 //Ryan C. Jones//2D game project game loop//working title TBD//instructor: Janisz//August 15th 2015 #include <windows.h>#include <d3d9.h>#include <d3dx9.h>#include <iostream>using namespace std; //program valuesconst string APPTITLE = "Portcullis of the Lattace";const int SCREENW = 1024;const int SCREENH = 768; //key macro#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)#define KEY_PRESS(vk_code) ((GetAsyncKeyState(vk_code) & 0x43) ? 1 : 0) //Direct3D objectsLPDIRECT3D9 d3d = NULL;LPDIRECT3DDEVICE9 d3ddev = NULL;LPDIRECT3DSURFACE9 backbuffer = NULL;LPDIRECT3DSURFACE9 surface = NULL; bool gameOver = false;bool switchPage = false; // Game initialization functionbool Game_Init(HWND window){//initialize Direct3Dd3d = Direct3DCreate9(D3D_SDK_VERSION);if (!d3d){MessageBox(window, "Error initializing Direct3D", "Error", MB_OK);return false;} //set Direct3D presentation parametersD3DPRESENT_PARAMETERS d3dpp;ZeroMemory(&d3dpp, sizeof(d3dpp));d3dpp.Windowed = TRUE;d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;d3dpp.BackBufferCount = 1;d3dpp.BackBufferWidth = SCREENW;d3dpp.BackBufferHeight = SCREENH;d3dpp.hDeviceWindow = window; //create Direct3D deviced3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev); if (!d3ddev){MessageBox(window, "Error creating Direct3D device", "Error", MB_OK);return false;} //clear the backbuffer to blackd3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); //create surfaceHRESULT result = d3ddev->CreateOffscreenPlainSurface(SCREENW,            //width of the surfaceSCREENH,            //height of the surfaceD3DFMT_X8R8G8B8,    //surface formatD3DPOOL_DEFAULT,    //memory pool to use&surface,           //pointer to the surfaceNULL);              //reserved (always NULL) if (result != D3D_OK){return false;} //load surface from file into newly created surfaceresult = D3DXLoadSurfaceFromFile(surface,         //destination surfaceNULL,            //destination paletteNULL,            //destination rectangle"PortcullisOfTheLattace(MenuImage).bmp",     //source filenameNULL,            //source rectangleD3DX_DEFAULT,    //controls how image is filtered0,               //for transparency (0 for none)NULL);           //source image info (usually NULL) //make sure file was loaded okayif (result != D3D_OK){return false;} return true;} // Game update functionvoid Game_Run(HWND hwnd){//make sure the Direct3D device is validif (!d3ddev){return;} //create pointer to the back bufferd3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); //start renderingif (d3ddev->BeginScene()){//draw surface to the backbufferd3ddev->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE); //stop renderingd3ddev->EndScene();d3ddev->Present(NULL, NULL, NULL, NULL);} //check for escape key (to exit program)if (KEY_DOWN(VK_ESCAPE)){PostMessage(hwnd, WM_DESTROY, 0, 0);}} // Game shutdown functionvoid Game_End(HWND hwnd){if (surface){surface->Release();}if (d3ddev){d3ddev->Release();}if (d3d){d3d->Release();}} // Windows event handling functionLRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){switch (msg){case WM_DESTROY:gameOver = true;break;}return DefWindowProc(hWnd, msg, wParam, lParam);} // Windows entry point functionint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){MessageBox(NULL, "Welcome to 'Portcullis of the Lattace', A 2D adventure RPG!\n\n\nBy Ryan C. Jones", "POTL welcome message", MB_OK | MB_ICONINFORMATION); WNDCLASSEX wc;HWND hwnd;MSG message; //initialize window settingswc.cbSize = sizeof(WNDCLASSEX);wc.style = CS_HREDRAW | CS_VREDRAW;wc.lpfnWndProc = (WNDPROC)WinProc;wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.lpszMenuName = NULL;wc.hIcon = NULL;wc.hIconSm = NULL;wc.hInstance = hInstance;wc.hCursor = LoadCursor(NULL, IDC_ARROW);wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wc.lpszClassName = "MainWindowClass"; if (!RegisterClassEx(&wc)){return FALSE;} //create a new windowhwnd = CreateWindow("MainWindowClass", APPTITLE.c_str(),WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,SCREENW, SCREENH, NULL, NULL, hInstance, NULL); //was there an error creating the window?if (hwnd == 0){return 0;} //display the windowShowWindow(hwnd, nCmdShow);UpdateWindow(hwnd); //initialize the gameif (!Game_Init(hwnd)){return FALSE;}  // main message loopwhile (!gameOver){if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE)){TranslateMessage(&message);DispatchMessage(&message);} //process game loop Game_Run(hwnd);} return message.wParam;} 
Link to comment
https://linustechtips.com/topic/435888-c-game-loop-help/
Share on other sites

Link to post
Share on other sites

Add the WM_KEYDOWN and WM_KEYUP messages to your wndproc. Then you can check the specific key that was pressed by checking the WPARAM.

 

edit: @Vulpix_Dev Don't forget to follow your topics with buttons in the top right.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
https://linustechtips.com/topic/435888-c-game-loop-help/#findComment-5845267
Share on other sites

Link to post
Share on other sites

Add the WM_KEYDOWN and WM_KEYUP messages to your wndproc. Then you can check the specific key that was pressed by checking the WPARAM.

 

edit: @Vulpix_Dev Don't forget to follow your topics with buttons in the top right.

For sure. I fell asleep last night that's why I didn't reply. Thank you. I'll try that and see what my results are.

Link to comment
https://linustechtips.com/topic/435888-c-game-loop-help/#findComment-5847425
Share on other sites

Link to post
Share on other sites

For sure. I fell asleep last night that's why I didn't reply. Thank you. I'll try that and see what my results are.

Its all good. Im happy to help.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
https://linustechtips.com/topic/435888-c-game-loop-help/#findComment-5847548
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×