Draw: Process DrawImage changes.

This commit is contained in:
2024-10-06 18:10:20 +02:00
parent 30439310f6
commit fd354add56

View File

@@ -57,6 +57,7 @@ struct TDrawImage {
int w, h; int w, h;
int flip; int flip;
GLuint tex; GLuint tex;
int changed;
}; };
// Globals // Globals
@@ -149,6 +150,7 @@ GLuint g_VertexObject;
void Draw_ShowInfo(); void Draw_ShowInfo();
void Draw_SetMatrix(float matrix[16]); void Draw_SetMatrix(float matrix[16]);
GLuint Draw_UploadGLTexture(int w, int h, const uint8_t *pixels); GLuint Draw_UploadGLTexture(int w, int h, const uint8_t *pixels);
void Draw_DeleteGLTexture(GLuint tex);
///////////////////////////// /////////////////////////////
// Draw_Init // Draw_Init
@@ -387,6 +389,12 @@ GLuint Draw_UploadGLTexture(int w, const int h, const uint8_t *pixels) {
return (tex); return (tex);
} }
/////////////////////////////
// Draw_DeleteGLTexture
//
//
void Draw_DeleteGLTexture(GLuint tex) { glDeleteTextures(1, &tex); }
///////////////////////////// /////////////////////////////
// Draw_Flush // Draw_Flush
// //
@@ -395,8 +403,14 @@ void Draw_Flush() {
if (g_CurrentImg == NULL || g_QuadArray->nVertex <= 0) { if (g_CurrentImg == NULL || g_QuadArray->nVertex <= 0) {
return; return;
} }
if (g_CurrentImg->tex != -1 && g_CurrentImg->changed) {
Draw_DeleteGLTexture(g_CurrentImg->tex);
g_CurrentImg->tex = -1;
g_CurrentImg->changed = 0;
}
if (g_CurrentImg->tex == -1) { if (g_CurrentImg->tex == -1) {
g_CurrentImg->tex = Draw_UploadGLTexture(g_CurrentImg->w, g_CurrentImg->h, g_CurrentImg->data); g_CurrentImg->tex = Draw_UploadGLTexture(g_CurrentImg->w, g_CurrentImg->h, g_CurrentImg->data);
g_CurrentImg->changed = 0;
} }
#if USE_OpenGL #if USE_OpenGL
@@ -654,6 +668,7 @@ DrawImg Draw_CreateImage(int w, int h) {
image->y = -(image->h / 2); image->y = -(image->h / 2);
image->flip = 0; image->flip = 0;
image->tex = -1; image->tex = -1;
image->changed = 1;
return ((DrawImg)image); return ((DrawImg)image);
} }
@@ -667,7 +682,8 @@ DrawImg Draw_LoadImage(char *filename) {
// Try loading PNG images // Try loading PNG images
if (EndsWith(filename, ".png") || EndsWith(filename, ".PNG")) { if (EndsWith(filename, ".png") || EndsWith(filename, ".PNG")) {
DrawImage image = malloc(sizeof(TDrawImage)); DrawImage image = malloc(sizeof(TDrawImage));
unsigned error = lodepng_decode32_file(&image->data, (unsigned *)&image->w, (unsigned *)&image->h, filename); const unsigned error =
lodepng_decode32_file(&image->data, (unsigned *)&image->w, (unsigned *)&image->h, filename);
if (error) { if (error) {
Print("Draw_LoadImage: PNG decoder error %u: %s on file %s\n", error, lodepng_error_text(error), filename); Print("Draw_LoadImage: PNG decoder error %u: %s on file %s\n", error, lodepng_error_text(error), filename);
return (NULL); return (NULL);
@@ -676,6 +692,7 @@ DrawImg Draw_LoadImage(char *filename) {
image->y = -(image->h / 2); image->y = -(image->h / 2);
image->flip = 0; image->flip = 0;
image->tex = -1; image->tex = -1;
image->changed = 1;
return (DrawImg)image; return (DrawImg)image;
} }
@@ -747,6 +764,7 @@ DrawImg Draw_SetPixel(DrawImg img, const int x, const int y, const ColorRgba col
image->data[offset + 1] = color[1]; image->data[offset + 1] = color[1];
image->data[offset + 2] = color[2]; image->data[offset + 2] = color[2];
image->data[offset + 3] = color[3]; image->data[offset + 3] = color[3];
image->changed = 1;
return img; return img;
} }
DrawImg Draw_AddPixel(DrawImg img, const int x, const int y, const ColorRgba color, const float factor) { DrawImg Draw_AddPixel(DrawImg img, const int x, const int y, const ColorRgba color, const float factor) {
@@ -776,6 +794,7 @@ DrawImg Draw_AddPixel(DrawImg img, const int x, const int y, const ColorRgba col
image->data[offset + 1] = SumClamp_uint8(image->data[offset + 1] * (1 - alpha), g); image->data[offset + 1] = SumClamp_uint8(image->data[offset + 1] * (1 - alpha), g);
image->data[offset + 2] = SumClamp_uint8(image->data[offset + 2] * (1 - alpha), b); image->data[offset + 2] = SumClamp_uint8(image->data[offset + 2] * (1 - alpha), b);
image->data[offset + 3] = SumClamp_uint8(image->data[offset + 3], a); image->data[offset + 3] = SumClamp_uint8(image->data[offset + 3], a);
image->changed = 1;
return img; return img;
} }
@@ -1074,6 +1093,7 @@ DrawImage Draw_DefaultFontImage(const ColorRgba color) {
} }
} }
} }
img->changed = 1;
return (img); return (img);
} }