[SOLVED] Direct3D program won't close when fraps is running :S

Started by
4 comments, last by chrisparton1991 15 years, 9 months ago
Hey, i'm learning directX (using c++) and i've run into a bit of a problem. My program compiles and runs perfectly fine (no errors or warnings whatsoever). It closes fine when fraps isn't running but if i DO have it running the process doesn't close(the window closes tho). Has anyone had this problem before? I'm learning directX from directXtutorial.com and when i took the wrapper example and pasted all the source files directly into a new project the same thing happened. For the record im running windows xp sp3, compiled with MSVC++ Express 2008, DirectX SDK June 2008. I've tried running it in both debug and release modes. I'll include all of the source files (sorry theres a few of them lol) WinMain.cpp

#include "global.h"

// Starting Point
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    GAMEWINDOW gw;

    DisplayWindow(&gw, hInstance, nCmdShow);

    InitDirect3D(&gw);

    LoadGraphics();

    MainLoop();

    CloseDirect3D();

    return 0;
}

// Create the Window Class and the Window
void DisplayWindow(GAMEWINDOW* gw, HINSTANCE hInstance, int nCmdShow)
{
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX)); wc.cbSize = sizeof(WNDCLASSEX);
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = TEXT("WindowClass");

    RegisterClassEx(&wc);

    gw->hWnd = CreateWindowEx(NULL,
                              wc.lpszClassName,
                              TEXT("Your Game Title Here"),
                              gw->Windowed ? WS_OVERLAPPEDWINDOW : WS_EX_TOPMOST|WS_POPUP,
                              0, 0,
                              gw->Width, gw->Height,
                              NULL,
                              NULL,
                              hInstance,
                              NULL);

    ShowWindow(gw->hWnd, nCmdShow);
    return;
}

// Check for Messages and Handle
bool HandleMessages()
{
    static MSG msg;

    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        if (msg.message == WM_QUIT)
            return FALSE;

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return TRUE;
}

// Process the Messages
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}




Direct3D.cpp

#include "global.h"

LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
D3DPRESENT_PARAMETERS d3dpp;
LPD3DXSPRITE d3dspt;

// Create Direct3D and the Direct3D Device
void InitDirect3D(GAMEWINDOW* gw)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION);

    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.Windowed = gw->Windowed;
    d3dpp.BackBufferWidth = gw->Width;
    d3dpp.BackBufferHeight = gw->Height;

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

    D3DXCreateSprite(d3ddev, &d3dspt);

    return;
}

// Close the Device and Direct3D
void CloseDirect3D()
{
    d3ddev->Release();
    d3d->Release();

    return;
}

// Start rendering
void StartRender()
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->BeginScene();
    d3dspt->Begin(D3DXSPRITE_ALPHABLEND);

    return;
}

// Stop rendering
void EndRender()
{
    d3dspt->End();
    d3ddev->EndScene();
    d3ddev->Present(NULL, NULL, NULL, NULL);

    return;
}

// Load a graphic into a SPRITE object
void LoadSprite(SPRITE* pSprite, LPCTSTR File, int width, int height, int cols, int rows)
{
    D3DXCreateTextureFromFileEx(d3ddev,
                                File,
                                D3DX_DEFAULT,
                                D3DX_DEFAULT,
                                D3DX_DEFAULT,
                                NULL,
                                D3DFMT_A8R8G8B8,
                                D3DPOOL_MANAGED,
                                D3DX_DEFAULT,
                                D3DX_DEFAULT,
                                D3DCOLOR_XRGB(255, 0, 255),
                                NULL,
                                NULL,
                                &pSprite->tex);

    pSprite->width = width;
    pSprite->height = height;
    pSprite->cols = cols;
    pSprite->rows = rows;

    return;
}

// Draw a frame from a SPRITE object
void DrawSprite(SPRITE* pSprite, int Frame, float x, float y, float z)
{
    RECT FrameBox;
    FrameBox.left = (Frame % pSprite->cols) * pSprite->width;
    FrameBox.top = (Frame / pSprite->cols) * pSprite->height;
    FrameBox.right = FrameBox.left + pSprite->width;
    FrameBox.bottom = FrameBox.top + pSprite->height;

    D3DXVECTOR3 position(x, y, z);

    d3dspt->Draw(pSprite->tex, &FrameBox, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));

    return;
}




