Original Post
I originally ran this program before and it worked. So I decided to go back through all the programs in this book I'm trying to learn from and now I can't get the current program to work and the original doesn't either. I'm about to rip my hair out!! Anyway here is the code, I've gone over it twice and I can't figure it out. Thanks. /* Beginning Game Programming, Third Edition MyDirectX.h */ #pragma once //Header files #define WIN32_EXTRA_LEAN #define DIRECTINPUT_VERSION 0x0800 #include #include #include #include #include #include #include #include using namespace std; //Libraries #pragma comment(lib, "winmm.lib") #pragma comment(lib, "user32.lib") #pragma comment(lib, "gdi32.lib") #pragma comment(lib, "dxguid.lib") #pragma comment(lib, "d3d9.lib") #pragma comment(lib, "d3dx9.lib") #pragma comment(lib, "dinput8.lib") #pragma comment(lib, "xinput.lib") //Program values extern const string APPTITLE; extern const int SCREENW; extern const int SCREENH; extern bool gameover; //macro to detect key presses //***ADDED #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) //Direct3D objects extern LPDIRECT3D9 d3d; extern LPDIRECT3DDEVICE9 d3ddev; extern LPDIRECT3DSURFACE9 backbuffer; extern LPD3DXSPRITE spriteobj; //Direct3D functions bool Direct3D_Init(HWND hwnd, int width, int height, bool fullscreen); void Direct3D_Shutdown(); LPDIRECT3DSURFACE9 LoadSurface(string filename); void DrawSurface(LPDIRECT3DSURFACE9 dest, float x, float y, LPDIRECT3DSURFACE9 source); D3DXVECTOR2 GetBitmapSize(string filename); LPDIRECT3DTEXTURE9 LoadTexture(string filename, D3DCOLOR transcolor = D3DCOLOR_XRGB(0, 0, 0)); //DirectInput objects, devices, and states extern LPDIRECTINPUT8 dinput; extern LPDIRECTINPUTDEVICE8 dimouse; extern LPDIRECTINPUTDEVICE8 dikeyboard; extern DIMOUSESTATE mouste_state; extern XINPUT_GAMEPAD controllers[4]; //DirectInput functions bool DirectInput_Init(HWND); void DirectInput_Update(); void DirectInput_Shutdown(); int Key_Down(int); int Mouse_Button(int); int Mouse_X(); int Mouse_Y(); void XInput_Vibrate(int contNum = 0, int amount = 65535); bool XInput_Controller_Found(); //Game functions bool Game_Init(HWND window); void Game_Run(HWND window); void Game_End(); /* Beginning Game Programming, Third Edition MyWindows.cpp */ #include "MyDirectX.h" using namespace std; bool gameover = false; /** **Windows event handler **/ LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_DESTROY: gameover = true; PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, msg, wParam, lParam); } /** **Windows entry point **/ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { //Initialize window settings WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); 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(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = APPTITLE.c_str(); wc.hIconSm = NULL; RegisterClassEx(&wc); //Create a new window HWND window = CreateWindow(APPTITLE.c_str(), APPTITLE.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, SCREENW, SCREENH, NULL, NULL, hInstance, NULL); if(window == 0) return 0; //Display the window ShowWindow(window, nCmdShow); UpdateWindow(window); //Initialize the game if(!Game_Init(window)) return 0; //Main message loop MSG message; while(!gameover) { if(PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&message); DispatchMessage(&message); } //Process game loop Game_Run(window); } //Shutdown Game_End(); return message.wParam; } /* Beginning Game Programming, Third Edition MyGame.cpp */ #include "MyDirectX.h" using namespace std; const string APPTITLE = "Transparent Sprite Demo"; const int SCREENW = 1024; const int SCREENH = 768; LPDIRECT3DTEXTURE9 image_colorkey = NULL; LPDIRECT3DTEXTURE9 image_alpha = NULL; LPDIRECT3DTEXTURE9 image_notrans = NULL; /** **Game initialization **/ bool Game_Init(HWND window) { //Initialize Direct3D if(!Direct3D_Init(window, SCREENW, SCREENH, false)); { MessageBox(0, "Error initializing Direct3D", "Error", 0); return false; } //Initialize DirectInput if(!DirectInput_Init(window)); { MessageBox(0, "Error initializing DirectInput", "Error", 0); return false; } //Load non-transparent image image_notrans = LoadTexture("shuttle_notrans.bmp"); if(!image_notrans) return false; //Load color-keyed transparent image image_colorkey = LoadTexture("shuttle_colorkey.bmp", D3DCOLOR_XRGB(255, 0, 255)); if(!image_colorkey) return false; //Load alpha transparent image image_alpha = LoadTexture("shuttle_alpha.tga"); if(!image_alpha) return false; //You can use this function to get the size of the image D3DXVECTOR2 size = GetBitmapSize("shuttle_alpha.tga"); return true; } /** **Game update **/ void Game_Run(HWND window) { //Make sure the Direct3D device is valid if(!d3ddev) return; //Update input devices DirectInput_Update(); //Clear the scene d3ddev -> Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 100), 1.0f, 0); //Start rendering if(d3ddev -> BeginScene()) { //Start drawing spriteobj -> Begin(D3DXSPRITE_ALPHABLEND); //Draw the sprite D3DXVECTOR3 pos1(10, 10, 0); spriteobj -> Draw(image_notrans, NULL, NULL, &pos1, D3DCOLOR_XRGB(255, 255, 255)); D3DXVECTOR3 pos2(350, 10, 0); spriteobj -> Draw(image_colorkey, NULL, NULL, &pos2, D3DCOLOR_XRGB(255, 255, 255)); D3DXVECTOR3 pos3(700, 10, 0); spriteobj -> Draw(image_alpha, NULL, NULL, &pos3, D3DCOLOR_XRGB(255, 255, 255)); //Stop drawing spriteobj -> End(); //Stop rendering d3ddev -> EndScene(); d3ddev -> Present(NULL, NULL, NULL, NULL); } //Escape key exits if(Key_Down(DIK_ESCAPE)) gameover = true; //Controller Back button also exits if(controllers[0].wButtons & XINPUT_GAMEPAD_BACK) gameover = true; } /** **Game shutdown **/ void Game_End() { //Free memory and shutdown image_notrans -> Release(); image_colorkey -> Release(); image_alpha -> Release(); DirectInput_Shutdown(); Direct3D_Shutdown(); } /* Beginning Game Programming, Third Edition MyDirectX.cpp */ #include "MyDirectX.h" #include using namespace std; //Direct3D variables LPDIRECT3D9 d3d = NULL; LPDIRECT3DDEVICE9 d3ddev = NULL; LPDIRECT3DSURFACE9 backbuffer = NULL; LPD3DXSPRITE spriteobj; //DirectInput variables LPDIRECTINPUT8 dinput = NULL; LPDIRECTINPUTDEVICE8 dimouse = NULL; LPDIRECTINPUTDEVICE8 dikeyboard = NULL; DIMOUSESTATE mouse_state; char keys[256]; XINPUT_GAMEPAD controllers[4]; /** **Direct3D initialization **/ bool Direct3D_Init(HWND window, int width, int height, bool fullscreen) { //Initialize Direct3D d3d = Direct3DCreate9(D3D_SDK_VERSION); if(!d3d) return false; //Set Direct3D presentation parameters D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.hDeviceWindow = window; d3dpp.Windowed = (!fullscreen); d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.EnableAutoDepthStencil = 1; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; d3dpp.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.BackBufferWidth = width; d3dpp.BackBufferHeight = height; //Create Direct3D device d3d -> CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev); if(!d3ddev) return false; //Get a pointer to the backbuffer surface d3ddev -> GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); //Create sprite object D3DXCreateSprite(d3ddev, &spriteobj); return 1; } /** **Direct3D shutdown **/ void Direct3D_Shutdown() { if(spriteobj) spriteobj -> Release(); if(d3ddev) d3ddev -> Release(); if(d3d) d3d -> Release(); } /** **Draws a surface to the screen using StretchRect **/ void DrawSurface(LPDIRECT3DSURFACE9 dest, float x, float y, LPDIRECT3DSURFACE9 source) { //Get width/height from source surface D3DSURFACE_DESC desc; source -> GetDesc(&desc); //Create rects for drawing RECT source_rect = {0, 0, (long)desc.Width, (long)desc.Height}; RECT dest_rect = {(long)x, (long)y, (long)x + desc.Width, (long)y + desc.Height}; //Draw the source surface onto the dest d3ddev -> StretchRect(source, &source_rect, dest, &dest_rect, D3DTEXF_NONE); } /** **Loads a bitmap file into a surface **/ LPDIRECT3DSURFACE9 LoadSurface(string filename) { LPDIRECT3DSURFACE9 image = NULL; //Get Width and height from bitmap file D3DXIMAGE_INFO info; HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info); if(result != D3D_OK) return NULL; //Create surface result = d3ddev -> CreateOffscreenPlainSurface( info.Width, //Width of the surface info.Height, //Height of the surface D3DFMT_X8R8G8B8, //Surface format D3DPOOL_DEFAULT, //Memory pool to use ℑ, //Point to the surface NULL); //Reserved (always NULL) if(result != D3D_OK) return NULL; //Load surface from file into newly created surface result = D3DXLoadSurfaceFromFile( image, //Destination surface NULL, //Destination palette NULL, //Destination rectangle filename.c_str(), //Source filename NULL, //Source rectangle D3DX_DEFAULT, //Controls how image is filtered D3DCOLOR_XRGB(0, 0, 0), //For transparency (0 for none) NULL); //Source image info (usually NULL) //Make sure file was loaded okay if(result != D3D_OK) return NULL; return image; } /** **Get Bitmap size **/ D3DXVECTOR2 GetBitmapSize(string filename) { D3DXIMAGE_INFO info; D3DXVECTOR2 size = D3DXVECTOR2(0.0f, 0.0f); HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info); if(result == D3D_OK) size = D3DXVECTOR2((float)info.Width, (float)info.Height); else size = D3DXVECTOR2((float)info.Width, (float)info.Height); return size; } /** **Load texture **/ LPDIRECT3DTEXTURE9 LoadTexture(std::string filename, D3DCOLOR transcolor) { LPDIRECT3DTEXTURE9 texture = NULL; //Get width and height from bitmap file D3DXIMAGE_INFO info; HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info); if(result != D3D_OK) return NULL; //Create the new texture by loading a bitmap image file D3DXCreateTextureFromFileEx( d3ddev, //Direct3D device object filename.c_str(), //Bitmap filename info.Width, //Bitmap image width info.Height, //Bitmap image height 1, //Mip-map levels (1 for no chain) D3DPOOL_DEFAULT, //The type of surface (standard) D3DFMT_UNKNOWN, //Surface format(default) D3DPOOL_DEFAULT, //Memory class for the texture D3DX_DEFAULT, //Image filter D3DX_DEFAULT, //Mip filter transcolor, //Color key for transparency &info, //Bitmap file info (from loaded file) NULL, //Color palette &texture); //Destination texture //Make sure the bitmap texture was loaded correctly if(result != D3D_OK) return NULL; return texture; } /** **DirectInput initialization **/ bool DirectInput_Init(HWND hwnd) { //Initialize DirectInput Object DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dinput, NULL); //Initialize the keyboard dinput -> CreateDevice(GUID_SysKeyboard, &dikeyboard, NULL); dikeyboard -> SetDataFormat(&c_dfDIKeyboard); dikeyboard -> SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); dikeyboard -> Acquire(); //Initialize the mouse dinput -> CreateDevice(GUID_SysMouse, &dimouse, NULL); dimouse -> SetDataFormat(&c_dfDIMouse); dimouse -> SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); dimouse -> Acquire(); d3ddev -> ShowCursor(false); return true; } /** **DirectInput update **/ void DirectInput_Update() { //Update mouse dimouse->Poll(); if (!SUCCEEDED(dimouse->GetDeviceState(sizeof(DIMOUSESTATE),&mouse_state))) { //mouse device lose, try to re-acquire dimouse->Acquire(); } //update keyboard dikeyboard->Poll(); if (!SUCCEEDED(dikeyboard->GetDeviceState(256,(LPVOID)&keys))) { //keyboard device lost, try to re-acquire dikeyboard->Acquire(); } //Update controllers for(int i = 0; i < 4; i++) { ZeroMemory(&controllers, sizeof(XINPUT_STATE)); //Get the state of the controller XINPUT_STATE state; DWORD result = XInputGetState(i, &state); //Store state in global controllers array if(result == 0) controllers = state.Gamepad; } } /** **Return mouse x movement **/ int Mouse_X() { return mouse_state.lX; } /** **Return mouse y movement **/ int Mouse_Y() { return mouse_state.lY; } /** **Return mouse button state **/ int Mouse_Button(int button) { return mouse_state.rgbButtons[button] & 0x80; } /** **Return key press state **/ int Key_Down(int key) { return(keys[key] & 0x80); } /** **DirectInput shutdown **/ void DirectInput_Shutdown() { if(dikeyboard) { dikeyboard -> Unacquire(); dikeyboard -> Release(); dikeyboard = NULL; } if(dimouse) { dimouse -> Unacquire(); dimouse -> Release(); dimouse = NULL; } } /** **Returns true if controller is plugged in **/ bool XInput_Controller_Found() { XINPUT_CAPABILITIES caps; ZeroMemory(∩︀, sizeof(XINPUT_CAPABILITIES)); XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, ∩︀); if(caps.Type != 0) return false; return true; } /** **Vibrates the controller **/ void XInput_Vibrate(int contNum, int amount) { XINPUT_VIBRATION vibration; ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION)); vibration.wLeftMotorSpeed = amount; vibration.wRightMotorSpeed = amount; XInputSetState(contNum, &vibration); }