From 1f19d9127a912f2851c46af538280d3e034b7227 Mon Sep 17 00:00:00 2001 From: Ole Morud Date: Thu, 9 Jun 2022 19:10:47 +0200 Subject: [PATCH] sub pixel rendering --- config.h | 4 ++-- graphics.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- graphics.h | 1 + physics.c | 26 +++++++++--------------- 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/config.h b/config.h index c3c6ebd..ad1751d 100644 --- a/config.h +++ b/config.h @@ -3,12 +3,12 @@ #define CONFIG_H_ -#define ON 'x' +#define ON '#' #define OFF ' ' #define HEIGHT 100 #define WIDTH 100 #define FRAMERATE 60 #define GRAVITY 15.0f -#define LINENUMBERS 1 +#define LINENUMBERS 0 #endif diff --git a/graphics.c b/graphics.c index 75b1656..a9d2b51 100644 --- a/graphics.c +++ b/graphics.c @@ -3,6 +3,7 @@ #include #include #include "config.h" +#include #define uint unsigned int @@ -19,6 +20,7 @@ void clear(); void render(Canvas* c); void line(Canvas* c, uint start_x, uint start_y, uint end_x, uint end_y); void dot(Canvas* c, const uint x, const uint y); +void subpixel(int m1, int m2, int n1, int n2); /* @@ -49,6 +51,34 @@ void render(Canvas* c){ } +/* + * Renders canvas with subpixel rendering + * */ +void subpixel_render(Canvas* c){ + setlocale(LC_ALL, "C.UTF-8"); + clear(); + putchar('\n'); + + for(int i=0; i < c->y; i+=2){ +#if LINENUMBERS + printf("%2i ", i*2); // horizontal line numbers +#endif + for(int j=0; j < c->x; j+=2){ + subpixel( c->data[i*(c->x) + j] == ON, c->data[i*(c->x) + j+1] == ON, + c->data[(i+1)*(c->x) + j] == ON, c->data[(i+1)*(c->x) + j+1] == ON); + } + putchar('\n'); + } + +#if 0 + // vertical line numbers + for(int i=0; i<(c->x) / 2; i+=2){ + printf(" %2i", i*2); + } +#endif +} + + /* * Sets pixel at x,y to ON */ @@ -100,9 +130,33 @@ void line(Canvas* c, uint start_x, uint start_y, uint end_x, uint end_y){ } -//TODO: find escape sequence for clearing screen +/* + * Clears terminal (for ANSI like terminal emulation) + */ void clear(){ printf("\033[2;2H"); - //printf("function clear() not implemented yet"); +} + +/* + * Print 4 pixels as unicode character + * pixel arrangement is like this: + * m1,m2 1 0 + * n1,n2, example: 0 1 = '▚' + */ +void subpixel(int m1, int m2, int n1, int n2){ + const wchar_t *quads[] = + { + /* 00, 01, 10, 11*/ + /*00*/ L" ", L"▝", L"▘", L"▀", + /*01*/ L"▗", L"▐", L"▚", L"▜", + /*10*/ L"▖", L"▞", L"▌", L"▛", + /*11*/ L"▄", L"▟", L"▙", L"█", + }; + + int x = !!m2 + (!!m1<<1); + int y = !!n2 + (!!n1<<1); + + printf("%ls", quads[y*4 + x]); + printf("%s", "" ); } diff --git a/graphics.h b/graphics.h index 607fa87..9ae7181 100644 --- a/graphics.h +++ b/graphics.h @@ -10,6 +10,7 @@ typedef struct Canvas { void clear(); void render(Canvas*); +void subpixel_render(Canvas*); void line(Canvas*, unsigned int, unsigned int, unsigned int, unsigned int); void dot(Canvas*, const unsigned int, const unsigned int); diff --git a/physics.c b/physics.c index 54fe277..2c64479 100644 --- a/physics.c +++ b/physics.c @@ -43,31 +43,24 @@ void update_link(Link* l); * Main */ int main(){ -#if 0 - Point a = {0, 0}; - Point b = {10, 10}; - unit_vec(&a, &b); -#endif - -#if 1 char *data = malloc(WIDTH*HEIGHT); Canvas cnv = {WIDTH, HEIGHT, data}; // Add 4 points, link them, and add points and links to respective arrays - Point a = { 10.0, 10.0, - 11.0, 10.0, + Point a = { 20.0, 10.0, + 21.0, 10.0, 0.0, GRAVITY/(FRAMERATE*FRAMERATE)}; - Point b = { 20.0, 10.0, - 20.0, 10.0, + Point b = { 40.0, 10.0, + 40.0, 10.0, 0.0, GRAVITY/(FRAMERATE*FRAMERATE)}; - Point c = { 20.0, 20.0, - 19.0, 20.0, + Point c = { 40.0, 30.0, + 39.0, 30.0, 0.0, GRAVITY/(FRAMERATE*FRAMERATE)}; - Point d = { 10.0, 20.0, - 10.0, 20.0, + Point d = { 20.0, 30.0, + 20.0, 30.0, 0.0, GRAVITY/(FRAMERATE*FRAMERATE)}; Link *l_ab = link_points(&a, &b); @@ -98,10 +91,9 @@ int main(){ } // then render and wait - render(&cnv); + subpixel_render(&cnv); usleep(1000000/FRAMERATE); } -#endif return 0; }