Loop.cpp

#include "global.h"

// The Main Loop
void MainLoop()
{
    while(HandleMessages())
    {
        Render();
    }
}




Render.cpp

#include "global.h"

SPRITE interceptor;

void LoadGraphics()
{
    LoadSprite(&interceptor, TEXT("Interceptor.png"), 40, 40, 6, 6);

    return;
}

void Render()
{
    static int frame = 0;
    if (frame == 36) frame = 0;

    StartRender();
    DrawSprite(&interceptor, frame++, 100, 100, 0);
    EndRender();

    return;
}




Global.h

// header files
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "gamewindow.h"
#include "sprite.h"

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

// prototypes for...
// WinMain.cpp
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void DisplayWindow(GAMEWINDOW* gw, HINSTANCE hInstance, int nCmdShow);
bool HandleMessages();
// Loop.cpp
void MainLoop();
// Direct3D.cpp
void InitDirect3D(GAMEWINDOW* gw);
void CloseDirect3D();
void StartRender();
void EndRender();
void LoadSprite(SPRITE* pSprite, LPCTSTR File, int width, int height, int cols, int rows);
void DrawSprite(SPRITE* pSprite, int Frame, float x, float y, float z);
// Render.cpp
void Render();
void LoadGraphics();




GameWindow.h

struct GAMEWINDOW
{
    HWND hWnd;
    int Width, Height;
    bool Windowed;

    GAMEWINDOW()
    {
        hWnd = NULL;
        Width = 1024;
        Height = 768;
        Windowed = true;
    }
};




Sprite.h

struct SPRITE
{
    int rows, cols;    // number of rows and columns
    int width, height;    // width and height of each frame
    LPDIRECT3DTEXTURE9 tex;    // texture
};




____________________________ Whoa... that took a while to copy in :P I just want to see if i can get that fixed before i move on in case i have to change anything majorly. If it can't be fixed ill just have to not use fraps lol. Thanks heaps, Chris P.S: I know this is probably a stupid question but ive only started learning, hence why its in the "for beginners" section lol [Edited by - chrisparton1991 on July 29, 2008 1:11:55 AM]
Advertisement
bool bump = true; // Bumping thread...
You don't appear to be releasing your ID3DXSprite interface anywhere.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Are you using the Debug Runtimes?

If everything works fine with them, and the app still doesn't close; I'd blame Fraps.
Hey, yes i am using the debug runtimes, it doesn't close either way. I get some nice errors though: 2443 errors after running the program for several seconds... All the same as these (with different memory addresses obviously):

Direct3D9: (ERROR) : [0] : Address 00F7AA18
Direct3D9: (ERROR) : [1] : Address 00F99B2B
Direct3D9: (ERROR) : [2] : Address 6356496B
Direct3D9: (ERROR) : [3] : Address 6356487C
Direct3D9: (ERROR) : [4] : Address 0041245A
Direct3D9: (ERROR) : [5] : Address 00412360
Direct3D9: (ERROR) : [6] : Address 004115C0
Direct3D9: (ERROR) : [7] : Address 00414478
Direct3D9: (ERROR) : [8] : Address 004141DF
Direct3D9: (ERROR) : [9] : Address 7C817067

@superpig: hmm true, i'll have to look into that. The code i provided is all taken from the website... you'd think that a tutorial website would make a wrapper that releases the sprite interface wouldnt you. I'll see if i can release it or otherwise just remove all sprite code and see if the problem persists.

Thanks guys, ill rate you both as extremely helpful for your troubles. This had better work!!! lol
well i released the sprite interface and nothing changed. I looked around and noticed that the IDirect3DTexture9 interface in the SPRITE struct wasn't being initialized anywhere (no LPDIRECT3DTEXTURE9), so I created one and changed all of my code to use the interface. The program won't close though. I'm going to try removing all sprite and texture code from the wrapper.


EDIT: removing all sprite and texture code worked, now ill have to try and find exactly what went wrong... yay

EDIT #2: I rewrote the sprite and texture code (havent implemented DirectInput or logic or anything yet but the problem seems to be fixed =]). Back on the road to game programming!!

[Edited by - chrisparton1991 on July 28, 2008 1:37:40 AM]

This topic is closed to new replies.

Advertisement