• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
mynameisnafe

Assimp and Common Dialog Box - Strings - C++

12 posts in this topic

Hi all,

 

I've been playing with Assimp: I've used it before, hardcoding file paths. However now I'm getting a complete file path from one of these Common Dialog Box things with

 GetOpenFileName()

http://msdn.microsoft.com/en-gb/library/windows/desktop/ms646829(v=vs.85).aspx#open_file

 

I'm then jumping through some (CString-shaped) hoops to format the string for the call below:


Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(filename, aiProcessPreset_TargetRealtime_Fast);
//aiProcessPreset_TargetRealtime_Fast has the configs we'll need// nickthecoder.com

 

The problem is that scene is NULL. I'm guessing this is because of the flilepath and my incorrect formatting.

- what steps do I need to go through to ensure the filepath is good for assimp?

Hope to hear from you soon :)

0

Share this post


Link to post
Share on other sites

Well, for starters, since you assume that your CString hoops are the culprit, you might show that particular code.

 

And to make it even easier for us, show the path before and after. This should make it trivial to point out any problem.

2

Share this post


Link to post
Share on other sites

Sounds reasonable :)

My Hoops are straight out of Stack Overflow results of google searches.

This was called wstrtostr() on SO by the Answerer I beleive - this is so I can turn a 

CString into a std::string (....trying to standardize on std::string type in a multi-byte character set build)

This fn looks reasobable enough

std::string FunkyString(const std::wstring &wstr)
{
    std::string strTo;
    
    char *szTo = new char[wstr.length() + 1];
    szTo[wstr.size()] = '\0';
    
    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, szTo, (int)wstr.length(), NULL, NULL);
    
    strTo = szTo;
    delete[] szTo;


    return strTo;
}

//
// Swap '//' for '\\' in the given string
//
CString SlashFix(CString strBang)
{
    int n = strBang.Replace(_T("//"), _T("\\"));
    return strBang;
}

Then here's my Filename getter: Please excuse the poor variable names etc

std::string App::DoOpenFileDialog()
{
	std::string fName;
	OPENFILENAME ofn;       // common dialog box structure
	char szFile[260];       // buffer for file name
	HANDLE hf ;              // file handle

	// Initialize OPENFILENAME
	ZeroMemory(&ofn, sizeof(ofn));
	...

	// Display the Open dialog box. 

	if( GetOpenFileName(&ofn) == TRUE ) 
	{	
		hf = CreateFileW (ofn.lpstrFile,  GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL,
						OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);

		CString file;
                file.Format(L"%s", ofn.lpstrFile);
		file = SlashFix(file);

		fName = FunkyString((LPCWSTR)file);
		OutputDebugString(file);
	}
	else
		fName = ("error!");

	return fName;
}

I also have this which lets me find out if a file exists - more stack overflow:

 

static bool FileExists( std::string path )
{
	std::ifstream i(path.c_str());
	return (i != 0);
}

And this will return false with any path from my dialog and 'CHoops',

However it works for files that are in subdirectories of the project dir - if there are no spaces in the name. So I think I need to escape the space characters?

If need more code please ask, however return value of DoOpenFileDialog() goes pretty much straight to the assimp call

case ID_FILE_OPEN:
{
	// returns full path
	std::string fName = DoOpenFileDialog();
				
	// deosn't work- string not fomratted correctly
        bool bReturnsFalse = Scatterbrain::FileExists(fName);
				
	//works: (this file is here and can be found from this space-less path);
	bool bReturnsTrue = Scatterbrain::FileExists("Models\\Cessna\\Cessna172.obj");

	MeshList meshes;
	GLFactory* f = GLFactory::Create();
				
        // loads with assimp
	if( f->LoadMeshListFromFile(fName, &meshes) )
	{
		OutputDebugString(L"Wow");
		// Make GL Meshes
	}
			OutputDebugString(L"OpenFile: ");
}
break;

Hope this helps :)

 

