SDL doesn't detect Arrow Keys - c++

I am working through the SDL tutorials over at http://lazyfoo.net/SDL_tutorials/index.php and I'm stuck on tutorial 8 where I'm working with key presses.
I'm using the following code:
//Our Main application loop
while(!quit){
if(SDL_PollEvent(&curEvents)){
if(curEvents.type == SDL_QUIT){
quit = true;
}
//If a key was pressed
if( curEvents.type == SDL_KEYDOWN )
{
//Set the proper message surface
switch( curEvents.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;
default:
message = TTF_RenderText_Solid(font, "Unknown Key", textColor);
break;
}
}
}
if( message != NULL )
{
//Apply the background to the screen
applySurface( 0, 0, background, screen );
//Apply the message centered on the screen
applySurface( ( SCREEN_WIDTH - message->w ) / 2, ( SCREEN_HEIGHT - message->h ) / 2, message, screen );
//Null the surface pointer
message = NULL;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
Where works fine, the default case is reached, for everything BUT pressing the arrow keys. I was wondering if someone could spot what I'm doing wrong.

I discovered the error which was not in the code posted above. The error was that for the arrow keypress messages, I used RenderText before the font was opened. By the time the posted code block was reached, font had already been opened and is the reason why that message shows.

Related

Texture drawn at wrong coordinates?

I tried drawing a '1' texture at mouse coordinates when I press the 1 key:
switch (e.type)
{
case SDL_QUIT:
{
quit = true;
break;
}
case SDL_KEYDOWN:
{
switch (e.key.keysym.sym)
{
case SDLK_1:
{
SDL_Rect rect = {e.motion.x - 8, e.motion.y - 8, 16, 16};
SDL_RenderCopy(gRenderer, gT[3], NULL, &rect);
printf("1\n");
break;
}
}
break;
}
}
I cannot comprehend why this doesn't work.
gT[3] is the texture of the '1'.
I thought maybe it is because its e.key.keysym.sym but I'm not sure.
SDL_Event::motion is only valid when SDL_Event::type is SDL_MOUSEMOTION.
Stop trying to use SDL_Event::motion when SDL_Event::type is SDL_KEYDOWN, perhaps by recording the X and Y coordinates of the most recent SDL_MOUSEMOTION event and using those instead.

Event handling bug with SDL

I am making a snake game using SDL and C++, and I found a bug involving events.
In this game, there are two important functions for now : the play function (the "main" function of the game) and the pause function. When the P key is pressed, the play function calls and executes the pause function. In it, you have two possibilities: either continue or quit the game.
Now, here is the problem. When I try to click "Continue game", the game flashes for a split second and comes back to the pause menu. When I keep the mouse button down, I realize the game itself is frozen until an event of any kind happens. If it does, it just comes back to where it was.
After some tests, I also noticed that the bug was due to the SDL_PollEvent function. So I changed this function in the play function to SDL_WaitEvent, which put the game back when I asked him to. However, another problem is created: since, in my play function, there is a timer that uses SDL_GetTicks, the game needs a trigger event to be fluid. For example, when I constantly move the cursor in any direction, the game can run. When I stop, it freezes.
Is there a way to fix this bug?
Here is the event handler in the play function:
SDL_PollEvent(&event); // Handle events
switch(event.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) // Change direction at certain key press
{
case SDLK_s:
currentDirection = SNAKE_DOWN;
break;
case SDLK_w:
currentDirection = SNAKE_UP;
break;
case SDLK_a:
currentDirection = SNAKE_LEFT;
break;
case SDLK_d:
currentDirection = SNAKE_RIGHT;
break;
case SDLK_ESCAPE: // Quit if escape key pressed
quit = true;
break;
case SDLK_p:
if (pause(screen) == false)
quit = true;
break;
break;
default: ; // Code to block the compiler's -Wall flag (does not do anything to the program itself)
}
break;
}
And here is the complete pause function:
bool pause(SDL_Surface *screen)
{
SDL_Surface *pauseMessage, *continueButton, *quitButton;
SDL_Rect posPauseMessage, posContinueButton, posQuitButton;
TTF_Font *fipps;
SDL_Color color = {255, 255, 255};
SDL_Event event;
int x = 0, y = 0;
bool quit = false, returnFalse = false;
fipps = TTF_OpenFont("fipps.ttf", 50);
pauseMessage = TTF_RenderText_Blended(fipps, "PAUSED", color);
continueButton = TTF_RenderText_Blended(fipps, "Continue Game", color);
quitButton = TTF_RenderText_Blended(fipps, "Exit to menu", color);
posPauseMessage.x = (screen->w - pauseMessage->w) / 2;
posPauseMessage.y = 200;
posContinueButton.x = (screen->w - continueButton->w) / 2;
posContinueButton.y = posPauseMessage.y + 200;
posQuitButton.x = (screen->w - quitButton->w) / 2;
posQuitButton.y = posContinueButton.y + 100;
while (!quit)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
returnFalse = true;
quit = true;
break;
case SDL_MOUSEBUTTONUP:
x = event.button.x;
y = event.button.y;
if ((x > posContinueButton.x) && (x < posContinueButton.x + continueButton->w) && (y > posContinueButton.y) && (y < posContinueButton.y + continueButton->h))
quit = true;
else if ((x > posQuitButton.x) && (x < posQuitButton.x + quitButton->w) && (y > posQuitButton.y) && (y < posQuitButton.y + quitButton->h))
returnFalse = true;
quit = true;
break;
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(pauseMessage, NULL, screen, &posPauseMessage);
SDL_BlitSurface(continueButton, NULL, screen, &posContinueButton);
SDL_BlitSurface(quitButton, NULL, screen, &posQuitButton);
SDL_Flip(screen);
}
TTF_CloseFont(fipps);
SDL_FreeSurface(pauseMessage);
SDL_FreeSurface(continueButton);
SDL_FreeSurface(quitButton);
if (returnFalse)
return false;
return true;
}
Thanks to #jordsti, I found a solution : I need to put the SDL_PollEvent as a condition for a while loop, which contains the switch block.

