diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f170070..db1693f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,11 @@ - - - - - - - - Client.connection.Write([]byte( - - - - - @@ -70,7 +51,7 @@ DEFINITION_ORDER - @@ -90,6 +71,7 @@ + @@ -118,13 +100,8 @@ - - - - - - + + - + - + @@ -253,26 +231,18 @@ - - + + - + - - - - - - - - - - + + @@ -281,182 +251,14 @@ - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -469,10 +271,185 @@ - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/markov/markov.go b/markov/markov.go index e8f4b5e..4e82a28 100644 --- a/markov/markov.go +++ b/markov/markov.go @@ -1,8 +1,8 @@ package main import ( - "math/rand" "git.jcg.re/jcgruenhage/pixclient" + "math/rand" "net" ) @@ -29,7 +29,7 @@ func main() { } else { panic("Illegal Direction!") } - if counter%500 == 0 { + if counter%50 == 0 { iterateColor(&r, &g, &b) } client.WritePixel(currentX, currentY, r, g, b, 255) diff --git a/pixclient.go b/pixclient.go index 84bedd0..833ee60 100644 --- a/pixclient.go +++ b/pixclient.go @@ -1,24 +1,33 @@ package pixclient import ( + "bufio" + "errors" "net" "strconv" + "strings" ) type Client struct { connection net.Conn + width int + height int } func Init(addr net.IP, port int) *Client { var client Client connection, err := net.Dial("tcp", addr.String()+":"+strconv.Itoa(port)) - client.connection = connection check(err) + client.connection = connection + client.Size() return &client } -func (Client *Client) WritePixel(x, y int, r, g, b, a byte) { +func (Client *Client) WritePixel(x, y int, r, g, b, a byte) error { var bytes []byte + if x <= 0 || y <= 0 || x >= Client.width || y >= Client.height { + return errors.New("Pixel out of range.") + } bytes = append(appendTwoDigitHex(appendTwoDigitHex(appendTwoDigitHex(appendTwoDigitHex(append(append(append(append(append( bytes, []byte("PX ")...), []byte(strconv.FormatInt(int64(x), 10))...), @@ -35,7 +44,21 @@ func (Client *Client) WritePixel(x, y int, r, g, b, a byte) { // //Client.connection.Write([]byte( // fmt.Sprintf("PX %d %d %X\n", x, y, []byte{r, g, b, a}))) + return nil } + +func (Client *Client) Size() (int, int) { + Client.connection.Write([]byte("SIZE\n")) + reader := bufio.NewReader(Client.connection) + size, err := reader.ReadBytes('\n') + check(err) + sizeString := string(size) + sizeArray := strings.Split(sizeString, " ") + Client.width, _ = strconv.Atoi(sizeArray[1]) + Client.height, _ = strconv.Atoi(sizeArray[2]) + return Client.width, Client.height +} + func appendTwoDigitHex(bytes []byte, val byte) []byte { if val < 0x10 { bytes = append(bytes, []byte("0")...)