Need help with Direct3D

Started by hansiec, October 13, 2012, 04:42:15 pm

Previous topic - Next topic

hansiec

Alright, so I'm working on a dll that renders Direct3D for rmxp, but my problem is when I try rendering it doesn't render anything.

This is written in c++

/*
*  XP 3D dll main
*  By: Hansiec
*  YOU ARE NOT ALLOWED TO MODIFY THIS EXCEPT FOR SELF-PURPOSES OR BUG FIXING!
*  ~~~ You have been warned... (Self purposes meaning no re-distributions at all unless it's for a fix of a bug/improvements)
*/

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

// Exclude rarely-used stuff from Windows headers
#define WIN32_LEAN_AND_MEAN       

// Windows Header Files:
#include <windows.h>
#include <windowsx.h>

// C++ RunTime Header Files
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

// DirectX Header Files
#include <d3d9.h>
#include <d3dx9.h>
#include "dll.h"

#define export extern "C" __declspec(dllexport)

// Variables
HWND hWnd;
int dll_version = 0;
LPDIRECT3DDEVICE9 p_dx_Device;
LPDIRECT3D9 p_dx_Object;
LPDIRECT3DDEVICE9 p_Device;
// Some usefull functions for setup.
export void init(int i){
  // Sets the Window handle that you must get from XP.
  hWnd = (HWND) i;

// Initialize some Direct 3D variables
p_dx_Object = Direct3DCreate9(D3D_SDK_VERSION);
if (p_dx_Object == NULL)
{
     // Only display if we do not have DirectX
     MessageBox(hWnd,"DirectX Runtime library not installed!","3DXP Error in: init()",MB_OK);
}

// initialize more variables
D3DPRESENT_PARAMETERS dx_PresParams;

// Device creation
ZeroMemory( &dx_PresParams, sizeof(dx_PresParams) );
dx_PresParams.Windowed = TRUE;
dx_PresParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
dx_PresParams.BackBufferFormat = D3DFMT_UNKNOWN;

p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device);

if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
{
     if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
     {
         MessageBox(hWnd,"Failed to create even the reference device!","3DXP Error in: init()",MB_OK);
     }
}
// finally we set our device.
//p_Device = InitializeDevice(hWnd);
//p_Device = p_dx_Object->AddRef(hWnd);
}

// Returns the owner of the DLL (Just for checks of theft)
export char* get_owner(){
  return "Hansiec";
}

// Returns the Direct 3D SDK version
export int get_version(){
  return D3D_SDK_VERSION;
}

// Checks the XP3D dll version
export int check_version(int version){
  return (int) version >= dll_version;
}

// Renders a frame
export int render(){
   HRESULT hr;
   struct D3DVERTEX {float x, y, z, rhw; DWORD color;} vertices[3];
  vertices[0].x = 50;
  vertices[0].y = 50;
  vertices[0].z = 0;
  vertices[0].rhw = 1.0f;
  vertices[0].color = 0x00ff00;



  vertices[1].x = 250;
  vertices[1].y = 50;
  vertices[1].z = 0;
  vertices[1].rhw = 1.0f;
  vertices[1].color = 0x0000ff;



  vertices[2].x = 50;
  vertices[2].y = 250;
  vertices[2].z = 0;
  vertices[2].rhw = 1.0f;
  vertices[2].color = 0xff0000;
  LPDIRECT3DVERTEXBUFFER9 pVertexObject = NULL;
  void *pVertexBuffer = NULL;
  if(FAILED(p_dx_Device->CreateVertexBuffer(3*sizeof(D3DVERTEX), 0,
          D3DFVF_XYZRHW|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &pVertexObject, NULL)))
   return(0);
   if(FAILED(pVertexObject->Lock(0, 3*sizeof(D3DVERTEX), &pVertexBuffer, 0)))
   return(0);
   memcpy(pVertexBuffer, vertices, 3*sizeof(D3DVERTEX));
   pVertexObject->Unlock();
   
  p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  p_dx_Device->BeginScene();
  p_dx_Device->SetStreamSource(0, pVertexObject, 0, sizeof(D3DVERTEX));
  p_dx_Device->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE);
  p_dx_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
  p_dx_Device->EndScene();
}



also, dll.h contains nothing of importance so this is not needed to post. (actually I think I can remove it since it serves no purpose at all.)


Blizzard

Just curious, was it the commented line "//p_Device = InitializeDevice(hWnd);"? xD
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

hansiec

Quote from: Blizzard on October 13, 2012, 06:41:49 pm
Just curious, was it the commented line "//p_Device = InitializeDevice(hWnd);"? xD


nope, I was mixing up some dimensions, I tried someone else's triangle vertices and it worked...