Fixed CanvasPortion using the same temp files across processes

This commit is contained in:
Michaël Lemaire 2015-11-18 22:22:09 +01:00
parent 6166031c8b
commit 10f8f46ca6
7 changed files with 42 additions and 3 deletions

1
TODO
View file

@ -6,7 +6,6 @@ Technlology Preview 2 :
- Refactor medium traversal to unify clouds, atmosphere and god rays.
- Fix potential holes in land rendering (OpenGL and software).
- Fix sun size not being consistent between opengl and software
- Fix CanvasPortion saves colliding on disk when running several instances
Technology Preview 3 :
- Alter aerial perspective using estimation of the amount of light left after cloud layers traversal.

View file

@ -1,11 +1,14 @@
#include "CanvasPortion.h"
#include <cassert>
#include <stdlib.h>
#include <vector>
#include "CanvasPixel.h"
#include "CanvasPreview.h"
#include "PackStream.h"
#include "FileSystem.h"
#include "System.h"
#define CHECK_COORDINATES() \
assert(x >= 0); \
@ -14,6 +17,15 @@
assert(y < height); \
assert(pixels != NULL)
// Keep track of created files to erase them at program exit
static std::vector<std::string> _files;
static void clean_all_files() {
for (auto &filepath : _files) {
FileSystem::removeFile(filepath);
}
}
static int _atexit = std::atexit(clean_all_files);
CanvasPortion::CanvasPortion(int index, CanvasPreview *preview) : index(index), preview(preview) {
width = 1;
height = 1;
@ -73,7 +85,9 @@ void CanvasPortion::discardPixels(bool save) {
void CanvasPortion::saveToDisk() {
if (pixels) {
filepath = FileSystem::getTempFile("paysages_portion_" + std::to_string(index) + ".dat");
auto pid = System::getProcessId();
filepath =
FileSystem::getTempFile("paysages_portion_" + std::to_string(index) + "_" + std::to_string(pid) + ".dat");
PackStream stream;
stream.bindToFile(filepath, true);
stream.write(&width);
@ -83,6 +97,7 @@ void CanvasPortion::saveToDisk() {
pixels[y * width + x].getComposite().save(&stream);
}
}
_files.push_back(filepath);
}
}

View file

@ -2,6 +2,7 @@
#include <QDir>
#include <QFileInfo>
#include <cstdio>
std::string FileSystem::getTempFile(const std::string &filename) {
return QDir::temp().filePath(QString::fromStdString(filename)).toStdString();
@ -10,3 +11,9 @@ std::string FileSystem::getTempFile(const std::string &filename) {
bool FileSystem::isFile(const std::string &filepath) {
return QFileInfo(QString::fromStdString(filepath)).exists();
}
bool FileSystem::removeFile(const std::string &filepath) {
if (FileSystem::isFile(filepath)) {
remove(filepath.c_str());
}
}

View file

@ -19,6 +19,11 @@ class SYSTEMSHARED_EXPORT FileSystem {
* Returns true if the given path points to a file.
*/
static bool isFile(const std::string &filepath);
/**
* Remove a file by its absolute path.
*/
static bool removeFile(const std::string &filepath);
};
}
}

View file

@ -133,6 +133,6 @@ void PackStream::skip(const double &value, int count) {
stream->skipRawData(sizeof(value) * count);
}
void paysages::system::PackStream::skipBytes(int bytes) {
void PackStream::skipBytes(int bytes) {
stream->skipRawData(bytes);
}

View file

@ -1,6 +1,7 @@
#include "System.h"
#include <QThread>
#include <QCoreApplication>
static int core_count = -1;
@ -18,3 +19,7 @@ int System::getCoreCount() {
}
return core_count;
}
int System::getProcessId() {
return QCoreApplication::applicationPid();
}

View file

@ -11,7 +11,15 @@ namespace system {
*/
class SYSTEMSHARED_EXPORT System {
public:
/**
* Get the number of CPU cores available on current system.
*/
static int getCoreCount();
/**
* Get an identifying number for the current process.
*/
static int getProcessId();
};
}
}