aboutsummaryrefslogtreecommitdiff
path: root/src/draw.odin
blob: 8e31b96628c39604bca7d56a4a971d52840644e4 (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
package demonchime

import "core:c"
import "core:math"

import rl "vendor:raylib"

Color :: [4]f32

SCREEN_WIDTH :: 320
SCREEN_HEIGHT :: 320
SCREEN_SIZE :: Vec2{SCREEN_WIDTH, SCREEN_HEIGHT}

renderer: struct {
  screen: rl.RenderTexture2D,
  tint: Color,
}

@(private = "file")
_color_to_rl :: proc(col: Color) -> rl.Color {
  return rl.Color {
    u8(col.r * 255),
    u8(col.g * 255),
    u8(col.b * 255),
    u8(col.a * 255),
  }
}

init_draw :: proc() {
  renderer.screen = rl.LoadRenderTexture(SCREEN_WIDTH, SCREEN_HEIGHT)
  renderer.tint = Color{1, 1, 1, 1}
}

deinit_draw :: proc() {
  rl.UnloadRenderTexture(renderer.screen)
}

draw_new_frame :: proc() {
  rl.BeginTextureMode(renderer.screen)
  rl.ClearBackground(rl.BLACK)
}

draw_end_frame :: proc() {
  rl.EndTextureMode()

  rl.BeginDrawing()
  {
    rl.ClearBackground(_color_to_rl(Color{0, 0, 0, 1}))
    scale := math.min(
      f32(rl.GetScreenWidth()) / f32(SCREEN_WIDTH),
      f32(rl.GetScreenHeight()) / f32(SCREEN_HEIGHT),
    )
    screen_start := Vec2 {
      (f32(rl.GetScreenWidth()) - (SCREEN_WIDTH * scale)) / 2,
      (f32(rl.GetScreenHeight()) - (SCREEN_HEIGHT * scale)) / 2,
    }

    draw_texture_full(
      renderer.screen.texture,
      screen_start,
      scale = Vec2{scale, scale},
    )
  }
  rl.EndDrawing()
}

draw_rect :: proc(rect: Rect) {
  assert(rect.size.x > 0 && rect.size.y > 0)

  rl.DrawRectangle(
    c.int(rect.start.x),
    c.int(rect.start.y),
    c.int(rect.size.x),
    c.int(rect.size.y),
    _color_to_rl(renderer.tint),
  )
}

draw_linerect :: proc(rect: Rect) {
  assert(rect.size.x > 0 && rect.size.y > 0)

  rl.DrawRectangleLines(
    c.int(rect.start.x),
    c.int(rect.start.y),
    c.int(rect.size.x),
    c.int(rect.size.y),
    _color_to_rl(renderer.tint),
  )
}

draw_texture :: proc {
  draw_texture_full,
  draw_texture_quad,
}

draw_texture_quad :: proc(
  img: rl.Texture2D,
  quad: Rect,
  position: Vec2,
  offset := Vec2{0, 0},
  rotation: f32 = 0,
  scale := Vec2{1, 1},
) {
  quad := quad
  if scale.x < 0 {
    quad.size.x *= -1
  }
  if scale.y < 0 {
    quad.size.y *= -1
  }

  rl.DrawTexturePro(
    img,
    transmute(rl.Rectangle)quad,
    rl.Rectangle {
      position.x,
      position.y,
      quad.size.x * scale.x,
      quad.size.y * scale.y,
    },
    offset,
    rotation,
    _color_to_rl(renderer.tint),
  )
}

draw_texture_full :: proc(
  img: rl.Texture2D,
  position: Vec2,
  offset := Vec2{0, 0},
  rotation: f32 = 0,
  scale := Vec2{1, 1},
) {
  size := Vec2{f32(img.width), -f32(img.height)}

  draw_texture_quad(img, Rect{Vec2{0, 0}, size}, position, offset, rotation, scale)
}