1
0
mirror of https://github.com/lefticus/6502-cpp.git synced 2025-07-18 19:24:16 +00:00

Cleanups for Conf

This commit is contained in:
Jason Turner
2016-08-24 16:56:07 -06:00
parent e3ebc954fa
commit 03d360ac56

View File

@@ -2,7 +2,10 @@
#include <array> #include <array>
#include <utility> #include <utility>
#include <algorithm> #include <algorithm>
#include <tuple>
constexpr uint16_t JOYSTICK_PORT_A = 56320; // joystick #2
constexpr uint16_t JOYSTICK_PORT_B = 56321; // joystick #1
constexpr uint16_t SPRITE_DATA_POINTERS = 2040; constexpr uint16_t SPRITE_DATA_POINTERS = 2040;
constexpr uint16_t VIDEO_REGISTERS = 53248; constexpr uint16_t VIDEO_REGISTERS = 53248;
constexpr uint16_t SPRITE_ENABLE_BITS = VIDEO_REGISTERS + 21; constexpr uint16_t SPRITE_ENABLE_BITS = VIDEO_REGISTERS + 21;
@@ -19,11 +22,6 @@ constexpr uint16_t BACKGROUND_COLOR = 53281;
constexpr uint16_t SPRITE_0_COLOR = VIDEO_REGISTERS + 39; constexpr uint16_t SPRITE_0_COLOR = VIDEO_REGISTERS + 39;
constexpr uint16_t SPRITE_1_COLOR = SPRITE_0_COLOR + 1; constexpr uint16_t SPRITE_1_COLOR = SPRITE_0_COLOR + 1;
constexpr uint16_t SPRITE_2_COLOR = SPRITE_1_COLOR + 1; constexpr uint16_t SPRITE_2_COLOR = SPRITE_1_COLOR + 1;
constexpr uint16_t SPRITE_3_COLOR = SPRITE_2_COLOR + 1;
constexpr uint16_t SPRITE_4_COLOR = SPRITE_3_COLOR + 1;
constexpr uint16_t SPRITE_5_COLOR = SPRITE_4_COLOR + 1;
constexpr uint16_t SPRITE_6_COLOR = SPRITE_5_COLOR + 1;
constexpr uint16_t SPRITE_7_COLOR = SPRITE_6_COLOR + 1;
@@ -131,9 +129,9 @@ auto joystick(const uint8_t port) {
}; };
if (port == 2) { if (port == 2) {
return State(memory(56320)); return State(memory(JOYSTICK_PORT_A));
} else { } else {
return State(memory(56321)); return State(memory(JOYSTICK_PORT_B));
} }
}; };
@@ -168,11 +166,46 @@ auto nearest_color(const Colors &colors)
} }
auto sprite_collisions() {
const auto collisions = memory(SPRITE_COLLISIONS);
return std::make_tuple(
test_bit(collisions, 0),test_bit(collisions, 1),test_bit(collisions, 2),
test_bit(collisions, 3),test_bit(collisions, 4),test_bit(collisions, 5),
test_bit(collisions, 6),test_bit(collisions, 7));
};
struct Player
{
Player(const uint8_t num, const uint8_t startx, const uint8_t starty)
: player_num(num),
x(sprite_x(player_num)),
y(sprite_y(player_num))
{
x = startx;
y = starty;
}
void update_position()
{
y += joystick(player_num).direction_vector().second * 3;
};
void scored() {
++score;
}
const uint8_t player_num;
volatile uint8_t &x;
volatile uint8_t &y;
uint8_t score = '0';
};
} }
int main() int main()
{ {
// http://www.pepto.de/projects/colorvic/
constexpr std::array<Color, 16> colors {{ constexpr std::array<Color, 16> colors {{
Color{0, 0x00, 0x00, 0x00}, Color{0, 0x00, 0x00, 0x00},
Color{1, 0xFF, 0xFF, 0xFF}, Color{1, 0xFF, 0xFF, 0xFF},
@@ -221,6 +254,7 @@ int main()
enable_sprite(0, 0, false, true, false, false); enable_sprite(0, 0, false, true, false, false);
make_sprite(1, make_sprite(1,
0,0,0,0,0,2,2,0,0,0,0,0, 0,0,0,0,0,2,2,0,0,0,0,0,
0,0,0,0,0,2,2,0,0,0,0,0, 0,0,0,0,0,2,2,0,0,0,0,0,
@@ -250,23 +284,6 @@ int main()
enable_sprite(1, 1, true, false, false, true); enable_sprite(1, 1, true, false, false, true);
enable_sprite(2, 1, true, false, false, true); enable_sprite(2, 1, true, false, false, true);
const auto sprite_collisions = []() {
const auto collisions = memory(SPRITE_COLLISIONS);
memory(SPRITE_COLLISIONS) = 0;
struct Col_Data {
bool sprite0;
bool sprite1;
bool sprite2;
bool sprite3;
bool sprite4;
bool sprite5;
bool sprite6;
bool sprite7;
};
return Col_Data{test_bit(collisions, 0),test_bit(collisions, 1),test_bit(collisions, 2),test_bit(collisions, 3),
test_bit(collisions, 4),test_bit(collisions, 5),test_bit(collisions, 6),test_bit(collisions, 7)};
};
std::pair<int8_t, int8_t> ball_velocity{1,1}; std::pair<int8_t, int8_t> ball_velocity{1,1};
@@ -283,31 +300,6 @@ int main()
memory(SPRITE_2_COLOR) = nearest_color<0,255,0>(colors).num; memory(SPRITE_2_COLOR) = nearest_color<0,255,0>(colors).num;
struct Player
{
Player(const uint8_t num, const uint8_t startx, const uint8_t starty)
: player_num(num),
x(sprite_x(player_num)),
y(sprite_y(player_num))
{
x = startx;
y = starty;
}
void update_position()
{
y += joystick(player_num).direction_vector().second * 3;
};
void scored() {
++score;
}
const uint8_t player_num;
volatile uint8_t &x;
volatile uint8_t &y;
uint8_t score = '0';
};
Player p1(1, 15, 255/2); Player p1(1, 15, 255/2);
Player p2(2, 255, 255/2); Player p2(2, 255, 255/2);
@@ -333,12 +325,13 @@ int main()
while (true) { while (true) {
Frame rt(p1, p2); Frame rt(p1, p2);
if (auto [s0, s1, s2, s3, s4, s5, s6, s7] = sprite_collisions(); if (const auto [s0, s1, s2, s3, s4, s5, s6, s7] = sprite_collisions();
s0 && (s1 || s2)) s0 && (s1 || s2))
{ {
std::get<0>(ball_velocity) *= -1; //invert ball x velocity auto &x_velocity = std::get<0>(ball_velocity);
x_velocity *= -1; //invert ball x velocity
// "bounce" ball out of collision area // "bounce" ball out of collision area
sprite_x(0) += std::get<0>(ball_velocity); sprite_x(0) += x_velocity;
} }
// Update paddle positions // Update paddle positions
@@ -346,7 +339,6 @@ int main()
p2.update_position(); p2.update_position();
// ball hit the top or bottom wall // ball hit the top or bottom wall
if (const auto ball_y = sprite_y(0) += std::get<1>(ball_velocity); if (const auto ball_y = sprite_y(0) += std::get<1>(ball_velocity);
ball_y == 45 || ball_y == 235) ball_y == 45 || ball_y == 235)
@@ -370,4 +362,3 @@ int main()
} }
} }