Single thread + inputs

This commit is contained in:
Marc
2023-01-21 12:09:39 +01:00
parent 8a93d29021
commit 41792a17d3
6 changed files with 176 additions and 52 deletions
+38 -15
View File
@@ -11,7 +11,7 @@
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <pthread.h>
#include "input.h"
typedef struct RenderItem {
RenderObject_t object;
@@ -28,12 +28,17 @@ pthread_t thread;
pthread_mutex_t mutex;
SDL_Window* win;
bool running;
SDL_Renderer * renderer;
static int compartSort(const RenderItem_t * a, const RenderItem_t * b) {
return a->object.z - b->object.z;
}
//int (*compar)(const void [.size], const void [.size], void *)
static void * render_thread(void *) {
void renderer_main(void(*game_main)(void)) {
// returns zero on success else non-zero
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
@@ -56,10 +61,38 @@ static void * render_thread(void *) {
objectStorageSize = 1000;
objects = malloc(objectStorageSize);
pthread_mutex_unlock(&mutex);
input_init();
running = true;
while(running) {
SDL_RenderClear(renderer);
game_main();
SDL_Event event;
// Events management
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
// handling of close button
running = false;
printf("Quitting my job!\n");
break;
case SDL_KEYDOWN:
// keyboard API for key pressed
input_poll(event.key.keysym.scancode);
break;
}
}
//no need to make an extra render pass
if(!running) break;
while(true) {
//SDL_RenderClear(renderer);
for(int i = 0; i < objectCount; i++) {
RenderItem_t * object = &objects[i];
if(object->cache == NULL) {
@@ -82,16 +115,6 @@ static void * render_thread(void *) {
SDL_RenderPresent(renderer);
SDL_Delay(1000 / 400); //400fps or u a pussy
}
return NULL;
}
void renderer_main() {
pthread_mutex_init(&mutex, NULL);
pthread_mutex_lock(&mutex);
pthread_create(&thread, NULL, render_thread, NULL);
pthread_mutex_lock(&mutex);
pthread_mutex_destroy(&mutex);
}
RenderId_t renderer_renderObject(RenderObject_t object) {