rewrite initiated

This commit is contained in:
Cole
2024-01-12 16:56:44 -06:00
parent d6eea8cc97
commit eac68c789e
49 changed files with 435 additions and 22 deletions

22
src/rewrite/Makefile Normal file
View File

@@ -0,0 +1,22 @@
CC=clang
CFLAGS=-Wall -Wextra -ansi -pedantic -include raylib.h
LIBS=-lraylib -lGL -lm -lpthread -ldl -lrt -lX11
default: tetris-clone
OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)
%.o: %.c $(HEADERS)
$(CC) -c $< -o $@ $(CFLAGS)
.PRECIOUS: $(TARGET) $(OBJECTS)
tetris-clone: $(OBJECTS)
$(CC) -o $@ $(OBJECTS) $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
rm -f *.o
rm -f tetris-clone

25
src/rewrite/block.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef BLOCK_H_
#define BLOCK_H_
#include "point.h"
/* still unsure how i want to name structs and enums */
typedef enum btype_t
{
bt_I,
bt_J,
bt_L,
bt_O,
bt_S,
bt_T,
bt_Z,
bt_Empty
} btype_t;
typedef struct block_t
{
point_t point;
btype_t type;
} block_t;
#endif /* BLOCK_H_ */

50
src/rewrite/m_block.c Normal file
View File

@@ -0,0 +1,50 @@
#include "m_block.h"
#include <stdlib.h>
#define BLOCK_ARRAY_COLUMNS 10
#define BLOCK_ARRAY_ROWS 20
#define BLOCK_ARRAY_LENGTH 200
block_t *_blocks[BLOCK_ARRAY_LENGTH];
block_t *_updated_blocks[BLOCK_ARRAY_LENGTH];
int updated_blocks_current_length = 0;
void M_Create_Blocks(void)
{
int i;
for (i = 0; i < BLOCK_ARRAY_LENGTH; i++)
{
block_t *block = malloc(sizeof(block_t));
block->point.x = i % BLOCK_ARRAY_COLUMNS;
block->point.y = i / BLOCK_ARRAY_COLUMNS;
block->type = bt_Empty;
_blocks[i] = block;
}
}
void M_Update_Blocks(void)
{
/* implement */
}
block_t **M_Get_Blocks(int *length)
{
*length = BLOCK_ARRAY_LENGTH;
return _blocks;
}
block_t **M_Get_Updated_Blocks(int *length)
{
*length = updated_blocks_current_length;
return _updated_blocks;
}
void M_Destroy_Blocks(void)
{
int i;
for (i = 0; i < BLOCK_ARRAY_LENGTH; i++)
{
free(_blocks[i]);
}
}

11
src/rewrite/m_block.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef M_BLOCK_H_
#define M_BLOCK_H_
#include "block.h"
void M_Create_Blocks(void);
void M_Update_Blocks(void);
block_t **M_Get_Blocks(int *length);
block_t **M_Get_Updated_Blocks(int *length);
void M_Destroy_Blocks(void);
#endif /* M_BLOCK_H_ */

View File

@@ -0,0 +1,6 @@
#include "m_tetromino.h"
void M_Update_Tetromino(void)
{
/* implement */
}

View File

@@ -0,0 +1,7 @@
#ifndef M_TETROMINO_H_
#define M_TETROMINO_H_
#include "block.h"
void M_Update_Tetromino(void);
#endif /* M_TETROMINO_H_ */

17
src/rewrite/point.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef POINT_H_
#define POINT_H_
#include "point.h"
typedef struct point_t
{
unsigned int x;
unsigned int y;
} point_t;
typedef struct point_offset_t
{
int xOffset;
int yOffset;
} point_offset_t;
#endif /* POINT_H_ */

67
src/rewrite/r_block.c Normal file
View File

