forked from vlang/v
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgg.v
More file actions
718 lines (668 loc) · 18.4 KB
/
Copy pathgg.v
File metadata and controls
718 lines (668 loc) · 18.4 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module gg
import stbi
import glm
import gl
#flag darwin -I/usr/local/Cellar/freetype/2.10.0/include/freetype2
#flag -lfreetype
#flag linux -I/usr/include/freetype2
#flag linux -I.
#include "ft2build.h"
#include FT_FREETYPE_H
#include "glad.h"
struct Vec2 {
x int
y int
}
import const (
GL_STATIC_DRAW
GL_FLOAT
GL_FALSE
GL_UNSIGNED_INT
GL_INT
)
const (
DEFAULT_FONT_SIZE = 12
)
pub fn vec2(x, y int) Vec2 {
res := Vec2 {
x: x,
y: y,
}
return res
}
struct Character {
texture_id u32
size Vec2
bearing Vec2
advance u32
}
fn init() {
println(gl.TEXT_VERT)
gl.init_glad()
}
struct Face {
cobj voidptr
kek int
}
struct Cfg {
width int
height int
use_ortho int
retina bool
font_size int
}
struct GG {
shader gl.Shader
// use_ortho bool
width int
height int
VAO u32
rect_vao u32
rect_vbo u32
line_vao u32
line_vbo u32
VBO u32
chars []gg.Character
utf_runes []string
utf_chars []gg.Character
text_ctx *GG
face Face
scale int // retina = 2 , normal = 1
}
// fn new_context(width, height int, use_ortho bool, font_size int) *GG {
pub fn new_context(cfg Cfg) *GG {
// println('new context orhto=$cfg.use_ortho')
// # glScissor(0,0,300,300);
shader := gl.new_shader('simple')
shader.use()
if cfg.use_ortho > 0 {
projection := glm.ortho(0, cfg.width, cfg.height, 0)
/*
// for debugging broken tetris in gg.o
# projection.data[0]=0.010000;
# projection.data[1]=0.000000;
# projection.data[2]=0.000000;
# projection.data[3]=0.000000;
# projection.data[4]=0.000000;
# projection.data[5]=-0.005000;
# projection.data[6]=0.000000;
# projection.data[7]=0.000000;
# projection.data[8]=0.000000;
# projection.data[9]=0.000000;
# projection.data[10]=1.000000;
# projection.data[11]=0.000000;
# projection.data[12]=-1.000000;
# projection.data[13]=1.000000;
# projection.data[14]=0.000000;
# projection.data[15]=1.000000;
*/
// projection_new := ortho(0, width, height, 0)
// println('\nORTHO OLD=')
# for (int i=0;i<16;i++) printf("%d=%f ",i, projection.data[i]);
// println('\n\n!ORTHO NEW=')
// # for (int i=0;i<16;i++) printf("%d=%f ",i, projection_new[i]);
// println('\n\n')
println('setting o')
shader.set_mat4('projection', projection)
}
else {
// TODO move to function (allow volt functions to return arrrays without allocations)
// i := glm.identity3()
shader.set_mat4('projection', glm.identity())
}
VAO := gl.gen_vertex_array()
println('new gg context VAO=$VAO')
VBO := gl.gen_buffer()
mut scale := 1
if cfg.retina {
scale = 2
}
mut ctx := &GG {
shader: shader,
width: cfg.width,
height: cfg.height,
VAO: VAO,
VBO: VBO,
// /line_vao: gl.gen_vertex_array()
// /line_vbo: gl.gen_buffer()
text_ctx: new_context_text(cfg, scale),
scale: scale
// use_ortho: use_ortho
}
// ctx.init_rect_vao()
return ctx
}
pub fn (ctx &GG) draw_triangle(x1, y1, x2, y2, x3, y3 float, c gx.Color) {
// println('draw_triangle $x1,$y1 $x2,$y2 $x3,$y3')
ctx.shader.use()
ctx.shader.set_color('color', c)
vertices := [
x1, y1, 0,
x2, y2, 0,
x3, y3, 0,
] !
// bind the Vertex Array Object first, then bind and set vertex buffer(s),
// and then configure vertex attributes(s).
gl.bind_vao(ctx.VAO)
gl.set_vbo(ctx.VBO, vertices, GL_STATIC_DRAW)
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 3, 0)
gl.enable_vertex_attrib_array(0)
// gl.bind_buffer(GL_ARRAY_BUFFER, uint(0))
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO,
// but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs
// (nor VBOs) when it's not directly necessary.
// gl.bind_vertex_array(uint(0))
// gl.bind_vertex_array(ctx.VAO)
gl.draw_arrays(GL_TRIANGLES, 0, 3)
}
pub fn (ctx &GG) draw_triangle_tex(x1, y1, x2, y2, x3, y3 float, c gx.Color) {
ctx.shader.use()
ctx.shader.set_color('color', c)
ctx.shader.set_int('has_texture', 1)
vertices := [
x1, y1, 0, 0, 0, 0, 1, 1,
x2, y2, 0, 0, 0, 0, 1, 0,
x3, y3, 0, 0, 0, 0, 0, 0,
] !
gl.bind_vao(ctx.VAO)
gl.set_vbo(ctx.VBO, vertices, GL_STATIC_DRAW)
// position attribute
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 3, 0)
gl.enable_vertex_attrib_array(0)
// color attribute
gl.vertex_attrib_pointer(1, 3, GL_FLOAT, false, 8, 3)
gl.enable_vertex_attrib_array(1)
// texture attribute
gl.vertex_attrib_pointer(2, 2, GL_FLOAT, false, 8, 6)
gl.enable_vertex_attrib_array(2)
// /
// gl.draw_arrays(GL_TRIANGLES, 0, 3)
gl.draw_elements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
}
fn (ctx &GG) draw_rect(x, y, w, h float, c gx.Color) {
// println('gg.draw_rect($x,$y,$w,$h)')
// wrong order
// // ctx.draw_triangle(x, y, x + w, y, x + w, y + h, c)
// // ctx.draw_triangle(x, y, x, y + h, x + w, y + h, c)
// good order. counter clock wise
// ctx.draw_triangle(x, y, x, y + h, x + w, y + h, c)
// ctx.draw_triangle(x, y, x + w, y + h, x + w, y, c)
ctx.draw_rect2(x, y, w, h, c)
}
/*
fn (ctx mut GG) init_rect_vao() {
ctx.rect_vao = gl.gen_vertex_array()
ctx.rect_vbo = gl.gen_buffer()
vertices := [
x + w, y, 0,
x + w, y + h, 0,
x, y + h, 0,
x, y, 0,
] !
indices := [
0, 1, 3,// first triangle
1, 2, 3// second triangle
] !
gl.bind_vao(ctx.rect_vao)
gl.set_vbo(ctx.rect_vbo, vertices, GL_STATIC_DRAW)
ebo := gl.gen_buffer()
// ///////
gl.set_ebo(ebo, indices, GL_STATIC_DRAW)
}
*/
fn (ctx &GG) draw_rect2(x, y, w, h float, c gx.Color) {
C.glDeleteBuffers(1, &ctx.VAO)
C.glDeleteBuffers(1, &ctx.VBO)
ctx.shader.use()
ctx.shader.set_color('color', c)
ctx.shader.set_int('has_texture', 0)
// 4--1
// 3--2
#ifdef linux
// y += h
#endif
vertices := [
x + w, y, 0,
x + w, y + h, 0,
x, y + h, 0,
x, y, 0,
] !
indices := [
0, 1, 3,// first triangle
1, 2, 3// second triangle
] !
gl.bind_vao(ctx.VAO)
gl.set_vbo(ctx.VBO, vertices, GL_STATIC_DRAW)
ebo := gl.gen_buffer()
// ///////
gl.set_ebo(ebo, indices, GL_STATIC_DRAW)// !!! LEAKS
// /////
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 3, 0)
gl.enable_vertex_attrib_array(0)
// gl.bind_vao(ctx.rect_vao)
gl.bind_vao(ctx.VAO)
gl.draw_elements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
C.glDeleteBuffers(1, &ebo)
}
// jfn ft_load_char(face FT_Face, code FT_ULong) Character {
// fn ft_load_char(_face voidptr, _code voidptr) Character {
fn ft_load_char(_face Face, code long) Character {
// #FT_Face face = *(FT_Face*)(_face); FT_ULong code = *(FT_ULong*)(code);
# FT_Face face = *((FT_Face*)_face.cobj);
# if (FT_Load_Char(face, code, FT_LOAD_RENDER))
{
println('freetype: Failed to load Glyph')
exit(1)
}
// Generate texture
# GLuint texture;
# glGenTextures(1, &texture);
# glBindTexture(GL_TEXTURE_2D, texture);
# glTexImage2D(
# GL_TEXTURE_2D,
# 0,
# GL_RED,
# face->glyph->bitmap.width,
# face->glyph->bitmap.rows,
# 0,
# GL_RED,
# GL_UNSIGNED_BYTE,
# face->glyph->bitmap.buffer
# );
// Set texture options
# glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
# glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
# glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
# glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Now store character for later use
ch := Character{}
# ch.texture_id=texture ;
# ch.size = gg__vec2(face->glyph->bitmap.width, face->glyph->bitmap.rows);
# ch.bearing = gg__vec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
# ch.advance = face->glyph->advance.x;
return ch
}
fn new_context_text(cfg Cfg, scale int) *GG {
// Can only have text in ortho mode
if !cfg.use_ortho {
return &GG{text_ctx: 0}
}
mut width := cfg.width * scale
mut height := cfg.height * scale
font_size := cfg.font_size * scale
// exit('fs=$font_size')
// if false {
// retina
// width = width * 2// scale// 2
// height = height * 2// scale// 2
// font_size *= scale// 2
// }
/*
gl.viewport(0, 0, width, height)
*/
// gl.enable(GL_CULL_FACE) // TODO NEED CULL? MEANS SHIT IS BROKEN?
gl.enable(GL_BLEND)
// return &GG{}
# glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
shader := gl.new_shader('text')
shader.use()
projection := glm.ortho(0, width, 0, height)// 0 at BOT
// projection_new := ortho(0, width, 0, height)// 0 at BOT
// projection := gl.ortho(0, width,height,0) // 0 at TOP
shader.set_mat4('projection', projection)
// FREETYPE
# FT_Library ft;
// All functions return a value different than 0 whenever an error occurred
# if (FT_Init_FreeType(&ft))
println('ERROR::FREETYPE: Could not init FreeType Library')
// Load font as face
// face := FT_Face{}
mut font_path := 'RobotoMono-Regular.ttf'
if !os.file_exists(font_path) {
font_path = '/var/tmp/RobotoMono-Regular.ttf'
}
if !os.file_exists(font_path) {
println('failed to load RobotoMono-Regular.ttf')
exit(1)
}
# FT_Face face;
# if (FT_New_Face(ft, font_path.str, 0, &face))
// # if (FT_New_Face(ft, "/Library/Fonts/Courier New.ttf", 0, &face))
// # if (FT_New_Face(ft, "/System/Library/Fonts/Apple Color Emoji.ttc", 0, &face))
{
println('freetyp: Failed to load font')
exit(1)
}
// Set size to load glyphs as
# FT_Set_Pixel_Sizes(face, 0, font_size) ;
// Disable byte-alignment restriction
# glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Gen texture
// Load first 128 characters of ASCII set
mut chars := []gg.Character{}
f := Face {
cobj: 0
kek: 0
}
# f.cobj = &face;
// # for (GLubyte c = 0; c < 128; c++)
for c := 0; c < 128; c++ {
// ch := Character{}
// ch:=ft_load_char(face, c)
// # ch =gg__ft_load_char(&face, &c);
// ////////////////////////////////
mut ch := ft_load_char(f, long(c))
// s := utf32_to_str(uint(0x043f))
// s := 'п'
// ch = ft_load_char(f, s.utf32_code())
// # ch = gg__ft_load_char(f, 0x043f); // RUS P
// # unsigned long c = FT_Get_Char_Index(face, 0x043f );
// # printf("!!!!!!!!! %lu\n", c);
// # c = FT_Get_Char_Index(face, 0xd0bf );
// # printf("!!!!!!!!! %lu\n", c);
// # ch = gg__ft_load_char(f, 0xd0bf) ; // UTF 8
chars << ch
}
ch := Character{}
// # ch = gg__ft_load_char(f, 0x0000043f);
// # ch = gg__ft_load_char(f, 128169);
// chars.push(ch)
// Configure VAO
VAO := gl.gen_vertex_array()
println('new gg text context VAO=$VAO')
VBO := gl.gen_buffer()
gl.bind_vao(VAO)
gl.bind_buffer(GL_ARRAY_BUFFER, VBO)
// # glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
gl.enable_vertex_attrib_array(0)
gl.vertex_attrib_pointer(0, 4, GL_FLOAT, false, 4, 0)
// # glVertexAttribPointer(0, 4, GL_FLOAT,false, 4 * sizeof(GLfloat), 0);
// gl.bind_buffer(GL_ARRAY_BUFFER, uint(0))
// # glBindVertexArray(0);
mut ctx := &GG {
shader: shader,
width: width,
height: height,
scale: scale
VAO: VAO,
VBO: VBO,
chars: chars,
face: f
text_ctx: 0
}
ctx.init_utf8_runes()
return ctx
}
// A dirty hack to implement rendering of cyrillic letters.
// All UTF-8 must be supported.
fn (ctx mut GG) init_utf8_runes() {
s := '≈йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ'
println(s)
us := s.ustring()
for i := 0; i < us.len; i++ {
_rune := us.at(i)
ch := ft_load_char(ctx.face, _rune.utf32_code())
// ctx.utf_rune_map.set(rune, ch)
ctx.utf_runes << _rune
ctx.utf_chars << ch
}
}
// fn (ctx &GG) render_text(text string, x, y, scale float, color gx.Color) {
pub fn (ctx &GG) draw_text(_x, _y int, text string, cfg gx.TextCfg) {
// dont draw non ascii for now
/*
for i := 0; i < text.len; i++ {
c := text[i]
if int(c) > 128 {
// ctx.text_ctx._draw_text(_x, _y, '[NON ASCII]', cfg)
// return
}
}
*/
// # glScissor(0,0,300,300);
utext := text.ustring_tmp()
// utext := text.ustring()
ctx.text_ctx._draw_text(_x, _y, utext, cfg)
// utext.free()
// # glScissor(0,0,ctx->width*2,ctx->height*2);
// gl.disable(GL_SCISSOR_TEST)// TODO
// #free(text.str);
}
fn (ctx &GG) draw_text_fast(_x, _y int, text ustring, cfg gx.TextCfg) {
ctx.text_ctx._draw_text(_x, _y, text, cfg)
}
// TODO HACK with second text context
// fn (ctx &GG) _draw_text(_x, _y int, text string, cfg gx.TextCfg) {
fn (ctx &GG) _draw_text(_x, _y int, utext ustring, cfg gx.TextCfg) {
/*
if utext.s.contains('on_seg') {
println('\nat(0)')
println(utext.runes)
firstc := utext.at(0)
println('drawtext "$utext.s" len=$utext.s.len ulen=$utext.len x=$_x firstc=$firstc')
if firstc != ' ' {
exit(1)
}
}
*/
// println('scale=$ctx.scale size=$cfg.size')
if cfg.align == gx.ALIGN_RIGHT {
width := utext.len * 7
_x -= width + 10
}
x := float(_x) * ctx.scale// float(2)
// println('y=$_y height=$ctx.height')
// _y = _y * int(ctx.scale) //+ 26
_y = _y * int(ctx.scale) + ((cfg.size * ctx.scale) / 2) + 5 * ctx.scale
y := float(ctx.height - _y)
color := cfg.color
// Activate corresponding render state
ctx.shader.use()
ctx.shader.set_color('textColor', color)
# glActiveTexture(GL_TEXTURE0);
gl.bind_vao(ctx.VAO)
// Iterate through all characters
// utext := text.ustring()
for i := 0; i < utext.len; i++ {
_rune := utext.at(i)
// println('$i => $_rune')
mut ch := Character{}
if _rune.len == 1 {
idx := _rune[0]
if idx < 0 || idx >= ctx.chars.len {
println('BADE RUNE $_rune')
continue
}
ch = ctx.chars[_rune[0]]
}
else if _rune.len > 1 {
// TODO O(1) use map
for j := 0; j < ctx.utf_runes.len; j++ {
rune_j := ctx.utf_runes[j]
// if string_eq(ctx.utf_runes[j], rune) {
if rune_j.eq(_rune) {
ch = ctx.utf_chars[j]
break
}
}
}
if ch.size.x == 0 {
// continue
}
// mut c := int(text[i])
// c = 128
// s := 'A'
// c := int(s[0])
// ch := ctx.chars[c]
xpos := x + float(ch.bearing.x) * 1
ypos := y - float(ch.size.y - ch.bearing.y) * 1
w := float(ch.size.x) * 1
h := float(ch.size.y) * 1
// Update VBO for each character
# GLfloat vertices[6][4] = {
# { xpos, ypos + h, 0.0, 0.0 },
# { xpos, ypos, 0.0, 1.0 },
# { xpos + w, ypos, 1.0, 1.0 },
# { xpos, ypos + h, 0.0, 0.0 },
# { xpos + w, ypos, 1.0, 1.0 },
# { xpos + w, ypos + h, 1.0, 0.0 }
# };
// t := glfw.get_time()
// Render glyph texture over quad
// t1 := glfw.get_time()
# glBindTexture(GL_TEXTURE_2D, ch.texture_id);
// Update content of VBO memory
gl.bind_buffer(GL_ARRAY_BUFFER, ctx.VBO)
// t2 := glfw.get_time()
// # glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
# glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
// t3 := glfw.get_time()
// gl.bind_buffer(GL_ARRAY_BUFFER, uint(0))
// t4 := glfw.get_time()
// Render quad
gl.draw_arrays(GL_TRIANGLES, 0, 6)
// t5 := glfw.get_time()
// # if (glfw__get_time() - t > 0.001)
// {
// # printf("do_text = %f '%s' \n", glfw__get_time() - t, text.str);
// # printf("t1=%f, t2=%f, t3=%f, t4=%f, t5=%f\n\n\n", t1-t, t2-t1, t3-t2, t4-t3, t5-t4);
// }
// Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
// Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
# x += (ch.advance >> 6) * 1;
}
gl.bind_vao(u32(0))
# glBindTexture(GL_TEXTURE_2D, 0);
// runes.free()
// #free(runes.data);
}
fn (ctx &GG) draw_text_def(x, y int, text string) {
cfg := gx.TextCfg {
color: gx.BLACK,
size: DEFAULT_FONT_SIZE,
align: gx.ALIGN_LEFT,
}
ctx.draw_text(x, y, text, cfg)
}
fn update() {
// # ui__post_empty_event();
}
pub fn (c GG) circle(x, y, r int) {
}
fn (c GG) fill_color(color gx.Color) {
}
fn (c GG) fill() {
}
fn (c GG) move_to(x, y int) {
}
fn (c GG) line_to(x, y int) {
}
fn (c GG) stroke_width(size int) {
}
fn (c GG) stroke_color(color gx.Color) {
}
fn (c GG) stroke() {
}
fn (c GG) save() {
}
fn (c GG) restore() {
}
fn (c GG) intersect_scissor(x, y, w, h int) {
}
fn (c GG) translate(x, y int) {
}
fn (c GG) create_font(name, file string) int {
return 0
}
fn (c GG) text(x, y int, text string) {
}
fn (c GG) text_box(x, y, max int, text string) {
}
fn (c GG) font_face(f string) {
}
fn (c GG) font_size(size int) {
}
fn (c GG) text_align(a int) {
}
pub fn create_image(file string) u32 {
println('gg create image "$file"')
if file.contains('twitch') {
return u32(0)// TODO
}
if !os.file_exists(file) {
println('gg create image no such file "$file"')
return u32(0)
}
texture := gl.gen_texture()
img := stbi.load(file)
gl.bind_2d_texture(texture)
img.tex_image_2d()
gl.generate_mipmap(GL_TEXTURE_2D)
img.free()
// println('gg end')
return texture
}
pub fn (ctx &GG) draw_line_c(x, y, x2, y2 int, color gx.Color) {
C.glDeleteBuffers(1, &ctx.VAO)
C.glDeleteBuffers(1, &ctx.VBO)
ctx.shader.use()
ctx.shader.set_color('color', color)
vertices := [float(x), float(y), float(x2), float(y2)] !
gl.bind_vao(ctx.VAO)
gl.set_vbo(ctx.VBO, vertices, GL_STATIC_DRAW)
gl.vertex_attrib_pointer(0, 2, GL_FLOAT, false, 2, 0)
gl.enable_vertex_attrib_array(0)
gl.bind_vao(ctx.VAO)
gl.draw_arrays(GL_LINES, 0, 2)
}
pub fn (c &GG) draw_line(x, y, x2, y2 int) {
c.draw_line_c(x, y, x2, y2, gx.GRAY)
}
pub fn (c &GG) draw_vertical(x, y, height int) {
c.draw_line(x, y, x, y + height)
}
// fn (ctx &GG) draw_image(x, y, w, h float, img stbi.Image) {
pub fn (ctx &GG) draw_image(x, y, w, h float, tex_id u32) {
// println('DRAW IMAGE $x $y $w $h $tex_id')
ctx.shader.use()
// ctx.shader.set_color('color', c)
ctx.shader.set_int('has_texture', 1)
// 4--1
// | |
// 3--2
vertices := [
x + w, y, 0, 1, 0, 0, 1, 1,
x + w, y + h, 0, 0, 1, 0, 1, 0,
x, y + h, 0, 0, 0, 1, 0, 0,
x, y, 0, 1, 1, 0, 0, 1,
] !
indices := [
0, 1, 3,// first triangle
1, 2, 3// second triangle
] !
// VAO := gl.gen_vertex_array()
// VBO := gl.gen_buffer()
gl.bind_vao(ctx.VAO)
gl.set_vbo(ctx.VBO, vertices, GL_STATIC_DRAW)
ebo := gl.gen_buffer()
gl.set_ebo(ebo, indices, GL_STATIC_DRAW)
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 8, 0)
gl.enable_vertex_attrib_array(0)
gl.vertex_attrib_pointer(1, 3, GL_FLOAT, false, 8, 3)
gl.enable_vertex_attrib_array(1)
gl.vertex_attrib_pointer(2, 2, GL_FLOAT, false, 8, 6)
gl.enable_vertex_attrib_array(2)
gl.bind_2d_texture(u32(tex_id))
gl.bind_vao(ctx.VAO)
gl.draw_elements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
}
pub fn (c &GG) draw_empty_rect(x, y, w, h int, color gx.Color) {
c.draw_line_c(x, y, x + w, y, color)
c.draw_line_c(x, y, x, y + h, color)
c.draw_line_c(x, y + h, x + w, y + h, color)
c.draw_line_c(x + w, y, x + w, y + h, color)
}