Files
mcpe/platforms/sdl/AppPlatform_sdlbase.cpp
Brent f12a3c1c61 Visual Studio Project Overhaul + Cleanup (#80)
* 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>
2023-10-22 10:08:59 +03:00

218 lines
4.2 KiB
C++

#include "AppPlatform_sdlbase.hpp"
#include <sstream>
#include <fstream>
#include <sys/stat.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#else
#include "thirdparty/GL/GL.hpp"
#endif
#include "common/Utils.hpp"
#include "platforms/openal/SoundSystemAL.hpp"
void AppPlatform_sdlbase::_init(std::string storageDir, SDL_Window *window)
{
_storageDir = storageDir;
_window = window;
_iconTexture = nullptr;
_icon = nullptr;
m_bShiftPressed[0] = false;
m_bShiftPressed[1] = false;
ensureDirectoryExists(_storageDir.c_str());
m_pLogger = new Logger;
m_pSoundSystem = nullptr;
}
void AppPlatform_sdlbase::initSoundSystem()
{
if (!m_pSoundSystem)
{
m_pSoundSystem = new SoundSystemAL();
LOG_I("Initializing OpenAL SoundSystem...");
}
else
{
LOG_E("Trying to initialize SoundSystem more than once!");
}
}
void AppPlatform_sdlbase::setIcon(const Texture& icon)
{
if (!icon.m_pixels)
return;
SAFE_DELETE(_iconTexture);
if (_icon) SDL_FreeSurface(_icon);
_iconTexture = new Texture(icon);
_icon = getSurfaceForTexture(_iconTexture);
if (_icon)
SDL_SetWindowIcon(_window, _icon);
}
AppPlatform_sdlbase::~AppPlatform_sdlbase()
{
if (_icon) SDL_FreeSurface(_icon);
SAFE_DELETE(_iconTexture);
SAFE_DELETE(m_pSoundSystem);
// DELETE THIS LAST
SAFE_DELETE(m_pLogger);
}
SDL_Surface* AppPlatform_sdlbase::getSurfaceForTexture(const Texture* const texture)
{
if (!texture) return nullptr;
void * const pixels = texture->m_pixels;
const int width = texture->m_width;
const int height = texture->m_height;
const int depth = 32; // Color depth (32-bit by default)
SDL_Surface* surface = SDL_CreateRGBSurfaceFrom(
pixels, width, height, depth,
width * 4, // Pitch
0x000000FF, 0x0000FF00, 0x00FF0000,
0xFF000000
);
if (!surface)
LOG_E("Failed loading SDL_Surface from Texture: %s", SDL_GetError());
return surface;
}
int AppPlatform_sdlbase::checkLicense()
{
// we own the game!!
return 1;
}
const char* const AppPlatform_sdlbase::getWindowTitle() const
{
return SDL_GetWindowTitle(_window);
}
int AppPlatform_sdlbase::getScreenWidth() const
{
int width;
SDL_GL_GetDrawableSize(_window, &width, nullptr);
return width;
}
int AppPlatform_sdlbase::getScreenHeight() const
{
int height;
SDL_GL_GetDrawableSize(_window, nullptr, &height);
return height;
}
void AppPlatform_sdlbase::setMouseGrabbed(bool b)
{
SDL_SetWindowGrab(_window, b ? SDL_TRUE : SDL_FALSE);
SDL_SetRelativeMouseMode(b ? SDL_TRUE : SDL_FALSE);
}
void AppPlatform_sdlbase::setMouseDiff(int x, int y)
{
xrel = x;
yrel = y;
}
void AppPlatform_sdlbase::getMouseDiff(int& x, int& y)
{
x = xrel;
y = yrel;
}
void AppPlatform_sdlbase::clearDiff()
{
xrel = 0;
yrel = 0;
}
bool AppPlatform_sdlbase::shiftPressed()
{
return m_bShiftPressed[0] || m_bShiftPressed[1];
}
void AppPlatform_sdlbase::setShiftPressed(bool b, bool isLeft)
{
m_bShiftPressed[isLeft ? 0 : 1] = b;
}
int AppPlatform_sdlbase::getUserInputStatus()
{
return -1;
}
Mouse::ButtonType AppPlatform_sdlbase::GetMouseButtonType(SDL_Event event)
{
switch (event.button.button)
{
case SDL_BUTTON_LEFT:
return Mouse::LEFT;
case SDL_BUTTON_RIGHT:
return Mouse::RIGHT;
case SDL_BUTTON_MIDDLE:
return Mouse::MIDDLE;
default:
return Mouse::NONE;
}
}
Mouse::ButtonState AppPlatform_sdlbase::GetMouseButtonState(SDL_Event event)
{
Mouse::ButtonState result;
switch (event.type)
{
case SDL_MOUSEBUTTONDOWN:
result = Mouse::DOWN;
break;
case SDL_MOUSEBUTTONUP:
result = Mouse::UP;
break;
case SDL_MOUSEWHEEL:
{
short wheelDelta = event.wheel.y;
if (wheelDelta > 0)
{
// "A positive value indicates that the wheel was rotated forward, away from the user."
result = Mouse::UP;
}
else
{
// "A negative value indicates that the wheel was rotated backward, toward the user."
result = Mouse::DOWN;
}
break;
}
default:
result = Mouse::UP;
break;
}
return result;
}
Keyboard::KeyState AppPlatform_sdlbase::GetKeyState(SDL_Event event)
{
switch (event.key.state)
{
case SDL_RELEASED:
return Keyboard::UP;
case SDL_PRESSED:
default:
return Keyboard::DOWN;
}
}