views:

54

answers:

2

[Edit] Note: These projects are projects that I have copied and renamed. Not sure if that would have anything to do with it.

I am using the directx sdk and just playing around with it. I was trying to blit a image on to the screen by loading a external file. For some reason it does not work when I build it and run in debug. But If I go to the debug folder and open the exe file. It works. I am not sure why. Here is my code

#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <time.h>
#include <iostream>
using namespace std;

#pragma comment(lib,"d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

const string APPTITLE = "Game Loop";
const int SCREENW = 1024;
const int SCREENH = 768;

bool gameover = false;

LPDIRECT3D9 d3d             = NULL;
LPDIRECT3DDEVICE9 d3ddev    = NULL;
LPDIRECT3DSURFACE9 surface  = NULL;
LPDIRECT3DSURFACE9 backbuffer = NULL;

#define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

bool Game_Init(HWND window)
{
    MessageBox(window, "Game_Init", "BREAKPOINT",0);
    //Initialize

    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    if(d3d == NULL)
    {
        MessageBox(window, "Error Initializing", "ERROR",MB_OK);
        return 0;
    }

    D3DPRESENT_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;
    d3dpp.Windowed = TRUE;

    d3d->CreateDevice(
        D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        window,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &d3dpp,
        &d3ddev
        );

    if(d3ddev == NULL)
    {
        MessageBox(window, "Error Creating Device", "ERROR",MB_OK);
        return 0;
    }

    srand(time(NULL));

    d3ddev->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0), 1.0f,0);

    d3ddev->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);
    HRESULT result = d3ddev->CreateOffscreenPlainSurface(
        SCREENW,SCREENH,
        D3DFMT_X8R8G8B8,
        D3DPOOL_DEFAULT,
        &surface,
        NULL);
    if(!SUCCEEDED(result)) return false;

    result = D3DXLoadSurfaceFromFile(
        surface,
        NULL,NULL,
        "legotron.bmp",
        NULL,
        D3DX_DEFAULT,
        0,
        NULL);

    if(!SUCCEEDED(result))
    {
        MessageBox(window, "Error Loading Image", "ERROR",MB_OK);
        return false;
    }

    return true;
}

void Game_Run(HWND hwnd)
{
    if(!d3ddev) return;

    d3ddev->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);

    if(d3ddev->BeginScene())
    {

        d3ddev->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE);

        d3ddev->EndScene();
        d3ddev->Present(NULL,NULL,NULL,NULL);
    }

    if(KEY_DOWN(VK_ESCAPE))
        PostMessage(hwnd, WM_DESTROY,0,0);
}

void Game_End(HWND hwnd)
{
    if(surface) surface->Release();
    if(d3ddev)d3ddev->Release();
    if(d3d)d3d->Release();
}

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM WParam, LPARAM lparam)
{
    switch(message)
    {
        case WM_DESTROY:
            gameover = true;
            PostQuitMessage(0);
            return 0;
        break;
    }

    return DefWindowProc(hWnd, message, WParam, lparam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                  LPSTR lpCmdLine, int nCmdShow)
{
        //set the new windows properties

    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);

    HWND window = CreateWindow(
        APPTITLE.c_str(),
        APPTITLE.c_str(),
        WS_OVERLAPPED,
        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;

    MSG message;

    //main message loop
    while(!gameover)
    {
        if(PeekMessage(&message,NULL, 0, 0,PM_REMOVE))
        {
            TranslateMessage(&message);
            DispatchMessage(&message);
        }
        Game_Run(window);
    }

    Game_End(window);

    return message.wParam;
}

I have it where it closes the application if the image fails to load.

result = D3DXLoadSurfaceFromFile(
            surface,
            NULL,NULL,
            "legotron.bmp",
            NULL,
            D3DX_DEFAULT,
            0,
            NULL);

        if(!SUCCEEDED(result))
        {
            MessageBox(window, "Error Loading Image", "ERROR",MB_OK);
            return false;
        }

the window stays open (but no image) if I remove the return false line. so apparently it fails there. The image is located in the same folder as the .exe file (within the debug folder) as it should. cause i havnt done anything to tell it to go anywhere else otherwise. Again it works when I personally open the .exe file within the debug folder. but does not work in the ide when I build it and try to run it.

[EDIT]

I Built the application from scratch and ran it. Still doesnt work.

A: 

Have you checked the current working directory? It's been a while, but I think the function to call is http://msdn.microsoft.com/en-us/library/aa364934%28VS.85%29.aspx. Add it as a debug output call to see where it's executing from when run as a debug project. The bmp will need to be in that folder, IIRC.

yodaj007
This could be a reason. This project I am using is a copied project. could that have anything to do with it.
numerical25
I suspect the issue is that the image isn't available in the CWD for the program. So if you debug what the program thinks the CWD is, then check that folder for the existence of the image. Copy it there, then try it again.
yodaj007
what is the CWD ???
numerical25
Sorry, Current Working Directory. I was using an abbreviation.
yodaj007
I did try that. Put the images into the debug folder where the exe file is and it didnt work. Also built another empty project and put all the code in. Still does the same thing. only works when I go into the debug folder and run the application from there. doesnt in compile time
numerical25
+1  A: 

The current folder isn't necessarily the folder where your .exe file is. Check the Debugging page of the project options and make sure the working folder is what you expect.

When you run the program in debug, you're running the same .exe file from that debug folder, but visual studio sets up the environment it runs in - working folder included. You know the program runs, therefore most likely the environment is at fault.

Programs that work as a release build but not debug happen occasionally, usually meaning there's a memory corruption. Programs where the same build works when called one way but not another are rarer, but can happen. These things are a nightmare to debug.

I doubt that's the problem here though.

Steve314
this project is a copied project. that might have something to do with it. how do I check that. I am new to the MSVS enviroment.
numerical25
Right-click the project in the "solution explorer" pane. Select "properties" in the context menu. In the dialog that appears, you should see "Debugging" in the treeview to the left. Select that, and you'll see various options (including the "working directory") to the right. Click on that and you can either type a folder name, or use the little arrow that appears to select from "<browse>" and the seemingly redundant "<edit>". Check the "configuration" (top left of the dialog) to make sure your dealing with the debug build, though.
Steve314
it worked. I change the directory to the debug directory and it worked. I wonder why its not set by default
numerical25
By default, IIRC, studio creates debug and release folders directly under a main folder that holds all the source. I guess they expect you to keep your files in that top-level folder, making it a reasonable default. The content of the "debug" and "release" folders are usually reserved for compiler outputs - e.g. I tend to have the whole folders marked "ignore" in source control - why store compiler output in the versioning database? But how you use them is up to you, of course.
Steve314