From 7dd722a863dadbe3aa3dc7f4a0f708c567735486 Mon Sep 17 00:00:00 2001 From: benjamin ausensi Date: Mon, 25 May 2020 00:41:28 -0400 Subject: [PATCH] On tmx_img_load_func, add a void *data argument which allows the user to point to any memory needed by their loader function which defeats the need of making more globals for them and allows for cleaner code. Update documentation to reflect the changes. Add global pointer tmx_img_load_data which allows the user to supply any additional memory needed by their loader. Such example would be an SDL_Renderer pointer defined inside a scope. --- doc/src/override.rst | 2 +- doc/src/renderer-from-scratch.rst | 10 ++++--- examples/allegro/allegro.c | 4 +-- examples/raylib/raylib.c | 4 +-- examples/sdl/sdl.c | 49 +++++++++++++++---------------- src/tmx.c | 3 +- src/tmx.h | 7 +++-- src/tmx_utils.c | 2 +- 8 files changed, 43 insertions(+), 38 deletions(-) diff --git a/doc/src/override.rst b/doc/src/override.rst index 9fdba62..2b5721d 100644 --- a/doc/src/override.rst +++ b/doc/src/override.rst @@ -53,7 +53,7 @@ Image Autoload/Autofree Set (there is no default implementation) load and free functions to load/free images automatically as they are read in the map source. -.. c:var:: void* (*tmx_img_load_func) (const char *path) +.. c:var:: void* (*tmx_img_load_func) (const char *path, void *data) Load the resource (image) at the given path, return a pointer to void. The returned value is then stored in a :c:member:`tmx_image.resource_image`. diff --git a/doc/src/renderer-from-scratch.rst b/doc/src/renderer-from-scratch.rst index bd14338..08e5591 100644 --- a/doc/src/renderer-from-scratch.rst +++ b/doc/src/renderer-from-scratch.rst @@ -203,19 +203,21 @@ an easier way to do that without the hassle: :ref:`callback functions 2) { SDL_RenderDrawLine(ren, x+points[0][0], y+points[0][1], x+points[pointsc-1][0], y+points[pointsc-1][1]); } } -void draw_objects(tmx_object_group *objgr) { +void draw_objects(tmx_object_group *objgr, SDL_Renderer *ren) { SDL_Rect rect; - set_color(objgr->color); + set_color(objgr->color, ren); tmx_object *head = objgr->head; while (head) { if (head->visible) { @@ -44,10 +42,10 @@ void draw_objects(tmx_object_group *objgr) { SDL_RenderDrawRect(ren, &rect); } else if (head->obj_type == OT_POLYGON) { - draw_polygon(head->content.shape->points, head->x, head->y, head->content.shape->points_len); + draw_polygon(head->content.shape->points, head->x, head->y, head->content.shape->points_len, ren); } else if (head->obj_type == OT_POLYLINE) { - draw_polyline(head->content.shape->points, head->x, head->y, head->content.shape->points_len); + draw_polyline(head->content.shape->points, head->x, head->y, head->content.shape->points_len, ren); } else if (head->obj_type == OT_ELLIPSE) { /* FIXME: no function in SDL2 */ @@ -57,7 +55,7 @@ void draw_objects(tmx_object_group *objgr) { } } -void draw_tile(void *image, unsigned int sx, unsigned int sy, unsigned int sw, unsigned int sh, +void draw_tile(SDL_Renderer *ren, void *image, unsigned int sx, unsigned int sy, unsigned int sw, unsigned int sh, unsigned int dx, unsigned int dy, float opacity, unsigned int flags) { SDL_Rect src_rect, dest_rect; src_rect.x = sx; @@ -69,7 +67,7 @@ void draw_tile(void *image, unsigned int sx, unsigned int sy, unsigned int sw, u SDL_RenderCopy(ren, (SDL_Texture*)image, &src_rect, &dest_rect); } -void draw_layer(tmx_map *map, tmx_layer *layer) { +void draw_layer(tmx_map *map, tmx_layer *layer, SDL_Renderer *ren) { unsigned long i, j; unsigned int gid, x, y, w, h, flags; float op; @@ -94,13 +92,13 @@ void draw_layer(tmx_map *map, tmx_layer *layer) { image = ts->image->resource_image; } flags = (layer->content.gids[(i*map->width)+j]) & ~TMX_FLIP_BITS_REMOVAL; - draw_tile(image, x, y, w, h, j*ts->tile_width, i*ts->tile_height, op, flags); + draw_tile(ren, image, x, y, w, h, j*ts->tile_width, i*ts->tile_height, op, flags); } } } } -void draw_image_layer(tmx_image *image) { +void draw_image_layer(tmx_image *image, SDL_Renderer *ren) { SDL_Rect dim; dim.x = dim.y = 0; @@ -109,31 +107,31 @@ void draw_image_layer(tmx_image *image) { SDL_RenderCopy(ren, texture, NULL, &dim); } -void draw_all_layers(tmx_map *map, tmx_layer *layers) { +void draw_all_layers(tmx_map *map, tmx_layer *layers, SDL_Renderer *ren) { while (layers) { if (layers->visible) { if (layers->type == L_GROUP) { - draw_all_layers(map, layers->content.group_head); + draw_all_layers(map, layers->content.group_head, ren); } else if (layers->type == L_OBJGR) { - draw_objects(layers->content.objgr); + draw_objects(layers->content.objgr, ren); } else if (layers->type == L_IMAGE) { - draw_image_layer(layers->content.image); + draw_image_layer(layers->content.image, ren); } else if (layers->type == L_LAYER) { - draw_layer(map, layers); + draw_layer(map, layers, ren); } } layers = layers->next; } } -void render_map(tmx_map *map) { - set_color(map->backgroundcolor); +void render_map(tmx_map *map, SDL_Renderer *ren) { + set_color(map->backgroundcolor, ren); SDL_RenderClear(ren); - draw_all_layers(map, map->ly_head); + draw_all_layers(map, map->ly_head, ren); } Uint32 timer_func(Uint32 interval, void *param) { @@ -154,6 +152,7 @@ Uint32 timer_func(Uint32 interval, void *param) { int main(int argc, char **argv) { SDL_Window *win; + SDL_Renderer *ren; SDL_Event ev; SDL_TimerID timer_id; @@ -191,7 +190,7 @@ int main(int argc, char **argv) { if (ev.type == SDL_QUIT) break; - render_map(map); + render_map(map, ren); SDL_RenderPresent(ren); } diff --git a/src/tmx.c b/src/tmx.c index e7e1392..8f46176 100755 --- a/src/tmx.c +++ b/src/tmx.c @@ -10,8 +10,9 @@ void* (*tmx_alloc_func) (void *address, size_t len) = NULL; void (*tmx_free_func ) (void *address) = NULL; -void* (*tmx_img_load_func) (const char *p) = NULL; +void* (*tmx_img_load_func) (const char *p, void *data) = NULL; void (*tmx_img_free_func) (void *address) = NULL; +void* tmx_img_load_data = NULL; /* Public functions diff --git a/src/tmx.h b/src/tmx.h index 65c3b7f..56198e7 100755 --- a/src/tmx.h +++ b/src/tmx.h @@ -38,9 +38,12 @@ TMXEXPORT extern void* (*tmx_alloc_func) (void *address, size_t len); /* realloc TMXEXPORT extern void (*tmx_free_func ) (void *address); /* free */ /* load/free tmx_image->resource_image, you should set this if you want - the library to load/free images */ -TMXEXPORT extern void* (*tmx_img_load_func) (const char *path); + the library to load/free images. Optionally, you can point tmx_img_load_data + to any memory needed by your loader function, and it will be accessed as + the data argument */ +TMXEXPORT extern void* (*tmx_img_load_func) (const char *path, void *data); TMXEXPORT extern void (*tmx_img_free_func) (void *address); +TMXEXPORT extern void* tmx_img_load_data; /* Data Structures diff --git a/src/tmx_utils.c b/src/tmx_utils.c index 2d99ab3..0f799b4 100755 --- a/src/tmx_utils.c +++ b/src/tmx_utils.c @@ -609,7 +609,7 @@ void* load_image(void **ptr, const char *base_path, const char *rel_path) { if (tmx_img_load_func) { ap_img = mk_absolute_path(base_path, rel_path); if (!ap_img) return 0; - *ptr = tmx_img_load_func(ap_img); + *ptr = tmx_img_load_func(ap_img, tmx_img_load_data); tmx_free_func(ap_img); return(*ptr); }