Address Review Comments

This commit is contained in:
TheBrokenRail
2023-08-05 15:33:45 -04:00
committed by iProgramInCpp
parent af74761c97
commit 4b8791100c
7 changed files with 435 additions and 278 deletions

View File

@@ -14,28 +14,33 @@
#include "client/common/Utils.hpp"
AppPlatform_sdl::AppPlatform_sdl(std::string storageDir, SDL_Window *window) {
AppPlatform_sdl::AppPlatform_sdl(std::string storageDir, SDL_Window *window)
{
_storageDir = storageDir;
_window = window;
}
int AppPlatform_sdl::checkLicense() {
int AppPlatform_sdl::checkLicense()
{
// we own the game!!
return 1;
}
// Ensure Screenshots Folder Exists
void ensure_screenshots_folder(const char *screenshots) {
void ensure_screenshots_folder(const char *screenshots)
{
// Check Screenshots Folder
struct stat obj;
if (stat(screenshots, &obj) != 0 || !S_ISDIR(obj.st_mode)) {
if (stat(screenshots, &obj) != 0 || !S_ISDIR(obj.st_mode))
{
// Create Screenshots Folder
#ifdef _WIN32
int ret = mkdir(screenshots);
#else
int ret = mkdir(screenshots, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
#endif
if (ret != 0) {
if (ret != 0)
{
// Unable To Create Folder
LogMsg("Error Creating Directory: %s: %s", screenshots, strerror(errno));
exit(EXIT_FAILURE);
@@ -44,10 +49,9 @@ void ensure_screenshots_folder(const char *screenshots) {
}
#ifndef __EMSCRIPTEN__
// 4 (Year) + 1 (Hyphen) + 2 (Month) + 1 (Hyphen) + 2 (Day) + 1 (Underscore) + 2 (Hour) + 1 (Period) + 2 (Minute) + 1 (Period) + 2 (Second) + 1 (Null Terminator)
#define TIME_SIZE 20
// 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)
{
// Return value
int ret = 0;
@@ -57,25 +61,29 @@ static int save_png(const char *filename, unsigned char *pixels, int line_size,
FILE *file = NULL;
png_colorp palette = NULL;
png_bytep rows[height];
for (int i = 0; i < height; ++i) {
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) {
if (!png)
{
ret = 1;
goto ret;
}
info = png_create_info_struct(png);
if (!info) {
if (!info)
{
ret = 1;
goto ret;
}
// Open File
file = fopen(filename, "wb");
if (!file) {
if (!file)
{
ret = 1;
goto ret;
}
@@ -84,7 +92,8 @@ static int save_png(const char *filename, unsigned char *pixels, int line_size,
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) {
if (!palette)
{
ret = 1;
goto ret;
}
@@ -98,20 +107,24 @@ static int save_png(const char *filename, unsigned char *pixels, int line_size,
ret:
// Free
if (palette != NULL) {
if (palette != NULL)
{
png_free(png, palette);
}
if (file != NULL) {
if (file != NULL)
{
fclose(file);
}
if (png != NULL) {
if (png != NULL)
{
png_destroy_write_struct(&png, &info);
}
// Return
return ret;
}
void AppPlatform_sdl::saveScreenshot(const std::string &filename, int glWidth, int glHeight) {
void AppPlatform_sdl::saveScreenshot(const std::string& filename, int glWidth, int glHeight)
{
// Get Directory
std::string screenshots = _storageDir + "/screenshots";
@@ -120,8 +133,8 @@ void AppPlatform_sdl::saveScreenshot(const std::string &filename, int glWidth, i
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char time[TIME_SIZE];
strftime(time, TIME_SIZE, "%Y-%m-%d_%H.%M.%S", timeinfo);
char time[256];
strftime(time, sizeof (time), "%Y-%m-%d_%H.%M.%S", timeinfo);
// Ensure Screenshots Folder Exists
ensure_screenshots_folder(screenshots.c_str());
@@ -129,7 +142,8 @@ void AppPlatform_sdl::saveScreenshot(const std::string &filename, int glWidth, i
// Prevent Overwriting Screenshots
int num = 1;
std::string file = screenshots + '/' + time + ".png";
while (access(file.c_str(), F_OK) != -1) {
while (access(file.c_str(), F_OK) != -1)
{
file = screenshots + '/' + time + '-' + std::to_string(num) + ".png";
num++;
}
@@ -150,7 +164,8 @@ void AppPlatform_sdl::saveScreenshot(const std::string &filename, int glWidth, i
glGetIntegerv(GL_PACK_ALIGNMENT, &alignment);
// Round
int diff = line_size % alignment;
if (diff > 0) {
if (diff > 0)
{
line_size = line_size + (alignment - diff);
}
}
@@ -159,53 +174,66 @@ void AppPlatform_sdl::saveScreenshot(const std::string &filename, int glWidth, i
// Read Pixels
bool fail = false;
unsigned char *pixels = (unsigned char *) malloc(size);
if (pixels == NULL) {
if (pixels == NULL)
{
fail = true;
} else {
}
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)) {
if (fail || save_png(file.c_str(), pixels, line_size, width, height))
{
LogMsg("Screenshot Failed: %s", file.c_str());
} else {
}
else
{
LogMsg("Screenshot Saved: %s", file.c_str());
}
// Free
if (pixels != NULL) {
if (pixels != NULL)
{
free(pixels);
}
}
#endif
int AppPlatform_sdl::getScreenWidth() const {
int AppPlatform_sdl::getScreenWidth() const
{
int width;
SDL_GL_GetDrawableSize(_window, &width, nullptr);
return width;
}
int AppPlatform_sdl::getScreenHeight() const {
int AppPlatform_sdl::getScreenHeight() const
{
int height;
SDL_GL_GetDrawableSize(_window, nullptr, &height);
return height;
}
#ifndef __EMSCRIPTEN__
static void png_read_sdl(png_structp png_ptr, png_bytep data, png_size_t length) {
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) {
static void nop_png_warning(png_structp png_ptr, png_const_charp warning_message)
{
// Do Nothing
}
#endif
Texture AppPlatform_sdl::loadTexture(const std::string& str, bool b) {
Texture AppPlatform_sdl::loadTexture(const std::string& str, bool b)
{
Texture out;
out.field_C = 0;
out.field_C = 1;
out.field_D = 0;
std::string realPath = str;
if (realPath.size() && realPath[0] == '/') {
if (realPath.size() && realPath[0] == '/')
{
// trim it off
realPath = realPath.substr(1);
}
@@ -214,14 +242,17 @@ Texture AppPlatform_sdl::loadTexture(const std::string& str, bool b) {
#ifndef __EMSCRIPTEN__
SDL_RWops *io = SDL_RWFromFile(realPath.c_str(), "rb");
if (io != NULL) {
if (io != NULL)
{
png_structp pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, nop_png_warning);
if (!pngPtr) {
if (!pngPtr)
{
return out;
}
png_infop infoPtr = png_create_info_struct(pngPtr);
if (!infoPtr) {
if (!infoPtr)
{
png_destroy_read_struct(&pngPtr, NULL, NULL);
return out;
}
@@ -238,21 +269,45 @@ Texture AppPlatform_sdl::loadTexture(const std::string& str, bool b) {
out.m_width = png_get_image_width(pngPtr, infoPtr);
out.m_height = png_get_image_height(pngPtr, infoPtr);
bool opaque = png_get_color_type(pngPtr, infoPtr) != PNG_COLOR_TYPE_RGBA;
if (!opaque) {
out.field_C = 1;
}
int pixelSize = opaque ? 3 : 4;
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++) {
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);
@@ -261,10 +316,10 @@ Texture AppPlatform_sdl::loadTexture(const std::string& str, bool b) {
}
#else
char *data = emscripten_get_preloaded_image_data(("/" + realPath).c_str(), &out.m_width, &out.m_height);
if (data != NULL) {
if (data != NULL)
{
size_t data_size = out.m_width * out.m_height * 4;
out.m_pixels = (uint32_t *) new unsigned char[data_size];
out.field_C = 1;
memcpy(out.m_pixels, data, data_size);
free(data);
return out;
@@ -275,34 +330,41 @@ Texture AppPlatform_sdl::loadTexture(const std::string& str, bool b) {
return out;
}
void AppPlatform_sdl::setMouseGrabbed(bool b) {
void AppPlatform_sdl::setMouseGrabbed(bool b)
{
SDL_SetWindowGrab(_window, b ? SDL_TRUE : SDL_FALSE);
SDL_SetRelativeMouseMode(b ? SDL_TRUE : SDL_FALSE);
}
void AppPlatform_sdl::setMouseDiff(int x, int y) {
void AppPlatform_sdl::setMouseDiff(int x, int y)
{
xrel = x;
yrel = y;
}
void AppPlatform_sdl::getMouseDiff(int& x, int& y) {
void AppPlatform_sdl::getMouseDiff(int& x, int& y)
{
x = xrel;
y = yrel;
}
void AppPlatform_sdl::clearDiff() {
void AppPlatform_sdl::clearDiff()
{
xrel = 0;
yrel = 0;
}
bool AppPlatform_sdl::shiftPressed() {
bool AppPlatform_sdl::shiftPressed()
{
return m_bShiftPressed[0] || m_bShiftPressed[1];
}
void AppPlatform_sdl::setShiftPressed(bool b, bool isLeft) {
void AppPlatform_sdl::setShiftPressed(bool b, bool isLeft)
{
m_bShiftPressed[isLeft ? 0 : 1] = b;
}
int AppPlatform_sdl::getUserInputStatus() {
int AppPlatform_sdl::getUserInputStatus()
{
return -1;
}

View File

@@ -12,34 +12,35 @@
void ensure_screenshots_folder(const char *screenshots);
class AppPlatform_sdl : public AppPlatform {
public:
AppPlatform_sdl(std::string storageDir, SDL_Window *window);
class AppPlatform_sdl : public AppPlatform
{
public:
AppPlatform_sdl(std::string storageDir, SDL_Window *window);
#ifndef __EMSCRIPTEN__
void saveScreenshot(const std::string& fileName, int width, int height) override;
void saveScreenshot(const std::string& fileName, int width, int height) override;
#endif
int checkLicense() override;
int getScreenWidth() const override;
int getScreenHeight() const override;
Texture loadTexture(const std::string& str, bool b) override;
int getUserInputStatus() override;
int checkLicense() override;
int getScreenWidth() const override;
int getScreenHeight() const override;
Texture loadTexture(const std::string& str, bool b) override;
int getUserInputStatus() override;
// Also add these to allow proper turning within the game.
void setMouseGrabbed(bool b) override;
void setMouseDiff(int x, int y);
void getMouseDiff(int& x, int& y) override;
void clearDiff() override;
// Also add these to allow proper turning within the game.
void setMouseGrabbed(bool b) override;
void setMouseDiff(int x, int y);
void getMouseDiff(int& x, int& y) override;
void clearDiff() override;
// Also add these to allow proper text input within the game.
bool shiftPressed() override;
void setShiftPressed(bool b, bool isLeft);
private:
std::string _storageDir;
SDL_Window *_window;
// Also add these to allow proper text input within the game.
bool shiftPressed() override;
void setShiftPressed(bool b, bool isLeft);
private:
std::string _storageDir;
SDL_Window *_window;
bool m_bShiftPressed[2] = {false, false};
bool m_bShiftPressed[2] = {false, false};
int xrel;
int yrel;
int xrel;
int yrel;
};

View File

@@ -2,9 +2,11 @@
#include "client/common/Utils.hpp"
SoundSystemAL::SoundSystemAL() {
SoundSystemAL::SoundSystemAL()
{
device = alcOpenDevice(NULL);
if (!device) {
if (!device)
{
LogMsg("Unable To Load Audio Engine");
return;
}
@@ -12,7 +14,8 @@ SoundSystemAL::SoundSystemAL() {
// Create Context
context = alcCreateContext(device, NULL);
ALCenum err = alcGetError(device);
if (err != ALC_NO_ERROR) {
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Open Audio Context: %s", alcGetString(device, err));
return;
}
@@ -20,7 +23,8 @@ SoundSystemAL::SoundSystemAL() {
// Select Context
alcMakeContextCurrent(context);
err = alcGetError(device);
if (err != ALC_NO_ERROR) {
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Select Audio Context: %s", alcGetString(device, err));
return;
}
@@ -32,34 +36,41 @@ SoundSystemAL::SoundSystemAL() {
loaded = true;
}
SoundSystemAL::~SoundSystemAL() {
if (loaded) {
// Delete Audio Sources
delete_sources();
SoundSystemAL::~SoundSystemAL()
{
if (!loaded)
{
return;
}
// Delete Audio Buffers
delete_buffers();
// Delete Audio Sources
delete_sources();
// Deselect Context
alcMakeContextCurrent(NULL);
ALCenum err = alcGetError(device);
if (err != ALC_NO_ERROR) {
LogMsg("Unable To Deselect Audio Context: %s", alcGetString(device, err));
}
// Delete Audio Buffers
delete_buffers();
// Destroy Context
alcDestroyContext(context);
err = alcGetError(device);
if (err != ALC_NO_ERROR) {
LogMsg("Unable To Destroy Audio Context: %s", alcGetString(device, err));
}
// Deselect Context
alcMakeContextCurrent(NULL);
ALCenum err = alcGetError(device);
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Deselect Audio Context: %s", alcGetString(device, err));
}
// Close Device
alcCloseDevice(device);
err = alcGetError(device);
if (err != ALC_NO_ERROR) {
LogMsg("Unable To Close Audio Device: %s", alcGetString(device, err));
}
// Destroy Context
alcDestroyContext(context);
err = alcGetError(device);
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Destroy Audio Context: %s", alcGetString(device, err));
}
// Close Device
alcCloseDevice(device);
err = alcGetError(device);
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Close Audio Device: %s", alcGetString(device, err));
}
}
@@ -68,20 +79,25 @@ SoundSystemAL::~SoundSystemAL() {
#define AL_ERROR_CHECK_MANUAL(val) \
{ \
ALenum __err = val; \
if (__err != AL_NO_ERROR) { \
if (__err != AL_NO_ERROR) \
{ \
LogMsg("(%s:%i) OpenAL Error: %s", __FILE__, __LINE__, alGetString(__err)); \
exit(EXIT_FAILURE); \
} \
}
// Delete Sources
void SoundSystemAL::delete_sources() {
if (loaded) {
for (ALuint source : idle_sources) {
void SoundSystemAL::delete_sources()
{
if (loaded)
{
for (ALuint source : idle_sources)
{
alDeleteSources(1, &source);
AL_ERROR_CHECK();
}
for (ALuint source : sources) {
for (ALuint source : sources)
{
alDeleteSources(1, &source);
AL_ERROR_CHECK();
}
@@ -91,10 +107,14 @@ void SoundSystemAL::delete_sources() {
}
// Delete Buffers
void SoundSystemAL::delete_buffers() {
if (loaded) {
for (auto &it : buffers) {
if (it.second && alIsBuffer(it.second)) {
void SoundSystemAL::delete_buffers()
{
if (loaded)
{
for (auto &it : buffers)
{
if (it.second && alIsBuffer(it.second))
{
alDeleteBuffers(1, &it.second);
AL_ERROR_CHECK();
}
@@ -104,15 +124,22 @@ void SoundSystemAL::delete_buffers() {
}
// Get Buffer
ALuint SoundSystemAL::get_buffer(const SoundDesc &sound) {
if (buffers.count(sound.m_pData) > 0) {
ALuint SoundSystemAL::get_buffer(const SoundDesc& sound)
{
if (buffers.count(sound.m_pData) > 0)
{
return buffers[sound.m_pData];
} else {
}
else
{
// Sound Format
ALenum format = AL_NONE;
if (sound.m_header.m_channels == 1) {
if (sound.m_header.m_channels == 1)
{
format = sound.m_header.m_bytes_per_sample == 2 ? AL_FORMAT_MONO16 : AL_FORMAT_MONO8;
} else if (sound.m_header.m_channels == 2) {
}
else if (sound.m_header.m_channels == 2)
{
format = sound.m_header.m_bytes_per_sample == 2 ? AL_FORMAT_STEREO16 : AL_FORMAT_STEREO8;
}
@@ -132,114 +159,140 @@ ALuint SoundSystemAL::get_buffer(const SoundDesc &sound) {
}
}
void SoundSystemAL::update(float x, float y, float z, float yaw) {
void SoundSystemAL::update(float x, float y, float z, float yaw)
{
// Check
if (loaded) {
// Update Listener Volume
float volume = 1;
alListenerf(AL_GAIN, volume);
AL_ERROR_CHECK();
if (!loaded)
{
return;
}
// Update Listener Position
alListener3f(AL_POSITION, x, y, z);
AL_ERROR_CHECK();
// Update Listener Volume
float volume = 1;
alListenerf(AL_GAIN, volume);
AL_ERROR_CHECK();
// Update Listener Orientation
float radian_yaw = yaw * (M_PI / 180);
ALfloat orientation[] = {-sinf(radian_yaw), 0.0f, cosf(radian_yaw), 0.0f, 1.0f, 0.0f};
alListenerfv(AL_ORIENTATION, orientation);
AL_ERROR_CHECK();
// Update Listener Position
alListener3f(AL_POSITION, x, y, z);
AL_ERROR_CHECK();
// Clear Finished Sources
std::vector<ALuint>::iterator it = sources.begin();
while (it != sources.end()) {
ALuint source = *it;
bool remove = false;
// Check
if (source && alIsSource(source)) {
// Is Valid Source
ALint source_state;
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
AL_ERROR_CHECK();
if (source_state != AL_PLAYING) {
// Finished Playing
remove = true;
if (idle_sources.size() < MAX_IDLE_SOURCES) {
idle_sources.push_back(source);
} else {
alDeleteSources(1, &source);
AL_ERROR_CHECK();
}
}
} else {
// Not A Source
// Update Listener Orientation
float radian_yaw = yaw * (M_PI / 180);
ALfloat orientation[] = {-sinf(radian_yaw), 0.0f, cosf(radian_yaw), 0.0f, 1.0f, 0.0f};
alListenerfv(AL_ORIENTATION, orientation);
AL_ERROR_CHECK();
// Clear Finished Sources
std::vector<ALuint>::iterator it = sources.begin();
while (it != sources.end())
{
ALuint source = *it;
bool remove = false;
// Check
if (source && alIsSource(source))
{
// Is Valid Source
ALint source_state;
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
AL_ERROR_CHECK();
if (source_state != AL_PLAYING)
{
// Finished Playing
remove = true;
}
// Remove If Needed
if (remove) {
it = sources.erase(it);
} else {
++it;
}
}
}
}
void SoundSystemAL::play(const SoundDesc &sound, float x, float y, float z, float volume, float pitch, bool is_ui) {
if (loaded) {
// Load Sound
ALuint buffer = get_buffer(sound);
if (volume > 0.0f && buffer) {
// Get Source
ALuint al_source;
if (idle_sources.size() > 0) {
// Use Idle Source
al_source = idle_sources.back();
idle_sources.pop_back();
} else {
// Create Source
alGenSources(1, &al_source);
// Special Out-Of-Memory Handling
if (idle_sources.size() < MAX_IDLE_SOURCES)
{
ALenum err = alGetError();
if (err == AL_OUT_OF_MEMORY) {
return;
} else {
AL_ERROR_CHECK_MANUAL(err);
}
idle_sources.push_back(source);
}
else
{
alDeleteSources(1, &source);
AL_ERROR_CHECK();
}
}
// Set Properties
alSourcef(al_source, AL_PITCH, pitch);
AL_ERROR_CHECK();
alSourcef(al_source, AL_GAIN, volume);
AL_ERROR_CHECK();
alSource3f(al_source, AL_POSITION, x, y, z);
AL_ERROR_CHECK();
alSource3f(al_source, AL_VELOCITY, 0, 0, 0);
AL_ERROR_CHECK();
alSourcei(al_source, AL_LOOPING, AL_FALSE);
AL_ERROR_CHECK();
alSourcei(al_source, AL_SOURCE_RELATIVE, is_ui ? AL_TRUE : AL_FALSE);
AL_ERROR_CHECK();
// Set Attenuation
alSourcef(al_source, AL_MAX_DISTANCE, 16.0f);
AL_ERROR_CHECK();
alSourcef(al_source, AL_ROLLOFF_FACTOR, 6.0f);
AL_ERROR_CHECK();
alSourcef(al_source, AL_REFERENCE_DISTANCE, 5.0f);
AL_ERROR_CHECK();
// Set Buffer
alSourcei(al_source, AL_BUFFER, buffer);
AL_ERROR_CHECK();
// Play
alSourcePlay(al_source);
AL_ERROR_CHECK();
sources.push_back(al_source);
}
else
{
// Not A Source
remove = true;
}
// Remove If Needed
if (remove) {
it = sources.erase(it);
}
else
{
++it;
}
}
}
void SoundSystemAL::play(const SoundDesc& sound, float x, float y, float z, float volume, float pitch, bool is_ui)
{
// Check
if (!loaded)
{
return;
}
// Load Sound
ALuint buffer = get_buffer(sound);
if (volume > 0.0f && buffer)
{
// Get Source
ALuint al_source;
if (idle_sources.size() > 0)
{
// Use Idle Source
al_source = idle_sources.back();
idle_sources.pop_back();
}
else
{
// Create Source
alGenSources(1, &al_source);
// Special Out-Of-Memory Handling
{
ALenum err = alGetError();
if (err == AL_OUT_OF_MEMORY)
{
return;
}
else
{
AL_ERROR_CHECK_MANUAL(err);
}
}
}
// Set Properties
alSourcef(al_source, AL_PITCH, pitch);
AL_ERROR_CHECK();
alSourcef(al_source, AL_GAIN, volume);
AL_ERROR_CHECK();
alSource3f(al_source, AL_POSITION, x, y, z);
AL_ERROR_CHECK();
alSource3f(al_source, AL_VELOCITY, 0, 0, 0);
AL_ERROR_CHECK();
alSourcei(al_source, AL_LOOPING, AL_FALSE);
AL_ERROR_CHECK();
alSourcei(al_source, AL_SOURCE_RELATIVE, is_ui ? AL_TRUE : AL_FALSE);
AL_ERROR_CHECK();
// Set Attenuation
alSourcef(al_source, AL_MAX_DISTANCE, 16.0f);
AL_ERROR_CHECK();
alSourcef(al_source, AL_ROLLOFF_FACTOR, 6.0f);
AL_ERROR_CHECK();
alSourcef(al_source, AL_REFERENCE_DISTANCE, 5.0f);
AL_ERROR_CHECK();
// Set Buffer
alSourcei(al_source, AL_BUFFER, buffer);
AL_ERROR_CHECK();
// Play
alSourcePlay(al_source);
AL_ERROR_CHECK();
sources.push_back(al_source);
}
}

View File

@@ -11,21 +11,22 @@
#define MAX_IDLE_SOURCES 50
class SoundSystemAL {
public:
SoundSystemAL();
~SoundSystemAL();
void update(float x, float y, float z, float yaw);
void play(const SoundDesc &sound, float x, float y, float z, float volume, float pitch, bool is_ui);
private:
void delete_sources();
void delete_buffers();
ALuint get_buffer(const SoundDesc &sound);
class SoundSystemAL
{
public:
SoundSystemAL();
~SoundSystemAL();
void update(float x, float y, float z, float yaw);
void play(const SoundDesc& sound, float x, float y, float z, float volume, float pitch, bool is_ui);
private:
void delete_sources();
void delete_buffers();
ALuint get_buffer(const SoundDesc& sound);
ALCdevice *device = NULL;
ALCcontext *context = NULL;
bool loaded = false;
std::vector<ALuint> sources;
std::vector<ALuint> idle_sources;
std::unordered_map<void *, ALuint> buffers;
ALCdevice *device = NULL;
ALCcontext *context = NULL;
bool loaded = false;
std::vector<ALuint> sources;
std::vector<ALuint> idle_sources;
std::unordered_map<void *, ALuint> buffers;
};

View File

@@ -13,7 +13,8 @@
#include <emscripten/html5.h>
#endif
void LogMsg(const char* fmt, ...) {
void LogMsg(const char* fmt, ...)
{
va_list lst;
va_start(lst, fmt);
@@ -23,7 +24,8 @@ void LogMsg(const char* fmt, ...) {
va_end(lst);
}
// I hate duplicating code, but yeah
void LogMsgNoCR(const char* fmt, ...) {
void LogMsgNoCR(const char* fmt, ...)
{
va_list lst;
va_start(lst, fmt);
@@ -37,8 +39,10 @@ NinecraftApp *g_pApp;
SDL_Window *window = NULL;
SDL_GLContext context = NULL;
static void teardown() {
if (window != NULL) {
static void teardown()
{
if (window != NULL)
{
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
@@ -48,57 +52,80 @@ static void teardown() {
// Resize From JS
#ifdef __EMSCRIPTEN__
extern "C" void resize_from_js(int new_width, int new_height) {
extern "C" void resize_from_js(int new_width, int new_height)
{
SDL_SetWindowSize(window, new_width, new_height);
}
#endif
// Handle Events
static bool window_resized = false;
static void handle_events() {
static void handle_events()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
case SDL_KEYUP: {
case SDL_KEYUP:
{
if (event.key.keysym.sym == SDLK_F2)
{
if (event.key.state == SDL_PRESSED && g_AppPlatform != nullptr)
{
g_AppPlatform->saveScreenshot("", -1, -1);
}
break;
}
Keyboard::feed(event.key.state == SDL_PRESSED ? 1 : 0, translate_sdl_key_to_mcpe(event.key.keysym.sym));
if (event.key.keysym.sym == SDLK_LSHIFT || event.key.keysym.sym == SDLK_RSHIFT) {
if (event.key.keysym.sym == SDLK_LSHIFT || event.key.keysym.sym == SDLK_RSHIFT)
{
g_AppPlatform->setShiftPressed(event.key.state == SDL_PRESSED, event.key.keysym.sym == SDLK_LSHIFT);
}
break;
}
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: {
case SDL_MOUSEBUTTONUP:
{
Mouse::feed(event.button.button == SDL_BUTTON_LEFT ? 1 : 2, event.button.state == SDL_PRESSED ? 1 : 0, event.button.x, event.button.y);
break;
}
case SDL_MOUSEMOTION: {
case SDL_MOUSEMOTION:
{
Mouse::_x = event.motion.x;
Mouse::_y = event.motion.y;
Mouse::feed(0, 0, event.motion.x, event.motion.y);
g_AppPlatform->setMouseDiff(event.motion.xrel, event.motion.yrel);
break;
}
case SDL_MOUSEWHEEL: {
case SDL_MOUSEWHEEL:
{
Mouse::feed(3, event.wheel.y, Mouse::_x, Mouse::_y);
break;
}
case SDL_TEXTINPUT: {
if (g_pApp != nullptr) {
case SDL_TEXTINPUT:
{
if (g_pApp != nullptr)
{
char x = event.text.text[0];
if (x >= ' ' && x <= '~') {
if (x >= ' ' && x <= '~')
{
g_pApp->handleCharInput(x);
}
}
break;
}
case SDL_WINDOWEVENT: {
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
case SDL_WINDOWEVENT:
{
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
window_resized = true;
}
break;
}
case SDL_QUIT: {
case SDL_QUIT:
{
g_pApp->quit();
break;
}
@@ -107,28 +134,37 @@ static void handle_events() {
}
// GUI Scale
static void calulate_gui_scale() {
static void calulate_gui_scale()
{
int width = Minecraft::width;
// Modified Version Of https://github.com/MCPI-Revival/Ninecraft/blob/3f71638a10b581f6a50669edb24bc1ef1a92fbea/ninecraft/src/main.c#L243-L255
if (width < 1000) {
if (width < 400) {
if (width < 1000)
{
if (width < 400)
{
Gui::InvGuiScale = 1.0;
} else {
}
else
{
Gui::InvGuiScale = 0.5;
}
} else {
}
else
{
Gui::InvGuiScale = 0.25;
}
}
// Resizing
static void resize() {
static void resize()
{
SDL_GL_GetDrawableSize(window, &Minecraft::width, &Minecraft::height);
calulate_gui_scale();
if (g_pApp != nullptr && g_pApp->m_pScreen != nullptr) {
if (g_pApp != nullptr && g_pApp->m_pScreen != nullptr)
{
g_pApp->m_pScreen->setSize(Minecraft::width * Gui::InvGuiScale, Minecraft::height * Gui::InvGuiScale);
}
}
@@ -140,12 +176,14 @@ static void resize() {
#define EM_FALSE false
#endif
static bool is_first_window_resize = true;
static EM_BOOL main_loop(double time, void *user_data) {
static EM_BOOL main_loop(double time, void *user_data)
{
// Handle Events
handle_events();
// Screen Size
if (window_resized) {
if (window_resized)
{
window_resized = false;
resize();
}
@@ -156,21 +194,26 @@ static EM_BOOL main_loop(double time, void *user_data) {
// Swap Buffers
SDL_GL_SwapWindow(window);
if (g_pApp->wantToQuit()) {
if (g_pApp->wantToQuit())
{
delete g_pApp;
delete g_AppPlatform;
teardown();
// Stop Looping
return EM_FALSE;
} else {
}
else
{
// Keep Looping
return EM_TRUE;
}
}
// Main
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
LOGE("Unable To Initialize SDL: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
@@ -196,7 +239,8 @@ int main(int argc, char *argv[]) {
// Create Window
window = SDL_CreateWindow("ReMinecraftPE", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Minecraft::width, Minecraft::height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if (!window) {
if (!window)
{
LOGE("Unable To Create SDL Window\n");
exit(EXIT_FAILURE);
}
@@ -206,7 +250,8 @@ int main(int argc, char *argv[]) {
// Create OpenGL ES Context
context = SDL_GL_CreateContext(window);
if (!context) {
if (!context)
{
LOGE("Unable To Create OpenGL Context\n");
exit(EXIT_FAILURE);
}
@@ -244,9 +289,11 @@ int main(int argc, char *argv[]) {
// Loop
#ifndef __EMSCRIPTEN__
while (true) {
while (true)
{
EM_BOOL result = main_loop(0, nullptr);
if (result == EM_FALSE) {
if (result == EM_FALSE)
{
break;
}
}

View File

@@ -92,7 +92,8 @@ void Minecraft::grabMouse()
void Minecraft::setScreen(Screen* pScreen)
{
#ifndef ORIGINAL_CODE
if (pScreen == nullptr && !isLevelGenerated()) {
if (pScreen == nullptr && !isLevelGenerated())
{
return;
}
#endif

View File

@@ -8,8 +8,6 @@
#include "Textures.hpp"
#include "GameMode.hpp"
bool Textures::MIPMAP = false;
int Textures::loadTexture(const std::string& name, bool b)
@@ -71,13 +69,7 @@ int Textures::assignTexture(const std::string& name, Texture& texture)
if (texture.field_C)
internalFormat = GL_RGBA;
#ifdef ORIGINAL_CODE
GLuint format = GL_RGBA;
#else
GLuint format = internalFormat;
#endif
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, texture.m_width, texture.m_height, 0, format, GL_UNSIGNED_BYTE, texture.m_pixels);
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, texture.m_width, texture.m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.m_pixels);
m_textures[name] = textureID;