From 58bef85206c7cce153400e51b247e2476e9e65a4 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 12 Jan 2016 20:41:56 +1300 Subject: Initial commit of rough-arse Tetris --- plot.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 plot.c (limited to 'plot.c') diff --git a/plot.c b/plot.c new file mode 100644 index 0000000..6a579d6 --- /dev/null +++ b/plot.c @@ -0,0 +1,103 @@ +#include + +#include "colour.h" +#include "plot.h" + +static SDL_Window *window = NULL; +static SDL_Renderer *renderer = NULL; + +void plot_update() +{ + SDL_RenderPresent(renderer); +} + +void plot_rect(unsigned int x, unsigned int y, unsigned int width, unsigned int height, struct colour *c) +{ + SDL_Rect rect; + + SDL_SetRenderDrawColor(renderer, c->r, c->g, c->b, 255); + + rect.x = x; + rect.y = y; + rect.w = width; + rect.h = height; + + SDL_RenderFillRect(renderer, &rect); +} + +void plot_cell(unsigned int x, unsigned int y, struct colour *c) +{ + plot_rect( + x*CELL_SIZE+(x-1)*BORDER_THICKNESS, + y*CELL_SIZE+(y-1)*BORDER_THICKNESS, + CELL_SIZE, + CELL_SIZE, + c + ); +} + +void plot_cell_borders() +{ + int i, j; + + struct colour col = {.r = 0, .g = 0, .b = 0}; + + for (i = 1; i < WIDTH_CELLS; i++) + { + j = i*CELL_SIZE + (i-1)*BORDER_THICKNESS; + plot_rect( + j, 0, + BORDER_THICKNESS, HEIGHT_PIXELS, + &col); + } + + for (i = 1; i < HEIGHT_CELLS; i++) + { + j = i*CELL_SIZE + (i-1)*BORDER_THICKNESS; + plot_rect( + 0, j, + WIDTH_PIXELS, BORDER_THICKNESS, + &col); + } + +} + + +int plot_init() +{ + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + fprintf(stderr, "Failed to init SDL video: %s\n", SDL_GetError()); + return 1; + } + + window = SDL_CreateWindow( + "Some title", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + WIDTH_PIXELS, + HEIGHT_PIXELS, + SDL_WINDOW_SHOWN + ); + + if (window == NULL) + { + fprintf(stderr, "Failed to create window: %s\n", SDL_GetError()); + return 1; + } + + renderer = SDL_CreateRenderer(window, 0, 0); + if (renderer == NULL) + { + fprintf(stderr, "Failed to get window renderer: %s\n", SDL_GetError()); + return 1; + } + + /* blank out the background with gray */ + SDL_SetRenderDrawColor(renderer, 0x33, 0x33, 0x33, 0xFF); + SDL_RenderFillRect(renderer, NULL); + + plot_cell_borders(); + + return 0; +} -- cgit v1.1