Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How can i use console in C when doing graphics using SDL

Reply
Thread Tools

How can i use console in C when doing graphics using SDL

 
 
smartbeginner
Guest
Posts: n/a
 
      09-26-2006
I cannot view in the console what all i have included in printf() .Why
this?
Code:
#include<stdio.h>
#include<conio.h>
#include "SDL/SDL.h"
int main(void)
{
int x=1;
SDL_Event event;
SDL_Init(SDL_INIT_JOYSTICK );
SDL_Joystick *joy;
// Check for joystick
if(SDL_NumJoysticks()>0){
// Open joystick
joy=SDL_JoystickOpen(0);
if(joy)
{
printf("Opened Joystick 0\n");
printf("Name: %s\n", SDL_JoystickName(0));
}
SDL_Quit();
SDL_JoystickClose(joy);
return 0;
}

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      09-26-2006
In article <. com>,
smartbeginner <> wrote:
>I cannot view in the console what all i have included in printf() .Why
>this?
>Code:
>#include<stdio.h>
>#include<conio.h>
>#include "SDL/SDL.h"


Neither conio nor SDL are part of the C standard, so in this newsgroup
we do not know what they do. In particular, as you have used <conio.h>
instead of "conio.h" you would be picking up a conio.h from the
implementation's headers, and the implementation might have done
something stupid such as redefining printf().

>int main(void)
>{
> int x=1;
> SDL_Event event;
> SDL_Init(SDL_INIT_JOYSTICK );
> SDL_Joystick *joy;
>// Check for joystick
> if(SDL_NumJoysticks()>0){
> // Open joystick
> joy=SDL_JoystickOpen(0);
> if(joy)
> {
> printf("Opened Joystick 0\n");
> printf("Name: %s\n", SDL_JoystickName(0));
>}
>SDL_Quit();
>SDL_JoystickClose(joy);
> return 0;
>}


This is not your real code. This code would not compile. The { on
the if(SDL_NumJoysticks()>0) line matches against the } after the
return 0, and there is no matching } for the { that begins the routine.

Notice that in your code, you only use printf() if you were able to
open a Joystick (whatever that means.) The reason you do not get
any output could be as simple as you not being able to open any
joysticks.

I question whether you should really be closing a joystick *after*
you Quit SDL -- doesn't Quit mean "get out of SDL", after which
point closing a joystick would not be valid?

Your code does not use the int x, but that would not cause any problems.

I note that your code is C99, not C89: you have a function call
(to SDL_Init) before the declaration of SDL_Joystick *joy, which
would not be valid in C89. The // comments are also not valid in C89
but are a common extension. Please be aware that there are very few
fully compliant "hosted" C99 implementations available, and it is fairly
unlikely that you are using one of them. Because of the non-compliance
of the environment, your code might do things that would not be expected
according to the C99 standard, and there isn't much you can do about
that except to study in detail exactly how your environment differs
from a true C99 environment. It is safer to write in C89.


Your code appears to have platform dependancies; in particular,
it appears to be Windows specific code. After fixing the above code
issues, if you continue to have difficulty, contact a newsgroup
that deals with your platform.

[Off topic]
As you are using Windows, are you sure the console isn't simply being
closed before you have a chance to read anything on it?
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-26-2006
(Walter Roberson) writes:
[...]
> I note that your code is C99, not C89: you have a function call
> (to SDL_Init) before the declaration of SDL_Joystick *joy, which
> would not be valid in C89. The // comments are also not valid in C89
> but are a common extension. Please be aware that there are very few
> fully compliant "hosted" C99 implementations available, and it is fairly
> unlikely that you are using one of them. Because of the non-compliance
> of the environment, your code might do things that would not be expected
> according to the C99 standard, and there isn't much you can do about
> that except to study in detail exactly how your environment differs
> from a true C99 environment. It is safer to write in C89.


Given that the program appears to be Windows-specific, and given
Microsoft's apparent lack of interest in supporting C99, it's more
likely that the OP is using a C++ compiler.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      09-26-2006
smartbeginner wrote:
> I cannot view in the console what all i have included in printf() .Why
> this?
> Code:
> #include<stdio.h>
> #include<conio.h>
> #include "SDL/SDL.h"
> int main(void)
> {
> int x=1;
> SDL_Event event;
> SDL_Init(SDL_INIT_JOYSTICK );
> SDL_Joystick *joy;
> // Check for joystick
> if(SDL_NumJoysticks()>0){
> // Open joystick
> joy=SDL_JoystickOpen(0);
> if(joy)
> {
> printf("Opened Joystick 0\n");
> printf("Name: %s\n", SDL_JoystickName(0));
> }
> SDL_Quit();
> SDL_JoystickClose(joy);
> return 0;
> }
>


To see the program output you should compile it as
a console application or use the windows API to open a console
under program control. Anyway, if you run it in a console window,
you should see the output. stdout and stderr are not initialized
correctly in windows applications since there is no "screen"
where they should be redirected to.

ANOTHER REAL POSSIBILITY is that obviously your
SDL_JoystickOpen(0) call failed and you do not see
anything because you did not consider the failure case

Rewrite your code so that there is
if (joy) {
}
else {
printf("Did not work!\n");
}
 
Reply With Quote
 
smartbeginner
Guest
Posts: n/a
 
      09-27-2006
/*
Example is directly from the SDL documentation....
The change made by me caused a console window to be opened
But what all i wrote in printf() wasn't appearing
I desperately tried fprintf() then also not working..
How can I achieve my goal..
Any tutorials or links giving the help
*/
#include<stdio.h>
#include "conio.h"
#include "iostream.h"
#include "SDL/SDL.h"
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "SDL Start";

int main(int argc, char **argv)
{
printf("\n Entering to code");
fprintf(stdout,"Hi");
int x=1;
SDL_Event event;
SDL_Init( SDL_INIT_VIDEO );
SDL_Init(SDL_INIT_JOYSTICK );
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH,
WINDOW_HEIGHT, 0,
SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption( WINDOW_TITLE, 0 );
SDL_Joystick *joy;

if(SDL_NumJoysticks()>0){
joy=SDL_JoystickOpen(0);

if(joy)
printf("Opened Joystick 0\n");
else
printf("Couldn't open Joystick 0\n");
}

while(x)
{
if (SDL_PollEvent(&event))
{
if(event.type==SDL_QUIT)
x=0;
if (event.type==SDL_KEYDOWN)
{
SDLKey keyPressed = event.key.keysym.sym;
switch (keyPressed)
{
case SDLK_ESCAPE:
x=0;
break;
}

}
}
}
SDL_Quit();
SDL_JoystickClose(joy);
printf("Can I now be active");
return 0;
}

 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      09-27-2006
smartbeginner wrote:
> /*
> Example is directly from the SDL documentation....
> The change made by me caused a console window to be opened
> But what all i wrote in printf() wasn't appearing
> I desperately tried fprintf() then also not working..
> How can I achieve my goal..
> Any tutorials or links giving the help
> */

I can think of two things to do:
1. Ask in a forum that deals with SDL and/or Windows, as this newsgroup
does not. Perhaps the SDL site have some pointers.

2. Install a *nix variant, as there you can still output to a console
when using SDL.
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      09-27-2006
Nils O. Selåsdal wrote:
> smartbeginner wrote:
>> /*
>> Example is directly from the SDL documentation....
>> The change made by me caused a console window to be opened
>> But what all i wrote in printf() wasn't appearing
>> I desperately tried fprintf() then also not working..
>> How can I achieve my goal..
>> Any tutorials or links giving the help
>> */

> I can think of two things to do:
> 1. Ask in a forum that deals with SDL and/or Windows, as this newsgroup
> does not. Perhaps the SDL site have some pointers.
>
> 2. Install a *nix variant, as there you can still output to a console
> when using SDL.

Oh, there's a 3. workaround way too.
freopen stdout to a file, and look at the file rather than a console.
Or redirect stdout to a file by other means
 
Reply With Quote
 
Nelu
Guest
Posts: n/a
 
      09-27-2006
Nils O. Selåsdal wrote:
> smartbeginner wrote:
>> /*
>> Example is directly from the SDL documentation....
>> The change made by me caused a console window to be opened
>> But what all i wrote in printf() wasn't appearing
>> I desperately tried fprintf() then also not working..
>> How can I achieve my goal..
>> Any tutorials or links giving the help
>> */

> I can think of two things to do:
> 1. Ask in a forum that deals with SDL and/or Windows, as this newsgroup
> does not. Perhaps the SDL site have some pointers.
>
> 2. Install a *nix variant, as there you can still output to a console
> when using SDL.


<OT>
I believe the Windows API allows you to attach a console to an
application if it detached at execution. Clearly, a better answer
can only come from a Windows group.
</OT>

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
SDL and best way of using rotating sprites Vampyyrivaari@gmail.com C++ 2 09-16-2007 05:28 PM
Some error happends when I use SDL model.. CFC Ruby 0 02-17-2006 11:43 AM
Any SDL Users out there? I need some help. Petec C++ 2 04-17-2004 05:21 AM
C++ Building derived class from sdl string!HELP! K.S. C++ 5 08-20-2003 05:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57