mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
split up the code a bit more
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#include "../main.h"
|
#include "../main.h"
|
||||||
#include "../window/colour.h"
|
#include "../window/colour.h"
|
||||||
#include "./tetromino/shapes.h"
|
#include "./tetromino/shapes.h"
|
||||||
|
#include "tetromino/placing.h"
|
||||||
|
|
||||||
|
|
||||||
static void clear_row(Row* row, uint8_t y) {
|
static void clear_row(Row* row, uint8_t y) {
|
||||||
@@ -39,38 +40,12 @@ static void clear_rows(Row* row) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sets a shape to the screen
|
|
||||||
static void set_shape_i(Row* row, const Shape shape, const Colour colour, const uint8_t pos_x) {
|
|
||||||
for (uint8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
|
||||||
ShapeRow shape_row = shape_get_row(shape, y);
|
|
||||||
|
|
||||||
if (shape_row == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
for (uint8_t x = 0; x < SHAPE_WIDTH; x++)
|
|
||||||
if (is_set(shape_row, x))
|
|
||||||
row[y].columns[x + pos_x] = colour;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void set_shape(Row* row, const Shape shape, const Colour colour, const uint8_t pos_x, const uint8_t pos_y) {
|
|
||||||
set_shape_i(&row[pos_y], shape, colour, pos_x); // calls itself, but omitting the pos_y argument, instead opting for specifying the row
|
|
||||||
}
|
|
||||||
|
|
||||||
// called every time the game's state is updated
|
// called every time the game's state is updated
|
||||||
void game_update(GameData* game_data, const uint8_t* keys) {
|
void game_update(GameData* game_data, const uint8_t* keys) {
|
||||||
if (keys[SDL_SCANCODE_ESCAPE]) {
|
if (keys[SDL_SCANCODE_ESCAPE]) {
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
set_shape(game_data->row, TETROMINO_I, COLOUR_CYAN, 1, 0);
|
//tmp_set_random(game_data);
|
||||||
set_shape(game_data->row, TETROMINO_O, COLOUR_YELLOW, 5, 4);
|
tmp_set_all(game_data);
|
||||||
|
|
||||||
set_shape(game_data->row, TETROMINO_Z, COLOUR_GREEN, 1, 8);
|
|
||||||
set_shape(game_data->row, TETROMINO_S, COLOUR_RED, 5, 8);
|
|
||||||
|
|
||||||
set_shape(game_data->row, TETROMINO_J, COLOUR_BLUE, 1, 12);
|
|
||||||
set_shape(game_data->row, TETROMINO_L, COLOUR_ORANGE, 5, 12);
|
|
||||||
|
|
||||||
set_shape(game_data->row, TETROMINO_T, COLOUR_MAGENTA, 5, 16);
|
|
||||||
}
|
}
|
||||||
|
|||||||
36
src/game/tetromino/placing.c
Normal file
36
src/game/tetromino/placing.c
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "../../window/colour.h"
|
||||||
|
#include "../game.h"
|
||||||
|
#include "shapes.h"
|
||||||
|
|
||||||
|
// sets a shape to the screen
|
||||||
|
static void set_shape_i(Row* row, const Shape shape, const Colour colour, const uint8_t pos_x) {
|
||||||
|
for (uint8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
||||||
|
ShapeRow shape_row = shape_get_row(shape, y);
|
||||||
|
|
||||||
|
if (shape_row == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (uint8_t x = 0; x < SHAPE_WIDTH; x++)
|
||||||
|
if (is_set(shape_row, x))
|
||||||
|
row[y].columns[x + pos_x] = colour;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void set_shape(Row* row, const Shape shape, const Colour colour, const uint8_t pos_x, const uint8_t pos_y) {
|
||||||
|
set_shape_i(&row[pos_y], shape, colour, pos_x); // calls itself, but omitting the pos_y argument, instead opting for specifying the row
|
||||||
|
}
|
||||||
|
|
||||||
|
void tmp_set_all(GameData* game_data) {
|
||||||
|
set_shape(game_data->row, TETROMINO_I, COLOUR_CYAN, 1, 0);
|
||||||
|
set_shape(game_data->row, TETROMINO_O, COLOUR_YELLOW, 5, 4);
|
||||||
|
|
||||||
|
set_shape(game_data->row, TETROMINO_Z, COLOUR_GREEN, 1, 8);
|
||||||
|
set_shape(game_data->row, TETROMINO_S, COLOUR_RED, 5, 8);
|
||||||
|
|
||||||
|
set_shape(game_data->row, TETROMINO_J, COLOUR_BLUE, 1, 12);
|
||||||
|
set_shape(game_data->row, TETROMINO_L, COLOUR_ORANGE, 5, 12);
|
||||||
|
|
||||||
|
set_shape(game_data->row, TETROMINO_T, COLOUR_MAGENTA, 5, 16);
|
||||||
|
}
|
||||||
7
src/game/tetromino/placing.h
Normal file
7
src/game/tetromino/placing.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../game.h"
|
||||||
|
|
||||||
|
|
||||||
|
void tmp_set_random(GameData* game_data);
|
||||||
|
void tmp_set_all(GameData* game_data);
|
||||||
Reference in New Issue
Block a user