1 //
2 //
3
4 #include "line_draw_list.h"
5 #include "2d.h"
6 #include "material.h"
7 #include "tracing/tracing.h"
8
9 namespace graphics {
10
line_draw_list()11 line_draw_list::line_draw_list() {
12 }
add_line(int x1,int y1,int x2,int y2,int resize_mode)13 void line_draw_list::add_line(int x1, int y1, int x2, int y2, int resize_mode) {
14 add_vertex(x1, y1, resize_mode, &gr_screen.current_color);
15 add_vertex(x2, y2, resize_mode, &gr_screen.current_color);
16 }
add_gradient(int x1,int y1,int x2,int y2,int resize_mode)17 void line_draw_list::add_gradient(int x1, int y1, int x2, int y2, int resize_mode) {
18 add_vertex(x1, y1, resize_mode, &gr_screen.current_color);
19
20 color endColor = gr_screen.current_color;
21 endColor.alpha = 0;
22 add_vertex(x2, y2, resize_mode, &endColor);
23 }
flush()24 void line_draw_list::flush() {
25 if (_line_vertices.empty()) {
26 // Nothing to do here...
27 return;
28 }
29
30 GR_DEBUG_SCOPE("Line draw list flush");
31 TRACE_SCOPE(tracing::LineDrawListFlush);
32
33 material line_mat;
34 line_mat.set_blend_mode(ALPHA_BLEND_ALPHA_BLEND_ALPHA);
35 line_mat.set_depth_mode(ZBUFFER_TYPE_NONE);
36 line_mat.set_cull_mode(false);
37 line_mat.set_color(1.0f, 1.0f, 1.0f, 1.0f); // Color is handled by the vertices
38
39
40 vertex_layout layout;
41 layout.add_vertex_component(vertex_format_data::POSITION2, sizeof(line_vertex), offsetof(line_vertex, position));
42 layout.add_vertex_component(vertex_format_data::COLOR4F, sizeof(line_vertex), offsetof(line_vertex, color));
43
44 gr_render_primitives_2d_immediate(&line_mat,
45 PRIM_TYPE_LINES,
46 &layout,
47 static_cast<int>(_line_vertices.size()),
48 _line_vertices.data(),
49 _line_vertices.size() * sizeof(line_vertex));
50
51 _line_vertices.clear();
52 }
add_vertex(int x,int y,int resize_mode,const color * color)53 void line_draw_list::add_vertex(int x, int y, int resize_mode, const color* color) {
54 line_vertex vtx{};
55 vtx.position.x = i2fl(x);
56 vtx.position.y = i2fl(y);
57
58 float w_scale = 1.0f;
59 float h_scale = 1.0f;
60
61 bool do_resize = gr_resize_screen_posf(&vtx.position.x, &vtx.position.y, &w_scale, &h_scale, resize_mode);
62
63 int offset_x = ((do_resize) ? gr_screen.offset_x_unscaled : gr_screen.offset_x);
64 int offset_y = ((do_resize) ? gr_screen.offset_y_unscaled : gr_screen.offset_y);
65
66 // m!m - This is silly but there is no better solution at the moment...
67 vtx.position.x += i2fl(offset_x) * w_scale;
68 vtx.position.y += i2fl(offset_y) * h_scale;
69
70 vtx.color.xyzw.x = color->red / 255.f;
71 vtx.color.xyzw.y = color->green / 255.f;
72 vtx.color.xyzw.z = color->blue / 255.f;
73 vtx.color.xyzw.w = color->is_alphacolor ? color->alpha / 255.f : 1.f;
74
75 _line_vertices.push_back(vtx);
76 }
77
78 }
79