Reading multiple serialized objects from a file - c++

While making my Breakout game in MFC I chose to serialize my CBlock objects so the player can make its own "maps" on a level editor I made, everything is fine while saving; everything I do is to serialize all my CBlock objects with a for since I have all my CBlock objects on a vector<CBlock*>. The problem comes when reading the files. The main problem is that my program does not know how to stop reading when the end of the file ends. I've trying most of the bool methods from CArchive. The only one that kinda worked was CArchive::IsLoading() but the problem is that it still continues to read from file despite having reached the end of the file.
CString m_filter = TEXT("Super Breakout Maker Files (*.sbm)|*.sbm|All Files (*.*)|*.*||");
CFile m_loadFile;
CFileDialog m_fileDlg(TRUE, TEXT(".sbm"), TEXT("mylayout"), OFN_HIDEREADONLY, m_filter, NULL, 0, TRUE);
//CFileDialog m_fileDlg(FALSE, TEXT(".sbm"), TEXT("mylayout"), 0, m_filter);
if (m_fileDlg.DoModal() == IDOK)
{
if (m_blockLayout.size() > 0)
{
for (int i = 0; i < m_blockLayout.size(); i++)
{
delete(m_blockLayout[i]);
}
m_blockLayout.clear();
}
m_loadFile.Open(m_fileDlg.GetFileName(), CFile::modeRead);
CArchive m_loadArchive(&m_loadFile, CArchive::load);
while (m_loadArchive.IsLoading()==true)
{
CBlock* block = new CBlock;
block->Serialize(m_loadArchive);
block->AssignBlockType();
block->m_blockState.CreateCompatibleDC(pDC);
LoadBlock(block->m_blockState, m_blockStateArray, block->GetBlockType(), block->GetHits());
m_blockLayout.push_back(block);
}
m_loadArchive.Close();
if (m_blockLayout.size() > 0)
{
MessageBox(TEXT("Your layout was successfully loaded! Resume game to play."), TEXT("Notification"), MB_ICONINFORMATION);
m_pBall->SetInitialPosition(m_ballBMP, m_pBar, m_barBMP);
}
else
{
MessageBox(TEXT("Unable to read file. Corrupted or incompatible data."), TEXT("Notification"), MB_ICONERROR);
}
}
else
{
return;
}
m_loadFile.Close();
This is my loading method, but as I mentioned, the iteration does not know where to end reading.

Couple of simpler options, easiest would be to store how many blocks are in the file as the first thing in the file before your blocks. Read for that many blocks, then exit.
have a predefined block with specific attributes and member variables that is your "End Of File" Block, whenever you read that specific block, you know to discard it and stop reading.
The other alternative is to just have some specific string at the end of all your block data and check the next line after every block is read for that specific string (this would require some pointer variable for your line in the file though)

Related

MFC serialize C++

