|
| 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 | + |
| 11 | +line_draw_list::line_draw_list() { |
| 12 | +} |
| 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 | +} |
| 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 | +} |
| 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 | +} |
| 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 | + gr_resize_screen_posf(&vtx.position.x, &vtx.position.y, nullptr, nullptr, resize_mode); |
| 59 | + vtx.color.xyzw.x = color->red / 255.f; |
| 60 | + vtx.color.xyzw.y = color->green / 255.f; |
| 61 | + vtx.color.xyzw.z = color->blue / 255.f; |
| 62 | + vtx.color.xyzw.w = color->is_alphacolor ? color->alpha / 255.f : 1.f; |
| 63 | + |
| 64 | + _line_vertices.push_back(vtx); |
| 65 | +} |
| 66 | + |
| 67 | +} |
0 commit comments