-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
90 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
module github.com/kelindar/tile | ||
|
||
go 1.16 | ||
go 1.17 | ||
|
||
require github.com/stretchr/testify v1.7.0 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.0 // indirect | ||
github.com/kelindar/iostream v1.2.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,66 @@ | ||
// Copyright (c) Roman Atachiants and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for details. | ||
|
||
package tile | ||
|
||
import ( | ||
"bufio" | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
const tileDataSize = 54 | ||
|
||
// WriteTo writes the grid to a specific writer. | ||
func (m *Grid) WriteTo(dst io.Writer) (n int64, err error) { | ||
var c int | ||
p1 := At(0, 0) | ||
p2 := At(m.Size.X-1, m.Size.Y-1) | ||
|
||
// Write the viewport size | ||
header := make([]byte, 8) | ||
binary.BigEndian.PutUint16(header[0:2], uint16(p1.X)) | ||
binary.BigEndian.PutUint16(header[2:4], uint16(p1.Y)) | ||
binary.BigEndian.PutUint16(header[4:6], uint16(p2.X)) | ||
binary.BigEndian.PutUint16(header[6:8], uint16(p2.Y)) | ||
if c, err = dst.Write(header); err != nil { | ||
return | ||
} | ||
n += int64(c) | ||
|
||
// Write the grid data | ||
m.pagesWithin(p1, p2, func(page *page) { | ||
if c, err = dst.Write(page.Data()); err != nil { | ||
return | ||
} | ||
n += int64(c) | ||
}) | ||
return | ||
} | ||
|
||
// ReadFrom reads the grid from the reader. | ||
func ReadFrom(rdr io.Reader) (grid *Grid, err error) { | ||
reader := bufio.NewReader(rdr) | ||
header := make([]byte, 8) | ||
if _, err = io.ReadFull(reader, header); err != nil { | ||
return | ||
} | ||
|
||
// Read the size | ||
var view Rect | ||
view.Min.X = int16(binary.BigEndian.Uint16(header[0:2])) | ||
view.Min.Y = int16(binary.BigEndian.Uint16(header[2:4])) | ||
view.Max.X = int16(binary.BigEndian.Uint16(header[4:6])) | ||
view.Max.Y = int16(binary.BigEndian.Uint16(header[6:8])) | ||
|
||
// Allocate a new grid | ||
grid = NewGrid(view.Max.X+1, view.Max.Y+1) | ||
buf := make([]byte, tileDataSize) | ||
grid.pagesWithin(view.Min, view.Max, func(page *page) { | ||
if _, err = io.ReadFull(reader, buf); err != nil { | ||
return | ||
} | ||
|
||
copy(page.Data(), buf) | ||
}) | ||
return | ||
} | ||
// Copyright (c) Roman Atachiants and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for details. | ||
|
||
package tile | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
|
||
"github.com/kelindar/iostream" | ||
) | ||
|
||
const tileDataSize = 54 | ||
|
||
// WriteTo writes the grid to a specific writer. | ||
func (m *Grid) WriteTo(dst io.Writer) (n int64, err error) { | ||
p1 := At(0, 0) | ||
p2 := At(m.Size.X-1, m.Size.Y-1) | ||
|
||
// Write the viewport size | ||
w := iostream.NewWriter(dst) | ||
header := make([]byte, 8) | ||
binary.BigEndian.PutUint16(header[0:2], uint16(p1.X)) | ||
binary.BigEndian.PutUint16(header[2:4], uint16(p1.Y)) | ||
binary.BigEndian.PutUint16(header[4:6], uint16(p2.X)) | ||
binary.BigEndian.PutUint16(header[6:8], uint16(p2.Y)) | ||
if _, err := w.Write(header); err != nil { | ||
return w.Offset(), err | ||
} | ||
|
||
// Write the grid data | ||
m.pagesWithin(p1, p2, func(page *page) { | ||
if _, err := w.Write(page.Data()); err != nil { | ||
return | ||
} | ||
}) | ||
return w.Offset(), nil | ||
} | ||
|
||
// ReadFrom reads the grid from the reader. | ||
func ReadFrom(src io.Reader) (grid *Grid, err error) { | ||
r := iostream.NewReader(src) | ||
header := make([]byte, 8) | ||
if _, err := io.ReadFull(r, header); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Read the size | ||
var view Rect | ||
view.Min.X = int16(binary.BigEndian.Uint16(header[0:2])) | ||
view.Min.Y = int16(binary.BigEndian.Uint16(header[2:4])) | ||
view.Max.X = int16(binary.BigEndian.Uint16(header[4:6])) | ||
view.Max.Y = int16(binary.BigEndian.Uint16(header[6:8])) | ||
|
||
// Allocate a new grid | ||
grid = NewGrid(view.Max.X+1, view.Max.Y+1) | ||
buf := make([]byte, tileDataSize) | ||
grid.pagesWithin(view.Min, view.Max, func(page *page) { | ||
if _, err := io.ReadFull(r, buf); err != nil { | ||
return | ||
} | ||
|
||
copy(page.Data(), buf) | ||
}) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters