Skip to content

Commit fa67d83

Browse files
committed
Add user sprite functions to C API.
1 parent 631573a commit fa67d83

File tree

4 files changed

+190
-3
lines changed

4 files changed

+190
-3
lines changed

src/api/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ target_sources(projectM_api
55
"${PROJECTM_EXPORT_HEADER}"
66
include/projectM-4/audio.h
77
include/projectM-4/callbacks.h
8+
include/projectM-4/core.h
89
include/projectM-4/debug.h
910
include/projectM-4/memory.h
1011
include/projectM-4/projectM.h
12+
include/projectM-4/render_opengl.h
13+
include/projectM-4/touch.h
1114
include/projectM-4/types.h
15+
include/projectM-4/user_sprites.h
1216
)
1317

1418
set_target_properties(projectM_api PROPERTIES

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

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@
3434
#include "projectM-4/render_opengl.h"
3535
#include "projectM-4/touch.h"
3636
#include "projectM-4/version.h"
37+
#include "projectM-4/user_sprites.h"
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* @file user_sprites.h
3+
* @copyright 2003-2024 projectM Team
4+
* @brief Types and enumerations used in the other API headers.
5+
* @since 4.2.0
6+
*
7+
* projectM -- Milkdrop-esque visualisation SDK
8+
* Copyright (C)2003-2024 projectM Team
9+
*
10+
* This library is free software; you can redistribute it and/or
11+
* modify it under the terms of the GNU Lesser General Public
12+
* License as published by the Free Software Foundation; either
13+
* version 2.1 of the License, or (at your option) any later version.
14+
*
15+
* This library is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
* Lesser General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Lesser General Public
21+
* License along with this library; if not, write to the Free Software
22+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23+
* See 'LICENSE.txt' included within this release
24+
*
25+
*/
26+
27+
#pragma once
28+
29+
#include "projectM-4/types.h"
30+
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
35+
/**
36+
* @brief Loads and displays a new sprite.
37+
*
38+
* Currently, these sprite types are supported:
39+
* <ul>
40+
* <li><tt>milkdrop</tt>: Original Milkdrop user sprite syntax. Pass the contents of an <tt>imgNN</tt> section in
41+
* the <tt>code</tt> argument.</li>
42+
* </ul>
43+
*
44+
* Unsupported types and loading errors will result in failure, and no sprite will be added or replaced.
45+
*
46+
* @important The same OpenGL context used to create the projectM instance @a must be active when calling this method!
47+
* @param instance The projectM instance handle.
48+
* @param type The case-insensitive type name of the sprite to be displayed. See description for supported values.
49+
* @param code The type-specific sprite code, e.g. the contents of an <tt>imgNN</tt> section from a Milkdrop user
50+
* sprite INI file.
51+
* @return A non-zero identifier if the sprite was successfully created, or zero if the type was
52+
* unrecognized or the code couldn't be parsed.
53+
* @since 4.2.0
54+
*/
55+
PROJECTM_EXPORT uint32_t projectm_sprite_create(projectm_handle instance,
56+
const char* type,
57+
const char* code);
58+
59+
/**
60+
* @brief Destroys a single sprite.
61+
*
62+
* If there is no active sprite with the given ID, this method is a no-op.
63+
*
64+
* @param instance The projectM instance handle.
65+
* @param sprite_id The ID of the sprite as returned by <tt>projectm_sprite_create()</tt>.
66+
* @since 4.2.0
67+
*/
68+
PROJECTM_EXPORT void projectm_sprite_destroy(projectm_handle instance, uint32_t sprite_id);
69+
70+
/**
71+
* @brief Destroys all active sprites.
72+
* @param instance The projectM instance handle.
73+
* @since 4.2.0
74+
*/
75+
PROJECTM_EXPORT void projectm_sprite_destroy_all(projectm_handle instance);
76+
77+
/**
78+
* @brief Returns the number of currently active sprites.
79+
*
80+
* @note Sprites may destroy themselves after a frame has been rendered, and projectM can also
81+
* remove a sprite if a new one is added and the sprite limit was already reached.
82+
* Keep this in mind when calling <tt>projectm_sprite_get_sprite_ids()</tt> - ideally,
83+
* call <tt>projectm_sprite_get_sprite_count()</tt> @a immediately before allocating the
84+
* ID list.
85+
* @param instance The projectM instance handle.
86+
* @return The current number of sprites being rendered.
87+
* @since 4.2.0
88+
*/
89+
PROJECTM_EXPORT uint32_t projectm_sprite_get_sprite_count(projectm_handle instance);
90+
91+
/**
92+
* @brief Returns the number of currently active sprites.
93+
*
94+
* Identifiers are ordered by sprite age, with the oldest sprite first and the newest last.
95+
*
96+
* @param instance The projectM instance handle.
97+
* @param sprite_ids A pointer to an already-allocated list which will receive the sprite IDs.
98+
* Call <tt>projectm_sprite_get_sprite_count()</tt> to get the required length
99+
* or allocate a list with the current sprite limit as its size and init all entries
100+
* to zero.
101+
* @since 4.2.0
102+
*/
103+
PROJECTM_EXPORT void projectm_sprite_get_sprite_ids(projectm_handle instance, uint32_t* sprite_ids);
104+
105+
/**
106+
* @brief Sets the limit of concurrently displayed sprites.
107+
*
108+
* Once the limit is exceeded, the oldest sprite will be destroyed in order to display a new one.
109+
*
110+
* @param instance The projectM instance handle.
111+
* @param max_sprites Maximum number of sprites to be displayed at once. Defaults to 16.
112+
* @since 4.2.0
113+
*/
114+
PROJECTM_EXPORT void projectm_sprite_set_max_sprites(projectm_handle instance,
115+
uint32_t max_sprites);
116+
117+
/**
118+
* @brief Returns the currently set limit of concurrently displayed sprites.
119+
*
120+
* @param instance The projectM instance handle.
121+
* @return The current maximum number of sprites to be displayed at once.
122+
* @since 4.2.0
123+
*/
124+
PROJECTM_EXPORT uint32_t projectm_sprite_get_max_sprites(projectm_handle instance);
125+
126+
#ifdef __cplusplus
127+
} // extern "C"
128+
#endif

