cache images by string (filename) rather than index; disable unloading for now

This commit is contained in:
Colin McMillen 2023-07-26 18:16:51 -04:00
parent 3c1e2c8dad
commit dc909a80f4

View File

@ -565,7 +565,7 @@ public class Game : GameWindow {
int VertexArrayObject; int VertexArrayObject;
List<Photo> allPhotos = new(); List<Photo> allPhotos = new();
List<Photo> photos = new(); List<Photo> photos = new();
HashSet<int> loadedImages = new(); HashSet<string> loadedImages = new();
int photoIndex = 0; int photoIndex = 0;
int ribbonIndex = 0; int ribbonIndex = 0;
Shader shader = new(); Shader shader = new();
@ -710,6 +710,8 @@ public class Game : GameWindow {
void FilterByRating(int rating) { void FilterByRating(int rating) {
Console.WriteLine("filter to " + rating); Console.WriteLine("filter to " + rating);
photos = allPhotos.Where(p => p.Rating >= rating).ToList();
photoIndex = 0;
} }
protected override void OnLoad() { protected override void OnLoad() {
@ -750,11 +752,11 @@ public class Game : GameWindow {
// Load photos from a directory. // Load photos from a directory.
// string[] files = Directory.GetFiles(@"c:\users\colin\desktop\photos-test\"); // string[] files = Directory.GetFiles(@"c:\users\colin\desktop\photos-test\");
// string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\"); string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\");
// string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\"); // string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\");
// string[] files = Directory.GetFiles(@"C:\Users\colin\Pictures\photos\2018\06\23"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Pictures\photos\2018\06\23");
// string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000");
string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\");
for (int i = 0; i < files.Count(); i++) { for (int i = 0; i < files.Count(); i++) {
string file = files[i]; string file = files[i];
@ -789,8 +791,9 @@ public class Game : GameWindow {
int maxLoadedImage = Math.Min(photoIndex + 20, photos.Count - 1); int maxLoadedImage = Math.Min(photoIndex + 20, photos.Count - 1);
// First, unload images that are far outside our window. // First, unload images that are far outside our window.
// FIXME: also cancel any in-progress loading tasks that have moved outside our window. // FIXME: also cancel any in-progress loading tasks that have moved outside our window.
// FIXME: maybe use an LRU cache for evicting images?
// FIXME: keep around thumbnail-sized textures? // FIXME: keep around thumbnail-sized textures?
// FIXME: turn unloading back on, using an LRU cache for evicting images.
/*
foreach (int i in loadedImages) { foreach (int i in loadedImages) {
if (i < minUnloadedImage || i > maxUnloadedImage) { if (i < minUnloadedImage || i > maxUnloadedImage) {
// Console.WriteLine("unloading " + i); // Console.WriteLine("unloading " + i);
@ -798,12 +801,12 @@ public class Game : GameWindow {
photos[i].UnloadAsync(); photos[i].UnloadAsync();
} }
} }
*/
// Then, start loading any images that aren't in our window. // Then, start loading any images that aren't in our window.
for (int i = minLoadedImage; i <= maxLoadedImage; i++) { for (int i = minLoadedImage; i <= maxLoadedImage; i++) {
if (!loadedImages.Contains(i)) { if (!loadedImages.Contains(photos[i].Filename)) {
// Console.WriteLine("loading " + i); // Console.WriteLine("loading " + i);
loadedImages.Add(i); loadedImages.Add(photos[i].Filename);
await Task.Run( () => { photos[i].LoadAsync(); }); await Task.Run( () => { photos[i].LoadAsync(); });
} }
} }
@ -816,7 +819,7 @@ public class Game : GameWindow {
LoadAndUnloadImagesAsync(); LoadAndUnloadImagesAsync();
GL.Clear(ClearBufferMask.ColorBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit);
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); HashSet<int> loadedImages = new(); GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
GL.ActiveTexture(TextureUnit.Texture0); GL.ActiveTexture(TextureUnit.Texture0);
Photo activePhoto = photos[photoIndex]; Photo activePhoto = photos[photoIndex];