Added threading? 😳

This commit is contained in:
2021-11-03 07:18:29 -07:00
parent 00ec323b60
commit 2f2f1395fa
6 changed files with 148 additions and 9 deletions

View File

@ -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);

45
src/engine/thread.c Normal file
View File

@ -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;
}

72
src/engine/thread.h Normal file
View File

@ -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);

View File

@ -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);

View File

@ -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"

View File

@ -25,7 +25,20 @@
// Windows Fixes
# define strtok_r strtok_s
# define sleep(n) _sleep(n)
#include <windows.h>
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 <unistd.h>
#include <pthread.h>
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