SDL Noob

This is a discussion on SDL Noob within the Game Programming forums, part of the General Programming Boards category; I am trying to apply a simple gravity via SDL. I can't seem to allow for one user input for ...

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407

    Event Handling in SDL

    I am trying to apply a simple gravity via SDL. I can't seem to allow for one user input for each drop gravity creates... for some reason when I key left or right on the keyboard that input gets stuck everytime gravity is enacted.

    Code:
    int gravity()
    {
      bool ground;
      ground = false;
      while(ground == false)
      { 
        SDL_PollEvent(&jevent);                            
        if(jevent.type == SDL_QUIT)
        {
          ground = true;
        }
        else if(jevent.type == SDL_KEYDOWN)
        {   
          switch( jevent.key.keysym.sym ) 
          { 
            case SDLK_UP: 
            break; 
            case SDLK_DOWN: 
            break; 
            case SDLK_LEFT: xxx -= 20;
                              apply_surface(0,0, background, screen);
                              apply_surface(xxx,yyy, unit, screen);
                              if (SDL_Flip(screen) == -1)
                              {
                                return 1;
                              }
                              if(yyy<=(460-15))
                              {
                                yyy+=20;
                                apply_surface(0,0, background, screen);
                                apply_surface(xxx,yyy, unit, screen);
                                if (SDL_Flip(screen) == -1)
                                {
                                  return 1;
                                }
                              }
                              else
                              {
                                ground = true;
                              } 
            break; 
            case SDLK_RIGHT: xxx += 20;
                              apply_surface(0,0, background, screen);
                              apply_surface(xxx,yyy, unit, screen);
                              if (SDL_Flip(screen) == -1)
                              {
                                return 1;
                              }
                              if(yyy<=(460-15))
                              {
                                yyy+=20;
                                apply_surface(0,0, background, screen);
                                apply_surface(xxx,yyy, unit, screen);
                                if (SDL_Flip(screen) == -1)
                                {
                                  return 1;
                                }
                                
                              }
                              else
                              {
                                ground = true;
                              }
            break;
          } 
        }             
      }
    }
    Should I not allow event handling during any function except main?
    I have tried to word this code a ton of different ways and can't seem to allow for it. I have not even started collision detection yet, because I can't get the jump to work the way I want it too.
    Thank you for your time.
    Last edited by Lesshardtofind; 07-10-2009 at 11:27 PM.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I havent gone through your code all that well, but why are you polling for input and flipping the screen buffer in your gravity function? Your main loop should generally have one section where it polls for all events, calling functions as necessary. At the end of your main loop you shoudl flip the buffer once, at one point, not in several situations in a switch statement as that just creates a load of redundant code. That may not fix your problem, but it will at least simplify things for you a whole lot.

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    I already scrapped that one. I realized how redundant the code was and I made those mistakes through the whole project. Not writing functions to cut down on reoccuring code, it just started to get really sloppy. So I started a new idea from scratch. I revisted the gravity idea yesterday and did as you suggested realizing gravity is just a function of acceleration due to gravity, which is only a distance over time. I had a SDL_Delay control the passing of time, a function to alter a global velocity variable, of course a bool to check whether it was on the ground or some other barrier, and a
    SDL_Flip(screen) at the end of the loop to update the screen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL Collision Handling
    By Neo1 in forum Game Programming
    Replies: 2
    Last Post: 04-02-2008, 03:04 PM
  2. Problems compiling this SDL app
    By Rider in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2007, 01:22 PM
  3. SDL and MinGW Studio
    By Vicious in forum Tech Board
    Replies: 0
    Last Post: 07-30-2004, 10:59 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 01:19 AM
  5. sdl in c++
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2002, 08:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21