MFC Controls are getting disappeared after scrolling

I am working on dialog based MFC application in WinCE.
I created few controls in a dialog and scrolled down.
When i scroll up again, the controls in the first screen got disappeared.
Controls getting created in OnInitDialog() like below at coordinates (50,10)
test->Create(_T("Title"), WS_CHILD|WS_VISIBLE, CRect(50,10,200,40), this, ID_TITLE);
Scroll handling i am doing in OnVScroll() like below
switch(nSBCode)
{
case SB_LINEDOWN:
{
if(nPos < max)
{
ScrollWindowEx(0, SCROLLDOWN_LINE_STEPSIZE, CRect(0,0, rect.right - 25, rect.bottom), NULL, NULL, NULL, SW_SCROLLCHILDREN | SW_INVALIDATE);
pScrollBar->SetScrollPos(nPos - SCROLLDOWN_LINE_STEPSIZE); //nPos+10
}
break;
}
case SB_LINEUP:
{
if(nPos > min)
{
ScrollWindowEx(0, SCROLLUP_LINE_STEPSIZE, CRect(0,0, rect.right - 25, rect.bottom), NULL, NULL, NULL, SW_SCROLLCHILDREN | SW_INVALIDATE);
pScrollBar->SetScrollPos(nPos - SCROLLUP_LINE_STEPSIZE); //Ex: nPos-10
}
break;
}
default:
printf("Notimplemented");
break;
}
I am handling Scroll down and scroll up.
While scrolling down, all the controls in Dialog are shown.
But while scrolling up, the controls at the top got disappeared.
What's going on ?
Should i implement OnPaint() method for drawing the controls each time i scroll ?
If so, how ?
I have the following code that works fine. I hope it will help you.
LRESULT CMyWindow::OnVScroll( UINT code, UINT position )
{
SCROLLINFO info = { sizeof( SCROLLINFO ), SIF_ALL };
GetScrollInfo( m_wnd, SB_VERT, &info );
int previous_pos = info.nPos;
switch( code )
{
case SB_TOP:
info.nPos = 0;
break;
case SB_BOTTOM:
info.nPos = info.nMax - info.nPage;
break;
case SB_LINEDOWN:
info.nPos = min( info.nPos + 1, info.nMax - (int)info.nPage );
break;
case SB_LINEUP:
info.nPos = max( info.nPos - 1, info.nMin );
break;
case SB_PAGEDOWN:
info.nPos = min( info.nPos + (int)info.nPage, info.nMax - (int)info.nPage );
break;
case SB_PAGEUP:
info.nPos = max( info.nPos - (int)info.nPage, info.nMin );
break;
case SB_THUMBTRACK:
case SB_THUMBPOSITION:
info.nPos = position;
break;
}
int offset = previous_pos - info.nPos;
if( offset != 0 )
{
ScrollWindowEx( m_wnd, 0, offset, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE );
SetScrollPos( m_wnd, SB_VERT, info.nPos, FALSE );
}
return 0L;
}
From the ScrollWindowEx docs:
dy Specifies the amount, in
device units, of vertical scrolling.
This parameter must be a negative
value to scroll up.
I bolded the relevant phrase.

