Jump to content
  • Advertisement
Sign in to follow this  
cpp forever

find a bug, plz

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I ahve developed a simple Direct3D8 app. It works on Windows Server 2003, Windows XP, but not on Windows XP version 2002 and -. Maybe smbody help me to find out the reason of crash: moframecount.h

#ifndef MOFRAMECOUNT_H
#define MOFRAMECOUNT_H

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

class moFrameCount
{
private:
  DWORD dwStartTime;               // &#1079;&#1084;&#1110;&#1085;&#1085;&#1110; &#1076;&#1083;&#1103; &#1079;&#1072;&#1076;&#1072;&#1085;&#1085;&#1103; &#1095;&#1072;&#1089;&#1091;
  DWORD dwNextTime;
  unsigned int TimeTicks;
public:
  moFrameCount();
  void Inc();
  void Reset();
  unsigned int Get();
};

#endif


moframecount.cpp

#include "moframecount.h"

moFrameCount::moFrameCount()
{
  this->Reset();
}

void moFrameCount::Reset()
{
  TimeTicks = dwStartTime = 0;
}

void moFrameCount::Inc()
{

  dwNextTime = GetTickCount();
  if(!this->dwStartTime)
  {
    this->dwStartTime = this->dwNextTime;
    ++dwNextTime;
  }
  else
  {
    if(dwNextTime - dwStartTime >= 1000)
     this->Reset();
  }
  ++TimeTicks;

}

unsigned int moFrameCount::Get()
{
  DWORD time = dwNextTime - dwStartTime;
  return (time ? (TimeTicks*1000)/time : 0);
}



main.cpp
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <d3dx8core.h> // fur Direct3D 8
#include <mmsystem.h>  // fur timeSetEvent()
#include <time.h>      // fur time()
#include <stdio.h>     // fur sprintf()

#include "moframecount.h"


#define DD_DEBUG
#undef DD_DEBUG

// my defines
#ifndef M_PI
#define M_PI              3.14159265358979323846
#endif
#define DD_PI             M_PI
#define DD_CLASSNAME      "ddscreensaver1"
#define DD_DOTS_MAX       16
#define DD_TIME_CALC      10
int mov = 2;
#define DD_FONT_SIZE      16
#define DD_TEXT_INTERVAL  50
#define DD_WINCHECK(func) if(!(func)) return -1
#define DD_CHECK(func)    if(FAILED((func))) { MessageBox(hwnd, "Application will now close", NULL, MB_OK | MB_ICONSTOP); return -1; }
#define DD_DCHECK(func)   DD_CHECK((func)); ++dd_app_init;
#define DD_RELEASE(lp)    (lp)->Release();(lp)=NULL;--dd_app_init


// global vars
int dd_app_init;
int slow_pc;
char Slow[128];
char* pSlow = Slow;
int draw_text = DD_TEXT_INTERVAL;
POINT screen;
int mouse_init;
LPARAM mouse_pos;
MMRESULT hTimer;
char status[256];
RECT rect;
LPDIRECT3DDEVICE8 lpd3dd;
LPDIRECT3DVERTEXBUFFER8 lpd3dvb;
LPD3DXFONT lpd3dxfont;
moFrameCount fc;
char* cData;
int Semaphore;
CRITICAL_SECTION cs;
PAINTSTRUCT ps;

struct DD_LINE
{
  typedef float DD_TYPE_ANGLE;
  
  struct DD_VERTEX
  {
    FLOAT x, y, z, rhw; // The transformed position for the vertex.
    DWORD color;        // The vertex color.
  } vertex;
  bool XSign, YSign;
  DD_TYPE_ANGLE angle;
} *line;                // &#1084;&#1072;&#1089;&#1080;&#1074; &#1090;&#1086;&#1095;&#1086;&#1082;



// calculate
void CALLBACK CalculateFrame(UINT uTimerID, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR)
{
  if(Semaphore)
  {
    slow_pc = 1;
    return;
  }
  
  Semaphore = 1;
  EnterCriticalSection(&cs);
  
  int j = 0;
  for(; j < DD_DOTS_MAX-1; ++j)
  {
    if(line[j].vertex.x <= 0 || line[j].vertex.x >= screen.x)
    {
     line[j].XSign = !line[j].XSign;
    }
    if(line[j].vertex.y <= 0 || line[j].vertex.y >= screen.y)
    {
     line[j].YSign = !line[j].YSign;
    }
    
    line[j].XSign ? line[j].vertex.x += cosf(line[j].angle)*mov : line[j].vertex.x -= cosf(line[j].angle)*mov;//= (rand() % screen.x);
    line[j].YSign ? line[j].vertex.y += sinf(line[j].angle)*mov : line[j].vertex.y -= sinf(line[j].angle)*mov;//= (rand() % screen.x);






  }
  line[DD_DOTS_MAX-1].vertex.x = line[0].vertex.x;
  line[DD_DOTS_MAX-1].vertex.y = line[0].vertex.y;




  Semaphore = 0;
  LeaveCriticalSection(&cs);

}


