Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 New Draw Module
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
copthesaint




PostPosted: 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.


Turing:
View.Set ("Graphics:640;440,offscreenonly,nobuttonbar")

type ColorValues :
    record
        CLR : array 1 .. 3 of int
    end record

type PictureVals :
    record
        RGB : ColorValues
        RGBBuffer : ColorValues
    end record

class Layer
    import ColorValues, PictureVals

    var colorBack : ColorValues
    colorBack.CLR (1) := 255 % Blue
    colorBack.CLR (2) := 255 % Green
    colorBack.CLR (3) := 255 % Red

end Layer

module Render
    import ColorValues, PictureVals
    export Update, ColorBack, DrawDot, Initialize, DrawRect, ColorInvert, ColorInvertArea, Cls, ColorFilter, ColorFilterArea, DrawLine


    var imageX : int := 0
    var imageY : int := 0
    var pictureBound : int := 0
    var picture : flexible array 1 .. 0 of 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
        end if
        for i : 1 .. pictureBound
            picture (i).RGB := colorBack
        end for
        imageX := sizeX
        imageY := sizeY
        put picture (upper (picture)).RGB.CLR (1)
    end Initialize

    procedure ColorBack (R, B, G : int)
        if R < 256 and B < 256 and G < 256 and R > -1 and B > -1 and G > -1 then
            colorBack.CLR (1) := B
            colorBack.CLR (2) := G
            colorBack.CLR (3) := R
        end if
        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)
        end for
    end ColorInvert

    procedure ColorInvertArea (x1, y1, x2, y2 : int)
        for i : x1 .. x2
            for j : y1 .. y2
                if i < imageX and i > -1 and j < imageY and j > -1 then
                    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)
                end if
            end for
        end for
    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) div 2
            picture (i).RGB.CLR (2) := (picture (i).RGB.CLR (2) * (1.0 - p) + G * p) div 2
            picture (i).RGB.CLR (3) := (picture (i).RGB.CLR (3) * (1.0 - p) + R * p) div 2
        end for
    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 + 1 and i > -1 and j < imageY + 1 and j > -1 then
                    picture ((j - 1) * imageX + i).RGB.CLR (1) := (picture ((j - 1) * imageX + i).RGB.CLR (1) * (1.0 - p) + B * p) div 2
                    picture ((j - 1) * imageX + i).RGB.CLR (2) := (picture ((j - 1) * imageX + i).RGB.CLR (2) * (1.0 - p) + G * p) div 2
                    picture ((j - 1) * imageX + i).RGB.CLR (3) := (picture ((j - 1) * imageX + i).RGB.CLR (3) * (1.0 - p) + R * p) div 2
                end if
            end for
        end for
    end ColorFilterArea
   
    procedure DrawDot (x, y, R, G, B : int)
        if x < imageX + 1 and x > -1 and y < imageY + 1 and y > -1 then
            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
        end if
    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 rem 1)), round (G* (inc rem 1)), round (B* (inc rem 1)))
            DrawDot (x1 + round (inc + (1-(inc rem 1))), y1 + y, round (R * (inc rem 1)), round (G* (inc rem 1)), round (B* (inc rem 1)))
            inc := inc + distX
        end for
    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)
            end for
        end for
    end DrawRect

    procedure Update
        var b : array 1 .. sizepic (1, 1, imageX, imageY) of int
        cls
        takepic (1, 1, imageX, imageY, b)
        for i : 19 .. upper (b)
            b (i) := 0
        end for
        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 < 24 then
                        n += 8
                    else
                        n := 0
                        id += 1
                    end if
                end for
            end for
            if n > 0 then
                id += 1
                n := 0
            end if
        end for
        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
        end for
    end Cls
end Render


Render.Initialize (maxx div 2, maxy div 2)
Render.DrawDot (maxx div 2, maxy div 2, 255, 255, 255)
Render.DrawDot (1, 1, 255, 255, 255)
Render.DrawLine (1, 1, maxx div 2, 50, 255,255, 255)
Render.DrawRect (32, 30, 50, 50, 255, 55, 55)
Render.ColorInvertArea (20, 20, 40, 40)
Render.ColorFilterArea (50, 50, 256, 256, 55, 250, 105, 0.2)

Render.Update
Sponsor
Sponsor
Sponsor
sponsor
evildaddy911




PostPosted: 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




PostPosted: 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.



PictureClass.t
 Description:

Download
 Filename:  PictureClass.t
 Filesize:  7.27 KB
 Downloaded:  56 Time(s)

Amarylis




PostPosted: 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 Razz
copthesaint




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
Amarylis




PostPosted: Thu May 10, 2012 1:27 pm   Post subject: RE:New Draw Module

What version of Turing are you using? Razz
Dreadnought




PostPosted: Thu May 10, 2012 1:49 pm   Post subject: Re: New Draw Module

http://compsci.ca/holtsoft/doc/flexible.html wrote:
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 : flexible array 1..3, 1..3 of int

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




PostPosted: 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




PostPosted: 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




PostPosted: 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 : flexible array 1 .. 0, 1 .. 0 of int

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




PostPosted: Thu May 10, 2012 3:54 pm   Post subject: Re: New Draw Module

Dreadnought @ Thu May 10, 2012 1:49 pm wrote:
http://compsci.ca/holtsoft/doc/flexible.html wrote:
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 : flexible array 1..3, 1..3 of int

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




PostPosted: 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?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 21 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: