Another way to disable stdout redirection

Hi All,

I’m using SDL with MingGW (gcc 3.2.3) under Windows 98
SE.
There’s a way to disable stdout redirection that isn’t
mentioned in the SDL FAQ for Windows:
http://www.libsdl.org/faq.php?action=listentries&category=4#85

I’m curious if it works for people using other
compilers or operating systems.

<main.c>
#include “pmain.h”

int main(int argc, char *argv[]) {
return(pmain(argc, argv));
}
</main.c>

<pmain.h>
#ifndef PMAIN_H
#define PMAIN_H

int pmain(int argc, char *argv[]);

#endif
</pmain.h>

<pmain.c>
#include “SDL.h”
#include “pmain.h”

/* pseudo main function */
int pmain(int argc, char *argv[]) {
}
</pmain.c>

Instead of main(), you now use pmain() as your main
function.
This code disables stdout redirection on my system.

How it works:

  • if SDL.h is included (either directly or indirectly)
    in the source file that contains main(), then stdout
    is redirected to stdout.txt (as it should be)
  • if SDL.h is not included (neither directly or
    indirectly) in the source file that contains main(),
    then stdout redirection is disabled

This little trick was discovered by the people working
on Nuvie:
http://nuvie.sf.net–
Tim Carlsen


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

I was reading ffplay.c for the FFMPEG library, and it uses SDL to
display a decompressed movie.

At the begining of that file there is this bit of code:
/***/
#include <SDL.h>
#include <SDL_thread.h>

#ifdef MINGW32
#undef main /* We don’t want SDL to override our main() /
#endif
/
**/

And after that, the definition of main() is done the normal way:
/*/
int main(int argc, char **argv){ /
[…main code here…]
/ }
/***/

Now this seems specific to MINGW32 enough and I wonder what motivates
this. I just read through the file src/main/win32/SDL_win32_main.c and
I believe what this to avoid the arguments reading, and error messaging
(where it uses windows message boxes and ffplay might want to display
errors another way).

But it also removes the definition of the stdout.txt and stderr.txt,
they become undefined i believe (unless they are defined to something else)

Reading more in this file gave many occurences of: #ifndef
NO_STDIO_REDIRECT

And I believe this is the correct way to disable these output files.
However this method seems to require a recompile of SDL, to make a
version of SDL that is a bit less suitable for development, but more
suitable for a final product. Your way of doing things seems a bit
tortuous and I wonder if it’s not just simpler to #undef main like in
ffplay’s example above.

Disabling sdl’s main will remove this code:
/**/
/
Redirect standard input and standard output /
newfp = freopen(stdoutPath, TEXT(“w”), stdout);
/and/
newfp = freopen(stderrPath, TEXT(“w”), stderr);
/
**/

Of course it also disables many other things specific to windows and I
wonder how it can affect the rest of the program’s functions. So this
should give a few good pointers for exploration, however, make sure to
understand how experimental this exploration will be! :wink:

Simon

Tim Carlsen wrote:> Hi All,

I’m using SDL with MingGW (gcc 3.2.3) under Windows 98
SE.
There’s a way to disable stdout redirection that isn’t
mentioned in the SDL FAQ for Windows:
http://www.libsdl.org/faq.php?action=listentries&category=4#85

I’m curious if it works for people using other
compilers or operating systems.

<main.c>
#include “pmain.h”

int main(int argc, char *argv[]) {
return(pmain(argc, argv));
}
</main.c>

<pmain.h>
#ifndef PMAIN_H
#define PMAIN_H

int pmain(int argc, char *argv[]);

#endif
</pmain.h>

<pmain.c>
#include “SDL.h”
#include “pmain.h”

/* pseudo main function */
int pmain(int argc, char *argv[]) {
}
</pmain.c>

Instead of main(), you now use pmain() as your main
function.
This code disables stdout redirection on my system.

How it works:

  • if SDL.h is included (either directly or indirectly)
    in the source file that contains main(), then stdout
    is redirected to stdout.txt (as it should be)
  • if SDL.h is not included (neither directly or
    indirectly) in the source file that contains main(),
    then stdout redirection is disabled

This little trick was discovered by the people working
on Nuvie:
http://nuvie.sf.net


Tim Carlsen


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi,

Does SDL support dual screen applications? If not, will SDL 1.3 have this functionality?

Thanks
Ed---------------------------------
All new Yahoo! Mail “The new Interface is stunning in its simplicity and ease of use.” - PC Magazine

Edward Byard <e_byard at yahoo.co.uk> wrote:

Does SDL support dual screen applications?

no.

If not, will SDL 1.3 have this functionality?

yes.

clemens