diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-02-08 15:39:56 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-03-30 08:20:21 +0200 |
| commit | 98f0d3e026f978d556cc91cfaa815d92a0b182c8 (patch) | |
| tree | bd9f39354da8fcbed0453bf45152c39e803abbf6 /src/gfx | |
| parent | 56286a0a962119ddebf26df58d4a7c5d2f6a3fc7 (diff) | |
| download | jancity-98f0d3e026f978d556cc91cfaa815d92a0b182c8.tar.gz | |
Replace x_get functions with macros
The PS1 port relies on a heap for primitives since the GPU renders the
scene asynchronously. However, SDL-based platforms render primitives
synchronously, so structures can be allocated on the stack instead.
Diffstat (limited to 'src/gfx')
| -rw-r--r-- | src/gfx/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/gfx/inc/gfx.h | 9 | ||||
| -rw-r--r-- | src/gfx/privinc/gfx_private.h | 4 | ||||
| -rw-r--r-- | src/gfx/ps1/src/heap.c | 41 | ||||
| -rw-r--r-- | src/gfx/ps1/src/sort.c | 1 | ||||
| -rw-r--r-- | src/gfx/sdl-1.2/inc/gfx/port.h | 3 | ||||
| -rw-r--r-- | src/gfx/sdl-1.2/src/heap.c | 11 | ||||
| -rw-r--r-- | src/gfx/sdl-1.2/src/sort.c | 22 | ||||
| -rw-r--r-- | src/gfx/src/heap.c | 56 |
9 files changed, 45 insertions, 105 deletions
diff --git a/src/gfx/CMakeLists.txt b/src/gfx/CMakeLists.txt index c449be5..6685608 100644 --- a/src/gfx/CMakeLists.txt +++ b/src/gfx/CMakeLists.txt @@ -1,4 +1,3 @@ -set(src "src/heap.c") set(inc "inc") set(privinc "privinc") set(deps util) @@ -21,10 +20,8 @@ elseif(SDL1_2_BUILD) set(privinc ${privinc} "sdl-1.2/privinc") set(src ${src} "sdl-1.2/src/env.c" - "sdl-1.2/src/heap.c" "sdl-1.2/src/line.c" "sdl-1.2/src/rect.c" - "sdl-1.2/src/sort.c" "sdl-1.2/src/sprite.c" "sdl-1.2/src/quad.c") set(deps ${deps} SDL) diff --git a/src/gfx/inc/gfx.h b/src/gfx/inc/gfx.h index 0c30911..7ea6df6 100644 --- a/src/gfx/inc/gfx.h +++ b/src/gfx/inc/gfx.h @@ -19,15 +19,16 @@ void quad_sort(struct quad *q); void rect_sort(struct rect *r); void stp_4line_sort(struct stp_4line *l); int sprite_from_fp(struct sprite *s, FILE *f); -struct sprite *sprite_get(void); -struct quad *quad_get(void); -struct rect *rect_get(bool semitrans); -struct stp_4line *stp_4line_get(void); int quad_from_sprite(const struct sprite *s, struct quad *q); bool gfx_inside_drawenv(short x, short y, short w, short h); void gfx_deinit(void); void sprite_free(struct sprite *src); +#define sprite_get_or_ret(x, ret) common_get_or_ret(sprite, x, ret) +#define stp_4line_get_or_ret(x, ret) common_get_or_ret(stp_4line, x, ret) +#define rect_get_or_ret(x, ret) common_get_or_ret(rect, x, ret) +#define quad_get_or_ret(x, ret) common_get_or_ret(quad, x, ret) + extern int screen_w, screen_h; #ifdef __cplusplus diff --git a/src/gfx/privinc/gfx_private.h b/src/gfx/privinc/gfx_private.h index 0553362..972efd0 100644 --- a/src/gfx/privinc/gfx_private.h +++ b/src/gfx/privinc/gfx_private.h @@ -11,10 +11,6 @@ extern "C" void rect_init(struct rect *r); void semitrans_rect_init(struct rect *r); -void stp_4line_init(struct stp_4line *l); -void *gfx_port_heap(size_t *n); -void gfx_reset_heap(void); - #ifdef __cplusplus } #endif diff --git a/src/gfx/ps1/src/heap.c b/src/gfx/ps1/src/heap.c index b67ee3e..ab45d94 100644 --- a/src/gfx/ps1/src/heap.c +++ b/src/gfx/ps1/src/heap.c @@ -3,17 +3,52 @@ #include <stddef.h> static unsigned int sel; +static size_t heap_i; void gfx_swapheap(void) { sel ^= 1; + heap_i = 0; } -void *gfx_port_heap(size_t *const n) +static void *get_element(const size_t sz) { enum {HEAP_SZ = 5120, N_HEAPS = 2}; static char heaps[N_HEAPS][HEAP_SZ]; + const size_t new_sz = heap_i + sz; + void *ret = NULL; - *n = sizeof *heaps / sizeof **heaps; - return heaps[sel]; + if (new_sz < sizeof *heaps / sizeof **heaps) + { + ret = &heaps[sel][heap_i]; + heap_i += sz; + } + else + { + fprintf(stderr, "%s: exceeded heap size (%zu/%zu)\n", + __func__, new_sz, sizeof *heaps / sizeof **heaps); + return NULL; + } + + return ret; +} + +struct sprite *sprite_get(void) +{ + return get_element(sizeof (struct sprite)); +} + +struct quad *quad_get(void) +{ + return get_element(sizeof (struct quad)); +} + +struct rect *rect_get(void) +{ + return get_element(sizeof (struct rect)); +} + +struct stp_4line *stp_4line_get(void) +{ + return get_element(sizeof (struct stp_4line)); } diff --git a/src/gfx/ps1/src/sort.c b/src/gfx/ps1/src/sort.c index 5cc14aa..03449fc 100644 --- a/src/gfx/ps1/src/sort.c +++ b/src/gfx/ps1/src/sort.c @@ -55,7 +55,6 @@ void gfx_draw(void) D2_BCR = 0; D2_CHCR = (1 << 0xa) | 1 | (1 << 0x18); first = NULL; - gfx_reset_heap(); } void gfx_sync(void) diff --git a/src/gfx/sdl-1.2/inc/gfx/port.h b/src/gfx/sdl-1.2/inc/gfx/port.h index ec1093e..3dbc23b 100644 --- a/src/gfx/sdl-1.2/inc/gfx/port.h +++ b/src/gfx/sdl-1.2/inc/gfx/port.h @@ -43,7 +43,8 @@ struct stp_4line } vertices[4]; }; -SDL_Surface *gfx_port_screen(void); +#define common_get_or_ret(t, x, ret) \ + struct t x##__, *const x = &x##__ #ifdef __cplusplus } diff --git a/src/gfx/sdl-1.2/src/heap.c b/src/gfx/sdl-1.2/src/heap.c deleted file mode 100644 index b84760a..0000000 --- a/src/gfx/sdl-1.2/src/heap.c +++ /dev/null @@ -1,11 +0,0 @@ -#include <gfx.h> -#include <gfx_private.h> -#include <stddef.h> - -void *gfx_port_heap(size_t *const n) -{ - static char heap[5120]; - - *n = sizeof heap / sizeof *heap; - return heap; -} diff --git a/src/gfx/sdl-1.2/src/sort.c b/src/gfx/sdl-1.2/src/sort.c deleted file mode 100644 index 2cc4e75..0000000 --- a/src/gfx/sdl-1.2/src/sort.c +++ /dev/null @@ -1,22 +0,0 @@ -#include <gfx.h> -#include <gfx_private.h> -#include <gfx/port.h> -#include <SDL/SDL.h> -#include <stdio.h> - -void gfx_draw(void) -{ - enum {FPS = 50, REFRESH_MS = 1000 / FPS}; - static Uint32 prev; - const Uint32 cur = SDL_GetTicks(); - - if (cur - prev < REFRESH_MS) - SDL_Delay(REFRESH_MS - (cur - prev)); - - prev = SDL_GetTicks(); - - if (SDL_Flip(gfx_port_screen())) - fprintf(stderr, "SDL_Flip: %s\n", SDL_GetError()); - - gfx_reset_heap(); -} diff --git a/src/gfx/src/heap.c b/src/gfx/src/heap.c deleted file mode 100644 index 40a856c..0000000 --- a/src/gfx/src/heap.c +++ /dev/null @@ -1,56 +0,0 @@ -#include <gfx.h> -#include <gfx_private.h> -#include <stddef.h> - -static size_t heap_i; - -void gfx_reset_heap(void) -{ - heap_i = 0; -} - -static void *get_element(const size_t sz) -{ - size_t n; - char *ret = gfx_port_heap(&n); - - if (ret && heap_i + sz < n) - { - ret += heap_i; - heap_i += sz; - } - else - return NULL; - - return ret; -} - -struct sprite *sprite_get(void) -{ - return get_element(sizeof (struct sprite)); -} - -struct quad *quad_get(void) -{ - return get_element(sizeof (struct quad)); -} - -struct rect *rect_get(const bool semitrans) -{ - struct rect *const r = get_element(sizeof (struct rect)); - - if (r) - semitrans ? semitrans_rect_init(r) : rect_init(r); - - return r; -} - -struct stp_4line *stp_4line_get(void) -{ - struct stp_4line *const l = get_element(sizeof (struct stp_4line)); - - if (l) - stp_4line_init(l); - - return l; -} |
