Sign in to follow this  
Rhaal

Win32 Drag/Drop for argv[1]?

Recommended Posts

Rhaal    754
I have written a small program to convert .tga files to .bmp files for my Warcraft friends who can't afford Photoshop or use Google for the word "viewer". Anyways... Problem is that I'm converting argv[1] (the .tga image) to a .bmp file. Everything works if I open a command prompt and type "wowview whatever.tga". However, I want this to work via drag and drop. Should dragging an image onto an exe open that image as argv[1]? If not, what do I need to do for this functionality?

Share this post


Link to post
Share on other sites
Rhaal    754
Quote:
Original post by DerAnged
i beleive setting .tga to open defaultly with your converter will work, did for me.

I'm sorry, I don't quite follow.


#pragma comment (lib, "SDL.lib")
#pragma comment (lib, "SDLmain.lib")
#pragma comment (lib, "SDL_Image.lib")

#include <SDL/SDL.h>
#include <SDL/SDL_Image.h>
#include <string>
#include <iostream>

int main(int argc, char **argv)
{
// Image Surface.
SDL_Surface* Image;
// File Name.
std::string FileName;
// Resulting File Name
std::string Result;

// Output the welcoming message.
std::cout << "Welcome to WoWView by REMOVED." << std::endl;

// Set FileName to the first (and only) argument.
FileName = argv[1];
std::cout << "Converting... ";

// Create an SDL_Surface from the TGA image.
Image = IMG_Load(FileName.c_str());

// Set up the Result file name.
Result = (("wv_" + FileName));
int i = Result.find(".tga");
Result.replace((i--), 4, ".bmp");
std::cout << "DONE!" << std::endl;

// Save the BMP.
SDL_SaveBMP(Image, Result.c_str());


return 0;
}


Share this post


Link to post
Share on other sites
Rhaal    754
With further testing I've attempted to print argv[1] to a file and it never happens. It appears argv[1] is not getting assigned the file name that's dragged on the exe. I've looked around online and it appears that argv[1] SHOULD be the file name that was dragged. I'm baffled.

Share this post


Link to post
Share on other sites
kinetik_mjg    298
That style of drag and drop behaviour only seems to work if the program is built as a console application. SDL programs are normally built as GUI applications, and you'll probably find that your program isn't even making it to your main() when you attempt to drag a file on to the executable.

It looks like SDL's pre-main() setup code should handle running as a console application, but my quick experiment with this resulted in the console application exiting before reaching my main().

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Nice code! Needs a few improvments though [wink] Take a look at this, works for me:

#pragma comment (lib, "SDL.lib")
#pragma comment (lib, "SDL_Image.lib")

#include <SDL/SDL.h>
#include <SDL/SDL_Image.h>
#include <string>
#include <iostream>

#undef main

int main(int argc, char **argv)
{
// Image Surface.
SDL_Surface* Image;
// File Name.
std::string FileName;
// Resulting File Name
std::string Result;

// Output the welcoming message.
std::cout << "Welcome to WoWView by REMOVED." << std::endl;

if( argc < 2 )
{
std::cout << "Need's 1 argument!";
// Tell correct usage!
return -1;
}

// Set FileName to the first (and only) argument.
FileName = argv[1];
std::cout << "Converting... ";

// Create an SDL_Surface from the TGA image.
Image = IMG_Load( FileName.c_str() );
if( !Image )
{
std::cout << "File cannot be opened!";
// Some problem!
return -1;
}

// Set up the Result file name.
Result = argv[1];
Result = Result.substr( 0, Result.find_last_of("."));
Result += ".bmp";
std::cout << "DONE!" << std::endl;

// Save the BMP.
SDL_SaveBMP(Image, Result.c_str());

// At least let them see success ;)
system("pause");

return 0;
}




I just removed all use of SDL_Main as well as fixed a few other things. By all means use whatever you want, all you asked for was the drag-n-drop, but I noticed if you don't handle a few things, you get crashes. [smile]

- Drew

Share this post


Link to post
Share on other sites
Rhaal    754
Drew saves the day again :) I'm a little confused though. How do you eliminate SDL_Main? It must be linker settings because I don't see anything in the code that does it. Thanks!

Edit: Nevermind. I see it [rolleyes]

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