Skip to content

Commit

Permalink
Added average frame timer return value for gifs.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinW1998 committed Aug 18, 2015
1 parent 75423bb commit be85a1f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 17 deletions.
2 changes: 1 addition & 1 deletion LunaDll/LuaMain/LuaProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ namespace LuaProxy {
void activateOverworldHud(WORLD_HUD_CONTROL activateFlag);
//CSprite functions
bool loadImage(const char* filename, int resNumber, int transColor);
luabind::object loadAnimatedImage(const std::string& filename, lua_State* L);
luabind::object loadAnimatedImage(const std::string& filename, int& smbxFrameTime, lua_State* L);
LuaImageResource* loadImage(const char* filename);
void placeSprite(int type, int imgResource, int xPos, int yPos, const char* extra, int time);
void placeSprite(int type, int imgResource, int xPos, int yPos, const char* extra);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ LuaProxy::Graphics::LuaImageResource* LuaProxy::Graphics::loadImage(const char*
return new LuaProxy::Graphics::LuaImageResource(resNumber);
}

luabind::object LuaProxy::Graphics::loadAnimatedImage(const std::string& filename, lua_State* L)
luabind::object LuaProxy::Graphics::loadAnimatedImage(const std::string& filename, int& smbxFrameTime, lua_State* L)
{
luabind::object tLuaImageResources = luabind::newtable(L);
std::vector<int> resCodes = gLunaRender.LoadAnimatedBitmapResource(utf8_decode(filename));
for (int i = 0; i < resCodes.size(); i++){
std::vector<int> resCodes = gLunaRender.LoadAnimatedBitmapResource(utf8_decode(filename), &smbxFrameTime);
for (unsigned int i = 0; i < resCodes.size(); i++){
tLuaImageResources[i + 1] = luabind::object(L, new LuaProxy::Graphics::LuaImageResource(resCodes[i]), luabind::adopt(luabind::result));
}
return tLuaImageResources;
Expand Down
2 changes: 1 addition & 1 deletion LunaDll/LuaMain/LunaLuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ void CLunaLua::bindAll()
class_<LuaProxy::Graphics::LuaImageResource>("LuaImageResource"),
def("loadImage", (bool(*)(const char*, int, int))&LuaProxy::Graphics::loadImage),
def("loadImage", (LuaProxy::Graphics::LuaImageResource*(*)(const char*))&LuaProxy::Graphics::loadImage, adopt(result)),
def("loadAnimatedImage", &LuaProxy::Graphics::loadAnimatedImage),
def("loadAnimatedImage", &LuaProxy::Graphics::loadAnimatedImage, pure_out_value(_2)),
def("placeSprite", (void(*)(int, int, int, int, const char*, int))&LuaProxy::Graphics::placeSprite),
def("placeSprite", (void(*)(int, int, int, int, const char*))&LuaProxy::Graphics::placeSprite),
def("placeSprite", (void(*)(int, int, int, int))&LuaProxy::Graphics::placeSprite),
Expand Down
54 changes: 48 additions & 6 deletions LunaDll/Rendering/RenderUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <wincodec.h>
#include <wincodecsdk.h>
#include <comutil.h>
#include <functional>
#include "../Globals.h"
#include "../GlobalFuncs.h"
Expand Down Expand Up @@ -32,12 +33,13 @@ HBITMAP CreateEmptyBitmap(int width, int height, int bpp, void** data)
return convHBMP;
}

void LoadGfxAndEnumFrames(const std::wstring& filename, std::function<bool(HBITMAP)> bitmapEnumFunc){
void LoadGfxAndEnumFrames(const std::wstring& filename, std::function<bool(HBITMAP, IWICMetadataQueryReader*)> bitmapEnumFunc){
HRESULT hr;
IWICImagingFactory *pFactory = NULL;
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pFrame = NULL;
IWICFormatConverter *pConvertedFrame = NULL;
IWICMetadataQueryReader* pMetadataReader = NULL;
HBITMAP hDIBBitmap = NULL;
unsigned int width = 0, height = 0;

Expand All @@ -60,6 +62,9 @@ void LoadGfxAndEnumFrames(const std::wstring& filename, std::function<bool(HBITM
for (UINT i = 0; i < frames; i++){
hr = pDecoder->GetFrame(i, &pFrame);
if (FAILED(hr)) goto cleanup;

hr = pFrame->GetMetadataQueryReader(&pMetadataReader);
if (FAILED(hr)) goto cleanup;

hr = pFactory->CreateFormatConverter(&pConvertedFrame);
if (FAILED(hr)) goto cleanup;
Expand Down Expand Up @@ -93,13 +98,29 @@ void LoadGfxAndEnumFrames(const std::wstring& filename, std::function<bool(HBITM
hDIBBitmap = NULL;
goto cleanup;
}
if (!bitmapEnumFunc(hDIBBitmap))
if (!bitmapEnumFunc(hDIBBitmap, pMetadataReader))
break;

if (pMetadataReader) {
pMetadataReader->Release();
pMetadataReader = NULL;
}
if (pConvertedFrame) {
pConvertedFrame->Release();
pConvertedFrame = NULL;
}
if (pFrame) {
pFrame->Release();
pFrame = NULL;
}
}

cleanup:
// NOTE: Not using CComPtr here because this should be possible to build with VS Express
#pragma warning(suppress: 6102)
if (pMetadataReader) {
pMetadataReader->Release();
}
if (pConvertedFrame) {
pConvertedFrame->Release();
}
Expand All @@ -115,22 +136,43 @@ void LoadGfxAndEnumFrames(const std::wstring& filename, std::function<bool(HBITM
HBITMAP LoadGfxAsBitmap(const std::wstring& filename)
{
HBITMAP retSingleBitmap = NULL;
LoadGfxAndEnumFrames(filename, [&retSingleBitmap](HBITMAP nextBitmap){
LoadGfxAndEnumFrames(filename, [&retSingleBitmap](HBITMAP nextBitmap, IWICMetadataQueryReader*){
retSingleBitmap = nextBitmap;
return false;
});

return retSingleBitmap;
}

std::vector<HBITMAP> LoadAnimatedGfx(const std::wstring& filename)
std::tuple<std::vector<HBITMAP>, int> LoadAnimatedGfx(const std::wstring& filename)
{
std::vector<HBITMAP> allBitmapFrames;
LoadGfxAndEnumFrames(filename, [&allBitmapFrames](HBITMAP nextBitmap){
int frameDelayQueries = 0;
short sumFrameDelay = 0;
LoadGfxAndEnumFrames(filename, [&allBitmapFrames, &frameDelayQueries, &sumFrameDelay](HBITMAP nextBitmap, IWICMetadataQueryReader* pMetadata){
allBitmapFrames.push_back(nextBitmap);

_variant_t delayVal;
HRESULT hr = pMetadata->GetMetadataByName(L"/grctlext/Delay", (PROPVARIANT*)&delayVal);
if (SUCCEEDED(hr)) {
frameDelayQueries++;
sumFrameDelay += (short)delayVal;
}

return true; //continue enum
});
return allBitmapFrames;

int frameTime = 0;
if (frameDelayQueries == 0)
{
frameTime = 9;
}
else
{
frameTime = sumFrameDelay / frameDelayQueries;
}

return std::make_tuple(allBitmapFrames, frameTime);
}


Expand Down
3 changes: 2 additions & 1 deletion LunaDll/Rendering/RenderUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#include <windows.h>
#include <string>
#include <vector>
#include <tuple>

HBITMAP CreateEmptyBitmap(int width, int height, int bpp, void** data);
HBITMAP LoadGfxAsBitmap(const std::wstring& filename);
std::vector<HBITMAP> LoadAnimatedGfx(const std::wstring& filename);
std::tuple<std::vector<HBITMAP>, int> LoadAnimatedGfx(const std::wstring& filename);

#endif
13 changes: 9 additions & 4 deletions LunaDll/Rendering/Rendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "../SMBXInternal/PlayerMOB.h"
#include "../GlobalFuncs.h"
#include "GLEngine.h"

#include <tuple>

using namespace std;

Expand Down Expand Up @@ -88,7 +88,7 @@ bool Renderer::LoadBitmapResource(std::wstring filename, int resource_code) {
}


std::vector<int> Renderer::LoadAnimatedBitmapResource(std::wstring filename)
std::vector<int> Renderer::LoadAnimatedBitmapResource(std::wstring filename, int* frameTime)
{
// Concoct full filepath
wstring full_path = L"";
Expand All @@ -107,9 +107,14 @@ std::vector<int> Renderer::LoadAnimatedBitmapResource(std::wstring filename)
full_path = filename;
}

std::vector<HBITMAP> bitmaps = LoadAnimatedGfx(filename);
std::tuple<std::vector<HBITMAP>, int> ret = LoadAnimatedGfx(filename);
std::vector<HBITMAP>& bitmaps = std::get<0>(ret);
if (frameTime) {
double avgFrameTime = (double)std::get<1>(ret);
*frameTime = (int)((avgFrameTime / 100) * 65);
}

std::vector<int> bitmapResCode;

for (HBITMAP nextBitmap : bitmaps) {
int nextResCode = GetAutoImageResourceCode();
BMPBox* pNewbox = new BMPBox(nextBitmap, m_hScreenDC);
Expand Down
2 changes: 1 addition & 1 deletion LunaDll/Rendering/Rendering.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct Renderer {

bool LoadBitmapResource(std::wstring filename, int resource_code, int transparency_color); // don't give full path
bool LoadBitmapResource(std::wstring filename, int resource_code);
std::vector<int> LoadAnimatedBitmapResource(std::wstring filename);
std::vector<int> LoadAnimatedBitmapResource(std::wstring filename, int* frameTime = 0);

bool DeleteImage(int resource_code);
int GetAutoImageResourceCode() const;
Expand Down

0 comments on commit be85a1f

Please sign in to comment.