cycle textures correctly

and remove experimental comments
This commit is contained in:
poikilos 2019-03-09 08:56:23 -05:00
parent 231e438af3
commit 07c7f080af
7 changed files with 123 additions and 79 deletions

View File

@ -5,6 +5,10 @@
### Added ### Added
* hotkeys to reload model/texture * hotkeys to reload model/texture
### Changed
* only try to load png or jpg textures--skip others when cycling
* cycle backwards correctly
## [git] - 2019-03-07 ## [git] - 2019-03-07
(poikilos) (poikilos)
### Added ### Added

View File

@ -127,6 +127,8 @@ Engine::Engine()
{ {
this->worldFPS = 60; this->worldFPS = 60;
this->prevFPS = 30; this->prevFPS = 30;
this->textureExtensions.push_back(L"png");
this->textureExtensions.push_back(L"jpg");
#if WIN32 #if WIN32
m_Device = createDevice( EDT_DIRECT3D9, dimension2d<u32>( 1024, 768 ), 32, false, false, false, nullptr ); m_Device = createDevice( EDT_DIRECT3D9, dimension2d<u32>( 1024, 768 ), 32, false, false, false, nullptr );
#else #else
@ -186,25 +188,22 @@ Engine::~Engine()
void Engine::loadMesh( const wstring &fileName ) void Engine::loadMesh( const wstring &fileName )
{ {
// if (m_LoadedMesh != nullptr) { this->m_PreviousPath = fileName; // even if bad, set this
//std::wstring fn; // to allow F5 to reload
//fn.assign(fileName.c_str());
// wcerr << "fileName = " << fn << endl;
// wcerr << "fileName = " << fileName << endl;
this->m_PreviousPath = fileName;
// wcerr << "this->m_PreviousPath = " << this->m_PreviousPath.c_str() << endl;
// }
if( m_LoadedMesh != nullptr ) if( m_LoadedMesh != nullptr )
m_LoadedMesh->remove(); m_LoadedMesh->remove();
m_LoadedMesh = m_Scene->addAnimatedMeshSceneNode( m_Scene->getMesh( fileName.c_str())); irr::scene::IAnimatedMesh* mesh = m_Scene->getMesh( fileName.c_str());
Utility::dumpMeshInfoToConsole( m_LoadedMesh ); if (mesh != nullptr) {
m_LoadedMesh = m_Scene->addAnimatedMeshSceneNode( mesh );
Utility::dumpMeshInfoToConsole( m_LoadedMesh );
}
} }
void Engine::reloadMesh() void Engine::reloadMesh()
{ {
if (this->m_PreviousPath.length() > 0) { if (this->m_PreviousPath.length() > 0) {
// wcerr << "this->m_PreviousPath = " << this->m_PreviousPath.c_str() << endl;
loadMesh(this->m_PreviousPath); loadMesh(this->m_PreviousPath);
} }
} }
@ -212,7 +211,6 @@ void Engine::reloadMesh()
void Engine::reloadTexture() void Engine::reloadTexture()
{ {
if (this->m_PrevTexturePath.length() > 0) { if (this->m_PrevTexturePath.length() > 0) {
// wcerr << "this->m_PrevTexturePath = " << this->m_PrevTexturePath.c_str() << endl;
loadTexture(this->m_PrevTexturePath); loadTexture(this->m_PrevTexturePath);
} }
} }
@ -325,7 +323,6 @@ void Engine::run()
checkResize(); checkResize();
if (this->m_LoadedMesh != nullptr) { if (this->m_LoadedMesh != nullptr) {
//this->m_LoadedMesh->setAnimationSpeed(this->fps);
if (isPlaying) { if (isPlaying) {
this->m_LoadedMesh->setLoopMode(true); this->m_LoadedMesh->setLoopMode(true);
} }

View File

@ -8,6 +8,8 @@ class View;
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <vector>
#include <irrlicht/irrlicht.h> #include <irrlicht/irrlicht.h>
#include "EventHandler.h" #include "EventHandler.h"
@ -55,6 +57,7 @@ private:
bool isPlaying; bool isPlaying;
irr::u32 worldFPS; irr::u32 worldFPS;
irr::u32 prevFPS; irr::u32 prevFPS;
std::vector<std::wstring> textureExtensions;
public: public:
std::wstring m_PreviousPath; std::wstring m_PreviousPath;

View File

@ -1,6 +1,7 @@
#include "UserInterface.h" #include "UserInterface.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <algorithm>
//#include <filesystem> //#include <filesystem>
#include <experimental/filesystem> #include <experimental/filesystem>
@ -174,62 +175,45 @@ bool UserInterface::loadNextTexture(int direction)
this->m_Engine->m_NextPath = L""; this->m_Engine->m_NextPath = L"";
std::wstring basePath = L"."; std::wstring basePath = L".";
if (this->m_Engine->m_PreviousPath.length() > 0) { if (this->m_Engine->m_PreviousPath.length() > 0) {
// std::wcerr << "this->m_PreviousPath: " << this->m_PreviousPath.c_str() << endl;
std::wstring lastName = Utility::basename(this->m_Engine->m_PreviousPath); std::wstring lastName = Utility::basename(this->m_Engine->m_PreviousPath);
std::wstring lastDirPath = Utility::parentOfPath(this->m_Engine->m_PreviousPath); std::wstring lastDirPath = Utility::parentOfPath(this->m_Engine->m_PreviousPath);
// std::wcerr << "lastDirPath: " << lastDirPath << endl;
std::wstring parentPath = Utility::parentOfPath(lastDirPath); std::wstring parentPath = Utility::parentOfPath(lastDirPath);
// std::wcerr << "parentPath: " << parentPath << endl;
std::wstring dirSeparator = Utility::delimiter(this->m_Engine->m_PreviousPath); std::wstring dirSeparator = Utility::delimiter(this->m_Engine->m_PreviousPath);
std::wstring texturesPath = parentPath + dirSeparator + L"textures"; std::wstring texturesPath = parentPath + dirSeparator + L"textures";
// std::wcerr << "lastName: " << lastName << endl;
// std::wcerr << "pathWithoutExt: " << Utility::withoutExtension(m_PreviousPath) << endl;
// std::wcerr << "nameWithoutExt: " << Utility::withoutExtension(lastName) << endl;
std::wstring tryTexPath = texturesPath + dirSeparator + Utility::withoutExtension(lastName) + L".png"; std::wstring tryTexPath = texturesPath + dirSeparator + Utility::withoutExtension(lastName) + L".png";
std::wcerr << "tryTexPath: " << tryTexPath << endl;
if (direction==0 && Utility::isFile(tryTexPath)) { if (direction==0 && Utility::isFile(tryTexPath)) {
std::wcerr << "is file: " << tryTexPath << endl;
this->m_Engine->m_NextPath = tryTexPath; this->m_Engine->m_NextPath = tryTexPath;
this->m_Engine->loadTexture(this->m_Engine->m_NextPath); this->m_Engine->loadTexture(this->m_Engine->m_NextPath);
} }
else { else {
tryTexPath = lastDirPath + dirSeparator + Utility::withoutExtension(lastName) + L".png"; tryTexPath = lastDirPath + dirSeparator + Utility::withoutExtension(lastName) + L".png";
// std::wcerr << "alternate tryTexPath: " << tryTexPath << endl;
if (direction==0 && Utility::isFile(tryTexPath)) { if (direction==0 && Utility::isFile(tryTexPath)) {
std::wcerr << "is file: " << tryTexPath << endl;
this->m_Engine->m_NextPath = tryTexPath; this->m_Engine->m_NextPath = tryTexPath;
ret = this->m_Engine->loadTexture(this->m_Engine->m_NextPath); ret = this->m_Engine->loadTexture(this->m_Engine->m_NextPath);
} }
else { else {
// wcerr << L"converting path " << texturesPath << endl;
// std::string path = Utility::toString(texturesPath);
std::wstring path = texturesPath; std::wstring path = texturesPath;
if (!fs::is_directory(fs::status(path))) if (!fs::is_directory(fs::status(path)))
path = lastDirPath; // cycle textures in model's directory instead path = lastDirPath; // cycle textures in model's directory instead
// cerr << "looking for next texture in " << path << endl;
// wcerr << "looking for next texture in " << path << endl;
fs::directory_iterator end_itr; // default construction yields past-the-end fs::directory_iterator end_itr; // default construction yields past-the-end
std::wstring nextPath = L""; std::wstring nextPath = L"";
std::wstring retroPath = L""; std::wstring retroPath = L"";
std::wstring lastPath = L""; std::wstring lastPath = L"";
// std::string nextPath = "";
bool found = false; bool found = false;
if (fs::is_directory(fs::status(path))) { if (fs::is_directory(fs::status(path))) {
// for (directory_iterator itr(path); itr != end_itr; ++itr) {
for (const auto & itr : fs::directory_iterator(path)) { for (const auto & itr : fs::directory_iterator(path)) {
// std::cout << entry.path() << std::endl; std::wstring ext = Utility::extensionOf(itr.path().wstring()); // no dot!
if (!is_directory(itr.status())) { if (!is_directory(itr.status())
// if (!itr.is_directory()) { && std::find(m_Engine->textureExtensions.begin(), m_Engine->textureExtensions.end(), ext) != m_Engine->textureExtensions.end()) {
// cycle through files (go to next after m_PrevTexturePath // cycle through files (go to next after m_PrevTexturePath
// if any, otherwise first) // if any previously loaded, otherwise first)
if (nextPath.length() == 0) nextPath = itr.path().wstring(); if (nextPath.length() == 0) nextPath = itr.path().wstring();
lastPath = itr.path().wstring(); lastPath = itr.path().wstring();
if (found) { if (found && direction > 0) {
nextPath = itr.path().wstring(); nextPath = itr.path().wstring();
break; break;
} }
@ -237,16 +221,19 @@ bool UserInterface::loadNextTexture(int direction)
if (!found) retroPath = itr.path().wstring(); if (!found) retroPath = itr.path().wstring();
} }
} }
if (retroPath.length()==0) retroPath = lastPath; // previous is last if at beginning if (retroPath.length()==0)
if (direction < 0) nextPath = retroPath; retroPath = lastPath; // previous is last if at beginning
if (nextPath.length() > 0) ret = this->m_Engine->loadTexture(nextPath); if (direction < 0)
wcerr << "chose texture '" << nextPath << "': " << (ret?"OK":"FAIL") << endl; nextPath = retroPath;
if (nextPath.length() > 0) {
ret = this->m_Engine->loadTexture(nextPath);
}
} }
// else wcerr << "no '" << path << "'" << endl;
} }
} }
} }
else debug() << "Can't cycle texture since no file was opened" << endl; else debug() << "Can't cycle texture since no file was opened" << endl;
return ret;
} }
// IEventReceiver // IEventReceiver
@ -269,17 +256,14 @@ bool UserInterface::OnEvent( const SEvent &event )
} }
else if (event.KeyInput.Char == L'+' || event.KeyInput.Char == L'=') { else if (event.KeyInput.Char == L'+' || event.KeyInput.Char == L'=') {
m_Engine->setAnimationFPS(m_Engine->animationFPS() + 5); m_Engine->setAnimationFPS(m_Engine->animationFPS() + 5);
// std::wcerr << "m_Engine->animationFPS(): " << m_Engine->animationFPS() << endl;
} }
else if (event.KeyInput.Char == L'-') { else if (event.KeyInput.Char == L'-') {
if (m_Engine->animationFPS() > 0) { if (m_Engine->animationFPS() > 0) {
m_Engine->setAnimationFPS(m_Engine->animationFPS() - 5); m_Engine->setAnimationFPS(m_Engine->animationFPS() - 5);
} }
// std::wcerr << "m_Engine->animationFPS(): " << m_Engine->animationFPS() << endl;
} }
else if (event.KeyInput.Char == L' ') { else if (event.KeyInput.Char == L' ') {
m_Engine->toggleAnimation(); m_Engine->toggleAnimation();
// std::wcerr << "m_Engine->animationFPS(): " << m_Engine->animationFPS() << endl;
} }
// std::wcerr << "Char: " << event.KeyInput.Char << endl; // std::wcerr << "Char: " << event.KeyInput.Char << endl;
} }
@ -289,33 +273,37 @@ bool UserInterface::OnEvent( const SEvent &event )
} }
else if (event.EventType == EET_MOUSE_INPUT_EVENT) else if (event.EventType == EET_MOUSE_INPUT_EVENT)
{ {
switch ( event.MouseInput.Event) // TODO: improve this copypasta
{ switch ( event.MouseInput.Event)
case EMIE_LMOUSE_LEFT_UP: {
if ( LMouseState == 2) case EMIE_LMOUSE_LEFT_UP:
{ if ( LMouseState == 2)
LMouseState = 3; {
} LMouseState = 3;
break; }
case EMIE_LMOUSE_PRESSED_DOWN: break;
if ( LMouseState == 0)
{ case EMIE_LMOUSE_PRESSED_DOWN:
LMouseState = 1; if ( LMouseState == 0)
} {
break; LMouseState = 1;
case EMIE_RMOUSE_LEFT_UP: }
if ( RMouseState == 2) break;
{
RMouseState = 3; case EMIE_RMOUSE_LEFT_UP:
} if ( RMouseState == 2)
break; {
case EMIE_RMOUSE_PRESSED_DOWN: RMouseState = 3;
if ( RMouseState == 0) }
{ break;
RMouseState = 1;
} case EMIE_RMOUSE_PRESSED_DOWN:
break; if ( RMouseState == 0)
} {
RMouseState = 1;
}
break;
}
} }
else if (!(event.EventType == EET_GUI_EVENT)) else if (!(event.EventType == EET_GUI_EVENT))
return false; return false;