@@ -0,0 +1,67 @@
#include "r_block.h"
#include "sc_def.h"
#define RES_PNG_FILE(name) "resources/" #name ".png"
#define TEXTURE_SIZE 32
Texture2D yellow_texture;
Texture2D blue_texture;
Texture2D light_blue_texture;
Texture2D red_texture;
Texture2D purple_texture;
Texture2D green_texture;
Texture2D orange_texture;
Texture2D empty_texture;
void R_Load_Textures(void)
{
yellow_texture = LoadTexture(RES_PNG_FILE(yellow));
blue_texture = LoadTexture(RES_PNG_FILE(blue));
light_blue_texture = LoadTexture(RES_PNG_FILE(light - blue));
red_texture = LoadTexture(RES_PNG_FILE(red));
purple_texture = LoadTexture(RES_PNG_FILE(purple));
green_texture = LoadTexture(RES_PNG_FILE(green));
orange_texture = LoadTexture(RES_PNG_FILE(orange));
empty_texture = LoadTexture(RES_PNG_FILE(black));
}
void R_Draw_Blocks(block_t **blocks, int block_count)
{
BeginDrawing();
while (block_count--)
{
R_Draw_Block(*(*blocks));
blocks++;
}
/* implement */
EndDrawing();
}
void R_Draw_Block(block_t block)
{
Texture2D *texture = __R_Get_Texture_By_Block_Type(block.type);
DrawTexture(*texture, block.point.x * TEXTURE_SIZE, block.point.y * TEXTURE_SIZE, SC_White);
}
Texture2D *__R_Get_Texture_By_Block_Type(btype_t block_type)
{
switch (block_type)
{
case bt_O:
return &yellow_texture;
case bt_I:
return &light_blue_texture;
case bt_T:
return &purple_texture;
case bt_J:
return &blue_texture;
case bt_L:
return &orange_texture;
case bt_S:
return &green_texture;
case bt_Z:
return &red_texture;
default:
return &empty_texture;
}
}

11
src/rewrite/r_block.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef R_BLOCK_H_
#define R_BLOCK_H_
#include "block.h"
#include "raylib.h"
void R_Load_Textures(void);
void R_Draw_Blocks(block_t **blocks, int block_count);
void R_Draw_Block(block_t block);
Texture2D *__R_Get_Texture_By_Block_Type(btype_t block_type);
#endif /* R_BLOCK_H_ */

0
src/rewrite/r_def.h Normal file
View File

21
src/rewrite/r_ui.c Normal file
View File

@@ -0,0 +1,21 @@
#include "r_ui.h"
#include "raylib.h"
#include "sc_def.h"
void R_Draw_Ui(void)
{
BeginDrawing();
DrawRectangle(SC_GAME_WIDTH, 0, SC_UI_WIDTH, SC_HEIGHT, SC_Gray);
EndDrawing();
}
void R_Draw_Game_Over(int score)
{
const char *score_message = TextFormat("Score: %d", score);
BeginDrawing();
DrawRectangle(0, 0, SC_GAME_WIDTH, SC_HEIGHT, SC_Black);
DrawText("Game Over", SC_GAME_WIDTH / 2 - MeasureText("Game Over", 24) / 2, 200, 24, SC_Red);
DrawText(score_message, SC_GAME_WIDTH / 2 - MeasureText(score_message, 18) / 2, 232, 18, SC_Red);
/* Draw a play again button */
EndDrawing();
}

7
src/rewrite/r_ui.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef R_UI_H_
#define R_UI_H_
void R_Draw_Ui(void);
void R_Draw_Game_Over(int score);
#endif

Binary file not shown.

After

Width:  |  Height:  |  Size: 881 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 897 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 899 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 901 B

15
src/rewrite/sc_def.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef SC_DEF_H_
#define SC_DEF_H_
#define SC_HEIGHT 640
#define SC_WIDTH 440
#define SC_GAME_WIDTH 320
#define SC_UI_WIDTH 140
/* These may fit better in a tc_raylib.h file or something of that nature */
static const Color SC_Black = {0, 0, 0, 255};
static const Color SC_Red = {230, 41, 55, 255};
static const Color SC_White = {255, 255, 255, 255};
static const Color SC_Gray = {130, 130, 130, 255};
#endif /* SC_DEF_H_ */

