run formatter

This commit is contained in:
Cole
2024-01-11 20:58:37 -06:00
parent 4298102525
commit e818e59037
9 changed files with 355 additions and 334 deletions

2
.gitignore vendored
View File

@@ -1 +1 @@
./src/tetris-clone tetris-clone

View File

@@ -1,65 +1,69 @@
#include <stdio.h> #include <stdio.h>
#include "raylib.h" #include "app.h"
#include "manager.h" #include "manager.h"
#include "raylib.h"
#include "renderer.h" #include "renderer.h"
#include "tetromino.h" #include "tetromino.h"
#include "app.h"
#define TARGET_FPS 60 #define TARGET_FPS 60
void a_run(void) { void a_run(void) {
_a_initialize(); _a_initialize();
_a_loop(); _a_loop();
_a_cleanup(); _a_cleanup();
} }
void _a_initialize(void) { void _a_initialize(void) {
_a_initialize_raylib(); _a_initialize_raylib();
m_initialize(); m_initialize();
r_initialize(); r_initialize();
_a_gameboard_clear(); _a_gameboard_clear();
} }
void _a_initialize_raylib(void) { void _a_initialize_raylib(void) {
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tetris Clone"); InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tetris Clone");
SetTargetFPS(TARGET_FPS); SetTargetFPS(TARGET_FPS);
} }
void _a_gameboard_clear(void) { void _a_gameboard_clear(void) {
m_blocks_set_empty(); m_blocks_set_empty();
int number_blocks_updated = 0; int number_blocks_updated = 0;
struct TetrominoBlock** blocks_updated = m_blocks_get_updated(&number_blocks_updated); struct TetrominoBlock **blocks_updated =
r_render_blocks(blocks_updated, number_blocks_updated); m_blocks_get_updated(&number_blocks_updated);
r_render_blocks(blocks_updated, number_blocks_updated);
} }
void _a_loop(void) { void _a_loop(void) {
int tick_rate = 0; // Guaranteed a better way to do this XD int tick_rate = 0; // Guaranteed a better way to do this XD
while(!WindowShouldClose()) { while (!WindowShouldClose()) {
while(m_tetromino_can_spawn()) { while (m_tetromino_can_spawn()) {
_a_input_process(); if (WindowShouldClose())
if (tick_rate == 0) { return;
m_update(); _a_input_process();
tick_rate = 50; if (tick_rate == 0) {
} m_update();
int number_blocks_updated = 0; tick_rate = 50;
struct TetrominoBlock** blocks_updated = GetUpdatedBlocks(&number_blocks_updated); }
r_render_blocks(blocks_updated, number_blocks_updated); int number_blocks_updated = 0;
tick_rate--; struct TetrominoBlock **blocks_updated =
} m_blocks_get_updated(&number_blocks_updated);
r_render_game_over(0); r_render_blocks(blocks_updated, number_blocks_updated);
tick_rate--;
} }
r_render_game_over(0);
}
} }
void _a_input_process(void) { void _a_input_process(void) {
if (IsKeyPressed(KEY_SPACE)) { if (IsKeyPressed(KEY_SPACE)) {
m_request_falling_tetromino_rotate(); m_request_falling_tetromino_rotate();
} }
if (IsKeyDown(KEY_A)) { if (IsKeyDown(KEY_A)) {
m_request_falling_tetromino_move_left(); m_request_falling_tetromino_move_left();
} else if (IsKeyDown(KEY_D)) { } else if (IsKeyDown(KEY_D)) {
m_request_falling_tetromino_move_right(); m_request_falling_tetromino_move_right();
} }
} }
void _a_cleanup() { void _a_cleanup() {

View File

@@ -1,7 +1,7 @@
#include "app.h" #include "app.h"
int main(void) { int main(void) {
a_run(); a_run();
return 0; return 0;
} }

View File

@@ -1,5 +1,5 @@
#include <stdlib.h> //srand,rand, #include <stdlib.h> //srand,rand,
#include <time.h> //time #include <time.h> //time
#define __USE_MISC #define __USE_MISC
#include <math.h> //cos,sin #include <math.h> //cos,sin
@@ -11,33 +11,55 @@
// should probably call these offsets // should probably call these offsets
struct Offset { struct Offset {
int xOffset; int xOffset;
int yOffset; int yOffset;
}; };
struct Point { struct Point {
int x; int x;
int y; int y;
}; };
struct Tetromino { struct Tetromino {
enum TetrominoType type; enum TetrominoType type;
struct TetrominoBlock* blocks[BLOCKS_WITHIN_A_TETROMINO]; struct TetrominoBlock *blocks[BLOCKS_WITHIN_A_TETROMINO];
}; };
struct TetrominoBlock *_m_block_at_point(struct Point point);
void _m_blocks_updated_register_block(struct TetrominoBlock *pTetrominoBlock);
void _m_blocks_alloc(void);
void _m_blocks_updated_erase(void);
void _m_blocks_spawn_at_offset_from_spawn_point(enum TetrominoType type,
struct Offset *offsets);
void _m_tetromino_spawn(void);
enum TetrominoType _m_tetromino_type_random(void);
void _m_block_set_type(struct TetrominoBlock *pTetrominoBlock,
enum TetrominoType type);
void _m_falling_tetromino_rotate(void);
bool _m_falling_tetromino_can_move_right(void);
void _m_falling_tetromino_translate(struct Offset offset);
bool _m_point_intersects_static_block(struct Point point);
bool _m_block_can_fall(struct TetrominoBlock block);
bool _m_falling_tetromino_can_fall(void);
void _m_falling_tetromino_fall(void);
bool _m_block_can_move_left(struct TetrominoBlock block);
bool _m_falling_tetromino_can_move_left(void);
bool _m_block_can_move_right(struct TetrominoBlock block);
// To find block (x,y) index = (x % FIELD_WIDTH) + (y * FIELD_WIDTH) // To find block (x,y) index = (x % FIELD_WIDTH) + (y * FIELD_WIDTH)
// Height doesn't matter but height is just the factor of how tall the board is. // Height doesn't matter but height is just the factor of how tall the board
// And in the end each row is just the next representation of width * units, height just determines row count. // is. And in the end each row is just the next representation of width *
// Doesn't factor into determining X,Y // units, height just determines row count. Doesn't factor into determining
struct TetrominoBlock* _blocks[FIELD_HEIGHT * FIELD_WIDTH]; // X,Y
struct TetrominoBlock* _blocks_updated[FIELD_HEIGHT * FIELD_WIDTH] = { NULL }; struct TetrominoBlock *_blocks[FIELD_HEIGHT * FIELD_WIDTH];
struct TetrominoBlock *_blocks_updated[FIELD_HEIGHT * FIELD_WIDTH] = {NULL};
int _blocks_updated_length = 0; int _blocks_updated_length = 0;
bool _should_spawn_tetromino; bool _should_spawn_tetromino;
struct Tetromino fallingTetromino; struct Tetromino fallingTetromino;
const char kSpawnX = FIELD_WIDTH / 2 - 1; const char kSpawnX = FIELD_WIDTH / 2 - 1;
const struct Offset kShiftDownOffset = { .xOffset = 0, .yOffset = 1 }; const struct Offset kShiftDownOffset = {.xOffset = 0, .yOffset = 1};
const struct Offset kShiftLeftOffset = { .xOffset = -1, .yOffset = 0 }; const struct Offset kShiftLeftOffset = {.xOffset = -1, .yOffset = 0};
const struct Offset kShiftRightOffset = { .xOffset = 1, .yOffset = 0 }; const struct Offset kShiftRightOffset = {.xOffset = 1, .yOffset = 0};
bool _should_tetromino_move_left = false; bool _should_tetromino_move_left = false;
bool _should_tetromino_move_right = false; bool _should_tetromino_move_right = false;
@@ -45,343 +67,343 @@ bool _should_tetromino_rotate = false;
bool _tetromino_can_spawn = true; bool _tetromino_can_spawn = true;
void m_initialize(void) { void m_initialize(void) {
_m_blocks_alloc(); _m_blocks_alloc();
_should_spawn_tetromino = true; _should_spawn_tetromino = true;
} }
void m_update(void) { void m_update(void) {
_m_blocks_updated_erase(); _m_blocks_updated_erase();
if (_should_spawn_tetromino) { if (_should_spawn_tetromino) {
_should_tetromino_move_left = false; _should_tetromino_move_left = false;
_should_tetromino_move_right = false; _should_tetromino_move_right = false;
_m_tetromino_spawn(); _m_tetromino_spawn();
_should_spawn_tetromino = false; _should_spawn_tetromino = false;
} else { } else {
// TODO move left and right refactor naming // TODO move left and right refactor naming
if(_should_tetromino_rotate) { if (_should_tetromino_rotate) {
_m_falling_tetromino_rotate(); _m_falling_tetromino_rotate();
_should_tetromino_rotate = false; _should_tetromino_rotate = false;
}
if (_should_tetromino_move_right && _m_falling_tetromino_can_move_right()) {
_m_falling_tetromino_translate(kShiftRightOffset);
_should_tetromino_move_right = false;
}
if (_should_tetromino_move_left && _m_falling_tetromino_can_move_left()) {
_m_falling_tetromino_translate(kShiftLeftOffset);
_should_tetromino_move_left = false;
}
_m_falling_tetromino_fall();
} }
if (_should_tetromino_move_right && _m_falling_tetromino_can_move_right()) {
_m_falling_tetromino_translate(kShiftRightOffset);
_should_tetromino_move_right = false;
}
if (_should_tetromino_move_left && _m_falling_tetromino_can_move_left()) {
_m_falling_tetromino_translate(kShiftLeftOffset);
_should_tetromino_move_left = false;
}
_m_falling_tetromino_fall();
}
} }
void m_blocks_set_empty(void) { void m_blocks_set_empty(void) {
for (int i = 0; i < FIELD_HEIGHT * FIELD_WIDTH; i++) { for (int i = 0; i < FIELD_HEIGHT * FIELD_WIDTH; i++) {
_m_block_set_type(_blocks[i], TT_EMPTY); _m_block_set_type(_blocks[i], TT_EMPTY);
} }
} }
bool m_tetromino_can_spawn(void) { bool m_tetromino_can_spawn(void) { return _tetromino_can_spawn; }
return _tetromino_can_spawn;
}
void m_request_falling_tetromino_rotate(void) { void m_request_falling_tetromino_rotate(void) {
_should_tetromino_rotate = true; _should_tetromino_rotate = true;
} }
void m_request_falling_tetromino_move_left(void) { void m_request_falling_tetromino_move_left(void) {
_should_tetromino_move_left = true; _should_tetromino_move_left = true;
_should_tetromino_move_right = false; _should_tetromino_move_right = false;
} }
void m_request_falling_tetromino_move_right(void) { void m_request_falling_tetromino_move_right(void) {
_should_tetromino_move_right = true; _should_tetromino_move_right = true;
_should_tetromino_move_left = false; _should_tetromino_move_left = false;
} }
struct TetrominoBlock** m_blocks_get_updated(int* numberOfUpdatedBlocks) { struct TetrominoBlock **m_blocks_get_updated(int *numberOfUpdatedBlocks) {
*numberOfUpdatedBlocks = _blocks_updated_length; *numberOfUpdatedBlocks = _blocks_updated_length;
return &_blocks_updated[0]; return &_blocks_updated[0];
} }
void m_deactivate(void) { void m_deactivate(void) {
for(int i = 0; i < sizeof(_blocks) / sizeof(struct TetrominoBlock*); i++) { for (int i = 0; i < sizeof(_blocks) / sizeof(struct TetrominoBlock *); i++) {
free(_blocks[i]); free(_blocks[i]);
} }
} }
void _m_blocks_alloc(void) { void _m_blocks_alloc(void) {
for (int i = 0; i < FIELD_HEIGHT * FIELD_WIDTH; i++) { for (int i = 0; i < FIELD_HEIGHT * FIELD_WIDTH; i++) {
struct TetrominoBlock* block = calloc(1, sizeof(struct TetrominoBlock)); struct TetrominoBlock *block = calloc(1, sizeof(struct TetrominoBlock));
block->x = i % FIELD_WIDTH; block->x = i % FIELD_WIDTH;
block->y = i / FIELD_WIDTH; block->y = i / FIELD_WIDTH;
_blocks[i] = block; _blocks[i] = block;
} }
} }
void _m_blocks_updated_erase() { void _m_blocks_updated_erase() { _blocks_updated_length = 0; }
_blocks_updated_length = 0;
}
void _m_tetromino_spawn() { void _m_tetromino_spawn() {
enum TetrominoType type = _m_tetromino_type_get_random(); enum TetrominoType type = _m_tetromino_type_random();
struct Offset offsets[BLOCKS_WITHIN_A_TETROMINO]; struct Offset offsets[BLOCKS_WITHIN_A_TETROMINO];
if (type == TT_O) { if (type == TT_O) {
offsets[0].xOffset = 0; offsets[0].xOffset = 0;
offsets[0].yOffset = 0; offsets[0].yOffset = 0;
offsets[1].xOffset = 1; offsets[1].xOffset = 1;
offsets[1].yOffset = 0; offsets[1].yOffset = 0;
offsets[2].xOffset = 0; offsets[2].xOffset = 0;
offsets[2].yOffset = 1; offsets[2].yOffset = 1;
offsets[3].xOffset = 1; offsets[3].xOffset = 1;
offsets[3].yOffset = 1; offsets[3].yOffset = 1;
} else if (type == TT_J) { } else if (type == TT_J) {
offsets[0].xOffset = 0; offsets[0].xOffset = 0;
offsets[0].yOffset = 0; offsets[0].yOffset = 0;
offsets[1].xOffset = 0; offsets[1].xOffset = 0;
offsets[1].yOffset = 1; offsets[1].yOffset = 1;
offsets[2].xOffset = 1; offsets[2].xOffset = 1;
offsets[2].yOffset = 1; offsets[2].yOffset = 1;
offsets[3].xOffset = 2; offsets[3].xOffset = 2;
offsets[3].yOffset = 1; offsets[3].yOffset = 1;
} else if (type == TT_L) { } else if (type == TT_L) {
offsets[0].xOffset = 0; offsets[0].xOffset = 0;
offsets[0].yOffset = 1; offsets[0].yOffset = 1;
offsets[1].xOffset = 1; offsets[1].xOffset = 1;
offsets[1].yOffset = 1; offsets[1].yOffset = 1;
offsets[2].xOffset = 2; offsets[2].xOffset = 2;
offsets[2].yOffset = 1; offsets[2].yOffset = 1;
offsets[3].xOffset = 2; offsets[3].xOffset = 2;
offsets[3].yOffset = 0; offsets[3].yOffset = 0;
} else if (type == TT_I) { } else if (type == TT_I) {
offsets[0].xOffset = 0; offsets[0].xOffset = 0;
offsets[0].yOffset = 0; offsets[0].yOffset = 0;
offsets[1].xOffset = 0; offsets[1].xOffset = 0;
offsets[1].yOffset = 1; offsets[1].yOffset = 1;
offsets[2].xOffset = 0; offsets[2].xOffset = 0;
offsets[2].yOffset = 2; offsets[2].yOffset = 2;
offsets[3].xOffset = 0; offsets[3].xOffset = 0;
offsets[3].yOffset = 3; offsets[3].yOffset = 3;
} else if (type == TT_S) { } else if (type == TT_S) {
offsets[0].xOffset = 0; offsets[0].xOffset = 0;
offsets[0].yOffset = 1; offsets[0].yOffset = 1;
offsets[1].xOffset = 1; offsets[1].xOffset = 1;
offsets[1].yOffset = 1; offsets[1].yOffset = 1;
offsets[2].xOffset = 1; offsets[2].xOffset = 1;
offsets[2].yOffset = 0; offsets[2].yOffset = 0;
offsets[3].xOffset = 2; offsets[3].xOffset = 2;
offsets[3].yOffset = 0; offsets[3].yOffset = 0;
} else if (type == TT_Z) { } else if (type == TT_Z) {
offsets[0].xOffset = 0; offsets[0].xOffset = 0;
offsets[0].yOffset = 0; offsets[0].yOffset = 0;
offsets[1].xOffset = 1; offsets[1].xOffset = 1;
offsets[1].yOffset = 0; offsets[1].yOffset = 0;
offsets[2].xOffset = 1; offsets[2].xOffset = 1;
offsets[2].yOffset = 1; offsets[2].yOffset = 1;
offsets[3].xOffset = 2; offsets[3].xOffset = 2;
offsets[3].yOffset = 1; offsets[3].yOffset = 1;
} else if (type == TT_T) { } else if (type == TT_T) {
offsets[0].xOffset = 1; offsets[0].xOffset = 1;
offsets[0].yOffset = 0; offsets[0].yOffset = 0;
offsets[1].xOffset = 0; offsets[1].xOffset = 0;
offsets[1].yOffset = 1; offsets[1].yOffset = 1;
offsets[2].xOffset = 1; offsets[2].xOffset = 1;
offsets[2].yOffset = 1; offsets[2].yOffset = 1;
offsets[3].xOffset = 2; offsets[3].xOffset = 2;
offsets[3].yOffset = 1; offsets[3].yOffset = 1;
} }
_m_blocks_spawn_at_offset_from_spawn_point(type, offsets); _m_blocks_spawn_at_offset_from_spawn_point(type, offsets);
} }
enum TetrominoType _m_tetromino_type_random(void) { enum TetrominoType _m_tetromino_type_random(void) {
srand(time(NULL)); srand(time(NULL));
return (enum TetrominoType) rand() % 6; return (enum TetrominoType)rand() % 6;
} }
void _m_blocks_spawn_at_offset_from_spawn_point(enum TetrominoType type, struct Offset *offsets) { void _m_blocks_spawn_at_offset_from_spawn_point(enum TetrominoType type,
fallingTetromino.type = type; struct Offset *offsets) {
int index = 0; fallingTetromino.type = type;
while(index < BLOCKS_WITHIN_A_TETROMINO) { int index = 0;
struct Point spawnPoint; while (index < BLOCKS_WITHIN_A_TETROMINO) {
spawnPoint.x = kSpawnX + offsets->xOffset; struct Point spawnPoint;
spawnPoint.y = offsets->yOffset; spawnPoint.x = kSpawnX + offsets->xOffset;
spawnPoint.y = offsets->yOffset;
struct TetrominoBlock* spawnBlock = _m_block_at_point(spawnPoint); struct TetrominoBlock *spawnBlock = _m_block_at_point(spawnPoint);
if (spawnBlock->type != TT_EMPTY) { if (spawnBlock->type != TT_EMPTY) {
_tetromino_can_spawn = false; _tetromino_can_spawn = false;
return; return;
} }
_m_block_set_type(spawnBlock, type); _m_block_set_type(spawnBlock, type);
// I'm not sure if this makes more sense to be here or return a list of spawnedBlocks to be handled in Parent function // I'm not sure if this makes more sense to be here or return a list of
fallingTetromino.blocks[index] = spawnBlock; // spawnedBlocks to be handled in Parent function
fallingTetromino.blocks[index] = spawnBlock;
index++; index++;
offsets++; offsets++;
} }
} }
struct TetrominoBlock *_m_block_at_point(struct Point point) { struct TetrominoBlock *_m_block_at_point(struct Point point) {
return _blocks[(point.x % FIELD_WIDTH) + (point.y * FIELD_WIDTH)]; return _blocks[(point.x % FIELD_WIDTH) + (point.y * FIELD_WIDTH)];
} }
void _m_block_set_type(struct TetrominoBlock *pTetrominoBlock, enum TetrominoType type) { void _m_block_set_type(struct TetrominoBlock *pTetrominoBlock,
pTetrominoBlock->type = type; enum TetrominoType type) {
_m_blocks_updated_register_block(pTetrominoBlock); pTetrominoBlock->type = type;
_m_blocks_updated_register_block(pTetrominoBlock);
} }
void _m_blocks_updated_register_block(struct TetrominoBlock *pTetrominoBlock) { void _m_blocks_updated_register_block(struct TetrominoBlock *pTetrominoBlock) {
if (_blocks_updated_length < FIELD_HEIGHT * FIELD_WIDTH) { if (_blocks_updated_length < FIELD_HEIGHT * FIELD_WIDTH) {
_blocks_updated[_blocks_updated_length] = pTetrominoBlock; _blocks_updated[_blocks_updated_length] = pTetrominoBlock;
_blocks_updated_length++; _blocks_updated_length++;
} }
} }
void _m_falling_tetromino_rotate(void) { void _m_falling_tetromino_rotate(void) {
if (fallingTetromino.type != TT_O) { if (fallingTetromino.type != TT_O) {
// i'm not a fan of this implementation // i'm not a fan of this implementation
// check this out instead and re-write // check this out instead and re-write
// https://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array?page=1&tab=scoredesc#tab-top // https://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array?page=1&tab=scoredesc#tab-top
}
}
} }
bool _m_falling_tetromino_can_move_right(void) { bool _m_falling_tetromino_can_move_right(void) {
bool result = true; bool result = true;
for(int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) { for (int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) {
if (!_m_block_can_move_right(*(fallingTetromino.blocks[i]))) { if (!_m_block_can_move_right(*(fallingTetromino.blocks[i]))) {
result = false; result = false;
break; break;
}
} }
}
return result; return result;
} }
void _m_falling_tetromino_translate(struct Offset offset) { void _m_falling_tetromino_translate(struct Offset offset) {
for(int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) { for (int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) {
_m_block_set_type(fallingTetromino.blocks[i], TT_EMPTY); _m_block_set_type(fallingTetromino.blocks[i], TT_EMPTY);
_m_blocks_updated_register_block(fallingTetromino.blocks[i]); _m_blocks_updated_register_block(fallingTetromino.blocks[i]);
} }
for(int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) { for (int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) {
struct Point updatedPoint = { .x = fallingTetromino.blocks[i]->x + offset.xOffset, .y = fallingTetromino.blocks[i]->y + offset.yOffset}; struct Point updatedPoint = {
fallingTetromino.blocks[i] = _m_block_at_point(updatedPoint); .x = fallingTetromino.blocks[i]->x + offset.xOffset,
.y = fallingTetromino.blocks[i]->y + offset.yOffset};
fallingTetromino.blocks[i] = _m_block_at_point(updatedPoint);
_m_block_set_type(fallingTetromino.blocks[i], fallingTetromino.type); _m_block_set_type(fallingTetromino.blocks[i], fallingTetromino.type);
_m_blocks_updated_register_block(fallingTetromino.blocks[i]); _m_blocks_updated_register_block(fallingTetromino.blocks[i]);
} }
} }
bool _m_point_intersects_static_block(struct Point point) { bool _m_point_intersects_static_block(struct Point point) {
bool result = false; bool result = false;
struct TetrominoBlock* blockAtPoint = _m_block_at_point(point); struct TetrominoBlock *blockAtPoint = _m_block_at_point(point);
if (blockAtPoint->type != TT_EMPTY) { if (blockAtPoint->type != TT_EMPTY) {
result = true; result = true;
for(int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) { for (int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) {
if (fallingTetromino.blocks[i] == blockAtPoint) { if (fallingTetromino.blocks[i] == blockAtPoint) {
result = false; result = false;
break; break;
} }
}
} }
}
return result; return result;
} }
bool _m_block_can_fall(struct TetrominoBlock block) { bool _m_block_can_fall(struct TetrominoBlock block) {
struct Point newBlockPoint = { .x = block.x, .y = block.y + 1}; struct Point newBlockPoint = {.x = block.x, .y = block.y + 1};
if (newBlockPoint.y >= FIELD_HEIGHT) { if (newBlockPoint.y >= FIELD_HEIGHT) {
return false; return false;
} else if (_m_point_intersects_static_block(newBlockPoint)) { } else if (_m_point_intersects_static_block(newBlockPoint)) {
return false; return false;
} else { } else {
return true; return true;
} }
} }
bool _m_falling_tetromino_can_fall(void) { bool _m_falling_tetromino_can_fall(void) {
bool result = true; bool result = true;
for(int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) { for (int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) {
if (!_m_block_can_fall(*(fallingTetromino.blocks[i]))) { if (!_m_block_can_fall(*(fallingTetromino.blocks[i]))) {
result = false; result = false;
break; break;
}
} }
}
return result; return result;
} }
void _m_falling_tetromino_fall(void) { void _m_falling_tetromino_fall(void) {
if(_m_falling_tetromino_can_fall()) { if (_m_falling_tetromino_can_fall()) {
_m_falling_tetromino_translate(kShiftDownOffset); _m_falling_tetromino_translate(kShiftDownOffset);
} else { } else {
_should_spawn_tetromino = true; _should_spawn_tetromino = true;
} }
} }
bool _m_block_can_move_left(struct TetrominoBlock block) { bool _m_block_can_move_left(struct TetrominoBlock block) {
struct Point newBlockPoint = { .x = block.x - 1, .y = block.y }; struct Point newBlockPoint = {.x = block.x - 1, .y = block.y};
if (newBlockPoint.x < 0) { if (newBlockPoint.x < 0) {
return false; return false;
} else if (_m_point_intersects_static_block(newBlockPoint)) { } else if (_m_point_intersects_static_block(newBlockPoint)) {
return false; return false;
} else { } else {
return true; return true;
} }
} }
bool _m_falling_tetromino_can_move_left(void) { bool _m_falling_tetromino_can_move_left(void) {
bool result = true; bool result = true;
for(int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) { for (int i = 0; i < BLOCKS_WITHIN_A_TETROMINO; i++) {
if (!_m_block_can_move_left(*(fallingTetromino.blocks[i]))) { if (!_m_block_can_move_left(*(fallingTetromino.blocks[i]))) {
result = false; result = false;
break; break;
}
} }
}
return result; return result;
} }
bool _m_block_can_move_right(struct TetrominoBlock block) { bool _m_block_can_move_right(struct TetrominoBlock block) {
struct Point newBlockPoint = { .x = block.x + 1, .y = block.y }; struct Point newBlockPoint = {.x = block.x + 1, .y = block.y};
if (newBlockPoint.x >= FIELD_WIDTH) { if (newBlockPoint.x >= FIELD_WIDTH) {
return false; return false;
} else if (_m_point_intersects_static_block(newBlockPoint)) { } else if (_m_point_intersects_static_block(newBlockPoint)) {
return false; return false;
} else { } else {
return true; return true;
} }
} }

View File

@@ -12,7 +12,7 @@ bool m_tetromino_can_spawn(void); // Bad name + Doesn't follow naming
void m_request_falling_tetromino_rotate(void); void m_request_falling_tetromino_rotate(void);
void m_request_falling_tetromino_move_left(void); void m_request_falling_tetromino_move_left(void);
void m_request_falling_tetromino_move_right(void); void m_request_falling_tetromino_move_right(void);
struct TetrominoBlock* *m_blocks_get_updated(int* lengthOfBlocksUpdated); struct TetrominoBlock **m_blocks_get_updated(int *lengthOfBlocksUpdated);
void m_deactivate(void); void m_deactivate(void);
#endif #endif

View File

@@ -13,57 +13,61 @@ Texture2D _orange_texture;
Texture2D _empty_texture; Texture2D _empty_texture;
void r_initialize(void) { void r_initialize(void) {
_yellow_texture = LoadTexture(RES_PNG_FILE(yellow)); _yellow_texture = LoadTexture(RES_PNG_FILE(yellow));
_blue_texture = LoadTexture(RES_PNG_FILE(blue)); _blue_texture = LoadTexture(RES_PNG_FILE(blue));
_light_blue_texture = LoadTexture(RES_PNG_FILE(light-blue)); _light_blue_texture = LoadTexture(RES_PNG_FILE(light - blue));
_red_texture = LoadTexture(RES_PNG_FILE(red)); _red_texture = LoadTexture(RES_PNG_FILE(red));
_purple_texture = LoadTexture(RES_PNG_FILE(purple)); _purple_texture = LoadTexture(RES_PNG_FILE(purple));
_green_texture = LoadTexture(RES_PNG_FILE(green)); _green_texture = LoadTexture(RES_PNG_FILE(green));
_orange_texture = LoadTexture(RES_PNG_FILE(orange)); _orange_texture = LoadTexture(RES_PNG_FILE(orange));
_empty_texture = LoadTexture(RES_PNG_FILE(black)); _empty_texture = LoadTexture(RES_PNG_FILE(black));
} }
void r_render_blocks(struct TetrominoBlock* *blocks, int length) { void r_render_blocks(struct TetrominoBlock **blocks, int length) {
BeginDrawing(); BeginDrawing();
while(length--) { while (length--) {
_r_render_block(*(*blocks)); _r_render_block(*(*blocks));
blocks++; blocks++;
} }
EndDrawing(); EndDrawing();
} }
void r_render_game_over(int score) { void r_render_game_over(int score) {
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
DrawText("Game Over", SCREEN_WIDTH / 2 - MeasureText("Game Over", 24) / 2, 200, 24, RED); DrawText("Game Over", SCREEN_WIDTH / 2 - MeasureText("Game Over", 24) / 2,
char* score_message = TextFormat("Score: %d", score); 200, 24, RED);
DrawText(score_message, SCREEN_WIDTH / 2 - MeasureText(score_message, 18) / 2, 232, 18, RED); char *score_message = TextFormat("Score: %d", score);
// Draw a play again button DrawText(score_message, SCREEN_WIDTH / 2 - MeasureText(score_message, 18) / 2,
EndDrawing(); 232, 18, RED);
// Draw a play again button
EndDrawing();
} }
void _r_render_block(struct TetrominoBlock blockToRender) { void _r_render_block(struct TetrominoBlock blockToRender) {
Texture2D* textureToRender = _r_get_texture_by_tetromino_type(blockToRender.type); Texture2D *textureToRender =
DrawTexture(*textureToRender, blockToRender.x * TEXTURE_SIZE, blockToRender.y * TEXTURE_SIZE, WHITE); _r_get_texture_by_tetromino_type(blockToRender.type);
DrawTexture(*textureToRender, blockToRender.x * TEXTURE_SIZE,
blockToRender.y * TEXTURE_SIZE, WHITE);
} }
Texture2D* _r_get_texture_by_tetromino_type(enum TetrominoType type) { Texture2D *_r_get_texture_by_tetromino_type(enum TetrominoType type) {
switch (type) { switch (type) {
case TT_O: case TT_O:
return &_yellow_texture; return &_yellow_texture;
case TT_I: case TT_I:
return &_light_blue_texture; return &_light_blue_texture;
case TT_T: case TT_T:
return &_purple_texture; return &_purple_texture;
case TT_J: case TT_J:
return &_blue_texture; return &_blue_texture;
case TT_L: case TT_L:
return &_orange_texture; return &_orange_texture;
case TT_S: case TT_S:
return &_green_texture; return &_green_texture;
case TT_Z: case TT_Z:
return &_red_texture; return &_red_texture;
default: default:
return &_empty_texture; return &_empty_texture;
} }
} }

View File

@@ -8,7 +8,7 @@
#define SCREEN_WIDTH 320 #define SCREEN_WIDTH 320
void r_initialize(void); void r_initialize(void);
void r_render_blocks(struct TetrominoBlock* *blocks, int length); void r_render_blocks(struct TetrominoBlock **blocks, int length);
void r_render_game_over(int score); void r_render_game_over(int score);
void _r_render_block(struct TetrominoBlock blockToRender); void _r_render_block(struct TetrominoBlock blockToRender);

Binary file not shown.

View File

@@ -1,16 +1,7 @@
#ifndef TETRIS_CLONE_TETROMINO_H_ #ifndef TETRIS_CLONE_TETROMINO_H_
#define TETRIS_CLONE_TETROMINO_H_ #define TETRIS_CLONE_TETROMINO_H_
enum TetrominoType { enum TetrominoType { TT_I, TT_J, TT_L, TT_O, TT_S, TT_T, TT_Z, TT_EMPTY };
TT_I,
TT_J,
TT_L,
TT_O,
TT_S,
TT_T,
TT_Z,
TT_EMPTY
};
struct TetrominoBlock { struct TetrominoBlock {
unsigned int x; unsigned int x;