|
|
|||||||
| Home | Register | Downloads | FAQ | Members List | Calendar | Arcade | Mark Forums Read |
» Less advertising throughout
» Post and participate in discussions
» Network with other forum members
» Free private messaging
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Effulgent Plasma Chemist
![]() ![]() ![]() ![]() Join Date: Jul 2001
Posts: 518
|
Loading files filtered by extension
I am in need of a bit of advice and/or guidance. My google searches thus far have been fruitless, pointing only to C# tutorials of an excessively complicated nature. I have an executable, and that executable loads a .png file on start up. That file is loaded by a specific name, and has to be in the executables directory. What I would like to do is scan the exe's directory on init, looking for any .png files. Load the first one found by default, and allow for 'flipping' through all the .pngs in the directory. I am using C++ with the SDL library. Suggestions, pointers, advice, links?
|
|
|
|
| Advertisement | [Remove Advertisement] | ||
|
|
|
#2 |
|
Registered User
![]() ![]() Join Date: Oct 2011
Posts: 189
|
GetModuleFileName and random shell functions to get the path out of it, then FindFirstFile/FindNextFile same with opendir for unix or boost's filesystem for possibly both |
|
|
|
|
|
#3 | |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,119
|
Quote:
such a Job in C# takes around 1 line of code or 2 if you want it to look better ![]() UPDATE: I helped Omegadox a while back to do something like that in C# and all required is this: Code:
string[] filters = new string[] {".z64", ".n64", ".v64", ".zip"};
var files Directory.GetFiles(dirpath, "*.*", SearchOption.AllDirectories).Where(u => filters.Contains(Path.GetExtension(u.ToLower()))).ToList());
Code:
var files = Directory.GetFiles(Environment.CurrentDirectory, "*.png", SearchOption.AllDirectories); ![]() Here the reference Posts(check post #202 and #203)
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; March 5th, 2013 at 21:00.. |
|
|
|
|
|
|
#4 |
|
Effulgent Plasma Chemist
![]() ![]() ![]() ![]() Join Date: Jul 2001
Posts: 518
|
Yea, regarding the c# stuff I found, more like 20-30 lines of codes that recourse through directories and sub-directories and lots of other things that are worthless to me. Regardless, I am using c++, not c#. I have heard quite a bit of mixed opinions regarding c#. Thanks for the suggestions guys, gives me a starting point to do some research, learn, and create. :-) |
|
|
|
|
|
#5 | |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,119
|
Quote:
Basically C# is the closest language to C++ and while C++ is powerful in many Areas C# is far better in others. I could start a rant over C++ annoyances and a huge post about it's awesomeness but that doesn't make the language worst or better. The same goes for C#... it has some annoyances but is incredibly powerful when used propertly and in most cases unlike C++ it saves you a lot of time and annoyances of the language itself. Again... not many People actually care to properly learn C# and those are the first ones to start writing how bad the language is. For instance let's say you are a advanced C++ coder and tried C# based on those horribly made tutorials... it's very understanable that you will get annoyed by seeing a solution where you have to write 20-30 lines of codes just to accomplish a very Basic Task... now imagine you start ranting about how bad the language is and never realize that you just got it wrong and that such a solution was as simple as the one i wrote on my post(hope you get the Point). Some are concern about Speeds and at a very few Areas they are right. However when done propertly you can get amazing Speeds and achieve similar or same results as C++ using far less code and spending far less time. Here a very small example of an application i wrote in pure C#: If a beginner write such a application the result would probably be a stuttering, resource hungry application that probably doesn't work at all since i'm pushing Frames myself on that Special case. The main difference between C++ and C# is that a beginner would be easily able to achieve that even getting bad results at the end while in C++ the same Person won't probably pass the hello world level. I'm not saying by any means that C++ is bad as that would be a lie nor trying to make you Switch from C++ to C# as that is not my goal but saying that you shouldn't listen to all the misinformation being spread this days and try to find out things by yourself
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; March 4th, 2013 at 01:36.. |
|
|
|
|
|
|
#6 |
|
Effulgent Plasma Chemist
![]() ![]() ![]() ![]() Join Date: Jul 2001
Posts: 518
|
Hmm, fair enough. I'll not write off c# just yet. If the advice of several indie devs is any clue, saving time and effort to produce the identical result is the best sort of optimization. I have been listening to a lot of advice lately, think I will condense my thoughts into a blog post here in a few days to get it all summarized and recorded. Again, thanks for the help. :-) |
|
|
|
|
|
#7 |
|
Effulgent Plasma Chemist
![]() ![]() ![]() ![]() Join Date: Jul 2001
Posts: 518
|
Ok, still researching but, I have found 3 possible ways to achieve this: 1. windows.h 2. boostfilesystem 3. dirent.h Boost seems like the most noob-friendly once installed, but I can't use it since it is incompatible with minGW, which is the compiler I am using. Since my compiler isn't POSIX compatible anyways, seems like the windows functions are the best choice. However, the windows examples (and particularly the microsoft webstie) seem excessively complicated to me. They don't seem concerned with catering to the noobs. Dirent.h I dont know much about because I was going to use boost, then windows. More and more learning, la de da. Maybe time for a beer and some virtual violence. ;-) EDIT: I just noticed that I have ended every post with a non-forum smiley. Except not anymore, this one ends in an edit! *resists urge to place smiley here* |
|
|
|
|
|
#8 |
|
Effulgent Plasma Chemist
![]() ![]() ![]() ![]() Join Date: Jul 2001
Posts: 518
|
Ugh, so tired. Here is what I have so far: Code:
std::string openfiles()
{
std::string imagefiles;
LPCTSTR lpFileName = "*.png";
LPWIN32_FIND_DATA lpFindFileData = NULL;
FindFirstFile(lpFileName, lpFindFileData);
imagefiles = lpFindFileData->cFileName;
return imagefiles;
}
//somewhere else
case SDLK_TAB: tiles = load_image(openfiles());
Oh, I realize this wont find subsequent files, but figured I should try to just get it working with a single file first. ... Just noticed a serious bug in the reloading image function, fixed, re-upping now. |
|
|
|
|
|
#9 |
|
Effulgent Plasma Chemist
![]() ![]() ![]() ![]() Join Date: Jul 2001
Posts: 518
|
Bloody hell, finally got something working. Using dirent.h: Code:
int directoryloader()
{
DIR *pngdir = NULL;
pngdir = opendir(".");
struct dirent *pent = NULL;
std::string extension = ".png";
while (pent = read//////////dir(pngdir))
{
imagefiles[filecounter] = pent->d_name;
if (imagefiles[filecounter].find(extension, (imagefiles[filecounter].length() - extension.length())) != std::string::npos)
{
filecounter ++;
}
else
{
imagefiles[filecounter] = "";
}
}
closedir(pngdir);
return (filecounter-1);
}
Loads all the files with ".png" extension up in an array of strings, and returns the number of elements of the array that have data in them. Don't have it fully implemented in ATV yet, but after it is and another feature I just thought of I will update it on my webpage. Yay for small victories! |
|
|
|
|
|
#10 |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,119
|
Almost forgot this thread, glad you got some results
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; March 15th, 2013 at 11:52.. |
|
|
|
|
|
#11 |
|
Registered User
![]() ![]() Join Date: Oct 2011
Posts: 189
|
(quick note: having a local vector and returning a pointer to it is a really really really bad idea, might want to pass it in via a reference)
|
|
|
|
|
|
#12 |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,119
|
Yeah, the idea is not to teach him better or super advanced ways but more that he get the idea and slowly improve itself. Just picking won't help him as he may not understand most of what we are trying to show him at the end and won't help him at all but looking for probably bad yet easy to understand ideas could have better results
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 |
|
|
|
|
|
#13 |
|
Registered User
![]() ![]() Join Date: Oct 2011
Posts: 189
|
Telling him not to do something that's wrong and will crash is not really picking, otherwise he already got all his answers in the first reply
|
|
|
|
|
|
#14 |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,119
|
Indeed but he didn't understood a bit hence why i was trying to look for simplier ways so that he can get the idea. Also i always choose pointers over references to implement algorithms and data structures but i agree that returning references is the better way
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 Last edited by @ruantec; March 15th, 2013 at 19:38.. |
|
|
|
|
|
#15 |
|
Registered User
![]() ![]() Join Date: Oct 2011
Posts: 189
|
I didn't mean returning a reference though, as that'd cause the same issue
|
|
|
|
|
|
#16 |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,119
|
I'm kinda confused now or i just misunderstood your post what issue do you mean??? a simple check can prevent issues don't know what you mean right now . Eitherway i was talking about a function that returns a list of the files found and as a general rule i always use references in function parameters and return types and pointers as mentioned on my previous post. Maybe i expressed myself wrong on my post and you misunderstood it?
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 |
|
|
|
|
|
#17 |
|
Registered User
![]() ![]() Join Date: Oct 2011
Posts: 189
|
Code:
vector<T> * f() {
vector<T> x;
return &x;
}
|
|
|
|
|
|
#18 |
|
Crazy GFX coder
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Dominican Republic/Austria
Posts: 8,119
|
Hmmmm seems like we're misunderstanding each other or i just expressed myself wrong... basically what i tried to explain was a very simple way that i believe he may easily comprehend and pretty much this: Code:
std::vector<std::string> GetFiles(std::string path)
{
std::vector<std::string> files;
//logic to fill the vector using the path parameter
files.push_back("MySuperFile");
return files;
}
Somewhere in the code:
std::vector<std::string> files = GetFiles("someDir");
std::vector<std::string>::iterator it = files.begin();
while (it != files.end())
{
//Do something with the object
}
__________________
![]() Current development tools: Visual C++.net, Visual C#.net Visual VB.net, Visual Webdeveloper.net Bloodshed Dev C++, Borland C++ Visual Basic 6 |
|
|
|
|
|
#19 |
|
Effulgent Plasma Chemist
![]() ![]() ![]() ![]() Join Date: Jul 2001
Posts: 518
|
lol, I have no idea what you two are talking about. ^_^ Regardless, I will continue to use the things mentioned in this thread as topics of study, so my genuine thanks for that.
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|