src/libprojectM/ProjectMCWrapper.cpp

+57-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#include <Audio/AudioConstants.hpp>
66

77
#include <cstring>
8-
#include <sstream>
9-
#include <projectM-4/render_opengl.h>
108
#include <projectM-4/parameters.h>
9+
#include <projectM-4/render_opengl.h>
10+
#include <sstream>
1111

1212

1313
namespace libprojectM {
@@ -26,7 +26,7 @@ void projectMWrapper::PresetSwitchFailedEvent(const std::string& presetFilename,
2626
if (m_presetSwitchFailedEventCallback)
2727
{
2828
m_presetSwitchFailedEventCallback(presetFilename.c_str(),
29-
failureMessage.c_str(), m_presetSwitchFailedEventUserData);
29+
failureMessage.c_str(), m_presetSwitchFailedEventUserData);
3030
}
3131
}
3232

@@ -395,4 +395,58 @@ auto projectm_pcm_add_uint8(projectm_handle instance, const uint8_t* samples, un
395395
auto projectm_write_debug_image_on_next_frame(projectm_handle, const char*) -> void
396396
{
397397
// UNIMPLEMENTED
398+
}
399+
400+
uint32_t projectm_sprite_create(projectm_handle instance, const char* type, const char* code)
401+
{
402+
auto* projectMInstance = handle_to_instance(instance);
403+
404+
return projectMInstance->AddUserSprite(type, code);
405+
}
406+
407+
void projectm_sprite_destroy(projectm_handle instance, uint32_t sprite_id)
408+
{
409+
auto* projectMInstance = handle_to_instance(instance);
410+
411+
projectMInstance->DestroyUserSprite(sprite_id);
412+
}
413+
414+
void projectm_sprite_destroy_all(projectm_handle instance)
415+
{
416+
auto* projectMInstance = handle_to_instance(instance);
417+
418+
projectMInstance->DestroyAllUserSprites();
419+
}
420+
421+
uint32_t projectm_sprite_get_sprite_count(projectm_handle instance)
422+
{
423+
auto* projectMInstance = handle_to_instance(instance);
424+
425+
return projectMInstance->UserSpriteCount();
426+
}
427+
428+
void projectm_sprite_get_sprite_ids(projectm_handle instance, uint32_t* sprite_ids)
429+
{
430+
auto* projectMInstance = handle_to_instance(instance);
431+
432+
auto spriteIdList = projectMInstance->UserSpriteIdentifiers();
433+
for (const auto& spriteId : spriteIdList)
434+
{
435+
*sprite_ids = spriteId;
436+
sprite_ids++;
437+
}
438+
}
439+
440+
void projectm_sprite_set_max_sprites(projectm_handle instance, uint32_t max_sprites)
441+
{
442+
auto* projectMInstance = handle_to_instance(instance);
443+
444+
projectMInstance->SetUserSpriteLimit(max_sprites);
445+
}
446+
447+
uint32_t projectm_sprite_get_max_sprites(projectm_handle instance)
448+
{
449+
auto* projectMInstance = handle_to_instance(instance);
450+
451+
return projectMInstance->UserSpriteLimit();
398452
}

0 commit comments

Comments
 (0)