Refator
This commit is contained in:
+68
-287
@@ -4,10 +4,9 @@ import e2_glyph "shared:Edit2D/glyph"
|
||||
import "core:strings"
|
||||
import "base:runtime"
|
||||
import "core:fmt"
|
||||
import "core:math"
|
||||
import "core:math/linalg"
|
||||
import gl "vendor:OpenGL"
|
||||
import b2 "vendor:box2d"
|
||||
|
||||
|
||||
CameraType :: enum
|
||||
{
|
||||
@@ -35,20 +34,12 @@ Rot :: struct
|
||||
|
||||
Transform :: struct
|
||||
{
|
||||
p: Vec2,
|
||||
p: [2]f32,
|
||||
q: Rot,
|
||||
}
|
||||
|
||||
|
||||
make_rgba :: proc(color : b2.HexColor, alpha : f32) -> RGBA8 {
|
||||
c := i32(color)
|
||||
return {
|
||||
u8((c >> 16) & 0xFF),
|
||||
u8((c >> 8) & 0xFF),
|
||||
u8(c & 0xFF),
|
||||
u8(0xFF * alpha),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
make_rgba_i32 :: proc(color : i32, alpha : f32) -> RGBA8 {
|
||||
c := i32(color)
|
||||
@@ -60,10 +51,6 @@ make_rgba_i32 :: proc(color : i32, alpha : f32) -> RGBA8 {
|
||||
}
|
||||
}
|
||||
|
||||
make_hex_color :: proc(rgba: RGBA8) -> b2.HexColor {
|
||||
return b2.HexColor((u32(rgba.r) << 16) | (u32(rgba.g) << 8) | u32(rgba.b))
|
||||
}
|
||||
|
||||
|
||||
camera_reset_view :: proc(camera : ^Camera) {
|
||||
camera.center = {0, 0}
|
||||
@@ -231,12 +218,14 @@ camera_build_project_matrix :: proc(
|
||||
return m * mat_rot
|
||||
}
|
||||
|
||||
/*
|
||||
camera_get_view_bounds :: proc(cam : ^Camera) -> b2.AABB {
|
||||
return b2.AABB {
|
||||
lowerBound = camera_convert_screen_to_world(cam, [2]f32{0, f32(cam.height)}),
|
||||
upperBound = camera_convert_screen_to_world(cam, [2]f32{f32(cam.width), 0}),
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
Background :: struct {
|
||||
@@ -430,8 +419,7 @@ points_destroy :: proc(point : ^Point) {
|
||||
}
|
||||
}
|
||||
|
||||
points_add :: proc(point : ^Point, v : [2]f32, size : f32, c : b2.HexColor) {
|
||||
rgba := make_rgba(c, 1.0)
|
||||
points_add :: proc(point : ^Point, v : [2]f32, size : f32, rgba : RGBA8) {
|
||||
append(&point.points, PointData{v, size, rgba})
|
||||
}
|
||||
|
||||
@@ -458,7 +446,7 @@ points_flush :: proc(point : ^Point, cam : ^Camera) {
|
||||
|
||||
gl.BufferSubData(
|
||||
gl.ARRAY_BUFFER, 0, int(batch_count * size_of(PointData)), &point.points[base])
|
||||
|
||||
|
||||
gl.DrawArrays(gl.POINTS, 0, batch_count)
|
||||
|
||||
check_opengl()
|
||||
@@ -573,8 +561,7 @@ lines_destroy :: proc(line : ^Lines) {
|
||||
}
|
||||
|
||||
|
||||
lines_add :: proc(line : ^Lines, p1, p2 : [2]f32, c : b2.HexColor) {
|
||||
rgba := make_rgba(c, 1.0)
|
||||
lines_add :: proc(line : ^Lines, p1, p2 : [2]f32, rgba : RGBA8) {
|
||||
append(&line.points, VertexData{p1, rgba})
|
||||
append(&line.points, VertexData{p2, rgba})
|
||||
}
|
||||
@@ -747,9 +734,9 @@ circle_add :: proc(
|
||||
circle : ^Circles,
|
||||
center : [2]f32,
|
||||
radius : f32,
|
||||
color : b2.HexColor,
|
||||
) {
|
||||
rgba := make_rgba(color, 1.0)
|
||||
rgba : RGBA8,
|
||||
)
|
||||
{
|
||||
append(&circle.circles, CircleData{center, radius, rgba})
|
||||
}
|
||||
|
||||
@@ -1098,11 +1085,11 @@ solid_capsules_add :: proc(
|
||||
capsule : ^SolidCapsules,
|
||||
p1, p2 : [2]f32,
|
||||
radius : f32,
|
||||
c : b2.HexColor,
|
||||
rgba : RGBA8,
|
||||
) {
|
||||
d := p2 - p1
|
||||
|
||||
length := b2.Length(d)
|
||||
length := math.sqrt(d.x * d.x + d.y * d.y)
|
||||
if length < 0.001 do return
|
||||
|
||||
axis := d / length
|
||||
@@ -1112,7 +1099,6 @@ solid_capsules_add :: proc(
|
||||
q = {c = axis.x, s = axis.y},
|
||||
}
|
||||
|
||||
rgba := make_rgba(c, 1.0)
|
||||
|
||||
append(&capsule.capsules, CapsuleData{transform, radius, length, rgba})
|
||||
}
|
||||
@@ -1235,86 +1221,21 @@ solid_polygon_create :: proc(polygon : ^SolidPolygon) {
|
||||
}
|
||||
|
||||
gl.BindBuffer(gl.ARRAY_BUFFER, polygon.vbo[0])
|
||||
gl.BufferData(
|
||||
gl.ARRAY_BUFFER,
|
||||
size_of([2]f32) * 6,
|
||||
&vertices[0],
|
||||
gl.STATIC_DRAW,
|
||||
)
|
||||
gl.BufferData(gl.ARRAY_BUFFER, size_of([2]f32) * 6, &vertices[0], gl.STATIC_DRAW)
|
||||
gl.VertexAttribPointer(vertex_attribute, 2, gl.FLOAT, gl.FALSE, 0, 0)
|
||||
|
||||
//
|
||||
gl.BindBuffer(gl.ARRAY_BUFFER, polygon.vbo[1])
|
||||
gl.BufferData(
|
||||
gl.ARRAY_BUFFER,
|
||||
int(batch_size * size_of(PolygonData)),
|
||||
nil,
|
||||
gl.DYNAMIC_DRAW,
|
||||
)
|
||||
gl.BufferData(gl.ARRAY_BUFFER, int(batch_size * size_of(PolygonData)), nil, gl.DYNAMIC_DRAW)
|
||||
|
||||
gl.VertexAttribPointer(
|
||||
instance_transform,
|
||||
4,
|
||||
gl.FLOAT,
|
||||
false,
|
||||
size_of(PolygonData),
|
||||
offset_of(PolygonData, transform),
|
||||
)
|
||||
gl.VertexAttribPointer(
|
||||
instance_point12,
|
||||
4,
|
||||
gl.FLOAT,
|
||||
false,
|
||||
size_of(PolygonData),
|
||||
offset_of(PolygonData, p1),
|
||||
)
|
||||
gl.VertexAttribPointer(
|
||||
instance_point34,
|
||||
4,
|
||||
gl.FLOAT,
|
||||
false,
|
||||
size_of(PolygonData),
|
||||
offset_of(PolygonData, p3),
|
||||
)
|
||||
gl.VertexAttribPointer(
|
||||
instance_point56,
|
||||
4,
|
||||
gl.FLOAT,
|
||||
false,
|
||||
size_of(PolygonData),
|
||||
offset_of(PolygonData, p5),
|
||||
)
|
||||
gl.VertexAttribPointer(
|
||||
instance_point78,
|
||||
4,
|
||||
gl.FLOAT,
|
||||
false,
|
||||
size_of(PolygonData),
|
||||
offset_of(PolygonData, p7),
|
||||
)
|
||||
gl.VertexAttribIPointer(
|
||||
instance_point_count,
|
||||
1,
|
||||
gl.INT,
|
||||
size_of(PolygonData),
|
||||
offset_of(PolygonData, count),
|
||||
)
|
||||
gl.VertexAttribPointer(
|
||||
instance_radius,
|
||||
1,
|
||||
gl.FLOAT,
|
||||
false,
|
||||
size_of(PolygonData),
|
||||
offset_of(PolygonData, radius),
|
||||
)
|
||||
gl.VertexAttribPointer(
|
||||
instance_color,
|
||||
4,
|
||||
gl.UNSIGNED_BYTE,
|
||||
true,
|
||||
size_of(PolygonData),
|
||||
offset_of(PolygonData, color),
|
||||
)
|
||||
gl.VertexAttribPointer(instance_transform, 4, gl.FLOAT, false, size_of(PolygonData), offset_of(PolygonData, transform))
|
||||
gl.VertexAttribPointer(instance_point12, 4, gl.FLOAT, false, size_of(PolygonData), offset_of(PolygonData, p1))
|
||||
gl.VertexAttribPointer(instance_point34, 4, gl.FLOAT, false, size_of(PolygonData), offset_of(PolygonData, p3))
|
||||
gl.VertexAttribPointer(instance_point56, 4, gl.FLOAT, false, size_of(PolygonData), offset_of(PolygonData, p5))
|
||||
gl.VertexAttribPointer(instance_point78, 4, gl.FLOAT, false, size_of(PolygonData), offset_of(PolygonData, p7))
|
||||
gl.VertexAttribIPointer(instance_point_count, 1, gl.INT, size_of(PolygonData), offset_of(PolygonData, count))
|
||||
gl.VertexAttribPointer(instance_radius, 1, gl.FLOAT, false, size_of(PolygonData), offset_of(PolygonData, radius))
|
||||
gl.VertexAttribPointer(instance_color, 4, gl.UNSIGNED_BYTE, true, size_of(PolygonData), offset_of(PolygonData, color))
|
||||
|
||||
|
||||
gl.VertexAttribDivisor(instance_transform, 1)
|
||||
@@ -1339,12 +1260,11 @@ solid_polygon_add :: proc(
|
||||
points : [^][2]f32,
|
||||
count : i32,
|
||||
radius : f32,
|
||||
color : b2.HexColor,
|
||||
color : RGBA8,
|
||||
) {
|
||||
|
||||
|
||||
data : PolygonData
|
||||
|
||||
data.transform = transform
|
||||
|
||||
n := min(count, 8)
|
||||
@@ -1357,7 +1277,7 @@ solid_polygon_add :: proc(
|
||||
|
||||
data.count = n
|
||||
data.radius = f32(radius)
|
||||
data.color = make_rgba(color, 1.0)
|
||||
data.color = color
|
||||
|
||||
append(&polygon.polygons, data)
|
||||
}
|
||||
@@ -1408,7 +1328,6 @@ solid_polygon_flush :: proc(polygon : ^SolidPolygon, cam : ^Camera) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
TextItem :: struct
|
||||
{
|
||||
str : string,
|
||||
@@ -1419,7 +1338,6 @@ TextItem :: struct
|
||||
Draw :: struct
|
||||
{
|
||||
show_ui : bool,
|
||||
debug_draw : b2.DebugDraw,
|
||||
cam : Camera,
|
||||
background : Background,
|
||||
points : Point,
|
||||
@@ -1431,162 +1349,43 @@ Draw :: struct
|
||||
drawCounters : bool,
|
||||
//regular_font : im.Font,
|
||||
frame_buffer : u32,
|
||||
|
||||
//There should be option to draw without glyph? maybe
|
||||
glyph : e2_glyph.GlyphState,
|
||||
|
||||
texts : [dynamic]TextItem,
|
||||
}
|
||||
|
||||
draw_aabb :: proc(draw : ^Draw, aabb : b2.AABB, c : b2.HexColor) {
|
||||
/*
|
||||
draw_aabb :: proc(draw : ^Draw, aabb : [2][2]f32, rgba : RGBA8) {
|
||||
p1 := aabb.lowerBound
|
||||
p2 : [2]f32 = {aabb.upperBound.x, aabb.lowerBound.y}
|
||||
|
||||
p3 := aabb.upperBound
|
||||
p4 : [2]f32 = {aabb.lowerBound.x, aabb.upperBound.y}
|
||||
|
||||
lines_add(&draw.lines, p1, p2, c)
|
||||
lines_add(&draw.lines, p2, p3, c)
|
||||
lines_add(&draw.lines, p3, p4, c)
|
||||
lines_add(&draw.lines, p4, p1, c)
|
||||
lines_add(&draw.lines, p1, p2, rgba)
|
||||
lines_add(&draw.lines, p2, p3, rgba)
|
||||
lines_add(&draw.lines, p3, p4, rgba)
|
||||
lines_add(&draw.lines, p4, p1, rgba)
|
||||
}
|
||||
*/
|
||||
|
||||
DrawPolygonFcn :: proc "c" (
|
||||
vertices : [^][2]f32,
|
||||
vertexCount : i32,
|
||||
color : b2.HexColor,
|
||||
ctx : rawptr,
|
||||
) {
|
||||
|
||||
context = runtime.default_context()
|
||||
draw : ^Draw = cast(^Draw)ctx
|
||||
|
||||
p1 := vertices[vertexCount - 1]
|
||||
for i in 0 ..< vertexCount {
|
||||
p2 := vertices[i]
|
||||
lines_add(&draw.lines, p1, vertices[i], color)
|
||||
p1 = p2
|
||||
}
|
||||
}
|
||||
|
||||
DrawSolidPolygonFcn :: proc "c" (
|
||||
transform : Transform,
|
||||
vertices : [^][2]f32,
|
||||
vertexCount : i32,
|
||||
radius : f32,
|
||||
color : b2.HexColor,
|
||||
ctx : rawptr,
|
||||
) {
|
||||
|
||||
|
||||
context = runtime.default_context()
|
||||
|
||||
draw : ^Draw = cast(^Draw)ctx
|
||||
solid_polygon_add(
|
||||
&draw.polygons,
|
||||
transform,
|
||||
vertices,
|
||||
vertexCount,
|
||||
radius,
|
||||
color,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
DrawCircleFcn :: proc "c" (
|
||||
center : [2]f32,
|
||||
radius : f32,
|
||||
color : b2.HexColor,
|
||||
ctx : rawptr,
|
||||
) {
|
||||
context = runtime.default_context()
|
||||
draw : ^Draw = cast(^Draw)ctx
|
||||
circle_add(&draw.circles, center, radius, color)
|
||||
}
|
||||
|
||||
DrawSolidCircle :: proc(
|
||||
circle : ^SolidCircle,
|
||||
transform : Transform,
|
||||
center : [2]f32,
|
||||
radius : f32,
|
||||
color : b2.HexColor,
|
||||
rgba: RGBA8,
|
||||
) {
|
||||
context = runtime.default_context()
|
||||
|
||||
transform := transform
|
||||
|
||||
rgba := make_rgba(color, 1.0)
|
||||
|
||||
transform.p = b2.TransformPoint(transform, center)
|
||||
solid_circle_add(circle, transform, radius, rgba)
|
||||
}
|
||||
|
||||
DrawTransform :: proc "c" (lines : ^Lines, transform : Transform) {
|
||||
context = runtime.default_context()
|
||||
k_axis_scale : f32 = 0.2
|
||||
p1 := transform.p
|
||||
|
||||
p2 := p1 + k_axis_scale * b2.Rot_GetXAxis(transform.q)
|
||||
lines_add(lines, p1, p2, b2.HexColor.Red)
|
||||
|
||||
p2 = p1 + k_axis_scale * b2.Rot_GetYAxis(transform.q)
|
||||
lines_add(lines, p1, p2, b2.HexColor.Green)
|
||||
}
|
||||
|
||||
DrawSolidCircleFcn :: proc "c" (
|
||||
transform : Transform,
|
||||
radius : f32,
|
||||
color : b2.HexColor,
|
||||
ctx : rawptr,
|
||||
) {
|
||||
|
||||
context = runtime.default_context()
|
||||
draw : ^Draw = cast(^Draw)ctx
|
||||
DrawSolidCircle(
|
||||
&draw.solid_circles,
|
||||
transform,
|
||||
{0, 0},
|
||||
radius,
|
||||
color,
|
||||
)
|
||||
}
|
||||
|
||||
DrawSolidCapsuleFcn :: proc "c" (
|
||||
p1, p2 : [2]f32,
|
||||
radius : f32,
|
||||
color : b2.HexColor,
|
||||
ctx : rawptr,
|
||||
) {
|
||||
context = runtime.default_context()
|
||||
draw : ^Draw = cast(^Draw)ctx
|
||||
solid_capsules_add(&draw.solid_capsules, p1, p2, radius, color)
|
||||
}
|
||||
|
||||
DrawSegmentFcn :: proc "c" (
|
||||
p1, p2 : [2]f32,
|
||||
color : b2.HexColor,
|
||||
ctx : rawptr,
|
||||
) {
|
||||
context = runtime.default_context()
|
||||
draw : ^Draw = cast(^Draw)ctx
|
||||
lines_add(&draw.lines, p1, p2, color)
|
||||
}
|
||||
|
||||
DrawTransformFcn :: proc "c" (transform : Transform, ctx : rawptr) {
|
||||
context = runtime.default_context()
|
||||
draw : ^Draw = cast(^Draw)ctx
|
||||
DrawTransform(&draw.lines, transform)
|
||||
}
|
||||
|
||||
DrawPointFcn :: proc "c" (
|
||||
p : [2]f32,
|
||||
size : f32,
|
||||
color : b2.HexColor,
|
||||
ctx : rawptr,
|
||||
) {
|
||||
context = runtime.default_context()
|
||||
draw : ^Draw = cast(^Draw)ctx
|
||||
points_add(&draw.points, p, size, color)
|
||||
}
|
||||
|
||||
DrawString :: proc(draw : ^Draw, x, y : int, cstr: cstring)
|
||||
{
|
||||
ps := camera_convert_world_to_screen(&draw.cam, {f32(x), f32(y)})
|
||||
@@ -1615,20 +1414,6 @@ DrawStringVec :: proc(draw : ^Draw, p : [2]f32, cstr: cstring)
|
||||
|
||||
|
||||
append(&draw.texts, TextItem{str, {f32(ps.x), f32(ps.y)}, {200,200,200,255}})
|
||||
/*
|
||||
im.DrawList_AddText(im.GetForegroundDrawList(), ps,im.GetColorU32(.Text), str)
|
||||
*/
|
||||
}
|
||||
|
||||
DrawStringFcn :: proc "c" (
|
||||
p : [2]f32,
|
||||
s : cstring,
|
||||
color : b2.HexColor,
|
||||
ctx : rawptr,
|
||||
) {
|
||||
context = runtime.default_context()
|
||||
draw : ^Draw = cast(^Draw)ctx
|
||||
DrawStringVec(draw, p, s)
|
||||
}
|
||||
|
||||
text_flush :: proc(draw: ^Draw)
|
||||
@@ -1642,7 +1427,7 @@ text_flush :: proc(draw: ^Draw)
|
||||
|
||||
draw_flush :: proc(draw : ^Draw) {
|
||||
|
||||
background_draw(&draw.background, &draw.cam)
|
||||
//background_draw(&draw.background, &draw.cam)
|
||||
|
||||
solid_circle_flush(&draw.solid_circles, &draw.cam)
|
||||
solid_polygon_flush(&draw.polygons, &draw.cam)
|
||||
@@ -1652,13 +1437,42 @@ draw_flush :: proc(draw : ^Draw) {
|
||||
circle_flush(&draw.circles, &draw.cam)
|
||||
text_flush(draw)
|
||||
|
||||
|
||||
|
||||
check_opengl()
|
||||
}
|
||||
|
||||
/*
|
||||
Create texture and render texture
|
||||
|
||||
create opengl texture when a buffer data is provided and store the textuer
|
||||
The function returns the actual opengl texture_id
|
||||
|
||||
render_texture :: gets a texture_id and rect and displays the texture (generic but convinent for micorui)
|
||||
*/
|
||||
|
||||
create_texutre :: proc(data: []u8, width, height: i32) -> u32
|
||||
{
|
||||
|
||||
texture : u32
|
||||
gl.GenTextures(1, &texture)
|
||||
gl.BindTexture(gl.TEXTURE_2D, texture)
|
||||
gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA8, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, raw_data(data))
|
||||
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
||||
|
||||
gl.BindTexture(gl.TEXTURE_2D, 0)
|
||||
|
||||
return texture
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//This should be handled in engine code separately
|
||||
draw_create :: proc(draw : ^Draw, camera : ^Camera, font_path : string = "") {
|
||||
draw_create :: proc(draw : ^Draw, camera : ^Camera, font_path : string = "")
|
||||
{
|
||||
|
||||
background_create(&draw.background)
|
||||
points_create(&draw.points)
|
||||
@@ -1679,38 +1493,5 @@ draw_create :: proc(draw : ^Draw, camera : ^Camera, font_path : string = "") {
|
||||
}
|
||||
|
||||
check_opengl()
|
||||
|
||||
|
||||
bounds : b2.AABB = {{-max(f32), -max(f32)}, {max(f32), max(f32)}}
|
||||
|
||||
draw.debug_draw.DrawPolygonFcn = DrawPolygonFcn
|
||||
draw.debug_draw.DrawSolidPolygonFcn = DrawSolidPolygonFcn
|
||||
draw.debug_draw.DrawCircleFcn = DrawCircleFcn
|
||||
draw.debug_draw.DrawSolidCircleFcn = DrawSolidCircleFcn
|
||||
draw.debug_draw.DrawSolidCapsuleFcn = DrawSolidCapsuleFcn
|
||||
draw.debug_draw.DrawSegmentFcn = DrawSegmentFcn
|
||||
draw.debug_draw.DrawTransformFcn = DrawTransformFcn
|
||||
draw.debug_draw.DrawPointFcn = DrawPointFcn
|
||||
draw.debug_draw.DrawStringFcn = DrawStringFcn
|
||||
draw.debug_draw.drawingBounds = bounds
|
||||
|
||||
draw.debug_draw.useDrawingBounds = false
|
||||
draw.debug_draw.drawShapes = true
|
||||
draw.debug_draw.drawJoints = true
|
||||
draw.debug_draw.drawJointExtras = false
|
||||
draw.debug_draw.drawBounds = false
|
||||
draw.debug_draw.drawMass = true
|
||||
draw.debug_draw.drawContacts = false
|
||||
draw.debug_draw.drawGraphColors = false
|
||||
draw.debug_draw.drawContactNormals = false
|
||||
draw.debug_draw.drawContactImpulses = false
|
||||
draw.debug_draw.drawContactFeatures = false
|
||||
draw.debug_draw.drawFrictionImpulses = false
|
||||
draw.debug_draw.drawIslands = false
|
||||
|
||||
draw.drawCounters = true
|
||||
|
||||
draw.debug_draw.userContext = rawptr(draw)
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user