mirror of
https://github.com/celisej567/mcpe.git
synced 2026-09-11 20:11:18 +03:00
* Visual Studio Project Overhaul + Cleanup * SDL2 project for Windows * Re-added game client icon to SDL2 code * Renamed "AppPlatform_windows" to "AppPlatform_win32" (this is the name of the Windows API and is not representative of the architecture type) * Renamed "LoggerWindows" to "LoggerWin32" * Renamed "SoundSystemWindows to "SoundSystemDS" (DirectSound). This may be used for the 360, so it wouldn't really be Windows-specific then. * Moved "ClientSideNetworkHandler" from "network" to "client/network". We don't need it being compiled for the server if the client's the only thing that needs it. * I wonder if this still works on macOS... * Bugfixes & Fixed for macOS * Options::savePropertiesToFile Logging Bugfix * Silence Winsock Deprecation Warnings in RakNet * VS Project Improvements - Replaced 50 billion relative paths with $(MC_ROOT) - Added $(RAKNET_PATH) variable to override RakNet location - Re-added gitignore for .vcxproj.user files - Added debugging config to Directory.Builds.props - Slimmed down project configurations for SDL2 * VS Project Config Bugfixes - Fixed RakNet header path for additional includes * RakNet Target for XCode * XCode Project Config Fixes * Packet logging * Network VS Project Filter Fix * Fix RakNet Packet ID Length We previously didn't have consistency between old and new C++ regarding PacketType enum length. Now we do. This is required or else it completely breaks networking between the versions. * Additional RakNet Error Handling * Disable packet logging * * Fix CMakeLists.txt This reflects the relocation of ClientSideNetworkHandler.cpp. * * Also add renderer/GL/GL.cpp to the CMakeLists.txt * * Replace libpng with stb_image * * Fix buggy water behavior. * * Put the CMakeLists of the SDL project in debug mode * Visual Studio 2010 Support * * Change the SdlIoCallbacks from an array to a single member. This fixes compilation of the sdl2 target on VS. * * Fix missing _error label. * Revert "* Fix missing _error label." This reverts commit 99a057fc84049a16c864bd840fb439a008af5c74. * Revert "* Replace libpng with stb_image" * info_updateGame Tiles --------- Co-authored-by: Brent Da Mage <BrentDaMage@users.noreply.github.com> Co-authored-by: iProgramInCpp <iprogramincpp@gmail.com>
302 lines
6.1 KiB
C++
302 lines
6.1 KiB
C++
#include "AppPlatform_sdl.hpp"
|
|
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <sys/stat.h>
|
|
#include <cerrno>
|
|
|
|
#include "thirdparty/LibPNG/png.h"
|
|
|
|
#include "thirdparty/GL/GL.hpp"
|
|
|
|
#include "common/Utils.hpp"
|
|
|
|
AppPlatform_sdl::AppPlatform_sdl(std::string storageDir, SDL_Window *window)
|
|
: AppPlatform_sdlbase(storageDir, window)
|
|
{
|
|
setIcon(loadTexture("icon.png"));
|
|
}
|
|
|
|
// Take Screenshot
|
|
static int save_png(const char *filename, unsigned char *pixels, int line_size, int width, int height)
|
|
{
|
|
// Return value
|
|
int ret = 0;
|
|
|
|
// Variables
|
|
png_structp png = NULL;
|
|
png_infop info = NULL;
|
|
FILE *file = NULL;
|
|
png_colorp palette = NULL;
|
|
png_bytep *rows = new png_bytep[height];
|
|
for (int i = 0; i < height; ++i)
|
|
{
|
|
rows[height - i - 1] = (png_bytep)(&pixels[i * line_size]);
|
|
}
|
|
|
|
// Init
|
|
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
if (!png)
|
|
{
|
|
ret = 1;
|
|
goto ret;
|
|
}
|
|
info = png_create_info_struct(png);
|
|
if (!info)
|
|
{
|
|
ret = 1;
|
|
goto ret;
|
|
}
|
|
|
|
// Open File
|
|
file = fopen(filename, "wb");
|
|
if (!file)
|
|
{
|
|
ret = 1;
|
|
goto ret;
|
|
}
|
|
|
|
// Prepare To Write
|
|
png_init_io(png, file);
|
|
png_set_IHDR(png, info, width, height, 8 /* Depth */, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
palette = (png_colorp) png_malloc(png, PNG_MAX_PALETTE_LENGTH * sizeof(png_color));
|
|
if (!palette)
|
|
{
|
|
ret = 1;
|
|
goto ret;
|
|
}
|
|
png_set_PLTE(png, info, palette, PNG_MAX_PALETTE_LENGTH);
|
|
png_write_info(png, info);
|
|
png_set_packing(png);
|
|
|
|
// Write
|
|
png_write_image(png, rows);
|
|
png_write_end(png, info);
|
|
|
|
ret:
|
|
// Free
|
|
if (palette != NULL)
|
|
{
|
|
png_free(png, palette);
|
|
}
|
|
if (rows != NULL)
|
|
{
|
|
delete rows[height];
|
|
}
|
|
if (file != NULL)
|
|
{
|
|
fclose(file);
|
|
}
|
|
if (png != NULL)
|
|
{
|
|
png_destroy_write_struct(&png, &info);
|
|
}
|
|
|
|
// Return
|
|
return ret;
|
|
}
|
|
|
|
// Ensure Screenshots Folder Exists
|
|
void AppPlatform_sdl::ensureDirectoryExists(const char* path)
|
|
{
|
|
// Check Screenshots Folder
|
|
struct stat obj;
|
|
if (stat(path, &obj) != 0 || !S_ISDIR(obj.st_mode))
|
|
{
|
|
// Create Screenshots Folder
|
|
#ifdef _WIN32
|
|
int ret = XPL_MKDIR(path);
|
|
#else
|
|
int ret = XPL_MKDIR(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
|
#endif
|
|
if (ret != 0)
|
|
{
|
|
// Unable To Create Folder
|
|
LOG_E("Error Creating Directory: %s: %s", path, strerror(errno));
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
}
|
|
|
|
void AppPlatform_sdl::saveScreenshot(const std::string& filename, int glWidth, int glHeight)
|
|
{
|
|
// 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)
|
|
{
|
|
file = path + SSTR(time << "-" << num << ".png");
|
|
num++;
|
|
}
|
|
|
|
// Get Image Size
|
|
GLint viewport[4];
|
|
glGetIntegerv(GL_VIEWPORT, viewport);
|
|
int x = viewport[0];
|
|
int y = viewport[1];
|
|
int width = viewport[2];
|
|
int height = viewport[3];
|
|
|
|
// Get Line Size
|
|
int line_size = width * 4;
|
|
{
|
|
// Handle Alignment
|
|
int alignment;
|
|
glGetIntegerv(GL_PACK_ALIGNMENT, &alignment);
|
|
// Round
|
|
int diff = line_size % alignment;
|
|
if (diff > 0)
|
|
{
|
|
line_size = line_size + (alignment - diff);
|
|
}
|
|
}
|
|
int size = height * line_size;
|
|
|
|
// Read Pixels
|
|
bool fail = false;
|
|
unsigned char *pixels = (unsigned char *) malloc(size);
|
|
if (pixels == NULL)
|
|
{
|
|
fail = true;
|
|
}
|
|
else
|
|
{
|
|
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());
|
|
}
|
|
|
|
// Free
|
|
if (pixels != NULL)
|
|
{
|
|
free(pixels);
|
|
}
|
|
}
|
|
|
|
static void png_read_sdl(png_structp png_ptr, png_bytep data, png_size_t length)
|
|
{
|
|
SDL_RWread((SDL_RWops *) png_get_io_ptr(png_ptr), (char *) data, length, 1);
|
|
}
|
|
|
|
static void nop_png_warning(png_structp png_ptr, png_const_charp warning_message)
|
|
{
|
|
// Do Nothing
|
|
}
|
|
|
|
Texture AppPlatform_sdl::loadTexture(const std::string& path, bool b)
|
|
{
|
|
Texture out;
|
|
out.field_C = 1;
|
|
out.field_D = 0;
|
|
|
|
std::string realPath = getAssetPath(path);
|
|
|
|
SDL_RWops *io = SDL_RWFromFile(realPath.c_str(), "rb");
|
|
|
|
if (!io)
|
|
{
|
|
LOG_E("Couldn't find file: %s", path.c_str());
|
|
return out;
|
|
}
|
|
|
|
png_structp pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, nop_png_warning);
|
|
if (!pngPtr)
|
|
{
|
|
return out;
|
|
}
|
|
|
|
png_infop infoPtr = png_create_info_struct(pngPtr);
|
|
if (!infoPtr)
|
|
{
|
|
png_destroy_read_struct(&pngPtr, NULL, NULL);
|
|
return out;
|
|
}
|
|
|
|
png_set_read_fn(pngPtr, (png_voidp) io, png_read_sdl);
|
|
|
|
png_read_info(pngPtr, infoPtr);
|
|
|
|
png_set_expand(pngPtr);
|
|
png_set_strip_16(pngPtr);
|
|
png_set_gray_to_rgb(pngPtr);
|
|
png_read_update_info(pngPtr, infoPtr);
|
|
|
|
out.m_width = png_get_image_width(pngPtr, infoPtr);
|
|
out.m_height = png_get_image_height(pngPtr, infoPtr);
|
|
|
|
int pixelSize = 4;
|
|
|
|
png_bytep *rowPtrs = new png_bytep[out.m_height];
|
|
unsigned char *pixels = new unsigned char[pixelSize * out.m_width * out.m_height];
|
|
|
|
int rowStrideBytes = pixelSize * out.m_width;
|
|
for (int i = 0; i < out.m_height; i++)
|
|
{
|
|
rowPtrs[i] = (png_bytep) &pixels[i * rowStrideBytes];
|
|
}
|
|
png_read_image(pngPtr, rowPtrs);
|
|
|
|
// Convert RGB Images To RGBA
|
|
bool opaque = png_get_color_type(pngPtr, infoPtr) != PNG_COLOR_TYPE_RGBA;
|
|
if (opaque)
|
|
{
|
|
for (int y = 0; y < out.m_height; y++)
|
|
{
|
|
unsigned char *row = &pixels[y * rowStrideBytes];
|
|
for (int x = out.m_width - 1; x >= 0; x--)
|
|
{
|
|
// Find Indexes
|
|
int rgb = x * 3;
|
|
int rgba = x * 4;
|
|
|
|
// Read RGB Pixel
|
|
unsigned char a = row[rgb];
|
|
unsigned char b = row[rgb + 1];
|
|
unsigned char c = row[rgb + 2];
|
|
|
|
// Store RGBA Pixel
|
|
row[rgba] = a;
|
|
row[rgba + 1] = b;
|
|
row[rgba + 2] = c;
|
|
row[rgba + 3] = 255;
|
|
}
|
|
}
|
|
}
|
|
|
|
out.m_pixels = (uint32_t *) pixels;
|
|
|
|
png_destroy_read_struct(&pngPtr, &infoPtr, (png_infopp) 0);
|
|
delete[](png_bytep) rowPtrs;
|
|
SDL_RWclose(io);
|
|
|
|
return out;
|
|
}
|
|
|
|
bool AppPlatform_sdl::hasFileSystemAccess()
|
|
{
|
|
return true;
|
|
}
|