mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 05:55:46 +01:00
make a start on the logic
This commit is contained in:
@@ -3,9 +3,20 @@
|
|||||||
#include "../main.h"
|
#include "../main.h"
|
||||||
|
|
||||||
|
|
||||||
|
static void add_shape(const Shape shape, Uint16* row) {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
Uint8 rowData = (shape >> (4 * i)) & 0x0F;
|
||||||
|
|
||||||
|
if (rowData != 0)
|
||||||
|
row[4 - i] |= rowData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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* keys) {
|
void game_update(GameData* game_data, const Uint8* keys) {
|
||||||
if (keys[SDL_SCANCODE_ESCAPE]) {
|
if (keys[SDL_SCANCODE_ESCAPE]) {
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
add_shape(TETROMINO_I, &game_data->row[4]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,34 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
typedef Uint16 Shape;
|
||||||
|
|
||||||
|
// 0 1 2 3
|
||||||
|
#define TETROMINO_I ((Shape)0b1000100010001000) // 1000 1000 1000 1000 the I tetromino with no rotation
|
||||||
|
#define TETROMINO_I_90 ((Shape)0b0000000000001111) // 0000 0000 0000 1111 the I tetromino with a no rotation
|
||||||
|
#define TETROMINO_O ((Shape)0b1100110000000000) // 1100 1100 0000 0000 the O tetromino with no rotation
|
||||||
|
#define TETROMINO_T ((Shape)0b1110010000000000) // 1110 0100 0000 0000 the T tetromino with no rotation
|
||||||
|
#define TETROMINO_T_90 ((Shape)0b1100110010000000) // 1100 1100 1000 0000 the T tetromino with a no rotation
|
||||||
|
#define TETROMINO_T_180 ((Shape)0b0100111000000000) // 0100 1110 0000 0000 the T tetromino with a 180° rotation
|
||||||
|
#define TETROMINO_T_270 ((Shape)0b0010011000100000) // 0010 0110 0010 0000 the T tetromino with a 270° rotation
|
||||||
|
#define TETROMINO_L ((Shape)0b1000100011000000) // 1000 1000 1100 0000 the L tetromino with no rotation
|
||||||
|
#define TETROMINO_L_90 ((Shape)0b1110100000000000) // 1110 1000 0000 0000 the L tetromino with a no rotation
|
||||||
|
#define TETROMINO_L_180 ((Shape)0b1100010001000000) // 1100 0100 0100 0000 the L tetromino with a 180° rotation
|
||||||
|
#define TETROMINO_L_270 ((Shape)0b0000001011100000) // 0000 0010 1110 0000 the L tetromino with a 270° rotation
|
||||||
|
#define TETROMINO_J ((Shape)0b0100010011000000) // 0100 0100 1100 0000 the J tetromino with no rotation
|
||||||
|
#define TETROMINO_J_90 ((Shape)0b1000111000000000) // 1000 1110 0000 0000 the J tetromino with a no rotation
|
||||||
|
#define TETROMINO_J_180 ((Shape)0b1100100010000000) // 1100 1000 1000 0000 the J tetromino with a 180° rotation
|
||||||
|
#define TETROMINO_J_270 ((Shape)0b1110001000000000) // 1110 0010 0000 0000 the J tetromino with a 270° rotation
|
||||||
|
#define TETROMINO_S ((Shape)0b0110110000000000) // 0110 1100 0000 0000 the S tetromino with no rotation
|
||||||
|
#define TETROMINO_S_90 ((Shape)0b1000110001000000) // 1000 1100 0100 0000 the S tetromino with a no rotation
|
||||||
|
#define TETROMINO_Z ((Shape)0b1100011000000000) // 1100 0110 0000 0000 the Z tetromino with no rotation
|
||||||
|
#define TETROMINO_Z_90 ((Shape)0b0100110010000000) // 0100 1100 1000 0000 the Z tetromino with a no rotation
|
||||||
|
|
||||||
// stores the data used in the game
|
// stores the data used in the game
|
||||||
|
#define ROWS 32
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
Uint16 row[ROWS];
|
||||||
} GameData;
|
} GameData;
|
||||||
|
|
||||||
// updates the game's state
|
// updates the game's state
|
||||||
void game_update(GameData game_data, const Uint8* keys);
|
void game_update(GameData* game_data, const Uint8* keys);
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ bool playing = true;
|
|||||||
|
|
||||||
SDL_Window* window = NULL;
|
SDL_Window* window = NULL;
|
||||||
SDL_Renderer* renderer = NULL;
|
SDL_Renderer* renderer = NULL;
|
||||||
|
GameData game_data = {};
|
||||||
|
|
||||||
|
|
||||||
// handles game application initialisation
|
// handles game application initialisation
|
||||||
static void init(void) {
|
static void init(void) {
|
||||||
@@ -36,7 +38,7 @@ static void init(void) {
|
|||||||
|
|
||||||
// initialize audio
|
// initialize audio
|
||||||
AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
||||||
//AudioData audio1 = audio_load_wav(audio_device, "FILE MANE");
|
// AudioData audio1 = audio_load_wav(audio_device, "FILE MANE");
|
||||||
}
|
}
|
||||||
|
|
||||||
// handles game application updating
|
// handles game application updating
|
||||||
@@ -54,9 +56,9 @@ static void update(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// preform updates
|
// preform updates
|
||||||
game_update((GameData){}, SDL_GetKeyboardState(NULL));
|
game_update(&game_data, SDL_GetKeyboardState(NULL));
|
||||||
renderer_update(&(RenderData){
|
renderer_update(&(RenderData){
|
||||||
window, renderer});
|
window, renderer, &game_data});
|
||||||
}
|
}
|
||||||
|
|
||||||
// handles game application quitting
|
// handles game application quitting
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
|
int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
|
||||||
// create a new window
|
// create a new window
|
||||||
*window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 100, 100, SDL_WINDOW_SHOWN);
|
*window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||||
if (*window == NULL) {
|
if (*window == NULL) {
|
||||||
error(ERROR_SDL_RENDERER_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError());
|
error(ERROR_SDL_RENDERER_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError());
|
||||||
return -1;
|
return -1;
|
||||||
@@ -28,6 +28,7 @@ int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
|
|||||||
|
|
||||||
void renderer_update(const RenderData* render_data) {
|
void renderer_update(const RenderData* render_data) {
|
||||||
SDL_Renderer* renderer = render_data->renderer;
|
SDL_Renderer* renderer = render_data->renderer;
|
||||||
|
GameData* data = render_data->game_data;
|
||||||
|
|
||||||
int success = 0; // if an error occurs, this value is <0
|
int success = 0; // if an error occurs, this value is <0
|
||||||
|
|
||||||
@@ -35,6 +36,17 @@ void renderer_update(const RenderData* render_data) {
|
|||||||
success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x50);
|
success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x50);
|
||||||
success |= SDL_RenderClear(renderer);
|
success |= SDL_RenderClear(renderer);
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||||
|
for (int y = 0; y < ROWS; y++) {
|
||||||
|
Uint16 row = data->row[y];
|
||||||
|
|
||||||
|
for (int x = 16; x >= 0; x--) {
|
||||||
|
if ((row & (1 << x)) != 0) {
|
||||||
|
success |= SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1, y * BLOCK_HEIGHT + 1, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (success < 0) {
|
if (success < 0) {
|
||||||
warn("\033[93mW\033[0m: something went wrong whilst renderering! SDL Error: %s\n", SDL_GetError());
|
warn("\033[93mW\033[0m: something went wrong whilst renderering! SDL Error: %s\n", SDL_GetError());
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,9 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include "../game/game.h"
|
||||||
|
|
||||||
|
#define SCREEN_WIDTH 256
|
||||||
|
#define SCREEN_HEIGHT (SCREEN_WIDTH * 2)
|
||||||
|
#define BLOCK_WIDTH (SCREEN_WIDTH / 16)
|
||||||
|
#define BLOCK_HEIGHT (SCREEN_HEIGHT / (BLOCK_WIDTH * 2))
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
SDL_Renderer* renderer;
|
SDL_Renderer* renderer;
|
||||||
|
GameData* game_data;
|
||||||
} RenderData;
|
} RenderData;
|
||||||
|
|
||||||
int renderer_init(SDL_Window** window, SDL_Renderer** renderer);
|
int renderer_init(SDL_Window** window, SDL_Renderer** renderer);
|
||||||
|
|||||||
Reference in New Issue
Block a user