aboutsummaryrefslogtreecommitdiff
path: root/sdl2/render.ha
blob: 19c3940a6e69097e32403b38cf8da9d20d49fc72 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// TODO: Flesh me out

// A structure representing rendering state. (Opaque)
export type renderer = void;

// An efficient driver-specific representation of pixel data. (Opaque)
export type texture = void;

// Flags used when creating a rendering context.
export type renderer_flags = enum u32 {
	NONE = 0,
	SOFTWARE = 0x00000001,
	ACCELERATED = 0x00000002,
	PRESENTVSYNC = 0x00000004,
	TARGETTEXTURE = 0x00000008,
};

@symbol("SDL_CreateWindowAndRenderer") fn _create_window_and_renderer(
	width: int, height: int, window_flags: window_flags,
	window: nullable **window, renderer: nullable **renderer) int;

// Create a window and default renderer.
//
// 'width' and 'height' set the width and height of the window, in screen
// coordinates. 'window_flags' configure additional window parameters.
//
// 'window' and 'renderer' are out parameters, or null, which are filled in with
// the created window and renderer respectively.
export fn create_window_and_renderer(
	width: int,
	height: int,
	window_flags: window_flags,
	window: nullable **window,
	renderer: nullable **renderer,
) (void | error) = wrapvoid(_create_window_and_renderer(width, height,
	window_flags, window, renderer));

@symbol("SDL_CreateRenderer") fn _create_renderer(window: *window,
	index: int, flags: renderer_flags) nullable *renderer;

// Create a 2D rendering context for a window.
// 
// 'window' is the window where rendering is displayed. 'index' is the index of
// the rendering driver to initialize, or -1 to initialize the first one
// supporting the requested flags.
// 
// See also: [[create_software_renderer]], [[get_renderer_info]],
// [[destroy_renderer]].
export fn create_renderer(
	window: *window,
	index: int,
	flags: renderer_flags,
) (*renderer | error) =
	wrapptr(_create_renderer(window, index, flags))?: *renderer;

// Destroy the rendering context for a window and free associated textures.
//
// See also: [[create_renderer]].
export @symbol("SDL_DestroyRenderer") fn destroy_renderer(renderer: *renderer) void;

// Opaque value for the alpha channel (255).
export def ALPHA_OPAQUE: u8 = 255;

@symbol("SDL_SetRenderDrawColor") fn _set_render_draw_color(renderer: *renderer,
	r: u8, g: u8, b: u8, a: u8) int;

// Set the color used for drawing operations (Rect, Line and Clear).
// 
// 'renderer' is the renderer for which drawing color should be set. 'r', 'g',
// 'b', and 'a' respectively set the red, gree, blue, and alpha channels.
export fn set_render_draw_color(
	renderer: *renderer,
	r: u8, g: u8, b: u8, a: u8,
) (void | error) = wrapvoid(_set_render_draw_color(renderer, r, g, b, a));

@symbol("SDL_RenderClear") fn _render_clear(renderer: *renderer) int;

// Clear the current rendering target with the drawing color
//
// This function clears the entire rendering target, ignoring the viewport and
// the clip rectangle.
export fn render_clear(renderer: *renderer) (void | error) = {
	return wrapvoid(_render_clear(renderer));
};

// Update the screen with rendering performed.
export @symbol("SDL_RenderPresent") fn render_present(renderer: *renderer) void;

// Destroy the specified texture.
export @symbol("SDL_DestroyTexture") fn destroy_texture(texture: *texture) void;

@symbol("SDL_QueryTexture") fn _query_texture(texture: *texture,
	format: nullable *u32, access: nullable *int,
	w: nullable *int, h: nullable *int) int;

// Query the attributes of a texture
export fn query_texture(
	texture: *texture,
	format: nullable *u32,
	access: nullable *int,
	w: nullable *int, h: nullable *int,
) (void | error) = {
	return wrapvoid(_query_texture(texture, format, access, w, h));
};

@symbol("SDL_SetTextureColorMod") fn _set_texture_color_mod(
	texture: *texture, r: u8, g: u8, b: u8) int;

// Set an additional color value multiplied into render copy operations.
//
// When this texture is rendered, during the copy operation each source color
// channel is modulated by the appropriate color value according to the
// following formula:
//
// 	srcC = srcC * (color / 255)
//
// Color modulation is not always supported by the renderer; it will return -1
// if color modulation is not supported.
export fn set_texture_color_mod(
	texture: *texture,
	r: u8, g: u8, b: u8,
) (void | error) = {
	return wrapvoid(_set_texture_color_mod(texture, r, g, b));
};

@symbol("SDL_SetTextureAlphaMod") fn _set_texture_alpha_mod(texture: *texture, a: u8) int;

// Set an additional alpha value multiplied into render copy operations.
//
// When this texture is rendered, during the copy operation the source alpha
// value is modulated by this alpha value according to the following formula:
//
// `srcA = srcA * (alpha / 255)`
//
// Alpha modulation is not always supported by the renderer; it will return an
// error if alpha modulation is not supported.
export fn set_texture_alpha_mod(texture: *texture, a: u8) (void | error) = {
	// TODO: Double check errors
	return wrapvoid(_set_texture_alpha_mod(texture, a));
};

@symbol("SDL_SetTextureBlendMode") fn _set_texture_blend_mode(
	texture: *texture, mode: blend_mode) int;

// Set the blend mode for a texture, used by SDL_RenderCopy().
export fn set_texture_blend_mode(
	texture: *texture,
	mode: blend_mode,
) (void | error) = {
	return wrapvoid(_set_texture_blend_mode(texture, mode));
};

@symbol("SDL_RenderCopy") fn _render_copy(renderer: *renderer,
	texture: *texture, srcrect: nullable *rect, dstrect: nullable *rect) int;

// Copy a portion of the texture to the current rendering target.
export fn render_copy(
	renderer: *renderer,
	texture: *texture,
	srcrect: nullable *rect,
	dstrect: nullable *rect,
) (void | error) = {
	return wrapvoid(_render_copy(renderer, texture, srcrect, dstrect));
};