View File

@ -3,6 +3,8 @@
#include <clocale> #include <clocale>
#include <locale> #include <locale>
#include <vector> #include <vector>
#include <cwctype> // #include <cwtype>
#include <algorithm>
#include "Utils.h" #include "Utils.h"
@ -96,6 +98,23 @@ wstring Utility::withoutExtension(const wstring &path)
return ret; return ret;
} }
wstring Utility::extensionOf(const wstring &path)
{
std::wstring ret = L"";
std::wstring::size_type lastDotPos = path.find_last_of(L".");
if (lastDotPos != std::wstring::npos) {
std::wstring::size_type lastSlashPos = path.find_last_of(L"/");
if (lastSlashPos == std::wstring::npos) {
std::wstring::size_type lastSlashPos = path.find_last_of(L"\\");
}
if (lastSlashPos != std::wstring::npos) {
if (lastDotPos > lastSlashPos) ret = path.substr(lastDotPos + 1);
}
else ret = path.substr(lastDotPos + 1);
}
return ret;
}
wstring Utility::delimiter(const wstring &path) wstring Utility::delimiter(const wstring &path)
{ {
std::wstring ret = L"/"; std::wstring ret = L"/";
@ -141,6 +160,20 @@ std::string Utility::toString(const std::wstring& ws) {
return ret; return ret;
} }
std::string Utility::toLower(const std::string &s)
{
std::string ret = s;
std::transform( ret.begin(), ret.end(), ret.begin(), ::tolower);
return ret;
}
wstring Utility::toLower(const wstring &s)
{
wstring ret = s;
std::transform( ret.begin(), ret.end(), ret.begin(), towlower);
return ret;
}
bool Utility::isFile(const std::wstring& name) { bool Utility::isFile(const std::wstring& name) {
std::string name_s = toString(name); std::string name_s = toString(name);
if (FILE *file = fopen(name_s.c_str(), "r")) { if (FILE *file = fopen(name_s.c_str(), "r")) {

View File

@ -13,10 +13,13 @@ public:
static std::wstring parentOfPath(const std::wstring &path); static std::wstring parentOfPath(const std::wstring &path);
static std::wstring basename(const std::wstring &path); static std::wstring basename(const std::wstring &path);
static std::wstring withoutExtension(const std::wstring &path); static std::wstring withoutExtension(const std::wstring &path);
static std::wstring extensionOf(const std::wstring &path);
static std::wstring delimiter(const std::wstring &path); static std::wstring delimiter(const std::wstring &path);
static bool isFile(const std::string &name); static bool isFile(const std::string &name);
static bool isFile(const std::wstring &name); static bool isFile(const std::wstring &name);
static std::string toString(const std::wstring &name); static std::string toString(const std::wstring &name);
static std::string toLower(const std::string &s);
static std::wstring toLower(const std::wstring &s);
}; };
#endif // UTILS_H #endif // UTILS_H

View File

@ -112,17 +112,30 @@ if [ ! -d "$MIMETYPES_DB_PATH/packages" ]; then
# echo "Creating '$MIMETYPES_DB_PATH/packages'..." # echo "Creating '$MIMETYPES_DB_PATH/packages'..."
mkdir "$MIMETYPES_DB_PATH/packages" mkdir "$MIMETYPES_DB_PATH/packages"
fi fi
update_mime_enable=false
if [ -f "$mime_path" ]; then if [ -f "$mime_path" ]; then
# echo "Copying as '$MIMETYPES_DB_PATH/packages/$mime_name'..." # echo "Copying as '$MIMETYPES_DB_PATH/packages/$mime_name'..."
cp -f "$mime_path" "$MIMETYPES_DB_PATH/packages/" try_dest="$MIMETYPES_DB_PATH/packages/$mime_name"
if [ -f "$MIMETYPES_DB_PATH/packages/$mime_name" ]; then if diff -q $mime_path $try_dest; then
echo "Successfully copied '$MIMETYPES_DB_PATH/packages/$mime_name'" echo "(You already have an identical $try_dest)"
else
cp -f "$mime_path" "$MIMETYPES_DB_PATH/packages/"
if [ -f "$try_dest" ]; then
echo "Successfully copied '$try_dest'"
update_mime_enable=true
fi
fi fi
mime_name=model-x.xml mime_name=model-x.xml
mime_path="$mimes_path/$mime_name" mime_path="$mimes_path/$mime_name"
cp -f "$mime_path" "$MIMETYPES_DB_PATH/packages/" try_dest="$MIMETYPES_DB_PATH/packages/$mime_name"
if [ -f "$MIMETYPES_DB_PATH/packages/$mime_name" ]; then if diff -q $mime_path $try_dest; then
echo "Successfully copied '$MIMETYPES_DB_PATH/packages/$mime_name'" echo "(You already have an identical $try_dest)"
else
cp -f "$mime_path" "$MIMETYPES_DB_PATH/packages/"
if [ -f "$try_dest" ]; then
echo "Successfully copied '$try_dest'"
update_mime_enable=true
fi
fi fi
# Since OBJ Mime type is broken on linux (detected as TGIF), trying # Since OBJ Mime type is broken on linux (detected as TGIF), trying
@ -136,8 +149,11 @@ if [ -f "$mime_path" ]; then
# rm -f "$MIMETYPES_DB_PATH/packages/$mime_name" # rm -f "$MIMETYPES_DB_PATH/packages/$mime_name"
# fi # fi
echo "Updating mime type database '$MIMETYPES_DB_PATH'..." if [ "@$update_mime_enable" = "@true" ]; then
update-mime-database "$MIMETYPES_DB_PATH" # must contain packages echo "Updating mime type database '$MIMETYPES_DB_PATH'..."
update-mime-database "$MIMETYPES_DB_PATH" # must contain packages
fi
echo "Done."
else else
echo "ERROR: $mime_path must be in working directory in order to install it using this script." echo "ERROR: $mime_path must be in working directory in order to install it using this script."
fi fi