Archived

This topic is now archived and is closed to further replies.

PsychoGears

Program works for me, not for others

Recommended Posts

PsychoGears    122
Hello all. I posted about this a week or two ago but have since been caught up with other things (like my real job). I have been trying to get information but have all but given up on trying to get my tiny program to work on a computer other than my own. I haven't progressed past programming DirectX Graphics because I figure if I can't get my program to run on another person's computer, then there's no point in making an elaborate program if no one will use it. Anyway, here is my code:
#define NDEBUG
// Include files
#include <windows.h>
#include <stdio.h>
#include <d3d8.h>
#include <d3dx8.h>

// Window handles, class and caption text
HWND          g_hWnd;
HINSTANCE     g_hInst;
static char   g_szClass[]   = "SomethingClass";
static char   g_szCaption[] = "Something Application";

typedef struct {
  FLOAT x, y, z;
  FLOAT nx, ny, nz;
} sVertex;
#define VertexFVF (D3DFVF_XYZ | D3DFVF_NORMAL)

// COM objects (we need to close these at the end)
IDirect3D8 *g_D3D8 = NULL; // Direct3D object
IDirect3DDevice8 *g_D3DD8 = NULL; // Direct3D Device object
IDirect3DVertexBuffer8 *g_D3DVB8 = NULL; // Direct3D Vertex Buffer
IDirect3DVertexBuffer8 *g_D3DVB8_SquareBG = NULL; // Direct3D Vertex Buffer (for bg

// Function prototypes
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

BOOL DoInit();
BOOL DoShutdown();
BOOL DoFrame();

int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
{
  WNDCLASSEX wcex;
  MSG        Msg;

  g_hInst = hInst;

  // Create the window class here and register it
  wcex.cbSize        = sizeof(wcex);
  wcex.style         = CS_CLASSDC;
  wcex.lpfnWndProc   = WindowProc;
  wcex.cbClsExtra    = 0;
  wcex.cbWndExtra    = 0;
  wcex.hInstance     = hInst;
  wcex.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wcex.hbrBackground = NULL;
  wcex.lpszMenuName  = NULL;
  wcex.lpszClassName = g_szClass;
  wcex.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  if(!RegisterClassEx(&wcex))
    return FALSE;

  // Create the Main Window
  g_hWnd = CreateWindow(g_szClass, g_szCaption,
        WS_CAPTION | WS_SYSMENU,
        0, 0, 400, 400,
        NULL, NULL,
        hInst, NULL );
  if(!g_hWnd)
    return FALSE;
  ShowWindow(g_hWnd, SW_NORMAL);
  UpdateWindow(g_hWnd);

  // Run init function and return on error
  if(DoInit() == FALSE)
    return FALSE;

  // Start message pump, waiting for signal to quit
  ZeroMemory(&Msg, sizeof(MSG));
  while(Msg.message != WM_QUIT) {
    if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
    }
    if(DoFrame() == FALSE)
      break;
  }

  // Run shutdown function
  DoShutdown();
  
  UnregisterClass(g_szClass, hInst);

  return Msg.wParam;
}

