forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRageTexture.cpp
169 lines (150 loc) · 5.57 KB
/
RageTexture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "global.h"
#include "RageTexture.h"
#include "RageUtil.h"
#include "RageTextureManager.h"
#include <cstring>
using std::vector;
RageTexture::RageTexture( RageTextureID name ):
m_iRefCount(1), m_bWasUsed(false), m_ID(name),
m_iSourceWidth(0), m_iSourceHeight(0),
m_iTextureWidth(0), m_iTextureHeight(0),
m_iImageWidth(0), m_iImageHeight(0),
m_iFramesWide(1), m_iFramesHigh(1) {}
RageTexture::~RageTexture()
{
}
void RageTexture::CreateFrameRects()
{
GetFrameDimensionsFromFileName( GetID().filename, &m_iFramesWide, &m_iFramesHigh, m_iSourceWidth, m_iSourceHeight );
// Fill in the m_FrameRects with the bounds of each frame in the animation.
m_TextureCoordRects.clear();
const float frame_w= static_cast<float>(m_iImageWidth) /
static_cast<float>(m_iFramesWide);
const float frame_h= static_cast<float>(m_iImageHeight) /
static_cast<float>(m_iFramesHigh);
const float tex_w= static_cast<float>(m_iTextureWidth);
const float tex_h= static_cast<float>(m_iTextureHeight);
for( int j=0; j<m_iFramesHigh; j++ ) // traverse along Y
{
for( int i=0; i<m_iFramesWide; i++ ) // traverse along X (important that this is the inner loop)
{
Rage::RectF frect(((i+0) * frame_w) / tex_w, ((j+0) * frame_h) / tex_h,
((i+1) * frame_w) / tex_w, ((j+1) * frame_h) / tex_h);
m_TextureCoordRects.push_back( frect ); // the index of this array element will be (i + j*m_iFramesWide)
//LOG->Trace( "Adding frect%d %f %f %f %f", (i + j*m_iFramesWide), frect.left, frect.top, frect.right, frect.bottom );
}
}
}
void RageTexture::GetFrameDimensionsFromFileName( std::string sPath, int* piFramesWide, int* piFramesHigh, int source_width, int source_height )
{
static Regex match( " ([0-9]+)x([0-9]+)([\\. ]|$)" );
vector<std::string> asMatch;
if( !match.Compare(sPath, asMatch) )
{
*piFramesWide = *piFramesHigh = 1;
return;
}
// Check for nonsense values. Some people might not intend the hint. -Kyz
int maybe_width= StringToInt(asMatch[0]);
int maybe_height= StringToInt(asMatch[1]);
if(maybe_width <= 0 || maybe_height <= 0)
{
*piFramesWide = *piFramesHigh = 1;
return;
}
// Font.cpp uses this function, but can't pass in a texture size. Other
// textures can pass in a size though, and having more frames than pixels
// makes no sense. -Kyz
if(source_width > 0 && source_height > 0)
{
if(maybe_width > source_width || maybe_height > source_height)
{
*piFramesWide = *piFramesHigh = 1;
return;
}
}
*piFramesWide = maybe_width;
*piFramesHigh = maybe_height;
}
const Rage::RectF *RageTexture::GetTextureCoordRect( int iFrameNo ) const
{
return &m_TextureCoordRects[iFrameNo % GetNumFrames()];
}
// lua start
#include "LuaBinding.h"
/** @brief Allow Lua to have access to the RageTexture. */
class LunaRageTexture: public Luna<RageTexture>
{
public:
static int position( T* p, lua_State *L ) { p->SetPosition( FArg(1) ); COMMON_RETURN_SELF; }
static int loop( T* p, lua_State *L ) { p->SetLooping( BIArg(1) ); COMMON_RETURN_SELF; }
static int rate( T* p, lua_State *L ) { p->SetPlaybackRate( FArg(1) ); COMMON_RETURN_SELF; }
static int GetTextureCoordRect( T* p, lua_State *L )
{
const Rage::RectF *pRect = p->GetTextureCoordRect( IArg(1) );
lua_pushnumber( L, pRect->left );
lua_pushnumber( L, pRect->top );
lua_pushnumber( L, pRect->right );
lua_pushnumber( L, pRect->bottom );
return 4;
}
static int GetNumFrames(T* p, lua_State* L)
{
lua_pushnumber(L, p->GetNumFrames());
return 1;
}
static int Reload(T* p, lua_State* L)
{
p->Reload();
COMMON_RETURN_SELF;
}
DEFINE_METHOD(GetSourceWidth, GetSourceWidth());
DEFINE_METHOD(GetSourceHeight, GetSourceHeight());
DEFINE_METHOD(GetTextureWidth, GetTextureWidth());
DEFINE_METHOD(GetTextureHeight, GetTextureHeight());
DEFINE_METHOD(GetImageWidth, GetImageWidth());
DEFINE_METHOD(GetImageHeight, GetImageHeight());
DEFINE_METHOD(GetPath, GetID().filename);
LunaRageTexture()
{
ADD_METHOD( position );
ADD_METHOD( loop );
ADD_METHOD( rate );
ADD_METHOD( GetTextureCoordRect );
ADD_METHOD( GetNumFrames );
ADD_METHOD( Reload );
ADD_METHOD(GetSourceWidth);
ADD_METHOD(GetSourceHeight);
ADD_METHOD(GetTextureWidth);
ADD_METHOD(GetTextureHeight);
ADD_METHOD(GetImageWidth);
ADD_METHOD(GetImageHeight);
ADD_METHOD(GetPath);
}
};
LUA_REGISTER_CLASS( RageTexture )
// lua end
/*
* Copyright (c) 2001-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/