cycle textures correctly

and remove experimental comments
master
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
* 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
(poikilos)
### Added

View File

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

View File

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

View File

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

View File

@ -3,6 +3,8 @@
#include <clocale>
#include <locale>
#include <vector>
#include <cwctype> // #include <cwtype>
#include <algorithm>
#include "Utils.h"
@ -96,6 +98,23 @@ wstring Utility::withoutExtension(const wstring &path)
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)
{
std::wstring ret = L"/";
@ -141,6 +160,20 @@ std::string Utility::toString(const std::wstring& ws) {
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) {
std::string name_s = toString(name);
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 basename(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 bool isFile(const std::string &name);
static bool isFile(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

View File

@ -112,17 +112,30 @@ if [ ! -d "$MIMETYPES_DB_PATH/packages" ]; then
# echo "Creating '$MIMETYPES_DB_PATH/packages'..."
mkdir "$MIMETYPES_DB_PATH/packages"
fi
update_mime_enable=false
if [ -f "$mime_path" ]; then
# echo "Copying as '$MIMETYPES_DB_PATH/packages/$mime_name'..."
cp -f "$mime_path" "$MIMETYPES_DB_PATH/packages/"
if [ -f "$MIMETYPES_DB_PATH/packages/$mime_name" ]; then
echo "Successfully copied '$MIMETYPES_DB_PATH/packages/$mime_name'"
try_dest="$MIMETYPES_DB_PATH/packages/$mime_name"
if diff -q $mime_path $try_dest; then
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
mime_name=model-x.xml
mime_path="$mimes_path/$mime_name"
cp -f "$mime_path" "$MIMETYPES_DB_PATH/packages/"
if [ -f "$MIMETYPES_DB_PATH/packages/$mime_name" ]; then
echo "Successfully copied '$MIMETYPES_DB_PATH/packages/$mime_name'"
try_dest="$MIMETYPES_DB_PATH/packages/$mime_name"
if diff -q $mime_path $try_dest; then
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
# 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"
# fi
echo "Updating mime type database '$MIMETYPES_DB_PATH'..."
update-mime-database "$MIMETYPES_DB_PATH" # must contain packages
if [ "@$update_mime_enable" = "@true" ]; then
echo "Updating mime type database '$MIMETYPES_DB_PATH'..."
update-mime-database "$MIMETYPES_DB_PATH" # must contain packages
fi
echo "Done."
else
echo "ERROR: $mime_path must be in working directory in order to install it using this script."
fi