long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg,                                         WPARAM wParam, LPARAM lParam)
{
  switch(uMsg) {
    case WM_DESTROY:
      PostQuitMessage(0);
      return 0;

  }

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

BOOL DoInit()
{
  D3DDISPLAYMODE d3ddm;
  D3DPRESENT_PARAMETERS d3dpp;
  BYTE *Ptr;
  D3DXMATRIX matWorld, matView, matProj;
  D3DXMATRIX matRot, matTrans, matScale;
  // Initialize the system
  // Step 1: Obtain an interface to Direct3D
  if((g_D3D8 = Direct3DCreate8(D3D_SDK_VERSION)) == NULL)
    return FALSE;
  // Step 2: Select a display mode
  // using windowed mode
  if(FAILED(g_D3D8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
    return FALSE;
  // Step 3: Set the presentation method
  ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
  d3dpp.Windowed = TRUE;
  d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  d3dpp.BackBufferFormat = d3ddm.Format;
  // Step 4: Create the device interface and initialize the display
  if(FAILED(g_D3D8->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DD8)))
    return FALSE;

  // make the vertex buffer

  // DiamondFace
  sVertex DiamondFace[3] = {
    {  50.0f,   0.0f, -50.0f, 0.0f, 0.5f, -0.866f},
    { -50.0f,   0.0f, -50.0f, 0.0f, 0.5f, -0.866f},
    {   0.0f, 100.0f,   0.0f, 0.0f, 0.5f, -0.866f}
  };
  if(FAILED(g_D3DD8->CreateVertexBuffer(sizeof(sVertex) * 3, D3DUSAGE_WRITEONLY, VertexFVF, D3DPOOL_MANAGED, &g_D3DVB8)))
    return FALSE;
  if(SUCCEEDED(g_D3DVB8->Lock(0, 0, (BYTE**)&Ptr, 0))){
    memcpy(Ptr, DiamondFace, sizeof(DiamondFace));
    g_D3DVB8->Unlock();
  }

  // SquareBG
  sVertex SquareBG[4] = {
    { -100.0f,  100.0f, 100.0f, 0.0f, 0.0f, -1.0f},
    {  100.0f,  100.0f, 100.0f, 0.0f, 0.0f, -1.0f},
    {  100.0f, -100.0f, 100.0f, 0.0f, 0.0f, -1.0f},
    { -100.0f, -100.0f, 100.0f, 0.0f, 0.0f, -1.0f}
  };
  if(FAILED(g_D3DD8->CreateVertexBuffer(sizeof(sVertex) * 4, D3DUSAGE_WRITEONLY, VertexFVF, D3DPOOL_MANAGED, &g_D3DVB8_SquareBG)))
    return FALSE;
  if(SUCCEEDED(g_D3DVB8_SquareBG->Lock(0, 0, (BYTE**)&Ptr, 0))){
    memcpy(Ptr, SquareBG, sizeof(SquareBG));
    g_D3DVB8_SquareBG->Unlock();
  }

  // Set the rendering states
  g_D3DD8->SetRenderState(D3DRS_LIGHTING, TRUE);
  g_D3DD8->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
  g_D3DD8->SetRenderState(D3DRS_ZENABLE, TRUE);
  g_D3DD8->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  g_D3DD8->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  g_D3DD8->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  //g_D3DD8->SetRenderState(D3DRS_AMBIENT, 0xFFFFFFFF);

  
  // Create and set the projection transformation
  D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 1000.0f);
  g_D3DD8->SetTransform(D3DTS_PROJECTION, &matProj);

  return TRUE;
}

BOOL DoShutdown()
{
  // Release all COM objects
  if(g_D3DVB8 != NULL) g_D3DVB8->Release();
  if(g_D3DVB8_SquareBG != NULL) g_D3DVB8_SquareBG->Release();
  if(g_D3DD8 != NULL) g_D3DD8->Release();
  if(g_D3D8 != NULL) g_D3D8->Release();

  return TRUE;
}

BOOL DoFrame()
{
  D3DXMATRIX matView, matWorld, matWorld2;
  D3DXMATRIX matTx;
  D3DMATERIAL8 d3dm;
  D3DLIGHT8 d3dlDirectional;
  D3DCOLORVALUE FourColors[4] = {
    {1.0f, 0.0f, 0.0f, 0.75f}, // Red, 75% opacity
    {0.0f, 1.0f, 0.0f, 0.75f}, // Green, 75% opacity
    {0.0f, 0.0f, 1.0f, 0.75f}, // Blue, 75% opacity
    {1.0f, 1.0f, 0.0f, 0.75f}, // Yellow, 75% opacity
  };
  D3DCOLORVALUE White = {1.0f, 1.0f, 1.0f, 0.75f};
  
  float Angle;
  Angle = (float)timeGetTime() / 3000.0f;

  // clear device backbuffer
  g_D3DD8->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 255), 1.0f, 0);

  // set View position
  //D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3((float)cos(Angle) * 400.0f, 150.0f, (float)sin(Angle) * 400.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(400.0f, 150.0f, -400.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  g_D3DD8->SetTransform(D3DTS_VIEW, &matView);

  ZeroMemory(&d3dlDirectional, sizeof(D3DLIGHT8));
  // make a white light coming from my direction to origin
  // set general light variables
  d3dlDirectional.Type = D3DLIGHT_DIRECTIONAL;
  // set directional light specific variables
  d3dlDirectional.Diffuse.r = d3dlDirectional.Ambient.r = 0.5f;
  d3dlDirectional.Diffuse.g = d3dlDirectional.Ambient.g = 0.5f;
  d3dlDirectional.Diffuse.b = d3dlDirectional.Ambient.b = 0.5f;
  D3DXVECTOR3 Dir = D3DXVECTOR3(-400.0f, -150.0f, 400.0f);
  D3DXVec3Normalize((D3DXVECTOR3*)&d3dlDirectional.Direction, &Dir);
  g_D3DD8->SetLight(0, &d3dlDirectional);
  g_D3DD8->LightEnable(0, TRUE);
