visual studio question
I am trying to compile and execute a program using visual studio 2008. When I run the program it compiles and then tries to run but only displays a black screen for a moment and then goes back to my code screen.
I'm sorry, I tried my tarot cards but didn't get any useful information from them about your problem. I AM going to meet a tall dark stranger though.
Perhaps you could provide a little more detail, like, your code (wrap in source /source tags), thanks. :)
Perhaps you could provide a little more detail, like, your code (wrap in source /source tags), thanks. :)
ok well here is some code.
#include <windows.h>#include <d3d9.h>#include <d3dx9.h>#include <time.h>#include <iostream>using namespace std;//initialize velocityint x_vel=2;int y_vel=2;//initialize ballint ball_left=400;int ball_right=450;int ball_top=300;int ball_bottom=350;//initialize paddleint paddle_left=300;int paddle_right=500;int paddle_top=500;int paddle_bottom=600;//program valuesconst string APPTITLE = "Load Bitmap Program";const int SCREENW = 800;const int SCREENH = 600;//key macro#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)//Direct3D objectsLPDIRECT3D9 d3d = NULL; LPDIRECT3DDEVICE9 d3ddev = NULL; LPDIRECT3DSURFACE9 backbuffer = NULL;LPDIRECT3DSURFACE9 surface = NULL;LPDIRECT3DSURFACE9 surface_two = NULL;LPDIRECT3DSURFACE9 surface_three = NULL;LPDIRECT3DSURFACE9 surface_four = NULL;LPDIRECT3DSURFACE9 surface_five = NULL;bool gameover = false;/** ** Game initialization function **/bool Game_Init(HWND window){ //initialize Direct3D d3d = Direct3DCreate9(D3D_SDK_VERSION); if (!d3d) { MessageBox(window, "Error initializing Direct3D", "Error", MB_OK); return false; } //set Direct3D presentation parameters D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = false; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.BackBufferWidth = 800; d3dpp.BackBufferHeight = 600; d3dpp.hDeviceWindow = window; //create Direct3D device d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev); if (!d3ddev) { MessageBox(window, "Error creating Direct3D device", "Error", MB_OK); return 0; } //clear the backbuffer to black d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0); //create surface HRESULT result = d3ddev->CreateOffscreenPlainSurface( 800, //width of the surface 600, //height of the surface D3DFMT_X8R8G8B8, //surface format D3DPOOL_DEFAULT, //memory pool to use &surface, //pointer to the surface NULL); //reserved (always NULL) //create surface HRESULT result_two = d3ddev->CreateOffscreenPlainSurface( 800, //width of the surface 600, //height of the surface D3DFMT_X8R8G8B8, //surface format D3DPOOL_DEFAULT, //memory pool to use &surface_two, //pointer to the surface NULL); //reserved (always NULL) if (!SUCCEEDED(result_two)) return false; //create surface HRESULT result_three = d3ddev->CreateOffscreenPlainSurface( 800, //width of the surface 600, //height of the surface D3DFMT_X8R8G8B8, //surface format D3DPOOL_DEFAULT, //memory pool to use &surface_three, //pointer to the surface NULL); //reserved (always NULL) if (!SUCCEEDED(result_three)) return false; //create surface HRESULT result_four = d3ddev->CreateOffscreenPlainSurface( 800, //width of the surface 600, //height of the surface D3DFMT_X8R8G8B8, //surface format D3DPOOL_DEFAULT, //memory pool to use &surface_four, //pointer to the surface NULL); //reserved (always NULL) if (!SUCCEEDED(result_four)) return false; //create surface HRESULT result_five = d3ddev->CreateOffscreenPlainSurface( 800, //width of the surface 600, //height of the surface D3DFMT_X8R8G8B8, //surface format D3DPOOL_DEFAULT, //memory pool to use &surface_five, //pointer to the surface NULL); //reserved (always NULL) if (!SUCCEEDED(result_five)) return false; //load surface from file into newly created surface result = D3DXLoadSurfaceFromFile( surface, //destination surface NULL, //destination palette NULL, //destination rectangle "paddle.bmp", //source filename NULL, //source rectangle D3DX_DEFAULT, //controls how image is filtered 0, //for transparency (0 for none) NULL); //source image info (usually NULL) //make sure file was loaded okay if (!SUCCEEDED(result)) return false; //load surface from file into newly created surface result = D3DXLoadSurfaceFromFile( surface_two, //destination surface NULL, //destination palette NULL, //destination rectangle "block.bmp", //source filename NULL, //source rectangle D3DX_DEFAULT, //controls how image is filtered 0, //for transparency (0 for none) NULL); //source image info (usually NULL) //make sure file was loaded okay if (!SUCCEEDED(result)) return false; //load surface from file into newly created surface result = D3DXLoadSurfaceFromFile( surface_three, //destination surface NULL, //destination palette NULL, //destination rectangle "block2.bmp", //source filename NULL, //source rectangle D3DX_DEFAULT, //controls how image is filtered 0, //for transparency (0 for none) NULL); //source image info (usually NULL) //make sure file was loaded okay if (!SUCCEEDED(result)) return false; //load surface from file into newly created surface result = D3DXLoadSurfaceFromFile( surface_four, //destination surface NULL, //destination palette NULL, //destination rectangle "block3.bmp", //source filename NULL, //source rectangle D3DX_DEFAULT, //controls how image is filtered 0, //for transparency (0 for none) NULL); //source image info (usually NULL) //make sure file was loaded okay if (!SUCCEEDED(result)) return false; //load surface from file into newly created surface result = D3DXLoadSurfaceFromFile( surface_five, //destination surface NULL, //destination palette NULL, //destination rectangle "ball10.bmp", //source filename NULL, //source rectangle D3DX_DEFAULT, //controls how image is filtered 0, //for transparency (0 for none) NULL); //source image info (usually NULL) //make sure file was loaded okay if (!SUCCEEDED(result)) return false; return true;}/** ** Game update function **/void Game_Run(HWND hwnd){ //make sure the Direct3D device is valid if (!d3ddev) return; //create pointer to the back buffer d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); //start rendering if (d3ddev->BeginScene()) { RECT src; src.left=0; src.right=800; src.top=0; src.bottom=600; RECT dest; dest.left=paddle_left; dest.right=paddle_right; dest.top=paddle_top; dest.bottom=paddle_bottom; //draw surface to the backbuffer d3ddev->StretchRect(surface, &src, backbuffer, &dest, D3DTEXF_NONE); ball_left += x_vel; ball_right += x_vel+5; ball_top += y_vel; ball_bottom += y_vel+5; if(ball_top<=0 || ball_top>=600) { y_vel*=-1; } if(ball_left<=0 || ball_left>=800) { x_vel*=-1; } RECT src_ball; src_ball.left=0; src_ball.right=800; src_ball.top=0; src_ball.bottom=600; RECT dest_ball; dest_ball.left=ball_left; dest_ball.right=ball_right; dest_ball.top=ball_top; dest_ball.bottom=ball_bottom; //draw surface to the backbuffer d3ddev->StretchRect(surface_five, &src_ball, backbuffer, &dest_ball, D3DTEXF_NONE); RECT src_two; src_two.left=0; src_two.right=800; src_two.top=0; src_two.bottom=600; RECT dest_two; dest_two.left=0; dest_two.right=200; dest_two.top=0; dest_two.bottom=50; //draw surface to the backbuffer d3ddev->StretchRect(surface_two, &src_two, backbuffer, &dest_two, D3DTEXF_NONE); RECT src_three; src_three.left=0; src_three.right=800; src_three.top=0; src_three.bottom=600; RECT dest_three; dest_three.left=200; dest_three.right=400; dest_three.top=0; dest_three.bottom=50; //draw surface to the backbuffer d3ddev->StretchRect(surface_three, &src_three, backbuffer, &dest_three, D3DTEXF_NONE); RECT src_four; src_four.left=0; src_four.right=800; src_four.top=0; src_four.bottom=600; RECT dest_four; dest_four.left=400; dest_four.right=600; dest_four.top=0; dest_four.bottom=50; //draw surface to the backbuffer d3ddev->StretchRect(surface_four, &src_four, backbuffer, &dest_four, D3DTEXF_NONE); RECT src_five; src_five.left=0; src_five.right=800; src_five.top=0; src_five.bottom=600; RECT dest_five; dest_five.left=600; dest_five.right=800; dest_five.top=0; dest_five.bottom=50; //draw surface to the backbuffer d3ddev->StretchRect(surface_two, &src_five, backbuffer, &dest_five, D3DTEXF_NONE); RECT src_six; src_six.left=0; src_six.right=800; src_six.top=0; src_six.bottom=600; RECT dest_six; dest_six.left=0; dest_six.right=200; dest_six.top=50; dest_six.bottom=100; //draw surface to the backbuffer d3ddev->StretchRect(surface_three, &src_six, backbuffer, &dest_six, D3DTEXF_NONE); RECT src_seven; src_seven.left=0; src_seven.right=800; src_seven.top=0; src_seven.bottom=600; RECT dest_seven; dest_seven.left=200; dest_seven.right=400; dest_seven.top=50; dest_seven.bottom=100; //draw surface to the backbuffer d3ddev->StretchRect(surface_four, &src_seven, backbuffer, &dest_seven, D3DTEXF_NONE); RECT src_eight; src_eight.left=0; src_eight.right=800; src_eight.top=0; src_eight.bottom=600; RECT dest_eight; dest_eight.left=400; dest_eight.right=600; dest_eight.top=50; dest_eight.bottom=100; //draw surface to the backbuffer d3ddev->StretchRect(surface_two, &src_eight, backbuffer, &dest_eight, D3DTEXF_NONE); RECT src_nine; src_nine.left=0; src_nine.right=800; src_nine.top=0; src_nine.bottom=600; RECT dest_nine; dest_nine.left=600; dest_nine.right=800; dest_nine.top=50; dest_nine.bottom=100; //draw surface to the backbuffer d3ddev->StretchRect(surface_three, &src_nine, backbuffer, &dest_nine, D3DTEXF_NONE); RECT src_ten; src_ten.left=0; src_ten.right=800; src_ten.top=0; src_ten.bottom=600; RECT dest_ten; dest_ten.left=0; dest_ten.right=200; dest_ten.top=100; dest_ten.bottom=150; //draw surface to the backbuffer d3ddev->StretchRect(surface_four, &src_ten, backbuffer, &dest_ten, D3DTEXF_NONE); RECT src_eleven; src_eleven.left=0; src_eleven.right=800; src_eleven.top=0; src_eleven.bottom=600; RECT dest_eleven; dest_eleven.left=200; dest_eleven.right=400; dest_eleven.top=100; dest_eleven.bottom=150; //draw surface to the backbuffer d3ddev->StretchRect(surface_two, &src_eleven, backbuffer, &dest_eleven, D3DTEXF_NONE); RECT src_twelve; src_twelve.left=0; src_twelve.right=800; src_twelve.top=0; src_twelve.bottom=600; RECT dest_twelve; dest_twelve.left=400; dest_twelve.right=600; dest_twelve.top=100; dest_twelve.bottom=150; //draw surface to the backbuffer d3ddev->StretchRect(surface_three, &src_twelve, backbuffer, &dest_twelve, D3DTEXF_NONE); RECT src_thirteen; src_thirteen.left=0; src_thirteen.right=800; src_thirteen.top=0; src_thirteen.bottom=600; RECT dest_thirteen; dest_thirteen.left=600; dest_thirteen.right=800; dest_thirteen.top=100; dest_thirteen.bottom=150; //draw surface to the backbuffer d3ddev->StretchRect(surface_four, &src_thirteen, backbuffer, &dest_thirteen, D3DTEXF_NONE); //stop rendering d3ddev->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); if (KEY_DOWN(VK_LEFT)) { paddle_left=paddle_left--; paddle_right=paddle_right--; if(paddle_left <= 0) { paddle_left=0; paddle_right=200; } } if (KEY_DOWN(VK_RIGHT)) { paddle_left=paddle_left++; paddle_right=paddle_right++; if(paddle_left >= 800) { paddle_left=600; paddle_right=800; } }}/** ** Game shutdown function **/void Game_End(HWND hwnd){ if (surface) surface->Release(); if (d3ddev) d3ddev->Release(); if (d3d) d3d->Release();}/** ** Windows event handling function **/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 function **/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_EX_TOPMOST|WS_VISIBLE|WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); //was there an error creating the window? 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); } return message.wParam;}
The program is designed to exit if certain things fail to happen. One of these is the failure to load the required resources.
On mine, the program initialize correctly until it it tries to load paddle.bmp, which I don't have.
I suspect you may not have them, or they are in the wrong place.
Do you have all the pictures it is trying to load? And do you have them in the correct directory which is probably (not an expert with VS2008) Projects\ProjectName\Debug
You can track the flow of the program as it executes in times such as this with breakpoints, the use of OutputDebugString, or using error codes on the returns.
On mine, the program initialize correctly until it it tries to load paddle.bmp, which I don't have.
I suspect you may not have them, or they are in the wrong place.
Do you have all the pictures it is trying to load? And do you have them in the correct directory which is probably (not an expert with VS2008) Projects\ProjectName\Debug
You can track the flow of the program as it executes in times such as this with breakpoints, the use of OutputDebugString, or using error codes on the returns.
As empirical2 said, it'll fail if the images aren't present. On my Visual Studio 2005, it failed (because of the bitmaps) while compiling. I created "dummy" bitmap files, placed them in the root folder with my .cpp file, and the program compiled and ran..."successfully". I say it that way because I'm not sure what it's supposed to do; it doesn't crash.
What it does:
Continues to alternate between a black and white screen, with an intensity that would even make a non-epileptic convulse until their limbs flew off. I then inserted a dreaded Sleep() call, to see what it was doing, and noticed that, at seemingly random times, the white becomes a smaller, centered, rectangle.
Compiled using the latest DirectX SDK (DXSDK_Jun10.exe).
What it does:
Continues to alternate between a black and white screen, with an intensity that would even make a non-epileptic convulse until their limbs flew off. I then inserted a dreaded Sleep() call, to see what it was doing, and noticed that, at seemingly random times, the white becomes a smaller, centered, rectangle.
Compiled using the latest DirectX SDK (DXSDK_Jun10.exe).
I'd use the debugger to step through your code to follow the flow of logic and see where it's going "wrong".
well I used the debugger and got a: Cannot find or open the PDB file. This is associated with some .dll files.
That's normal. VS doesn't find debug symbols for some installed dlls.
Really, step through with the debugger (F10 per default) and you'll see where your program maybe exits too early.
Really, step through with the debugger (F10 per default) and you'll see where your program maybe exits too early.
that also happened in my VC 6 to develop SDL application. When I moved to VC 2005 I thought that the problem were resolved...But it doesn't
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement