473,772 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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_IN IT_JOYSTICK );
SDL_Joystick *joy;
// Check for joystick
if(SDL_NumJoyst icks()>0){
// Open joystick
joy=SDL_Joystic kOpen(0);
if(joy)
{
printf("Opened Joystick 0\n");
printf("Name: %s\n", SDL_JoystickNam e(0));
}
SDL_Quit();
SDL_JoystickClo se(joy);
return 0;
}

Sep 26 '06 #1
7 3126
In article <11************ **********@e3g2 000cwe.googlegr oups.com>,
smartbeginner <rr******@gmail .comwrote:
>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_IN IT_JOYSTICK );
SDL_Joystick *joy;
// Check for joystick
if(SDL_NumJoyst icks()>0){
// Open joystick
joy=SDL_Joystic kOpen(0);
if(joy)
{
printf("Opened Joystick 0\n");
printf("Name: %s\n", SDL_JoystickNam e(0));
}
SDL_Quit();
SDL_JoystickCl ose(joy);
return 0;
}
This is not your real code. This code would not compile. The { on
the if(SDL_NumJoyst icks()>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
Sep 26 '06 #2
ro******@ibd.nr c-cnrc.gc.ca (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_Keit h) ks***@mib.org <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.
Sep 26 '06 #3
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_IN IT_JOYSTICK );
SDL_Joystick *joy;
// Check for joystick
if(SDL_NumJoyst icks()>0){
// Open joystick
joy=SDL_Joystic kOpen(0);
if(joy)
{
printf("Opened Joystick 0\n");
printf("Name: %s\n", SDL_JoystickNam e(0));
}
SDL_Quit();
SDL_JoystickClo se(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_JoystickOpe n(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");
}
Sep 26 '06 #4
/*
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_IN IT_JOYSTICK );
SDL_Surface* screen = SDL_SetVideoMod e( WINDOW_WIDTH,
WINDOW_HEIGHT, 0,
SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCapti on( WINDOW_TITLE, 0 );
SDL_Joystick *joy;

if(SDL_NumJoyst icks()>0){
joy=SDL_Joystic kOpen(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==SD L_KEYDOWN)
{
SDLKey keyPressed = event.key.keysy m.sym;
switch (keyPressed)
{
case SDLK_ESCAPE:
x=0;
break;
}

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

Sep 27 '06 #5
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.
Sep 27 '06 #6
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
Sep 27 '06 #7
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...)
Sep 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2718
by: Moosebumps | last post by:
When I have time, I am planning to evaluate Python for console game development (on Playstation 2, GameCube, and Xbox). Does anyone have any experience with this? Pretty much the only resource I have found, and the only thing that makes me think it might be possible is this: http://asbahr.com/python.html I would be interested to hear anybody's experience with Python on consoles.
1
3766
by: frank | last post by:
Hi all I don't think this is strictly a Python problem, but as it manifests itself in one of my Python programs, I am hoping that somebody in this group can help me. The following is a message I sent to co.os.linux.setup - "My question concerns line graphics on a text-based console. ­My actual problem relates to a program I have written using
1
5387
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
1
9577
by: Paul Hoad | last post by:
I'm trying to use MeasureString() to determine the length in pixels of a string However to do I need a System.Drawing.Graphics object to do this I need to create a System.Drawing.Graphics object for which there is only two constructors System.Drawing.Graphics.FromHdc
1
3646
by: Novice | last post by:
I'm afraid I will incur the wraith of Mr. Powell on this one - but I did read his #1 FAQ and some others and I still can't figure this out. I created this little c# app. and I have a PictureBox in my Form. I load this image from the filesystem into the PictureBox and then I draw random little lines on the image. Then when I minimize and reopen the application the little lines are gone. Is there a way to save my lines in memory and...
3
1611
by: Mr Mind - Lion | last post by:
Is there any way in .NET to force to run console application through it and get the result of tht application in our program. Please reply me quickly. Any reply will highly be appreciated. Thanx.
4
4638
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this program is like so: static void Main() {
2
14394
by: SriBhargav | last post by:
Hi, I've a question on setting timeout on console.readline() I would like the user to input something through Console.readline() in 5 secs. If there is no input in that time, I would like to proceed further with the program logic. I had a difficulty in implementing this, as console.readline() indefinitely waits for the user input.
4
223
by: =?Utf-8?B?UGFvbG8=?= | last post by:
Is there a way of accessing the various graphics characters to facilitate drawing on the screen in console apps. I don't want to use the '-' (hyphen) character - I thoughtthere were some 'graphics' characters hat were available. I've looked at the Console Class specs and can't find anything about this. Any advice would be appreciated. Thanks
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.