aboutsummaryrefslogtreecommitdiff
path: root/plot.c
blob: c13b314c6dd25d302d2773c8f87cce4cfda89f38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <SDL.h>
#include <SDL_ttf.h>

#include "colour.h"
#include "plot.h"

static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;

void
plot_update(void)
{
	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_text(const char *message, TTF_Font *font, SDL_Color color, int x, int y)
{
	SDL_Texture *texture = NULL;
	SDL_Rect dst;
	dst.x = x;
	dst.y = y;
	SDL_Surface *surf = TTF_RenderUTF8_Blended(font, message, color);
	if (!surf) {
		printf("Error in TTF_RenderUTF8_Blended\n");
		return;
	}
	texture = SDL_CreateTextureFromSurface(renderer, surf);
	if (!texture){
		printf("Error in SDL_CreateTextureFromSurface\n");
	}
	SDL_FreeSurface(surf);

	SDL_QueryTexture(texture, NULL, NULL, &dst.w, &dst.h);
	SDL_RenderCopy(renderer, texture, NULL, &dst);

	SDL_DestroyTexture(texture);
}

void
plot_clear(void)
{
	/* blank out the background with black */
	SDL_SetRenderDrawColor(renderer, 0x28, 0x28, 0x28, 0xFF);
	SDL_RenderFillRect(renderer, NULL);
}

int
plot_init(void)
{
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		fprintf(stderr, "Failed to init SDL video: %s\n", SDL_GetError());
		return 1;
	}

	window = SDL_CreateWindow(
		"Tetris Clone",
		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;
	}

	plot_clear();

	return 0;
}