From 2f2f1395fa647e2aa5d17913375dff210a13682a Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 3 Nov 2021 07:18:29 -0700 Subject: [PATCH] =?UTF-8?q?Added=20threading=3F=20=F0=9F=98=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/display/texture.c | 7 ----- src/engine/thread.c | 45 +++++++++++++++++++++++++++ src/engine/thread.h | 72 +++++++++++++++++++++++++++++++++++++++++++ src/game/poker/game.c | 15 +++++++++ src/game/poker/game.h | 5 +-- src/libs.h | 13 ++++++++ 6 files changed, 148 insertions(+), 9 deletions(-) create mode 100644 src/engine/thread.c create mode 100644 src/engine/thread.h diff --git a/src/display/texture.c b/src/display/texture.c index 2b41a3f2..265b8350 100644 --- a/src/display/texture.c +++ b/src/display/texture.c @@ -25,13 +25,6 @@ void textureInit(texture_t *texture, int32_t width, int32_t height, glBindTexture(GL_TEXTURE_2D, texture->id); // Setup our preferred texture params - // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); diff --git a/src/engine/thread.c b/src/engine/thread.c new file mode 100644 index 00000000..d9fc41c6 --- /dev/null +++ b/src/engine/thread.c @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "thread.h" + +void threadInit(thread_t *thread, threadfunction_t *call) { + thread->call = call; + thread->user = NULL; + thread->state = 0x00; +} + +void threadStart(thread_t *thread) { + thread->state |= THREAD_FLAG_QUEUED; + sysThreadCreate(&_threadWrappedCallback, thread->pt, thread); +} + +void threadJoin(thread_t *thread) { + sysThreadJoin(thread->pt); +} + +void threadCancel(thread_t *thread) { + sysThreadCancel(thread->pt, 1); +} + +void threadSleep(float time) { + sleep((time * 1000.0f)); +} + +int32_t _threadWrappedCallback(thread_t *thread) { + int32_t response; + + flagOff(thread->state, THREAD_FLAG_QUEUED); + thread->state |= THREAD_FLAG_RUNNING; + + response = thread->call(thread); + + thread->state |= THREAD_FLAG_HAS_RUN; + flagOff(thread->state, THREAD_FLAG_RUNNING); + + return response; +} \ No newline at end of file diff --git a/src/engine/thread.h b/src/engine/thread.h new file mode 100644 index 00000000..f5f6ac8a --- /dev/null +++ b/src/engine/thread.h @@ -0,0 +1,72 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../libs.h" +#include "../util/flags.h" + +typedef struct _thread_t thread_t; + +#define THREAD_FLAG_RUNNING flagDefine(0) +#define THREAD_FLAG_QUEUED flagDefine(1) +#define THREAD_FLAG_HAS_RUN flagDefine(2) + +/** + * Thread function itself. + * + * @param thread Thread that was called. + * @return The exit code, try to ensure only 0 is possible. + */ +typedef int32_t threadfunction_t(thread_t *thread); + +typedef struct _thread_t { + threadhandle_t pt; + void *user; + uint8_t state; + threadfunction_t *call; +} thread_t; + +/** + * Initialize a thread, prepare it to be started. You need to manually start it + * yourself. + * + * @param thread Thread pointer to maintain. + * @param call User callback to fire when the thread is started. + */ +void threadInit(thread_t *thread, threadfunction_t *call); + +/** + * Start a previously prepared thread. + * + * @param thread Thread to start. + */ +void threadStart(thread_t *thread); + +/** + * Wait for the given thread to finish execution. + * + * @param thread Thread to join. + */ +void threadJoin(thread_t *thread); + +/** + * Cancel a running thread, avoid using where possible incase of any undefined + * behaviour. + * + * @param thread Thread to cancel. + */ +void threadCancel(thread_t *thread); + +/** + * Sleep the current thread. + * + * @param time Time in seconds to sleep for. + */ +void threadSleep(float time); + +/** Managing function for thread to help update the state */ +int32_t _threadWrappedCallback(thread_t *thread); \ No newline at end of file diff --git a/src/game/poker/game.c b/src/game/poker/game.c index 5a5fa963..9a672d77 100644 --- a/src/game/poker/game.c +++ b/src/game/poker/game.c @@ -7,7 +7,22 @@ #include "game.h" +thread_t thread; + +int32_t threadTest(thread_t *thread) { + printf("Thread?\n"); + threadSleep(2); + printf("THREAD2!?!?\n"); + return 0; +} + bool pokerGameInit(pokergame_t *game) { + threadInit(&thread, &threadTest); + threadStart(&thread); + threadJoin(&thread); + printf("Done?\n"); + + // Load the Assets. pokerGameAssetsInit(&game->assets); diff --git a/src/game/poker/game.h b/src/game/poker/game.h index 123e5aa9..df0e1c79 100644 --- a/src/game/poker/game.h +++ b/src/game/poker/game.h @@ -6,13 +6,14 @@ */ #pragma once -#include "pokergame.h" #include "../../libs.h" -#include "pokergameassets.h" #include "../../poker/poker.h" #include "../../vn/conversation/talk.h" #include "../../vn/vnscene.h" #include "../../util/array.h" +#include "../../engine/thread.h" +#include "pokergame.h" +#include "pokergameassets.h" #include "pokerui.h" #include "pokerworld.h" #include "pokergameaction.h" diff --git a/src/libs.h b/src/libs.h index 34dae2f4..d90d3def 100644 --- a/src/libs.h +++ b/src/libs.h @@ -25,7 +25,20 @@ // Windows Fixes # define strtok_r strtok_s # define sleep(n) _sleep(n) + #include + + + typedef HANDLE threadhandle_t; + #define sysThreadCreate(mthd,otp,usr) otp=CreateThread(NULL,0,mthd,usr,0,NULL) + #define sysThreadJoin(tp) WaitForSingleObject(tp, INFINITE) + #define sysThreadCancel(tp,ec) TerminateThread(tp,ec) #else // Unix Fixes #include + #include + + typedef pthread_t threadhandle_t; + #define sysThreadCreate(mthd,otp,usr) pthread_create(&otp,NULL,mthd,usr) + #define sysThreadJoin(tp) pthread_join(tp, NULL) + #define sysThreadCancel(tp,ec) pthread_cancel(tp) #endif \ No newline at end of file