Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // animation.cpp - GUIAnimation object |
| 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/reboot.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/ioctl.h> |
| 14 | #include <time.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <string> |
| 19 | |
| 20 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 21 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | #include "rapidxml.hpp" |
| 26 | #include "objects.hpp" |
| 27 | |
| 28 | |
| 29 | GUIAnimation::GUIAnimation(xml_node<>* node) |
| 30 | { |
| 31 | xml_node<>* child; |
| 32 | xml_attribute<>* attr; |
| 33 | |
| 34 | mAnimation = NULL; |
| 35 | mFrame = 1; |
| 36 | mFPS = 1; |
| 37 | mLoop = -1; |
| 38 | mRender = 1; |
| 39 | mUpdateCount = 0; |
| 40 | |
| 41 | if (!node) return; |
| 42 | |
| 43 | child = node->first_node("resource"); |
| 44 | if (child) |
| 45 | { |
| 46 | attr = child->first_attribute("name"); |
| 47 | if (attr) |
| 48 | mAnimation = (AnimationResource*) PageManager::FindResource(attr->value()); |
| 49 | } |
| 50 | |
| 51 | // Load the placement |
| 52 | LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, NULL, NULL, &mPlacement); |
| 53 | |
| 54 | child = node->first_node("speed"); |
| 55 | if (child) |
| 56 | { |
| 57 | attr = child->first_attribute("fps"); |
| 58 | if (attr) |
| 59 | mFPS = atoi(attr->value()); |
| 60 | attr = child->first_attribute("render"); |
| 61 | if (attr) |
| 62 | mRender = atoi(attr->value()); |
| 63 | } |
| 64 | if (mFPS > 30) mFPS = 30; |
| 65 | |
| 66 | child = node->first_node("loop"); |
| 67 | if (child) |
| 68 | { |
| 69 | attr = child->first_attribute("frame"); |
| 70 | if (attr) |
| 71 | mLoop = atoi(attr->value()) - 1; |
| 72 | attr = child->first_attribute("start"); |
| 73 | if (attr) |
| 74 | mFrame = atoi(attr->value()); |
| 75 | } |
| 76 | |
| 77 | // Fetch the render sizes |
| 78 | if (mAnimation && mAnimation->GetResource()) |
| 79 | { |
| 80 | mRenderW = gr_get_width(mAnimation->GetResource()); |
| 81 | mRenderH = gr_get_height(mAnimation->GetResource()); |
| 82 | |
| 83 | // Adjust for placement |
| 84 | if (mPlacement != TOP_LEFT && mPlacement != BOTTOM_LEFT) |
| 85 | { |
| 86 | if (mPlacement == CENTER) |
| 87 | mRenderX -= (mRenderW / 2); |
| 88 | else |
| 89 | mRenderX -= mRenderW; |
| 90 | } |
| 91 | if (mPlacement != TOP_LEFT && mPlacement != TOP_RIGHT) |
| 92 | { |
| 93 | if (mPlacement == CENTER) |
| 94 | mRenderY -= (mRenderH / 2); |
| 95 | else |
| 96 | mRenderY -= mRenderH; |
| 97 | } |
| 98 | SetPlacement(TOP_LEFT); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | int GUIAnimation::Render(void) |
| 103 | { |
| 104 | if (!mAnimation || !mAnimation->GetResource(mFrame)) return -1; |
| 105 | |
| 106 | gr_blit(mAnimation->GetResource(mFrame), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int GUIAnimation::Update(void) |
| 111 | { |
| 112 | if (!mAnimation) return -1; |
| 113 | |
| 114 | // Handle the "end-of-animation" state |
| 115 | if (mLoop == -2) return 0; |
| 116 | |
| 117 | // Determine if we need the next frame yet... |
| 118 | if (++mUpdateCount > 30 / mFPS) |
| 119 | { |
| 120 | mUpdateCount = 0; |
| 121 | if (++mFrame >= mAnimation->GetResourceCount()) |
| 122 | { |
| 123 | if (mLoop < 0) |
| 124 | { |
| 125 | mFrame = mAnimation->GetResourceCount() - 1; |
| 126 | mLoop = -2; |
| 127 | } |
| 128 | else |
| 129 | mFrame = mLoop; |
| 130 | } |
| 131 | if (mRender == 2) return 2; |
| 132 | return (Render() == 0 ? 1 : -1); |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | |