Language C - Position my Printf in X and Y on the screen.

Click For Summary
SUMMARY

The discussion focuses on positioning the output of the printf function in a C program using Microsoft Visual Studio 2010 on Windows 7. The user encounters issues with positioning after printing a 2D array labyrinth map. The provided code utilizes the gotoxy function to set cursor positions, but fails when the map is displayed. The suggestion to use ANSI codes for better control over text positioning is highlighted as a potential solution.

PREREQUISITES
  • Understanding of C programming language and syntax
  • Familiarity with Microsoft Visual Studio 2010 environment
  • Knowledge of console screen buffer manipulation in Windows
  • Basic understanding of ANSI escape codes for text formatting
NEXT STEPS
  • Research ANSI escape codes for cursor positioning in console applications
  • Explore advanced console manipulation techniques in C
  • Learn about the Windows Console API for enhanced output control
  • Investigate alternative libraries for console graphics in C, such as ncurses
USEFUL FOR

Programmers working on character-based games, C developers looking to enhance console output, and anyone interested in mastering text positioning in console applications.

Max0007
Messages
66
Reaction score
1

Homework Statement


I work on Microsoft Visual studio 2010 on Win7.

Ok so let me explain the problem here, The functions works very good to position my printf in X and Y on my screen ONLY when my 2D Array labyrinth Map is NOT printed on the screen.

But once I printf my Map on the screen I cannot position the printf in X and Y.

This is the current code I use to position my printf and only works when my Map is not printed on the screen.

gotoxy(posx,5); clreoscr();
textcolor(15);
posx = printf("\nSpeed of Character: %d / %d",speed, m1);
textcolor(15);
posx = printf("\nSpeed of enemie: %d / %d", speed_e, m2);This is the functions I have to use to give my printf a position on my screen.
----------------------------------------------------------------------------------------------------------------------------
void gotoxy(int x,int y) //positions text cursor at (x, y) screen position
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; //variable declarations
HANDLE hConsoleOut;

hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);

csbiInfo.dwCursorPosition.X = x; //cursor position X coordinate
csbiInfo.dwCursorPosition.Y = y; //cursor position Y coordinate
SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition); //set cursor at the given screen coordinate
}/*-------------------------wherex()------------------------------------*/

int wherex() //returns current text cursor (x) coordinate
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
return csbiInfo.dwCursorPosition.X;
}

/*-------------------------wherey()----------------------------------*/

int wherey() //returns current text cursor (y) coordinate
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
return csbiInfo.dwCursorPosition.Y;
}

Homework Equations


n/A

The Attempt at a Solution


N/A
 
Physics news on Phys.org
If this is a character based game then have you looked at ANSI codes?

http://en.m.wikipedia.org/wiki/ANSI_codes

I've,used them a lot in my character based programs and scripts to control where I print things to the display. They work in all sorts of languages once you learn how to represent escape codes in your program.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K