[color="#333333"]So i've done some coding regarding showing what key is beeing pressed down at the moment. [color="#333333"]The code compiles and everything, but the screen is just black. [color="#333333"]When I close down the application the image pops up for a split second, saying what key that was pressed down. It's like it's beeing saved to the very last moment in the buffer. What is going wrong?
//Generate the message surfaces upMessage = TTF_RenderText_Solid( font, "Up was pressed.", textColor ); downMessage = TTF_RenderText_Solid( font, "Down was pressed.", textColor ); leftMessage = TTF_RenderText_Solid( font, "Left was pressed", textColor ); rightMessage = TTF_RenderText_Solid( font, "Right was pressed", textColor );
//Apply the images to the screen apply_surface( 0, 0, background, displaySurface );
//The program will wait until the user either press 'x' or esc. while(!quit) { quit = pollInput(event); } //If a message needs to be displayed if( message != NULL ) { //Apply the background to the screen apply_surface( 0, 0, background, displaySurface );
//Apply the message centered on the screen apply_surface( ( screenWidth - message->w ) / 2, ( screenHeight - message->h ) / 2, message, displaySurface );
if(event.type == SDL_KEYDOWN) { //what key is pressed down? Then show the correct message or quit. switch(event.key.keysym.sym) { case SDLK_UP: message = upMessage; break; case SDLK_DOWN: message = downMessage; break; case SDLK_LEFT: message = leftMessage; break; case SDLK_RIGHT: message = rightMessage; break; case SDL_QUIT: //If you want to quit using the 'x' return true; case SDLK_ESCAPE: return true;
if(surface != NULL) //if the file was loaded { newSurface = SDL_DisplayFormat(surface); // We create a new surface, to convert the 24BMP file to work faster on our 32bit SDL_FreeSurface(surface); // // if(newSurface != NULL) { //Color key surface SDL_SetColorKey( newSurface, SDL_SRCCOLORKEY, SDL_MapRGB( newSurface->format, 255,0,255 ) );
[color="#333333"]So i've done some coding regarding showing what key is beeing pressed down at the moment. [color="#333333"]The code compiles and everything, but the screen is just black. [color="#333333"]When I close down the application the image pops up for a split second, saying what key that was pressed down. It's like it's beeing saved to the very last moment in the buffer. What is going wrong?
//Generate the message surfaces upMessage = TTF_RenderText_Solid( font, "Up was pressed.", textColor ); downMessage = TTF_RenderText_Solid( font, "Down was pressed.", textColor ); leftMessage = TTF_RenderText_Solid( font, "Left was pressed", textColor ); rightMessage = TTF_RenderText_Solid( font, "Right was pressed", textColor );
//Apply the images to the screen apply_surface( 0, 0, background, displaySurface );
//The program will wait until the user either press 'x' or esc. while(!quit) { quit = pollInput(event); } //If a message needs to be displayed if( message != NULL ) { //Apply the background to the screen apply_surface( 0, 0, background, displaySurface );
//Apply the message centered on the screen apply_surface( ( screenWidth - message->w ) / 2, ( screenHeight - message->h ) / 2, message, displaySurface );
if(event.type == SDL_KEYDOWN) { //what key is pressed down? Then show the correct message or quit. switch(event.key.keysym.sym) { case SDLK_UP: message = upMessage; break; case SDLK_DOWN: message = downMessage; break; case SDLK_LEFT: message = leftMessage; break; case SDLK_RIGHT: message = rightMessage; break; case SDL_QUIT: //If you want to quit using the 'x' return true; case SDLK_ESCAPE: return true;
if(surface != NULL) //if the file was loaded { newSurface = SDL_DisplayFormat(surface); // We create a new surface, to convert the 24BMP file to work faster on our 32bit SDL_FreeSurface(surface); // // if(newSurface != NULL) { //Color key surface SDL_SetColorKey( newSurface, SDL_SRCCOLORKEY, SDL_MapRGB( newSurface->format, 255,0,255 ) );
//Close the font that was used TTF_CloseFont( font );
//Quit SDL_ttf TTF_Quit();
//Quit SDL SDL_Quit(); }
It's because you only apply your message surface after quit is equal to zero, so right after you press escape or close the window, it exits the loop, displayed the message and quits
A quick fix to this would be to add an SDL_Delay(5000) rigt after your if statement after the loop Doing this will allow you to press a key, the quit the program. You will then see the message for 5 sec
A quick fix to this would be to add an SDL_Delay(5000) rigt after your if statement after the loop Doing this will allow you to press a key, the quit the program. You will then see the message for 5 sec
That would indeed be a quick fix, not my intention though. My intention were that the images would display when I press a key. If I press down it would show the message, and if I click another key it would show another message
You need to add your rendering inside of your while(!quit) loop. A more correct definition of what is happening is your while(!quit) loop is what your sitting there watching. Inside that loop you are doing nothing more then getting the input and setting your message variable. However inside that loop you are not attempting to draw the surface based on the message. So once your user hits x or escape you break from your main loop, THEN you check what the last message was and render that to the screen but it's to late your already closing your app. So basically put the if(message != null) block into your while(!quit) loop and you will start to see results.
You need to add your rendering inside of your while(!quit) loop. A more correct definition of what is happening is your while(!quit) loop is what your sitting there watching. Inside that loop you are doing nothing more then getting the input and setting your message variable. However inside that loop you are not attempting to draw the surface based on the message. So once your user hits x or escape you break from your main loop, THEN you check what the last message was and render that to the screen but it's to late your already closing your app. So basically put the if(message != null) block into your while(!quit) loop and you will start to see results.
It is true what you are saying! Stupid me for not seeing that. But yeah, I tried to put my if(message != null) to my while loop and it didn't help at all. It still waits for the input to quit. Am I doing something wrong or is it maybe something else that is wrong?
//The program will wait until the user either press 'x' or esc. while(!quit) {
//If a message needs to be displayed if(message != NULL) { //Apply the background to the screen apply_surface( 0, 0, background, displaySurface );
//Apply the message centered on the screen apply_surface( ( screenWidth - message->w ) / 2, ( screenHeight - message->h ) / 2, message, displaySurface );
Done some more thinking, if I put my if(messages != NULL) to my while-loop. Does that mean that the if-statement will only execute if I press I key? That sounds.. Wrong.
Done some more thinking, if I put my if(messages != NULL) to my while-loop. Does that mean that the if-statement will only execute if I press I key? That sounds.. Wrong. [/quote] The body of the if statement will only execute during loop iterations where "message" is not null. The variable "message" is initially null, and is only given a value when the player presses a key. When the message has been drawn, you set "message" back to null.
It is all of these factors interacting that will cause the message to only be displayed the instant a key is pressed. Is that not what you intended?
Am I doing something wrong or is it maybe something else that is wrong? [/quote] You aren't flipping the screen. You have rendered the images to an off screen buffer, you need to flip it to really "apply" it to the screen. Move the call to SDL_Flip() inside your loop.
Your next problem will likely be that the image disappears too fast to make use of. One suggestion I have is to wait for keyup events before nulling out message, rather than nulling it immediately after it has been rendered.
Done some more thinking, if I put my if(messages != NULL) to my while-loop. Does that mean that the if-statement will only execute if I press I key? That sounds.. Wrong.
The body of the if statement will only execute during loop iterations where "message" is not null. The variable "message" is initially null, and is only given a value when the player presses a key. When the message has been drawn, you set "message" back to null.
It is all of these factors interacting that will cause the message to only be displayed the instant a key is pressed. Is that not what you intended?
Am I doing something wrong or is it maybe something else that is wrong? [/quote] You aren't flipping the screen. You have rendered the images to an off screen buffer, you need to flip it to really "apply" it to the screen. Move the call to SDL_Flip() inside your loop.
Your next problem will likely be that the image disappears too fast to make use of. One suggestion I have is to wait for keyup events before nulling out message, rather than nulling it immediately after it has been rendered. [/quote]
Jeezus, glad I posted here! Thx for the fast reply, the flipping was the problem. Cheers