How do i make for example an x move using your arrows in a console application?

Dani AI

Generated

— good move from console to OpenGL. Quick summary: capture the arrow key events, update the triangle's position (a simple translation), then ask the windowing system to redraw. is right that a plain console is not designed for smooth animation, and 's tab/newline trick is only a hack. In OpenGL you handle input via your window toolkit (GLUT/freeglut, GLFW, SDL or native Win32), update a position variable, and then redraw the scene.

Here is a minimal GLUT-style example showing the idea (catch arrow keys, change translation, then redisplay):

float tx = 0.0f, ty = 0.0f;

void display() {
  glClear(GL_COLOR_BUFFER_BIT);
  glLoadIdentity();
  glTranslatef(tx, ty, 0.0f);
  // draw your red triangle here
  glutSwapBuffers();
}

void specialKeys(int key, int x, int y) {
  const float step = 0.05f;
  if (key == GLUT_KEY_LEFT)  tx -= step;
  if (key == GLUT_KEY_RIGHT) tx += step;
  if (key == GLUT_KEY_UP)    ty += step;
  if (key == GLUT_KEY_DOWN)  ty -= step;
  glutPostRedisplay();
}

Notes and tips: call the right callback for your toolkit (GLFW uses glfwSetKeyCallback, SDL uses SDL_PollEvent). For smooth motion handle key-down/key-up state or move per-frame using delta time instead of single-step jumps. If you use modern OpenGL, update a model matrix uniform instead of fixed-function calls like glTranslatef. If you want, post which toolkit you use and we can tailor the code.

Recommended Answers

All 4 Replies

You can't really. The console isn't meant for that sort of shennanigans. You could make something horribly ugly (with a lot of effort) I suppose...

lol, just throw a bunch of \t and \n if your really desperate for the functionality your seeking.

Ok fine.
What if i have this REALLY simple OpenGL program that only draws a red triangle.
How do i make the arrows on my keyboard move it ?

Bump!
Any help appreciateeeed...

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.