/*
  ZeroMemory(&d3dm, sizeof(D3DMATERIAL8));
  // Fill Diffuse and Ambient to white 75% opaque color
  d3dm.Diffuse.r = d3dm.Ambient.r = 1.0f;
  d3dm.Diffuse.g = d3dm.Ambient.g = 1.0f;
  d3dm.Diffuse.b = d3dm.Ambient.b = 1.0f;
  d3dm.Diffuse.a = d3dm.Ambient.a = 0.75f;
  g_D3DD8->SetMaterial(&d3dm);
*/

  // begin scene
  if(SUCCEEDED(g_D3DD8->BeginScene())) {
    // background square
    g_D3DD8->SetStreamSource(0, g_D3DVB8_SquareBG, sizeof(sVertex));
    g_D3DD8->SetVertexShader(VertexFVF);
    D3DXMatrixIdentity(&matWorld);
    g_D3DD8->SetTransform(D3DTS_WORLD, &matWorld);
    ZeroMemory(&d3dm, sizeof(D3DMATERIAL8));
    // Fill Diffuse and Ambient to white 75% opaque color
    d3dm.Diffuse = d3dm.Ambient = White;
    g_D3DD8->SetMaterial(&d3dm);
    g_D3DD8->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);

    // gay diamond
    g_D3DD8->SetStreamSource(0, g_D3DVB8, sizeof(sVertex));
    // set up the world matrix
    D3DXMatrixIdentity(&matWorld);
    D3DXMatrixIdentity(&matWorld2);
    D3DXMatrixRotationZ(&matTx, D3DX_PI);
    D3DXMatrixMultiply(&matWorld2, &matWorld2, &matTx);
    // rotate matWorld2 around y axis by PI radians (180 degrees)
    D3DXMatrixRotationY(&matTx, D3DX_PI);
    D3DXMatrixMultiply(&matWorld2, &matWorld2, &matTx);
    //g_D3DD8->SetTransform(D3DTS_WORLD, &matWorld);
    D3DXMatrixRotationY(&matTx, -Angle);
    D3DXMatrixMultiply(&matWorld, &matWorld, &matTx);
    D3DXMatrixMultiply(&matWorld2, &matWorld2, &matTx);
    

    
    //g_D3DD8->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
    for(int j=0; j<4; j++){
      ZeroMemory(&d3dm, sizeof(D3DMATERIAL8));
      // Fill Diffuse and Ambient to white 75% opaque color
      d3dm.Diffuse = d3dm.Ambient = FourColors[j];
      g_D3DD8->SetMaterial(&d3dm);
      g_D3DD8->SetTransform(D3DTS_WORLD, &matWorld);
      g_D3DD8->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);
      D3DXMatrixRotationY(&matTx, -D3DX_PI/2);
      D3DXMatrixMultiply(&matWorld, &matWorld, &matTx);
    }
    // do under side now
    for(int j=0; j<4; i++){
      ZeroMemory(&d3dm, sizeof(D3DMATERIAL8));
      // Fill Diffuse and Ambient to white 75% opaque color
      d3dm.Diffuse = d3dm.Ambient = FourColors[j];
      g_D3DD8->SetMaterial(&d3dm);
      g_D3DD8->SetTransform(D3DTS_WORLD, &matWorld2);
      g_D3DD8->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);
      D3DXMatrixRotationY(&matTx, -D3DX_PI/2);
      D3DXMatrixMultiply(&matWorld2, &matWorld2, &matTx);
    }
    //g_D3DD8->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    g_D3DD8->EndScene();
  }

  // display scene
  g_D3DD8->Present(NULL, NULL, NULL, NULL);
  return TRUE;
}
  
This code works fine on my own computer. I compile it, I run it, I see a rainbow diamond in the picture spinning in front of a white 2-D square, diamond being 25% see-through. I can't figure out how to get this same program to run on any of my work computers which all have DX9.0a installed in them, nor on my friend's computer who has DX9 but only sees the corners of the diamond and a jaggy shape where the square should be. Any help is appreciated, even if you could just run the above code and tell me if it screws up. I'm not really sure what kind of project to open up in Visual C++ to start everything off with, anyway. All I know is I have to set the Runtime Library to Multi-Threaded (/MT), and add in the libraries d3d8.lib and d3dx8.lib and winmm.lib for my one call to timeGetTime(). Oh, before I forget, my computer has DirectX 9.0a also. -TJ [edited by - PsychoGears on August 8, 2003 2:19:51 AM]

Share this post


Link to post
Share on other sites
Jmuulian    126
Haven''t tested it but from briefly looking at your code and your explanation of the problem on your friend''s computer, my guess would be depth buffering.

Experiment with depth buffer/stencil formats and try and find out whether your friend''s video card supports the same depth buffering format you''re using.

Share this post


Link to post
Share on other sites