Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6244965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:25:22+00:00 2026-05-24T12:25:22+00:00

This code compiles, but refuses to make a ball jump when there is no

  • 0

This code compiles, but refuses to make a ball jump when there is no cout. When there is a cout, it properly makes the object (dot) jump. This is just a bit of practice using SDL to create a super primitive game

Main loop:

while (!quit){
    while (SDL_PollEvent(&event))
        if ((event.type == SDL_QUIT) || ((event.type == SDL_KEYDOWN) && (event.key.keysym.sym == SDLK_ESCAPE)))
            quit = true;

    Uint8 * keystates = SDL_GetKeyState(NULL);

    if (keystates[SDLK_LEFT])
        dot.left();
    if (keystates[SDLK_RIGHT])
        dot.right();
    if (keystates[SDLK_SPACE]){     // press spacebar to jump
        if (!jumping){
            jumping = true;
            jump_time = 0;          // new count - not an actual timer
            SDL_Delay(1);
        }
    }

    while (jumping && ((t.now() + 2) < 1000 / FPS)){  // while jumping and 2ms away from frame cap time
        jump_time += dt;                              // float values. dt = .0002
                                                      // why its so low is beyond me


        // if i dont have this line, the dot will not jump
        std::cout << std::endl;


        // G = 9.81
        // MAX_HEIGT = 20
        // X shift = sqrt(MAX_HEIGHT * 2 / G)
        dot.offset.y = height - dot.offset.h - (-G / 2 * (jump_time - XSHIFT) * (jump_time - XSHIFT) + MAX_HEIGHT);
        if (dot.offset.y > (height - dot.offset.h)){
            jumping = false;
            dot.offset.y = height - dot.offset.h;
        }
    }

    SDL_FillRect(screen, NULL, 0xFFFFFF);
    dot.blit(screen);

    if (SDL_Flip(screen) == -1)
        return 1;

    if (t.now() < 1000 / FPS){              // cap frame rate
        SDL_Delay(1000 / FPS - t.now());
        t.start();                          // reset timer
    }
}

Can anyone explain why? I don’t understand why this is happening. Does SDL have something to do with it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:25:22+00:00Added an answer on May 24, 2026 at 12:25 pm

    I’m pretty sure it’s not something specific to using cout. It only has to do with the amount of time that using cout takes. That inner loop that the cout statement is in has nothing controlling it’s pace. It just controls the jump for what? 2 milliseconds, right?

    During that 2 milliseconds, the dot goes through as many states as it can, then you do another frame, where it get’s another 2 milliseconds. That process is repeated until the dot completes it’s jump.

    When the cout statement is in there, it probably takes up a significant portion of that 2 milliseconds, meaning that it takes the dot more frames to finish it’s jump.

    When the cout statement is taken out, the loop just goes so fast, that the jump is completed in very few frames, possibly just one. So either it’s so fast that you just don’t see it, or it’s so fast that you can’t see it, because it’s done before the screen ever updates.

    I’d recommend you work out a mechanism for timing the jump consistently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have non-template class with a templatized constructor. This code compiles for me. But
This code compiles in CodeGear 2009 and Visual Studio 2010 but not gcc. Why?
This code compiles and works as expected (it throws at runtime, but never mind):
Why is this code note working (the code compiles and run fine, but is
I just found that when it comes to templates this code compiles in g++
This code compiles fine but give segmentation fault error while running? Can anyone tell
this code compiles in Linux but not Solaris, because apparently ppoll() is Linux specific
This code compiles but looks very strange. I have a typical and simple parent/child
This code compiles, but no surprises, it fails while linking (no main found): Listing
I'm not sure why I'm getting this error, but shouldn't this code compile, since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.