0

Share this post


Link to post
Share on other sites

Before and After Strings copied from Output Window (I'm using VS2012 if it matters):

 

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

As I said, I think it's the spaces,
 

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

 

Code from DoOpenFileDialog() used to produce

	if( GetOpenFileName(&ofn) == TRUE ) 
	{	
		hf = CreateFileW (ofn.lpstrFile,  GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL,
						OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);

		CString file;
		file.Format(L"%s", ofn.lpstrFile);
		OutputDebugString(file);
		file = SlashFix(file);
		fName = FunkyString((LPCWSTR)file);

		OutputDebugString(file);
	}

On another note - I did try and 'replaces the spaces' with the SlashFix function with 

 n = strBang.Replace("\ ", "\\ ");

to no apparent avail except this warning:

c:\users\nathan\documents\visual studio 2012\darkstar\darkstar\appdialog.cpp(27): warning C4129: ' ' : unrecognized character escape sequence
0

Share this post


Link to post
Share on other sites

The problem is that scene is NULL. I'm guessing this is because of the flilepath and my incorrect formatting.

Have you actually checked what is the error?

if(!scene) std::cout << importer.GetErrorString() << std::endl;

 


On another note - I did try and 'replaces the spaces' with the SlashFix function with 
n = strBang.Replace("\ ", "\\ ");
Not sure what you're trying to do here, but "\ " is not a valid escape sequence. You probably wanted just " ".
 
Edit. Damn, I really hate this buggy quotation thingie on this forum. Whatever.
Edited by Sponji
0

Share this post


Link to post
Share on other sites

 


The problem is that scene is NULL. I'm guessing this is because of the flilepath and my incorrect formatting.

Have you actually checked what is the error?

if(!scene) std::cout << importer.GetErrorString() << std::endl;

 


On another note - I did try and 'replaces the spaces' with the SlashFix function with 
n = strBang.Replace("\ ", "\\ ");
Not sure what you're trying to do here, but "\ " is not a valid escape sequence. You probably wanted just " ".

 

I have tried looking at the error:

 

if(!scene)
{
	const char* pszErr = aiGetErrorString();
	MessageBox(NULL, (LPCWSTR)pszErr, L"Error: Assimp", NULL);
	return false;
}

Yields an empty string and message box.

On spaces replaces:

 

C:\Users\Nathan\Documents\Visual Studio 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj
C:\Users\Nathan\Documents\Visual\ Studio\ 2012\DarkStar\DarkStar\Models\Cessna\Cessna172.obj

 Is what I get in the output but in the debugger it looks like

C:\\Users\Nathan\\Documents\\Visual Studio\\ 2012\\DarkStar\\DarkStar\\Models\\Cessna\\Cessna172.obj

I'm sure, looking at that this, that I don't know what I'm doing preparing this string. My lord I feel a fool!

I've looked back at another project using hardcoded paths with assimp and they're of the form

 

"../Models/Cessna/Cessna172.obj"

I think I've got enough to play with for a while but I may get stuck..

0

Share this post


Link to post
Share on other sites

You shouldn't need to escape those spaces anyway. Maybe try hard coding the filename to the absolute path and see if that works.

filename = "C:\\Users\\Nathan\\Documents\\Visual Studio 2012\\DarkStar\\DarkStar\\Models\\Cessna\Cessna172.obj";
0

Share this post


Link to post
Share on other sites

RIght - I'm close now.

I would of thought so too?

I tried the following and found a file with the hardcoded path and didn't when selecting the same file in the DIalog
 

CString SlashFix(CString strBang)
{
	int n = strBang.Replace(_T("\\"), _T("//"));
		n += strBang.Replace(_T(" "), _T("\\ "));

	return strBang;
}

// With this doing my testing

.....
   bool bDialogOpen = true;
	std::string path;
				
	if(bDialogOpen)			                        
		path = DoOpenFileDialog();                      // SlashFix() broken
	else
		path = "Models//Cessna\ 172//Cessna172.obj";	// works
				
	bool bSanity = Scatterbrain::FileExists(path);
				
	MeshList meshes;
	GLFactory* f = GLFactory::Create();

	if( f->LoadMeshListFromFile(path, &meshes) )
		OutputDebugString(L"// Make GL Meshes");
...

I'll try now with the full path hardcoded..

Meanwhile, as an aside pondering, if file.cpp opens writes to and saves a file, it would be created in the directory of that file, right?

Okay so the path hardcoded like this passes the FileExists test - also with slashes going the other way:

path = "C:\\Users\\Nathan\\Documents\\Visual Studio 2012\\DarkStar\\DarkStar\\Models\\Cessna\\Cessna172.obj";

So what's in the CString that I'm creating that I can't see, that I'm messing up when I SlashFix() ?

Edited by mynameisnafe
0

Share this post


Link to post
Share on other sites

It appears you're simply trying to get a file path from the user and load the file with Assimp. If so, you shouldn't be creating any file of your own. Try loading a std::string directly from the GetOpenFileName fileName buffer, and call Assimp. This assumes (as you imply) you've set the Character Set property to "Use Multi-Byte Character Set"

    char fileName[256], fileTitle[256];
    fileName[0] = 0;
    fileTitle[0] = 0;
    std::string userFilePath;

    OPENFILENAME ofn;
    ZeroMemory(&ofn, sizeof(OPENFILENAME));

    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.lpstrFilter = "All files (*.*)\0*.*\0\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFile = fileName;
    ofn.nMaxFile = 256;
    ofn.Flags = OFN_FILEMUSTEXIST;
    BOOL response = GetOpenFileNameA(&ofn);
    if (!response)
    {
        return false;
    }
    userFilePath = fileName;
    scene = importer.ReadFile(userFilePath, ... [your flags]);
Edited by Buckeye
1

Share this post


Link to post
Share on other sites


It appears you're simply trying to get a file path from the user and load the file with Assimp. If so, you shouldn't be creating any file of your own. Try loading a std::string directly from the GetOpenFileName fileName buffer, and call Assimp. This assumes (as you imply) you've set the Character Set property to "Use Multi-Byte Character Set"


- precisely ..

So, what you're saying is that this line in DoFileOpenDialog()
hf = CreateFileW (ofn.lpstrFile, GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL,
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
 

is preventing me from then opening the file with ifstream later (Scatterbrain::FileExists(..) ) and Assimp after that ( .ReadFile(...) )

Why did I not think of that, given it says "LPSECURITY_ATTRIBUTES" in capital flippin' letters?

?

See, now that is something I hadn't thought of - I don't know that much about Win32 / MFC, just the basics like "Give me something I can draw on without using glut or GLFW"; this project is me trying to do some sort of editor.
 

0

Share this post


Link to post
Share on other sites

Buckeye - I declare you the Victor!

Thank you sir. I very much should of spotted that, and I get to delete some code - which is always a bonus.

0

Share this post


Link to post
Share on other sites

So, what you're saying is ...

 

Thanks for giving me credit for astute observations, but, actually, I wasn't trying to say anything more than I did wink.png

 

I just tried to get to the basics, and didn't understand what you were trying to do with the string manipulations and CreateFile call. My suggestion was simply how I use Assimp! biggrin.png

 

So.. did that all help? EDIT: cross-post - it appears it did.

Edited by Buckeye
0

Share this post


Link to post
Share on other sites

Indeed!

It's a long story - basically having gotten fed up of MFC while trying to create an anything-to-myformat application, I'm rolling Win32 basics and like building a framework on that. I just copied that thing from MSDN and didn't touch it, just adding stuff (......below the line of the code that was causing the problem......) to try and make it work. Face-to-the-palm!

I had a cube a minute ago, I've lost it now, but I'm now back in my comfort zone at least.

Thanks again :)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0