From f0417b09f6af01ad388899e5e2ed73a25c6c81e5 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Fri, 9 Feb 2018 03:34:50 +0100 Subject: [PATCH] rumina hacks 2 --- src/character.c | 31 +++++++++++++++++++++++-------- src/character.h | 8 ++++---- src/internal.c | 1 + 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/character.c b/src/character.c index 48ecaeb..a5b41bf 100644 --- a/src/character.c +++ b/src/character.c @@ -22,6 +22,7 @@ #include "utils.h" #include #include +#include #include #include @@ -46,12 +47,14 @@ SYMBOL_EXPORT void SelectSpritesheet(struct Game* game, struct Character* charac character->repeat = tmp->repeat; character->pos = 0; if (character->bitmap) { - if ((al_get_bitmap_width(character->bitmap) != tmp->width / tmp->cols) || (al_get_bitmap_height(character->bitmap) != tmp->height / tmp->rows)) { - al_destroy_bitmap(character->bitmap); - character->bitmap = al_create_bitmap(tmp->width / tmp->cols, tmp->height / tmp->rows); - } + //if ((al_get_bitmap_width(character->bitmap) != tmp->width / tmp->cols) || (al_get_bitmap_height(character->bitmap) != tmp->height / tmp->rows)) { + // al_destroy_bitmap(character->bitmap); + // character->bitmap = al_create_bitmap(tmp->width / tmp->cols, tmp->height / tmp->rows); + //} + al_reparent_bitmap(character->bitmap, tmp->bitmap, 0, 0, tmp->width / tmp->cols, tmp->height / tmp->rows); } else { - character->bitmap = al_create_bitmap(tmp->width / tmp->cols, tmp->height / tmp->rows); + //character->bitmap = al_create_bitmap(tmp->width / tmp->cols, tmp->height / tmp->rows); + character->bitmap = al_create_sub_bitmap(tmp->bitmap, 0, 0, tmp->width / tmp->cols, tmp->height / tmp->rows); } PrintConsole(game, "SUCCESS: Spritesheet for %s activated: %s (%dx%d)", character->name, character->spritesheet->name, al_get_bitmap_width(character->bitmap), al_get_bitmap_height(character->bitmap)); return; @@ -210,6 +213,9 @@ SYMBOL_EXPORT void AnimateCharacter(struct Game* game, struct Character* charact } } } + al_reparent_bitmap(character->bitmap, character->spritesheet->bitmap, + (character->pos % character->spritesheet->cols) * (character->spritesheet->width / character->spritesheet->cols), (character->pos / character->spritesheet->cols) * (character->spritesheet->height / character->spritesheet->rows), + character->spritesheet->width / character->spritesheet->cols, character->spritesheet->height / character->spritesheet->rows); } } @@ -262,7 +268,8 @@ SYMBOL_EXPORT void DrawCharacter(struct Game* game, struct Character* character, if (character->spritesheet->flip) { flags = flags | ALLEGRO_FLIP_HORIZONTAL | ALLEGRO_FLIP_VERTICAL; } - DrawScaledCharacter(game, character, tint, 1, 1, flags); + al_draw_bitmap(character->bitmap, GetCharacterX(game, character), GetCharacterY(game, character), flags); + // DrawScaledCharacter(game, character, tint, 1, 1, flags); } SYMBOL_EXPORT void SetCharacterConfines(struct Game* game, struct Character* character, int x, int y) { @@ -290,9 +297,17 @@ SYMBOL_EXPORT float GetCharacterAngle(struct Game* game, struct Character* chara return character->angle; } -SYMBOL_EXPORT bool IsOnCharacter(struct Game* game, struct Character* character, int x, int y) { +SYMBOL_EXPORT bool IsOnCharacter(struct Game* game, struct Character* character, int x, int y, bool pixelperfect) { int x1 = GetCharacterX(game, character), y1 = GetCharacterY(game, character); int x2 = x1 + al_get_bitmap_width(character->bitmap), y2 = y1 + al_get_bitmap_height(character->bitmap); - return ((x >= x1) && (x <= x2) && (y >= y1) && (y <= y2)); + bool test = ((x >= x1) && (x <= x2) && (y >= y1) && (y <= y2)); + + if (test && pixelperfect) { + // TODO: handle being flipped + ALLEGRO_COLOR color = al_get_pixel(character->bitmap, x - x1, y - y1); + return (color.a > 0.0); + } + + return test; } diff --git a/src/character.h b/src/character.h index bab61e9..5b076eb 100644 --- a/src/character.h +++ b/src/character.h @@ -57,10 +57,10 @@ struct Character { float angle; /*!< Characters display angle (radians). */ float pivotX, pivotY; /*!< Pivot point, relative of character's size. */ int confineX, confineY; /*!< Size of the canvas being drawn to, for correct position calculation; when -1, uses viewport size */ - void* data; /*!< Additional, custom character data (HP etc.). */ - int repeat; + int repeat; // TODO: remove bool shared; - bool dead; + bool dead; // TODO: remove + void* data; /*!< Additional, custom character data (HP etc.). */ }; void SelectSpritesheet(struct Game* game, struct Character* character, char* name); @@ -92,6 +92,6 @@ float GetCharacterAngle(struct Game* game, struct Character* character); int GetCharacterConfineX(struct Game* game, struct Character* character); int GetCharacterConfineY(struct Game* game, struct Character* character); -bool IsOnCharacter(struct Game* game, struct Character* character, int x, int y); +bool IsOnCharacter(struct Game* game, struct Character* character, int x, int y, bool pixelperfect); #endif diff --git a/src/internal.c b/src/internal.c index 595d8f3..539e804 100644 --- a/src/internal.c +++ b/src/internal.c @@ -410,6 +410,7 @@ SYMBOL_INTERNAL void ClearScreen(struct Game* game) { al_set_target_backbuffer(game->display); al_set_clipping_rectangle(0, 0, al_get_display_width(game->display), al_get_display_height(game->display)); al_clear_to_color(al_map_rgb(0, 0, 0)); + al_clear_depth_buffer(1.0); al_set_clipping_rectangle(game->_priv.clip_rect.x, game->_priv.clip_rect.y, game->_priv.clip_rect.w, game->_priv.clip_rect.h); }