Skip to content

Commit

Permalink
* Add saveImage to Android and SDL AppPlatforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
iProgramMC committed May 2, 2024
1 parent a235833 commit b2b7e63
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 48 deletions.
24 changes: 16 additions & 8 deletions platforms/android/AppPlatform_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,29 @@ void AppPlatform_android::setExternalStoragePath(const std::string& path)
m_storageDir = path;
}

void AppPlatform_android::saveScreenshot(const std::string& fileName, int width, int height)
bool AppPlatform_android::_saveImage(const std::string& saveName, int width, int height, uint32_t* pixels, bool flipVertically)
{
std::string saveName = m_storageDir + "/" + fileName;

LOG_I("Saving in %s", saveName.c_str());

stbi_flip_vertically_on_write(flipVertically);
return stbi_write_png(saveName.c_str(), width, height, 4, pixels, width * 4) != 0;
}

bool AppPlatform_android::saveImage(const std::string& fileName, int width, int height, uint32_t* pixels, bool flipVertically)
{
std::string saveName = m_storageDir + "/" + fileName;
return _saveImage(saveName, width, height, pixels, flipVertically);
}

bool AppPlatform_android::saveScreenshot(const std::string& fileName, int width, int height)
{
int npixels = width * height;
uint32_t* pixels = new uint32_t[npixels];
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

stbi_flip_vertically_on_write(true);

stbi_write_png(saveName.c_str(), width, height, 4, pixels, width * 4);


bool res = saveImage(fileName, width, height, pixels, true);
delete[] pixels;
return res;
}

int AppPlatform_android::getScreenWidth() const
Expand Down
4 changes: 3 additions & 1 deletion platforms/android/AppPlatform_android.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class AppPlatform_android : public AppPlatform
~AppPlatform_android();
void initConsts();
void buyGame() override;
void saveScreenshot(const std::string& fileName, int width, int height) override;
bool saveImage(const std::string& fileName, int width, int height, uint32_t* pixels, bool flipVertically) override;
bool saveScreenshot(const std::string& fileName, int width, int height) override;
int checkLicense() override;
void createUserInput() override;
std::vector<std::string> getUserInput() override;
Expand Down Expand Up @@ -63,6 +64,7 @@ class AppPlatform_android : public AppPlatform
void setExternalStoragePath(const std::string& path);

private:
bool _saveImage(const std::string& saveName, int width, int height, uint32_t* pixels, bool flipVertically);
void changeKeyboardVisibility(bool bShown);

int m_ScreenWidth;
Expand Down
77 changes: 39 additions & 38 deletions platforms/sdl/desktop/AppPlatform_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ AppPlatform_sdl::AppPlatform_sdl(std::string storageDir, SDL_Window *window)
}

// Take Screenshot
static int save_png(const char *filename, unsigned char *pixels, int line_size, int width, int height)
static int save_png(const char *filename, unsigned char *pixels, int line_size, int width, int height, bool flip_vert)
{
// Setup
stbi_flip_vertically_on_write(true);
stbi_flip_vertically_on_write(flip_vert);

// Write Image
return stbi_write_png(filename, width, height, 4, pixels, line_size);
Expand Down Expand Up @@ -50,32 +50,45 @@ void AppPlatform_sdl::ensureDirectoryExists(const char* path)
}
}

void AppPlatform_sdl::saveScreenshot(const std::string& filename, int glWidth, int glHeight)
bool AppPlatform_sdl::_saveImage(const std::string& fileName, int width, int height, int lineSize, uint32_t* pixels, bool flipVertically)
{
// Get Directory
std::string screenshots = _storageDir + "/screenshots";

// Get Timestamp
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char time[256];
strftime(time, sizeof (time), "%Y-%m-%d_%H.%M.%S", timeinfo);

// Ensure Screenshots Folder Exists
ensureDirectoryExists(screenshots.c_str());

// Prevent Overwriting Screenshots
int num = 1;
const std::string path = screenshots + "/";
std::string file = path + time + ".png";
while (XPL_ACCESS(file.c_str(), F_OK) != -1)
std::string path, pad;
while (true)
{
path = screenshots + "/" + pad + fileName;
if (XPL_ACCESS(path.c_str(), F_OK) == -1)
break;

// could access, add a dash to avoid overwriting. HACK
pad += '-';
}

// Save Image
if (!save_png(path.c_str(), (unsigned char*) pixels, lineSize, width, height, flipVertically))
{
file = path + SSTR(time << "-" << num << ".png");
num++;
LOG_E("Couldn't save screenshot: %s", path.c_str());
return false;
}

LOG_I("Saved to %s", path.c_str());
return true;
}

bool AppPlatform_sdl::saveImage(const std::string& filename, int glWidth, int glHeight, uint32_t* pixels, bool flipVertically)
{
return _saveImage(filename, glWidth, glHeight, glWidth * sizeof (uint32_t), pixels, flipVertically);
}

bool AppPlatform_sdl::saveScreenshot(const std::string& filename, int glWidth, int glHeight)
{
// Get Image Size
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
Expand All @@ -101,31 +114,18 @@ void AppPlatform_sdl::saveScreenshot(const std::string& filename, int glWidth, i

// Read Pixels
bool fail = false;
unsigned char *pixels = new unsigned char[size];
unsigned char *pixels = new (std::nothrow) unsigned char[size];
if (!pixels)
{
fail = true;
}
else
{
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
}
return false;

glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

// Save Image
if (fail || !save_png(file.c_str(), pixels, line_size, width, height))
{
LOG_E("Screenshot Failed: %s", file.c_str());
}
else
{
LOG_I("Screenshot Saved: %s", file.c_str());
}
// Call saveImage To Save The Image
bool res = _saveImage(filename, glWidth, glHeight, line_size, (uint32_t*) pixels, true);

// Free
if (!pixels)
{
delete[] pixels;
}
delete[] pixels;

return res;
}

Texture AppPlatform_sdl::loadTexture(const std::string& path, bool bIsRequired)
Expand Down Expand Up @@ -173,6 +173,7 @@ Texture AppPlatform_sdl::loadTexture(const std::string& path, bool bIsRequired)
// Return
return out;
}

bool AppPlatform_sdl::doesTextureExist(const std::string& path)
{
// Get Full Path
Expand Down
6 changes: 5 additions & 1 deletion platforms/sdl/desktop/AppPlatform_sdl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class AppPlatform_sdl : public AppPlatform_sdl_base
public:
AppPlatform_sdl(std::string storageDir, SDL_Window *window);

void saveScreenshot(const std::string& fileName, int width, int height) override;
bool saveScreenshot(const std::string& fileName, int width, int height) override;
bool saveImage(const std::string& fileName, int width, int height, uint32_t* pixels, bool flipVertically) override;
Texture loadTexture(const std::string& path, bool b = false) override;
bool doesTextureExist(const std::string& path) override;

Expand All @@ -22,4 +23,7 @@ class AppPlatform_sdl : public AppPlatform_sdl_base

protected:
void ensureDirectoryExists(const char* path) override;

private:
bool _saveImage(const std::string& fileName, int width, int height, int lineSize, uint32_t* pixels, bool flipVertically);
};

0 comments on commit b2b7e63

Please sign in to comment.