1+ #include < memory>
2+ #include < string>
3+ #include < qjspp.hpp>
4+ #include < raylib.h>
5+ #include " js_types.hpp"
6+
7+ namespace App ::Module::VectorJS {
8+
9+ static void register_sound_class (qjspp::Engine& engine, qjspp::ModuleBuilder& builder) {
10+ auto sound = engine.make_class <JSSound>(" Sound" );
11+
12+ // Constructor: const mySound = new vectorjs.Sound("assets/sfx.wav");
13+ sound.constructor ([](const qjspp::ArgList& args) -> std::unique_ptr<JSSound> {
14+ if (args.empty ()) return nullptr ;
15+ return std::make_unique<JSSound>(args[0 ].to_string ());
16+ });
17+
18+ // Instance Method: mySound.play()
19+ sound.instance_method (" play" , [](JSSound* self, const qjspp::ArgList& args) -> qjspp::Value {
20+ if (self && self->sound_ptr ) {
21+ PlaySound (*(self->sound_ptr ));
22+ }
23+ return {};
24+ });
25+
26+ // Instance Method: mySound.pause()
27+ sound.instance_method (" pause" , [](JSSound* self, const qjspp::ArgList& args) -> qjspp::Value {
28+ if (self && self->sound_ptr ) {
29+ PauseSound (*(self->sound_ptr ));
30+ }
31+ return {};
32+ });
33+
34+ // Instance Method: mySound.resume()
35+ sound.instance_method (" resume" , [](JSSound* self, const qjspp::ArgList& args) -> qjspp::Value {
36+ if (self && self->sound_ptr ) {
37+ ResumeSound (*(self->sound_ptr ));
38+ }
39+ return {};
40+ });
41+
42+ // Instance Method: mySound.stop()
43+ sound.instance_method (" stop" , [](JSSound* self, const qjspp::ArgList& args) -> qjspp::Value {
44+ if (self && self->sound_ptr ) {
45+ StopSound (*(self->sound_ptr ));
46+ }
47+ return {};
48+ });
49+
50+ // Instance Method: mySound.setVolume(0.5)
51+ sound.instance_method (" setVolume" , [](JSSound* self, const qjspp::ArgList& args) -> qjspp::Value {
52+ if (self && self->sound_ptr && !args.empty ()) {
53+ SetSoundVolume (*(self->sound_ptr ), static_cast <float >(args[0 ].to_double ()));
54+ }
55+ return {};
56+ });
57+
58+ builder.export_class (" Sound" , sound.build ());
59+ }
60+
61+ static void register_music_class (qjspp::Engine& engine, qjspp::ModuleBuilder& builder) {
62+ auto music = engine.make_class <JSMusic>(" Sound" );
63+
64+ // Constructor: const mySound = new vectorjs.Sound("assets/sfx.wav");
65+ music.constructor ([](const qjspp::ArgList& args) -> std::unique_ptr<JSMusic> {
66+ if (args.empty ()) return nullptr ;
67+ return std::make_unique<JSMusic>(args[0 ].to_string ());
68+ });
69+
70+ // Instance Method: mySound.play()
71+ music.instance_method (" play" , [](JSMusic* self, const qjspp::ArgList& args) -> qjspp::Value {
72+ if (self && self->music_ptr ) {
73+ PlayMusicStream (*(self->music_ptr ));
74+ }
75+ return {};
76+ });
77+
78+ // Instance Method: mySound.pause()
79+ music.instance_method (" pause" , [](JSMusic* self, const qjspp::ArgList& args) -> qjspp::Value {
80+ if (self && self->music_ptr ) {
81+ PauseMusicStream (*(self->music_ptr ));
82+ }
83+ return {};
84+ });
85+
86+ // Instance Method: mySound.resume()
87+ music.instance_method (" resume" , [](JSMusic* self, const qjspp::ArgList& args) -> qjspp::Value {
88+ if (self && self->music_ptr ) {
89+ ResumeMusicStream (*(self->music_ptr ));
90+ }
91+ return {};
92+ });
93+
94+ // Instance Method: mySound.stop()
95+ music.instance_method (" stop" , [](JSMusic* self, const qjspp::ArgList& args) -> qjspp::Value {
96+ if (self && self->music_ptr ) {
97+ StopMusicStream (*(self->music_ptr ));
98+ }
99+ return {};
100+ });
101+
102+ // Instance Method: myMusic.update()
103+ music.instance_method (" update" , [](JSMusic* self, const qjspp::ArgList& args) -> qjspp::Value {
104+ if (self && self->music_ptr ) {
105+ UpdateMusicStream (*(self->music_ptr ));
106+ }
107+ return {};
108+ });
109+
110+ // Instance Method: mySound.setVolume(0.5)
111+ music.instance_method (" setVolume" , [](JSMusic* self, const qjspp::ArgList& args) -> qjspp::Value {
112+ if (self && self->music_ptr && !args.empty ()) {
113+ SetMusicVolume (*(self->music_ptr ), static_cast <float >(args[0 ].to_double ()));
114+ }
115+ return {};
116+ });
117+
118+ builder.export_class (" Music" , music.build ());
119+ }
120+
121+ void register_audio_classes (qjspp::Engine& engine, qjspp::ModuleBuilder& builder) {
122+ register_sound_class (engine, builder);
123+ register_music_class (engine, builder);
124+ }
125+
126+ } // namespace App::Module::VectorJS
0 commit comments