Skip to main content
GameDev.net gamedev.net
🔒 Locked

Beginner's Problem with SDL in C++

Started by nyl2000 May 8, 2012 at 11:45 AM 4 replies 2k views
Original Post
nyl2000
nyl2000
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,

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]




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.
http://lazyfoo.net/S...son01/index.php

The full tutorial source code is also attached.

I hope someone can at least tell me what the problem is exactly.
endless11111
endless11111
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.
Ectara
Ectara
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).
DominicHughes
DominicHughes
Basically you needed a while loop so the program is continous for ever until done is equal to true:


#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;
}


Hopefully this works smile.png

Here is a pastebin link

http://pastebin.com/bJSx40g5


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"
nyl2000
nyl2000
Thank you, DarkHorseKnight,

May I ask what #undef main is exactly?
fastcall22
fastcall22
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 #undefining main, you forbid SDL from using its own main function, and the linker chooses your main function over SDL_main's main as your program's entry point.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.