• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Go で書き直した Ikemen


Commit MetaInfo

Revision30b260132b06ec3d39d164d125e4c4e4fcf2e3d9 (tree)
Zeit2016-11-05 22:29:50
AutorSUEHIRO <supersuehiro@user...>
CommiterSUEHIRO

Log Message

Sprite 途中

Ändern Zusammenfassung

Diff

--- a/src/gen/gen.go
+++ b/src/gen/gen.go
@@ -4,7 +4,7 @@ import "os"
44
55 var AppendFunc = [...][2]string{
66 {"I", "int"},
7- {"Pal", "[]color.Color"},
7+ {"Pal", "[]uint32"},
88 }
99
1010 func main() {
@@ -19,7 +19,7 @@ func main() {
1919 panic(err)
2020 }
2121 }
22- write("package main\n\nimport \"image/color\"\n\n")
22+ write("package main\n\n")
2323 for i := range AppendFunc {
2424 write("func Append")
2525 write(AppendFunc[i][0])
--- a/src/generated.go
+++ b/src/generated.go
@@ -1,7 +1,5 @@
11 package main
22
3-import "image/color"
4-
53 func AppendI(slice *[]int, data ...int) {
64 m := len(*slice)
75 n := m + len(data)
@@ -13,11 +11,11 @@ func AppendI(slice *[]int, data ...int) {
1311 *slice = (*slice)[:n]
1412 copy((*slice)[m:n], data)
1513 }
16-func AppendPal(slice *[][]color.Color, data ...[]color.Color) {
14+func AppendPal(slice *[][]uint32, data ...[]uint32) {
1715 m := len(*slice)
1816 n := m + len(data)
1917 if n > cap(*slice) {
20- newSlice := make([][]color.Color, n+n/4)
18+ newSlice := make([][]uint32, n+n/4)
2119 copy(newSlice, *slice)
2220 *slice = newSlice
2321 }
--- a/src/image.go
+++ b/src/image.go
@@ -1,11 +1,19 @@
11 package main
22
3+// #cgo pkg-config: libpng
4+// #include <png.h>
5+import "C"
36 import (
47 "encoding/binary"
58 "github.com/go-gl/gl/v2.1/gl"
6- "image/color"
9+ "image"
10+ "image/draw"
11+ "image/png"
12+ "io"
713 "os"
14+ "runtime"
815 "strings"
16+ "unsafe"
917 )
1018
1119 func gltest() {
@@ -111,8 +119,42 @@ func gltest() {
111119 gl.DeleteObjectARB(vertObj)
112120 }
113121
122+type Texture uint32
123+
124+func textureFinalizer(t *Texture) {
125+ if *t != 0 {
126+ gl.DeleteTextures(1, (*uint32)(t))
127+ }
128+}
129+func NewTexture() (t *Texture) {
130+ t = new(Texture)
131+ runtime.SetFinalizer(t, textureFinalizer)
132+ return
133+}
134+func (t *Texture) FromImageRGBA(rgba *image.RGBA) {
135+ gl.BindTexture(gl.TEXTURE_2D, uint32(*t))
136+ gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
137+ gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA,
138+ int32(rgba.Bounds().Dx()), int32(rgba.Bounds().Dy()),
139+ 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&rgba.Pix[0]))
140+ gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
141+ gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
142+ gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP)
143+ gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP)
144+}
145+func (t *Texture) FromImage(im image.Image) {
146+ switch trueim := im.(type) {
147+ case *image.RGBA:
148+ t.FromImageRGBA(trueim)
149+ default:
150+ copy := image.NewRGBA(trueim.Bounds())
151+ draw.Draw(copy, trueim.Bounds(), trueim, image.Pt(0, 0), draw.Src)
152+ t.FromImageRGBA(copy)
153+ }
154+}
155+
114156 type PalleteList struct {
115- palletes [][]color.Color
157+ palletes [][]uint32
116158 palleteMap []int
117159 PalTable map[[2]int16]int
118160 }
@@ -122,7 +164,7 @@ func (pl *PalleteList) Clear() {
122164 pl.palleteMap = nil
123165 pl.PalTable = make(map[[2]int16]int)
124166 }
125-func (pl *PalleteList) SetSource(i int, p []color.Color) {
167+func (pl *PalleteList) SetSource(i int, p []uint32) {
126168 if i < len(pl.palleteMap) {
127169 pl.palleteMap[i] = i
128170 } else {
@@ -140,13 +182,13 @@ func (pl *PalleteList) SetSource(i int, p []color.Color) {
140182 AppendPal(&pl.palletes, p)
141183 }
142184 }
143-func (pl *PalleteList) NewPal() (i int, p []color.Color) {
185+func (pl *PalleteList) NewPal() (i int, p []uint32) {
144186 i = len(pl.palletes)
145- p = make([]color.Color, 256)
187+ p = make([]uint32, 256)
146188 pl.SetSource(i, p)
147189 return
148190 }
149-func (pl *PalleteList) Get(i int) []color.Color {
191+func (pl *PalleteList) Get(i int) []uint32 {
150192 return pl.palletes[pl.palleteMap[i]]
151193 }
152194 func (pl *PalleteList) Remap(source int, destination int) {
@@ -178,9 +220,9 @@ type SffHeader struct {
178220 NumberOfPalettes uint32
179221 }
180222
181-func (sh *SffHeader) Read(f *os.File, lofs *uint32, tofs *uint32) error {
223+func (sh *SffHeader) Read(r io.Reader, lofs *uint32, tofs *uint32) error {
182224 buf := make([]byte, 12)
183- n, err := f.Read(buf)
225+ n, err := r.Read(buf)
184226 if err != nil {
185227 return err
186228 }
@@ -188,7 +230,7 @@ func (sh *SffHeader) Read(f *os.File, lofs *uint32, tofs *uint32) error {
188230 return Error("ElecbyteSprではありません")
189231 }
190232 read := func(x interface{}) error {
191- return binary.Read(f, binary.LittleEndian, x)
233+ return binary.Read(r, binary.LittleEndian, x)
192234 }
193235 if err := read(&sh.Ver3); err != nil {
194236 return err
@@ -250,3 +292,424 @@ func (sh *SffHeader) Read(f *os.File, lofs *uint32, tofs *uint32) error {
250292 }
251293 return nil
252294 }
295+
296+type Sprite struct {
297+ Pal []uint32
298+ Tex *Texture
299+ Group, Number int16
300+ Size [2]uint16
301+ Offset [2]int16
302+ palidx, link int
303+ rle int
304+}
305+
306+func NewSprite() *Sprite {
307+ return &Sprite{palidx: -1, link: -1}
308+}
309+func (s *Sprite) shareCopy(src *Sprite) {
310+ s.Pal = src.Pal
311+ s.Tex = src.Tex
312+ s.Size = src.Size
313+ s.palidx = src.palidx
314+}
315+func (s *Sprite) GetPal(pl *PalleteList) []uint32 {
316+ if s.Pal != nil || s.rle == -12 {
317+ return s.Pal
318+ }
319+ return pl.Get(int(s.palidx))
320+}
321+func (s *Sprite) SetPxl(px []byte) {
322+ if int64(len(px)) != int64(s.Size[0])*int64(s.Size[1]) {
323+ return
324+ }
325+ s.Tex = NewTexture()
326+ gl.BindTexture(gl.TEXTURE_2D, uint32(*s.Tex))
327+ gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
328+ gl.TexImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE,
329+ int32(s.Size[0]), int32(s.Size[1]),
330+ 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, unsafe.Pointer(&px[0]))
331+ gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
332+ gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
333+ gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP)
334+ gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP)
335+}
336+func (s *Sprite) readHeader(r io.Reader, ofs *uint32, size *uint32,
337+ link *uint16) error {
338+ read := func(x interface{}) error {
339+ return binary.Read(r, binary.LittleEndian, x)
340+ }
341+ if err := read(ofs); err != nil {
342+ return err
343+ }
344+ if err := read(size); err != nil {
345+ return err
346+ }
347+ if err := read(s.Offset[:]); err != nil {
348+ return err
349+ }
350+ if err := read(&s.Group); err != nil {
351+ return err
352+ }
353+ if err := read(&s.Number); err != nil {
354+ return err
355+ }
356+ if err := read(link); err != nil {
357+ return err
358+ }
359+ return nil
360+}
361+func (s *Sprite) readPcxHeader(f *os.File, offset int64) error {
362+ f.Seek(offset, 0)
363+ read := func(x interface{}) error {
364+ return binary.Read(f, binary.LittleEndian, x)
365+ }
366+ var dummy uint16
367+ if err := read(&dummy); err != nil {
368+ return err
369+ }
370+ var encoding, bpp byte
371+ if err := read(&encoding); err != nil {
372+ return err
373+ }
374+ if err := read(&bpp); err != nil {
375+ return err
376+ }
377+ if bpp != 8 {
378+ return Error("256色でありません")
379+ }
380+ var rect [4]uint16
381+ if err := read(rect[:]); err != nil {
382+ return err
383+ }
384+ f.Seek(offset+66, 0)
385+ var bpl uint16
386+ if err := read(&bpl); err != nil {
387+ return err
388+ }
389+ s.Size[0] = rect[2] - rect[0] + 1
390+ s.Size[1] = rect[3] - rect[1] + 1
391+ if encoding == 1 {
392+ s.rle = int(bpl)
393+ } else {
394+ s.rle = 0
395+ }
396+ return nil
397+}
398+func (s *Sprite) RlePcxDecode(rle []byte) (p []byte) {
399+ if len(rle) == 0 || s.rle <= 0 {
400+ return rle
401+ }
402+ p = make([]byte, int(s.Size[0])*int(s.Size[1]))
403+ i, j, k, w := 0, 0, 0, int(s.Size[0])
404+ for j < len(p) {
405+ n, d := 1, rle[i]
406+ if i < len(rle)-1 {
407+ i++
408+ }
409+ if d >= 0xc0 {
410+ n = int(d & 0x3f)
411+ d = rle[i]
412+ if i < len(rle)-1 {
413+ i++
414+ }
415+ }
416+ for ; n > 0; n-- {
417+ if k < w && j < len(p) {
418+ p[j] = d
419+ j++
420+ }
421+ k++
422+ if k == s.rle {
423+ k = 0
424+ n = 1
425+ }
426+ }
427+ }
428+ s.rle = 0
429+ return
430+}
431+func (s *Sprite) read(f *os.File, sh *SffHeader, offset int64,
432+ datasize uint32, nextSubheader uint32, prev *Sprite,
433+ palletSame *bool, pl *PalleteList, c00 bool) error {
434+ if int64(nextSubheader) > offset {
435+ // 最後以外datasizeを無視
436+ datasize = nextSubheader - uint32(offset)
437+ }
438+ read := func(x interface{}) error {
439+ return binary.Read(f, binary.LittleEndian, x)
440+ }
441+ var ps byte
442+ if err := read(&ps); err != nil {
443+ return err
444+ }
445+ *palletSame = ps != 0 && prev != nil
446+ if err := s.readPcxHeader(f, offset); err != nil {
447+ return err
448+ }
449+ f.Seek(offset+128, 0)
450+ var palSize int
451+ if c00 || *palletSame {
452+ palSize = 0
453+ } else {
454+ palSize = 768
455+ }
456+ px := make([]byte, int(datasize)-(128+palSize))
457+ if err := read(px); err != nil {
458+ return err
459+ }
460+ if *palletSame {
461+ if prev != nil {
462+ s.palidx = prev.palidx
463+ }
464+ if s.palidx < 0 {
465+ s.palidx, _ = pl.NewPal()
466+ }
467+ } else {
468+ var pal []uint32
469+ s.palidx, pal = pl.NewPal()
470+ if c00 {
471+ f.Seek(offset+int64(datasize)-768, 0)
472+ }
473+ var rgb [3]byte
474+ for i := range pal {
475+ if err := read(rgb[:]); err != nil {
476+ return err
477+ }
478+ pal[i] = uint32(rgb[0])<<16 | uint32(rgb[1])<<16 | uint32(rgb[2])
479+ }
480+ }
481+ s.SetPxl(s.RlePcxDecode(px))
482+ return nil
483+}
484+func (s *Sprite) readHeaderV2(r io.Reader, ofs *uint32, size *uint32,
485+ lofs uint32, tofs uint32, link *uint16) error {
486+ read := func(x interface{}) error {
487+ return binary.Read(r, binary.LittleEndian, x)
488+ }
489+ if err := read(&s.Group); err != nil {
490+ return err
491+ }
492+ if err := read(&s.Number); err != nil {
493+ return err
494+ }
495+ if err := read(s.Size[:]); err != nil {
496+ return err
497+ }
498+ if err := read(s.Offset[:]); err != nil {
499+ return err
500+ }
501+ if err := read(link); err != nil {
502+ return err
503+ }
504+ var format byte
505+ if err := read(&format); err != nil {
506+ return err
507+ }
508+ s.rle = -int(format)
509+ var dummy byte
510+ if err := read(&dummy); err != nil {
511+ return err
512+ }
513+ if err := read(ofs); err != nil {
514+ return err
515+ }
516+ if err := read(size); err != nil {
517+ return err
518+ }
519+ var tmp uint16
520+ if err := read(&tmp); err != nil {
521+ return err
522+ }
523+ s.palidx = int(tmp)
524+ if err := read(&tmp); err != nil {
525+ return err
526+ }
527+ if tmp&1 == 0 {
528+ *ofs += lofs
529+ } else {
530+ *ofs += tofs
531+ }
532+ return nil
533+}
534+func (s *Sprite) Rle8Decode(rle []byte) (p []byte) {
535+ if len(rle) == 0 {
536+ return rle
537+ }
538+ p = make([]byte, int(s.Size[0])*int(s.Size[1]))
539+ i, j := 0, 0
540+ for j < len(p) {
541+ n, d := 1, rle[i]
542+ if i < len(rle)-1 {
543+ i++
544+ }
545+ if d&0xc0 == 0x40 {
546+ n = int(d & 0x3f)
547+ d = rle[i]
548+ if i < len(rle)-1 {
549+ i++
550+ }
551+ }
552+ for ; n > 0; n-- {
553+ if j < len(p) {
554+ p[j] = d
555+ j++
556+ }
557+ }
558+ }
559+ return
560+}
561+func (s *Sprite) Rle5Decode(rle []byte) (p []byte) {
562+ if len(rle) == 0 {
563+ return rle
564+ }
565+ p = make([]byte, int(s.Size[0])*int(s.Size[1]))
566+ i, j := 0, 0
567+ for j < len(p) {
568+ rl := int(rle[i])
569+ if i < len(rle)-1 {
570+ i++
571+ }
572+ dl := int(rle[i] & 0x7f)
573+ c := byte(0)
574+ if rle[i]>>7 != 0 {
575+ if i < len(rle)-1 {
576+ i++
577+ }
578+ c = rle[i]
579+ }
580+ if i < len(rle)-1 {
581+ i++
582+ }
583+ for {
584+ if j < len(p) {
585+ p[j] = c
586+ j++
587+ }
588+ rl--
589+ if rl < 0 {
590+ dl--
591+ if dl < 0 {
592+ break
593+ }
594+ c = rle[i] & 0x1f
595+ rl = int(rle[i] >> 5)
596+ if i < len(rle)-1 {
597+ i++
598+ }
599+ }
600+ }
601+ }
602+ return
603+}
604+func (s *Sprite) Lz5Decode(rle []byte) (p []byte) {
605+ if len(rle) == 0 {
606+ return rle
607+ }
608+ p = make([]byte, int(s.Size[0])*int(s.Size[1]))
609+ i, j, n := 0, 0, 0
610+ ct, cts, rb, rbc := rle[i], uint(0), byte(0), uint(0)
611+ if i < len(rle)-1 {
612+ i++
613+ }
614+ for j < len(p) {
615+ d := int(rle[i])
616+ if i < len(rle)-1 {
617+ i++
618+ }
619+ if ct&byte(1<<cts) != 0 {
620+ if d&0x3f == 0 {
621+ d = (d<<2 | int(rle[i])) + 1
622+ if i < len(rle)-1 {
623+ i++
624+ }
625+ n = int(rle[i]) + 2
626+ if i < len(rle)-1 {
627+ i++
628+ }
629+ } else {
630+ rb |= byte(d & 0xc0 >> rbc)
631+ rbc += 2
632+ n = int(d & 0x3f)
633+ if rbc < 8 {
634+ d = int(rle[i]) + 1
635+ if i < len(rle)-1 {
636+ i++
637+ }
638+ } else {
639+ d = int(rb) + 1
640+ rb, rbc = 0, 0
641+ }
642+ }
643+ for {
644+ if j < len(p) {
645+ p[j] = p[j-d]
646+ j++
647+ }
648+ n--
649+ if n < 0 {
650+ break
651+ }
652+ }
653+ } else {
654+ if d&0xe0 == 0 {
655+ n = int(rle[i]) + 8
656+ if i < len(rle)-1 {
657+ i++
658+ }
659+ } else {
660+ n = d >> 5
661+ d &= 0x1f
662+ }
663+ for ; n > 0; n-- {
664+ if j < len(p) {
665+ p[j] = byte(d)
666+ j++
667+ }
668+ }
669+ }
670+ cts++
671+ if cts >= 8 {
672+ ct, cts = rle[i], 0
673+ if i < len(rle)-1 {
674+ i++
675+ }
676+ }
677+ }
678+ return
679+}
680+func (s *Sprite) readV2(f *os.File, sh *SffHeader, offset int64,
681+ datasize uint32) error {
682+ f.Seek(offset+4, 0)
683+ if s.rle < 0 {
684+ format := -s.rle
685+ var px []byte
686+ if 2 <= format && format <= 4 {
687+ px = make([]byte, datasize)
688+ if err := binary.Read(f, binary.LittleEndian, px); err != nil {
689+ return err
690+ }
691+ }
692+ switch format {
693+ case 2:
694+ px = s.Rle8Decode(px)
695+ case 3:
696+ px = s.Rle5Decode(px)
697+ case 4:
698+ px = s.Lz5Decode(px)
699+ case 10:
700+ case 11, 12:
701+ s.rle = -12
702+ img, err := png.Decode(f)
703+ if err != nil {
704+ return err
705+ }
706+ s.Tex = NewTexture()
707+ s.Tex.FromImage(img)
708+ return nil
709+ default:
710+ return Error("不明な形式です")
711+ }
712+ s.SetPxl(px)
713+ }
714+ return nil
715+}
--- a/src/sound.go
+++ b/src/sound.go
@@ -300,7 +300,7 @@ func (n *NormalizerLR) process(bai float64, sam *float32) float64 {
300300
301301 type Vorbis struct {
302302 dec *vorbis.Vorbis
303- fh *os.File
303+ fd *os.File
304304 buf []int16
305305 bufi float64
306306 openReq chan string
@@ -316,18 +316,18 @@ func (v *Vorbis) Open(file string) {
316316 func (v *Vorbis) openFile(file string) bool {
317317 v.clear()
318318 var err error
319- if v.fh, err = os.Open(file); err != nil {
319+ if v.fd, err = os.Open(file); err != nil {
320320 return false
321321 }
322322 return v.restart()
323323 }
324324 func (v *Vorbis) restart() bool {
325- if v.fh == nil {
325+ if v.fd == nil {
326326 return false
327327 }
328- _, err := v.fh.Seek(0, 0)
328+ _, err := v.fd.Seek(0, 0)
329329 chk(err)
330- if v.dec, err = vorbis.Open(v.fh); err != nil {
330+ if v.dec, err = vorbis.Open(v.fd); err != nil {
331331 v.clear()
332332 return false
333333 }
@@ -338,9 +338,9 @@ func (v *Vorbis) clear() {
338338 if v.dec != nil {
339339 v.dec = nil
340340 }
341- if v.fh != nil {
342- chk(v.fh.Close())
343- v.fh = nil
341+ if v.fd != nil {
342+ chk(v.fd.Close())
343+ v.fd = nil
344344 }
345345 }
346346 func (v *Vorbis) samToAudioOut(buf [][]float32) (out []int16) {
@@ -495,7 +495,7 @@ func LoadSndFile(filename string) (*Snd, error) {
495495 if err != nil {
496496 return nil, err
497497 }
498- defer f.Close()
498+ defer func() { chk(f.Close()) }()
499499 buf := make([]byte, 12)
500500 var n int
501501 if n, err = f.Read(buf); err != nil {