textmode renderer

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
freqit
Posts: 15
Joined: February 18th, 2017, 4:47 pm

textmode renderer

Post by freqit » May 14th, 2017, 10:34 am

Hi, I'm working on a small platform for textmode rendering in linux terminal. This is what I got so far.
Why am I doing this? I currently only own a Mac laptop and when I'm away from work I can't follow along and code in c++ with chili's framework so I'm just doing this to learn atleast something while I'm at work.

Code: Select all


#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>

int ScreenWidth = 178;
int ScreenHeight = 54;

int CheckWindowSize()
{
    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    int x = w.ws_col;
    int y = w.ws_row;

    printf ("lines %d\n", y);
    printf ("columns %d\n", x);

    return(x,y);
}

int main()
{
    //int testx; int testy;
    CheckWindowSize();
    //printf("test: %d, %d\n", testx, testy);

    char grid[ScreenHeight][ScreenWidth];

    int x,y;

/* Initialize grid (clear screen) */
    for(y=0;y<ScreenHeight;y++)
        for(x=0;x<ScreenWidth;x++)
            grid[y][x] = ' ';

/* draw a vertical line */
    for(y=0;y<ScreenHeight;y++)
        grid[y][ScreenWidth/2] = '|';

/* draw a horizontal line */
    for(x=0;x<ScreenWidth; x++)
        grid[ScreenHeight/2][x] = '-';

/* render to textmode grid */
    for(y=0;y<ScreenHeight;y++)
    {
        for(x=0;x<ScreenWidth;x++)
            putchar(grid[y][x]);
        putchar('\n');
    }

    return(0);
}

As im quite new with programming I'm a bit stuck here. I need somehow a "timer" so I can start doing animations. Any suggestions on where to look or a small example?

Thanks guys!

freqit
Posts: 15
Joined: February 18th, 2017, 4:47 pm

Re: textmode renderer

Post by freqit » May 14th, 2017, 11:21 am

Correction on the CheckWindowSize function

Code: Select all

int CheckWindowSize()
{
    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    int x = w.ws_col;
    int y = w.ws_row;
//Debug prints
    printf ("lines (y) %d\n", y);
    printf ("columns (x) %d\n", x);

    ScreenWidth = x;
    ScreenHeight = y;

    return 0;
}


albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: textmode renderer

Post by albinopapa » May 14th, 2017, 5:36 pm

as far as a timer goes, the chili framework's timer is standard C++ and not platform specific if I remember correctly.

Just look into std::chrono::steady_clock or std::chrono::high_resolution_clock.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: textmode renderer

Post by albinopapa » May 14th, 2017, 5:53 pm

Code: Select all


#include <chrono>

class Timer
{
public:
	Timer() : tp( std::chrono::steady_clock::now() ) {}
	float Mark()
	{
		const auto ntp = std::chrono::steady_clock::now();
		const float mark = std::chrono::duration<float>( ntp - tp ).count();
		tp = ntp;
		return mark;
	}

private:
	std::chrono::steady_clock::time_point tp;
};

int main()
{
	Timer t;	
	_sleep( 1000ul );
	auto dt = t.Mark();
	std::cout << dt << std::endl;
	_sleep( 1000ul );
	dt = t.Mark();
	std::cout << dt << std::endl;

	return 0;
}
See if this works. At the beginning of each frame, call timer.Mark() to get the elapsed time since last call to Mark.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: textmode renderer

Post by chili » May 15th, 2017, 3:32 am

I too recommend using the standard library when possible. Can't really say much about stuff like sys/ioctl; that's a linux-specific header. If you're going non std route there, maybe take a look at the curses library. It's got a lot of features, and it has ports to Windows etc.

Also, if you wanna do stuff from the tutorials on your Mac, you can put Oracle Virtual Box on your machine and get some Win 7 action on there. That will be enough for most of the tutorial stuff.
Chili

Post Reply