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

Added function to draw OT_TILE #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 37 additions & 26 deletions examples/raylib/raylib.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,32 @@ void draw_polygon(double offset_x, double offset_y, double **points, int points_
}
}

void draw_objects(tmx_object_group *objgr) {
void draw_tile(void *image, unsigned int sx, unsigned int sy, unsigned int sw, unsigned int sh,
unsigned int dx, unsigned int dy, float opacity) {
Texture2D *texture = (Texture2D*)image;
int op = 0xFF * opacity;
DrawTextureRec(*texture, (Rectangle) {sx, sy, sw, sh}, (Vector2) {dx, dy}, (Color) {op, op, op, op});
}

void draw_object_tile(tmx_tile *tile, unsigned long x, unsigned long y, float op) {
tmx_image *im = tile->image;
void *image;
unsigned int ux, uy, w, h;
ux = tile->ul_x;
uy = tile->ul_y;
w = tile->width;
h = tile->height;
if (im && im->resource_image)
{
image = im->resource_image;
}
else {
image = tile->tileset->image->resource_image;
}
draw_tile(image, ux, uy, w, h, x, y, op);
}

void draw_objects(tmx_map *map, tmx_layer *layer, tmx_object_group *objgr) {
tmx_object *head = objgr->head;
Color color = int_to_color(objgr->color);

Expand All @@ -60,7 +85,13 @@ void draw_objects(tmx_object_group *objgr) {
else if (head->obj_type == OT_ELLIPSE) {
DrawEllipseLines(head->x + head->width/2.0, head->y + head->height/2.0, head->width/2.0, head->height/2.0, color);
}
}
else if (head->obj_type == OT_TILE) {
unsigned int gid = head->content.gid;
if (map->tiles[gid] != NULL) {
draw_object_tile(map->tiles[gid], head->x, head->y - head->height, layer->opacity);
}
}
}
head = head->next;
}
}
Expand All @@ -70,39 +101,19 @@ void draw_image_layer(tmx_image *image) {
DrawTexture(*texture, 0, 0, WHITE);
}

void draw_tile(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) {
Texture2D *texture = (Texture2D*)image;
int op = 0xFF * opacity;
DrawTextureRec(*texture, (Rectangle) {sx, sy, sw, sh}, (Vector2) {dx, dy}, (Color) {op, op, op, op});
}

void draw_layer(tmx_map *map, tmx_layer *layer) {
unsigned long i, j;
unsigned int gid, x, y, w, h, flags;
unsigned int gid;
float op;
tmx_tileset *ts;
tmx_image *im;
void* image;
op = layer->opacity;
for (i=0; i<map->height; i++) {
for (j=0; j<map->width; j++) {
gid = (layer->content.gids[(i*map->width)+j]) & TMX_FLIP_BITS_REMOVAL;
if (map->tiles[gid] != NULL) {
ts = map->tiles[gid]->tileset;
im = map->tiles[gid]->image;
x = map->tiles[gid]->ul_x;
y = map->tiles[gid]->ul_y;
w = ts->tile_width;
h = ts->tile_height;
if (im) {
image = im->resource_image;
}
else {
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);
ts = map->tiles[gid]->tileset;
draw_object_tile(map->tiles[gid], j*ts->tile_width, i*ts->tile_height, op);
}
}
}
Expand All @@ -116,7 +127,7 @@ void draw_all_layers(tmx_map *map, tmx_layer *layers) {
draw_all_layers(map, layers->content.group_head); // recursive call
}
else if (layers->type == L_OBJGR) {
draw_objects(layers->content.objgr);
draw_objects(map, layers, layers->content.objgr);
}
else if (layers->type == L_IMAGE) {
draw_image_layer(layers->content.image);
Expand Down