Re-add entities and assets
This commit is contained in:
parent
9b07ad02d8
commit
8592d887f2
22823
assets/songs/json/tetris.json
Normal file
22823
assets/songs/json/tetris.json
Normal file
File diff suppressed because it is too large
Load Diff
79
src/entities/song.h
Normal file
79
src/entities/song.h
Normal file
@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/filereadstream.h"
|
||||
#include <cstdio>
|
||||
#include <string> // Added: needed for std::string
|
||||
#include <vector> // Added: needed for std::vector
|
||||
#include <cassert> // Added: needed for assert
|
||||
|
||||
struct Note {
|
||||
std::string name;
|
||||
int midi;
|
||||
int duration_ticks;
|
||||
int ticks;
|
||||
};
|
||||
|
||||
struct Instrument {
|
||||
std::string family;
|
||||
int number;
|
||||
};
|
||||
|
||||
struct Track {
|
||||
std::string name;
|
||||
Instrument instrument;
|
||||
std::vector<Note> notes;
|
||||
};
|
||||
|
||||
struct Header {
|
||||
std::string name;
|
||||
};
|
||||
|
||||
struct Song {
|
||||
Header header;
|
||||
std::vector<Track> tracks;
|
||||
};
|
||||
|
||||
Song parseSong(const rapidjson::Document& doc) {
|
||||
assert(doc.IsObject());
|
||||
|
||||
Song song;
|
||||
|
||||
if (doc.HasMember("header") && doc["header"].IsObject()) {
|
||||
const auto& h = doc["header"].GetObject();
|
||||
Header header;
|
||||
header.name = h["name"].GetString();
|
||||
song.header = header;
|
||||
}
|
||||
|
||||
if (doc.HasMember("tracks") && doc["tracks"].IsArray()) {
|
||||
const auto& a = doc["tracks"].GetArray();
|
||||
for (auto& t : a) {
|
||||
Track track;
|
||||
track.name = t["name"].GetString();
|
||||
|
||||
if (t.HasMember("instrument") && t["instrument"].IsObject()) { // Fixed: added closing parenthesis
|
||||
const auto& i = t["instrument"].GetObject();
|
||||
Instrument instrument;
|
||||
instrument.family = i["family"].GetString();
|
||||
instrument.number = i["number"].GetInt();
|
||||
track.instrument = instrument;
|
||||
} // Fixed: removed extra closing parenthesis
|
||||
|
||||
if (t.HasMember("notes") && t["notes"].IsArray()) {
|
||||
const auto& notes = t["notes"].GetArray();
|
||||
for (auto& n : notes) {
|
||||
Note note;
|
||||
note.name = n["name"].GetString();
|
||||
note.midi = n["midi"].GetInt();
|
||||
note.duration_ticks = n["durationTicks"].GetInt();
|
||||
note.ticks = n["ticks"].GetInt();
|
||||
track.notes.push_back(note);
|
||||
}
|
||||
}
|
||||
|
||||
song.tracks.push_back(track);
|
||||
}
|
||||
}
|
||||
|
||||
return song;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user