twitchapon-anim

Basic Twitchapon Receiver/Visuals
git clone git://bsandro.tech/twitchapon-anim
Log | Files | Refs | README | LICENSE

debugprint.go (2241B)


      1 // Copyright 2014 Hajime Hoshi
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package ebitenutil
     16 
     17 import (
     18 	"image"
     19 
     20 	"github.com/hajimehoshi/ebiten/v2"
     21 	"github.com/hajimehoshi/ebiten/v2/ebitenutil/internal/assets"
     22 )
     23 
     24 var (
     25 	debugPrintTextImage     = ebiten.NewImageFromImage(assets.CreateTextImage())
     26 	debugPrintTextSubImages = map[rune]*ebiten.Image{}
     27 )
     28 
     29 // DebugPrint draws the string str on the image on left top corner.
     30 //
     31 // The available runes are in U+0000 to U+00FF, which is C0 Controls and Basic Latin and C1 Controls and Latin-1 Supplement.
     32 func DebugPrint(image *ebiten.Image, str string) {
     33 	DebugPrintAt(image, str, 0, 0)
     34 }
     35 
     36 // DebugPrintAt draws the string str on the image at (x, y) position.
     37 //
     38 // The available runes are in U+0000 to U+00FF, which is C0 Controls and Basic Latin and C1 Controls and Latin-1 Supplement.
     39 func DebugPrintAt(image *ebiten.Image, str string, x, y int) {
     40 	drawDebugText(image, str, x+1, y+1, true)
     41 	drawDebugText(image, str, x, y, false)
     42 }
     43 
     44 func drawDebugText(rt *ebiten.Image, str string, ox, oy int, shadow bool) {
     45 	op := &ebiten.DrawImageOptions{}
     46 	if shadow {
     47 		op.ColorM.Scale(0, 0, 0, 0.5)
     48 	}
     49 	x := 0
     50 	y := 0
     51 	w, _ := debugPrintTextImage.Size()
     52 	for _, c := range str {
     53 		const (
     54 			cw = assets.CharWidth
     55 			ch = assets.CharHeight
     56 		)
     57 		if c == '\n' {
     58 			x = 0
     59 			y += ch
     60 			continue
     61 		}
     62 		s, ok := debugPrintTextSubImages[c]
     63 		if !ok {
     64 			n := w / cw
     65 			sx := (int(c) % n) * cw
     66 			sy := (int(c) / n) * ch
     67 			s = debugPrintTextImage.SubImage(image.Rect(sx, sy, sx+cw, sy+ch)).(*ebiten.Image)
     68 			debugPrintTextSubImages[c] = s
     69 		}
     70 		op.GeoM.Reset()
     71 		op.GeoM.Translate(float64(x), float64(y))
     72 		op.GeoM.Translate(float64(ox+1), float64(oy))
     73 		rt.DrawImage(s, op)
     74 		x += cw
     75 	}
     76 }