Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On tmx_img_load_func, add a void *data argument which allows the user… #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/src/override.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
10 changes: 6 additions & 4 deletions doc/src/renderer-from-scratch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,29 @@ an easier way to do that without the hassle: :ref:`callback functions <image-aut
| **libTMX** can use two callback functions to delegate the image loading and image freeing to your library/engine.
| One callback to load: :c:data:`tmx_img_load_func`.
| One callback to free: :c:data:`tmx_img_free_func`.
| Optional pointer to any data needed by your load function: :c:data:`tmx_img_load_data`.
| These callbacks **must be set BEFORE you call any load function**.

.. tabs::

.. code-tab:: c SDL 2

void* SDL_tex_loader(const char *path) {
return IMG_LoadTexture(ren, path);
void* SDL_tex_loader(const char *path, void *data) {
return IMG_LoadTexture((SDL_Renderer*)data, path);
}

/* Set the callback globs in the main function */
tmx_img_load_func = SDL_tex_loader;
tmx_img_free_func = (void (*)(void*))SDL_DestroyTexture;
tmx_img_load_data = ren;

tmx_map *map = tmx_load(argv[1]);
/* ... */
tmx_map_free(map);

.. code-tab:: c Allegro 5

void* Allegro5_tex_loader(const char *path) {
void* Allegro5_tex_loader(const char *path, void *data) {
ALLEGRO_BITMAP *res = NULL;
ALLEGRO_PATH *alpath = NULL;

Expand All @@ -247,7 +249,7 @@ an easier way to do that without the hassle: :ref:`callback functions <image-aut

.. code-tab:: c raylib

void* raylib_tex_loader(const char *path) {
void* raylib_tex_loader(const char *path, void *data) {
Texture2D *returnValue = malloc(sizeof(Texture2D));
*returnValue = LoadTexture(path);
return returnValue;
Expand Down
4 changes: 2 additions & 2 deletions examples/allegro/allegro.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define DISPLAY_H 600
#define DISPLAY_W 800

void* Allegro5_tex_loader(const char *path) {
void* Allegro5_tex_loader(const char *path, void *data) {
ALLEGRO_BITMAP *res = NULL;
ALLEGRO_PATH *alpath = NULL;

Expand Down Expand Up @@ -191,4 +191,4 @@ int main(int argc, char **argv) {
al_destroy_display(display);

return 0;
}
}
4 changes: 2 additions & 2 deletions examples/raylib/raylib.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define DISPLAY_H 600
#define DISPLAY_W 800

void *raylib_tex_loader(const char *path) {
void *raylib_tex_loader(const char *path, void *data) {
Texture2D *returnValue = malloc(sizeof(Texture2D));
*returnValue = LoadTexture(path);
return returnValue;
Expand Down Expand Up @@ -164,4 +164,4 @@ int main(int argc, char **argv) {
CloseWindow();

return 0;
}
}
49 changes: 24 additions & 25 deletions examples/sdl/sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,32 @@
#define DISPLAY_H 600
#define DISPLAY_W 800

static SDL_Renderer *ren = NULL;

void* SDL_tex_loader(const char *path) {
return IMG_LoadTexture(ren, path);
void* SDL_tex_loader(const char *path, void *data) {
return IMG_LoadTexture((SDL_Renderer*)data, path);
}

void set_color(int color) {
void set_color(int color, SDL_Renderer *ren) {
tmx_col_bytes col = tmx_col_to_bytes(color);
SDL_SetRenderDrawColor(ren, col.r, col.g, col.b, col.a);
}

void draw_polyline(double **points, double x, double y, int pointsc) {
void draw_polyline(double **points, double x, double y, int pointsc, SDL_Renderer *ren) {
int i;
for (i=1; i<pointsc; i++) {
SDL_RenderDrawLine(ren, x+points[i-1][0], y+points[i-1][1], x+points[i][0], y+points[i][1]);
}
}

void draw_polygon(double **points, double x, double y, int pointsc) {
draw_polyline(points, x, y, pointsc);
void draw_polygon(double **points, double x, double y, int pointsc, SDL_Renderer *ren) {
draw_polyline(points, x, y, pointsc, ren);
if (pointsc > 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) {
Expand All @@ -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 */
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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) {
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion src/tmx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/tmx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/tmx_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down