run formatter
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1 @@
|
|||||||
./src/tetris-clone
|
tetris-clone
|
||||||
12
src/app.c
12
src/app.c
@@ -1,10 +1,10 @@
|
|||||||
#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
|
||||||
|
|
||||||
@@ -29,7 +29,8 @@ void _a_initialize_raylib(void) {
|
|||||||
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 =
|
||||||
|
m_blocks_get_updated(&number_blocks_updated);
|
||||||
r_render_blocks(blocks_updated, number_blocks_updated);
|
r_render_blocks(blocks_updated, number_blocks_updated);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,13 +38,16 @@ 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()) {
|
||||||
|
if (WindowShouldClose())
|
||||||
|
return;
|
||||||
_a_input_process();
|
_a_input_process();
|
||||||
if (tick_rate == 0) {
|
if (tick_rate == 0) {
|
||||||
m_update();
|
m_update();
|
||||||
tick_rate = 50;
|
tick_rate = 50;
|
||||||
}
|
}
|
||||||
int number_blocks_updated = 0;
|
int number_blocks_updated = 0;
|
||||||
struct TetrominoBlock** blocks_updated = GetUpdatedBlocks(&number_blocks_updated);
|
struct TetrominoBlock **blocks_updated =
|
||||||
|
m_blocks_get_updated(&number_blocks_updated);
|
||||||
r_render_blocks(blocks_updated, number_blocks_updated);
|
r_render_blocks(blocks_updated, number_blocks_updated);
|
||||||
tick_rate--;
|
tick_rate--;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,10 +25,32 @@ struct Tetromino {
|
|||||||
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
|
||||||
|
// X,Y
|
||||||
struct TetrominoBlock *_blocks[FIELD_HEIGHT * FIELD_WIDTH];
|
struct TetrominoBlock *_blocks[FIELD_HEIGHT * FIELD_WIDTH];
|
||||||
struct TetrominoBlock *_blocks_updated[FIELD_HEIGHT * FIELD_WIDTH] = {NULL};
|
struct TetrominoBlock *_blocks_updated[FIELD_HEIGHT * FIELD_WIDTH] = {NULL};
|
||||||
int _blocks_updated_length = 0;
|
int _blocks_updated_length = 0;
|
||||||
@@ -84,9 +106,7 @@ void m_blocks_set_empty(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
@@ -124,12 +144,10 @@ void _m_blocks_alloc(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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];
|
||||||
|
|
||||||
@@ -227,7 +245,8 @@ enum TetrominoType _m_tetromino_type_random(void) {
|
|||||||
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,
|
||||||
|
struct Offset *offsets) {
|
||||||
fallingTetromino.type = type;
|
fallingTetromino.type = type;
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (index < BLOCKS_WITHIN_A_TETROMINO) {
|
while (index < BLOCKS_WITHIN_A_TETROMINO) {
|
||||||
@@ -242,7 +261,8 @@ void _m_blocks_spawn_at_offset_from_spawn_point(enum TetrominoType type, struct
|
|||||||
}
|
}
|
||||||
_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
|
||||||
|
// spawnedBlocks to be handled in Parent function
|
||||||
fallingTetromino.blocks[index] = spawnBlock;
|
fallingTetromino.blocks[index] = spawnBlock;
|
||||||
|
|
||||||
index++;
|
index++;
|
||||||
@@ -254,7 +274,8 @@ 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,
|
||||||
|
enum TetrominoType type) {
|
||||||
pTetrominoBlock->type = type;
|
pTetrominoBlock->type = type;
|
||||||
_m_blocks_updated_register_block(pTetrominoBlock);
|
_m_blocks_updated_register_block(pTetrominoBlock);
|
||||||
}
|
}
|
||||||
@@ -271,7 +292,6 @@ void _m_falling_tetromino_rotate(void) {
|
|||||||
// 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
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,7 +314,9 @@ 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++) {
|
||||||
struct Point updatedPoint = { .x = fallingTetromino.blocks[i]->x + offset.xOffset, .y = fallingTetromino.blocks[i]->y + offset.yOffset};
|
struct Point updatedPoint = {
|
||||||
|
.x = fallingTetromino.blocks[i]->x + offset.xOffset,
|
||||||
|
.y = fallingTetromino.blocks[i]->y + offset.yOffset};
|
||||||
fallingTetromino.blocks[i] = _m_block_at_point(updatedPoint);
|
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);
|
||||||
|
|||||||
@@ -35,16 +35,20 @@ void r_render_blocks(struct TetrominoBlock* *blocks, int length) {
|
|||||||
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,
|
||||||
|
200, 24, RED);
|
||||||
char *score_message = TextFormat("Score: %d", score);
|
char *score_message = TextFormat("Score: %d", score);
|
||||||
DrawText(score_message, SCREEN_WIDTH / 2 - MeasureText(score_message, 18) / 2, 232, 18, RED);
|
DrawText(score_message, SCREEN_WIDTH / 2 - MeasureText(score_message, 18) / 2,
|
||||||
|
232, 18, RED);
|
||||||
// Draw a play again button
|
// Draw a play again button
|
||||||
EndDrawing();
|
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) {
|
||||||
|
|||||||
BIN
src/tetris-clone
BIN
src/tetris-clone
Binary file not shown.
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user