From 1bfc015ff0d9bbaaec065600dfc49ed1f13a75c3 Mon Sep 17 00:00:00 2001
From: Ethan <codedcells@gmail.com>
Date: Sat, 19 Oct 2024 04:27:55 +0100
Subject: [PATCH] added missing texture fallback

kinda useless since if there's no textures, likely no shaders either, but may be useful, 3 modes, 0 = white, 1 = checkerboard, 2 = colourful
---
 ScuffedMinecraft/src/Application.cpp | 51 ++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/ScuffedMinecraft/src/Application.cpp b/ScuffedMinecraft/src/Application.cpp
index 953a117..7906b5d 100644
--- a/ScuffedMinecraft/src/Application.cpp
+++ b/ScuffedMinecraft/src/Application.cpp
@@ -184,6 +184,57 @@ int main()
 	else
 	{
 		std::cout << "Failed to load texture\n";
+		// Use fallbacks
+		width = 256;
+		height = 256;
+		unsigned char* whiteTexture = new unsigned char[width * height * 4]; // RGBA
+		
+		int errorTexture = 1;  // 0 = white, 1 = checkerboard, 2 = colourful
+		
+		// Helper function to set RGBA values
+		auto setPixel = [](unsigned char* texture, int index, int r, int g, int b, int alpha) {
+			texture[index] = r;
+			texture[index + 1] = g;
+			texture[index + 2] = b;
+			texture[index + 3] = alpha;
+		};
+		
+		// Default to white
+		int r = 255, g = 255, b = 255;
+		
+		for (int y = 0; y < height; ++y)
+			{
+			int alpha = (y >= 64 && y < 96) ? 218 : 255; // Water transparency
+			
+			for (int x = 0; x < width; ++x)
+			{
+				if (errorTexture == 1)
+				{
+					// Checkerboard pattern
+					r = g = b = 0;
+					if ((x % 16 < 8) ^ (y % 16 < 8))
+						r = b = 255;
+				}
+				else if (errorTexture == 2)
+				{
+					// Colorful pattern, distinct color for each block
+					int spriteX = x / 16, spriteY = y / 16;
+					r = spriteX * 16;
+					g = spriteY * 16;
+					b = (spriteX + spriteY) * 8;
+				}
+				
+				int i = (y * width + x) * 4;
+				setPixel(whiteTexture, i, r, g, b, alpha);
+			}
+		}
+
+		// Upload the white texture
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, whiteTexture);
+		glGenerateMipmap(GL_TEXTURE_2D);
+
+		// Clean up
+		delete[] whiteTexture;
 	}
 
 	stbi_image_free(data);