mirror of
https://github.com/stevenhowes/GoRetro.git
synced 2026-05-26 15:53:31 +01:00
File caching and stats for tex/file caches.
This commit is contained in:
@@ -9,6 +9,7 @@ package GoRetro
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
@@ -119,7 +120,10 @@ func NewSequence(
|
||||
|
||||
var seq Sequence
|
||||
|
||||
jsonFile := GetFile(indexpath)
|
||||
jsonFile, err := GetFile(indexpath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading sequence %v: %v", indexpath, err)
|
||||
}
|
||||
|
||||
json.Unmarshal(jsonFile.Data, &seq.frames)
|
||||
|
||||
|
||||
@@ -24,6 +24,9 @@ var Delta float64
|
||||
var LastDebugStatePrint time.Time
|
||||
var DebugTick bool
|
||||
|
||||
var CacheHitsFile int
|
||||
var CacheHitsTex int
|
||||
|
||||
func Init() (*sdl.Renderer, *sdl.Window) {
|
||||
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
|
||||
fmt.Println("initializing SDL:", err)
|
||||
@@ -49,6 +52,7 @@ func Init() (*sdl.Renderer, *sdl.Window) {
|
||||
}
|
||||
|
||||
TexList = make(map[string]*sdl.Texture)
|
||||
FileList = make(map[string]*vFile)
|
||||
|
||||
LastDebugStatePrint = time.Now()
|
||||
|
||||
@@ -123,6 +127,8 @@ func Tick(renderer *sdl.Renderer) bool {
|
||||
fmt.Printf("TPS: %d\n", 1000000/(time.Since(frameStartTime).Microseconds()+1))
|
||||
fmt.Printf("Elements: %d\n", len(Elements))
|
||||
fmt.Printf("Viewport: %.0f %.0f %.0f %.0f\n", ViewPort.Position.X, ViewPort.Position.Y, ViewPort.Size.X, ViewPort.Size.Y)
|
||||
fmt.Printf("Cache hits: File %d Texture %d\n", CacheHitsFile, CacheHitsTex)
|
||||
|
||||
}
|
||||
|
||||
Delta = time.Since(frameStartTime).Seconds() * Config.TargetTicksPerSecond
|
||||
|
||||
+17
-4
@@ -8,18 +8,31 @@ package GoRetro
|
||||
* the use of archive files etc in future.
|
||||
*/
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type vFile struct {
|
||||
Data []byte
|
||||
Size int
|
||||
}
|
||||
|
||||
func GetFile(filename string) *vFile {
|
||||
Data, _ := os.ReadFile(Config.DataDir + filename)
|
||||
var FileList map[string]*vFile
|
||||
|
||||
func GetFile(filename string) (*vFile, error) {
|
||||
if val, ok := FileList[filename]; ok {
|
||||
CacheHitsFile++
|
||||
return val, nil
|
||||
}
|
||||
|
||||
Data, err := os.ReadFile(Config.DataDir + filename)
|
||||
vf := vFile{
|
||||
Size: len(Data),
|
||||
Data: Data,
|
||||
}
|
||||
return &vf
|
||||
|
||||
fmt.Printf("File Caching %s\n", filename)
|
||||
FileList[filename] = &vf
|
||||
return &vf, err
|
||||
}
|
||||
|
||||
+12
-4
@@ -44,15 +44,23 @@ func drawTexture(
|
||||
|
||||
func loadTextureFromBMP(filename string, renderer *sdl.Renderer) (*sdl.Texture, error) {
|
||||
if val, ok := TexList[filename]; ok {
|
||||
CacheHitsTex++
|
||||
return val, nil
|
||||
}
|
||||
|
||||
vFile := GetFile(filename)
|
||||
file, _ := sdl.RWFromMem(vFile.Data)
|
||||
vFile, err := GetFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading %v: %v", filename, err)
|
||||
}
|
||||
|
||||
file, err := sdl.RWFromMem(vFile.Data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("making RW from %v: %v", filename, err)
|
||||
}
|
||||
|
||||
img, err := sdl.LoadBMPRW(file, true)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading %v: %v", filename, err)
|
||||
return nil, fmt.Errorf("loading from RW for file %v: %v", filename, err)
|
||||
}
|
||||
defer img.Free()
|
||||
tex, err := renderer.CreateTextureFromSurface(img)
|
||||
@@ -61,6 +69,6 @@ func loadTextureFromBMP(filename string, renderer *sdl.Renderer) (*sdl.Texture,
|
||||
}
|
||||
|
||||
TexList[filename] = tex
|
||||
fmt.Printf("Caching %s\n", filename)
|
||||
fmt.Printf("Texture Caching %s\n", filename)
|
||||
return tex, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user