Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isometric screenshot feature. #122

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};
23 changes: 16 additions & 7 deletions platforms/windows/AppPlatform_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ void AppPlatform_win32::buyGame()
MessageBoxA(GetHWND(), "Buying the game!", getWindowTitle(), MB_OK | MB_ICONINFORMATION);
}

void AppPlatform_win32::saveScreenshot(const std::string& fileName, int width, int height)
bool AppPlatform_win32::saveImage(const std::string& fileName, int width, int height, uint32_t* pixels, bool flipVertically)
{
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);
// Flip vertically because OpenGL dumps framebuffer data upside down.
stbi_flip_vertically_on_write(flipVertically);

// Verify if the folder exists for saving screenshots and
// create it if it doesn't
Expand Down Expand Up @@ -108,9 +105,21 @@ void AppPlatform_win32::saveScreenshot(const std::string& fileName, int width, i
char fullpath[MAX_PATH];
sprintf(fullpath, "%s\\%s", str, fileName.c_str());

stbi_write_png(fullpath, width, height, 4, pixels, width * 4);
int result_wr = stbi_write_png(fullpath, width, height, 4, pixels, width * 4);
return result_wr != 0;
}

bool AppPlatform_win32::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);

bool res = saveImage(fileName, width, height, pixels, true);

delete[] pixels;

return res;
}

void AppPlatform_win32::createUserInput()
Expand Down
3 changes: 2 additions & 1 deletion platforms/windows/AppPlatform_win32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class AppPlatform_win32 : public AppPlatform
void initSoundSystem() override;

void buyGame() override;
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);
int checkLicense() override;
void createUserInput() override;
std::vector<std::string> getUserInput() override { return m_UserInput; }
Expand Down
7 changes: 6 additions & 1 deletion source/client/app/AppPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,14 @@ bool AppPlatform::hasBuyButtonWhenInvalidLicense()

// void AppPlatform::loadTexture(const std::string&, bool);

void AppPlatform::saveScreenshot(const std::string&, int, int)
bool AppPlatform::saveScreenshot(const std::string&, int, int)
{
return false;
}

bool AppPlatform::saveImage(const std::string& fileName, int width, int height, uint32_t* pixels, bool flipVertically)
{
return false;
}

void AppPlatform::showDialog(eDialogType type)
Expand Down
6 changes: 5 additions & 1 deletion source/client/app/AppPlatform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AppPlatform
virtual std::vector<std::string> getUserInput();
virtual int getUserInputStatus();
virtual bool hasBuyButtonWhenInvalidLicense();
virtual void saveScreenshot(const std::string&, int, int);
virtual bool saveScreenshot(const std::string&, int, int);
virtual void showDialog(eDialogType);
virtual void uploadPlatformDependentData(int, void*);
virtual Texture loadTexture(const std::string&, bool bIsRequired);
Expand Down Expand Up @@ -80,8 +80,12 @@ class AppPlatform
virtual bool hasFileSystemAccess();
// Also add this to allow dynamic patching.
virtual std::string getPatchData();

virtual void initSoundSystem();
virtual SoundSystem* const getSoundSystem() const;

// Also add this to allow isometric screenshots.
virtual bool saveImage(const std::string& fileName, int width, int height, uint32_t* pixels, bool flipVertically);
#endif

public:
Expand Down
11 changes: 8 additions & 3 deletions source/client/app/Minecraft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,14 @@ void Minecraft::tickInput()
else if (getOptions()->isKey(KM_TOGGLEAO, keyCode))
{
// Toggle ambient occlusion.
getOptions()->m_bAmbientOcclusion = !getOptions()->m_bAmbientOcclusion;
Minecraft::useAmbientOcclusion = getOptions()->m_bAmbientOcclusion;
m_pLevelRenderer->allChanged();
// getOptions()->m_bAmbientOcclusion = !getOptions()->m_bAmbientOcclusion;
// Minecraft::useAmbientOcclusion = getOptions()->m_bAmbientOcclusion;
// m_pLevelRenderer->allChanged();

if (m_pGameRenderer->m_bIsometric)
m_pGameRenderer->cancelIsometricRender();
else
m_pGameRenderer->startIsometricRender();
}
#endif
}
Expand Down
Loading
Loading