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 7, 2024
1 parent 58e18ff commit 1c182c4
Show file tree
Hide file tree
Showing 12 changed files with 639 additions and 77 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
97 changes: 70 additions & 27 deletions libgba-cpp/engine/graphics/tilemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include <libgba-cpp/arch/display/layers.h>
#include <libgba-cpp/arch/display/tilemap.h>
#include "palette.h"

#include "palette.h"

namespace gba::graphics {

Expand All @@ -20,20 +20,17 @@ using Tile = gba::display::map::Tile;
class Tileset {
public:
template <std::size_t N>
Tileset(const Palette& palette,
const std::array<Tile, N>& tiles):
Tileset(const Palette& palette, const std::array<Tile, N>& tiles):
palette_{palette},
count_{N},
tiles_{reinterpret_cast<const Tile*>(tiles.data())}
{}
tiles_{reinterpret_cast<const Tile*>(tiles.data())} {
}

Tileset(const Palette& palette,
const Tile tiles[],
unsigned count):
Tileset(const Palette& palette, const Tile tiles[], unsigned count):
palette_{palette},
count_{count},
tiles_{tiles}
{}
tiles_{tiles} {
}

auto length() const {
return count_;
Expand Down Expand Up @@ -65,13 +62,13 @@ class Tilemap {
template <std::size_t N>
Tilemap(const std::array<uint16_t, N>& tiles):
count_{N},
tiles_{reinterpret_cast<const uint16_t*>(tiles.data())}
{}
tiles_{reinterpret_cast<const uint16_t*>(tiles.data())} {
}

Tilemap(const uint16_t tiles[], unsigned count):
count_{count},
tiles_{tiles}
{}
tiles_{tiles} {
}

auto length() const {
return count_;
Expand All @@ -90,7 +87,6 @@ class Tilemap {
const uint16_t* tiles_;
};


class Map {
public:
Map(const Tileset& tileset,
Expand All @@ -104,20 +100,25 @@ class Map {
layer1_{layer1},
layer2_{layer2},
layer3_{layer3},
size_{size}
{}
size_{size} {
}

const auto& tileset() const {
return tileset_;
}

const auto& layer(const gba::display::Layer layer) const {
switch (layer) {
case display::Layer::BG0: return layer0_;
case display::Layer::BG1: return layer1_;
case display::Layer::BG2: return layer2_;
case display::Layer::BG3: return layer3_;
default: return layer0_;
case display::Layer::BG0:
return layer0_;
case display::Layer::BG1:
return layer1_;
case display::Layer::BG2:
return layer2_;
case display::Layer::BG3:
return layer3_;
default:
return layer0_;
}
}

Expand Down Expand Up @@ -149,7 +150,11 @@ class Map {
/**
* Loads map into map memory.
*/
inline void load_tilemap(const Tilemap& tilemap, int screenblock, int charblock) {
inline void load_tilemap(
const Tilemap& tilemap,
int screenblock,
int charblock
) {
const auto base = 0x400 * screenblock + 0x2000 * charblock;
for (auto i = 0u; i < tilemap.length(); ++i) {
display::map::tilemap()[i + base] = tilemap[i];
Expand All @@ -169,18 +174,21 @@ inline void load_tileset(const Tileset& tileset) {
* Setup and load map into memory.
*/
inline void load_map(const Map& map) {
using gba::display::Layer;
using gba::display::BGPriority;
using gba::display::Layer;
using gba::display::PaletteMode;

/* load the palette from the image into palette memory*/
load_palette(map.tileset().palette());
auto& palette = map.tileset().palette();
//volatile auto x = *reinterpret_cast<const long*>(palette.colors());
load_palette(palette);

/* load the image into char block 0 (16 bits at a time) */
load_tileset(map.tileset());

/* set all control the bits in this register */
constexpr auto layers = std::array{Layer::BG0, Layer::BG1, Layer::BG2, Layer::BG3};
constexpr auto layers =
std::array{Layer::BG0, Layer::BG1, Layer::BG2, Layer::BG3};
constexpr auto priorities = std::array{
BGPriority::LOWEST,
BGPriority::LOW,
Expand All @@ -205,6 +213,41 @@ inline void load_map(const Map& map) {
}
}

}
constexpr auto null_tilemap = std::array<uint16_t, 32*32>{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

} // namespace gba::graphics

#endif
15 changes: 8 additions & 7 deletions tests/meson.build
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
generic_test_dirs = [
'alphablend',
'interrupts',
'pong',
'mode5',
'mosaic',
'sound',
'windowing',
# 'alphablend',
# 'interrupts',
# 'pong',
'pong-mode0',
# 'mode5',
# 'mosaic',
# 'sound',
# 'windowing',
]

foreach test_dir : generic_test_dirs
Expand Down
2 changes: 2 additions & 0 deletions tests/pong-mode0/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
subprojects/libgba
20 changes: 20 additions & 0 deletions tests/pong-mode0/cross_file.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[binaries]
c = '/opt/devkitpro/devkitARM/bin/arm-none-eabi-gcc'
cpp = '/opt/devkitpro/devkitARM/bin/arm-none-eabi-g++'
ar = '/opt/devkitpro/devkitARM/bin/arm-none-eabi-ar'
strip = '/opt/devkitpro/devkitARM/bin/arm-none-eabi-strip'

[build-in options]
cpp_eh = 'none'
cpp_rtti = false
cpp_args = [
'-ffast-math',
'-fomit-frame-pointer',
'-mthumb-interwork', ]
default_library = 'static'

[host_machine]
system = 'none'
cpu_family = 'arm'
cpu = 'arm7tdmi'
endian = 'little'
Loading

0 comments on commit 1c182c4

Please sign in to comment.