Posted: Thu May 03, 2012 4:04 pm Post subject: New Draw Module
I have been working on something to replace the Turing Draw Module with something that will hopefully be an improvement. The whole idea is to use a picture as the buffer for the screen, and since you can edit the values of the pixels in Turing in 24bit format, I thought it would be awesome if people could ditch the crappy 8 bit colors that are given in the Draw Module and replace it with this! The reason why I posted this in the help, is because I have never attempted making drawing methods before and I would like some help. Currently I added a few fun things into the class like Color Filter, Color Inverting. If anyone can help, it would be appreciated.
var imageX :int:=0 var imageY :int:=0 var pictureBound :int:=0 var picture :flexiblearray1.. 0of PictureVals
var colorBack : ColorValues
colorBack.CLR (1):=0% Blue
colorBack.CLR (2):=0% Green
colorBack.CLR (3):=0% Red
procedure NewLayer
end NewLayer
procedure Initialize (sizeX, sizeY :int)
pictureBound := sizeX * sizeY
if pictureBound > upper(picture)then new picture, pictureBound
endif for i :1.. pictureBound
picture (i).RGB := colorBack
endfor
imageX := sizeX
imageY := sizeY
put picture (upper(picture)).RGB.CLR (1) end Initialize
procedure ColorBack (R, B, G :int) if R < 256and B < 256and G < 256and R > -1and B > -1and G > -1then
colorBack.CLR (1):= B
colorBack.CLR (2):= G
colorBack.CLR (3):= R
endif Error.Halt("Color Must stay within the range of 0-255.") end ColorBack
procedure ColorInvert
for i :1.. pictureBound
picture (i).RGB.CLR (1):=255 - picture (i).RGB.CLR (1)
picture (i).RGB.CLR (2):=255 - picture (i).RGB.CLR (2)
picture (i).RGB.CLR (3):=255 - picture (i).RGB.CLR (3) endfor end ColorInvert
procedure ColorInvertArea (x1, y1, x2, y2 :int) for i : x1 .. x2
for j : y1 .. y2
if i < imageX and i > -1and j < imageY and j > -1then
picture ((j - 1)* imageX + i).RGB.CLR (1):=255 - picture ((j - 1)* imageX + i).RGB.CLR (1)
picture ((j - 1)* imageX + i).RGB.CLR (2):=255 - picture ((j - 1)* imageX + i).RGB.CLR (2)
picture ((j - 1)* imageX + i).RGB.CLR (3):=255 - picture ((j - 1)* imageX + i).RGB.CLR (3) endif endfor endfor end ColorInvertArea
procedure ColorFilter (R, G, B :int, p :real) for i :1.. pictureBound
picture (i).RGB.CLR (1):=(picture (i).RGB.CLR (1)*(1.0 - p) + B * p)div2
picture (i).RGB.CLR (2):=(picture (i).RGB.CLR (2)*(1.0 - p) + G * p)div2
picture (i).RGB.CLR (3):=(picture (i).RGB.CLR (3)*(1.0 - p) + R * p)div2 endfor end ColorFilter
procedure ColorFilterArea (x1, y1, x2, y2, R, G, B :int, p :real) for i : x1 .. x2
for j : y1 .. y2
if i < imageX + 1and i > -1and j < imageY + 1and j > -1then
picture ((j - 1)* imageX + i).RGB.CLR (1):=(picture ((j - 1)* imageX + i).RGB.CLR (1)*(1.0 - p) + B * p)div2
picture ((j - 1)* imageX + i).RGB.CLR (2):=(picture ((j - 1)* imageX + i).RGB.CLR (2)*(1.0 - p) + G * p)div2
picture ((j - 1)* imageX + i).RGB.CLR (3):=(picture ((j - 1)* imageX + i).RGB.CLR (3)*(1.0 - p) + R * p)div2 endif endfor endfor end ColorFilterArea
procedure DrawDot (x, y, R, G, B :int) if x < imageX + 1and x > -1and y < imageY + 1and y > -1then
picture ((y - 1)* imageX + x).RGB.CLR (1):= B
picture ((y - 1)* imageX + x).RGB.CLR (2):= G
picture ((y - 1)* imageX + x).RGB.CLR (3):= R
endif end DrawDot
procedure DrawLine (x1, y1, x2, y2, R, G, B :int) var distX :real:= x2 - x1 % 5 var distY :real:= y2 - y1 % 10 var totDist :real:=sqrt(distX * distX + distY * distY)
distX := distX / distY % 0.5
distY :=1% 1 var inc :real:=0 for y :0.. round(totDist)%25 + 100 = 11.18
DrawDot (x1 + round(inc), y1 + y, round(R *(inc rem1)),round(G* (inc rem1)),round(B* (inc rem1)))
DrawDot (x1 + round(inc + (1-(inc rem1))), y1 + y, round(R *(inc rem1)),round(G* (inc rem1)),round(B* (inc rem1)))
inc := inc + distX
endfor end DrawLine
procedure DrawRect (x1, y1, x2, y2, R, G, B :int) for i : x1 .. x2
for j : y1 .. y2
DrawDot (i, j, R, G, B) endfor endfor end DrawRect
procedure Update
var b :array1.. sizepic(1, 1, imageX, imageY)ofint cls takepic(1, 1, imageX, imageY, b) for i :19.. upper(b)
b (i):=0 endfor
b (7):=48 var id :=19 var imageID :int:=0 var n :int:=0 for j :1.. imageY
for i :1.. imageX
imageID := imageID + 1 for p :1.. 3
b (id)| = picture (imageID).RGB.CLR (p)shl n %b if n < 24then
n +=8 else
n :=0
id +=1 endif endfor endfor if n > 0then
id +=1
n :=0 endif endfor drawpic(1, 1, b, 1) var pic1 :int:=Pic.New(1, 1, imageX, imageY) var pic2 :int:=Pic.Scale(pic1, maxx, maxy) Pic.Draw(pic2, 0, 0, picCopy) View.Update Pic.Free(pic1) Pic.Free(pic2) end Update
procedure Cls
for i :1.. pictureBound
picture (i).RGB := colorBack
endfor end Cls
end Render
Posted: Fri May 04, 2012 10:05 am Post subject: Re: New Draw Module
i wouldnt mind some rounded rectangles, such as:
Turing:
proc drawfillroundbox (x1, y1, x2, y2, xr, yr, clr :int) var minx :int:=min(x1, x2) var miny :int:=min(y1, y2) var Maxx :int:=max(x1, x2) var Maxy :int:=max(y1, y2) var XR :int:=min(Maxx - minx, xr)% so it doesnt end up drawing a box var YR :int:=min(Maxy - miny, yr) drawfillarc(minx + XR, miny + YR, XR, YR, 180, 270, clr) drawfillarc(minx + XR, Maxy - YR, XR, YR, 90, 180, clr) drawfillarc(Maxx - XR, miny + YR, XR, YR, 270, 360, clr) drawfillarc(Maxx - XR, Maxy - YR, XR, YR, 0, 90, clr) drawfillbox(minx, miny + YR, Maxx, Maxy - YR, clr) drawfillbox(minx + XR, miny, Maxx - XR, Maxy, clr) end drawfillroundbox
proc drawroundbox (x1, y1, x2, y2, xr, yr, clr :int) var minx :int:=min(x1, x2) var miny :int:=min(y1, y2) var Maxx :int:=max(x1, x2) var Maxy :int:=max(y1, y2) var XR :int:=min(Maxx - minx, xr)% so it doesnt end up drawing a box var YR :int:=min(Maxy - miny, yr) drawarc(minx + XR, miny + YR, XR, YR, 180, 270, clr) drawarc(minx + XR, Maxy - YR, XR, YR, 90, 180, clr) drawarc(Maxx - XR, miny + YR, XR, YR, 270, 360, clr) drawarc(Maxx - XR, Maxy - YR, XR, YR, 0, 90, clr) drawline(minx + XR, miny, Maxx - XR, miny, clr) drawline(minx + XR, Maxy, Maxx - XR, Maxy, clr) drawline(minx, miny + YR, minx, Maxy - YR, clr) drawline(Maxx, miny + YR, Maxx, Maxy - YR, clr) end drawroundbox
edit for your module's style of course
also, the program either crashes ("segment violation" on line 163), says Halt is not on the export list of Error, or crashes Turing because of a bug in the environment
(depending on the version)
ps, what will newLayer do?
copthesaint
Posted: Tue May 08, 2012 2:03 pm Post subject: Re: New Draw Module
I have updated the module,
Currently you can draw:
- Ovals
- FillRectangles
- lines (Problem with rounding I think, if anyone wants to look at that.)
- polygons (Problem with lines causes problem with polygons)
Currently you can also:
-Invert Colors
-Fixed Color Filter
I wish I could have done this sooner but I have been busy.
@evildaddy that can definatly be done but right now Im just figuring the math out behind the draw modules. Its something ive never spent the time to learn or figure out.
@compsci, if anyone wants anything like evildaddy Its more then fine to post it here, but right now I just need reasorces for the mathematics of drawing objects by each individual pixel.
Posted: Wed May 09, 2012 9:10 pm Post subject: RE:New Draw Module
As I mentioned in the other post, I'd love 2D array buffers as opposed to 1D array buffers
copthesaint
Posted: Wed May 09, 2012 10:36 pm Post subject: RE:New Draw Module
Although I could do that, it would be useless because turing doesnt allow flexible arrays of fleixble array. plus manipulating a one dimensional array that holds only values specific to the pixel at that index is a hell of alot easier then dealing with a 1 dimensional buffer that holds more then one pixels values at a certain index point.
Raknarg
Posted: Thu May 10, 2012 12:44 pm Post subject: RE:New Draw Module
@copthesaint
var arr : flexible array 1 .. 3, 1 .. 3 of int
new arr, 2, 3
Works as long as you only change the first element. Maybe that helps.
Amarylis
Posted: Thu May 10, 2012 12:49 pm Post subject: RE:New Draw Module
Works with both of them
new ar, 1, 0 chances the array bounds to 1, 0
Raknarg
Posted: Thu May 10, 2012 12:50 pm Post subject: RE:New Draw Module
You're sure it works? Whenever I try to change the second element, it gives me an error, saying that it hasnt been implemented yet
Sponsor Sponsor
Amarylis
Posted: Thu May 10, 2012 1:27 pm Post subject: RE:New Draw Module
What version of Turing are you using?
Dreadnought
Posted: Thu May 10, 2012 1:49 pm Post subject: Re: New Draw Module
In the current implementation (1999), with a multi-dimensional array with a non-zero number of total elements, it is a run-time error to change any but the first dimension (unless one of the new upper bounds is one less than the corresponding lower bound, giving 0 elements in the array) as the algorithm to rearrange the element memory locations has not yet been implemented.
Turing:
% Basically var test :flexiblearray1..3, 1..3ofint
new test, 4, 3% Is allowed since only the first dimension in modified
new test, 0, 0% Are all allowed since the upper bound of at least one new test, 3, 0% of the dimensions is lower than its lower bounds new test, 5, 0% (this creates an array of size 0)
new test, 3, 6% Are not allowed since a dimension other than the first new test, 2, 9% (the second in this case) is modified without producing an array of size 0
Hope this clear things up.
Raknarg
Posted: Thu May 10, 2012 2:20 pm Post subject: RE:New Draw Module
So basically you can either make it smaller or change the first one.
Dreadnought
Posted: Thu May 10, 2012 2:47 pm Post subject: Re: New Draw Module
There is only one kind of "smaller" that is allowed and that is size 0.
Note that a size 0 flexible array can be changed into an array of any size you desire.
copthesaint
Posted: Thu May 10, 2012 3:14 pm Post subject: Re: New Draw Module
You cannot do flexible array of flexible arrays.
Its not happening. The buffer for drawpic is an array 1 .. * of int, It would be completly pointless to convert the values then set them to a 2D array so then it can be set as a 1D array for the buffer again. Plus I already have done the math so you Can draw as if it were a 2D array...
for the amount of work for this to work would be ridiculous since you can already, easily used a single dimension array. :
Turing:
var x :flexiblearray1.. 0, 1.. 0ofint
new x ,1,1
x(1,1):=0 put x(1,1)
new x ,1,2
x(1,2):=0 put x(1,2)
Amarylis
Posted: Thu May 10, 2012 3:54 pm Post subject: Re: New Draw Module
In the current implementation (1999), with a multi-dimensional array with a non-zero number of total elements, it is a run-time error to change any but the first dimension (unless one of the new upper bounds is one less than the corresponding lower bound, giving 0 elements in the array) as the algorithm to rearrange the element memory locations has not yet been implemented.
Turing:
% Basically var test :flexiblearray1..3, 1..3ofint
new test, 4, 3% Is allowed since only the first dimension in modified
new test, 0, 0% Are all allowed since the upper bound of at least one new test, 3, 0% of the dimensions is lower than its lower bounds new test, 5, 0% (this creates an array of size 0)
new test, 3, 6% Are not allowed since a dimension other than the first new test, 2, 9% (the second in this case) is modified without producing an array of size 0
Hope this clear things up.
I tried using this:
[syntax="turing]var ar : flexible array 1 .. 0, 1 .. 0 of int
new ar, 1, 1
new ar, 1, 5
put upper (ar, 2)[/syntax]
Didn't give me any errors
Dreadnought
Posted: Thu May 10, 2012 4:10 pm Post subject: Re: New Draw Module
I feel bad for steering this post off-topic, but your code (copy-pasted as is) produces the same error in both Turing 4.0.5, 4.1.1 and 4.1.2. What version might you be using?