I am doing a game and i need to refresh the screen to make it less messy, Can anyone teach me how to do plz?

Dani AI

Generated

For a game the screen should be refreshed from inside a controlled run loop, not by shell commands. Split each frame into: handle input, advance game state, draw into an off‑screen buffer, then present that buffer to the screen. This is the standard game‑loop approach and keeps timing predictable across machines. (gameprogrammingpatterns.com)

A compact, practical pattern (fixed‑timestep + accumulator) keeps physics stable while letting rendering run at display rate. Example pseudocode:

using namespace std::chrono_literals;
auto last = high_resolution_clock::now();
double accumulator = 0.0;
const double dt = 1.0/60.0;

while (running) {
  auto now = high_resolution_clock::now();
  double frameTime = duration_cast<duration<double>>(now - last).count();
  last = now;
  accumulator += frameTime;

  processInput();

  while (accumulator >= dt) {
    update(dt);
    accumulator -= dt;
  }

  clearBackbuffer();
  render();
  swapBuffers();   // library/OS call
  std::this_thread::sleep_for(1ms); // small yield / FPS cap if needed
}

Use a back buffer and swap/present once per frame to avoid flicker and tearing (double/triple buffering is how modern systems do this). If you need a fixed simulation timestep, the accumulator pattern is recommended. (learn.microsoft.com)

For portability and fewer platform surprises, prefer a cross‑platform library that gives you windowing, input and a safe present call (examples of maintained libraries and their docs cover these workflows). For text/terminal UIs, use a TUI library or terminal escape sequences that batch cursor moves rather than issuing OS shell clears. These approaches reduce flicker and are far less brittle than calling external console commands. (wiki.libsdl.org)

Notes tied to earlier posts: this expands on 's redraw point and addresses / concerns about portability; it implements the frame timing ideas implied by while avoiding slow, platform‑specific console hacks mentioned by other responders. Common troubleshooting: present exactly once per frame, keep draw order consistent, and profile update() if you miss your target frame time.

Recommended Answers

All 7 Replies

simple -- redraw whatever it is that you want on the screen.

If your question wasn't already answered by Ancient Dragon, and you're using text, then try using system("cls"); this should work to clear the screen.

If you are doing a game, don't use system("cls"). Write it properly.

What OS are you using? Windows? Linux?

if you do use sytem("cls"); then you will have to put in time wasting loops or else you won't be able to see much because the system("cls") will clear it all before you get to see it. so in short use some time wasting loops in it if you do use system("cls");

Mr. Cool, what do you do when you have to run the program on a system that does not have the CLS operating system command?

Here at DaniWeb, we try to give help that doesn't rely on system-specific solutions. Solutions that can work for as many people as possible.

Unfortunately, clearing the screen is impossible to do without platform specificity.

mugenoid, let us know what OS you are using and we'll give you a good solution.

Mr. Cool, unless you are running on hardware as old or older than an AT, it is just way too fast to make coding no-op loops useful. Even then, it is a bad idea, as the OS can do better for just pausing long enough for the user to read the screen...

I agree with Duoas. If you want delays used timed functions like delay() or sleep() or whatever else.


If you are doing a txt game ... the screen can be cleared using clrscr() (conio.h)

If you want a proper screen under DOS, look up Mode13h on the net . You will need a compiler like DJGPP. It's kinda hard to get into that particular mode with std compilers these days .... you have to twiddle with the settings, or use a compiler with protected DOS memory management like DJGPP.

If you are under windows (any), and want to do the rasterization (draw yourself), you should checkout which gives you a frame buffer to play with just like Mode 13 h.

Once you have a frame buffer, you can pretty much do anything ... draw lines, circles , quads etc etc , for any kinda game. ^^

Later you should checkout OpenGL and DirectX.

So... whatcha making?
Tic Tac Toe?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.