scene_gfx.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <furi.h>
  2. #include "scene.h"
  3. #include "assets/items.h"
  4. #include "assets/meta.h"
  5. #include <gui/elements.h>
  6. void dolphin_scene_transition_handler(SceneState* state) {
  7. uint8_t speed_mod = (state->player_v.x || state->player_v.y || state->transition) ? 6 : 10;
  8. if(state->player_v.x < 0) {
  9. state->frame_pending = DirLeft;
  10. } else if(state->player_v.x > 0) {
  11. state->frame_pending = DirRight;
  12. } else if(state->player_v.y < 0) {
  13. state->frame_pending = DirUp;
  14. } else if(state->player_v.y > 0) {
  15. state->frame_pending = DirDown;
  16. }
  17. state->transition_pending = state->frame_group != state->frame_pending;
  18. if(*&frames[state->frame_group][state->frame_type]->frames[state->frame_idx].f) {
  19. state->current_frame = *&frames[state->frame_group][state->frame_type];
  20. }
  21. uint8_t total = state->current_frame->frames[2].f == NULL ? 2 : 3;
  22. if(state->transition_pending && !state->frame_idx) {
  23. state->transition_pending = false;
  24. state->transition = true;
  25. }
  26. if(state->transition) {
  27. state->frame_type = state->frame_pending;
  28. state->frame_group = state->last_group;
  29. state->transition = !(state->frame_idx == total - 1);
  30. } else {
  31. state->frame_group = state->frame_type;
  32. }
  33. state->player_anim++;
  34. if(!(state->player_anim % speed_mod)) {
  35. state->frame_idx = (state->frame_idx + 1) % total;
  36. }
  37. }
  38. void dolphin_scene_render_dolphin(SceneState* state, Canvas* canvas) {
  39. furi_assert(state);
  40. furi_assert(canvas);
  41. dolphin_scene_transition_handler(state);
  42. canvas_set_bitmap_mode(canvas, true);
  43. canvas_set_color(canvas, ColorWhite);
  44. canvas_draw_icon(
  45. canvas, state->player.x, state->player.y, state->current_frame->frames[state->frame_idx].b);
  46. canvas_set_color(canvas, ColorBlack);
  47. canvas_draw_icon(
  48. canvas, state->player.x, state->player.y, state->current_frame->frames[state->frame_idx].f);
  49. canvas_set_bitmap_mode(canvas, false);
  50. }
  51. static bool item_screen_bounds_x(int32_t pos) {
  52. return pos > -SCREEN_WIDTH && pos < (SCREEN_WIDTH * 2);
  53. }
  54. static bool item_screen_bounds_y(int32_t pos) {
  55. return pos > -SCREEN_HEIGHT * 2 && pos < (SCREEN_HEIGHT * 2);
  56. }
  57. void dolphin_scene_render(SceneState* state, Canvas* canvas, uint32_t t) {
  58. furi_assert(state);
  59. furi_assert(canvas);
  60. canvas_set_font(canvas, FontSecondary);
  61. canvas_set_color(canvas, ColorBlack);
  62. const Item** current_scene = get_scene(state);
  63. for(uint8_t l = 0; l < LAYERS; l++) {
  64. for(uint8_t i = 0; i < ItemsEnumTotal; i++) {
  65. int32_t item_pos_X = (current_scene[i]->pos.x - state->player_global.x);
  66. int32_t item_pos_Y = (current_scene[i]->pos.y - state->player_global.y);
  67. if(item_screen_bounds_x(item_pos_X) && item_screen_bounds_y(item_pos_Y)) {
  68. if(l == current_scene[i]->layer) {
  69. if(current_scene[i]->draw) {
  70. current_scene[i]->draw(canvas, state);
  71. }
  72. }
  73. }
  74. }
  75. if(l == DOLPHIN_LAYER) dolphin_scene_render_dolphin(state, canvas);
  76. }
  77. }
  78. void dolphin_scene_render_state(SceneState* state, Canvas* canvas) {
  79. furi_assert(state);
  80. furi_assert(canvas);
  81. char buf[64];
  82. canvas_set_font(canvas, FontSecondary);
  83. canvas_set_color(canvas, ColorBlack);
  84. // dolphin_scene_debug
  85. if(state->debug) {
  86. sprintf(
  87. buf,
  88. "%d:%d %d/%dP%dL%d T%d-%d",
  89. state->frame_idx,
  90. state->current_frame->frames[2].f == NULL ? 2 : 3,
  91. state->frame_group,
  92. state->frame_type,
  93. state->frame_pending,
  94. state->last_group,
  95. state->transition_pending,
  96. state->transition);
  97. canvas_draw_str(canvas, 0, 13, buf);
  98. }
  99. if(state->action == INTERACT) scene_activate_item_callback(state, canvas);
  100. }