diff options
author | kdx <kikoodx@paranoici.org> | 2023-04-28 12:43:20 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-04-28 12:43:20 +0200 |
commit | 34bdf3c6c5fc1853c0e2b7c8cdfb2425b1013cd7 (patch) | |
tree | 7a4ccbf99e493bff47d8a84c2aa7b79da39f62b7 | |
parent | 0f2098fa984627b0f698ba58bf18d15c0fcdba39 (diff) | |
download | px-34bdf3c6c5fc1853c0e2b7c8cdfb2425b1013cd7.tar.gz |
update tzr
-rw-r--r-- | src/TZR.c | 94 | ||||
-rw-r--r-- | src/TZR.h | 46 |
2 files changed, 127 insertions, 13 deletions
@@ -1,3 +1,4 @@ +/* Licensing information can be found at the end of the file. */ #include <math.h> #include <SDL2/SDL_error.h> #include <SDL2/SDL_events.h> @@ -156,8 +157,13 @@ TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size) SDL_RWops *const rw = SDL_RWFromConstMem(data, size); if (rw == NULL) return sdl_error(-1); - SDL_Surface *const surf = IMG_Load_RW(rw, 0); - SDL_RWclose(rw); + SDL_Surface *surf = IMG_Load_RW(rw, 1); + if (surf == NULL) { + SDL_RWops *const rw = SDL_RWFromConstMem(data, size); + if (rw == NULL) + return sdl_error(-1); + surf = SDL_LoadBMP_RW(rw, 1); + } if (surf == NULL) return sdl_error(-1); SDL_Texture *const tex = @@ -257,6 +263,7 @@ TZR_DrawEnd(void) &dest) < 0) return sdl_error(-1); } + if (___tzr_config.target_fps > 0) { ___tzr_next_time += ___tzr_min_dt; const unsigned long cur_time = SDL_GetTicks64(); @@ -266,6 +273,7 @@ TZR_DrawEnd(void) SDL_Delay(___tzr_next_time - cur_time); } SDL_RenderPresent(___tzr_renderer); + ___tzr_tick += 1; return 0; } @@ -493,9 +501,11 @@ _TZR_Init(const TZR_Config *config) return _sdl_error(); if (IMG_Init(IMG_INIT_PNG * ___tzr_config.png_loading) < 0) return _sdl_error(); - if (___tzr_config.mixer && Mix_Init(MIX_INIT_FLAC) != MIX_INIT_FLAC) { + if (___tzr_config.mixer == TZR_MIXER_FLAC && + Mix_Init(MIX_INIT_FLAC) != MIX_INIT_FLAC) + { SDL_Log("%s", Mix_GetError()); - ___tzr_config.mixer = false; + ___tzr_config.mixer = TZR_MIXER_OFF; } char *const basepath = SDL_GetBasePath(); @@ -506,16 +516,23 @@ _TZR_Init(const TZR_Config *config) if (chdir_rv < 0) return perror("TZR_Init"), -1; - ___tzr_window = SDL_CreateWindow("TZR", SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - ___tzr_config.width, - ___tzr_config.height, - SDL_WINDOW_RESIZABLE); + ___tzr_window = + SDL_CreateWindow("TZR", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + ___tzr_config.width * ___tzr_config.scale, + ___tzr_config.height * ___tzr_config.scale, + SDL_WINDOW_RESIZABLE); if (___tzr_window == NULL) return _sdl_error(); +#ifdef __EMSCRIPTEN__ + ___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1, + SDL_RENDERER_SOFTWARE); +#else ___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1, SDL_RENDERER_ACCELERATED); +#endif if (___tzr_renderer == NULL) return _sdl_error(); @@ -710,6 +727,42 @@ TZR_LoadResourceTyped(TZR_ResourceType type, const char *path) strcpy(res->path, path); return id; } +/* sources/TZR_MainLoop.c */ + +#ifdef __EMSCRIPTEN__ + +static int (*_main_loop)(void *udata); +static void *_udata; +static void +_em_loop(void) +{ + TZR_CycleEvents(); + _main_loop(_udata); +} + +int +TZR_MainLoop(int (*main_loop)(void *udata), void *udata) +{ + _main_loop = main_loop; + _udata = udata; + emscripten_set_main_loop(_em_loop, 0, 1); + return 0; +} + +#else + +int +TZR_MainLoop(int (*main_loop)(void *udata), void *udata) +{ + while (!TZR_ShouldQuit()) { + TZR_CycleEvents(); + if (main_loop(udata)) + return 1; + } + return 0; +} + +#endif /* sources/TZR_PlaySound.c */ int @@ -884,4 +937,25 @@ TZR_ShouldQuit(void) return ___tzr_should_quit; } -/* commit hash: 6370f11b5a93a00045969fa4637db46d5e792dae */ +/* +** Copyright (c) 2023 kdx +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to +** deal in the Software without restriction, including without limitation the +** rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +** sell copies of the Software, and to permit persons to whom the Software is +** furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in +** all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +** IN THE SOFTWARE. +*/ +/* commit hash: 6044a3bac4433e9ce8d114bdbc180ca7b48b2344 */ @@ -1,3 +1,4 @@ +/* Licensing information can be found at the end of the file. */ #pragma once #include <limits.h> #include <SDL2/SDL_main.h> @@ -6,6 +7,9 @@ #include <SDL2/SDL_scancode.h> #include <stdbool.h> #include <stddef.h> +#ifdef __EMSCRIPTEN__ +#include <emscripten.h> +#endif /* headers/TZR_types.h */ @@ -29,6 +33,12 @@ enum TZR_KeyState { TZR_KEYSTATE_PRESS }; +enum TZR_MixerFlags { + TZR_MIXER_OFF, + TZR_MIXER_ON, + TZR_MIXER_FLAC +}; + typedef unsigned int TZR_Uint; typedef struct TZR_Config TZR_Config; typedef struct TZR_Point TZR_Point; @@ -50,10 +60,11 @@ struct TZR_Config { int _; int width; int height; + int scale; int target_fps; bool pixel_perfect; bool show_cursor; - bool mixer; + unsigned int mixer; bool png_loading; bool light_system; const char *title; @@ -260,6 +271,11 @@ int TZR_DrawEnd(void); int _TZR_DrawSetColor(const TZR_Color *color); /* Return -1 on error. */ +#ifdef TZR_PARANOID +#if __STDC_VERSION__ > 201710L +[[nodiscard]] +#endif +#endif int TZR_DrawSetColor8(uint8_t r, uint8_t g, uint8_t b, uint8_t a); /* Return -1 on error. */ @@ -342,10 +358,11 @@ bool TZR_IsKeyPressed(int scancode); #define TZR_Init(...) _TZR_Init(&(const TZR_Config){ \ .width=256, \ .height=224, \ + .scale=2, \ .target_fps=60, \ .pixel_perfect=true, \ .show_cursor=false, \ - .mixer=true, \ + .mixer=TZR_MIXER_ON, \ .png_loading=true, \ .light_system=false, \ .title="TZR", \ @@ -365,6 +382,29 @@ void TZR_Quit(void); /* Return non-zero value when a quit event has been received. */ int TZR_ShouldQuit(void); +int TZR_MainLoop(int (*main_loop)(void *udata), void *udata); + unsigned long TZR_GetTick(void); -/* commit hash: 6370f11b5a93a00045969fa4637db46d5e792dae */ +/* +** Copyright (c) 2023 kdx +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to +** deal in the Software without restriction, including without limitation the +** rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +** sell copies of the Software, and to permit persons to whom the Software is +** furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in +** all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +** IN THE SOFTWARE. +*/ +/* commit hash: 6044a3bac4433e9ce8d114bdbc180ca7b48b2344 */ |