7
src/rewrite/tc_input.c Normal file
View File

@@ -0,0 +1,7 @@
#include "tc_input.h"
#include "raylib.h"
void TC_Process_Input(void)
{
/* implement */
}

6
src/rewrite/tc_input.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef TC_INPUT_H_
#define TC_INPUT_H_
void TC_Process_Input(void);
#endif /* TC_INPUT_H_ */

80
src/rewrite/tc_main.c Normal file
View File

@@ -0,0 +1,80 @@
#include "m_block.h"
#include "m_tetromino.h"
#include "r_block.h"
#include "r_ui.h"
#include "tc_input.h"
#include "tc_settings.h"
#include "tc_window.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void TC_Start(void);
void TC_Initialize(void);
void TC_Game_Loop(void);
void TC_Stop(void);
int main(int c, char *v[])
{
/* Move somwhere else to parse options */
/* =================================== */
if (c > 1)
{
if (strcmp(v[1], "--speed") == 0)
{
TC_Set_Game_Speed(atoi(v[2]));
}
}
/* =================================== */
TC_Start();
TC_Stop();
}
void TC_Start(void)
{
TC_Initialize();
TC_Game_Loop();
}
void TC_Initialize(void)
{
int n_blocks_created = 0;
block_t **blocks_created = NULL;
TC_Create_Window();
M_Create_Blocks();
R_Load_Textures();
blocks_created = M_Get_Blocks(&n_blocks_created);
R_Draw_Blocks(blocks_created, n_blocks_created);
}
void TC_Game_Loop(void)
{
int tick_rate, n_updated_blocks = 0;
block_t **updated_blocks = NULL;
while (!TC_Close_Window())
{
R_Draw_Ui();
if (tick_rate == 0)
{
TC_Process_Input();
M_Update_Tetromino();
M_Update_Blocks();
M_Get_Updated_Blocks(&n_updated_blocks);
R_Draw_Blocks(updated_blocks, n_updated_blocks);
tick_rate = game_speed_setting;
}
else
{
tick_rate--;
}
}
R_Draw_Game_Over(0);
getchar(); /* putting this here just to stop prog from ending atm */
}
void TC_Stop(void)
{
M_Destroy_Blocks();
TC_Close_Window();
}

16
src/rewrite/tc_settings.c Normal file
View File

@@ -0,0 +1,16 @@
#include "tc_settings.h"
int game_speed_setting = 50;
int target_fps_setting = 60;
struct TC_Setting *tc_settings;
void TC_Set_Game_Speed(int speed)
{
game_speed_setting = speed;
}
void TC_Set_Target_Fps(int fps)
{
target_fps_setting = fps;
}

13
src/rewrite/tc_settings.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef TC_SETTINGS_H_
#define TC_SETTINGS_H_
/* these settings could probably be more robust, probably doesn't matter until i need more*/
/* Maybe have a load defaults, then optionally update? */
void TC_Set_Game_Speed(int speed);
void TC_Set_Target_Fps(int fps);
extern int game_speed_setting;
extern int target_fps_setting;
#endif /* TC_SETTINGS_H_ */

15
src/rewrite/tc_window.c Normal file
View File

@@ -0,0 +1,15 @@
#include "tc_window.h"
#include "raylib.h"
#include "sc_def.h"
#include "tc_settings.h"
void TC_Create_Window(void)
{
InitWindow(SC_WIDTH, SC_HEIGHT, "Tetris Clone");
SetTargetFPS(target_fps_setting);
}
int TC_Close_Window(void)
{
return WindowShouldClose();
}

7
src/rewrite/tc_window.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef TC_WINDOW_H_
#define TC_WINDOW_H_
void TC_Create_Window(void);
int TC_Close_Window(void);
#endif /* TC_WINDOW_H_ */

0
src/rewrite/tetromino.h Normal file
View File