Skip to content

Commit

Permalink
Add functions to convert colours
Browse files Browse the repository at this point in the history
Update examples and documentation.
Add deprecation notice on function tmx_get_tile.
  • Loading branch information
baylej committed May 3, 2020
1 parent 78591de commit 369932f
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 59 deletions.
22 changes: 22 additions & 0 deletions doc/src/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,25 @@ Utilities
Call the given callback function for each properties, userdata is forwarded as-is.
See :c:type:`tmx_property_functor`.
Colour conversion functions
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Helper functions to convert the unsigned int colour from the datastructure, see :c:member:`tmx_map.backgroundcolor`,
:c:member:`tmx_object_group.color`, :c:member:`tmx_property_value.color`.
.. c:type:: tmx_col_bytes
4 unsigned bytes in the following order: r, g, b, a.
.. c:function:: tmx_col_bytes tmx_col_to_bytes(uint32_t color);
Splits the colour into 4 bytes.
.. c:type:: tmx_col_floats
4 floats in the following order: r, g, b, a.
.. c:function:: tmx_col_floats tmx_col_to_floats(uint32_t color);
Splits the colour into 4 floats.
60 changes: 28 additions & 32 deletions doc/src/renderer-from-scratch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,8 @@ Rendering a map is quite simple, clear the display to the background colour, the
.. code-tab:: c SDL 2

void set_color(int color) {
unsigned char r, g, b;

r = (color >> 16) & 0xFF;
g = (color >> 8) & 0xFF;
b = (color) & 0xFF;

SDL_SetRenderDrawColor(ren, r, g, b, SDL_ALPHA_OPAQUE);
tmx_col_bytes col = tmx_col_to_bytes(color);
SDL_SetRenderDrawColor(ren, col.r, col.g, col.b, col.a);
}

