make photo loading a separate thing from construction

This commit is contained in:
Colin McMillen 2023-07-16 20:06:37 -04:00
parent e58f717ffe
commit 693269b8f5

View File

@ -140,21 +140,19 @@ void main() {
public class Photo {
public Texture Texture;
public bool Loaded = false;
private readonly string filename;
public Photo(string filename) {
Image<Rgba32> image = Image.Load<Rgba32>(filename);
Texture = new Texture(image);
public Photo(string filename, Texture unloadedTexture) {
this.filename = filename;
Texture = unloadedTexture;
}
}
public class Texture : IDisposable {
public int Handle;
public Vector2i Size;
public void Load() {
Image<Rgba32> image = Image.Load<Rgba32>(filename);
public Texture(Image<Rgba32> image) {
Size = new Vector2i(image.Width, image.Height);
Console.WriteLine("----------------------------------");
Console.WriteLine($"image loaded: {Size}");
Console.WriteLine($"image loaded: {image.Width}x{image.Height}");
ExifProfile? exifs = image.Metadata.ExifProfile;
if (exifs != null) {
@ -164,6 +162,17 @@ public class Texture : IDisposable {
}
}
}
Texture = new Texture(image);
Loaded = true;
}
}
public class Texture : IDisposable {
public int Handle;
public Vector2i Size;
public Texture(Image<Rgba32> image) {
Size = new Vector2i(image.Width, image.Height);
byte[] pixelBytes = new byte[Size.X * Size.Y * Unsafe.SizeOf<Rgba32>()];
image.CopyPixelDataTo(pixelBytes);
image.Dispose();
@ -345,9 +354,14 @@ public class Game : GameWindow {
// Load textures from JPEGs.
// string[] files = Directory.GetFiles(@"c:\users\colin\desktop\photos-test\");
string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\");
foreach (string file in files) {
for (int i = 0; i < files.Count(); i++) {
string file = files[i];
if (file.ToLower().EndsWith(".jpg")) {
photos.Add(new Photo(file));
Photo photo = new(file, TEXTURE_WHITE);
if (i == 0) {
photo.Load();
}
photos.Add(photo);
if (photos.Count > 10) {
break;
}
@ -388,6 +402,7 @@ public class Game : GameWindow {
SwapBuffers();
}
// FIXME: pull these out into a Util class and let photos be able to draw themselves?
void DrawTexture(Texture texture, Box2i box) {
DrawTexture(texture, box, Color4.White);
}