Jump to content




Draw API [1.3]



  • You cannot reply to this topic
11 replies to this topic

#1 wynro

  • Members
  • 6 posts
  • LocationIn my own world

Posted 13 March 2012 - 11:20 AM

Well as my first experiment in LUA I had created a little API to use.

This API are usefull for those to love to create great pixel art and complex draw.

Tutorial and functions
Spoiler

V 0.1 (Easy version):
Spoiler

V 0.3(Object-oriented version)
Spoiler

V 0.4
Spoiler

V 0.5
Spoiler


Work to do:
-More Objects(circles...)
-Make the code more easy-to-read(comment the code, improve some variables, etc)
-And the best of all: 3D support (by now it only can draw a cube in oblique projection, but this will be better)
-Upload the code to github or other server instead of copy the code into the post(edit this is a huge pain)
-Custom colors(with custom fonts like in MiM)
-Any idea you can say.

(Thank you to mrgreaper for the usefull (and a little obvious :mellow:/> ) advice)

#2 mrgreaper

  • Members
  • 88 posts

Posted 15 March 2012 - 03:30 AM

View Postwynro, on 13 March 2012 - 11:20 AM, said:



P.D: Oh C'mon! More than 200 views and any single reply?

you havent explained how to use the api

give an example, show us the codes we can use , i scrolled through it was about to hit back then i saw your comment...well if you want more replies tell us how to use it :D/>

#3 princepsp

  • New Members
  • 3 posts

Posted 16 March 2012 - 12:19 PM

it gives error where i will write this?

#4 wynro

  • Members
  • 6 posts
  • LocationIn my own world

Posted 16 March 2012 - 03:05 PM

View Postprincepsp, on 16 March 2012 - 12:19 PM, said:

it gives error where i will write this?

I dont understand. If you dont say the error I cant help you.

#5 Ray1235

  • New Members
  • 3 posts

Posted 16 March 2012 - 05:42 PM

got an error when i made array. It wants me to end array at 2nd line.
I need help because i am making nazi zombies map.
Code:
local side = "right"
local character = "O"
local screen = false
local monitor = peripheral.wrap( side )
function setMonitor( appear, side )
  screen = appear
  side = side
end
local function approx( var )
  decimal = var-math.floor(var)
  entera = math.floor(var)
  if decimal>= 0.5 then
	    return entera+1
  else
	    return entera
  end
end
function setChar( char )
  character = char
end
function draw( x, y )
  if screen then
	    term.redirect( monitor )
term.setCursorPos(x,y)
write(character)
term.restore()
  else
	    term.setCursorPos(x,y)
write(character)
  end
end
local line = {

  draw = function ( self )
	    drawline( self )
return true
  end,

  move = function(self, x, y)
	    return newLine(
   self.x0 + x,
   self.y0 + y,
   self.x1 + x,
   self.y1 + y
)
  end,

  length = function( self )
return math.sqrt( (self.x1-self.x0)^2 + (self.y1-self.y0)^2 )
  end,

  tostring = function( self )
	    return self.x0..", "..self.y0..", "..self.x1..", "..self.y1
  end,

}
local lmetatable = {
__index = line,
__draw = line.draw,
__move = line.move,
-- __turn = line.turn,
__length = line.length,
__tostring = line.tostring,
}
function newLine( x0, y0, x1, y1 )
  local l = {
	    x0 = x0 or 0,
	    y0 = y0 or 0,
	    x1 = x1 or 0,
	    y1 = y1 or 0
  }
  setmetatable( l, lmetatable )
  return l
end
function drawline( line )
  if line.x0==line.x1 and line.y0==line.y1 then
	    return false
  end

  if line.x0==line.x1 then
	    if line.y0>line.y1 then
		  inc=-1
else
   inc=1
end
	    for y=line.y0, line.y1, inc do
   draw(line.x0, y)
end
return true
  end

  if line.y0==line.y1 then
	    if line.x0>line.x1 then
   inc=-1
else
   inc=1
end
for x=line.x0, line.x1, inc do
   draw(x, line.y0)
end
return true
  end

  if line.x0~=line.x1 and line.y0~=line.y1 then
	    m=((line.y1 - line.y0)/(line.x1 - line.x0))


