This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
oli414's profile picture

[CC 1.76] Pixel Canvas API

Started by oli414, 12 December 2015 - 09:51 PM
oli414 #1
Posted 12 December 2015 - 10:51 PM
Hey folks, as you may know from my previous post I really enjoy the new characters, especially the pixel characters.

To make it easier to use these characters in an effective way I've created this simple to use API:
http://pastebin.com/TFFJwxvp

It contains two classes, the character class and the canvas class.
Character Class (Please note that this class isn't really ment to be used outside of the API):
textColor :: color :: The foreground color
backgroundColor :: color :: The background color
pixelMode :: bool :: true if the character is made out of seperate pixels
pixel :: bool[2][3] :: a two dimensionay array, each element represents a pixel. A character consist out of 2 by 3 pixels
character :: char :: the actual character
invert :: bool :: wether or not the text- and backgroundcolor are switched around (required for some pixel combinations)

create() :: returns a new character object
update() :: sets the "character" and "invert" member based on the values in the "pixel" member array
draw() :: writes the character (or pixels) with the right text and background color

Canvas Class:
x :: number :: x position of the canvas on the terminal
y :: number :: y position of the canvas on the terminal
character :: Character[<canvasWidth>][<canvasHeight>] :: an two-dimensional array containing all the characters

create() :: returns a new canvas object.
setSize() :: set the size of the canvas. Clears all the characters.
setPixel(number x, number y, bool value) :: set a pixel within the canvas. Please note that these positions are in pixels and not in character row/columns. For example, to set the right most pixel the X would have to be twice the canvas width. The lowest pixel would be three times the canvas height (each pixel consist out of 2x3 pixels).
setCharacter(number x, number y, char char) :: set a character on the canvas.
draw() :: draws the canvas to the screen.

Example:

local canvas = Canvas.create()
for i=1,20 do
  canvas:setPixel(i, i, true)
  canvas:setPixel(28 - i, i + 4, true)
  canvas:setPixel(26 - i, i + 2, true)
  canvas:setPixel(8, i + 8, true)
  canvas:setPixel(i + 8, 28, true)
end
canvas:draw()
term.setCursorPos(1, 18)
which would draw:



And that's it, hope you like it :-)

-Oli414
Edited on 12 December 2015 - 10:08 PM
Quartz101 #2
Posted 12 December 2015 - 11:37 PM
I just noticed… with these pixels… HIGHER RES?!?!!?!?!?!!?!?!?
Bomb Bloke #3
Posted 12 December 2015 - 11:54 PM
Yes, about six times. There's a catch, though: You can only use two colours within every 2x3 pixel block! Even so, that's a big step up. :)/>
Creator #4
Posted 12 December 2015 - 11:58 PM
How?
oli414 #5
Posted 13 December 2015 - 12:25 AM
^ Which reminds me of the MCI graphic mode on the Commodore 64 where only 4 colors could be used per 8*8 cell (As seen in this great video)

It adds a whole lot of new possiblities. Especially for the OS makers.
However, it will require some great API to make it easy to use (which this isn't). Can't wait to see what people will come up with.
Bomb Bloke #6
Posted 13 December 2015 - 01:17 AM
You've got a good start here, yourself. How about some vector-plotting functions? :)/>

How?

If you divide a character up into six squares - in a 2x3 shape - and say each square can either be "on" or "off", you get 2^6 = 64 possibilities.

Characters 128 - 159 of CC 1.76's new character set represent exactly half of these. Inverting your text/background colours gives you the other half. You can hence use these characters to plot out images using effectively six times the resolution previously possible - for example, a 51x19 display becomes 102x57. Oh, and these new "pixels" are actually square, too.

However, since every group of six "pixels" is actually just a regular character, colour depth is the price you pay for the extra display resolution: you can't use six colours per character (one per pixel), you can only use two.
oli414 #7
Posted 13 December 2015 - 10:57 AM
So I was thinking about the color limitations last night, and I came up with a solution where it's possible to have as many colors as you want with a smaller resolution than the original character size. If you make the pixels 2x2 each character would hold 1 and a half pixel. Meaning there are only two colors ever needed per character.

If I have the time I'd like to spend some on a graphics render API with several overlay-modes taking care of the limited color problem.

Can't wait to see what others come up with with these new characters. The possibilities are endless.
Bomb Bloke #8
Posted 13 December 2015 - 11:06 AM
So I was thinking about the color limitations last night, and I came up with a solution where it's possible to have as many colors as you want with a smaller resolution than the original character size. If you make the pixels 2x2 each character would hold 1 and a half pixel. Meaning there are only two colors ever needed per character.

That's pretty clever - no increase in screen width, but a 50% increase in height, with no loss of colour depth. A 51x19 screen becomes something like 51x28.5.

One thing I noticed is that the colour bleed along the left of the CC display is especially annoying when these characters are used.
oli414 #9
Posted 13 December 2015 - 11:11 AM
One thing I noticed is that the colour bleed along the left of the CC display is especially annoying when these characters are used.

Yeah it is, took me a while to realise it was even there, at first I thought the characters contained 9 pixels because of that.