// win-prok
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
{

  switch(msg)
  {
    case WM_CREATE:
      // &#1089;&#1090;&#1074;&#1086;&#1088;&#1102;&#1108;&#1084;&#1086; &#1090;&#1086;&#1095;&#1082;&#1080;
      line = new DD_LINE[DD_DOTS_MAX];
      
      // &#1086;&#1089;&#1090;&#1072;&#1085;&#1085;&#1102; &#1090;&#1086;&#1095;&#1082;&#1091; &#1085;&#1077; &#1088;&#1091;&#1093;&#1072;&#1090;&#1080;, &#1073;&#1086; &#1074;&#1086;&#1085;&#1072; &#1110; &#1090;&#1072;&#1082; &#1073;&#1091;&#1076;&#1077; &#1082;&#1086;&#1087;&#1110;&#1102;&#1074;&#1072;&#1090;&#1080;&#1089;&#1100; &#1110;&#1079; &#1087;&#1077;&#1088;&#1096;&#1086;&#1111;
      for(int j = 0; j < DD_DOTS_MAX; ++j)
      {
        line[j].vertex.x = (FLOAT)(rand() % screen.x);
        line[j].vertex.y = (FLOAT)(rand() % screen.y);
        line[j].vertex.z = 0.5f;
        line[j].vertex.rhw = 1.0f;
        line[j].vertex.color = rand()*(INFINITE/RAND_MAX);
        line[j].angle = (rand()%360)*(float)DD_PI/180;

      }

      line[DD_DOTS_MAX-1].vertex.z      = line[0].vertex.z;
      line[DD_DOTS_MAX-1].vertex.rhw    = line[0].vertex.rhw;
      line[DD_DOTS_MAX-1].vertex.color  = line[0].vertex.color;

      rect.left = 0;
      rect.bottom = screen.y;
      rect.top = rect.bottom - DD_FONT_SIZE*2;
      rect.right = screen.x;

      InitializeCriticalSection(&cs);
      timeBeginPeriod(1);
      hTimer = timeSetEvent(DD_TIME_CALC, 0, (LPTIMECALLBACK)CalculateFrame, (DWORD_PTR)0, TIME_KILL_SYNCHRONOUS | TIME_PERIODIC);
      break;
    
    case WM_MOUSEMOVE:
      if(!mouse_init)
      {
        ++mouse_init;
        mouse_pos = l;
        return DefWindowProc(hwnd, msg, w, l);
      }
      if(mouse_pos == l)
       break;
    case WM_KEYDOWN:
    case WM_KEYUP:
    case WM_SYSKEYDOWN:
    case WM_SYSKEYUP:
    case WM_LBUTTONDOWN:
    case WM_RBUTTONDOWN:
    case WM_MBUTTONDOWN:
#ifndef DD_DEBUG
      SendMessage(hwnd, WM_DESTROY, 0, 0);
#endif
      break;

    case WM_DESTROY:
      timeKillEvent(hTimer);
      timeEndPeriod(1);
      DeleteCriticalSection(&cs);
      
      // &#1074;&#1080;&#1076;&#1072;&#1083;&#1103;&#1108;&#1084;&#1086; &#1090;&#1086;&#1095;&#1082;&#1080;
      delete[] line;
      PostQuitMessage(0);
      break;
    
    case WM_PAINT:
      
//      BeginPaint(hwnd, &ps);
//      EndPaint(hwnd, &ps);
      DD_CHECK(lpd3dd->Clear(0, NULL, D3DCLEAR_TARGET, (D3DCOLOR)0, 1.0f, 0));
      void* ppbData;
                                 //sizeof(DD_LINE::DD_VERTEX) * DD_DOTS_MAX
      DD_CHECK(lpd3dvb->Lock(0, 0, (BYTE **)&(ppbData), 0));//sizeof(vertex)
      for(int k = 0; k < DD_DOTS_MAX; ++k)
      {
        memcpy(ppbData, &(line[k].vertex), sizeof(DD_LINE::DD_VERTEX));
        
        cData = (char *)ppbData;
        cData += sizeof(DD_LINE::DD_VERTEX);
        ppbData = cData;
      }
      DD_CHECK(lpd3dvb->Unlock());

      DD_CHECK(lpd3dd->BeginScene());
      DD_CHECK(lpd3dd->SetStreamSource(0, lpd3dvb, sizeof(DD_LINE::DD_VERTEX)));
      DD_CHECK(lpd3dd->SetVertexShader(D3DFVF_XYZRHW | D3DFVF_DIFFUSE));
      DD_CHECK(lpd3dd->DrawPrimitive(D3DPT_LINESTRIP, 0, DD_DOTS_MAX-1));
      
      fc.Inc();
      if(draw_text++ == DD_TEXT_INTERVAL)
      {
        if(slow_pc)
        {
          strcpy(pSlow, "; Your PC too slow for rendering frames");
          slow_pc = 0;
        }
        sprintf(status, "dd screensaver_lines; beta; 2005/05/18, 2005/05/19; requires Direct3D 8, Direct3DX%s; rendering %d fps", pSlow, fc.Get());
        draw_text = 0;
      }
      lpd3dxfont->DrawText(status, (INT)strlen(status), &rect, 0, 0xFFFFFFFF);

      DD_CHECK(lpd3dd->EndScene());
      DD_CHECK(lpd3dd->Present(NULL, NULL, NULL, NULL));
      break;

    default:
      return DefWindowProc(hwnd, msg, w, l);
  }
  return 0;


}

