1 //Copyright Paul Reiche, Fred Ford. 1992-2002
2 
3 /*
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #include "gfxintrn.h"
20 #include "libs/log.h"
21 
22 DRAWABLE
GetFrameParentDrawable(FRAME f)23 GetFrameParentDrawable (FRAME f)
24 {
25 	if (f != NULL)
26 	{
27 		return f->parent;
28 	}
29 	return NULL;
30 }
31 
32 FRAME
CaptureDrawable(DRAWABLE DrawablePtr)33 CaptureDrawable (DRAWABLE DrawablePtr)
34 {
35 	if (DrawablePtr)
36 	{
37 		return &DrawablePtr->Frame[0];
38 	}
39 
40 	return NULL;
41 }
42 
43 DRAWABLE
ReleaseDrawable(FRAME FramePtr)44 ReleaseDrawable (FRAME FramePtr)
45 {
46 	if (FramePtr != 0)
47 	{
48 		DRAWABLE Drawable;
49 
50 		Drawable = GetFrameParentDrawable (FramePtr);
51 
52 		return (Drawable);
53 	}
54 
55 	return NULL;
56 }
57 
58 COUNT
GetFrameCount(FRAME FramePtr)59 GetFrameCount (FRAME FramePtr)
60 {
61 	DRAWABLE_DESC *DrawablePtr;
62 
63 	if (FramePtr == 0)
64 		return (0);
65 
66 	DrawablePtr = GetFrameParentDrawable (FramePtr);
67 	return DrawablePtr->MaxIndex + 1;
68 }
69 
70 COUNT
GetFrameIndex(FRAME FramePtr)71 GetFrameIndex (FRAME FramePtr)
72 {
73 	if (FramePtr == 0)
74 		return (0);
75 
76 	return FramePtr->Index;
77 }
78 
79 FRAME
SetAbsFrameIndex(FRAME FramePtr,COUNT FrameIndex)80 SetAbsFrameIndex (FRAME FramePtr, COUNT FrameIndex)
81 {
82 	if (FramePtr != 0)
83 	{
84 		DRAWABLE_DESC *DrawablePtr;
85 
86 		DrawablePtr = GetFrameParentDrawable (FramePtr);
87 
88 		FrameIndex = FrameIndex	% (DrawablePtr->MaxIndex + 1);
89 		FramePtr = &DrawablePtr->Frame[FrameIndex];
90 	}
91 
92 	return FramePtr;
93 }
94 
95 FRAME
SetRelFrameIndex(FRAME FramePtr,SIZE FrameOffs)96 SetRelFrameIndex (FRAME FramePtr, SIZE FrameOffs)
97 {
98 	if (FramePtr != 0)
99 	{
100 		COUNT num_frames;
101 		DRAWABLE_DESC *DrawablePtr;
102 
103 		DrawablePtr = GetFrameParentDrawable (FramePtr);
104 		num_frames = DrawablePtr->MaxIndex + 1;
105 		if (FrameOffs < 0)
106 		{
107 			while ((FrameOffs += num_frames) < 0)
108 				;
109 		}
110 
111 		FrameOffs = ((SWORD)FramePtr->Index + FrameOffs) % num_frames;
112 		FramePtr = &DrawablePtr->Frame[FrameOffs];
113 	}
114 
115 	return FramePtr;
116 }
117 
118 FRAME
SetEquFrameIndex(FRAME DstFramePtr,FRAME SrcFramePtr)119 SetEquFrameIndex (FRAME DstFramePtr, FRAME SrcFramePtr)
120 {
121 	COUNT Index;
122 
123 	if (!DstFramePtr || !SrcFramePtr)
124 		return 0;
125 
126 	Index = GetFrameIndex (SrcFramePtr);
127 #ifdef DEBUG
128 	{
129 		DRAWABLE_DESC *DrawablePtr = GetFrameParentDrawable (DstFramePtr);
130 		if (Index > DrawablePtr->MaxIndex)
131 			log_add (log_Debug, "SetEquFrameIndex: source index (%d) beyond "
132 					"destination range (%d)", (int)Index,
133 					(int)DrawablePtr->MaxIndex);
134 	}
135 #endif
136 
137 	return SetAbsFrameIndex (DstFramePtr, Index);
138 }
139 
140 FRAME
IncFrameIndex(FRAME FramePtr)141 IncFrameIndex (FRAME FramePtr)
142 {
143 	DRAWABLE_DESC *DrawablePtr;
144 
145 	if (FramePtr == 0)
146 		return (0);
147 
148 	DrawablePtr = GetFrameParentDrawable (FramePtr);
149 	if (FramePtr->Index < DrawablePtr->MaxIndex)
150 		return ++FramePtr;
151 	else
152 		return DrawablePtr->Frame;
153 }
154 
155 FRAME
DecFrameIndex(FRAME FramePtr)156 DecFrameIndex (FRAME FramePtr)
157 {
158 	if (FramePtr == 0)
159 		return (0);
160 
161 	if (FramePtr->Index > 0)
162 		return --FramePtr;
163 	else
164 	{
165 		DRAWABLE_DESC *DrawablePtr;
166 
167 		DrawablePtr = GetFrameParentDrawable (FramePtr);
168 		return &DrawablePtr->Frame[DrawablePtr->MaxIndex];
169 	}
170 }
171