Skip to content

Commit

Permalink
[wip] tests/pong-mode0: Add mode 0 pong example
Browse files Browse the repository at this point in the history
  • Loading branch information
JPTIZ committed Jan 9, 2024
1 parent 58e18ff commit d9de4d2
Show file tree
Hide file tree
Showing 16 changed files with 708 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyReturnTypeOnItsOwnLine: 1000000
PointerAlignment: Left
PPIndentWidth: -1
QualifierAlignment: Leave
Expand Down
10 changes: 5 additions & 5 deletions libgba-cpp/arch/display/layers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <libgba-cpp/arch/display/layers.h>

#include <libgba-cpp/arch/registers.h>
#include <libgba-cpp/utils/general.h>

Expand All @@ -8,15 +7,16 @@ namespace {
using gba::display::BackgroundControl;
using gba::display::RawPalette;

using gba::arch::registers::display::bg_controls;

static auto const bg_address = reinterpret_cast<RawPalette<256>*>(0x0500'0000);
static auto& bg_palette = *new (bg_address) RawPalette<256>{};

}
} // namespace

BackgroundControl& gba::display::bg_control(gba::display::Layer layer) {
return *(reinterpret_cast<BackgroundControl*>(0x0400'0008) + utils::value_of(layer));
return *(
reinterpret_cast<BackgroundControl*>(0x0400'0008) +
utils::value_of(layer)
);
}

RawPalette<256>& gba::display::bg_palette() {
Expand Down
2 changes: 1 addition & 1 deletion libgba-cpp/arch/display/layers.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ enum class MapSize {
};


constexpr geometry::Size extract_size(MapSize size) {
constexpr auto get_size_values(MapSize size) -> geometry::Size {
switch (size) {
case MapSize::TEXT_256X256:
return {256, 256};
Expand Down
23 changes: 16 additions & 7 deletions libgba-cpp/arch/display/objects.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
#include <libgba-cpp/arch/display/objects.h>
#include <libgba-cpp/arch/display/tilemap.h>

using gba::display::Color;
using gba::display::RawPalette;

namespace {

static auto const obj_palette_address = reinterpret_cast<RawPalette<256>*>(0x0500'0200);
static auto& obj_palette =
*new (obj_palette_address) RawPalette<256>{};
static auto const obj_palette_address =
reinterpret_cast<RawPalette<256>*>(0x0500'0200);
static auto& obj_palette = *new (obj_palette_address) RawPalette<256>{};

}
static auto& oam = *new (reinterpret_cast<void*>(0x0700'0000))
std::array<gba::display::OAMEntry, 128>{};
static auto& sprite_tiles = *new (reinterpret_cast<void*>(0x0601'0000)) std::array<gba::display::map::Tile, 256>{};
// auto oam_map = array<pair<optional<Sprite&>, int>, 128>{};

namespace gba {
} // namespace

RawPalette<256>& display::obj_palette() {
auto gba::display::obj_palette() -> RawPalette<256>& {
return ::obj_palette;
}

auto gba::display::oam_entry(int index) -> OAMEntry& {
return ::oam[index];
}

auto gba::display::sprite_tile(int index) -> gba::display::map::Tile& {
return sprite_tiles[index];
};
108 changes: 105 additions & 3 deletions libgba-cpp/arch/display/objects.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef GBA_DRIVERS_DISPLAY_OBJ_H
#define GBA_DRIVERS_DISPLAY_OBJ_H

#include <libgba-cpp/arch/display/tilemap.h>
#include <libgba-cpp/arch/display/video.h>
#include <libgba-cpp/utils/bitset.h>

namespace gba::display {

Expand All @@ -20,12 +22,10 @@ enum class ObjectMapping {
MAP_TILE_MATRIX,
};


/**
* Object Color Palette array.
*/
RawPalette<256>& obj_palette();

auto obj_palette() -> RawPalette<256>&;

/**
* Changes object mapping mode.
Expand All @@ -34,6 +34,108 @@ inline void object_mapping(ObjectMapping map) {
gba::arch::registers::display::lcd_control[6] = utils::value_of(map);
}

enum class ObjectMode {
NORMAL,
SEMI_TRANSPARENT,
OBJECT_WINDOW,
};

enum class ObjectColorMode {
COLORS_16,
COLORS_256,
};

enum class ObjectShape {
SQUARE,
HORIZONTAL,
VERTICAL,
};

enum class ObjectSize {
TINY,
SMALL,
MEDIUM,
BIG,
};

enum class ObjectPriority {
HIGHEST,
HIGH,
LOW,
LOWEST,
};

struct OAMEntry {
gba::utils::bitset<uint16_t> attr0;
gba::utils::bitset<uint16_t> attr1;
gba::utils::bitset<uint16_t> attr2;
uint16_t _unused;

auto set_x(int y) -> void {
attr1 = (attr1.to_ulong() & ~0b11111111) | (y & 0b11111111);
}

auto set_y(int y) -> void {
attr0 = (attr0.to_ulong() & ~0b11111111) | (y & 0b11111111);
}

auto rotation_scaling(bool enabled) -> void {
attr0[8] = enabled;
}

auto visible(bool visible) -> void {
attr0[9] = not visible;
}

auto mode(ObjectMode mode) -> void {
attr0 = (attr0.to_ulong() & ~0b110000000000) |
(utils::value_of(mode) << 10);
}

auto mosaic(bool enabled) -> void {
attr0[12] = enabled;
}

auto color_mode(ObjectColorMode mode) -> void {
attr0[13] = utils::value_of(mode);
}

auto shape(ObjectShape shape) -> void {
attr0 = (attr0.to_ulong() & 0b0011111111111111) |
(utils::value_of(shape) << 14);
}

auto size(ObjectSize size) -> void {
attr1 = (attr1.to_ulong() & 0b0011111111111111) |
(utils::value_of(size) << 14);
}

auto flip_horizontally(bool flip) -> void {
attr1[12] = flip;
}

auto flip_vertically(bool flip) -> void {
attr1[13] = flip;
}

auto tile(int index) -> void {
attr2 = (attr2.to_ulong() & ~0b111111111) | (index & 0b111111111);
}

auto priority(ObjectPriority priority) -> void {
attr2 = (attr2.to_ulong() & ~0b11000000000) |
(utils::value_of(priority) << 10);
}

auto palette(int index) -> void {
attr2 = (attr2.to_ulong() & ~(0xff << 12)) | ((index & 0xff) << 12);
}
};

auto oam_entry(int index) -> OAMEntry&;

auto sprite_tile(int index) -> gba::display::map::Tile&;

}

#endif
2 changes: 1 addition & 1 deletion libgba-cpp/arch/display/tilemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Tile {
Tile() = default;

Tile(std::array<uint32_t, 8> rows):
rows_{move(rows)}
rows_{std::move(rows)}
{}

/**
Expand Down
Loading

0 comments on commit d9de4d2

Please sign in to comment.