I'm trying to serialize a listbox in MFC.
I used this code :
CFileDialog fileDlg(FALSE, _T(".txt"), NULL, 0, _T("Text File (.txt)|*.txt|")
_T("All files (*.*)|*.*||"));
if (fileDlg.DoModal() == IDOK)
{
const int numItems = m_listBox.GetCount();
CString itemText;
CStdioFile file;
if (file.Open(fileDlg.GetFileName(), CStdioFile::modeCreate | CStdioFile::modeWrite))
{
for (int i = 0; i < numItems; ++i)
{
m_listBox.GetText(i, itemText);
file.WriteString(itemText);
file.WriteString(_T("\n"));
}
file.Close();
}
But the saved file is always empty.
I tried a lot of versions of functions that save from listbox to text file but it didn't work.
In your code you are using CFileDialog::GetFileName. Is that intentional? That will only pass in the file name.
I would use CFileDialog::GetPathName which returns the full path to the file.
And you should be using a debugger (compile in debug mode) so you can walk your code.
The CStdioFile::Open method can also be passed a pointer to an exception object. Have a look at the help.

Parsing yaml files with yaml-cpp

I'm having problem with parsing yaml files using yaml-cpp, I'm making a application using wxWidgets and I'm trying to read the frame size from a yaml file which looks like,
---
This is the configuration file for the Sample Browser,
feel free to edit this file as needed
...
Window:
SizeW: 1280
SizeH: 720
Media:
Autoplay: false
And this the code that should handle the parsing,
int sizeH, sizeW;
try
{
YAML::Node config = YAML::LoadFile("/home/apoorv/repos/cpp-projects/wxWidgets/SampleBrowser/build/config.yaml");
if (!config["Window"])
{
wxLogDebug("Error! Cannot fetch values.");
}
sizeH = config["SizeH"].as<int>();
sizeW = config["SizeW"].as<int>();
}
catch(const YAML::ParserException& ex)
{
std::cout << ex.what() << std::endl;
}
this->SetSize(sizeW, sizeH);
But when I try to parse this file and set the frame size this->SetSize() it errors out saying *** Caught unhandled unknown exception; terminating.
Since SizeH and SizeW are children of Window, your two lines should look like
sizeH = config["Window"]["SizeH"].as<int>();
sizeW = config["Window"]["SizeW"].as<int>();
or, merged with the previous check,
if (auto window = config["Window"]) {
sizeH = window["SizeH"].as<int>();
sizeW = window["SizeW"].as<int>();
} else {
wxLogDebug("Error! Cannot fetch values.");
}
Generally, the error handling is bad. In your code, if an error is encountered, sizeH and sizeW are not set but are still given to SetSize. This is undefined behavior. You should initialize them with some default values, e.g.
int sizeH = 480, sizeW = 640;
Also, since you keep us in the dark about what this is, there may be other errors.
It's fine to try to do this for learning yaml-cpp, but if you really want to save/restore your frame geometry, you should use wxPersistentTLW instead. To use it, just call wxPersistentRegisterAndRestore(frame, "NameToIdentifyYourFrame") after creating your frame, see the manual for more details.
In the cross-platform environment it is better to save the client size of the window, as for GTK what's important is the client size. There might be other OSes/toolkits where this is the case.

How do I make a custom event with VTK?

I'm making a thread software with VTK, where I need to change the model itself in real time, while I need to change his method of rendering. Everything is working fine, but, the problem start with the interactor->start(); , the model data gets updated just fine, but it's only showed on screen when I move The camera. Also I have selected some methods for generating a 3D data from a imagedata file, for that I need to close the vtk window (interactor window) and then the code will reopen it and send the new data generated to it...
I would need something like these:
int force close_window = false; int refresh_interactor = false;
I managed to make the Window close, but only with vtkcommand::Keypressed command, but idk how do I do with a new command :S, I tried the vtkcommand::UserEvent but I didn't found a good information about how to deal with that data (like some way to call it)
the way I'm dealing with VTK is with two threads, the first one, is just about the vtk iren loop, and the second one would manage the models and check if iren requires to be updated.
In my dream code it should be something like this:
=======================================================
bool VTKWindow()
{
...
vtkSmartPointer ator = vtkSmartPointer::New();
iren = vtkSmartPointer::New();
RenWindow = vtkSmartPointer::New();
render->SetBackground(.1, .2, .3);
RenWindow->AddRenderer(renderer);
iren->SetRenderWindow(RenWindow);
if(data_type == voxel_type)
{
Render->AddViewProp(VoxelData);
}
else
{
actor->SetMapper(PolyData);
Render->AddActor(Actor);
}
RenWindow->Render();
iren->Start();
}
void ManageVTK()
{
while true loop...
if(force close_window == true)
do some command to exit the iren loop
if(refresh_interactor == true)
do some command to refresh iren
}
Sorry for the english, it's not my native language, and also sorry about the question format, it's the first time I'm using stackoverflow
It may sounds stupid, but, I found a kind of solution for the problem.
I saw on related links this guy vtkRenderWindowInteractor event loop and threading and, it's almost the same problem...
class VTKNewEvent : public vtkCommand{
public:
vtkTypeMacro(VTKNewEvent , vtkCommand);
static VTKNewEvent * New(){
return new VTKNewEvent ;
}
void Execute(vtkObject * caller, unsigned long vtkNotUsed(eventId), void * vtkNotUsed(callData)){
vtkRenderWindowInteractor *iren = static_cast<vtkRenderWindowInteractor*>(caller);
if (iren_close == true){
iren->GetRenderWindow()->Finalize // Stop the interactor
iren->TerminateApp();
iren_close = false;
}
if (iren_update== true){
renderJanela->Render();
iren_update= false;
}
}
};
bool VTKWindow(){
vtkSmartPointer<VTKNewEvent > IrenRefresh= vtkSmartPointer<VTKNewEvent>::New();
...
iren->CreateRepeatingTimer(1);//this makes that IrenRefresh will be called at every 1ms
iren->AddObserver(vtkCommand::TimerEvent, IrenRefresh);
iren->Start();
...
}
it's simple, but, maybe not the best, but it did Th job, I hope this link will help people that are starting into the VTK world, since threads + rendering loop wasn't a simple job to understand what was going on

Creating and Saving New Photos Win32 Webcam Application

I am trying to make my webcam Win32 application save more than one photo at a time i.e. save one photo then another then another etc.
To do this, I am appending a string to an integer variable so that each new photo name can be unique and conform to the format of the second argument of CreateBMPFile. This would usually be a case of writing TEXT("string literal"), but I need to keep modifying the filename as each new photo is created.
PBITMAPINFO pbi = CreateBitmapInfoStruct(hwnd, hbm);
int i = 1;
std::string image = "..\\..\\..\\..\\WebCam\\frame" + std::to_string(i) + ".bmp";
while (!exists(image)) {
LPTSTR filename = (LPTSTR)image.c_str();
CreateBMPFile(hwnd, filename, pbi, hbm, hdcMem);
i++;
}
This compiles and executes, however, when I click the "Grab Frame" button and it attempts to save it, the application crashes i.e. I cannot see the GUI anymore and it becomes a stagnant process.
I am using an exists() function to see whether the file exists in the system:
inline bool exists(const std::string& name) {
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}
I've also tried using sprintf_s() with the same result of crashing the application.

Windows phone 8 on emulator - why can't I play audio files?

I'm converting an app that was written in Silverlight, and so far I've succeeded in solving all of the problems, except for one:
For some reason, the emulator refuses to play any audio files of the app, and it doesn't even throw an exception. I've checked, and in the ringtone category it can make sounds.
The original code was :
<Grid x:Name="sharedFullScreenFilePathContainer"
Tag="{Binding StringFormat=\{0\},Converter={StaticResource fullScreenImageConverter}}">
<Image x:Name="fullScreenImage" Stretch="Fill"
Source="{Binding ElementName=sharedFullScreenFilePathContainer,Path=Tag, StringFormat=../Assets/images/\{0\}.jpg}"
ImageFailed="onFullScreenImageFailedToLoad" MouseLeftButtonDown="onPressedOnFullScreenImage" />
<MediaElement x:Name="mediaPlayer" AutoPlay="True"
Source="{Binding ElementName=sharedFullScreenFilePathContainer,Path=Tag, StringFormat=../Assets/sounds/\{0\}.wma}" />
</Grid>
so, the image that I set to this item's context is really shown, but the sound that really exists on the path I set to it doesn't play (I've checked in the "Bin" folder).
I've tried to use code instead of xaml, but I still have the same problem.
I've tried this (though it's usually used for background music):
AudioTrack audioTrack = new AudioTrack(new Uri("../Assets/sounds/" + fileToOpen, UriKind.Relative), "", "", "", null);
BackgroundAudioPlayer player = BackgroundAudioPlayer.Instance;
player.Track = audioTrack;
player.Play();
It didn't play anything, and also didn't throw any exception.
I've also tried the next code, but it throws an exception (file not found exception) probably because I don't call it right:
Stream stream = TitleContainer.OpenStream("#Assets/sounds/" + fileToOpen);
SoundEffect effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
I've also tried using wma files but it also didn't work.
I also tried to play with the "copy to output directory" parameter of the mp3 files (to "always" and "only if new" ) and with the "build action" parameter (to "none" and "content" ). Nothing helps.
Can anyone please help me? I didn't develop for Silverlight/WP for a very long time and I can't find out how to fix it .
Btw, since later I need to know when the sound has finished playing (and also to be able to stop it), I would like to use code anyway. I would be happy if you could also tell me how to do it too (I can ask it on a new post if needed).
EDIT:
ok , i've found out the problem : i kept getting a weird exception when using the MediaPlayer.Play() method , and after checking out about the exception , i've found out that it's a known issue , and that i need to call FrameworkDispatcher.Update(); right before i call the Play() method .
so the solution would be to do something like this:
Song song = Song.FromUri(...);
MediaPlayer.Stop();
FrameworkDispatcher.Update();
MediaPlayer.Play(song);
the exception is:
'System.InvalidOperationException' occurred in System.Windows.ni.dll"
i've found the solution here .
Now the question is why , and how come I didn't find anything related to it in the demos of windows phone ? Also , i would like to know what this function does .
ok , since nobody gave me an answer to both questions , and I still wish to give the bounty , I will ask another question :
If there is really no solution other than using the MediaPlayer class for windows phone , how do i capture the event of finishing playing an audio file ? Even getting the audio file duration doesn't work (keeps returning 0 length , no matter which class i've tried to use) ...
The BackgroundAudioPlayer can play files only from isolated storage or from a remote URI, that is why you can here anything!
If you have your file as resources in your app, you must first copy them to the isolated store, and then make a reference to the file in the isolated store to your BackgroundAudioPlayer.
private void CopyToIsolatedStorage()
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
string[] files = new string[]
{ "Kalimba.mp3",
"Maid with the Flaxen Hair.mp3",
"Sleep Away.mp3" };
foreach (var _fileName in files)
{
if (!storage.FileExists(_fileName))
{
string _filePath = "Audio/" + _fileName;
StreamResourceInfo resource = Application.GetResourceStream(new Uri(_filePath, UriKind.Relative));
using (IsolatedStorageFileStream file = storage.CreateFile(_fileName))
{
int chunkSize = 4096;
byte[] bytes = new byte[chunkSize];
int byteCount;
while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0)
{
file.Write(bytes, 0, byteCount);
}
}
}
}
}
}
And then you can make a list of your songs
private static List<AudioTrack> _playList = new List<AudioTrack>
{
new AudioTrack(new Uri("Kalimba.mp3", UriKind.Relative),
"Kalimba",
"Mr. Scruff",
"Ninja Tuna",
null),
new AudioTrack(new Uri("Maid with the Flaxen Hair.mp3", UriKind.Relative),
"Maid with the Flaxen Hair",
"Richard Stoltzman",
"Fine Music, Vol. 1",
null),
new AudioTrack(new Uri("Sleep Away.mp3", UriKind.Relative),
"Sleep Away",
"Bob Acri",
"Bob Acri",
null),
// A remote URI
new AudioTrack(new Uri("http://traffic.libsyn.com/wpradio/WPRadio_29.mp3", UriKind.Absolute),
"Episode 29",
"Windows Phone Radio",
"Windows Phone Radio Podcast",
null)
};
And play your tracks!
private void PlayNextTrack(BackgroundAudioPlayer player)
{
if (++currentTrackNumber >= _playList.Count)
{
currentTrackNumber = 0;
}
PlayTrack(player);
}
private void PlayPreviousTrack(BackgroundAudioPlayer player)
{
if (--currentTrackNumber < 0)
{
currentTrackNumber = _playList.Count - 1;
}
PlayTrack(player);
}
private void PlayTrack(BackgroundAudioPlayer player)
{
// Sets the track to play. When the TrackReady state is received,
// playback begins from the OnPlayStateChanged handler.
player.Track = _playList[currentTrackNumber];
}
If you want the MediaElement to work from your code you can do something like this!
MediaElement me = new MediaElement();
// Must add the MediaElement to some UI container on
// your page or some UI-control, otherwise it will not play!
this.LayoutRoot.Children.Add(me);
me.Source = new Uri("Assets/AwesomeMusic.mp3", UriKind.RelativeOrAbsolute);
me.Play();
Use the MediaElement instead, this can play media files stored in your application, but stops playing when the application stops (you can make some advance chance to your app so it keep running, but it will not work very well)
In your XAML:
<Button x:Name="PlayFile"
Click="PlayFile_Click_1"
Content="Play mp3" />
In your Code behind:
MediaElement MyMedia = new MediaElement();
// Constructor
public MainPage()
{
InitializeComponent();
this.LayoutRoot.Children.Add(MyMedia);
MyMedia.CurrentStateChanged += MyMedia_CurrentStateChanged;
MyMedia.MediaEnded += MyMedia_MediaEnded;
}
void MyMedia_MediaEnded(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Ended event " + MyMedia.CurrentState.ToString());
// Set the source to null, force a Close event in current state
MyMedia.Source = null;
}
void MyMedia_CurrentStateChanged(object sender, RoutedEventArgs e)
{
switch (MyMedia.CurrentState)
{
case System.Windows.Media.MediaElementState.AcquiringLicense:
break;
case System.Windows.Media.MediaElementState.Buffering:
break;
case System.Windows.Media.MediaElementState.Closed:
break;
case System.Windows.Media.MediaElementState.Individualizing:
break;
case System.Windows.Media.MediaElementState.Opening:
break;
case System.Windows.Media.MediaElementState.Paused:
break;
case System.Windows.Media.MediaElementState.Playing:
break;
case System.Windows.Media.MediaElementState.Stopped:
break;
default:
break;
}
System.Diagnostics.Debug.WriteLine("CurrentState event " + MyMedia.CurrentState.ToString());
}
private void PlayFile_Click_1(object sender, RoutedEventArgs e)
{
// Play Awesome music file, stored as content in the Assets folder in your app
MyMedia.Source = new Uri("Assets/AwesomeMusic.mp3", UriKind.RelativeOrAbsolute);
MyMedia.Play();
}
Audio track plays audio files stored in isolated storage or streamed over the internet.
Playing audio files stored within the application package works fine for me. I got files named like "Alarm01.wma" in project folder Resources\Alarms. I then play these sounds like this:
using Microsoft.Xna.Framework.Media;
...
Song s = Song.FromUri("alarm", new Uri(#"Resources/Alarms/Alarm01.wma", UriKind.Relative));
MediaPlayer.Play(s);
Also don't forget to reference Microsoft.Xna.Framework library.
I guess it should work fine for mp3 files and files stored in IsolatedStorage as well.