void render_map(tmx_map *map) {
Expand All @@ -299,13 +294,8 @@ Rendering a map is quite simple, clear the display to the background colour, the
.. code-tab:: c Allegro 5

ALLEGRO_COLOR int_to_al_color(int color) {
unsigned char r, g, b;

r = (color >> 16) & 0xFF;
g = (color >> 8) & 0xFF;
b = (color) & 0xFF;

return al_map_rgb(r, g, b);
tmx_col_floats res = tmx_col_to_floats(color);
return *((ALLEGRO_COLOR*)&res);
}

void render_map(tmx_map *map) {
Expand All @@ -315,8 +305,13 @@ Rendering a map is quite simple, clear the display to the background colour, the
.. code-tab:: c raylib

Color int_to_color(int color) {
tmx_col_bytes res = tmx_col_to_bytes(color);
return *((Color*)&res);
}

void render_map(tmx_map *map) {
ClearBackground(ClearBackground(GetColor(map->backgroundcolor)));
ClearBackground(int_to_color(map->backgroundcolor));
draw_all_layers(map, map->ly_head); // Function to be implemented
}
Expand Down Expand Up @@ -536,42 +531,43 @@ are limited by the availability of draw functions for each kind of shape.
.. code-tab:: c raylib

void draw_polyline(double **points, double x, double y, int pointsc) {
#define LINE_THICKNESS 2.5

void draw_polyline(double offset_x, double offset_y, double **points, int points_count, Color color) {
int i;
for (i=1; i<pointsc; i++) {
DrawLine(x+points[i-1][0], y+points[i-1][1], x+points[i][0], y+points[i][1], color);
for (i=1; i<points_count; i++) {
DrawLineEx((Vector2){offset_x + points[i-1][0], offset_y + points[i-1][1]},
(Vector2){offset_x + points[i][0], offset_y + points[i][1]},
LINE_THICKNESS, color);
}
}
void draw_polygon(double **points, double x, double y, int pointsc) {
draw_polyline(points, x, y, pointsc);
if (pointsc > 2) {
DrawLine(x+points[0][0], y+points[0][1], x+points[pointsc-1][0], y+points[pointsc-1][1], color);
void draw_polygon(double offset_x, double offset_y, double **points, int points_count, Color color) {
draw_polyline(offset_x, offset_y, points, points_count, color);
if (points_count > 2) {
DrawLineEx((Vector2){offset_x + points[0][0], offset_y + points[0][1]},
(Vector2){offset_x + points[points_count-1][0], offset_y + points[points_count-1][1]},
LINE_THICKNESS, color);
}
}
void draw_objects(tmx_object_group *objgr) {
Rectangle rect;
set_color(objgr->color);
tmx_object *head = objgr->head;
Color color = int_to_color(objgr->color);
while (head) {
if (head->visible) {
if (head->obj_type == OT_SQUARE) {
rect.x = head->x;
rect.y = head->y;
rect.width = head->width;
rect.height = head->height;
DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, color);
DrawRectangleLinesEx((Rectangle){head->x, head->y, head->width, head->height}, LINE_THICKNESS, color);
}
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->x, head->y, head->content.shape->points, head->content.shape->points_len, color);
}
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->x, head->y, head->content.shape->points, head->content.shape->points_len, color);
}
else if (head->obj_type == OT_ELLIPSE) {
/* Ellipse function */
DrawEllipseLines(head->x + head->width/2.0, head->y + head->height/2.0, head->width/2.0, head->height/2.0, color);
}
}
head = head->next;
Expand Down
9 changes: 2 additions & 7 deletions examples/allegro/allegro.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@ void* Allegro5_tex_loader(const char *path) {
}

ALLEGRO_COLOR int_to_al_color(int color) {
unsigned char r, g, b;

r = (color >> 16) & 0xFF;
g = (color >> 8) & 0xFF;
b = (color) & 0xFF;

return al_map_rgb(r, g, b);
tmx_col_floats res = tmx_col_to_floats(color);
return *((ALLEGRO_COLOR*)&res);
}

#define LINE_THICKNESS 2.5
Expand Down
29 changes: 17 additions & 12 deletions examples/raylib/raylib.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,39 @@ void raylib_free_tex(void *ptr) {
free(ptr);
}

Color int_to_color(int color) {
tmx_col_bytes res = tmx_col_to_bytes(color);
return *((Color*)&res);
}

#define LINE_THICKNESS 2.5

void draw_polyline(double offset_x, double offset_y, double **points, int points_count, Color color) {
int i;
for (i=1; i<points_count; i++) {
DrawLine(offset_x + points[i-1][0],
offset_y + points[i-1][1],
offset_x + points[i][0],
offset_y + points[i][1], color);
DrawLineEx((Vector2){offset_x + points[i-1][0], offset_y + points[i-1][1]},
(Vector2){offset_x + points[i][0], offset_y + points[i][1]},
LINE_THICKNESS, color);
}
}

void draw_polygon(double offset_x, double offset_y, double **points, int points_count, Color color) {
draw_polyline(offset_x, offset_y, points, points_count, color);
if (points_count > 2) {
DrawLine(offset_x + points[0][0],
offset_y + points[0][1],
offset_x + points[points_count-1][0],
offset_y + points[points_count-1][1], color);
DrawLineEx((Vector2){offset_x + points[0][0], offset_y + points[0][1]},
(Vector2){offset_x + points[points_count-1][0], offset_y + points[points_count-1][1]},
LINE_THICKNESS, color);
}
}

void draw_objects(tmx_object_group *objgr) {
tmx_object *head = objgr->head;
Color color = GetColor(objgr->color << 8); color.a = 255;
Color color = int_to_color(objgr->color);

while (head) {
if (head->visible) {
if (head->obj_type == OT_SQUARE) {
DrawRectangleLines(head->x, head->y, head->width, head->height, color);
DrawRectangleLinesEx((Rectangle){head->x, head->y, head->width, head->height}, LINE_THICKNESS, color);
}
else if (head->obj_type == OT_POLYGON) {
draw_polygon(head->x, head->y, head->content.shape->points, head->content.shape->points_len, color);
Expand All @@ -53,7 +58,7 @@ void draw_objects(tmx_object_group *objgr) {
draw_polyline(head->x, head->y, head->content.shape->points, head->content.shape->points_len, color);
}
else if (head->obj_type == OT_ELLIPSE) {
/* Ellipse function */
DrawEllipseLines(head->x + head->width/2.0, head->y + head->height/2.0, head->width/2.0, head->height/2.0, color);
}
}
head = head->next;
Expand Down Expand Up @@ -125,7 +130,7 @@ void draw_all_layers(tmx_map *map, tmx_layer *layers) {
}

void render_map(tmx_map *map) {
ClearBackground(GetColor(map->backgroundcolor));
ClearBackground(int_to_color(map->backgroundcolor));
draw_all_layers(map, map->ly_head);
}

Expand Down
9 changes: 2 additions & 7 deletions examples/sdl/sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ void* SDL_tex_loader(const char *path) {
}

void set_color(int color) {
unsigned char r, g, b;

r = (color >> 16) & 0xFF;
g = (color >> 8) & 0xFF;
b = (color) & 0xFF;

SDL_SetRenderDrawColor(ren, r, g, b, SDL_ALPHA_OPAQUE);
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) {
Expand Down
11 changes: 10 additions & 1 deletion src/tmx.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ TMXEXPORT tmx_map* tmx_load_callback(tmx_read_functor callback, void *userdata);
/* Frees the map data structure */
TMXEXPORT void tmx_map_free(tmx_map *map);

/* Returns the tile associated with this gid, returns NULL if it fails */
/* DEPRECATED: use `map->tiles[gid]` instead.
Returns the tile associated with this gid, returns NULL if it fails */
TMXEXPORT tmx_tile* tmx_get_tile(tmx_map *map, unsigned int gid);

/* Returns the tmx_property from given hashtable and key, returns NULL if not found */
Expand All @@ -285,6 +286,14 @@ typedef void (*tmx_property_functor)(tmx_property *property, void *userdata);
/* Calls `callback` for each entry in the property hashtable, order of entries is random */
TMXEXPORT void tmx_property_foreach(tmx_properties *hash, tmx_property_functor callback, void *userdata);

/* Color conversion functions */
typedef struct { uint8_t r,g,b,a; } tmx_col_bytes;
typedef struct { float r,g,b,a; } tmx_col_floats;
/* Color component ranging from 0 to 255 */
TMXEXPORT tmx_col_bytes tmx_col_to_bytes(uint32_t color);
/* Color component as floats ranging from 0.f to 1.f */
TMXEXPORT tmx_col_floats tmx_col_to_floats(uint32_t color);

/*
Resource Manager functions
*/
Expand Down
18 changes: 18 additions & 0 deletions src/tmx_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,24 @@ uint32_t get_color_rgb(const char *c) {
return res;
}

tmx_col_bytes tmx_col_to_bytes(uint32_t color) {
tmx_col_bytes res;
res.a = (color >> 24) & 0xFF;
res.r = (color >> 16) & 0xFF;
res.g = (color >> 8) & 0xFF;
res.b = (color) & 0xFF;
return res;
}

tmx_col_floats tmx_col_to_floats(uint32_t color) {
tmx_col_floats res;
res.a = (float)((color >> 24) & 0xFF) / 255.f;
res.r = (float)((color >> 16) & 0xFF) / 255.f;
res.g = (float)((color >> 8) & 0xFF) / 255.f;
res.b = (float)((color) & 0xFF) / 255.f;
return res;
}

int count_char_occurences(const char *str, char c) {
int res = 0;
while(*str != '\0') {
Expand Down

0 comments on commit 369932f

Please sign in to comment.