// winmain
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR cmd, int)
{

  srand((unsigned)time(NULL)); 

  WNDCLASS wc = { 0, WndProc, 0, 0, hInst, (HICON)NULL, (HCURSOR)NULL, (HBRUSH)(COLOR_BACKGROUND+1), 0, DD_CLASSNAME };
  DD_WINCHECK(RegisterClass(&wc));

  screen.x = GetSystemMetrics(SM_CXSCREEN);
  screen.y = GetSystemMetrics(SM_CYSCREEN);
  HWND hwnd = CreateWindow(DD_CLASSNAME, NULL, WS_POPUP | WS_VISIBLE, 0, 0, screen.x, screen.y, GetDesktopWindow(), (HMENU)NULL, hInst, NULL);
  DD_WINCHECK(hwnd);
  ShowCursor(FALSE);

  // &#1089;&#1090;&#1074;&#1086;&#1088;&#1102;&#1108;&#1084; Direct3D
  LPDIRECT3D8 lpd3d = Direct3DCreate8(D3D_SDK_VERSION);
  DD_WINCHECK(lpd3d);
  
  // &#1076;&#1110;&#1089;&#1090;&#1072;&#1108;&#1084; &#1110;&#1085;&#1092;&#1086;&#1088;&#1084;&#1072;&#1094;&#1110;&#1102; &#1087;&#1088;&#1086; &#1077;&#1082;&#1088;&#1072;&#1085;
  D3DDISPLAYMODE d3ddm;
  DD_DCHECK(lpd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm));

  // &#1086;&#1087;&#1094;&#1110;&#1111; &#1076;&#1083;&#1103; application
  D3DPRESENT_PARAMETERS d3dpp; 
  memset(&d3dpp, 0, sizeof(d3dpp));
  d3dpp.Windowed         = TRUE;
  d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
  
  d3dpp.BackBufferFormat = d3ddm.Format;
  d3dpp.BackBufferCount  = 1;
  
  d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;

//  d3dpp.MultiSampleType  = D3DMULTISAMPLE_NONE


  // &#1089;&#1090;&#1074;&#1086;&#1088;&#1102;&#1108;&#1084; device
  if(D3DERR_NOTAVAILABLE == lpd3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &lpd3dd))
   if(D3DERR_NOTAVAILABLE == lpd3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &lpd3dd))
    if(D3DERR_NOTAVAILABLE == lpd3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &lpd3dd))
     return -1;
  DD_DCHECK(D3D_OK);

/*  
  DD_CHECK(lpd3dd->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE));
  DD_CHECK(lpd3dd->SetRenderState(D3DRS_DITHERENABLE, TRUE));
  
  DD_CHECK(lpd3dd->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR));
  DD_CHECK(lpd3dd->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR));
*/
  DD_DCHECK(lpd3dd->CreateVertexBuffer(DD_DOTS_MAX * sizeof(DD_LINE::DD_VERTEX), 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &lpd3dvb));

  
  HFONT hFont = CreateFont(DD_FONT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, VARIABLE_PITCH | FF_SWISS, NULL);
  DD_DCHECK(D3DXCreateFont(lpd3dd, hFont, &lpd3dxfont));

  MSG msg;
  while(GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  DD_RELEASE(lpd3dxfont);
  DD_RELEASE(lpd3dvb);
  DD_RELEASE(lpd3dd);
  DD_RELEASE(lpd3d);

  return (int)(msg.wParam ? msg.wParam : -dd_app_init);

}



[Edited by - Coder on September 9, 2005 2:55:45 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by cpp forever
Maybe smbody help me to find out the reason of crash


You haven't described the crash. Nor have you described what the program is supposed to do. Nor have you described what you have already tried to find/correct the problem.

Just dumping a bunch of code in the forum and asking us to fix your problem is not the best way to go about getting help.

Please provide the following information:

1) A brief description of what the program is supposed to do.
2) A detailed description of the problem you have encountered.
3) A list of the things you have already tried in debugging the problem.

Also, please use source tags when posting large amounts of code.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!