Sign in to follow this  

Beginner's Problem with SDL in C++

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have just gotten started with trying to use SDL/OpenGL for C++. I have followed several tutorials, and have gotten the example programs to compile without error.

However, when I try to run the program, a blank window shows up (instead of the intended image of the tutorial). If I remove the SDL_Delay, the blank window also appears to be unresponsive. Also, [left]any cin and cout I/O that I add don't seem to show up on the window (cout writes on a txt file and cin does not? wait for keyboard input before moving on in the program)[/background][/left]


I am using a windows 7 64 bit machine, and have attempted the tutorial on both Codeblocks and Visual Studio 2010 with the same problematic result.

I have already tried adjusting for the 32-bit compatibility problem by using WOW64, it seems to have no effect.

This is the tutorial.
[url="http://lazyfoo.net/SDL_tutorials/lesson01/index.php"]http://lazyfoo.net/S...son01/index.php[/url]

The full tutorial source code is also attached.

I hope someone can at least tell me what the problem is exactly. Edited by nyl2000

Share this post


Link to post
Share on other sites
the image might not be loading up because you don't have the image file in the same folder as the project file. if you are compiling a program, all the files accosiated with the program must be in the same folder as the .cpp files, and if you are running the .exe file, all the files accosiated with the program must be in the same folder as the .exe file. and i am not really familiar with text intput and output in sdl using cin and cout, but as far as i know, you can't use cin or cout like you would with a console application, you can't just output text or input text directly using cin and cout.

Share this post


Link to post
Share on other sites
SDL cannot simply print text and get text to the screen; it moves pixels, and watches for events, and such. If you want terminal functionality, you must write that yourself.

As far as stdin and stdout, I'm pretty sure those are redirected to an output text file and something non-keyboard for input when used in a Windows application (as opposed to a console application or most other WM's).

Share this post


Link to post
Share on other sites
Basically you needed a while loop so the program is continous for ever until done is equal to true:

[CODE]
#undef main
#include <SDL/SDL.h>
int main( int argc, char **argv[] )
{
bool done = false;

SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;

SDL_Init( SDL_INIT_EVERYTHING );

screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );

hello = SDL_LoadBMP( "hello.bmp" );

SDL_Event event;

while(!done)
{

SDL_BlitSurface( hello, NULL, screen, NULL );

while(SDL_PollEvent(&event))
{
switch(event.type)
{

case SDL_QUIT:
return 0;
break;

}

}

SDL_Flip( screen );

}
return 0;
}
[/CODE]

Hopefully this works [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Here is a pastebin link

[url="http://pastebin.com/bJSx40g5"]http://pastebin.com/bJSx40g5[/url]


EDIT:

and about cin and cout you need to #undef main at the top of your main.cpp and "remove linker flag -lSDLmain in your IDE linker settings" Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
The SDL_main library part of SDL tries to be clever in renaming your "main" function to "SDL_main", in this way the library provides its own "main" function with some additional features before calling your "main" function. SDL_main provides platform independent non-console window setup: You get the same no-console program in all platforms, along with some additional "features", such as redirecting stdin and stdout to files.

By [tt]#undef[/tt]ining [tt]main[/tt], you forbid SDL from using its own [tt]main[/tt] function, and the linker chooses your [tt]main[/tt] function over SDL_main's [tt]main[/tt] as your program's entry point. Edited by fastcall22

Share this post


Link to post
Share on other sites

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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