Jump to content

  • Log In with Google      Sign In   
  • Create Account


cout to console in SDL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
41 replies to this topic

#1 cignox1   Members   -  Reputation: 723

Like
0Likes
Like

Posted 06 February 2005 - 10:57 PM

I find it very hard using the debugger provided with Dev-C++ as I cannot make him stop where I want and check the value of every variable, so I would like to print them on the console of course, not on the actual SDL window, but on the dos prompt). It happens that cout doesn't work: is there a way to get cout working as always? Thank you all!

Sponsor:

#2 happybara   Members   -  Reputation: 312

Like
0Likes
Like

Posted 07 February 2005 - 12:32 AM

try to put these lines in your code

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



#3 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
0Likes
Like

Posted 07 February 2005 - 04:06 AM

If that does not work, make sure you are outputting endl's at the end of the line. I opened up a test project and w/o them, I got no text, but after I added them, it worked fine.

- Drew

#4 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
0Likes
Like

Posted 07 February 2005 - 04:35 AM

Adding Console I/O to a Win32 GUI App.

#5 cignox1   Members   -  Reputation: 723

Like
0Likes
Like

Posted 07 February 2005 - 08:03 AM

Thank you all for replying:
happybara and Drew_Benton: don't know why, but I tried both the tips and it still doesn't work.. my fault?

Oluseyi: I'm not using the win32 api. The project was initially set up with the SDL template of Dev-C++ wich includes windows.h just in order to use MessageBox(). I would like to keep the code as portable as possible, using only c++ standard libraries... thank you anyway for the link, it may come handy someday.



#6 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
0Likes
Like

Posted 07 February 2005 - 08:59 AM

Ok then give C-styled output a try.


#include <stdio.h>

printf("This is a test!\n");
int val = 5;
printf("Value of val: %i\n",val);


And as a side note, you are outputting *before* the breakpoint correct?

- Drew

#7 sand_man   Members   -  Reputation: 100

Like
0Likes
Like

Posted 07 February 2005 - 11:20 AM

The above wont work because the stdout is no longer the console it is stdout.txt
I did see a solution to this problem not too long ago here somewhere, unfortunately i cant remember what it was :)

#8 cignox1   Members   -  Reputation: 723

Like
0Likes
Like

Posted 07 February 2005 - 08:19 PM


Quote:

And as a side note, you are outputting *before* the breakpoint correct?


I'm not debugging now, I'm just trying to make it work -sigh-



#9 Easca   Members   -  Reputation: 163

Like
0Likes
Like

Posted 07 February 2005 - 09:13 PM

Quote:
Original post by sand_man
The above wont work because the stdout is no longer the console it is stdout.txt
I did see a solution to this problem not too long ago here somewhere, unfortunately i cant remember what it was :)

One solution is to recompile SDL.

#10 Rhaal   Members   -  Reputation: 754

Like
0Likes
Like

Posted 08 February 2005 - 01:11 PM

Quote:
Original post by Easca
Quote:
Original post by sand_man
The above wont work because the stdout is no longer the console it is stdout.txt
I did see a solution to this problem not too long ago here somewhere, unfortunately i cant remember what it was :)

One solution is to recompile SDL.


I just clicked ahead a few posts from that link and this seems a little easier:

Quote:
Actually, all that needs to be done is to recompile with "NO_STDIO_REDIRECT"
defined in src/main/SDL_main.c (you can either modify the file or re-compile
SDL_main.lib by hand with -DNO_STDIO_REDIRECT, your mileage may varry)


#11 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
0Likes
Like

Posted 08 February 2005 - 01:14 PM

I cannot see why this is not working. Can you please post your code. It makes no sense that none of the suggestions have yet to work. What OS and IDE are you using? What version of SDL as well. My cout and printf work fine in Dev, VS6, and VS7, so there's one detail that is missing somewhere.

- Drew

#12 MrBeaner   Members   -  Reputation: 144

Like
0Likes
Like

Posted 08 February 2005 - 01:22 PM

Quote:
Original post by Oluseyi
Adding Console I/O to a Win32 GUI App.


thanks for the link.

#13 Easca   Members   -  Reputation: 163

Like
0Likes
Like

Posted 08 February 2005 - 10:50 PM

Quote:
Original post by Drew_Benton
I cannot see why this is not working. Can you please post your code. It makes no sense that none of the suggestions have yet to work. What OS and IDE are you using? What version of SDL as well. My cout and printf work fine in Dev, VS6, and VS7, so there's one detail that is missing somewhere.