How can I clear a SDL_Surface to be replaced with another one?

Been trying to find this online for a while now.
I have a SDL_Surface with some content (in one it's text, in another is a part of a sprite). Inside the game loop I get the data onto the screen fine. But then it loops again and it doesn't replace the old data but just writes over it. So in the case of the text, it becomes a mess.
I've tried SDL_FreeSurface and it didn't work, anyone know another way?
fpsStream.str("");
fpsStream << fps.get_ticks();
fpsString = fpsStream.str();
game.fpsSurface = TTF_RenderText_Solid(game.fpsFont, fpsString.c_str(), textColor);
game.BlitSurface(0, 0, game.fpsSurface, game.screen);
Try something like:
SDL_FillRect(screen, NULL, 0x000000);
at the beginning of your loop.
What I do usually is drawing to a secondary surface (that is, an in-memory surface that's not the screen) and then SDL_BlitSurface when it's ready to be copied to the screen. You can then clear the whole secondary buffer (with SDL_FillRect) in the next iteration and redraw everything or just a part of if you don't want to lose the whole surface and only changed a rectangle.
This way, you also get doublebuffering and avoid flickering. Also don't forget SDL_UpdateRects after blitting.
If you are drawing something with transparency (eg. stuff from SDL_ttf) then the transparent areas between the text won't be altered, meaning previous writes will remain. This isn't usually a problem because the usual behaviour is for the program to clear the frame buffer and redraw the entire scene once per frame. In the old days it was common to only redraw the 'dirty' parts of the screen but that is not so common now.
while( !quit )
{
while( SDL_PollEvent( &e ) != 0)
{
if( e.type == SDL_QUIT )
{
quit = true;
}
else if( e.type == SDL_KEYDOWN)
{
**SDL_FillRect(screenSurface, NULL, 0x000000);**
switch(e.key.keysym.sym)
{
case SDLK_w:
CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_UP ];
break;
case SDLK_d:
CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_RIGHT ];
break;
case SDLK_s:
CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_DOWN ];
break;
case SDLK_a:
CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_LEFT ];
break;
default:
CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
break;
}
}
}
SDL_BlitSurface(CurrentSurface, NULL, screenSurface, NULL);
SDL_UpdateWindowSurface( window );
}

hardware buffering using SDL, question about how it works

I'm deciding to do my first game, its going to be simple but I want to use c++ and I chose SDL for my learning. So my question is about how the "buffers" are handled when writing code. I'll post my related code at the bottom.
Ok, so basically the way I understand it is that SDL takes care of which buffer is actually being drawn to the screen. When I am writing to a buffer it is always the backbuffer I am writing to, or the buffer currently not being drawn on the screen. So, when I call SDL_Flip(screen) it "blits" my screen surface onto the backbuffer, then moves the pointer to which buffer is being drawn to that buffer which used to be the backbuffer, the one I had been working on, and the old buffer that was showing now becomes the backbuffer. At this point if I call SDL_FillRect(arguments) it will be performed on the now back buffer?
I'm going to post my entire "heartbeat" of my learning game as it may help clarify my question:
//While the user hasn't quit
while( quit == false )
{
//If there's an event to handle
if( SDL_PollEvent( &event ) )
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the proper message surface
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;
}
}
else if( event.type == SDL_QUIT ) //if the user clicks the little X in the upper right corner.
{
quit = true;
}
}
//If a message needs to be displayed
if( message != NULL )
{
// Clear the back buffer.
SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
//Draw the backgroudn to the back buffer.
apply_surface( 0, 0, background, screen );
// Draw the "message" to the back buffer.
apply_surface( ( SCREEN_WIDTH - message->w ) / 2, ( SCREEN_HEIGHT - message->h ) / 2, message, screen );
//Null the surface pointer
message = NULL;
}
//Swap the current and back buffer.
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
it highly depends on the your system (ie. X11, Linux frame buffer, Windows), and the backend SDL uses to interact with it. Also which flags you passs to SDL_SetVideoMode. There are basically software surfaces which sit in a region of memory in you program and hardware surfaces which are placed in graphical card's memory. What you describe seems to me to be a double buffer, which is enabled if you pass SDL_HWSURFACE | SDL_DOUBLEBUF to SDL. Just remember this is not supported on all platforms and configurations and you may get something different instead.