Jump to content
  • Advertisement
Sign in to follow this  
mattor

SDL keyboard/ char array problem

This topic is 4829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay I've been struggling with this for awhile now...I'm making a graphics enhanced number guessing game. It displays the numbers 0 through 9, and I want to have it put an X over the number you press. So I have a font that displays the numbers, and an identical font that is semi-transparent so you can see what number the X covers. here's my code:
  loop=0;
  while(loop==0)
  {
    SDL_Event event2;
    keys = SDL_GetKeyState(NULL);
    while ( SDL_PollEvent(&event2) )
    {
      if ( event2.type == SDL_QUIT )  { done = 1; loop=1;}
      if ( event2.type == SDL_KEYDOWN )
      {
        if ( event2.key.keysym.sym == SDLK_ESCAPE ) { done = 1; loop=1;}
        if ( keys[SDLK_0] && trueNumList[0] != 'X') { trueNumList[0] = 'X'; xList[0] = 'X';}
        if ( keys[SDLK_1] && trueNumList[1] != 'X') { trueNumList[1] = 'X'; xList[3] = 'X';}
        if ( keys[SDLK_2] && trueNumList[2] != 'X') { trueNumList[2] = 'X'; xList[6] = 'X';}
        if ( keys[SDLK_3] && trueNumList[3] != 'X') { trueNumList[3] = 'X'; xList[9] = 'X';}
        if ( keys[SDLK_4] && trueNumList[4] != 'X') { trueNumList[4] = 'X'; xList[12] = 'X';}
        if ( keys[SDLK_5] && trueNumList[5] != 'X') { trueNumList[5] = 'X'; xList[15] = 'X';}
        if ( keys[SDLK_6] && trueNumList[6] != 'X') { trueNumList[6] = 'X'; xList[18] = 'X';}
        if ( keys[SDLK_7] && trueNumList[7] != 'X') { trueNumList[7] = 'X'; xList[21] = 'X';}
        if ( keys[SDLK_8] && trueNumList[8] != 'X') { trueNumList[8] = 'X'; xList[24] = 'X';}
        if ( keys[SDLK_9] && trueNumList[9] != 'X') { trueNumList[9] = 'X'; xList[27] = 'X';}
        if ( keys[SDLK_RETURN] ) { loop = 1;}
      }
    }
    drawString(screen,xFont,400-stringWidth(numFont,numList)/2,100,xList); 
    SDL_Flip(screen); 
  }  

A couple of things to mention: xList is a char* array, and trueNumList is a char array. I think the problem is where I try to replace xList[whatever]. The prog crashes when I press any digit. Is that because I am somehow trying to change the address to 'X' instead of the value? I tried xList[0] = "X" and that didn't work either.

Share this post


Link to post
Share on other sites
Advertisement
I couldn't find the error , but why do you use if in the event structure ?

Shouldn't the switch() - case style be more efficient and easier to use ?

For example


while (SDL_PollEvent(&event2) )
{
switch( event2.type )
{
case SDL_QUIT:
//do some stuff when quit
break;

case SDL_KEYDOWN:
//here are keys pressed - the same structure

break;

}
}

Share this post


Link to post
Share on other sites
Show us the code where you define trueNumList and xList. It's possible that you're not creating your array properly and your assignment attempt is going out of bounds.


Ryan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!