Sign in to follow this  
Followers 0
cignox1

cout to console in SDL

41 posts in this topic

Quote:
Original post by cignox1
Quote:

freopen( "stdout.txt", "wt", stdout );
freopen( "stderr.txt", "wt", stderr );


Uh, I thought that it would have been opened from the program without specific instructions...


No, your right. That is what SDL does auto. One last thing to try before I say throw in the towel. happybara's first post mentioned this, but maybe it was the use of the parameters that made it fail. Instead try using:
freopen( "CON", "w", stdout ); 


As you can see, there is no 't'. I got that idea from this MSDN Article. If that does not solve your problems, well then I'm sorry, I guess there is nothing you can really do. Best of luck!

- Drew
0

Share this post


Link to post
Share on other sites
Thank you again... but guess what? It doesn't work! I always hated console interfaces, I never liked to use them, and I always had to work with them... once that I need a simle output on a console, there is no way to make it work!
Allright, I will solve this problem in the future. First I have to being able to invert a matrix, but I already opened a topic about that.
0

Share this post


Link to post
Share on other sites
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....
0

Share this post


Link to post
Share on other sites
added to the SDL FAQ since i see it a lot

http://www.libsdl.org/cgi/docwiki.cgi/FAQ_20Console

worked for you guys? AFTER SDL_Init?
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Madster
added to the SDL FAQ since i see it a lot

http://www.libsdl.org/cgi/docwiki.cgi/FAQ_20Console

worked for you guys? AFTER SDL_Init?


It does not matter to me really since I am not a Dev-CPP user, but it did not work for me. I think it may be of the DevPak that I am using. I will try updating my SDL from a non-3rd party source.

#include <SDL/SDL.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

/* The screen surface */
SDL_Surface *screen = NULL;

int main(int argc, char *argv[] )
{
char *msg;
int done;

// Initialize SDL //
if (SDL_Init (SDL_INIT_EVERYTHING) < 0)
{
sprintf (msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());
free (msg);
exit (1);
}
atexit (SDL_Quit);

// Set 640x480 16-bits video mode //
screen = SDL_SetVideoMode (640, 480, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL)
{
sprintf (msg, "Couldn't set 640x480x16 video mode: %s\n",
SDL_GetError ());
free (msg);
exit (2);
}

freopen( "stdout.txt", "w", stdout );
freopen( "stderr.txt", "w", stderr );

cout << "Test" << endl;
printf("Test");

done = 0;
while (!done)
{
SDL_Event event;

// Check for events //
while (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
done = 1;
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
}

return 0;
}








[edit] Yea it still doesn't work. Once I add SDL to a new project, the project stops working correctly. I do not know why this is happening. I will try one more thing though.

I even tried a reinstall of Dev, then I tried 1.2.7 first, then 1.2.8 and still couldn't get it to work. Sheesh what's going on. I guess it has to be something we are doing if it works for you guys fine.

[Edited by - Drew_Benton on February 13, 2005 11:00:05 PM]
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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 ?
0

Share this post


Link to post
Share on other sites
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;
}


0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
...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 ;-)
0

Share this post


Link to post
Share on other sites
I'm also looking to use cout in my SDL project. Including the lines

freopen("CON", "w", stdout );
freopen("CON", "w", stderr );

works while running. I'm working in code blocks and I check console application and pause after execution. The problem is when I try to exit, I get what I think is a seg fault, "Process terminated with status -1073741510". I guess this corresponds to a seg fault, according to my google searches. Sometimes it crashes completely. I've tried moving the freopen to before and after SDL_Init() and I've tried including fclose( stdout ) and fclose (stderr) at various places. Am I missing something? Is there a safe way end the program once you override SDL using freopen? I'm not too sure exactly what freopen does, anyway. Thanks.
0

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  
Followers 0