Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: C++ & DirectX headaches  (Read 1728 times)

0 Members and 1 Guest are viewing this topic.
C++ & DirectX headaches
« on: October 06, 2009, 08:11:35 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Okay, I have been slowly learning some directX. After a few tutorials I try to make something myself with the knowledge I have gained. Now I was just trying to setup a checkersboard. So I try to make the squares by drawing single colored squares. This is the code I made:

dxgraphics.h
Code: [Select]
#ifndef _DXGRAPHICS_H
#define _DXGRAPHICS_H

int Init_Direct3D(HWND, int, int, int);
LPDIRECT3DSURFACE9 LoadSurface(char*, D3DCOLOR);
int drawPlainSurface(int, int, int, int, int, int, int, HWND);

extern LPDIRECT3D9 d3d;
extern LPDIRECT3DDEVICE9 d3ddev;
extern LPDIRECT3DSURFACE9 backbuffer;
extern LPDIRECT3DSURFACE9 surface;

#endif

dxgraphics.cpp
Code: [Select]
#include <d3d9.h>
#include <d3dx9.h>
#include "dxgraphics.h"

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

int Init_Direct3D(HWND hWnd, int width, int height, int fullscreen){
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(d3d == NULL){
MessageBox(hWnd, "Error initializing Direct3D", "Error", MB_OK);
return 0;
}

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));

d3dpp.Windowed = (!fullscreen);
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
d3dpp.hDeviceWindow = hWnd;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

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

if(d3ddev == NULL){
MessageBox(hWnd, "Error creating Direct3D device", "Error", MB_OK);
return 0;
}

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

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

HRESULT result;
/****
 * THE NEXT PART GOES WRONG
 */
result = d3ddev->CreateOffscreenPlainSurface(
50,
50,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
&surface,
NULL);

if(!result){
MessageBox(hWnd, "Error creating plain surface", "error", MB_OK);
return 0;
}

return 1;
}


LPDIRECT3DSURFACE9 LoadSurface(char* filename, D3DCOLOR transcolor){
LPDIRECT3DSURFACE9 image = NULL;
D3DXIMAGE_INFO info;
HRESULT result;

result = D3DXGetImageInfoFromFile(filename, &info);
if(result != D3D_OK)
return NULL;

result = d3ddev->CreateOffscreenPlainSurface(
info.Width,
info.Height,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
&image,
NULL);

if(result != D3D_OK)
return NULL;

result = D3DXLoadSurfaceFromFile(
image,
NULL,
NULL,
filename,
NULL,
D3DX_DEFAULT,
transcolor,
NULL);

if(result != D3D_OK)
return NULL;

return image;
}

int drawPlainSurface(int x, int y, int width, int height, int r, int g, int b, HWND hWnd){
HRESULT result;

RECT rect;
MessageBox(hWnd, "Begin drawing plain surface", "error", MB_OK);


d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(r,g,b));

rect.left = x;
rect.top = y;
rect.right = x + width;
rect.bottom = y + height;
d3ddev->StretchRect(surface, NULL, backbuffer, &rect, D3DTEXF_NONE);
MessageBox(hWnd, "End drawing plain surface", "error", MB_OK);

return 1;
}

The LPDIRECT3DSURFACE9 surface never got instantiated. I don't know what goes wrong, because in the tutorial it goes right. I have looked on google what the problem might be. The only advice I got was that d3ddev was not initialized, but it doesn't fail on that check.

Oh yeah, I should add that I am running the program in windowed mode. Anyone some good advice, because this is giving me headaches.
Logged

Jeod

Team Dekunutz, Doubleteam
Re: C++ & DirectX headaches
« Reply #1 on: October 06, 2009, 08:13:50 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1675
Uh...update DirectX? Sorry, I don't know anything about writing code for it. But if it's exactly like the tutorial you used, then the error is outside the script. (Usually a problem with Windows components like DirectX or D3D)
Logged
"You should challenge your fates. When all else fails, you can still die fighting." ~Yune
___________________________________

Zelda GBC+ Engine for Multimedia Fusion 2
  • Doubleteam Project Page
Re: C++ & DirectX headaches
« Reply #2 on: October 06, 2009, 08:34:11 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
I've got the latest DirectX SDK and runtime.
Logged
Re: C++ & DirectX headaches
« Reply #3 on: October 07, 2009, 07:42:43 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Okay, I found the source of my problem. I still don't know why it doesn't work in my project and works in the tutorial. But if I remove the following code  after I create the surface it just works.

Code: [Select]
if(!result){
MessageBox(hWnd, "Error creating plain surface", "error", MB_OK);
return 0;
}


So if anyone can explain why. I'd love to hear it.
Logged
Re: C++ & DirectX headaches
« Reply #4 on: October 07, 2009, 08:49:09 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
So if anyone can explain why. I'd love to hear it.

replace it with
Code: [Select]
if(result != D3D_OK){
MessageBox(hWnd, "Error creating plain surface", "error", MB_OK);
return 0;
}


if D3D_OK is defined as 0 (I dunno if it is or not, but it makes sense if it is), what you had is going to give an error when it shouldn't and only not give an error when it goes wrong.
« Last Edit: October 07, 2009, 08:57:49 pm by TheDarkJay »
Logged
Re: C++ & DirectX headaches
« Reply #5 on: October 08, 2009, 05:52:32 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
So if anyone can explain why. I'd love to hear it.

replace it with
Code: [Select]
if(result != D3D_OK){
MessageBox(hWnd, "Error creating plain surface", "error", MB_OK);
return 0;
}


if D3D_OK is defined as 0 (I dunno if it is or not, but it makes sense if it is), what you had is going to give an error when it shouldn't and only not give an error when it goes wrong.

I tried it and that works, thnx. I wonder why the tutorial did it the other way and it worked.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.483 seconds with 50 queries.

anything