2
« on: August 08, 2011, 03:33:17 pm »
My code:
TestSFMLDlg.h ->
#pragma once
//////////////////////////////////////////////////////////////////////////
static UINT FFT_MSG = ::RegisterWindowMessage(_T("fft thread msg"));
//////////////////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#ifdef _DEBUG
#pragma comment(lib,"sfml-window-d.lib")
#pragma comment(lib,"sfml-graphics-d.lib")
#pragma comment(lib,"sfml-system-d.lib")
#else
#pragma comment(lib,"sfml-window.lib")
#pragma comment(lib,"sfml-graphics.lib")
#pragma comment(lib,"sfml-system.lib")
#endif
#include "fftw3.h"
#pragma comment(lib, "libfftw3-3.lib")
class CTestSFMLDlg : public CDialog
{
public:
CTestSFMLDlg(CWnd* pParent = NULL);
~CTestSFMLDlg();
enum { IDD = IDD_TESTSFML_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
afx_msg LRESULT OnFFTMsg( WPARAM wp,LPARAM lp );
public:
afx_msg void OnBnClickedOk();
protected:
static UINT Thread(LPVOID lp);
void DrawSpectralLines();
protected:
sf::RenderWindow* ttl2D;
fftw_complex* mpIncomeData;
fftw_complex* mpResultFFTData;
static const int FFT_SIZE;
CString mstrInFileName;
BOOL mbThreadIsRunning;
BOOL mbQuitThread;
};
TestSFMLDlg.cpp ->
#include "stdafx.h"
#include "TestSFML.h"
#include "TestSFMLDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
const int CTestSFMLDlg::FFT_SIZE = 1024;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
CTestSFMLDlg::CTestSFMLDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestSFMLDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
mbQuitThread = mbThreadIsRunning = FALSE;
mpIncomeData = mpResultFFTData = NULL;
}
CTestSFMLDlg::~CTestSFMLDlg()
{
delete ttl2D;
if ( mpIncomeData && mpResultFFTData ) {
fftw_free(mpIncomeData);
fftw_free(mpResultFFTData);
}
}
void CTestSFMLDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CTestSFMLDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, &CTestSFMLDlg::OnBnClickedOk)
ON_REGISTERED_MESSAGE(FFT_MSG,&CTestSFMLDlg::OnFFTMsg)
END_MESSAGE_MAP()
BOOL CTestSFMLDlg::OnInitDialog()
{
CDialog::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
//////////////////////////////////////////////////////////////////////////
sf::WindowSettings ws;
ws.AntialiasingLevel = 1;
ttl2D = new sf::RenderWindow( GetDlgItem(IDC_STATIC_2D)->GetSafeHwnd(),ws );
mpIncomeData = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*FFT_SIZE);
mpResultFFTData = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*FFT_SIZE);
//////////////////////////////////////////////////////////////////////////
return TRUE;
}
void CTestSFMLDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CTestSFMLDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
DrawSpectralLines();
}
HCURSOR CTestSFMLDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CTestSFMLDlg::OnBnClickedOk()
{
CFileDialog dlg(TRUE);
mstrInFileName = dlg.GetPathName();
AfxBeginThread(Thread,this);
}
LRESULT CTestSFMLDlg::OnFFTMsg( WPARAM wp,LPARAM lp )
{
Invalidate();
return 0;
}
UINT CTestSFMLDlg::Thread(LPVOID lp)
{
CTestSFMLDlg* pdlg = (CTestSFMLDlg*)lp;
CFile inFile(pdlg->mstrInFileName,CFile::modeRead);
short __declspec(align(16)) readBuf[ FFT_SIZE ] = {0};
DWORD rd = 0;
//////////////////////////////////////////////////////////////////////////
fftw_plan p = fftw_plan_dft_1d( FFT_SIZE,pdlg->mpIncomeData,pdlg->mpResultFFTData,FFTW_FORWARD,FFTW_MEASURE );
//////////////////////////////////////////////////////////////////////////
while ( !pdlg->mbQuitThread ){
rd = inFile.Read( readBuf,sizeof(readBuf) );
DWORD sampleSize = rd >> 1;
for ( DWORD i = 0 ; i < sampleSize ; i ++ ) {
pdlg->mpIncomeData[i][0] = readBuf[i];
pdlg->mpIncomeData[i][1] = 0.;
}
fftw_execute(p);
pdlg->PostMessage( FFT_MSG,0,0 );
Sleep(200);
}
//////////////////////////////////////////////////////////////////////////
fftw_destroy_plan(p);
//////////////////////////////////////////////////////////////////////////
return 0;
}
void CTestSFMLDlg::DrawSpectralLines()
{
ttl2D->Clear();
ttl2D->Display();
}
In OnPaint(), if I remove "CDialog::OnPaint();", the result is correct.
So I think when use RenderWindow with mfc, we shouldnt call base class's OnPaint() method . right ?