From 44f9b921246bc69350361d34bf67760333aa0975 Mon Sep 17 00:00:00 2001 From: Pouar Date: Thu, 18 Jun 2020 19:34:57 -0500 Subject: [PATCH 1/8] Bugfix The bullets snufit shoots immediately got deleted shortly after shooting when NODRAWDISTANCE was enabled. This fixes it --- src/game/behaviors/snufit.inc.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/game/behaviors/snufit.inc.c b/src/game/behaviors/snufit.inc.c index 76e78c09..2acf442f 100644 --- a/src/game/behaviors/snufit.inc.c +++ b/src/game/behaviors/snufit.inc.c @@ -181,10 +181,9 @@ void bhv_snufit_balls_loop(void) { // If far from Mario or in a different room, despawn. if ((o->activeFlags & ACTIVE_FLAG_IN_DIFFERENT_ROOM) #ifndef NODRAWINGDISTANCE - || (o->oTimer != 0 && o->oDistanceToMario > 1500.0f)) { -#else - || (o->oTimer != 0)) { + || (o->oTimer != 0 && o->oDistanceToMario > 1500.0f) #endif + ){ obj_mark_for_deletion(o); } From dfd1a084755aec7213b7542a6563a4683be80c2f Mon Sep 17 00:00:00 2001 From: fgsfds Date: Fri, 28 Aug 2020 22:55:40 +0300 Subject: [PATCH 2/8] add --poolsize arg for modifying main pool size --- src/game/memory.h | 1 + src/pc/cliopts.c | 9 +++++++++ src/pc/cliopts.h | 1 + src/pc/pc_main.c | 10 ++++++---- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/game/memory.h b/src/game/memory.h index 48cd703c..a1eba8a7 100644 --- a/src/game/memory.h +++ b/src/game/memory.h @@ -9,6 +9,7 @@ #define MEMORY_POOL_RIGHT 1 #define GFX_POOL_SIZE (512 * 1024) +#define DEFAULT_POOL_SIZE (0x165000 * 8) struct AllocOnlyPool { diff --git a/src/pc/cliopts.c b/src/pc/cliopts.c index b9e9ee77..54dbb5c7 100644 --- a/src/pc/cliopts.c +++ b/src/pc/cliopts.c @@ -34,6 +34,12 @@ static inline int arg_string(const char *name, const char *value, char *target) return 1; } +static inline int arg_uint(const char *name, const char *value, unsigned int *target) { + const unsigned long int v = strtoul(value, NULL, 0); + *target = v; + return 1; +} + void parse_cli_opts(int argc, char* argv[]) { // Initialize options with false values. memset(&gCLIOpts, 0, sizeof(gCLIOpts)); @@ -51,6 +57,9 @@ void parse_cli_opts(int argc, char* argv[]) { else if (strcmp(argv[i], "--cheats") == 0) // Enable cheats menu Cheats.EnableCheats = true; + else if (strcmp(argv[i], "--poolsize") == 0) // Main pool size + arg_uint("--poolsize", argv[++i], &gCLIOpts.PoolSize); + else if (strcmp(argv[i], "--configfile") == 0 && (i + 1) < argc) arg_string("--configfile", argv[++i], gCLIOpts.ConfigFile); diff --git a/src/pc/cliopts.h b/src/pc/cliopts.h index f9a45e18..a0281e22 100644 --- a/src/pc/cliopts.h +++ b/src/pc/cliopts.h @@ -6,6 +6,7 @@ struct PCCLIOptions { unsigned int SkipIntro; unsigned int FullScreen; + unsigned int PoolSize; char ConfigFile[SYS_MAX_PATH]; char SavePath[SYS_MAX_PATH]; char GameDir[SYS_MAX_PATH]; diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index ada661f1..923e7ea8 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -172,10 +172,6 @@ static void on_anim_frame(double time) { #endif void main_func(void) { - static u64 pool[0x165000/8 / 4 * sizeof(void *)]; - main_pool_init(pool, pool + sizeof(pool) / sizeof(pool[0])); - gEffectsMemoryPool = mem_pool_init(0x4000, MEMORY_POOL_LEFT); - const char *gamedir = gCLIOpts.GameDir[0] ? gCLIOpts.GameDir : FS_BASEDIR; const char *userpath = gCLIOpts.SavePath[0] ? gCLIOpts.SavePath : sys_user_path(); fs_init(sys_ropaths, gamedir, userpath); @@ -187,6 +183,12 @@ void main_func(void) { else if (gCLIOpts.FullScreen == 2) configWindow.fullscreen = false; + const size_t poolsize = gCLIOpts.PoolSize ? gCLIOpts.PoolSize : DEFAULT_POOL_SIZE; + u64 *pool = malloc(poolsize); + if (!pool) sys_fatal("Could not alloc %u bytes for main pool.\n", poolsize); + main_pool_init(pool, pool + poolsize / sizeof(pool[0])); + gEffectsMemoryPool = mem_pool_init(0x4000, MEMORY_POOL_LEFT); + #if defined(WAPI_SDL1) || defined(WAPI_SDL2) wm_api = &gfx_sdl; #elif defined(WAPI_DXGI) From 840eadb9e6f2f945ad04c7e1c6758c3ad9175b76 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sat, 29 Aug 2020 02:13:07 +0300 Subject: [PATCH 3/8] change GL_LEGACY to only use GL1.1 (1.2?) features --- src/pc/gfx/gfx_opengl_legacy.c | 587 ++++++++++++++++----------------- 1 file changed, 283 insertions(+), 304 deletions(-) diff --git a/src/pc/gfx/gfx_opengl_legacy.c b/src/pc/gfx/gfx_opengl_legacy.c index 4547b305..a069f349 100644 --- a/src/pc/gfx/gfx_opengl_legacy.c +++ b/src/pc/gfx/gfx_opengl_legacy.c @@ -3,6 +3,7 @@ #include #include #include +#include #ifndef _LANGUAGE_C # define _LANGUAGE_C @@ -32,40 +33,11 @@ # endif #endif -// redefine this if using a different GL loader -#define mglGetProcAddress(name) SDL_GL_GetProcAddress(name) - -// we'll define and load it manually in init, just in case -typedef void (*PFNMGLFOGCOORDPOINTERPROC)(GLenum type, GLsizei stride, const void *pointer); -static PFNMGLFOGCOORDPOINTERPROC mglFogCoordPointer = NULL; - -// since these can have different names, might as well redefine them to a single one -#undef GL_FOG_COORD_SRC -#undef GL_FOG_COORD -#undef GL_FOG_COORD_ARRAY -#define GL_FOG_COORD_SRC 0x8450 -#define GL_FOG_COORD 0x8451 -#define GL_FOG_COORD_ARRAY 0x8457 - #include "../platform.h" #include "gfx_cc.h" #include "gfx_rendering_api.h" #include "macros.h" -enum MixFlags { - SH_MF_OVERRIDE_ALPHA = 1, - - SH_MF_MULTIPLY = 2, - SH_MF_MIX = 4, - SH_MF_SINGLE = 8, - - SH_MF_MULTIPLY_ALPHA = 16, - SH_MF_MIX_ALPHA = 32, - SH_MF_SINGLE_ALPHA = 64, - - SH_MF_INPUT_ALPHA = 128, -}; - enum MixType { SH_MT_NONE, SH_MT_TEXTURE, @@ -76,119 +48,121 @@ enum MixType { }; struct ShaderProgram { + bool enabled; uint32_t shader_id; + struct CCFeatures cc; enum MixType mix; - uint32_t mix_flags; bool texture_used[2]; + int texture_ord[2]; int num_inputs; }; +struct SamplerState { + GLenum min_filter; + GLenum mag_filter; + GLenum wrap_s; + GLenum wrap_t; + GLuint tex; +}; + static struct ShaderProgram shader_program_pool[64]; static uint8_t shader_program_pool_size; static struct ShaderProgram *cur_shader = NULL; +static struct SamplerState tmu_state[2]; + static const float *cur_buf = NULL; static const float *cur_fog_ofs = NULL; static size_t cur_buf_size = 0; static size_t cur_buf_num_tris = 0; static size_t cur_buf_stride = 0; static bool gl_blend = false; -static bool gl_adv_fog = false; +static bool gl_npot = false; +static bool gl_multitexture = false; + +static void *scale_buf = NULL; +static int scale_buf_size = 0; + +static float c_mix[] = { 0.f, 0.f, 0.f, 1.f }; +static float c_invmix[] = { 1.f, 1.f, 1.f, 1.f }; static const float c_white[] = { 1.f, 1.f, 1.f, 1.f }; +// from https://github.com/z2442/sm64-port + +static void resample_32bit(const uint32_t *in, const int inwidth, const int inheight, uint32_t *out, const int outwidth, const int outheight) { + int i, j; + const uint32_t *inrow; + uint32_t frac, fracstep; + + fracstep = inwidth * 0x10000 / outwidth; + for (i = 0; i < outheight; i++, out += outwidth) { + inrow = in + inwidth * (i * inheight / outheight); + frac = fracstep >> 1; + for (j = 0; j < outwidth; j += 4) { + out[j] = inrow[frac >> 16]; + frac += fracstep; + out[j + 1] = inrow[frac >> 16]; + frac += fracstep; + out[j + 2] = inrow[frac >> 16]; + frac += fracstep; + out[j + 3] = inrow[frac >> 16]; + frac += fracstep; + } + } +} + +static inline uint32_t next_pot(uint32_t v) { + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v++; + return v; +} + +static inline uint32_t is_pot(const uint32_t v) { + return (v & (v - 1)) == 0; +} + static bool gfx_opengl_z_is_from_0_to_1(void) { return false; } -#define TEXENV_COMBINE_ON() glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE) -#define TEXENV_COMBINE_OFF() glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) - -#define TEXENV_COMBINE_OP(num, cval, aval) \ - do { \ - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND ## num ## _RGB, cval); \ - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND ## num ## _ALPHA, aval); \ - } while (0) - -#define TEXENV_COMBINE_SET1(what, mode, val) \ - do { \ - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ ## what, mode); \ - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ ## what, val); \ - } while (0) - -#define TEXENV_COMBINE_SET2(what, mode, val1, val2) \ - do { \ - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ ## what, mode); \ - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ ## what, val1); \ - glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ ## what, val2); \ - } while (0) - -#define TEXENV_COMBINE_SET3(what, mode, val1, val2, val3) \ - do { \ - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ ## what, mode); \ - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ ## what, val1); \ - glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ ## what, val2); \ - glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_ ## what, val3); \ - } while (0) - -static inline void texenv_set_texture_color(struct ShaderProgram *prg) { - glActiveTexture(GL_TEXTURE0); - - if (prg->mix_flags & SH_MF_OVERRIDE_ALPHA) { - TEXENV_COMBINE_ON(); - if (prg->mix_flags & SH_MF_SINGLE_ALPHA) { - if (prg->mix_flags & SH_MF_MULTIPLY) { - // keep the alpha but modulate the color - const GLenum alphasrc = (prg->mix_flags & SH_MF_INPUT_ALPHA) ? GL_PRIMARY_COLOR : GL_TEXTURE; - TEXENV_COMBINE_SET2(RGB, GL_MODULATE, GL_TEXTURE, GL_PRIMARY_COLOR); - TEXENV_COMBINE_SET1(ALPHA, GL_REPLACE, alphasrc); - } else { - // somehow makes it keep the color while taking the alpha from primary color - TEXENV_COMBINE_SET1(RGB, GL_REPLACE, GL_TEXTURE); - } - } else { // if (prg->mix_flags & SH_MF_SINGLE) { - if (prg->mix_flags & SH_MF_MULTIPLY_ALPHA) { - // modulate the alpha but keep the color - TEXENV_COMBINE_SET2(ALPHA, GL_MODULATE, GL_TEXTURE, GL_PRIMARY_COLOR); - TEXENV_COMBINE_SET1(RGB, GL_REPLACE, GL_TEXTURE); - } else { - // somehow makes it keep the alpha - TEXENV_COMBINE_SET1(ALPHA, GL_REPLACE, GL_TEXTURE); - } - } - // TODO: MIX and the other one - } else if (prg->mix_flags & SH_MF_MULTIPLY) { - // TODO: is this right? - TEXENV_COMBINE_OFF(); - } else if (prg->mix_flags & SH_MF_MIX) { - TEXENV_COMBINE_ON(); - // HACK: determine this using flags and not this crap - if (prg->num_inputs > 1) { - // out.rgb = mix(color0.rgb, color1.rgb, texel0.rgb); - // no color1 tho, so mix with white (texenv color is set in init()) - TEXENV_COMBINE_OP(2, GL_SRC_COLOR, GL_SRC_ALPHA); - TEXENV_COMBINE_SET3(RGB, GL_INTERPOLATE, GL_CONSTANT, GL_PRIMARY_COLOR, GL_TEXTURE); - TEXENV_COMBINE_SET1(ALPHA, GL_REPLACE, GL_CONSTANT); - } else { - // out.rgb = mix(color0.rgb, texel0.rgb, texel0.a); - TEXENV_COMBINE_OP(2, GL_SRC_ALPHA, GL_SRC_ALPHA); - TEXENV_COMBINE_SET3(RGB, GL_INTERPOLATE, GL_TEXTURE, GL_PRIMARY_COLOR, GL_TEXTURE); - } - } else { - TEXENV_COMBINE_OFF(); - } +static inline GLenum texenv_set_color(UNUSED struct ShaderProgram *prg) { + return GL_MODULATE; } -static inline void texenv_set_texture_texture(UNUSED struct ShaderProgram *prg) { - glActiveTexture(GL_TEXTURE0); - TEXENV_COMBINE_OFF(); - glActiveTexture(GL_TEXTURE1); - TEXENV_COMBINE_ON(); - // out.rgb = mix(texel0.rgb, texel1.rgb, color0.rgb); - TEXENV_COMBINE_OP(2, GL_SRC_COLOR, GL_SRC_ALPHA); - TEXENV_COMBINE_SET3(RGB, GL_INTERPOLATE, GL_PREVIOUS, GL_TEXTURE, GL_PRIMARY_COLOR); - // out.a = texel0.a; - TEXENV_COMBINE_SET1(ALPHA, GL_REPLACE, GL_PREVIOUS); +static inline GLenum texenv_set_texture(UNUSED struct ShaderProgram *prg) { + return GL_MODULATE; +} + +static inline GLenum texenv_set_texture_color(struct ShaderProgram *prg) { + GLenum mode; + + // HACK: lord forgive me for this, but this is easier + + switch (prg->shader_id) { + case 0x0000038D: // mario's eyes + case 0x01045A00: // peach letter + case 0x01200A00: // intro copyright fade in + mode = GL_DECAL; + break; + case 0x00000551: // goddard + mode = GL_BLEND; + break; + default: + mode = GL_MODULATE; + break; + } + + return mode; +} + +static inline GLenum texenv_set_texture_texture(UNUSED struct ShaderProgram *prg) { + return GL_MODULATE; } static void gfx_opengl_apply_shader(struct ShaderProgram *prg) { @@ -199,31 +173,19 @@ static void gfx_opengl_apply_shader(struct ShaderProgram *prg) { ofs += 4; // have texture(s), specify same texcoords for every active texture - for (int i = 0; i < 2; ++i) { - if (prg->texture_used[i]) { - glEnable(GL_TEXTURE0 + i); - glClientActiveTexture(GL_TEXTURE0 + i); - glActiveTexture(GL_TEXTURE0 + i); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(2, GL_FLOAT, cur_buf_stride, ofs); - glEnable(GL_TEXTURE_2D); - ofs += 2; - } + if (prg->texture_used[0] || prg->texture_used[1]) { + glEnable(GL_TEXTURE_2D); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, cur_buf_stride, ofs); + ofs += 2; + } else { + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisable(GL_TEXTURE_2D); } if (prg->shader_id & SHADER_OPT_FOG) { - // fog requested, we can deal with it in one of two ways - if (gl_adv_fog) { - // if GL_EXT_fog_coord is available, use the provided fog factor as scaled depth for GL fog - const float fogrgb[] = { ofs[0], ofs[1], ofs[2] }; - glEnable(GL_FOG); - glFogfv(GL_FOG_COLOR, fogrgb); // color is the same for all verts, only intensity is different - glEnableClientState(GL_FOG_COORD_ARRAY); - mglFogCoordPointer(GL_FLOAT, cur_buf_stride, ofs + 3); // point it to alpha, which is fog factor - } else { - // if there's no fog coords available, blend it on top of normal tris later - cur_fog_ofs = ofs; - } + // blend it on top of normal tris later + cur_fog_ofs = ofs; ofs += 4; } @@ -232,139 +194,112 @@ static void gfx_opengl_apply_shader(struct ShaderProgram *prg) { // TODO: more than one color (maybe glSecondaryColorPointer?) // HACK: if there's a texture and two colors, one of them is likely for speculars or some shit (see mario head) // if there's two colors but no texture, the real color is likely the second one - const int hack = (prg->num_inputs > 1) * (4 - (int)prg->texture_used[0]); - glEnableClientState(GL_COLOR_ARRAY); - glColorPointer(4, GL_FLOAT, cur_buf_stride, ofs + hack); - ofs += 4 * prg->num_inputs; + // HACKHACK: alpha is 0 in the transition shader (0x01A00045), maybe figure out the flags instead + const int vlen = (prg->cc.opt_alpha && prg->shader_id != 0x01A00045) ? 4 : 3; + const int hack = vlen * (prg->num_inputs > 1); + + if (prg->texture_used[1] && prg->cc.do_mix[0]) { + // HACK: when two textures are mixed by vertex color, store the color + // it will be used later when rendering two texture passes + c_mix[0] = *(ofs + hack + 0); + c_mix[1] = *(ofs + hack + 1); + c_mix[2] = *(ofs + hack + 2); + c_invmix[0] = 1.f - c_mix[0]; + c_invmix[1] = 1.f - c_mix[1]; + c_invmix[2] = 1.f - c_mix[2]; + glDisableClientState(GL_COLOR_ARRAY); + glColor3f(c_mix[0], c_mix[1], c_mix[2]); + } else { + // otherwise use vertex colors as normal + glEnableClientState(GL_COLOR_ARRAY); + glColorPointer(vlen, GL_FLOAT, cur_buf_stride, ofs + hack); + } + + ofs += prg->num_inputs * vlen; + } else { + glDisableClientState(GL_COLOR_ARRAY); } - if (prg->shader_id & SHADER_OPT_TEXTURE_EDGE) { - // (horrible) alpha discard - glEnable(GL_ALPHA_TEST); - glAlphaFunc(GL_GREATER, 0.3f); - } + if (!prg->enabled) { + // we only need to do this once + prg->enabled = true; - // configure formulae - switch (prg->mix) { - case SH_MT_TEXTURE: - glActiveTexture(GL_TEXTURE0); - TEXENV_COMBINE_OFF(); - break; + if (prg->shader_id & SHADER_OPT_TEXTURE_EDGE) { + // (horrible) alpha discard + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_GREATER, 0.666f); + } else { + glDisable(GL_ALPHA_TEST); + } - case SH_MT_TEXTURE_COLOR: - texenv_set_texture_color(prg); - break; - - case SH_MT_TEXTURE_TEXTURE: - texenv_set_texture_texture(prg); - break; - - default: - break; + // configure texenv + GLenum mode; + switch (prg->mix) { + case SH_MT_TEXTURE: mode = texenv_set_texture(prg); break; + case SH_MT_TEXTURE_TEXTURE: mode = texenv_set_texture_texture(prg); break; + case SH_MT_TEXTURE_COLOR: mode = texenv_set_texture_color(prg); break; + default: mode = texenv_set_color(prg); break; + } + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode); } } static void gfx_opengl_unload_shader(struct ShaderProgram *old_prg) { - if (cur_shader == old_prg || old_prg == NULL) + if (cur_shader && (cur_shader == old_prg || !old_prg)) { + cur_shader->enabled = false; cur_shader = NULL; - - glClientActiveTexture(GL_TEXTURE0); - glActiveTexture(GL_TEXTURE0); - glDisable(GL_TEXTURE_2D); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - - glClientActiveTexture(GL_TEXTURE1); - glActiveTexture(GL_TEXTURE1); - glDisable(GL_TEXTURE_2D); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - - glDisable(GL_TEXTURE1); - glDisable(GL_TEXTURE0); - glDisable(GL_TEXTURE_2D); - glDisable(GL_ALPHA_TEST); - glDisable(GL_FOG); + } cur_fog_ofs = NULL; // clear fog colors - - glDisableClientState(GL_COLOR_ARRAY); - if (gl_adv_fog) glDisableClientState(GL_FOG_COORD_ARRAY); } static void gfx_opengl_load_shader(struct ShaderProgram *new_prg) { cur_shader = new_prg; - // gfx_opengl_apply_shader(cur_shader); + if (cur_shader) + cur_shader->enabled = false; } static struct ShaderProgram *gfx_opengl_create_and_load_new_shader(uint32_t shader_id) { - uint8_t c[2][4]; - for (int i = 0; i < 4; i++) { - c[0][i] = (shader_id >> (i * 3)) & 7; - c[1][i] = (shader_id >> (12 + i * 3)) & 7; - } - - bool used_textures[2] = {0, 0}; - int num_inputs = 0; - for (int i = 0; i < 2; i++) { - for (int j = 0; j < 4; j++) { - if (c[i][j] >= SHADER_INPUT_1 && c[i][j] <= SHADER_INPUT_4) { - if (c[i][j] > num_inputs) { - num_inputs = c[i][j]; - } - } - if (c[i][j] == SHADER_TEXEL0 || c[i][j] == SHADER_TEXEL0A) { - used_textures[0] = true; - } - if (c[i][j] == SHADER_TEXEL1) { - used_textures[1] = true; - } - } - } - - const bool color_alpha_same = (shader_id & 0xfff) == ((shader_id >> 12) & 0xfff); - const bool do_multiply[2] = {c[0][1] == 0 && c[0][3] == 0, c[1][1] == 0 && c[1][3] == 0}; - const bool do_mix[2] = {c[0][1] == c[0][3], c[1][1] == c[1][3]}; - const bool do_single[2] = {c[0][2] == 0, c[1][2] == 0}; + struct CCFeatures ccf; + gfx_cc_get_features(shader_id, &ccf); struct ShaderProgram *prg = &shader_program_pool[shader_program_pool_size++]; prg->shader_id = shader_id; - prg->num_inputs = num_inputs; - prg->texture_used[0] = used_textures[0]; - prg->texture_used[1] = used_textures[1]; + prg->cc = ccf; + prg->num_inputs = ccf.num_inputs; + prg->texture_used[0] = ccf.used_textures[0]; + prg->texture_used[1] = ccf.used_textures[1]; - if (used_textures[0] && used_textures[1]) + if (ccf.used_textures[0] && ccf.used_textures[1]) { prg->mix = SH_MT_TEXTURE_TEXTURE; - else if (used_textures[0] && num_inputs) + if (ccf.do_single[1]) { + prg->texture_ord[0] = 1; + prg->texture_ord[1] = 0; + } else { + prg->texture_ord[0] = 0; + prg->texture_ord[1] = 1; + } + } else if (ccf.used_textures[0] && ccf.num_inputs) { prg->mix = SH_MT_TEXTURE_COLOR; - else if (used_textures[0]) + } else if (ccf.used_textures[0]) { prg->mix = SH_MT_TEXTURE; - else if (num_inputs > 1) + } else if (ccf.num_inputs > 1) { prg->mix = SH_MT_COLOR_COLOR; - else if (num_inputs) + } else if (ccf.num_inputs) { prg->mix = SH_MT_COLOR; - - if (do_single[0]) prg->mix_flags |= SH_MF_SINGLE; - if (do_multiply[0]) prg->mix_flags |= SH_MF_MULTIPLY; - if (do_mix[0]) prg->mix_flags |= SH_MF_MIX; - - if (!color_alpha_same && (shader_id & SHADER_OPT_ALPHA)) { - prg->mix_flags |= SH_MF_OVERRIDE_ALPHA; - if (do_single[1]) prg->mix_flags |= SH_MF_SINGLE_ALPHA; - if (do_multiply[1]) prg->mix_flags |= SH_MF_MULTIPLY_ALPHA; - if (do_mix[1]) prg->mix_flags |= SH_MF_MIX_ALPHA; - if (c[1][3] < SHADER_TEXEL0) prg->mix_flags |= SH_MF_INPUT_ALPHA; } + prg->enabled = false; + gfx_opengl_load_shader(prg); return prg; } static struct ShaderProgram *gfx_opengl_lookup_shader(uint32_t shader_id) { - for (size_t i = 0; i < shader_program_pool_size; i++) { - if (shader_program_pool[i].shader_id == shader_id) { + for (size_t i = 0; i < shader_program_pool_size; i++) + if (shader_program_pool[i].shader_id == shader_id) return &shader_program_pool[i]; - } - } return NULL; } @@ -381,35 +316,72 @@ static GLuint gfx_opengl_new_texture(void) { } static void gfx_opengl_select_texture(int tile, GLuint texture_id) { - glActiveTexture(GL_TEXTURE0 + tile); + tmu_state[tile].tex = texture_id; // remember this for multitexturing later glBindTexture(GL_TEXTURE_2D, texture_id); } -static void gfx_opengl_upload_texture(uint8_t *rgba32_buf, int width, int height) { +static inline void *gfx_opengl_scale_texture(const uint8_t *data, const int w, const int h, const int to_w, const int to_h) { + const int psize = to_w * to_h * 4; + + // realloc scale buffer if it's too small + if (psize > scale_buf_size) { + scale_buf = realloc(scale_buf, psize); + if (!scale_buf) sys_fatal("Out of memory allocating NPOT scale buffer\n"); + scale_buf_size = psize; + } + + resample_32bit((const uint32_t *)data, w, h, scale_buf, to_w, to_h); + + return scale_buf; +} + +static void gfx_opengl_upload_texture(const uint8_t *rgba32_buf, int width, int height) { + if (!gl_npot) { + // we don't support non power of two textures, scale to next power of two if necessary + if (!is_pot(width) || !is_pot(height)) { + const int pwidth = next_pot(width); + const int pheight = next_pot(height); + rgba32_buf = gfx_opengl_scale_texture(rgba32_buf, width, height, pwidth, pheight); + width = pwidth; + height = pheight; + } + } + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba32_buf); } -static uint32_t gfx_cm_to_opengl(uint32_t val) { - if (val & G_TX_CLAMP) - return GL_CLAMP_TO_EDGE; +static inline GLenum gfx_cm_to_opengl(uint32_t val) { + if (val & G_TX_CLAMP) return GL_CLAMP_TO_EDGE; return (val & G_TX_MIRROR) ? GL_MIRRORED_REPEAT : GL_REPEAT; } +static inline void gfx_opengl_apply_tmu_state(const int tile) { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, tmu_state[tile].min_filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, tmu_state[tile].mag_filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, tmu_state[tile].wrap_s); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tmu_state[tile].wrap_t); +} + static void gfx_opengl_set_sampler_parameters(int tile, bool linear_filter, uint32_t cms, uint32_t cmt) { const GLenum filter = linear_filter ? GL_LINEAR : GL_NEAREST; - glActiveTexture(GL_TEXTURE0 + tile); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, gfx_cm_to_opengl(cms)); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, gfx_cm_to_opengl(cmt)); + + const GLenum wrap_s = gfx_cm_to_opengl(cms); + const GLenum wrap_t = gfx_cm_to_opengl(cmt); + + tmu_state[tile].min_filter = filter; + tmu_state[tile].mag_filter = filter; + tmu_state[tile].wrap_s = wrap_s; + tmu_state[tile].wrap_t = wrap_t; + + // set state for the first texture right away + if (!tile) gfx_opengl_apply_tmu_state(tile); } static void gfx_opengl_set_depth_test(bool depth_test) { - if (depth_test) { + if (depth_test) glEnable(GL_DEPTH_TEST); - } else { + else glDisable(GL_DEPTH_TEST); - } } static void gfx_opengl_set_depth_mask(bool z_upd) { @@ -436,26 +408,19 @@ static void gfx_opengl_set_scissor(int x, int y, int width, int height) { static void gfx_opengl_set_use_alpha(bool use_alpha) { gl_blend = use_alpha; - if (use_alpha) { + if (use_alpha) glEnable(GL_BLEND); - } else { + else glDisable(GL_BLEND); - } } // draws the same triangles as plain fog color + fog intensity as alpha // on top of the normal tris and blends them to achieve sort of the same effect // as fog would -static inline void gfx_opengl_blend_fog_tris(void) { - // if a texture was used, replace it with fog color instead, but still keep the alpha - if (cur_shader->texture_used[0]) { - glActiveTexture(GL_TEXTURE0); - TEXENV_COMBINE_ON(); - // out.rgb = input0.rgb - TEXENV_COMBINE_SET1(RGB, GL_REPLACE, GL_PRIMARY_COLOR); - // out.a = texel0.a * input0.a - TEXENV_COMBINE_SET2(ALPHA, GL_MODULATE, GL_TEXTURE, GL_PRIMARY_COLOR); - } +static inline void gfx_opengl_pass_fog(void) { + // if texturing is enabled, disable it, since we're blending colors + if (cur_shader->texture_used[0] || cur_shader->texture_used[1]) + glDisable(GL_TEXTURE_2D); glEnableClientState(GL_COLOR_ARRAY); // enable color array temporarily glColorPointer(4, GL_FLOAT, cur_buf_stride, cur_fog_ofs); // set fog colors as primary colors @@ -467,11 +432,38 @@ static inline void gfx_opengl_blend_fog_tris(void) { glDepthFunc(GL_LESS); // set back to default if (!gl_blend) glDisable(GL_BLEND); // disable blending if it was disabled glDisableClientState(GL_COLOR_ARRAY); // will get reenabled later anyway + + // if texturing was enabled, re-enable it + if (cur_shader->texture_used[0] || cur_shader->texture_used[1]) + glEnable(GL_TEXTURE_2D); +} + +// this assumes the two textures are combined like so: +// result = mix(tex0.rgb, tex1.rgb, vertex.rgb) +static inline void gfx_opengl_pass_mix_texture(void) { + // set second texture + glBindTexture(GL_TEXTURE_2D, tmu_state[cur_shader->texture_ord[1]].tex); + gfx_opengl_apply_tmu_state(cur_shader->texture_ord[1]); + + if (!gl_blend) glEnable(GL_BLEND); // enable blending temporarily + glBlendFunc(GL_ONE, GL_ONE); // additive blending + glDepthFunc(GL_LEQUAL); // Z is the same as the base triangles + + // draw the same triangles, but with the inverse of the mix color + glColor3f(c_invmix[0], c_invmix[1], c_invmix[2]); + glDrawArrays(GL_TRIANGLES, 0, 3 * cur_buf_num_tris); + glColor3f(1.f, 1.f, 1.f); // reset color + + glDepthFunc(GL_LESS); // set back to default + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // same here + if (!gl_blend) glDisable(GL_BLEND); // disable blending if it was disabled + + // set old texture + glBindTexture(GL_TEXTURE_2D, tmu_state[cur_shader->texture_ord[0]].tex); + gfx_opengl_apply_tmu_state(cur_shader->texture_ord[0]); } static void gfx_opengl_draw_triangles(float buf_vbo[], size_t buf_vbo_len, size_t buf_vbo_num_tris) { - //printf("flushing %d tris\n", buf_vbo_num_tris); - cur_buf = buf_vbo; cur_buf_size = buf_vbo_len * 4; cur_buf_num_tris = buf_vbo_num_tris; @@ -479,10 +471,17 @@ static void gfx_opengl_draw_triangles(float buf_vbo[], size_t buf_vbo_len, size_ gfx_opengl_apply_shader(cur_shader); + // if there's two textures, set primary texture first + if (cur_shader->texture_used[1]) + glBindTexture(GL_TEXTURE_2D, tmu_state[cur_shader->texture_ord[0]].tex); + glDrawArrays(GL_TRIANGLES, 0, 3 * cur_buf_num_tris); + // if there's two textures, draw polys with the second texture + if (cur_shader->texture_used[1]) gfx_opengl_pass_mix_texture(); + // cur_fog_ofs is only set if GL_EXT_fog_coord isn't used - if (cur_fog_ofs) gfx_opengl_blend_fog_tris(); + if (cur_fog_ofs) gfx_opengl_pass_fog(); } static inline bool gl_check_ext(const char *name) { @@ -492,7 +491,7 @@ static inline bool gl_check_ext(const char *name) { extstr = (const char *)glGetString(GL_EXTENSIONS); if (!strstr(extstr, name)) { - fprintf(stderr, "GL extension not supported: %s\n", name); + printf("GL extension not supported: %s\n", name); return false; } @@ -526,54 +525,34 @@ static void gfx_opengl_init(void) { int vmajor, vminor; bool is_es = false; gl_get_version(&vmajor, &vminor, &is_es); - if (vmajor < 2 && vminor < 2 && !is_es) - sys_fatal("OpenGL 1.2+ is required.\nReported version: %s%d.%d", is_es ? "ES" : "", vmajor, vminor); + if ((vmajor < 2 && vminor < 1) || is_es) + sys_fatal("OpenGL 1.1+ is required.\nReported version: %s%d.%d\n", is_es ? "ES" : "", vmajor, vminor); - // check extensions that we need - const bool supported = - gl_check_ext("GL_ARB_multitexture") && - gl_check_ext("GL_ARB_texture_env_combine"); - - if (!supported) - sys_fatal("required GL extensions are not supported"); - - gl_adv_fog = false; - - // check whether we can use advanced fog shit - const bool fog_ext = - vmajor > 1 || vminor > 3 || - gl_check_ext("GL_EXT_fog_coord") || - gl_check_ext("GL_ARB_fog_coord"); - - if (fog_ext) { - // try to load manually, as this might be an extension, and even then the ext list may lie - mglFogCoordPointer = mglGetProcAddress("glFogCoordPointer"); - if (!mglFogCoordPointer) mglFogCoordPointer = mglGetProcAddress("glFogCoordPointerEXT"); - if (!mglFogCoordPointer) mglFogCoordPointer = mglGetProcAddress("glFogCoordPointerARB"); - if (!mglFogCoordPointer) - printf("glFogCoordPointer is not actually available, it won't be used.\n"); - else - gl_adv_fog = true; // appears to be all good + // check if we support non power of two textures + gl_npot = gl_check_ext("GL_ARB_texture_non_power_of_two"); + if (!gl_npot) { + // don't support NPOT textures, prepare buffer for rescaling + // this will be realloc'd as necessary + scale_buf_size = 64 * 64 * 4; + scale_buf = malloc(scale_buf_size); + if (!scale_buf) sys_fatal("Out of memory allocating for NPOT scale buffer\n"); } + // check if we support multitexturing + gl_multitexture = vmajor > 1 || vminor > 2 || gl_check_ext("GL_ARB_multitexture"); + printf("GL_VERSION = %s\n", glGetString(GL_VERSION)); printf("GL_EXTENSIONS =\n%s\n", glGetString(GL_EXTENSIONS)); - if (gl_adv_fog) { - // set fog params, they never change - printf("GL_EXT_fog_coord available, using that for fog\n"); - glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD); - glFogi(GL_FOG_MODE, GL_LINEAR); - glFogf(GL_FOG_START, 0.0f); - glFogf(GL_FOG_END, 1.0f); - } - // these also never change + glDisable(GL_LIGHTING); + glDisable(GL_CULL_FACE); + // glDisable(GL_DITHER); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glEnableClientState(GL_VERTEX_ARRAY); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, c_white); - TEXENV_COMBINE_OP(0, GL_SRC_COLOR, GL_SRC_ALPHA); - TEXENV_COMBINE_OP(1, GL_SRC_COLOR, GL_SRC_ALPHA); } static void gfx_opengl_on_resize(void) { From 468887a6f97c08137784443bd5841ec826e793ba Mon Sep 17 00:00:00 2001 From: Garrett Date: Fri, 28 Aug 2020 23:36:56 -0400 Subject: [PATCH 4/8] Check for zero rumble setting before allowing rumble Fixes controllers which don't check for rumble_strength and have constant rumble from rumbling even when set to 0 in the config. --- src/pc/controller/controller_entry_point.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pc/controller/controller_entry_point.c b/src/pc/controller/controller_entry_point.c index d5403f93..bc5e9fe6 100644 --- a/src/pc/controller/controller_entry_point.c +++ b/src/pc/controller/controller_entry_point.c @@ -33,12 +33,16 @@ s32 osContInit(UNUSED OSMesgQueue *mq, u8 *controllerBits, UNUSED OSContStatus * s32 osMotorStart(UNUSED void *pfs) { // Since rumble stops by osMotorStop, its duration is not nessecary. // Set it to 5 seconds and hope osMotorStop() is called in time. - controller_rumble_play(configRumbleStrength / 100.0f, 5.0f); + if (configRumbleStrength>0){ + controller_rumble_play(configRumbleStrength / 100.0f, 5.0f); + } return 0; } s32 osMotorStop(UNUSED void *pfs) { - controller_rumble_stop(); + if (configRumbleStrength>0){ + controller_rumble_stop(); + } return 0; } From da20f79e96c24f94f242042a21553687e47aa4a0 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sun, 30 Aug 2020 01:29:26 +0300 Subject: [PATCH 5/8] GL_LEGACY: this should've been GL_REPLACE all along --- src/pc/gfx/gfx_opengl_legacy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pc/gfx/gfx_opengl_legacy.c b/src/pc/gfx/gfx_opengl_legacy.c index a069f349..2e3f1216 100644 --- a/src/pc/gfx/gfx_opengl_legacy.c +++ b/src/pc/gfx/gfx_opengl_legacy.c @@ -132,11 +132,11 @@ static bool gfx_opengl_z_is_from_0_to_1(void) { } static inline GLenum texenv_set_color(UNUSED struct ShaderProgram *prg) { - return GL_MODULATE; + return GL_REPLACE; } static inline GLenum texenv_set_texture(UNUSED struct ShaderProgram *prg) { - return GL_MODULATE; + return GL_REPLACE; } static inline GLenum texenv_set_texture_color(struct ShaderProgram *prg) { From 420d82ce8c9c9d0746e4be6ec33f2f4d358484ac Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sun, 30 Aug 2020 15:39:41 +0300 Subject: [PATCH 6/8] fix spaces --- src/pc/controller/controller_entry_point.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pc/controller/controller_entry_point.c b/src/pc/controller/controller_entry_point.c index bc5e9fe6..f1eeeedc 100644 --- a/src/pc/controller/controller_entry_point.c +++ b/src/pc/controller/controller_entry_point.c @@ -33,16 +33,14 @@ s32 osContInit(UNUSED OSMesgQueue *mq, u8 *controllerBits, UNUSED OSContStatus * s32 osMotorStart(UNUSED void *pfs) { // Since rumble stops by osMotorStop, its duration is not nessecary. // Set it to 5 seconds and hope osMotorStop() is called in time. - if (configRumbleStrength>0){ + if (configRumbleStrength) controller_rumble_play(configRumbleStrength / 100.0f, 5.0f); - } return 0; } s32 osMotorStop(UNUSED void *pfs) { - if (configRumbleStrength>0){ + if (configRumbleStrength) controller_rumble_stop(); - } return 0; } From dd0e86dbee48dce28f1de7bb05dad51574461ed7 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sun, 30 Aug 2020 16:06:22 +0300 Subject: [PATCH 7/8] add rudimentary SDL1 controller backend it's more to deal with the mouse not working I suppose --- Makefile | 2 +- src/pc/controller/controller_entry_point.c | 5 +- src/pc/controller/controller_sdl1.c | 285 +++++++++++++++++++++ 3 files changed, 288 insertions(+), 4 deletions(-) create mode 100644 src/pc/controller/controller_sdl1.c diff --git a/Makefile b/Makefile index fc8f8e61..aa9c3d7a 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ RENDER_API ?= GL WINDOW_API ?= SDL2 # Audio backends: SDL1, SDL2 AUDIO_API ?= SDL2 -# Controller backends (can have multiple, space separated): SDL2 +# Controller backends (can have multiple, space separated): SDL2, SDL1 CONTROLLER_API ?= SDL2 # Misc settings for EXTERNAL_DATA diff --git a/src/pc/controller/controller_entry_point.c b/src/pc/controller/controller_entry_point.c index 5e8f16c9..34ca9ca0 100644 --- a/src/pc/controller/controller_entry_point.c +++ b/src/pc/controller/controller_entry_point.c @@ -13,16 +13,15 @@ static struct ControllerAPI *controller_implementations[] = { &controller_recorded_tas, - #ifdef CAPI_SDL2 + #if defined(CAPI_SDL2) || defined(CAPI_SDL1) &controller_sdl, #endif &controller_keyboard, }; s32 osContInit(UNUSED OSMesgQueue *mq, u8 *controllerBits, UNUSED OSContStatus *status) { - for (size_t i = 0; i < sizeof(controller_implementations) / sizeof(struct ControllerAPI *); i++) { + for (size_t i = 0; i < sizeof(controller_implementations) / sizeof(struct ControllerAPI *); i++) controller_implementations[i]->init(); - } *controllerBits = 1; return 0; } diff --git a/src/pc/controller/controller_sdl1.c b/src/pc/controller/controller_sdl1.c new file mode 100644 index 00000000..37316674 --- /dev/null +++ b/src/pc/controller/controller_sdl1.c @@ -0,0 +1,285 @@ +#ifdef CAPI_SDL1 + +#include +#include +#include +#include + +#include + +// Analog camera movement by Pathétique (github.com/vrmiguel), y0shin and Mors +// Contribute or communicate bugs at github.com/vrmiguel/sm64-analog-camera + +#include + +#include "controller_api.h" +#include "controller_sdl.h" +#include "../configfile.h" +#include "../platform.h" +#include "../fs/fs.h" + +#include "game/level_update.h" + +// mouse buttons are also in the controller namespace (why), just offset 0x100 +#define VK_OFS_SDL_MOUSE 0x0100 +#define VK_BASE_SDL_MOUSE (VK_BASE_SDL_GAMEPAD + VK_OFS_SDL_MOUSE) +#define MAX_JOYBINDS 32 +#define MAX_MOUSEBUTTONS 8 // arbitrary +#define MAX_JOYBUTTONS 32 // arbitrary; includes virtual keys for triggers +#define AXIS_THRESHOLD (30 * 256) + +enum { + JOY_AXIS_LEFTX, + JOY_AXIS_LEFTY, + JOY_AXIS_RIGHTX, + JOY_AXIS_RIGHTY, + JOY_AXIS_LTRIG, + JOY_AXIS_RTRIG, + MAX_AXES, +}; + +int mouse_x; +int mouse_y; + +#ifdef BETTERCAMERA +extern u8 newcam_mouse; +#endif + +static bool init_ok; +static SDL_Joystick *sdl_joy; + +static u32 num_joy_binds = 0; +static u32 num_mouse_binds = 0; +static u32 joy_binds[MAX_JOYBINDS][2]; +static u32 mouse_binds[MAX_JOYBINDS][2]; +static int joy_axis_binds[MAX_AXES] = { 0, 1, 2, 3, 4, 5 }; + +static bool joy_buttons[MAX_JOYBUTTONS] = { false }; +static u32 mouse_buttons = 0; +static u32 last_mouse = VK_INVALID; +static u32 last_joybutton = VK_INVALID; + +static int num_joy_axes = 0; +static int num_joy_buttons = 0; +static int num_joy_hats = 0; + +static inline void controller_add_binds(const u32 mask, const u32 *btns) { + for (u32 i = 0; i < MAX_BINDS; ++i) { + if (btns[i] >= VK_BASE_SDL_GAMEPAD && btns[i] <= VK_BASE_SDL_GAMEPAD + VK_SIZE) { + if (btns[i] >= VK_BASE_SDL_MOUSE && num_joy_binds < MAX_JOYBINDS) { + mouse_binds[num_mouse_binds][0] = btns[i] - VK_BASE_SDL_MOUSE; + mouse_binds[num_mouse_binds][1] = mask; + ++num_mouse_binds; + } else if (num_mouse_binds < MAX_JOYBINDS) { + joy_binds[num_joy_binds][0] = btns[i] - VK_BASE_SDL_GAMEPAD; + joy_binds[num_joy_binds][1] = mask; + ++num_joy_binds; + } + } + } +} + +static void controller_sdl_bind(void) { + bzero(joy_binds, sizeof(joy_binds)); + bzero(mouse_binds, sizeof(mouse_binds)); + num_joy_binds = 0; + num_mouse_binds = 0; + + controller_add_binds(A_BUTTON, configKeyA); + controller_add_binds(B_BUTTON, configKeyB); + controller_add_binds(Z_TRIG, configKeyZ); + controller_add_binds(STICK_UP, configKeyStickUp); + controller_add_binds(STICK_LEFT, configKeyStickLeft); + controller_add_binds(STICK_DOWN, configKeyStickDown); + controller_add_binds(STICK_RIGHT, configKeyStickRight); + controller_add_binds(U_CBUTTONS, configKeyCUp); + controller_add_binds(L_CBUTTONS, configKeyCLeft); + controller_add_binds(D_CBUTTONS, configKeyCDown); + controller_add_binds(R_CBUTTONS, configKeyCRight); + controller_add_binds(L_TRIG, configKeyL); + controller_add_binds(R_TRIG, configKeyR); + controller_add_binds(START_BUTTON, configKeyStart); +} + +static void controller_sdl_init(void) { + if (SDL_Init(SDL_INIT_JOYSTICK) != 0) { + fprintf(stderr, "SDL init error: %s\n", SDL_GetError()); + return; + } + + if (SDL_NumJoysticks() > 0) + sdl_joy = SDL_JoystickOpen(0); + + if (sdl_joy) { + num_joy_axes = SDL_JoystickNumAxes(sdl_joy); + num_joy_buttons = SDL_JoystickNumButtons(sdl_joy); + num_joy_hats = SDL_JoystickNumHats(sdl_joy); + + for (int i = 0; i < MAX_AXES; ++i) + if (i >= num_joy_axes) + joy_axis_binds[i] = -1; + } + +#ifdef BETTERCAMERA + if (newcam_mouse == 1) + SDL_WM_GrabInput(SDL_GRAB_ON); + SDL_GetRelativeMouseState(&mouse_x, &mouse_y); +#endif + + controller_sdl_bind(); + + init_ok = true; +} + +static inline void update_button(const int i, const bool new) { + const bool pressed = !joy_buttons[i] && new; + joy_buttons[i] = new; + if (pressed) last_joybutton = i; +} + +static inline int16_t get_axis(const int i) { + if (joy_axis_binds[i] >= 0) + return SDL_JoystickGetAxis(sdl_joy, i); + else + return 0; +} + +static void controller_sdl_read(OSContPad *pad) { + if (!init_ok) return; + +#ifdef BETTERCAMERA + if (newcam_mouse == 1 && sCurrPlayMode != 2) + SDL_WM_GrabInput(SDL_GRAB_ON); + else + SDL_WM_GrabInput(SDL_GRAB_OFF); + + u32 mouse = SDL_GetRelativeMouseState(&mouse_x, &mouse_y); + + for (u32 i = 0; i < num_mouse_binds; ++i) + if (mouse & SDL_BUTTON(mouse_binds[i][0])) + pad->button |= mouse_binds[i][1]; + + // remember buttons that changed from 0 to 1 + last_mouse = (mouse_buttons ^ mouse) & mouse; + mouse_buttons = mouse; +#endif + + if (!sdl_joy) return; + + SDL_JoystickUpdate(); + + int16_t leftx = get_axis(JOY_AXIS_LEFTX); + int16_t lefty = get_axis(JOY_AXIS_LEFTY); + int16_t rightx = get_axis(JOY_AXIS_RIGHTX); + int16_t righty = get_axis(JOY_AXIS_RIGHTY); + + int16_t ltrig = get_axis(JOY_AXIS_LTRIG); + int16_t rtrig = get_axis(JOY_AXIS_RTRIG); + +#ifdef TARGET_WEB + // Firefox has a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1606562 + // It sets down y to 32768.0f / 32767.0f, which is greater than the allowed 1.0f, + // which SDL then converts to a int16_t by multiplying by 32767.0f, which overflows into -32768. + // Maximum up will hence never become -32768 with the current version of SDL2, + // so this workaround should be safe in compliant browsers. + if (lefty == -32768) { + lefty = 32767; + } + if (righty == -32768) { + righty = 32767; + } +#endif + + for (int i = 0; i < num_joy_buttons; ++i) { + const bool new = SDL_JoystickGetButton(sdl_joy, i); + update_button(i, new); + } + + update_button(VK_LTRIGGER - VK_BASE_SDL_GAMEPAD, ltrig > AXIS_THRESHOLD); + update_button(VK_RTRIGGER - VK_BASE_SDL_GAMEPAD, rtrig > AXIS_THRESHOLD); + + u32 buttons_down = 0; + for (u32 i = 0; i < num_joy_binds; ++i) + if (joy_buttons[joy_binds[i][0]]) + buttons_down |= joy_binds[i][1]; + + pad->button |= buttons_down; + + const u32 xstick = buttons_down & STICK_XMASK; + const u32 ystick = buttons_down & STICK_YMASK; + if (xstick == STICK_LEFT) + pad->stick_x = -128; + else if (xstick == STICK_RIGHT) + pad->stick_x = 127; + if (ystick == STICK_DOWN) + pad->stick_y = -128; + else if (ystick == STICK_UP) + pad->stick_y = 127; + + if (rightx < -0x4000) pad->button |= L_CBUTTONS; + if (rightx > 0x4000) pad->button |= R_CBUTTONS; + if (righty < -0x4000) pad->button |= U_CBUTTONS; + if (righty > 0x4000) pad->button |= D_CBUTTONS; + + uint32_t magnitude_sq = (uint32_t)(leftx * leftx) + (uint32_t)(lefty * lefty); + uint32_t stickDeadzoneActual = configStickDeadzone * DEADZONE_STEP; + if (magnitude_sq > (uint32_t)(stickDeadzoneActual * stickDeadzoneActual)) { + pad->stick_x = leftx / 0x100; + int stick_y = -lefty / 0x100; + pad->stick_y = stick_y == 128 ? 127 : stick_y; + } + + magnitude_sq = (uint32_t)(rightx * rightx) + (uint32_t)(righty * righty); + stickDeadzoneActual = configStickDeadzone * DEADZONE_STEP; + if (magnitude_sq > (uint32_t)(stickDeadzoneActual * stickDeadzoneActual)) { + pad->ext_stick_x = rightx / 0x100; + int stick_y = -righty / 0x100; + pad->ext_stick_y = stick_y == 128 ? 127 : stick_y; + } +} + +static void controller_sdl_rumble_play(f32 strength, f32 length) { } + +static void controller_sdl_rumble_stop(void) { } + +static u32 controller_sdl_rawkey(void) { + if (last_joybutton != VK_INVALID) { + const u32 ret = last_joybutton; + last_joybutton = VK_INVALID; + return ret; + } + + for (int i = 0; i < MAX_MOUSEBUTTONS; ++i) { + if (last_mouse & SDL_BUTTON(i)) { + const u32 ret = VK_OFS_SDL_MOUSE + i; + last_mouse = 0; + return ret; + } + } + return VK_INVALID; +} + +static void controller_sdl_shutdown(void) { + if (SDL_WasInit(SDL_INIT_JOYSTICK)) { + if (sdl_joy) { + SDL_JoystickClose(sdl_joy); + sdl_joy = NULL; + } + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + } + + init_ok = false; +} + +struct ControllerAPI controller_sdl = { + VK_BASE_SDL_GAMEPAD, + controller_sdl_init, + controller_sdl_read, + controller_sdl_rawkey, + controller_sdl_rumble_play, + controller_sdl_rumble_stop, + controller_sdl_bind, + controller_sdl_shutdown +}; + +#endif // CAPI_SDL1 From 0414287c9a749d8efb48ec135656b67f53cbae5c Mon Sep 17 00:00:00 2001 From: Russell Patterson Date: Thu, 3 Sep 2020 21:18:09 -0400 Subject: [PATCH 8/8] Fix Yoshi dialog error that has bothered me for 24 years. --- text/us/dialogs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/us/dialogs.h b/text/us/dialogs.h index 34a18b20..fc847e13 100644 --- a/text/us/dialogs.h +++ b/text/us/dialogs.h @@ -1982,7 +1982,7 @@ Enjoy!!!" DEFINE_DIALOG(DIALOG_161, 1, 4, 30, 200, _("\ Mario!!!\n\ -It that really you???\n\ +Is that really you???\n\ It has been so long since\n\ our last adventure!\n\ They told me that I might\n\