Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What's wrong with my code ?  (Read 2946 times)

0 Members and 1 Guest are viewing this topic.

stng2003

  • Newbie
  • *
  • Posts: 3
    • View Profile
What's wrong with my code ?
« on: August 07, 2011, 05:31:57 am »
Hi,I decide to write a little program with SFML using VC.

I write a Dialog based program, and declare a sf::RenderWindow pointer "ttl2D" in dialog class .

The pointer is initiaized in OnInitDialog() method, and attach a CStatic control:

Code: [Select]

    ttl2D = new sf::RenderWindow( GetDlgItem(IDC_STATIC_2D)->GetSafeHwnd(),ws );


In the OnPaint method,I write

Code: [Select]

    ttl2D->Clear();
    ttl2D->Display();


I hope the static control should be filled with black,but nothing happened with the static contol.

I wonder whether there was something wrong with my code ?

Thanx!

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
What's wrong with my code ?
« Reply #1 on: August 07, 2011, 10:57:42 pm »
It'd be nice if you gave us a complete and minimal code that recreates the problem, complete meaning it can be compiled on its own, and minimal meaning one file, and only containing code related to the problem.
I use the latest build of SFML2

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
What's wrong with my code ?
« Reply #2 on: August 08, 2011, 12:48:25 am »
Quote from: "OniLink10"
It'd be nice if you gave us a complete and minimal code that recreates the problem, complete meaning it can be compiled on its own, and minimal meaning one file, and only containing code related to the problem.

I assume he's using MFC since he said dialog-based application so he'd likely have to provide the entire solution.

stng2003

  • Newbie
  • *
  • Posts: 3
    • View Profile
What's wrong with my code ?
« Reply #3 on: August 08, 2011, 03:33:17 pm »
My code:

TestSFMLDlg.h ->

Code: [Select]


#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 ->

Code: [Select]


#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  ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
What's wrong with my code ?
« Reply #4 on: August 08, 2011, 03:37:50 pm »
That's why we asked for a complete and minimal source code... in order to avoid wasting time with a huge amount of code which is not related to the problem ;)
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
What's wrong with my code ?
« Reply #5 on: August 08, 2011, 03:41:03 pm »
Quote
So I think when use RenderWindow with mfc, we shouldnt call base class's OnPaint() method . right ?

Yes, I don't think that you should call the base class' OnPaint() function, since you're overriding the paint event completely.
Laurent Gomila - SFML developer

stng2003

  • Newbie
  • *
  • Posts: 3
    • View Profile
What's wrong with my code ?
« Reply #6 on: August 08, 2011, 03:43:44 pm »
Quote from: "Laurent"
Yes, I don't think that you should call the base class' OnPaint() function, since you're overriding the paint event completely.


Thanx. Its my first sfml-program.  :wink: