diff --git a/src/blocks.c b/src/blocks.c new file mode 100644 index 0000000..0c2f287 --- /dev/null +++ b/src/blocks.c @@ -0,0 +1,160 @@ +#include "tetris.h" +int generateNextBlock(){ + int seed = rand(); + srand(seed); + int nextBlock = rand() % (7 - 1 + 1); + return nextBlock; +} + +void renderBlockSquare(GameInit *game){ + static SDL_FRect BlockSquare[4]; + int i = 0, gridPosX = 221, gridPosY = 100; + + while(i < 4){ + BlockSquare[i].x = gridPosX; + BlockSquare[i].y = gridPosY; + BlockSquare[i].w = BlockSquare[i].h = squareSize; + + gridPosX = gridPosX + squareSize; + i++; + + if(i == 2){ + gridPosX = 221; + gridPosY = gridPosY + squareSize; + } + + } + + SDL_SetRenderDrawColor(game->renderer, ColourSquare); + SDL_RenderFillRects(game->renderer, BlockSquare, 4); + +} + +void renderBlockRightL(GameInit *game){ + static SDL_FRect BlockRightL[4]; + int i = 0, gridPosX = 221, gridPosY = 100; + + while(i < 4){ + BlockRightL[i].x = gridPosX; + BlockRightL[i].y = gridPosY; + BlockRightL[i].w = BlockRightL[i].h = squareSize; + + gridPosY = gridPosY + squareSize; + i++; + + if(i == 3){ + gridPosY = gridPosY - squareSize; + gridPosX = gridPosX - squareSize; + } + + } + + SDL_SetRenderDrawColor(game->renderer, ColourRightL); + SDL_RenderFillRects(game->renderer, BlockRightL, 4); +} + +void renderBlockLeftL(GameInit *game){ + static SDL_FRect BlockLeftL[4]; + int i = 0, gridPosX = 221, gridPosY = 100; + + while(i < 4){ + BlockLeftL[i].x = gridPosX; + BlockLeftL[i].y = gridPosY; + BlockLeftL[i].w = BlockLeftL[i].h = squareSize; + + gridPosY = gridPosY + squareSize; + i++; + + if(i == 3){ + gridPosY = gridPosY - squareSize; + gridPosX = gridPosX + squareSize; + } + } + SDL_SetRenderDrawColor(game->renderer, ColourLeftL); + SDL_RenderFillRects(game->renderer, BlockLeftL, 4); + +} + +void renderBlockT(GameInit *game){ + static SDL_FRect BlockT[4]; + int i = 0, gridPosX = 221, gridPosY = 130; + + while(i < 4){ + BlockT[i].x = gridPosX; + BlockT[i].y = gridPosY; + BlockT[i].w = BlockT[i].h = squareSize; + + gridPosX = gridPosX + squareSize; + i++; + + if(i == 2){ + gridPosY = gridPosY - squareSize; + gridPosX = gridPosX - squareSize; + } + if(i == 3) + gridPosY = gridPosY + squareSize; + // gridPosX = gridPosX - squareSize; + } + SDL_SetRenderDrawColor(game->renderer, ColourT); + SDL_RenderFillRects(game->renderer, BlockT, 4); +} + +void renderBlockI(GameInit *game){ + static SDL_FRect BlockI[4]; + int i = 0, gridPosX = 221, gridPosY = 100; + + while(i < 4){ + BlockI[i].x = gridPosX; + BlockI[i].y = gridPosY; + BlockI[i].w = BlockI[i].h = squareSize; + + gridPosY = gridPosY + squareSize; + i++; + + } + SDL_SetRenderDrawColor(game->renderer, ColourI); + SDL_RenderFillRects(game->renderer, BlockI, 4); + +} + +void renderBlockLeftZ(GameInit *game){ + static SDL_FRect BlockLeftZ[4]; + int i = 0, gridPosX = 221, gridPosY = 130; + + while(i < 4){ + BlockLeftZ[i].x = gridPosX; + BlockLeftZ[i].y = gridPosY; + BlockLeftZ[i].w = BlockLeftZ[i].h = squareSize; + + gridPosX = gridPosX + squareSize; + i++; + + if(i == 2){ + gridPosX = gridPosX - squareSize; + gridPosY = gridPosY - squareSize; + } + } + SDL_SetRenderDrawColor(game->renderer, ColourLeftZ); + SDL_RenderFillRects(game->renderer, BlockLeftZ, 4); +} + +void renderBlockRightZ(GameInit *game){ + static SDL_FRect BlockRightZ[4]; + int i = 0, gridPosX = 221, gridPosY = 100; + + while(i < 4){ + BlockRightZ[i].x = gridPosX; + BlockRightZ[i].y = gridPosY; + BlockRightZ[i].w = BlockRightZ[i].h = squareSize; + + gridPosX = gridPosX + squareSize; + i++; + + if(i == 2){ + gridPosX = gridPosX - squareSize; + gridPosY = gridPosY + squareSize; + } + } + SDL_SetRenderDrawColor(game->renderer, ColourRightZ); + SDL_RenderFillRects(game->renderer, BlockRightZ, 4); +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..24bbacc --- /dev/null +++ b/src/main.c @@ -0,0 +1,101 @@ +#include "tetris.h" + +// initalize sdl +void sdlGameInit(GameInit *game){ + + // ensure sdl loads flags + if(!SDL_Init(SDL_FLAGS)){ + safeError("error in sdl init\n"); + } + + // create window and check if it exists + game->window = SDL_CreateWindow(WindowTitle, width, height, 0); + if(!game->window){ + safeError("error in creating window\n"); + } + + game->renderer = SDL_CreateRenderer(game->window, NULL); + if(!game->renderer){ + safeError("error in creating renderer\n"); + } + +} + +// clean up and removal +void sdlGameFree(GameInit **gameInit){ + + // check to see if the pointer to gameinit exists + if(!*gameInit){ + safeError("null pointer where value is expected\n"); + } + + // set the pointer for game from gameinit + GameInit *game = *gameInit; + + // kill prosseses + if(game->window){ + SDL_DestroyWindow(game->window); + game->window = NULL; + } + + if(game->renderer){ + SDL_DestroyRenderer(game->renderer); + game->renderer = NULL; + } + + //quit the renderer and clean up pointers + SDL_Quit(); + free(game); + game = NULL; + *gameInit = NULL; +} + +// create game context +void gameCreateContext(GameInit **gameInit){ + + *gameInit = calloc(1, sizeof(GameInit)); + if(*gameInit == NULL){ + safeError("failed to allocate pointer\n"); + } + GameInit *game = *gameInit; + sdlGameInit(game); + + game->isRunning = true; + +} + +// get events from the game +void gameEventCheck(GameInit *game){ + while(SDL_PollEvent(&game->event)){ + switch(game->event.type){ + + case SDL_EVENT_QUIT: + game->isRunning = false; + break; + + default: + break; + } + } +} + +// game loop handle logic and frames +void gameLoop(GameInit *game){ + while(game->isRunning){ + gameDrawContext(game); + gameEventCheck(game); + } +} + + +// entry point and declerations +int main(void){ + GameInit *gamePoint = NULL; + + gameCreateContext(&gamePoint); + gameLoop(gamePoint); + + sdlGameFree(&gamePoint); + + return 0; +} diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..bed0975 --- /dev/null +++ b/src/player.c @@ -0,0 +1,12 @@ +#include "tetris.h" + +//void tetrisPlayerSwitch(GameInit *game){ + + //switch(generateNextBlock()){ + // case 1: + // playerBlock[i].x = BlockSquare[i].x; + // playerBlock[i].y = BlockSquare[i].y; + // playerBlock[i].w = + // playerBlock[i].h = +// } +//} diff --git a/src/render.c b/src/render.c new file mode 100644 index 0000000..091a661 --- /dev/null +++ b/src/render.c @@ -0,0 +1,61 @@ +#include "tetris.h" + +void tetrisGridBoxes(GameInit *game){ + SDL_FRect gridrect[200]; + int i = 0, gridY = 100, gridX = 101, tmpX = 101; + + while(i < 200 /*&& gridX < 501*/) { + // declare the starting values for the grid + gridrect[i].x = gridX; + gridrect[i].y = gridY; + gridrect[i].w = gridrect[i].h = squareSize; + + // grid marcher + gridX = gridX + squareSize; + i++; + + // chage Y on multiples of 10 + if(i %10 == 0){ + gridX = tmpX; + gridY = gridY + squareSize; + } + + } + + SDL_SetRenderDrawColor(game->renderer, 128, 128, 128, SDL_ALPHA_OPAQUE); + SDL_RenderRects(game->renderer, gridrect, 200); + +} + +void playerSquare(GameInit *game){ + //SDL_FRect playerBlock[1]; + //playerBlock[0].w = playerBlock[0].h = squareSize; + //playerBlock[0].x = 221; + //playerBlock[0].y = 100; + + //SDL_SetRenderDrawColor(game->renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); +// SDL_RenderFillRect(game->renderer, &playerBlock[0]); +renderBlockRightZ(game); +} + +// draws everything related to the grid +void tetrisDrawGrid(GameInit *game){ + SDL_FRect backrect[1]; + backrect[0].x = backrect[0].y = 100; + backrect[0].w = 300; + backrect[0].h = 600; + SDL_SetRenderDrawColor(game->renderer, 112, 128, 144, SDL_ALPHA_OPAQUE); + SDL_RenderRect(game->renderer, &backrect[0]); + tetrisGridBoxes(game); + } + + +// the code for drawing shit to the screen +void gameDrawContext(GameInit *game){ + SDL_SetRenderDrawColor(game->renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderClear(game->renderer); + tetrisDrawGrid(game); + playerSquare(game); + SDL_RenderPresent(game->renderer); + SDL_Delay(16); +}