if m<1 and m>-1 then
   n=line.y0-m*line.x0
   if line.x0>line.x1 then
			    inc=-1
   else
		 inc=1
   end
		  for x=line.x0, line.x1, inc do
		 y=m*x+n
  y=approx(y)
		 draw(x, y)
   end
   return true
else
   m=(line.x1-line.x0)/(line.y1 - line.y0)
   n=line.x0-m*line.y0
   if line.y0>line.y1 then
			    inc=-1
   else
		 inc=1
   end
		  for y=line.y0, line.y1, inc do
		 x=m*y+n
  x=approx(x)
		 draw(x, y)
   end
   return true
end
  end
end
local rectangle = {

  draw = function ( self, mode)
  
	    if mode then
   if monitor then
		 drawrectanglefull( self ,true)
   else
		 drawrectanglefull( self ,false)
		  end
else
   if monitor then
		 drawrectangle( self ,true)
   else
		 drawrectangle( self ,false)
		  end
end
return true
  end,

  move = function(self, x, y)
	    return newRectangle(
   self.x0 + x,
   self.y0 + y,
   self.x1 + x,
   self.y1 + y
)
  end,

  area = function( self )
return (self.x1-self.x0)^2 + (self.y1-self.y0)^2
  end,

}
local rmetatable = {
__index = rectangle,
__draw = rectangle.draw,
__move = rectangle.move,
-- __turn = rectangle.turn,
__area = rectangle.area,
}
function newRectangle(x0, y0, x1, y1)
  local r = {
	    x0 = x0 or 0,
	    y0 = y0 or 0,
	    x1 = x1 or 0,
	    y1 = y1 or 0
  }
  setmetatable( r, rmetatable )
  return r
end
function drawrectangle( rectangle )
  if rectangle.x0<rectangle.x1 then
	    inc=1
  else
	    inc=-1
  end
  for x=rectangle.x0, rectangle.x1, inc do
	    draw(x, rectangle.y0)
	    draw(x, rectangle.y1)
  end
  if rectangle.y0<rectangle.y1 then
	    inc=1
  else
	    inc=-1
  end
  for y=rectangle.y0, rectangle.y1, inc do
	    draw(rectangle.x0, y)
	    draw(rectangle.x1, y)
  end
end
function drawrectanglefull( rectangle )
  if rectangle.x0<rectangle.x1 then
	    incx=1
  else
	    incx=-1
  end
  if rectangle.y0<rectangle.y1 then
	    incy=1
  else
	    incy=-1
  end
  for y=rectangle.y0, rectangle.y1, incy do
	    for x=rectangle.x0, rectangle.x1, incx do
		  draw(x, y)
end
  end
end
local image = {
  draw = function (self, x, y )
	    drawimage( self, x, y )
return true
  end,
}
local imetatable = {
__index = image,
__draw = image.draw,
}
function newImage( data )
  local i = {
	    height = #data or 0,
	    data = data or 0
  }
  setmetatable( i, imetatable )
  return i
end
function drawimage( image, x, y )
  if screen then
	    term.redirect( monitor )
	    y = y - 1
	    for h=1, image.height, 1 do
		  y = y + 1
		  term.setCursorPos(x,y)
		  write(image.data[h])
	    end
term.restore()
  else
	    y = y - 1
	    for h=1, image.height, 1 do
		  y = y + 1
		  term.setCursorPos(x,y)
		  write(image.data[h])
	    end
  end
  return true
