Right, so I'm trying to learn me some SDL by writing a simple RL. I'm using SDL (duh) and C++. I know C++, not an expert, but good enough. However, I'm having this weird error in one of my functions. The code is the same as with my character movement code, which works perfectly, however in this particular function it reads my keypresses twice. I fixed it by putting a SDL_WaitEvent() call at the end, but this now means that you have to hit 'Q' twice to exit. Without the SDL_WaitEvent() it'll skip 2 tiles when you hit space fr the next tile, and move 2 squares in whatever direction I press. I can't figure out what the problem is, because, as I understand it, an event is only created when a key is pressed (and, indeed, that is what happens with my movement code). Can anyone shed some light on this for me? Code after the spoiler.
void Map::editMap( BitmapFont disp_font, SDL_Surface * displaySurface )
{
int curX, curY, curTile; //location of the cursor and current tile
curX = 0;
curY = 0;
curTile = FLOOR_ID;
std::string cursor = "X";
std::string tilemessage;
bool stopedit = false;
tilemessage = "Current tile: ";
tilemessage += tileKey[curTile].getSym();
//fill screen with black and display the current map, cursor, info, and selected tile to place
SDL_FillRect( displaySurface, &displaySurface->clip_rect, SDL_MapRGB( displaySurface->format, 0, 0, 0 ) );
display( disp_font, displaySurface );
disp_font.show_text( curX * disp_font.get_cellW(), curY * disp_font.get_cellH(), cursor, displaySurface, YELLOW );
disp_font.show_text( 0, (WORLD_Y + 1) * disp_font.get_cellH(), "Editing map. Arrows to move, ", displaySurface, WHITE );
disp_font.show_text( (WORLD_X + 1) * disp_font.get_cellW(), 0, tilemessage, displaySurface, tileKey[curTile].getColor() );
SDL_Flip( displaySurface );
while( !stopedit )
{
//check for input
while( SDL_PollEvent( &event ) )
{
if (event.type = SDL_KEYDOWN)
{
switch( event.key.keysym.sym )
{
case SDLK_KP8:
if( curY > 0 )
curY -= 1;
break;
case SDLK_KP2:
if( curY < WORLD_Y -1 )
curY += 1;
break;
case SDLK_KP4:
if( curX > 0 )
curX -= 1;
break;
case SDLK_KP6:
if( curX < WORLD_X - 1 )
curX += 1;
break;
case SDLK_KP7:
if( curX > 0 && curY > 0)
{
curX -= 1;
curY -= 1;
}
break;
case SDLK_KP9:
if( curX < WORLD_X - 1 && curY > 0 )
{
curX += 1;
curY -= 1;
}
break;
case SDLK_KP3:
if( curX < WORLD_X - 1 && curY < WORLD_Y - 1 )
{
curX += 1;
curY += 1;
}
break;
case SDLK_KP1:
if( curX > 0 && curY < WORLD_Y - 1 )
{
curX -= 1;
curY += 1;
}
break;
case SDLK_SPACE:
if( curTile < NUMTILES - 1 )
curTile += 1;
else
curTile = 0;
tilemessage = "Current tile: ";
tilemessage += tileKey[curTile].getSym();
break;
case SDLK_RETURN:
mapArray[curX][curY] = curTile;
break;
case SDLK_q:
stopedit = true;
break;
case SDLK_l:
loadMap( "map.sav" );
break;
case SDLK_s:
saveMap( "map.sav" );
break;
}
//fill screen with black and display the current map, cursor, info, and selected tile to place
SDL_FillRect( displaySurface, &displaySurface->clip_rect, SDL_MapRGB( displaySurface->format, 0, 0, 0 ) );
display( disp_font, displaySurface );
disp_font.show_text( curX * disp_font.get_cellW(), curY * disp_font.get_cellH(), cursor, displaySurface, YELLOW );
disp_font.show_text( 0, (WORLD_Y + 1) * disp_font.get_cellH(), "Editing map. NUMPAD to move, ", displaySurface, WHITE );
disp_font.show_text( (WORLD_X + 1) * disp_font.get_cellW(), 0, tilemessage, displaySurface, tileKey[curTile].getColor() );
SDL_Flip( displaySurface );
//wait for keyrelease
SDL_WaitEvent( &event );
}
}
}
}