Jump to content

  • Log In with Google      Sign In   
  • Create Account


Beginner's Problem with SDL in C++


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
5 replies to this topic

#1 nyl2000   Members   -  Reputation: 129

Like
0Likes
Like

Posted 08 May 2012 - 05:45 AM

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.

Attached Files


Edited by nyl2000, 08 May 2012 - 09:21 AM.


Sponsor:

#2 endless11111   Members   -  Reputation: 151

Like
-1Likes
Like

Posted 08 May 2012 - 10:14 AM

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.

#3 Ectara   Crossbones+   -  Reputation: 2930

Like
-1Likes
Like

Posted 08 May 2012 - 10:47 AM

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).

#4 DarkHorseKnight   Members   -  Reputation: 143

Like
1Likes
Like

Posted 08 May 2012 - 11:48 AM

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 Posted Image

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"

Edited by DarkHorseKnight, 08 May 2012 - 12:16 PM.


#5 nyl2000   Members   -  Reputation: 129

Like
1Likes
Like

Posted 08 May 2012 - 08:20 PM

Thank you, DarkHorseKnight,

May I ask what #undef main is exactly?

Edited by nyl2000, 08 May 2012 - 08:20 PM.


#6 fastcall22   Crossbones+   -  Reputation: 4271

Like
1Likes
Like

Posted 08 May 2012 - 08:29 PM

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.

Edited by fastcall22, 08 May 2012 - 08:31 PM.

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=




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