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]