PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

API for getting application directory?

Using GapiDraw, it helpfully loads relative-path files from the application directory, when calling CGapiSurface::CreateSurface.

I would like to also load some of my data files from the application directory.

Is there a GapiDraw API that returns that directory for me, so I can load other files from there?

That would be best, since then I am sure whatever algorithm looks for the application directory for my image files, works the same for my other data files.

How do people normally do this?
Hi there,

What you want is GetModuleFileName().

It's a Win32 function and it returns the path+filename of the program you are running. You simply have to remove the filename and you have the directory where the application is.

--Eric
mlepage · 03 Aug 2003, 21:17 UTC — OK
OK, I assume that works, but if GapiDraw is already doing it, and I'm already using GapiDraw, then why can't GapiDraw expose that functionality in another API? Then I could reuse that code instead of rewriting it myself.
Are you that lazy that you cant write a simple stripping function?
mlepage · 04 Aug 2003, 00:59 UTC — It's not about laziness.
It's not about laziness. It's about having one function to do one thing. If two functions try to do it, they do it differently.

And this is a case in point. I used GetModuleFileName and stripped off the last bit. It works fine, except THAT'S NOT WHAT GapiDraw IS DOING.

My project is a copy of the sample Minimal. When I run the PC build via F5 from Visual Studio, my function correctly says the application myapp.exe is in directory samples\myapp\xp\Debug. But CGapiDraw::CreateSurface looks in samples\myapp\xp.

(Incidentally, that's where a file is created if I don't give it a path. I assume it's the current working directory or something when running from Visual Studio. But GapiDraw's documentation says it looks where myapp.exe is, so it is wrong.)

When I run myapp.exe from File Explorer, both my function and GapiDraw correctly look for files in samples\myapp\xp\Debug.

So, anonymous, don't be sarcastic or presumptuous. We're talking about software engineering here. Two bits of code have the potential to yield different results. And in this case they do.
Anonymous, are you that cowardly that you can't log in and post as yourself?

(See, it doesn't feel too good when it's coming at you, does it?)

mlepage is making a good point... A good programmer can write the code himself, an excellent programmer copies the code or otherwise finds an existing version of what he needs.

The main reason for the existence of object-oriented techniques is in fact code reuse (not the only reason of course, but this is usually sited as the #1 reason it's a good thing). And that doesn't even go into what he's talking about, which is the potential for different outcomes from the "same" code. If he's getting different results, it's possible his code is broke and he could have saved time if the API was exposed... Or it could be a flaw in the GapiDraw code, in which case I think Johan would be greatful for someone pointing it out.

Asking the question he asked doesn't make him lazy, it makes him a programmer that knows he shouldn't try re-inventing the wheel every chance he gets, and that's a good thing.
Actually Anon was me, I too have the same problems where it looses my login as you do Fzam.

I am not hiding behind anything, but I often see posts demanding stuff of GapiDraw when it can be easily done by the person themself. Its not like coding the getPath instruction is more than 10 lines of code.

If someone cant handel "reinventing" 10 lines of code, they sure as heck should not be trying to make games. Why make a clone of a game, Its allready been done, and its reinventing the wheel?

I was just venting, since I constantly see posts about people begging SBL to impliment clipping and some various function that are absolutly trival. Now that clipping has been added, I will use it, but I didnt NEED it.

I also believe there becomes a point where you distance yourself soo far from the base code, that you dont even know what the hecks going on. I personally have written blitters, which do rotation/scaling and a few other various effects, but I choose to use GapiDraw since its such a wonderful feature set.

While I am abstracted with GapiDraw, I know whats going on at the base level. I dont think everyone needs to know, but when your complaning about a simple function that wont even take you a total of 5 min to impliment, I get a little miffed.

I entirely welcome questions, and creative thinking.
Well, I had a feeling it was one of the regulars that lost their logon, good thing I didn't go off too much :) Ironically, that problem for me seems to have gone away. Don't ask how or why, I haven't the slightest clue!

Obviously my coward comment doesn't apply to you, that was on the chance that it wasn't a regular.

In any case, I agree with you to an extent. I'm like you it seems in that I tend to WANT to do things myself, if for no other reason than to know every detail of what's going on. I frankly have to fight myself at work all the time, where all I do is Java all day, to actually look through the class libraries and other sources of code before diving in and writing every little thing myself. It's a little different when your being paid to code I think, but it's still true outside work.

I think the original point, which I think is valid, is that since GD is doing this already, exposing the functionality probably isn't a big deal, and if it saves us five minutes and takes Johan 30 seconds to implement it, then it's not a bad request. I think it's obviously something that should fall to the bottom of the long list, but it's not a bad request.

I also agree with you that people DO tend to come here sometimes and complain about things and "request" things be added that aren't really important, relatively speaking. I totally understand your need to vent sometimes :) But I didn't think mlepage was complaining at all. Granted, he probably could have written the code in the time it took to read the replies and type his own :)