end
local tonext = 60
local wave = 1
local i = 1
wave1 = {													   
"|"
"|"
"|"
"|"
"|"
}
wave2 = {													
"| |"
"| |"
"| |"
"| |"
"| |"
}
wave3 = {													
"| | |"
"| | |"
"| | |"
"| | |"
"| | |"
}
wave3 = {											 
"| | |"
"| | |"
"| | |"
"| | |"
"| | |"
}
wave4 = {													
"| | | |"
"| | | |"
"| | | |"
"| | | |"
"| | | |"
}
wave5 = {												  
"| | | |"
"| | | |"
"+-+-+-+"
"| | | |"
"| | | |"
}
term.setCursorPos(9,1)
print("--	    SYSTEM ERROR	    --")
print("--   Power level is critical  --")
print("-- Shutting down Pack-a-punch --")
print("We'll need to turn it on somehow...")
for i=99,1,-1 do
term.clear()
term.setCursorPos(9,9)
term.clear()
line = draw.newLine(1,1,7,9)  --create a line from 1,1 to 7,9
line:draw()							    --draw the line into the console
line = line:move(5,3)		    --move the line 5 to right and 3 to down
line:draw()
sleep(2)
term.clear()
draw.setMonitor(true, "right")			  --select the monitor in the left side
rectangle = draw.newRectangle(1,1,5,6)  --create a create a rectangle with 1,1 and 5,3 as opossed vertex
rectangle:draw(true)								    --draw the rectangle with color
sleep(1)
draw.setChar("H")										  --select H as the default char
rectangle = draw.newRectangle(1,9,8,14)
rectangle:draw(false)							  --draw the rectangle (only the edges)
sleep(1)
term.clear()
draw.setMonitor(false)						    --deselect the monitor
term.clear()
if wave = 1 do
image = draw.newImage(wave1)				   --create an image with the previous array
image:draw(9,9)										   --draw the image in the selected coordinates
end
if wave = 2 do
image = draw.newImage(wave2)				   --create an image with the previous array
image:draw(9,9)  
end
if wave = 3 do
image = draw.newImage(wave3)				   --create an image with the previous array
image:draw(9,9)	
end
if wave = 4 do
image = draw.newImage(wave4)				   --create an image with the previous array
image:draw(9,9)	
end
if wave = 5 do
image = draw.newImage(wave5)				   --create an image with the previous array
image:draw(9,9)	
end
if wave > 5 do
term.setCursorPosition(9,9)
print("Wave "..wave)
end
sleep(6*wave)
tonext = 6*wave
wave = wave + 1
end


#6 wynro

  • Members
  • 6 posts
  • LocationIn my own world

Posted 17 March 2012 - 08:43 AM

View PostRay1235, on 16 March 2012 - 05:42 PM, said:

got an error when i made array. It wants me to end array at 2nd line.
I need help because i am making nazi zombies map.

A little recomendation, for futures errors try to write the error message that gives you the computer.
Another tip when you are going to write a lot of code, try to use spoilers to avoid huge posts.
I assume that you have installed correctly the API.
Your problem is that this
wave1 = {	 
"|"
"|"
"|"
"|"
"|"
}
should be like this:
wave1 = {
"|",
"|",
"|",
"|",
"|",
}

And the ifs are:
if wave == 1 then
  image = draw.newImage(wave1)
  image:draw(9,9)
end
instead of:
if wave = 1 do
image = draw.newImage(wave1)
image:draw(9,9)
end
Note the then instead of the do, and the == instead of the =.

So the code fixed is:
Spoiler

If this code doesnt work, the problem is that you hasnt installed correctly the API

If you doesnt know about programming in LUA I recomend you to read this

#7 princepsp

  • New Members
  • 3 posts

Posted 17 March 2012 - 03:51 PM

Oww i understand i doesnt installed too

#8 Hapsburger

  • New Members
  • 1 posts

Posted 22 March 2012 - 04:40 AM

You can change the approx function to:
local function approx(var)
  return math.floor(var + 0.5)
end


#9 wynro

  • Members
  • 6 posts
  • LocationIn my own world

Posted 22 March 2012 - 04:09 PM

View PostHapsburger, on 22 March 2012 - 04:40 AM, said:

You can change the approx function to:
local function approx(var)
  return math.floor(var + 0.5)
end

Thank you for the tip :(/>

#10 Ray1235

  • New Members
  • 3 posts

Posted 30 March 2012 - 12:42 PM

Thank you. I used pascal mostly before and that's why i have errors. BTW thx u :o/>
EDIT: Still had errors and had to do it manually and I had erased Draw API code :o/>
BTW i made program for checking if the power is on and controls pack-a-punch door :)/>

#11 Smidge

  • New Members
  • 5 posts

Posted 01 August 2012 - 02:35 AM

View Postwynro, on 22 March 2012 - 04:09 PM, said:

View PostHapsburger, on 22 March 2012 - 04:40 AM, said:

You can change the approx function to:
local function approx(var)
  return math.floor(var + 0.5)
end

Thank you for the tip :ph34r:/>
But what if the value is negative? You would have to use math.ceil instead.

#12 Smidge

  • New Members
  • 5 posts

Posted 01 August 2012 - 02:37 AM

I've got a pretty good circle drawing function you can use. Not only does it draw circles but can also draw ellipsis' or in other words, ovals.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users