summaryrefslogtreecommitdiff
path: root/src/surface.c
blob: cbc6c948006d0697efdd0b3a33a2d4ecd9db380f (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
#include "gl.h"
#include "surface.h"

#undef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))

void surface_initialise(
		struct surface *surface,
		struct wl_display *wl_display,
		struct image *texture)
{
	egl_create_window(
			&surface->egl,
			surface->wl_surface,
			surface->width,
			surface->height);
	egl_create_context(&surface->egl, wl_display);
	gl_initialise(&surface->gl, texture);
}

void surface_destroy(struct surface *surface)
{
	egl_make_current(&surface->egl);
	gl_destroy(&surface->gl);
	egl_destroy(&surface->egl);
}

void surface_draw(
		struct surface *surface,
		struct color *color,
		struct image *texture)
{
	wl_egl_window_resize(
			surface->egl.window,
			surface->width,
			surface->height,
			0,
			0);
	egl_make_current(&surface->egl);
	gl_clear(&surface->gl, color);

	if (texture != NULL && texture->buffer != NULL) {
		double scale = MAX(
				(double)surface->width / texture->width,
				(double)surface->height / texture->height
				);
		uint32_t width = (uint32_t)(scale * texture->width);
		uint32_t height = (uint32_t)(scale * texture->height);
		int32_t x = -((int32_t)width - (int32_t)surface->width) / 2;
		int32_t y = -((int32_t)height - (int32_t)surface->height) / 2;

		gl_draw_texture(&surface->gl, texture, x, y, width, height);
	}

	wl_surface_damage_buffer(surface->wl_surface, 0, 0, INT32_MAX, INT32_MAX);

	egl_swap_buffers(&surface->egl);
}