Skip to content

Commit 74ca73d

Browse files
committed
Address Copilot feedback: improve docs and error handling
1 parent a3812ea commit 74ca73d

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/api/include/projectM-4/callbacks.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,16 @@ PROJECTM_EXPORT void projectm_set_preset_switch_failed_event_callback(projectm_h
100100
* If neither is provided (data is NULL and texture_id is 0), projectM will
101101
* attempt to load the texture from the filesystem.
102102
*
103+
* @warning When providing a texture_id, projectM takes ownership of the OpenGL texture
104+
* and will delete it (via glDeleteTextures) when it is no longer needed. Do not
105+
* delete the texture yourself or reuse the texture ID after passing it here.
106+
*
103107
* @since 4.2.0
104108
*/
105109
typedef struct projectm_texture_load_data {
106-
const unsigned char* data; /**< Pointer to raw pixel data (RGBA/RGB format, bottom-to-top). Can be NULL. */
107-
unsigned int width; /**< Width of the texture in pixels. */
108-
unsigned int height; /**< Height of the texture in pixels. */
110+
const unsigned char* data; /**< Pointer to raw pixel data in standard OpenGL format (first row is bottom of image). Can be NULL. */
111+
unsigned int width; /**< Width of the texture in pixels. Must be > 0 when providing data or texture_id. */
112+
unsigned int height; /**< Height of the texture in pixels. Must be > 0 when providing data or texture_id. */
109113
unsigned int channels; /**< Number of color channels (3 for RGB, 4 for RGBA). */
110114
unsigned int texture_id; /**< An existing OpenGL texture ID to use. Set to 0 if not used. */
111115
} projectm_texture_load_data;
@@ -129,6 +133,8 @@ typedef struct projectm_texture_load_data {
129133
* it needs to be retained for later use.
130134
* @note If providing raw pixel data, the data pointer must remain valid until
131135
* projectM has finished processing it (i.e., until the callback returns).
136+
* @note This callback is always invoked from the same thread that calls projectM
137+
* rendering functions. No additional synchronization is required.
132138
*
133139
* @param texture_name The name of the texture being requested, as used in the preset.
134140
* @param[out] data Pointer to a structure where the application should place texture data.

src/libprojectM/Renderer/TextureManager.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ auto TextureManager::TryLoadingTexture(const std::string& name) -> TextureSample
178178
m_textureLoadCallback(unqualifiedName, loadData);
179179

180180
// Check if callback provided an existing OpenGL texture ID
181-
if (loadData.textureId != 0)
181+
if (loadData.textureId != 0 && loadData.width > 0 && loadData.height > 0)
182182
{
183183
auto newTexture = std::make_shared<Texture>(unqualifiedName, loadData.textureId,
184184
GL_TEXTURE_2D, loadData.width, loadData.height, true);
@@ -188,6 +188,10 @@ auto TextureManager::TryLoadingTexture(const std::string& name) -> TextureSample
188188
LOG_DEBUG("[TextureManager] Loaded texture \"" + unqualifiedName + "\" from callback (texture ID)");
189189
return {newTexture, m_samplers.at({wrapMode, filterMode}), name, unqualifiedName};
190190
}
191+
else if (loadData.textureId != 0)
192+
{
193+
LOG_WARN("[TextureManager] Callback provided texture ID for \"" + unqualifiedName + "\" but width/height are invalid; falling back to filesystem");
194+
}
191195

192196
// Check if callback provided raw pixel data
193197
if (loadData.data != nullptr && loadData.width > 0 && loadData.height > 0)
@@ -211,6 +215,10 @@ auto TextureManager::TryLoadingTexture(const std::string& name) -> TextureSample
211215
LOG_DEBUG("[TextureManager] Loaded texture \"" + unqualifiedName + "\" from callback (pixel data)");
212216
return {newTexture, m_samplers.at({wrapMode, filterMode}), name, unqualifiedName};
213217
}
218+
else
219+
{
220+
LOG_WARN("[TextureManager] Failed to create OpenGL texture from callback pixel data for \"" + unqualifiedName + "\"; falling back to filesystem");
221+
}
214222
}
215223
}
216224

0 commit comments

Comments
 (0)