diff options
author | kdx <kikoodx@paranoici.org> | 2023-07-10 12:51:33 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-07-10 12:51:42 +0200 |
commit | 993420e792c273a80eb2c9df81f6f1c075597543 (patch) | |
tree | c7cd2a78e4ad7494856a272d13c3b30653c14ac0 | |
parent | 74bfb703a6c58285aeb3f9480ca8bbe22c7186ba (diff) | |
download | tzr-993420e792c273a80eb2c9df81f6f1c075597543.tar.gz |
reusable reserve module & joystick begins
-rw-r--r-- | headers/TZR_globals.h | 4 | ||||
-rw-r--r-- | sources/TZR_Init.c | 2 | ||||
-rw-r--r-- | sources/TZR_LoadResourceFromMemory.c | 35 | ||||
-rw-r--r-- | sources/TZR_PollEvent.c | 17 | ||||
-rw-r--r-- | sources/TZR_Quit.c | 7 | ||||
-rw-r--r-- | sources/globals.c | 4 | ||||
-rw-r--r-- | sources/reserve.h | 26 |
7 files changed, 70 insertions, 25 deletions
diff --git a/headers/TZR_globals.h b/headers/TZR_globals.h index 2406ff5..db03ce8 100644 --- a/headers/TZR_globals.h +++ b/headers/TZR_globals.h @@ -2,6 +2,7 @@ #include "TZR_types.h" #include <stddef.h> #include <SDL2/SDL_scancode.h> +#include <SDL2/SDL_joystick.h> extern TZR_Config ___tzr_config; extern TZR_Color ___tzr_color; @@ -23,3 +24,6 @@ extern float ___tzr_scale; extern int ___tzr_off_x; extern int ___tzr_off_y; extern Mix_Music *___tzr_music; +extern SDL_Joystick **___tzr_joysticks; +extern size_t ___tzr_joysticks_capacity; +extern size_t ___tzr_joysticks_size; diff --git a/sources/TZR_Init.c b/sources/TZR_Init.c index f22fccb..a508892 100644 --- a/sources/TZR_Init.c +++ b/sources/TZR_Init.c @@ -19,7 +19,7 @@ _TZR_Init(const TZR_Config *config) { memcpy(&___tzr_config, config, sizeof(TZR_Config)); - if (SDL_Init(SDL_INIT_VIDEO) < 0) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) return _sdl_error(); if (___tzr_config.mixer == TZR_MIXER_FLAC && Mix_Init(MIX_INIT_FLAC) != MIX_INIT_FLAC) diff --git a/sources/TZR_LoadResourceFromMemory.c b/sources/TZR_LoadResourceFromMemory.c index 1df0824..2ac0950 100644 --- a/sources/TZR_LoadResourceFromMemory.c +++ b/sources/TZR_LoadResourceFromMemory.c @@ -1,43 +1,30 @@ #include "TZR_resource.h" #include "TZR_globals.h" +#include "reserve.h" #include <SDL2/SDL_render.h> #include <SDL2/SDL_rwops.h> #include <stdio.h> #include <stdlib.h> -static int -reserve_ressources(size_t size) -{ - if (size <= ___tzr_resources_capacity) - return 0; - size_t target = 16; - while (target < size * sizeof(TZR_Resource)) - target *= 2; - TZR_Resource *new_resources; - if (___tzr_resources == NULL) - new_resources = malloc(target); - else - new_resources = realloc(___tzr_resources, target); - if (new_resources == NULL) { - perror("TZR_LoadResourceFromMemory:reserve_ressources"); - return 1; - } - ___tzr_resources = new_resources; - return 0; -} - TZR_Uint TZR_LoadResourceFromMemory(TZR_ResourceType type, const void *data, int size) { - if (reserve_ressources(___tzr_resources_size + 1)) { + void *vec = ___tzr_resources; + if (reserve(&vec, + ___tzr_resources_size + 1, + &___tzr_resources_capacity, + sizeof(TZR_Resource))) + { fprintf(stderr, "failed to reserve for new ressource\n"); return 0; } - TZR_Resource *const res = &___tzr_resources[___tzr_resources_size]; + ___tzr_resources = vec; + ___tzr_resources_size += 1; + + TZR_Resource *const res = &___tzr_resources[___tzr_resources_size - 1]; res->type = type; res->path = NULL; if (TZR_DirectResourceLoad(res, data, size)) return 0; - ___tzr_resources_size += 1; return ___tzr_resources_size; } diff --git a/sources/TZR_PollEvent.c b/sources/TZR_PollEvent.c index 7b3b94e..d89f7d8 100644 --- a/sources/TZR_PollEvent.c +++ b/sources/TZR_PollEvent.c @@ -1,5 +1,6 @@ #include "TZR_events.h" #include "TZR_globals.h" +#include "sdl_error.h" #include <SDL2/SDL_events.h> int @@ -41,6 +42,22 @@ TZR_PollEvent(TZR_Event *e) ___tzr_mouse_x = se.motion.x; ___tzr_mouse_y = se.motion.y; break; + case SDL_JOYDEVICEADDED: { + SDL_Joystick *const jstick = SDL_JoystickOpen(se.jdevice.which); + if (jstick == NULL) { + sdl_error(0); + break; + } + break; + } + case SDL_JOYDEVICEREMOVED: + break; + case SDL_JOYAXISMOTION: + break; + case SDL_JOYBUTTONDOWN: + break; + case SDL_JOYBUTTONUP: + break; default: break; } diff --git a/sources/TZR_Quit.c b/sources/TZR_Quit.c index 13b29db..4998629 100644 --- a/sources/TZR_Quit.c +++ b/sources/TZR_Quit.c @@ -13,6 +13,13 @@ TZR_Quit(void) ___tzr_resources = NULL; ___tzr_resources_size = 0; ___tzr_resources_capacity = 0; + for (size_t i = 0; i < ___tzr_joysticks_size; i++) + SDL_JoystickClose(___tzr_joysticks[i]); + if (___tzr_joysticks != NULL) + free(___tzr_joysticks); + ___tzr_joysticks = NULL; + ___tzr_joysticks_size = 0; + ___tzr_joysticks_capacity = 0; if (___tzr_target != NULL) { SDL_DestroyTexture(___tzr_target); ___tzr_target = NULL; diff --git a/sources/globals.c b/sources/globals.c index 0833d28..92d1348 100644 --- a/sources/globals.c +++ b/sources/globals.c @@ -2,6 +2,7 @@ #include <stddef.h> #include <SDL2/SDL_render.h> #include <SDL2/SDL_scancode.h> +#include <SDL2/SDL_joystick.h> TZR_Config ___tzr_config = {0}; TZR_Color ___tzr_color = {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; @@ -23,3 +24,6 @@ float ___tzr_scale = 1.0; int ___tzr_off_x = 1.0; int ___tzr_off_y = 1.0; Mix_Music *___tzr_music = NULL; +SDL_Joystick **___tzr_joysticks = NULL; +size_t ___tzr_joysticks_capacity = 0; +size_t ___tzr_joysticks_size = 0; diff --git a/sources/reserve.h b/sources/reserve.h new file mode 100644 index 0000000..74bad50 --- /dev/null +++ b/sources/reserve.h @@ -0,0 +1,26 @@ +#pragma once +#include <stdio.h> +#include <stdlib.h> + +static int +reserve(void **vec, size_t size, size_t *capacity, size_t elem) +{ + if (size <= *capacity) + return 0; + size_t target = 16; + while (target < size * elem) + target *= 2; + void *new_resources; + if (*vec == NULL) + new_resources = malloc(target); + else + new_resources = realloc(*vec, target); + if (new_resources == NULL) { + perror("TZR_LoadResourceFromMemory:reserve_ressources"); + return 1; + } + *vec = new_resources; + *capacity = target / elem; + return 0; +} + |