From aa345a5c5f2c97b3b44bc2a24f4161c737691885 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 2 Aug 2019 22:15:03 +1000 Subject: [PATCH] don't pointlessly use Random_Range with 0 min, use Random_Next instead --- src/Audio.c | 6 +++--- src/Http.c | 3 +++ src/Particle.c | 4 ++-- src/Platform.c | 2 +- src/Widgets.c | 2 +- src/Window.c | 27 +++++++++++++++++++++------ 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Audio.c b/src/Audio.c index 4323e6fbe..c8dd76843 100644 --- a/src/Audio.c +++ b/src/Audio.c @@ -569,7 +569,7 @@ static struct Sound* Soundboard_PickRandom(struct Soundboard* board, uint8_t typ group = Soundboard_Find(board, &name); if (!group) return NULL; - idx = Random_Range(&board->Rnd, 0, group->Count); + idx = Random_Next(&board->Rnd, group->Count); return &group->Sounds[idx]; } @@ -840,7 +840,7 @@ static void Music_RunLoop(void) { Audio_Open(&music_out, AUDIO_MAX_BUFFERS); while (!music_pendingStop && count) { - idx = Random_Range(&rnd, 0, count); + idx = Random_Next(&rnd, count); file = StringsBuffer_UNSAFE_Get(&files, musicFiles[idx]); String_InitArray(path, pathBuffer); @@ -860,7 +860,7 @@ static void Music_RunLoop(void) { if (res) { Logger_Warn2(res, "closing", &path); break; } if (music_pendingStop) break; - delay = 1000 * 120 + Random_Range(&rnd, 0, 1000 * 300); + delay = 120 * 1000 + Random_Next(&rnd, 300 * 1000); Waitable_WaitFor(music_waitable, delay); } diff --git a/src/Http.c b/src/Http.c index 7438a68cc..8eba70f22 100644 --- a/src/Http.c +++ b/src/Http.c @@ -977,6 +977,9 @@ void Http_UrlEncodeUtf8(String* dst, const String* src) { *-----------------------------------------------------Http component------------------------------------------------------* *#########################################################################################################################*/ static void Http_Init(void) { +#ifdef CC_BUILD_ANDROID + if (workerThread) return; +#endif ScheduledTask_Add(30, Http_CleanCacheTask); RequestList_Init(&pendingReqs); RequestList_Init(&processedReqs); diff --git a/src/Particle.c b/src/Particle.c index 0ca458ace..0786cdb8c 100644 --- a/src/Particle.c +++ b/src/Particle.c @@ -419,7 +419,7 @@ void Particles_BreakBlockEffect(IVec3 coords, BlockID old, BlockID now) { p->rec = rec; p->texLoc = loc; p->block = old; - type = Random_Range(&rnd, 0, 30); + type = Random_Next(&rnd, 30); p->base.size = (uint8_t)(type >= 28 ? 12 : (type >= 25 ? 10 : 8)); } } @@ -447,7 +447,7 @@ void Particles_RainSnowEffect(Vec3 pos) { Vec3_Add(&pos, &origin, &offset); Particle_Reset(&p->base, pos, velocity, 40.0f); - type = Random_Range(&rnd, 0, 30); + type = Random_Next(&rnd, 30); p->base.size = (uint8_t)(type >= 28 ? 2 : (type >= 25 ? 4 : 3)); } } diff --git a/src/Platform.c b/src/Platform.c index 0b663bdf3..d912acd72 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -1126,7 +1126,7 @@ static void Font_DirCallback(const String* path, void* obj) { /* Completely skip windows .FON files */ if (String_CaselessEnds(path, &fonExt)) return; - /* If font is already known good, skip it */ + /* If font is already known, skip it */ for (i = 0; i < font_list.entries.count; i++) { entry = StringsBuffer_UNSAFE_Get(&font_list.entries, i); String_UNSAFE_Separate(&entry, font_list.separator, &name, &value); diff --git a/src/Widgets.c b/src/Widgets.c index 878f33f96..222a1590f 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -2274,7 +2274,7 @@ static int TextGroupWidget_NextUrl(char* chars, int charsLen, int i) { return charsLen; } -static int TextGroupWidget_UrlEnd(char* chars, int charsLen, int32_t* begs, int begsLen, int i) { +static int TextGroupWidget_UrlEnd(char* chars, int charsLen, int* begs, int begsLen, int i) { int start = i, j; int next, left; bool isBeg; diff --git a/src/Window.c b/src/Window.c index 0426c7920..a29b19bf1 100644 --- a/src/Window.c +++ b/src/Window.c @@ -2772,7 +2772,7 @@ static void Window_RefreshBounds(void) { Event_RaiseVoid(&WindowEvents.Resized); } -static Key Window_MapKey(int32_t code) { +static Key Window_MapKey(int code) { if (code >= AKEYCODE_0 && code <= AKEYCODE_9) return (code - AKEYCODE_0) + '0'; if (code >= AKEYCODE_A && code <= AKEYCODE_Z) return (code - AKEYCODE_A) + 'A'; if (code >= AKEYCODE_F1 && code <= AKEYCODE_F12) return (code - AKEYCODE_F1) + KEY_F1; @@ -3056,7 +3056,7 @@ void Window_DrawFramebuffer(Rect2D r) { uint32_t* src; uint32_t* dst; ARect b; - int32_t y, res, size; + int y, res, size; Platform_LogConst("DRAW_RAW"); /* window not created yet */ @@ -3070,9 +3070,9 @@ void Window_DrawFramebuffer(Rect2D r) { if (res) Logger_Abort2(res, "Locking window pixels"); Platform_Log4("ADJUS: %i,%i - %i,%i", &b.left, &b.top, &b.right, &b.bottom); - int32_t width = ANativeWindow_getWidth(win_handle); - int32_t height = ANativeWindow_getHeight(win_handle); - int32_t format = ANativeWindow_getFormat(win_handle); + int width = ANativeWindow_getWidth(win_handle); + int height = ANativeWindow_getHeight(win_handle); + int format = ANativeWindow_getFormat(win_handle); Platform_Log3("WIN SIZE: %i,%i %i", &width, &height, &format); Platform_Log4("BUF SIZE: %i,%i %i/%i", &buffer.width, &buffer.height, &buffer.format, &buffer.stride); @@ -3562,10 +3562,13 @@ static EGLint ctx_numConfig; static void GLContext_InitSurface(void) { if (!win_handle) return; /* window not created or lost */ ctx_surface = eglCreateWindowSurface(ctx_display, ctx_config, win_handle, NULL); + + if (!ctx_surface) return; eglMakeCurrent(ctx_display, ctx_surface, ctx_surface, ctx_context); } static void GLContext_FreeSurface(void) { + if (!ctx_surface) return; eglMakeCurrent(ctx_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); eglDestroySurface(ctx_display, ctx_surface); ctx_surface = NULL; @@ -3602,6 +3605,7 @@ void GLContext_Update(void) { } bool GLContext_TryRestore(void) { + GLContext_FreeSurface(); GLContext_InitSurface(); return ctx_surface != NULL; } @@ -3617,8 +3621,19 @@ void* GLContext_GetAddress(const char* function) { } bool GLContext_SwapBuffers(void) { + EGLint err; if (!ctx_surface) return false; - eglSwapBuffers(ctx_display, ctx_surface); + if (eglSwapBuffers(ctx_display, ctx_surface)) return true; + + err = eglGetError(); + /* We get EGL_BAD_SURFACE if the window surface is destroyed on android */ + /* TODO: Maybe detect from the SurfaceDestroyed event handler instead */ + if (err == EGL_BAD_SURFACE) { + GLContext_FreeSurface(); return false; + } + + /* TODO: figure out how to handle other errors */ + Logger_Abort2(err, "Failed to swap buffers"); return true; }