Skip to content

Commit

Permalink
Add a few cd-reader methods.
Browse files Browse the repository at this point in the history
Also fixed the busted declarations that are described in #8.
  • Loading branch information
Clownacy committed Mar 5, 2024
1 parent 7e2f395 commit 74d86d2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 13 additions & 1 deletion cd-reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ bool CDReader::DetermineSectorSize(SDL::RWops &stream)
return buffer == sector_header;
}

void CDReader::SeekToSector(const cc_u32f sector_index)
void CDReader::SeekToSector(const SectorIndex sector_index)
{
current_sector_index = sector_index;
SDL_RWseek(stream.get(), sector_size_2352 ? sector_index * EXTENDED_SECTOR_SIZE + 0x10 : sector_index * SECTOR_SIZE, RW_SEEK_SET);
}

Expand All @@ -35,5 +36,16 @@ CDReader::Sector CDReader::ReadSector()
if (sector_size_2352)
SDL_RWseek(stream.get(), EXTENDED_SECTOR_SIZE - SECTOR_SIZE, SEEK_CUR);

++current_sector_index;

return sector_buffer;
}

CDReader::Sector CDReader::ReadSector(const SectorIndex sector_index)
{
const auto sector_backup = GetCurrentSector();
SeekToSector(sector_index);
const Sector sector = ReadSector();
SeekToSector(sector_backup);
return sector;
}
16 changes: 12 additions & 4 deletions cd-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@

class CDReader
{
public:
using SectorIndex = cc_u32f;

private:
SDL::RWops stream;
bool sector_size_2352;
SectorIndex current_sector_index;
static constexpr cc_u16f EXTENDED_SECTOR_SIZE = 2352;

static bool DetermineSectorSize(SDL::RWops &stream);

public:
using SectorIndex = cc_u32f;

static constexpr cc_u16f SECTOR_SIZE = 2048;
using Sector = std::array<cc_u8l, SECTOR_SIZE>;

CDReader() = default;
CDReader(SDL::RWops &&stream);
void CDReader::Close() { stream = nullptr; }
bool CDReader::IsOpen() const { return stream.get() != nullptr; }
void CDReader::SeekToSector(cc_u32f sector_index);
Sector CDReader::ReadSector();
void Close() { stream = nullptr; }
bool IsOpen() const { return stream.get() != nullptr; }
void SeekToSector(SectorIndex sector_index);
Sector ReadSector();
Sector ReadSector(SectorIndex sector_index);
SectorIndex GetCurrentSector() { return current_sector_index; }
};

#endif // CD_READER_H

0 comments on commit 74d86d2

Please sign in to comment.