Thread: Help porting two console functions?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by MK27 View Post
    Does it work?
    I actually don't use ncurses that much, and usually via the perl API, so I keep getting object notation flashing in my mind. Using the return value of initscr() in getmaxyx() seems a little unorthodox to me, unless you are planning to call get_screen_height_and_width() BEFORE set_row_and_column_character(), since I would guess calling mvaddch() BEFORE initscr() will be a problem. And if that's all you are doing, you don't need to keep the return value of initscr(), so probably all this is fine.

    Looking thru some old code you don't need to keep the initscr() return normally either, there is the predefined "stdscr":

    Code:
    getmaxyx(stdscr,maxy,maxx);
    Again, I assume initscr() must be called first.

    I still haven't found a nice indexed version of the C API beyond the man pages.
    Ah, okay, thanks. So something like this then:

    Code:
    #include "stdlib.h" // for size_t
    #if defined( WIN32 ) || defined( MSWIN )
    #include "windows.h"
    // zero-based indexing
    void set_row_and_column_character( size_t row, size_t column, unsigned char character )
    {
        COORD
            position;
        position.X = column;
        position.Y = row;
        SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), position );
        putchar( character );
    }
    // for resizeable screens, calculation should be done dynamically
    void get_screen_height_and_width( size_t* height, size_t* width )
    {
        CONSOLE_SCREEN_BUFFER_INFO
            info;
        GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &info );
        *width = info.srWindow.Right;
        *height = info.srWindow.Bottom - info.srWindow.Top;
    }
    #elif defined( NCURSES )
    #include "curses.h"
    void initscr_helper_( void )
    {
        static WINDOW*
            unused = initscr( );	
    }
    void set_row_and_column_character( size_t row, size_t column, unsigned char character )
    {
        initscr_helper_( );
        mvaddch( row, column, character );
    }
    void get_screen_height_and_width( size_t* height, size_t* width )
    { 
        initscr_helper_( );
        getmaxyx( stdscr, *height, *width );
    }
    #elif defined( NextPlatformNamePlaceHolder_ )
    // platform implementation
    #else
    #error Console platform not specified
    #endif
    Last edited by Sebastiani; 04-20-2010 at 01:06 PM. Reason: ncurses fix
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exchange functions demo code
    By kryptkat in forum C++ Programming
    Replies: 0
    Last Post: 04-05-2009, 02:06 PM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  5. Console Functions
    By unanimous in forum C++ Programming
    Replies: 1
    Last Post: 12-28-2001, 08:09 PM