mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 05:55:46 +01:00
use common integer type in shapes.c/shapes.h, for decreasing namespace pollution
This commit is contained in:
@@ -9,15 +9,16 @@
|
||||
#include <time.h>
|
||||
|
||||
#include "../io/audio.h"
|
||||
#include "../util/types.h"
|
||||
#include "./tetromino/shapes.h"
|
||||
#include "gametime.h"
|
||||
#include "tetromino/placing.h"
|
||||
|
||||
/* shuffle the array using a Fisher–Yates shuffle */
|
||||
static inline void shuffle(uint8_t const size, shape_id* const elmnts) {
|
||||
for (uint8_t i = 0; i < (size - 1); i++) {
|
||||
uint8_t const j = i + rand() % (size - i);
|
||||
shape_id const cache = elmnts[i];
|
||||
static inline void shuffle(uint8_t const size, u8* const elmnts) {
|
||||
for (uint i = 0; i < (size - 1); i++) {
|
||||
uint const j = i + rand() % (size - i);
|
||||
u8 const cache = elmnts[i];
|
||||
elmnts[i] = elmnts[j];
|
||||
elmnts[j] = cache;
|
||||
}
|
||||
@@ -35,7 +36,7 @@ void next_shape(gamedata* const dat) {
|
||||
dat->curr_idx = 0;
|
||||
|
||||
shuffle(TETROMINO_COUNT - 1, dat->nxt);
|
||||
shape_id cache = dat->nxt[0];
|
||||
u8 cache = dat->nxt[0];
|
||||
dat->nxt[0] = dat->nxt[TETROMINO_COUNT - 1];
|
||||
dat->nxt[TETROMINO_COUNT - 1] = cache;
|
||||
}
|
||||
@@ -70,7 +71,7 @@ void game_init(gamedata* const dat) {
|
||||
}
|
||||
|
||||
// set the shape data in each slot to it's corrsponding ID
|
||||
for (shape_id i = 0; i < TETROMINO_COUNT; i++)
|
||||
for (uint i = 0; i < TETROMINO_COUNT; i++)
|
||||
dat->nxt[i] = i;
|
||||
|
||||
dat->curr_idx = -1; // set the current index to the max so it becomes zero after increasement
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
#include "../io/audio.h"
|
||||
#include "../io/colour/colour8.h"
|
||||
#include "./tetromino/shapes.h"
|
||||
#include "../util/types.h"
|
||||
#include "gametime.h"
|
||||
|
||||
// constants for pi(π) and tau(τ)
|
||||
@@ -28,7 +28,7 @@ typedef struct {
|
||||
audiodata music;
|
||||
audiodata place_sfx;
|
||||
uint16_t score;
|
||||
shape_id nxt[7]; // the order of the shape ids that they should appear in
|
||||
u8 nxt[7]; // the order of the shape ids that they should appear in
|
||||
uint8_t curr_idx; // current shape index
|
||||
int8_t sel_x; // selected shape x position
|
||||
int8_t sel_y; // selected shape y position
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "../../io/audio.h"
|
||||
#include "../../io/colour/colour8.h"
|
||||
#include "../../util/types.h"
|
||||
#include "../game.h"
|
||||
#include "shapes.h"
|
||||
|
||||
@@ -56,11 +57,11 @@ static void clear_rows(row* const rows, uint16_t* const score) {
|
||||
}
|
||||
|
||||
// sets a shape to the screen
|
||||
static void set_shape_i(row const* const row, shape_id const id, int8_t const pos_x) {
|
||||
shape const shape = shape_from_id(id);
|
||||
static void set_shape_i(row const* const row, u8 const id, int8_t const pos_x) {
|
||||
u16 const shape = shape_from_id(id);
|
||||
colour8 const colour = colour_from_id(id);
|
||||
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
||||
shape_row const shape_row = shape_get_row(shape, y);
|
||||
u8 const shape_row = shape_get_row(shape, y);
|
||||
|
||||
if (shape_row == 0)
|
||||
continue;
|
||||
@@ -71,15 +72,15 @@ static void set_shape_i(row const* const row, shape_id const id, int8_t const po
|
||||
}
|
||||
}
|
||||
|
||||
static inline void set_shape(row const* const row, shape_id const id, int8_t const pos_x, int8_t const pos_y) {
|
||||
static inline void set_shape(row const* const row, u8 const id, int8_t const pos_x, int8_t const pos_y) {
|
||||
set_shape_i(&row[pos_y], id, pos_x); // calls itself, but omitting the pos_y argument, instead opting for specifying the row
|
||||
}
|
||||
|
||||
static int shape_intersects(row const* const rows, shape_id const id, int8_t const x, int8_t const y) {
|
||||
shape const shape = shape_from_id(id);
|
||||
static int shape_intersects(row const* const rows, u8 const id, int8_t const x, int8_t const y) {
|
||||
u16 const shape = shape_from_id(id);
|
||||
|
||||
for (int y0 = 0; y0 < SHAPE_HEIGHT; y0++) {
|
||||
shape_row const shape_row = shape_get_row(shape, y0); // get the shape row
|
||||
u8 const shape_row = shape_get_row(shape, y0); // get the shape row
|
||||
if (shape_row == 0) continue; // if the row doesn't contain data; continue
|
||||
|
||||
for (int x0 = 0; x0 < SHAPE_WIDTH; x0++) {
|
||||
@@ -95,14 +96,14 @@ static int shape_intersects(row const* const rows, shape_id const id, int8_t con
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline shape_id rotate_id(shape_id const id, int const dir) {
|
||||
static inline u8 rotate_id(u8 const id, int const dir) {
|
||||
return (id + dir) & 31;
|
||||
}
|
||||
|
||||
void place_update(gamedata* const game_data, input_data const move) {
|
||||
// store the current index and ID, only changes when placed (which yields no movement) and rotation (which occurs last)
|
||||
uint8_t const curr_idx = game_data->curr_idx;
|
||||
shape_id const curr_id = game_data->nxt[curr_idx];
|
||||
u8 curr_id = game_data->nxt[curr_idx];
|
||||
|
||||
|
||||
// set the shape if we moved vertically and intersected
|
||||
@@ -134,7 +135,7 @@ void place_update(gamedata* const game_data, input_data const move) {
|
||||
|
||||
// update the shape's rotation
|
||||
if (move & 8 || move & 16) {
|
||||
shape_id const id = move & 8 // check which direction we should move
|
||||
u8 const id = move & 8 // check which direction we should move
|
||||
? rotate_id(curr_id, -8)
|
||||
: rotate_id(curr_id, 8);
|
||||
if (shape_intersects(game_data->rows, id, game_data->sel_x, game_data->sel_y) == false) {
|
||||
|
||||
@@ -1,42 +1,43 @@
|
||||
#include "shapes.h"
|
||||
|
||||
#include "../../io/colour/colour8.h"
|
||||
#include "../../util/types.h"
|
||||
|
||||
/* 0 1 2 3 */
|
||||
#define SHAPE_O ((shape)0x0660) // 0000 0110 0110 0000 the O tetromino with no rotation
|
||||
#define SHAPE_O ((u16)0x0660) // 0000 0110 0110 0000 the O tetromino with no rotation
|
||||
|
||||
#define SHAPE_I ((shape)0x0F00) // 0000 1111 0000 0000 the I tetromino with no rotation
|
||||
#define SHAPE_I_90 ((shape)0x2222) // 0010 0010 0010 0010 the I tetromino with a 90° rotation
|
||||
#define SHAPE_I_180 ((shape)0x00F0) // 0000 0000 1111 0000 the I tetromino with a 180° rotation
|
||||
#define SHAPE_I_270 ((shape)0x4444) // 0100 0100 0100 0100 the I tetromino with a 270° rotation
|
||||
#define SHAPE_I ((u16)0x0F00) // 0000 1111 0000 0000 the I tetromino with no rotation
|
||||
#define SHAPE_I_90 ((u16)0x2222) // 0010 0010 0010 0010 the I tetromino with a 90° rotation
|
||||
#define SHAPE_I_180 ((u16)0x00F0) // 0000 0000 1111 0000 the I tetromino with a 180° rotation
|
||||
#define SHAPE_I_270 ((u16)0x4444) // 0100 0100 0100 0100 the I tetromino with a 270° rotation
|
||||
|
||||
#define SHAPE_S ((shape)0xC600) // 1100 0110 0000 0000 the Z tetromino with no rotation
|
||||
#define SHAPE_S_90 ((shape)0x2640) // 0010 0110 0100 0000 the Z tetromino with a 90° rotation
|
||||
#define SHAPE_S_180 ((shape)0x0C60) // 0000 1100 0110 0000 the Z tetromino with a 180° rotation
|
||||
#define SHAPE_S_270 ((shape)0x4C80) // 0100 1100 1000 0000 the Z tetromino with a 270° rotation
|
||||
#define SHAPE_S ((u16)0xC600) // 1100 0110 0000 0000 the Z tetromino with no rotation
|
||||
#define SHAPE_S_90 ((u16)0x2640) // 0010 0110 0100 0000 the Z tetromino with a 90° rotation
|
||||
#define SHAPE_S_180 ((u16)0x0C60) // 0000 1100 0110 0000 the Z tetromino with a 180° rotation
|
||||
#define SHAPE_S_270 ((u16)0x4C80) // 0100 1100 1000 0000 the Z tetromino with a 270° rotation
|
||||
|
||||
#define SHAPE_Z ((shape)0x6C00) // 0110 1100 0000 0000 the S tetromino with no rotation
|
||||
#define SHAPE_Z_90 ((shape)0x4620) // 0100 0110 0010 0000 the S tetromino with a 90° rotation
|
||||
#define SHAPE_Z_180 ((shape)0x06C0) // 0000 0110 1100 0000 the S tetromino with a 180° rotation
|
||||
#define SHAPE_Z_270 ((shape)0x8C40) // 1000 1100 0100 0000 the S tetromino with a 270° rotation
|
||||
#define SHAPE_Z ((u16)0x6C00) // 0110 1100 0000 0000 the S tetromino with no rotation
|
||||
#define SHAPE_Z_90 ((u16)0x4620) // 0100 0110 0010 0000 the S tetromino with a 90° rotation
|
||||
#define SHAPE_Z_180 ((u16)0x06C0) // 0000 0110 1100 0000 the S tetromino with a 180° rotation
|
||||
#define SHAPE_Z_270 ((u16)0x8C40) // 1000 1100 0100 0000 the S tetromino with a 270° rotation
|
||||
|
||||
#define SHAPE_T ((shape)0x0E40) // 0000 1110 0100 0000 the T tetromino with no rotation
|
||||
#define SHAPE_T_90 ((shape)0x4C40) // 0100 1100 0100 0000 the T tetromino with a 90° rotation
|
||||
#define SHAPE_T_180 ((shape)0x4E00) // 0100 1110 0000 0000 the T tetromino with a 180° rotation
|
||||
#define SHAPE_T_270 ((shape)0x4640) // 0100 0110 0100 0000 the T tetromino with a 270° rotation
|
||||
#define SHAPE_T ((u16)0x0E40) // 0000 1110 0100 0000 the T tetromino with no rotation
|
||||
#define SHAPE_T_90 ((u16)0x4C40) // 0100 1100 0100 0000 the T tetromino with a 90° rotation
|
||||
#define SHAPE_T_180 ((u16)0x4E00) // 0100 1110 0000 0000 the T tetromino with a 180° rotation
|
||||
#define SHAPE_T_270 ((u16)0x4640) // 0100 0110 0100 0000 the T tetromino with a 270° rotation
|
||||
|
||||
#define SHAPE_L ((shape)0x4460) // 0100 0100 0110 0000 the L tetromino with no rotation
|
||||
#define SHAPE_L_90 ((shape)0x0E80) // 0000 1110 1000 0000 the L tetromino with a 90° rotation
|
||||
#define SHAPE_L_180 ((shape)0xC440) // 1100 0100 0100 0000 the L tetromino with a 180° rotation
|
||||
#define SHAPE_L_270 ((shape)0x2E00) // 0010 1110 0000 0000 the L tetromino with a 270° rotation
|
||||
#define SHAPE_L ((u16)0x4460) // 0100 0100 0110 0000 the L tetromino with no rotation
|
||||
#define SHAPE_L_90 ((u16)0x0E80) // 0000 1110 1000 0000 the L tetromino with a 90° rotation
|
||||
#define SHAPE_L_180 ((u16)0xC440) // 1100 0100 0100 0000 the L tetromino with a 180° rotation
|
||||
#define SHAPE_L_270 ((u16)0x2E00) // 0010 1110 0000 0000 the L tetromino with a 270° rotation
|
||||
|
||||
#define SHAPE_J ((shape)0x44C0) // 0100 0100 1100 0000 the J tetromino with no rotation
|
||||
#define SHAPE_J_90 ((shape)0x8E00) // 1000 1110 0000 0000 the J tetromino with a 90° rotation
|
||||
#define SHAPE_J_180 ((shape)0x6440) // 0110 0100 0100 0000 the J tetromino with a 180° rotation
|
||||
#define SHAPE_J_270 ((shape)0x0E20) // 0000 1110 0010 0000 the J tetromino with a 270° rotation
|
||||
#define SHAPE_J ((u16)0x44C0) // 0100 0100 1100 0000 the J tetromino with no rotation
|
||||
#define SHAPE_J_90 ((u16)0x8E00) // 1000 1110 0000 0000 the J tetromino with a 90° rotation
|
||||
#define SHAPE_J_180 ((u16)0x6440) // 0110 0100 0100 0000 the J tetromino with a 180° rotation
|
||||
#define SHAPE_J_270 ((u16)0x0E20) // 0000 1110 0010 0000 the J tetromino with a 270° rotation
|
||||
|
||||
shape shape_from_id(shape_id const id) {
|
||||
static shape const shapes[TETROMINO_COUNT][4] = {
|
||||
u16 shape_from_id(u8 id) {
|
||||
static u16 const shapes[TETROMINO_COUNT][4] = {
|
||||
// 0° 90° 180° 170°
|
||||
{SHAPE_O, SHAPE_O, SHAPE_O, SHAPE_O },
|
||||
{SHAPE_I, SHAPE_I_90, SHAPE_I_180, SHAPE_I_270},
|
||||
@@ -47,11 +48,11 @@ shape shape_from_id(shape_id const id) {
|
||||
{SHAPE_J, SHAPE_J_90, SHAPE_J_180, SHAPE_J_270},
|
||||
};
|
||||
|
||||
// first 3 bits is the shape type, the rest is rotation data
|
||||
// first 3 least significant bits is the shape type, the rest is rotation data
|
||||
return shapes[id & 7][id >> 3];
|
||||
}
|
||||
|
||||
colour8 colour_from_id(shape_id const id) {
|
||||
colour8 colour_from_id(u8 id) {
|
||||
switch (id & 7) {
|
||||
case TETROMINO_O: return COLOUR8_YELLOW;
|
||||
case TETROMINO_I: return COLOUR8_CYAN;
|
||||
|
||||
@@ -3,11 +3,8 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../../io/colour/colour8.h"
|
||||
#include "../../util/types.h"
|
||||
|
||||
typedef uint16_t shape;
|
||||
typedef uint8_t shape_row;
|
||||
|
||||
typedef uint8_t shape_id;
|
||||
enum {
|
||||
TETROMINO_O = 0,
|
||||
TETROMINO_I = 1,
|
||||
@@ -27,13 +24,13 @@ enum {
|
||||
|
||||
|
||||
|
||||
static inline shape_row shape_get_row(shape const shape, uint8_t const index) {
|
||||
static inline u8 shape_get_row(u16 shape, u8 index) {
|
||||
return shape >> (((SHAPE_HEIGHT - 1) - index) * SHAPE_WIDTH) & 0xF;
|
||||
}
|
||||
|
||||
static inline bool shape_is_set(shape_row const row, uint8_t const index) {
|
||||
static inline bool shape_is_set(u8 row, u8 index) {
|
||||
return (row >> ((SHAPE_WIDTH - 1) - index) & 1) != 0;
|
||||
}
|
||||
|
||||
shape shape_from_id(shape_id id);
|
||||
colour8 colour_from_id(shape_id id);
|
||||
u16 shape_from_id(u8 id);
|
||||
colour8 colour_from_id(u8 id);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "../error.h"
|
||||
#include "../game/game.h"
|
||||
#include "../game/tetromino/shapes.h"
|
||||
#include "../util/types.h"
|
||||
#include "colour/colour32.h"
|
||||
#include "colour/colour8.h"
|
||||
|
||||
@@ -90,12 +91,12 @@ static inline int draw_block(SDL_Renderer* const renderer, int8_t const x, int8_
|
||||
}
|
||||
|
||||
// draws a shape at the specified position
|
||||
static void draw_shape(SDL_Renderer* const renderer, shape_id const id, int8_t const pos_x, int8_t const pos_y) {
|
||||
shape const shape = shape_from_id(id);
|
||||
static void draw_shape(SDL_Renderer* const renderer, u8 const id, int8_t const pos_x, int8_t const pos_y) {
|
||||
u16 shape = shape_from_id(id);
|
||||
set_colour8(renderer, colour_from_id(id));
|
||||
|
||||
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
||||
shape_row const shape_row = shape_get_row(shape, y);
|
||||
u8 shape_row = shape_get_row(shape, y);
|
||||
|
||||
if (shape_row == 0)
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user