add save
This commit is contained in:
parent
ca01498cb2
commit
70861d3e35
8
inc/sys.hpp
Normal file
8
inc/sys.hpp
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
|
||||
namespace sys
|
||||
{
|
||||
size_t saveDataToFile(const char *filename, const char *data, size_t size);
|
||||
size_t loadDataFromFile(const char *filename, char *data, size_t size);
|
||||
bool fileExists(const char *filename);
|
||||
}
|
@ -4,8 +4,6 @@
|
||||
#include <cinttypes>
|
||||
#include <array>
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
#define MAX_DEPTH 8
|
||||
#define MAX_POSIBLE_DEPTH 11
|
||||
static_assert(MAX_DEPTH <= MAX_POSIBLE_DEPTH);
|
||||
|
@ -14,8 +14,6 @@ void App::init(int screenWidth, int screenHeight)
|
||||
this->screenHeight = screenHeight;
|
||||
this->canvas.init(screenWidth);
|
||||
|
||||
mrand::setSeed(1);
|
||||
|
||||
canvasTexure = LoadRenderTexture(screenWidth, screenWidth);
|
||||
manager.init();
|
||||
unit = manager.next();
|
||||
|
57
src/sys.cpp
Normal file
57
src/sys.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include "sys.hpp"
|
||||
#include <raylib.h>
|
||||
|
||||
namespace sys
|
||||
{
|
||||
|
||||
const char *transformFilePath(const char *filename)
|
||||
{
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
return TextFormat(
|
||||
"%s/%s", GetAndroidApp()->activity->internalDataPath, filename);
|
||||
#else
|
||||
return filename;
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t saveDataToFile(const char *filename, const char *data, size_t size)
|
||||
{
|
||||
const char *saveFilePath = transformFilePath(filename);
|
||||
|
||||
FILE *file = fopen(saveFilePath, "wb");
|
||||
if (file == NULL)
|
||||
return false;
|
||||
|
||||
size_t ret = fwrite(data, 1, size, file);
|
||||
fclose(file);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t loadDataFromFile(const char *filename, char *data, size_t size)
|
||||
{
|
||||
const char *saveFilePath = transformFilePath(filename);
|
||||
|
||||
FILE *file = fopen(saveFilePath, "rb");
|
||||
if (file == NULL)
|
||||
return false;
|
||||
|
||||
size_t ret = fread(data, 1, size, file);
|
||||
fclose(file);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool fileExists(const char *filename)
|
||||
{
|
||||
const char *saveFilePath = transformFilePath(filename);
|
||||
|
||||
bool result = false;
|
||||
if (access(saveFilePath, F_OK) != -1)
|
||||
result = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +1,27 @@
|
||||
#include <ctime>
|
||||
|
||||
#include "values/DnaManager.hpp"
|
||||
#include "values/mrand.hpp"
|
||||
#include "sys.hpp"
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
void DnaManager::init()
|
||||
{
|
||||
randSeed.a = mrand::getInt();
|
||||
randSeed.b = mrand::getInt();
|
||||
randSeed.c = mrand::getInt();
|
||||
randSeed.d = mrand::getInt();
|
||||
mrand::setSeed(time(nullptr));
|
||||
|
||||
if (sys::fileExists("id"))
|
||||
{
|
||||
sys::loadDataFromFile("id", (char *)&randSeed, sizeof(uint128));
|
||||
}
|
||||
else
|
||||
{
|
||||
randSeed.a = mrand::getInt();
|
||||
randSeed.b = mrand::getInt();
|
||||
randSeed.c = mrand::getInt();
|
||||
randSeed.d = mrand::getInt();
|
||||
sys::saveDataToFile("id", (const char *)&randSeed, sizeof(uint128));
|
||||
}
|
||||
|
||||
vector.resize(NUM_PER_GEN);
|
||||
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
|
||||
@ -22,11 +37,6 @@ void DnaManager::deinit()
|
||||
|
||||
Unit DnaManager::next()
|
||||
{
|
||||
if (queued.empty())
|
||||
{
|
||||
return {nullptr, Liked::tbd};
|
||||
}
|
||||
|
||||
Dna *ret = queued.front();
|
||||
queued.pop_front();
|
||||
showed.push_back(ret);
|
||||
|
Loading…
x
Reference in New Issue
Block a user