-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.cpp
69 lines (55 loc) · 1.89 KB
/
draw.cpp
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
#include <stdint.h> // for uint32_t
#include <stdio.h> // for printf
#include <stdlib.h> // for exit
#include "draw.hpp"
float vertex_buffer[1000];
int vertex_buffer_used = 0;
uint32_t index_buffer[1000];
int index_buffer_used = 0;
void clear_draw_buffers()
{
vertex_buffer_used = 0;
index_buffer_used = 0;
}
void append_vertex(float x, float y, Color col)
{
if ((vertex_buffer_used + 7) * sizeof(*vertex_buffer) >= sizeof(vertex_buffer)) {
printf("Error: vertex buffer full!\n");
exit(-1);
}
int window_w = sapp_width();
int window_h = sapp_height();
vertex_buffer[vertex_buffer_used++] = (x / window_w) * 2 - 1;
vertex_buffer[vertex_buffer_used++] = (y / window_h) * 2 - 1;
vertex_buffer[vertex_buffer_used++] = 0.5f;
vertex_buffer[vertex_buffer_used++] = col.r;
vertex_buffer[vertex_buffer_used++] = col.g;
vertex_buffer[vertex_buffer_used++] = col.b;
vertex_buffer[vertex_buffer_used++] = col.a;
}
void draw_rect(float x, float y, float sx, float sy, Color col)
{
int start_index = vertex_buffer_used / (3 + 4);
append_vertex(x, y, col);
append_vertex(x+sx, y, col);
append_vertex(x+sx, y+sy, col);
append_vertex(x, y+sy, col);
if ((index_buffer_used + 6) * sizeof(*index_buffer) >= sizeof(index_buffer)) {
printf("Error: index buffer full!\n");
exit(-1);
}
index_buffer[index_buffer_used++] = start_index + 0;
index_buffer[index_buffer_used++] = start_index + 1;
index_buffer[index_buffer_used++] = start_index + 2;
index_buffer[index_buffer_used++] = start_index + 0;
index_buffer[index_buffer_used++] = start_index + 2;
index_buffer[index_buffer_used++] = start_index + 3;
}
void draw_circle(float center_x, float center_y, float radius, Color col)
{
// to implement
}
void draw_line(float start_x, float start_y, float thickness, Color col)
{
// to implement
}