- Drew

I don't get any output on the console. I didn't try the winapi redirects though.
I use dev-cpp 4.9.9.1 and SDL 1.2.4. The freopen works fine if I just put a arbituary filename and stops any output to stdout.txt using freopen( "CON", "wt", stdout ).


#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>


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

int main (int argc, char *argv[])
{
int done;
printf( "Hello World1\n");
/* Initialize SDL */
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
printf ("Couldn't initialize SDL: %s\n", SDL_GetError ());
exit (1);
}
atexit (SDL_Quit);
freopen( "CON", "wt", stdout ); //hello world2 doesn't get outputted
// freopen( "filename", "wt", stdout ); //hello world2 goes to filename
printf( "hello world2\n");

exit( 0 );
}





It is compiled as a console application (Win32 console).

#14 cignox1   Members   -  Reputation: 723

Like
0Likes
Like

Posted 09 February 2005 - 03:51 AM

Well, that's my code (just the basic stuff):

#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#include <iostream>

using namespace std;

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

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

freopen( "CON", "wt", stdout );
freopen( "CON", "wt", stderr );
printf("ciccio\n");


/* Initialize SDL */
if (SDL_Init (SDL_INIT_VIDEO) < 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);
}

SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);

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

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

return 0;
}



The Dev-C++ version is 4.9.9.1 with SDL version 1.2.4. Note that the program I tested was initially created by the Dev template, but I think that this makes no differences since parameters should be the same.

#15 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
1Likes
Like

Posted 09 February 2005 - 04:08 AM

I will take a look at the code, but I am sure the problem lies in the fact you are using SDL 1.2.4. However, you are free to use it, but take a look at all the updates made! I would strongly suggest using at least version 1.2.7, but once again, you are free to use whatever you want. I know I have 1.2.8 and my console functions work just fine, cout and c-style alike. I'd say download a more recent version, but do not install over your current, simply make a test project, and move the files into that project and see if you get the same errors.

Instead of linking SDL.lib and such, use relative path names - ie

#pragma comment ( lib, "C:\\sdl 1.2.7\\lib\\sdl.lib" )


That's just an example of what you can do. I think it would be better for whatever you are doing because of the serious bugs that are fixed as of 1.2.7, but you can determine that for yourself.

- Drew

#16 cignox1   Members   -  Reputation: 723

Like
0Likes
Like

Posted 09 February 2005 - 09:51 AM

First of all thank you for having point out that there are more recent versions of sdl... I'm so lazy that I trusted that if it were a new version, it would have been released as package for Dev-C++ (sometimes I feel stupid :-( ((Uh, this sentences were too complex for my bad english: I hope they are at least understandable))

Second: it still doesn't work. I will try others configurations (don't know if #pragma used to link libraries is supported by Dev-C++, I thought that it was VC++ specific).

@Easca: could you tell me if the problem was solved on your system by updating SDL? (...if you want to update it, of course)

Thank you all anyway!

#17 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
0Likes
Like

Posted 09 February 2005 - 10:48 AM

Ahh! [lol] I'm sorry, I got confused and thought that Easca was the OP. Ok for dev-cpp that #pragma will not work for the most part with SDL, so I would just copy the current version of SDL you have to a folder then install the new one. Then you can try your program again, since the linker libs will now be updated. Do not forget to update the .DLL in the system directory as well.

- Drew

#18 Easca   Members   -  Reputation: 163

Like
0Likes
Like

Posted 09 February 2005 - 11:20 AM

Ok at work I had sdl 1.2.4 I also assumed it would automatically give me the latest version through the package manager :-(.

Anyway at home I have SDl 1.2.7 installed and I also put in a copy of SDL 1.2.8 and both of them still print to stdout.txt.

#19 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
0Likes
Like

Posted 09 February 2005 - 12:59 PM

Ok I'd like to say sorry for not trying out my suggestions first. I was using VS6 and VS7 the whole time and it worked fine. I am thinking that the only way to fix this under Dev-CPP and MingGW is by recompiling per the response to the link that Easca posted. I tried the redirecting output myself and could not get it to work either. I then tried Oluseyi's link as well, but it failed the same. As of now that is all I can think of.

- Drew

#20 Easca   Members   -  Reputation: 163

Like
0Likes
Like

Posted 10 February 2005 - 12:52 AM

Ah, well I'll just put up with the stdout.txt. It would be nice to be able to do it without recompiling though.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS