For some reason the simple program I was working on stopped working. And it happened ever since it decided to ask me for the location of ONEXIT.C after I press F5. Anyone know what that is about? Also it crashes when I try to CListCtrl.Insertitem.
And I have a feeling I have some bad programming techniques. Here is a simple dialog class that I use simply to load a WAV sample and play it using a ListControl. I have no idea if I need to delete the pointers or not. I thought maybe I only delete pointers if I say 'new'. Anyways takes a look and let me know if there is a better way
And I have a feeling I have some bad programming techniques. Here is a simple dialog class that I use simply to load a WAV sample and play it using a ListControl. I have no idea if I need to delete the pointers or not. I thought maybe I only delete pointers if I say 'new'. Anyways takes a look and let me know if there is a better way
- Code:
// PlayerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "PlayerDlg.h"
#include <fmod.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPlayerDlg dialog
FSOUND_SAMPLE *sndarr[30];
FSOUND_SAMPLE *testtemp;
CPlayerDlg::CPlayerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPlayerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPlayerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPlayerDlg)
DDX_Control(pDX, IDC_LIST1, m_lcSounds);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPlayerDlg, CDialog)
//{{AFX_MSG_MAP(CPlayerDlg)
ON_BN_CLICKED(IDC_TEST, OnPlay)
ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
ON_BN_CLICKED(IDC_OPENFILE, OnOpenfile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CPlayerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CenterWindow(GetDesktopWindow()); // center to the hpc screen
if (!FSOUND_Init(44100, 32, FSOUND_INIT_GLOBALFOCUS))
{
MessageBox((_T("Could not init")));
exit(1);
}
for (int j = 0;j<30;j++)
{
sounds[j] = "";
}
sounds[0] = "bmyname.wav";
sounds[1] = "byessir.wav";
for (int i = 0;i < 2;i++)
{
int x = m_lcSounds.InsertItem(i, sounds[i]);
if (x == -1)
MessageBox(_T("error!"));
TRACE2("Loading sound: %s into sndarr[%d]",sounds[i],i);
char *pSoundName = GetAnsiString(sounds[i]);
sndarr[i] = FSOUND_Sample_Load(FSOUND_FREE,pSoundName,FSOUND_NORMAL,0);
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CPlayerDlg message handlers
void CPlayerDlg::OnPlay()
{
TRACE1 ("Playing sound located at sndarr[%d] \n", soundindex);
FSOUND_PlaySound(FSOUND_FREE,sndarr[soundindex]);
}
void CPlayerDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
POSITION pos = m_lcSounds.GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = m_lcSounds.GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
soundindex = nItem;
}
}
*pResult = 0;
}
void CPlayerDlg::OnOpenfile()
{
CString message,sPathName,sFileName;
CFileDialog fileDialog(TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, (_T("All Files (*.*)|*.*|")), NULL);
fileDialog.DoModal();
sFileName = fileDialog.GetFileTitle();
sPathName = fileDialog.GetPathName();
TRACE2("Loaded file: %s \nFiles Path: %s \n",sFileName,sPathName);
char *pFileName = GetAnsiString(sFileName);
TRACE0("Going to PopulateListControl function\n");
PopulateListControl (pFileName);
}
void CPlayerDlg::PopulateListControl(char* fn)
{
CString tempstr;
tempstr = sounds[0];
int ind = 0;
while (tempstr != "")
{
ind++;
tempstr = sounds[ind];
}
CString fn2 = fn;
TRACE2("Inserting word: %s at index %d into listcontrol\n",fn2,ind);
int xx = m_lcSounds.InsertItem(ind, fn2);
TRACE2("Loading sample named %s into array at: sndarr[%d]\n",ind,fn2);
sndarr[ind] = FSOUND_Sample_Load(FSOUND_FREE,fn,FSOUND_NORMAL,0);
}
char* CPlayerDlg::GetAnsiString(const CString &s)
{
int nSize = s.GetLength();
char *pAnsiString = new char[nSize+1];
wcstombs(pAnsiString, s, nSize+1);
return pAnsiString;
}