Ah, in any case, I have some code to write myself, and this isn't getting that done either I suppose ;)
mlepage · 04 Aug 2003, 03:46 UTC — Reexplaining
No worries guys. But let me explain the reasons again.

It's not laziness or inability. It's simple software engineering. Right now, I have two functions which attempt to do the same thing.

One function is mine, and seems to work as advertised. The other is hidden without an API, and doesn't seem to work as advertised by the documentation.

So these functions may differ in their results. In fact, I have found that they *do* differ in their results, when running my game from within Visual Studio.

So, I am risking putting my game in a position where it can find its configuration files, but not its image files, under some circumstances.

So, to properly manage this risk, I am forced to simply always use my function to obtain an absolute path, and always pass an absolute path into the GapiDraw image loading routines.

Don't you find it odd that I am forced to reimplement something that is in GapiDraw, and not use GapiDraw's version at all?

Now consider this: GapiDraw probably already has this API internally. It just needs to be exposed for our use. Then, I can remove my implementation of the same functionality. Then, even if that function somehow fails, at least my game is looking for everything in one place, and not two places.

I'm not asking for a feature that isn't already in GapiDraw. It's a low-hanging fruit. It probably only requires an entry in a header file.

It could be worse, I could be asking for const correctness. :-)
jheez...

I use GetModuleFileName() in all my projects, and I never had a single problem. Not on the desktop, not on Toshiba or iPaqs, and not from running in Visual Studio or eVC.

If some of your files cannot be found when you run the application from VS, check where your .EXE is created. Move it to your root folder or something.

And DillRye is right, this function is short. Mine has 4 lines plus the final "return S_OK". It gets the module name and get rids of the filename.

This is not brain surgery, this is making a freaking function.

I can't believe I'm still writing...

--Eric
mlepage · 04 Aug 2003, 05:23 UTC — Functions
I can't believe I'm still writing either!

I already wrote a function using GetModuleFileName. It is short. It does indeed work properly.

Now go here: http://www.gapidraw.com/doc/204/doc-surface-main.htm#CreateSurface

It says:
GapiDraw will use the application path of your executable file and append the image file name (e.g. if your executable is "\Program Files\Games\MyGame\MyGame.exe" and you call CreateSurface with pImageFile set to "MyPicture.png", GapiDraw will try to load the image as "\Program Files\Games\MyGame\MyPicture.png").


That is wrong! When run from F5 in Visual Studio, GapiDraw will pick the directory one level up from the executable, while the GetModuleFileName method picks the correct directory.

I suspect that this is because GapiDraw uses another method, such as getting the current working directory, which yields different results under those circumstances.

So do you understand? GapiDraw has a bug in either its implementation or its documentation. I have already demonstrated a case where GapiDraw fails to perform as documented.

Try to understand, I'm not a newbie. I have been programming professionally for over seven years, from enterprise software to data mining to machine learning, on Mac/Unix/Windows/Palm/PocketPC, in C/C++/Java. I'm not an idiot, nor am I lazy.

So yes, I can write my own function, but that doesn't change the fact that there is a problem with GapiDraw (either implementation or documentation). Notwithstanding whether that function is ever exposed in the API. No amount of encouraging me not to be lazy is going to make this issue go away.

In the kind of software I am used to writing, it would be a serious issue if there were two functions to find a location for files. You can't have a server get half its files from one directory, and half from another, unexpectedly, under odd circumstances. If that happened, I would identify it as a problem to be fixed.

In this case, I am forced to write my own function, to use it to load my own files, and to also use it to create absolute paths for GapiDraw to load images, since I cannot rely on GapiDraw's functionality to find those files properly, as documented, under all circumstances.

Explain to me again why this isn't a problem?
Actually I think it is visual studio that is giving you problems. In all my Windows apps that I've written, the .exe is always run from the project-folder, not the release or debug folder. I think they do that with setting the 'working dir'. I guess GapiDraw does a 'GetCurrentDir' when starting and when you use VStudio (thus a Win32 app) you get as a result the project folder. GetModuleFilename actually returns the Debug/Release folder since the .exe is located there.

So if you wan't to be sure you get the correct folder, use GetModuleFilename and strip the exe name.


Maurice
mlepage: GapiDraw uses the currently "active" directory to load images from a file. Which is why it will try to load images from the "\xp" folder when you start it with "F5" and not "\xp\debug".. Thanks for pointing out the error in the documentation, I'll fix it when I get back from my vacation.

I am thinking of moving all image loading functions (all CreateSurface except the widthxheight one) to a separate library for the next GapiDraw release.. It will loose over 100kb from the GapiDraw DLL, and it will provide an easier starting point for porting GapiDraw to other platforms.. Many people use their own image loaders from resource bundles anyways...
mlepage · 04 Aug 2003, 14:37 UTC — As suspected
So it's as I suspected. Thanks sbl. I'll just use my function for now. I am using your image loader, so hopefully it'll make it into another library and not disappear completely!