diff --git a/src/render.c b/src/render.c index 8bea39a..37107d5 100644 --- a/src/render.c +++ b/src/render.c @@ -12,13 +12,12 @@ extern int spr_x[5][6]; extern int grid[2][18][8]; extern SDL_Renderer *sr; extern SDL_Texture * texture; -int tile_c; extern int tile_pos; extern bool next_p; // next piece flag int tile_shp, tilesrc_x, tilesrc_y, tiledst_x, tiledst_y; int row = 0, col = 0; - +int tile_c; int last_time, current_time, deltatime, score; ///////////////// SPRITE SHEET TOP LEFT COORDINATES ///////////////// @@ -42,6 +41,7 @@ SDL_Rect level_back = {231, 7, 76, 66}; SDL_Rect level_trim = {230, 6, 78, 68}; SDL_Rect next_back = {257, 86, 18, 37}; SDL_Rect next_trim = {256, 85, 20, 39}; +SDL_Rect next_tile = {260, 88, 12, 11}; SDL_Rect high_back = {13, 7, 76, 186}; SDL_Rect high_trim = {12, 6, 78, 188}; SDL_Rect tile_src = {72, 0, 12, 11}; @@ -87,17 +87,25 @@ return 0; ///////////////// DISPLAY NEXT PIECE ///////////////// int Ren_next(void) { + tile_shp = 1; // tile shape if (next_p == 0) { } else { - SDL_SetRenderDrawColor(sr, 0xff , 0xff , 0xff, 0xff); + SDL_SetRenderDrawColor(sr, 0x00 , 0x00 , 0x00, 0xff); SDL_RenderFillRect(sr, &next_back); // background - SDL_SetRenderDrawColor (sr, 0x00, 0x00, 0x00, 255); - SDL_RenderDrawRect(sr, &next_trim); // trim SDL_RenderDrawLine (sr, 259, 124, 276, 124); // next shadow SDL_RenderDrawLine (sr, 259, 125, 277, 125); SDL_RenderDrawLine (sr, 276, 124, 276, 89); SDL_RenderDrawLine (sr, 277, 125, 277, 89); + SDL_SetRenderDrawColor (sr, 0xff, 0xff, 0xff, 0xff); + SDL_RenderDrawRect(sr, &next_trim); // trim + for (int i = 0; i < 3; i++) { + tile_src.x = spr_x[tile_shp][tile_next[i] - 1]; // source x position in sprite map depends on tile shape and colour + tile_src.y = 0; // source y position in sprite map = 0 + tile_dst.x = 260; // tile destination x position = 260 + tile_dst.y = 88 + (i * 11); // add 11 pixels to move to next tile destination y location + SDL_RenderCopy(sr, texture, &tile_src, &tile_dst); } + } return 0; } @@ -120,7 +128,7 @@ int Ren_high(void) { ///////////////// DISPLAY LOGO ///////////////// int Ren_logo(void) { SDL_RenderCopy(sr, texture, &logo_src, &logo_dst); - SDL_SetRenderDrawColor (sr, 0x0, 0x0, 0x0, 255); // shadow + SDL_SetRenderDrawColor (sr, 0x0, 0x0, 0x0, 255); // shadow SDL_RenderDrawLine (sr, 234, 190, 308, 190); SDL_RenderDrawLine (sr, 234, 191, 309, 191); SDL_RenderDrawLine (sr, 308, 190, 308, 140); @@ -157,7 +165,7 @@ int Ren_info(void) { SDL_RenderFillRect(sr, &main_back); // background SDL_SetRenderDrawColor (sr, 0xff, 0xff, 0xff, 255); SDL_RenderDrawRect(sr, &main_trim); // trim - for (int z = 0; z < 11; z++) { // 11 lines of text + for (int z = 0; z < 11; z++) { // 11 lines of text Ren_line(info, infol[z][0], infol[z][1], z * 11, 11, 0); } SDL_RenderPresent (sr); @@ -168,7 +176,7 @@ int Ren_info(void) { int Ren_restart(void) { SDL_SetRenderDrawColor(sr, 0xff , 0xff , 0xff, 0xff); SDL_RenderFillRect(sr, &gameo_back); // background - for (int z = 0; z < 1; z++) { // 1 line of text + for (int z = 0; z < 1; z++) { // 1 line of text Ren_line(gameo, gameol[z][0], gameol[z][1], z * 9, 9, 1); } SDL_RenderPresent (sr); @@ -179,7 +187,7 @@ int Ren_restart(void) { int Ren_pause(void) { SDL_SetRenderDrawColor(sr, 0xff , 0xff , 0xff, 0xff); SDL_RenderFillRect(sr, &gameo_back); // background - for (int z = 0; z < 1; z++) { // 1 line of text + for (int z = 0; z < 1; z++) { // 1 line of text Ren_line(pause, pausel[z][0], pausel[z][1], z * 9, 9, 1); } SDL_RenderPresent (sr); @@ -189,15 +197,15 @@ int Ren_pause(void) { ///////////////// DISPLAY MAIN MENU ///////////////// int Ren_menu(void) { backdrop(colour); - Ren_logo(); // logo - Ren_level(); // render current level/score - Ren_next(); // render next brick/block/piece - Ren_high(); // render high scores + Ren_logo(); // logo + Ren_level(); // render current level/score + Ren_next(); // render next brick/block/piece + Ren_high(); // render high scores SDL_SetRenderDrawColor(sr, 0x0 , 0x0 , 0x0, 0xff); SDL_RenderFillRect(sr, &main_back); // background SDL_SetRenderDrawColor (sr, 0xff, 0xff, 0xff, 255); SDL_RenderDrawRect(sr, &main_trim); // trim - for (int z = 0; z < 10; z++) { // 10 lines of text + for (int z = 0; z < 10; z++) { // 10 lines of text Ren_line(m_menu, m_menul[z][0], m_menul[z][1], z * 11, 11, 0); } SDL_RenderPresent (sr); @@ -205,75 +213,65 @@ int Ren_menu(void) { } ///////////////// DISPLAY GAME BOARD///////////////// -int display_grid() -{ - tiledst_y = 1; +int display_grid() { for (int i = 0; i < 18; i++) { - tiledst_x = 112; for (int j = 0; j < 8; j++) { int k = grid[0][i][j]; - tile_shp = 1; // tile shape + tile_shp = 1; // tile shape tilesrc_y = 0; if (k > 0) { k--; tilesrc_x = spr_x[tile_shp][k]; tile_src.x = tilesrc_x; tile_src.y = tilesrc_y; - tile_dst.x = tiledst_x; - tile_dst.y = tiledst_y; - SDL_RenderCopy(sr, texture, &tile_src, &tile_dst); // draw colour tile + tile_dst.x = tiledst_x + 112 + (j * 12); + tile_dst.y = tiledst_y + 1 + (i * 11); + SDL_RenderCopy(sr, texture, &tile_src, &tile_dst); // draw colour tile } else { - tile_dst.x = tiledst_x; - tile_dst.y = tiledst_y; + tile_dst.x = tiledst_x + 112 + (j * 12); + tile_dst.y = tiledst_y + 1 + (i * 11); SDL_SetRenderDrawColor(sr, 0x0 , 0x0 , 0x0, 0xff); - SDL_RenderFillRect(sr, &tile_dst); // draw empty tile + SDL_RenderFillRect(sr, &tile_dst); // draw empty tile } - tiledst_x = tiledst_x + 12; // next column } - tiledst_y = tiledst_y + 11; // next row } return 0; } - ///////////////// UPDATE GAME ///////////////// int Ren_game(void) { -if (pause_f == true) { - Ren_pause(); - SDL_Delay(40); - return 0; -} + if (pause_f == true) { + Ren_pause(); + return 0; + } backdrop(colour); - Ren_logo(); // logo - Ren_level(); // render current level/score - Ren_next(); // render next brick/block/piece - Ren_high(); // render high scores + Ren_logo(); // logo + Ren_level(); // render current level/score + Ren_next(); // render next brick/block/piece + Ren_high(); // render high scores SDL_SetRenderDrawColor(sr, 0x0 , 0x0 , 0x0, 0xff); SDL_RenderFillRect(sr, &main_back); // background SDL_SetRenderDrawColor (sr, 0xff, 0xff, 0xff, 255); SDL_RenderDrawRect(sr, &main_trim); // trim display_grid(); if(row > 15) { - grid[0][15][col] = 0; - grid[0][16][col] = 0; - grid[0][17][col] = 0; - row = 0; + grid[0][15][col] = 0; + grid[0][16][col] = 0; + grid[0][17][col] = 0; + row = 0; } for (int i = 0; i < 1; i++) { - - current_time = SDL_GetTicks(); - deltatime = current_time - last_time; - - if (deltatime > 1000) { // deltatime > 500 - (score * 10) - - last_time = current_time; - deltatime = 0; - disp_column (row, col); - row++; - grid[0][row - 2][col] = 0; + current_time = SDL_GetTicks(); + deltatime = current_time - last_time; + if (deltatime > 500) { // deltatime > 500 - (score * 10) + last_time = current_time; + deltatime = 0; + disp_column (row, col); + row++; + grid[0][row - 2][col] = 0; } - SDL_RenderPresent (sr); - return 0; -} + SDL_RenderPresent (sr); + return 0; + } }