{"id":751,"date":"2014-06-21T00:01:31","date_gmt":"2014-06-21T07:01:31","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=751"},"modified":"2014-06-14T07:26:16","modified_gmt":"2014-06-14T14:26:16","slug":"the-big-text-mode-graphical-grid","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=751","title":{"rendered":"The Big (Text Mode) Graphical Grid"},"content":{"rendered":"<p>To output graphics, you need a palette. That is, you need a buffer that&#8217;s so many pixels wide and rows high. Create the buffer, draw in the buffer, output the buffer. That&#8217;s how  graphics works, both in graphics as well as text mode.<br \/>\n<!--more--><br \/>\nFor text mode graphics, you start by creating a graphics buffer, which is basically a <em>char<\/em> array. You could build a single dimension array, but use two dimensions to reflect the <em>x<\/em> and <em>y<\/em> coordinates of the grid. Here&#8217;s what I came up with:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define WIDTH 60\r\n#define HEIGHT 20\r\n\r\nint main()\r\n{\r\n    char grid[HEIGHT][WIDTH];\r\n    int x,y;\r\n\r\n<span class=\"comments\">\/* Initialize grid *\/<\/span>\r\n    for(y=0;y&lt;HEIGHT;y++)\r\n        for(x=0;x&lt;WIDTH;x++)\r\n            grid[y][x] = ' ';\r\n\r\n<span class=\"comments\">\/* display grid *\/<\/span>\r\n    for(y=0;y&lt;HEIGHT;y++)\r\n    {\r\n        for(x=0;x&lt;WIDTH;x++)\r\n            putchar(grid[y][x]);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The text mode graphical grid&#8217;s dimension is set at Lines 3 and 4. The <code>WIDTH<\/code> and <code>HEIGHT<\/code> constants correspond to character columns and rows.<\/p>\n<p>Line 8 declares the grid.<\/p>\n<p>Lines 12 through 14 initialize the grid, filling it with space characters.<\/p>\n<p>Lines 17 through 22 output the grid, breaking it up into rows.<\/p>\n<p>What this code does is rather simple, but the potential is great.<\/p>\n<p>When compiled and run, the code outputs a huge block of . . . nothing! Figure 1 shows the output from my computer&#8217;s terminal window.<\/p>\n<div id=\"attachment_752\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-752\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/TerminalScreenSnapz001.png\" alt=\"Figure 1.\" width=\"585\" height=\"414\" class=\"size-full wp-image-752\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/TerminalScreenSnapz001.png 585w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/TerminalScreenSnapz001-300x212.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/TerminalScreenSnapz001-423x300.png 423w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><p id=\"caption-attachment-752\" class=\"wp-caption-text\">Figure 1. The text mode graphical grid is displayed in a terminal window.<\/p><\/div>\n<p><em>Yawn . . .<\/em><\/p>\n<p>The canvas is empty, and like all blank canvases, it&#8217;s boring.<\/p>\n<p>Oh, yeah: Some avant garde artist somewhere can do an installation of blank canvases. I don&#8217;t think the university professors are giving out grades for such things any longer. No, a blank canvass begs for something to be drawn on it. That missing part of the code is shown below, from Lines 16 through 18:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define WIDTH 60\r\n#define HEIGHT 20\r\n\r\nint main()\r\n{\r\n    char grid[HEIGHT][WIDTH];\r\n    int x,y;\r\n\r\n<span class=\"comments\">\/* Initialize grid *\/<\/span>\r\n    for(y=0;y&lt;HEIGHT;y++)\r\n        for(x=0;x&lt;WIDTH;x++)\r\n            grid[y][x] = ' ';\r\n\r\n<span class=\"comments\">\/* draw a vertical line *\/<\/span>\r\n    for(y=0;y&lt;HEIGHT;y++)\r\n        grid[y][WIDTH\/2] = '|';\r\n\r\n<span class=\"comments\">\/* display grid *\/<\/span>\r\n    for(y=0;y&lt;HEIGHT;y++)\r\n    {\r\n        for(x=0;x&lt;WIDTH;x++)\r\n            putchar(grid[y][x]);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>After the grid is created, a vertical line is drawn by using the | (pipe) character. Line 18 does the job:<\/p>\n<p><code>grid[y][WIDTH\/2] = '|';<\/code><\/p>\n<p>The center of the <code>grid<\/code> is calculated by dividing the <code>WIDTH<\/code> in two. Then the <code>|<\/code> character is placed in that column for each row (variable <code>y<\/code>) in the <code>grid<\/code> array.<\/p>\n<p>This drawing takes place after the <code>grid<\/code> (palette) is created. When the <code>grid<\/code> is displayed, the line appears, as shown in Figure 2.<\/p>\n<div id=\"attachment_753\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-753\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/TerminalScreenSnapz002.png\" alt=\"Figure 2. Text mode graphics drawing success! A vertical line.\" width=\"585\" height=\"414\" class=\"size-full wp-image-753\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/TerminalScreenSnapz002.png 585w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/TerminalScreenSnapz002-300x212.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/TerminalScreenSnapz002-423x300.png 423w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><p id=\"caption-attachment-753\" class=\"wp-caption-text\">Figure 2. Text mode graphics drawing success! A vertical line.<\/p><\/div>\n<p>This code is pretty crude, but it works. A better solution would be to craft a function that actually draws a line between two points on the grid. That&#8217;s also possible. In fact, you can craft functions that draw circles, squares, polygons, and more. As long as you work within the <em>char<\/em> array and then display it, all kinds of text mode graphics are possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Text mode graphics are crude, but entirely possible by using a text mode character grid. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=751\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-751","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=751"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/751\/revisions"}],"predecessor-version":[{"id":776,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/751\/revisions\/776"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}