cout to console in SDL

Started by
40 comments, last by fastcall22 12 years, 5 months ago
Quote:Original post by Arenth
This might seem sort of obvious but use the freopen( "CON", "w", stdout ); call after sdl_init(); since SDL_init(); is the call that calls
freopens ("CON",w/e,"stdout.txt");.. It worked awesome for me....

Is that using mingw/dev-cpp?
Advertisement
It seems that the freopen method works but you have to call the IMG_Load function at least once...

A working example
#include <iostream>#include <SDL/SDL.h>#include <SDL/SDL_opengl.h>#include <SDL/SDL_image.h>using namespace std;  SDL_Surface *screen;  int init_sdl(){     freopen( "CON", "w", stdout );    SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);int  videoflags= SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_OPENGL|SDL_ASYNCBLIT;   screen = SDL_SetVideoMode(300, 300 , 32, videoflags);	glViewport( 0, 0,300, 300 );};int main_loop(){   SDL_Event event;    Uint8 *keystate;   int  done=0;       while(!done)   {   	cout<<"1"<<endl;      glClearColor (0.7f, 0.9f, 0.9f, 1.0f);      glClear (GL_COLOR_BUFFER_BIT |  GL_DEPTH_BUFFER_BIT);       SDL_GL_SwapBuffers( );          keystate = SDL_GetKeyState(NULL);            if ( keystate[SDLK_ESCAPE] ) {done=1;}      while ( SDL_PollEvent(&event) )      {         if ( event.type == SDL_QUIT )  {  done = 1;  }     } //while  } //while  return 0;}int dummy(){        SDL_Surface *img = IMG_Load("ab.bmp");};    int main(int argc, char *argv[]){      init_sdl();    main_loop();	  return 0;}

compiled as a 'win32 console' application
linker parameters
-lmingw32
-lopengl32
-lSDLmain
-lSDL
-lSDL_Image
-s
note that the dummy() function is not called.

Can anyone confirm that it works ?
Dolphins - The sharks of the sea.
That doesn't work for myself. :-(
I've just installed the newest version of SDL libraries and my previous code stopped working :/. But the following one does. Can you give it a try ?

#include <iostream>#include <fstream>#include <SDL/SDL.h>#include <SDL/SDL_opengl.h>using namespace std;  SDL_Surface *screen;  ofstream ctt("CON");  int init_sdl(){     SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);    freopen( "CON", "w", stdout );    freopen( "con", "w", stderr );    screen = SDL_SetVideoMode(300, 300 , 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_OPENGL|SDL_ASYNCBLIT);    glViewport( 0, 0,300, 300 );    ctt.close();	};int main_loop(){   SDL_Event event;    Uint8 *keystate;   int  done=0;       while(!done)   {   	cout<<"cout"<<endl;   	cerr<<"cerr"<<endl;      glClearColor (0.7f, 0.9f, 0.9f, 1.0f);      glClear (GL_COLOR_BUFFER_BIT |  GL_DEPTH_BUFFER_BIT);       SDL_GL_SwapBuffers( );          keystate = SDL_GetKeyState(NULL);            if ( keystate[SDLK_ESCAPE] ) {done=1;}      while ( SDL_PollEvent(&event) )      {         if ( event.type == SDL_QUIT )  {  done = 1;  }     } //while  } //while  return 0;}  int main(int argc, char *argv[]){      init_sdl();    main_loop();	  return 0;}
Dolphins - The sharks of the sea.
That works for myself using [dev-cpp 4.9.9.1] and [SDL 1.2.4].


edit: C source-code

#include <SDL/SDL.h>#include <stdio.h>SDL_Surface *screen;FILE * ctt = fopen("CON", "w" );  int init_sdl(){     SDL_Init( SDL_INIT_VIDEO );    freopen( "CON", "w", stdout );    freopen( "con", "w", stderr );    screen = SDL_SetVideoMode(300, 300 , 32, SDL_HWSURFACE|SDL_DOUBLEBUF );    fclose( ctt );	};int main_loop(){	SDL_Event event; 	Uint8 *keystate;	int  done=0;	while(!done){		   		fprintf( stdout, "stdout\n" );		fprintf( stderr, "stderr\n" );		keystate = SDL_GetKeyState(NULL);  		if ( keystate[SDLK_ESCAPE] ){			done = 1;		}		while ( SDL_PollEvent(&event) ){			if ( event.type == SDL_QUIT ){				done = 1;			}		} //while	} //while	return 0;}  int main( int argc, char *argv[] ){	init_sdl();	main_loop();	return 0;}


Linker options: -lmingw32 -lSDLmain -lSDL -liberty -s

[Edited by - Easca on February 14, 2005 9:19:32 AM]
Quote:Original post by happybara
I've just installed the newest version of SDL libraries and my previous code stopped working :/. But the following one does. Can you give it a try ?

*** Source Snippet Removed ***


That works for me too! As well as the C version Easca has put as well! I've figured out what does the trick, but it's not something I can understand. I did not use the same linker line, just the code.

Those two lines:
FILE * ctt = fopen("CON", "w" );fclose( ctt );


Are what made it work for me. I tried removing them and the program did not work as has been happening all along. Thanks to you two for posting that! Now my Dev-CPP works at least in console mode. This is something I will have to take a look into to see why this is a problem on Dev-CPP and not in Windows. Much appreciations for figuring this one out.

- Drew
OK from a quick scan through some documents ( C++ reference: cstdio: freopen:, MSDN and the UNIX manual page for freopen) I noted this little snippet:

Quote:
stream
pointer to open file that has to be reopened.


So it appears that using devcpp/mingw the stdout/CON isn't opened so cannot be reopened. I'm guessing VC++ opens this file (CON) (and sdl or vc++ don't close it) allowing the freopen to work.

Another insignificant thing I noticed:

    FILE * ctt = fopen("CON", "w" );    foo = (int)freopen( "CON", "w", stdout );    bar = (int)freopen( "con", "w", stderr );    freopen( "stdout.txt", "w", stdout );    printf( "foo = %d, bar = %d, %d\n ", foo ,bar );    screen = SDL_SetVideoMode(300, 300 , 32, SDL_HWSURFACE|SDL_DOUBLEBUF );    fclose( ctt );	


With the first line commented out "foo" and "bar" have the values 0 and 0 or NULL and NULL.
Quote:Original post by Easca
OK from a quick scan through some documents ( C++ reference: cstdio: freopen:, MSDN and the UNIX manual page for freopen) I noted this little snippet:

Quote:
stream
pointer to open file that has to be reopened.


So it appears that using devcpp/mingw the stdout/CON isn't opened so cannot be reopened. I'm guessing VC++ opens this file (CON) (and sdl or vc++ don't close it) allowing the freopen to work.

Another insignificant thing I noticed:

*** Source Snippet Removed ***

With the first line commented out "foo" and "bar" have the values 0 and 0 or NULL and NULL.


Thank you for taking a look into that. However, I think the problem is not with devcpp/mingw, for my console works fine wit it. It's with SDL. Someone has made a mistake or something on the Dev-CPP release of SDL, so that it messed with those streams. I know this because I can start a new project and have everything work fine. As soon as I link in SDL though, everything goes FUBAR'd.

Thank you for your efforts in this manner! They are appreciated.

- Drew
...and this trick works for me too! Thank you all guys! Don't know why, but I felt that recompiling all was not the only solution, so I didn't do it ;-)
http://www.gamedev.net/community/forums/topic.asp?topic_id=485639

This topic is closed to new replies.

Advertisement