remove "import const" everywhere

pull/1714/head
Alexander Medvednikov 2019-08-23 00:00:31 +03:00
parent dcfc9eb1a1
commit f61b14584a
11 changed files with 172 additions and 308 deletions

View File

@ -135,7 +135,7 @@ fn main() {
}) })
ft: 0 ft: 0
} }
game.gg.window.set_user_ptr(game) // TODO remove this when `window_user_ptr:` works game.gg.window.set_user_ptr(game) // TODO remove this when `window_user_ptr:` works
game.init_game() game.init_game()
game.gg.window.onkeydown(key_down) game.gg.window.onkeydown(key_down)
go game.run() // Run the game loop in a new thread go game.run() // Run the game loop in a new thread
@ -374,7 +374,7 @@ fn key_down(wnd voidptr, key, code, action, mods int) {
switch key { switch key {
case glfw.KEY_ESCAPE: case glfw.KEY_ESCAPE:
glfw.set_should_close(wnd, true) glfw.set_should_close(wnd, true)
case GLFW_KEY_SPACE: case glfw.key_space:
if game.state == .running { if game.state == .running {
game.state = .paused game.state = .paused
} else if game.state == .paused { } else if game.state == .paused {

View File

@ -8,10 +8,6 @@ import math
#include <sys/syscall.h> #include <sys/syscall.h>
import const(
SYS_getrandom
)
// const ( // const (
// SYS_getrandom = 278 // AArch65 // SYS_getrandom = 278 // AArch65
// SYS_getrandom = 384 // ARM // SYS_getrandom = 384 // ARM
@ -47,5 +43,5 @@ fn _getrandom(bytes_needed int, buffer voidptr) int {
if bytes_needed > ReadBatchSize { if bytes_needed > ReadBatchSize {
panic('_getrandom() dont request more thane $ReadBatchSize bytes at once.') panic('_getrandom() dont request more thane $ReadBatchSize bytes at once.')
} }
return C.syscall(SYS_getrandom, buffer, bytes_needed, 0) return C.syscall(C.SYS_getrandom, buffer, bytes_needed, 0)
} }

View File

@ -6,20 +6,10 @@ module rand
#flag darwin -framework Security #flag darwin -framework Security
// import const (
// kSecRandomDefault
// errSecSuccess
// )
const (
kSecRandomDefault = 0
errSecSuccess = 0
)
pub fn read(bytes_needed int) ?[]byte { pub fn read(bytes_needed int) ?[]byte {
mut buffer := malloc(bytes_needed) mut buffer := malloc(bytes_needed)
status := C.SecRandomCopyBytes(kSecRandomDefault, bytes_needed, buffer) status := C.SecRandomCopyBytes(0, bytes_needed, buffer)
if status != errSecSuccess { if status != 0 {
return ReadError return ReadError
} }
return c_array_to_bytes_tmp(bytes_needed, buffer) return c_array_to_bytes_tmp(bytes_needed, buffer)

View File

@ -111,9 +111,9 @@ fn ft_load_char(_face Face, code i64) Character {
C.glTexImage2D(C.GL_TEXTURE_2D, 0, C.GL_RED, fgwidth, fgrows, C.glTexImage2D(C.GL_TEXTURE_2D, 0, C.GL_RED, fgwidth, fgrows,
0, C.GL_RED, C.GL_UNSIGNED_BYTE, face.glyph.bitmap.buffer) 0, C.GL_RED, C.GL_UNSIGNED_BYTE, face.glyph.bitmap.buffer)
// Set texture options // Set texture options
C.glTexParameteri(GL_TEXTURE_2D, C.GL_TEXTURE_WRAP_S, C.GL_CLAMP_TO_EDGE) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_WRAP_S, C.GL_CLAMP_TO_EDGE)
C.glTexParameteri(GL_TEXTURE_2D, C.GL_TEXTURE_WRAP_T, C.GL_CLAMP_TO_EDGE) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_WRAP_T, C.GL_CLAMP_TO_EDGE)
C.glTexParameteri(GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_LINEAR) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_LINEAR)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_LINEAR) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_LINEAR)
fgleft := face.glyph.bitmap_left fgleft := face.glyph.bitmap_left
fgtop := face.glyph.bitmap_top fgtop := face.glyph.bitmap_top
@ -146,7 +146,7 @@ pub fn new_context(cfg gg.Cfg) *Context {
gl.viewport(0, 0, width, height) gl.viewport(0, 0, width, height)
*/ */
// gl.enable(GL_CULL_FACE) // TODO NEED CULL? // gl.enable(GL_CULL_FACE) // TODO NEED CULL?
gl.enable(GL_BLEND) gl.enable(C.GL_BLEND)
C.glBlendFunc(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA) C.glBlendFunc(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA)
shader := gl.new_shader('text') shader := gl.new_shader('text')
shader.use() shader.use()
@ -209,10 +209,10 @@ pub fn new_context(cfg gg.Cfg) *Context {
println('new gg text context vao=$vao') println('new gg text context vao=$vao')
vbo := gl.gen_buffer() vbo := gl.gen_buffer()
gl.bind_vao(vao) gl.bind_vao(vao)
gl.bind_buffer(GL_ARRAY_BUFFER, vbo) gl.bind_buffer(C.GL_ARRAY_BUFFER, vbo)
// # glBufferData(GL_ARRAY_BUFFER, sizeof(GLf32) * 6 * 4, NULL, GL_DYNAMIC_DRAW); // # glBufferData(GL_ARRAY_BUFFER, sizeof(GLf32) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
gl.enable_vertex_attrib_array(0) gl.enable_vertex_attrib_array(0)
gl.vertex_attrib_pointer(0, 4, GL_FLOAT, false, 4, 0) gl.vertex_attrib_pointer(0, 4, C.GL_FLOAT, false, 4, 0)
// # glVertexAttribPointer(0, 4, GL_FLOAT,false, 4 * sizeof(GLf32), 0); // # glVertexAttribPointer(0, 4, GL_FLOAT,false, 4 * sizeof(GLf32), 0);
// gl.bind_buffer(GL_ARRAY_BUFFER, uint(0)) // gl.bind_buffer(GL_ARRAY_BUFFER, uint(0))
// # glBindVertexArray(0); // # glBindVertexArray(0);
@ -336,11 +336,11 @@ fn (ctx mut Context) _draw_text(_x, _y int, utext ustring, cfg gx.TextCfg) {
// Render glyph texture over quad // Render glyph texture over quad
C.glBindTexture(C.GL_TEXTURE_2D, ch.texture_id) C.glBindTexture(C.GL_TEXTURE_2D, ch.texture_id)
// Update content of VBO memory // Update content of VBO memory
gl.bind_buffer(GL_ARRAY_BUFFER, ctx.vbo) gl.bind_buffer(C.GL_ARRAY_BUFFER, ctx.vbo)
// glBufferSubData(..) // glBufferSubData(..)
C.glBufferData(GL_ARRAY_BUFFER, 96, vertices.data, C.GL_DYNAMIC_DRAW) C.glBufferData(C.GL_ARRAY_BUFFER, 96, vertices.data, C.GL_DYNAMIC_DRAW)
// Render quad // Render quad
gl.draw_arrays(GL_TRIANGLES, 0, 6) gl.draw_arrays(C.GL_TRIANGLES, 0, 6)
// Now advance cursors for next glyph (note that advance is number of 1/64 pixels) // 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)) // 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 >> u32(6) x += ch.advance >> u32(6)

View File

@ -5,30 +5,22 @@
module gg module gg
import stbi import stbi
import glm import glm
import gl import gl
import gx import gx
import os import os
import glfw import glfw
struct Vec2 { struct Vec2 {
pub: pub:
x int x int
y int y int
} }
import const (
GL_STATIC_DRAW
GL_FLOAT
GL_FALSE
GL_UNSIGNED_INT
GL_INT
)
pub fn vec2(x, y int) Vec2 { pub fn vec2(x, y int) Vec2 {
res := Vec2 { res := Vec2 {
x: x x: x
y: y y: y
} }
return res return res
} }
@ -41,19 +33,19 @@ pub fn init() {
struct Cfg { struct Cfg {
pub: pub:
width int width int
height int height int
use_ortho bool use_ortho bool
retina bool retina bool
font_size int font_size int
font_path string font_path string
create_window bool create_window bool
window_user_ptr voidptr window_user_ptr voidptr
window_title string window_title string
always_on_top bool always_on_top bool
scale int scale int
} }
struct GG { struct GG {
@ -68,29 +60,29 @@ struct GG {
line_vbo u32 line_vbo u32
vbo u32 vbo u32
scale int // retina = 2 , normal = 1 scale int // retina = 2 , normal = 1
pub mut: pub mut:
window *glfw.Window window *glfw.Window
render_fn fn() render_fn fn()
} }
// fn new_context(width, height int, use_ortho bool, font_size int) *GG { // fn new_context(width, height int, use_ortho bool, font_size int) *GG {
pub fn new_context(cfg Cfg) *GG { pub fn new_context(cfg Cfg) *GG {
mut window := &glfw.Window{!} mut window := &glfw.Window{!}
if cfg.create_window { if cfg.create_window {
window = glfw.create_window(glfw.WinCfg{ window = glfw.create_window(glfw.WinCfg{
title: cfg.window_title title: cfg.window_title
width: cfg.width width: cfg.width
height: cfg.height height: cfg.height
ptr: cfg.window_user_ptr ptr: cfg.window_user_ptr
always_on_top: cfg.always_on_top always_on_top: cfg.always_on_top
}) })
window.make_context_current() window.make_context_current()
init() init()
} }
shader := gl.new_shader('simple') shader := gl.new_shader('simple')
shader.use() shader.use()
if cfg.use_ortho { if cfg.use_ortho {
projection := glm.ortho(0, cfg.width, cfg.height, 0) projection := glm.ortho(0, cfg.width, cfg.height, 0)
shader.set_mat4('projection', projection) shader.set_mat4('projection', projection)
} }
@ -113,15 +105,15 @@ pub fn new_context(cfg Cfg) *GG {
//gl.bind_buffer(GL_ARRAY_BUFFER, VBO) //gl.bind_buffer(GL_ARRAY_BUFFER, VBO)
//gl.enable_vertex_attrib_array(0) //gl.enable_vertex_attrib_array(0)
//gl.vertex_attrib_pointer(0, 4, GL_FLOAT, false, 4, 0) //gl.vertex_attrib_pointer(0, 4, GL_FLOAT, false, 4, 0)
todo_remove_me(cfg, scale) todo_remove_me(cfg, scale)
return &GG { return &GG {
shader: shader shader: shader
width: cfg.width width: cfg.width
height: cfg.height height: cfg.height
vao: vao vao: vao
vbo: vbo vbo: vbo
window: window window: window
// /line_vao: gl.gen_vertex_array() // /line_vao: gl.gen_vertex_array()
// /line_vbo: gl.gen_buffer() // /line_vbo: gl.gen_buffer()
//text_ctx: new_context_text(cfg, scale), //text_ctx: new_context_text(cfg, scale),
@ -133,25 +125,25 @@ pub fn new_context(cfg Cfg) *GG {
//return ctx //return ctx
} }
/* /*
pub fn (gg &GG) render_loop() bool { pub fn (gg &GG) render_loop() bool {
for !gg.window.show_close() { for !gg.window.show_close() {
gg.render_fn() gg.render_fn()
gg.window.swap_buffers() gg.window.swap_buffers()
glfw.wait_events() glfw.wait_events()
} }
} }
*/ */
pub fn clear(color gx.Color) { pub fn clear(color gx.Color) {
gl.clear() gl.clear()
gl.clear_color(255, 255, 255, 255) gl.clear_color(255, 255, 255, 255)
} }
pub fn (gg &GG) render() { pub fn (gg &GG) render() {
gg.window.swap_buffers() gg.window.swap_buffers()
glfw.wait_events() glfw.wait_events()
} }
pub fn (ctx &GG) draw_triangle(x1, y1, x2, y2, x3, y3 f32, c gx.Color) { pub fn (ctx &GG) draw_triangle(x1, y1, x2, y2, x3, y3 f32, c gx.Color) {
// println('draw_triangle $x1,$y1 $x2,$y2 $x3,$y3') // println('draw_triangle $x1,$y1 $x2,$y2 $x3,$y3')
@ -165,8 +157,8 @@ pub fn (ctx &GG) draw_triangle(x1, y1, x2, y2, x3, y3 f32, c gx.Color) {
// bind the Vertex Array Object first, then bind and set vertex buffer(s), // bind the Vertex Array Object first, then bind and set vertex buffer(s),
// and then configure vertex attributes(s). // and then configure vertex attributes(s).
gl.bind_vao(ctx.vao) gl.bind_vao(ctx.vao)
gl.set_vbo(ctx.vbo, vertices, GL_STATIC_DRAW) gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 3, 0) gl.vertex_attrib_pointer(0, 3, C.GL_FLOAT, false, 3, 0)
gl.enable_vertex_attrib_array(0) gl.enable_vertex_attrib_array(0)
// gl.bind_buffer(GL_ARRAY_BUFFER, uint(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, // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO,
@ -175,7 +167,7 @@ pub fn (ctx &GG) draw_triangle(x1, y1, x2, y2, x3, y3 f32, c gx.Color) {
// (nor VBOs) when it's not directly necessary. // (nor VBOs) when it's not directly necessary.
// gl.bind_vertex_array(uint(0)) // gl.bind_vertex_array(uint(0))
// gl.bind_vertex_array(ctx.VAO) // gl.bind_vertex_array(ctx.VAO)
gl.draw_arrays(GL_TRIANGLES, 0, 3) gl.draw_arrays(C.GL_TRIANGLES, 0, 3)
} }
pub fn (ctx &GG) draw_triangle_tex(x1, y1, x2, y2, x3, y3 f32, c gx.Color) { pub fn (ctx &GG) draw_triangle_tex(x1, y1, x2, y2, x3, y3 f32, c gx.Color) {
@ -188,19 +180,19 @@ pub fn (ctx &GG) draw_triangle_tex(x1, y1, x2, y2, x3, y3 f32, c gx.Color) {
x3, y3, 0, 0, 0, 0, 0, 0, x3, y3, 0, 0, 0, 0, 0, 0,
] ! ] !
gl.bind_vao(ctx.vao) gl.bind_vao(ctx.vao)
gl.set_vbo(ctx.vbo, vertices, GL_STATIC_DRAW) gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
// position attribute // position attribute
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 3, 0) gl.vertex_attrib_pointer(0, 3, C.GL_FLOAT, false, 3, 0)
gl.enable_vertex_attrib_array(0) gl.enable_vertex_attrib_array(0)
// color attribute // color attribute
gl.vertex_attrib_pointer(1, 3, GL_FLOAT, false, 8, 3) gl.vertex_attrib_pointer(1, 3, C.GL_FLOAT, false, 8, 3)
gl.enable_vertex_attrib_array(1) gl.enable_vertex_attrib_array(1)
// texture attribute // texture attribute
gl.vertex_attrib_pointer(2, 2, GL_FLOAT, false, 8, 6) gl.vertex_attrib_pointer(2, 2, C.GL_FLOAT, false, 8, 6)
gl.enable_vertex_attrib_array(2) gl.enable_vertex_attrib_array(2)
// / // /
// gl.draw_arrays(GL_TRIANGLES, 0, 3) // gl.draw_arrays(GL_TRIANGLES, 0, 3)
gl.draw_elements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0) gl.draw_elements(C.GL_TRIANGLES, 6, C.GL_UNSIGNED_INT, 0)
} }
pub fn (ctx &GG) draw_rect(x, y, w, h f32, c gx.Color) { pub fn (ctx &GG) draw_rect(x, y, w, h f32, c gx.Color) {
@ -214,9 +206,9 @@ pub fn (ctx &GG) draw_rect(x, y, w, h f32, c gx.Color) {
ctx.draw_rect2(x, y, w, h, c) ctx.draw_rect2(x, y, w, h, c)
} }
/* /*
fn (ctx mut GG) init_rect_vao() { fn (ctx mut GG) init_rect_vao() {
ctx.rect_vao = gl.gen_vertex_array() ctx.rect_vao = gl.gen_vertex_array()
ctx.rect_vbo = gl.gen_buffer() ctx.rect_vbo = gl.gen_buffer()
vertices := [ vertices := [
@ -230,11 +222,11 @@ fn (ctx mut GG) init_rect_vao() {
1, 2, 3// second triangle 1, 2, 3// second triangle
] ! ] !
gl.bind_vao(ctx.rect_vao) gl.bind_vao(ctx.rect_vao)
gl.set_vbo(ctx.rect_vbo, vertices, GL_STATIC_DRAW) gl.set_vbo(ctx.rect_vbo, vertices, C.GL_STATIC_DRAW)
ebo := gl.gen_buffer() ebo := gl.gen_buffer()
// /////// // ///////
gl.set_ebo(ebo, indices, GL_STATIC_DRAW) gl.set_ebo(ebo, indices, C.GL_STATIC_DRAW)
} }
*/ */
pub fn (ctx &GG) draw_rect2(x, y, w, h f32, c gx.Color) { pub fn (ctx &GG) draw_rect2(x, y, w, h f32, c gx.Color) {
C.glDeleteBuffers(1, &ctx.vao) C.glDeleteBuffers(1, &ctx.vao)
@ -244,9 +236,9 @@ pub fn (ctx &GG) draw_rect2(x, y, w, h f32, c gx.Color) {
ctx.shader.set_int('has_texture', 0) ctx.shader.set_int('has_texture', 0)
// 4--1 // 4--1
// 3--2 // 3--2
$if linux { $if linux {
// y += h // y += h
} }
vertices := [ vertices := [
x + w, y, 0, x + w, y, 0,
x + w, y + h, 0, x + w, y + h, 0,
@ -258,20 +250,20 @@ pub fn (ctx &GG) draw_rect2(x, y, w, h f32, c gx.Color) {
1, 2, 3// second triangle 1, 2, 3// second triangle
] ! ] !
gl.bind_vao(ctx.vao) gl.bind_vao(ctx.vao)
gl.set_vbo(ctx.vbo, vertices, GL_STATIC_DRAW) gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
ebo := gl.gen_buffer() ebo := gl.gen_buffer()
// /////// // ///////
gl.set_ebo(ebo, indices, GL_STATIC_DRAW)// !!! LEAKS gl.set_ebo(ebo, indices, C.GL_STATIC_DRAW)// !!! LEAKS
// ///// // /////
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 3, 0) gl.vertex_attrib_pointer(0, 3, C.GL_FLOAT, false, 3, 0)
gl.enable_vertex_attrib_array(0) gl.enable_vertex_attrib_array(0)
// gl.bind_vao(ctx.rect_vao) // gl.bind_vao(ctx.rect_vao)
gl.bind_vao(ctx.vao) gl.bind_vao(ctx.vao)
gl.draw_elements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0) gl.draw_elements(C.GL_TRIANGLES, 6, C.GL_UNSIGNED_INT, 0)
C.glDeleteBuffers(1, &ebo) C.glDeleteBuffers(1, &ebo)
} }
fn todo_remove_me(cfg Cfg, scale int) { fn todo_remove_me(cfg Cfg, scale int) {
// Can only have text in ortho mode // Can only have text in ortho mode
if !cfg.use_ortho { if !cfg.use_ortho {
return return
@ -279,8 +271,8 @@ fn todo_remove_me(cfg Cfg, scale int) {
mut width := cfg.width * scale mut width := cfg.width * scale
mut height := cfg.height * scale mut height := cfg.height * scale
font_size := cfg.font_size * scale font_size := cfg.font_size * scale
gl.enable(GL_BLEND) gl.enable(C.GL_BLEND)
//# glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //# glBlendFunc(C.GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
shader := gl.new_shader('text') shader := gl.new_shader('text')
shader.use() shader.use()
projection := glm.ortho(0, width, 0, height)// 0 at BOT projection := glm.ortho(0, width, 0, height)// 0 at BOT
@ -291,18 +283,18 @@ fn todo_remove_me(cfg Cfg, scale int) {
//println('new gg text context VAO=$VAO') //println('new gg text context VAO=$VAO')
vbo := gl.gen_buffer() vbo := gl.gen_buffer()
gl.bind_vao(vao) gl.bind_vao(vao)
gl.bind_buffer(GL_ARRAY_BUFFER, vbo) gl.bind_buffer(C.GL_ARRAY_BUFFER, vbo)
gl.enable_vertex_attrib_array(0) gl.enable_vertex_attrib_array(0)
gl.vertex_attrib_pointer(0, 4, GL_FLOAT, false, 4, 0) gl.vertex_attrib_pointer(0, 4, C.GL_FLOAT, false, 4, 0)
} }
fn update() { fn update() {
// # ui__post_empty_event(); // # ui__post_empty_event();
} }
pub fn post_empty_event() { pub fn post_empty_event() {
glfw.post_empty_event() glfw.post_empty_event()
} }
pub fn (c GG) circle(x, y, r int) { pub fn (c GG) circle(x, y, r int) {
} }
@ -372,7 +364,7 @@ pub fn create_image(file string) u32 {
img := stbi.load(file) img := stbi.load(file)
gl.bind_2d_texture(texture) gl.bind_2d_texture(texture)
img.tex_image_2d() img.tex_image_2d()
gl.generate_mipmap(GL_TEXTURE_2D) gl.generate_mipmap(C.GL_TEXTURE_2D)
img.free() img.free()
// println('gg end') // println('gg end')
return texture return texture
@ -385,11 +377,11 @@ pub fn (ctx &GG) draw_line_c(x, y, x2, y2 f32, color gx.Color) {
ctx.shader.set_color('color', color) ctx.shader.set_color('color', color)
vertices := [f32(x), f32(y), f32(x2), f32(y2)] ! vertices := [f32(x), f32(y), f32(x2), f32(y2)] !
gl.bind_vao(ctx.vao) gl.bind_vao(ctx.vao)
gl.set_vbo(ctx.vbo, vertices, GL_STATIC_DRAW) gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
gl.vertex_attrib_pointer(0, 2, GL_FLOAT, false, 2, 0) gl.vertex_attrib_pointer(0, 2, C.GL_FLOAT, false, 2, 0)
gl.enable_vertex_attrib_array(0) gl.enable_vertex_attrib_array(0)
gl.bind_vao(ctx.vao) gl.bind_vao(ctx.vao)
gl.draw_arrays(GL_LINES, 0, 2) gl.draw_arrays(C.GL_LINES, 0, 2)
} }
pub fn (c &GG) draw_line(x, y, x2, y2 f32) { pub fn (c &GG) draw_line(x, y, x2, y2 f32) {
@ -425,18 +417,18 @@ pub fn (ctx &GG) draw_image(x, y, w, h f32, tex_id u32) {
// VAO := gl.gen_vertex_array() // VAO := gl.gen_vertex_array()
// VBO := gl.gen_buffer() // VBO := gl.gen_buffer()
gl.bind_vao(ctx.vao) gl.bind_vao(ctx.vao)
gl.set_vbo(ctx.vbo, vertices, GL_STATIC_DRAW) gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
ebo := gl.gen_buffer() ebo := gl.gen_buffer()
gl.set_ebo(ebo, indices, GL_STATIC_DRAW) gl.set_ebo(ebo, indices, C.GL_STATIC_DRAW)
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 8, 0) gl.vertex_attrib_pointer(0, 3, C.GL_FLOAT, false, 8, 0)
gl.enable_vertex_attrib_array(0) gl.enable_vertex_attrib_array(0)
gl.vertex_attrib_pointer(1, 3, GL_FLOAT, false, 8, 3) gl.vertex_attrib_pointer(1, 3, C.GL_FLOAT, false, 8, 3)
gl.enable_vertex_attrib_array(1) gl.enable_vertex_attrib_array(1)
gl.vertex_attrib_pointer(2, 2, GL_FLOAT, false, 8, 6) gl.vertex_attrib_pointer(2, 2, C.GL_FLOAT, false, 8, 6)
gl.enable_vertex_attrib_array(2) gl.enable_vertex_attrib_array(2)
gl.bind_2d_texture(u32(tex_id)) gl.bind_2d_texture(u32(tex_id))
gl.bind_vao(ctx.vao) gl.bind_vao(ctx.vao)
gl.draw_elements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0) gl.draw_elements(C.GL_TRIANGLES, 6, C.GL_UNSIGNED_INT, 0)
} }
pub fn (c &GG) draw_empty_rect(x, y, w, h int, color gx.Color) { pub fn (c &GG) draw_empty_rect(x, y, w, h int, color gx.Color) {

View File

@ -9,15 +9,6 @@ import gx
import glm import glm
// import darwin // import darwin
import const (
GL_VERTEX_SHADER
GL_FRAGMENT_SHADER
GL_ARRAY_BUFFER
GL_TRIANGLES
GL_CULL_FACE
GL_BLEND
GL_LINES
)
struct Shader { struct Shader {
program_id int program_id int
@ -43,7 +34,7 @@ uniform sampler2D text;
uniform vec3 textColor; uniform vec3 textColor;
void main() void main()
{ {
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);
color = vec4(textColor, 1.0) * sampled; color = vec4(textColor, 1.0) * sampled;
} ' } '
@ -51,11 +42,11 @@ void main()
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor; layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord; layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor; out vec3 ourColor;
out vec2 TexCoord; out vec2 TexCoord;
uniform mat4 projection; uniform mat4 projection;
@ -64,8 +55,8 @@ void main() {
// gl_Position = vec4(aPos, 1.0); // gl_Position = vec4(aPos, 1.0);
ourColor = aColor; ourColor = aColor;
//TexCoord = vec2(aTexCoord.x, aTexCoord.y); //TexCoord = vec2(aTexCoord.x, aTexCoord.y);
TexCoord = aTexCoord; TexCoord = aTexCoord;
} }
' '
SIMPLE_FRAG = '#version 330 core SIMPLE_FRAG = '#version 330 core
@ -73,30 +64,30 @@ void main() {
out vec4 FragColor; out vec4 FragColor;
uniform vec3 color; uniform vec3 color;
uniform bool has_texture; uniform bool has_texture;
in vec3 ourColor; in vec3 ourColor;
in vec2 TexCoord; in vec2 TexCoord;
uniform sampler2D ourTexture; uniform sampler2D ourTexture;
void main() { void main() {
// FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); // FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
// FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f); // FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
if (has_texture) { if (has_texture) {
FragColor = texture(ourTexture, TexCoord); FragColor = texture(ourTexture, TexCoord);
} else { } else {
FragColor = vec4(color, 1.0f); FragColor = vec4(color, 1.0f);
} }
} }
' '
) )
pub fn new_shader(name string) Shader { pub fn new_shader(name string) Shader {
// TODO This is not used, remove // TODO This is not used, remove
mut dir := '' mut dir := ''
// Already have absolute path // Already have absolute path
if name.starts_with('/') { if name.starts_with('/') {
dir = '' dir = ''
@ -118,7 +109,7 @@ pub fn new_shader(name string) Shader {
fragment_src = SIMPLE_FRAG fragment_src = SIMPLE_FRAG
} }
// //////////////////////////////////////// // ////////////////////////////////////////
vertex_shader := gl.create_shader(GL_VERTEX_SHADER) vertex_shader := gl.create_shader(C.GL_VERTEX_SHADER)
gl.shader_source(vertex_shader, 1, vertex_src, 0) gl.shader_source(vertex_shader, 1, vertex_src, 0)
gl.compile_shader(vertex_shader) gl.compile_shader(vertex_shader)
if gl.shader_compile_status(vertex_shader) == 0 { if gl.shader_compile_status(vertex_shader) == 0 {
@ -130,7 +121,7 @@ pub fn new_shader(name string) Shader {
} }
// fragment shader // fragment shader
// fragment_src := os.read_file(fragment_path.trim_space()) // fragment_src := os.read_file(fragment_path.trim_space())
fragment_shader := gl.create_shader(GL_FRAGMENT_SHADER) fragment_shader := gl.create_shader(C.GL_FRAGMENT_SHADER)
gl.shader_source(fragment_shader, 1, fragment_src, 0) gl.shader_source(fragment_shader, 1, fragment_src, 0)
gl.compile_shader(fragment_shader) gl.compile_shader(fragment_shader)
if gl.shader_compile_status(fragment_shader) == 0 { if gl.shader_compile_status(fragment_shader) == 0 {

View File

@ -4,24 +4,9 @@
module gl module gl
import const (
GL_TEXTURE_2D
GL_TEXTURE0
GL_FLOAT
GL_VERTEX_SHADER
GL_ELEMENT_ARRAY_BUFFER
GL_DEPTH_TEST
GL_COLOR_BUFFER_BIT
GL_DEPTH_BUFFER_BIT
GL_STENCIL_BUFFER_BIT
GL_COMPILE_STATUS
GL_LINK_STATUS
GL_ARRAY_BUFFER
)
#flag -I @VROOT/thirdparty/glad #flag -I @VROOT/thirdparty/glad
#include "glad.h" #include "glad.h"
#flag @VROOT/thirdparty/glad/glad.o #flag @VROOT/thirdparty/glad/glad.o
pub fn init_glad() { pub fn init_glad() {
ok := C.gladLoadGL() ok := C.gladLoadGL()
@ -40,7 +25,7 @@ pub fn clear_color(r, g, b, a int) {
} }
pub fn clear() { pub fn clear() {
C.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT) C.glClear(C.GL_COLOR_BUFFER_BIT | C.GL_DEPTH_BUFFER_BIT | C.GL_STENCIL_BUFFER_BIT)
} }
pub fn create_shader(typ int) int { pub fn create_shader(typ int) int {
@ -61,7 +46,7 @@ pub fn compile_shader(shader int) {
pub fn shader_compile_status(shader int) int { pub fn shader_compile_status(shader int) int {
success := 0 success := 0
C.glGetShaderiv(shader, GL_COMPILE_STATUS, &success) C.glGetShaderiv(shader, C.GL_COMPILE_STATUS, &success)
return success return success
} }
@ -76,7 +61,7 @@ pub fn link_program(program int) {
pub fn get_program_link_status(program int) int { pub fn get_program_link_status(program int) int {
success := 0 success := 0
C.glGetProgramiv(program, GL_LINK_STATUS, &success) C.glGetProgramiv(program, C.GL_LINK_STATUS, &success)
return success return success
} }
@ -115,7 +100,7 @@ pub fn active_texture(t int) {
} }
pub fn bind_2d_texture(texture u32) { pub fn bind_2d_texture(texture u32) {
C.glBindTexture(GL_TEXTURE_2D, texture) C.glBindTexture(C.GL_TEXTURE_2D, texture)
} }
pub fn delete_texture(texture u32) { pub fn delete_texture(texture u32) {
@ -137,14 +122,14 @@ pub fn buffer_data_f32(typ int, vertices []f32, draw_typ int) {
} }
pub fn set_vbo(vbo u32, vertices []f32, draw_typ int) { pub fn set_vbo(vbo u32, vertices []f32, draw_typ int) {
gl.bind_buffer(GL_ARRAY_BUFFER, vbo) gl.bind_buffer(C.GL_ARRAY_BUFFER, vbo)
gl.buffer_data_f32(GL_ARRAY_BUFFER, vertices, draw_typ) gl.buffer_data_f32(C.GL_ARRAY_BUFFER, vertices, draw_typ)
} }
pub fn set_ebo(ebo u32, indices []int, draw_typ int) { pub fn set_ebo(ebo u32, indices []int, draw_typ int) {
gl.bind_buffer(GL_ELEMENT_ARRAY_BUFFER, ebo) gl.bind_buffer(C.GL_ELEMENT_ARRAY_BUFFER, ebo)
// gl.buffer_data_int(GL_ELEMENT_ARRAY_BUFFER, indices, draw_typ) // gl.buffer_data_int(GL_ELEMENT_ARRAY_BUFFER, indices, draw_typ)
gl.buffer_data_int(GL_ELEMENT_ARRAY_BUFFER, indices, draw_typ) gl.buffer_data_int(C.GL_ELEMENT_ARRAY_BUFFER, indices, draw_typ)
} }
// ///////////////////// // /////////////////////
@ -166,7 +151,7 @@ pub fn use_program(program int) {
pub fn gen_vertex_array() u32 { pub fn gen_vertex_array() u32 {
vao := u32(0) vao := u32(0)
C.glGenVertexArrays(1, &vao) C.glGenVertexArrays(1, &vao)
return vao return vao
} }
pub fn enable_vertex_attrib_array(n int) { pub fn enable_vertex_attrib_array(n int) {
@ -176,13 +161,13 @@ pub fn enable_vertex_attrib_array(n int) {
pub fn gen_buffer() u32 { pub fn gen_buffer() u32 {
vbo := u32(0) vbo := u32(0)
C.glGenBuffers(1, &vbo) C.glGenBuffers(1, &vbo)
return vbo return vbo
} }
pub fn vertex_attrib_pointer(index, size int, typ int, normalized bool, _stride int, _ptr int) { pub fn vertex_attrib_pointer(index, size int, typ int, normalized bool, _stride int, _ptr int) {
mut stride := _stride mut stride := _stride
mut ptr := _ptr mut ptr := _ptr
if typ == GL_FLOAT { if typ == C.GL_FLOAT {
stride *= sizeof(f32) stride *= sizeof(f32)
ptr *= sizeof(f32) ptr *= sizeof(f32)
} }
@ -190,7 +175,7 @@ pub fn vertex_attrib_pointer(index, size int, typ int, normalized bool, _stride
} }
pub fn tex_param(key, val int) { pub fn tex_param(key, val int) {
C.glTexParameteri(GL_TEXTURE_2D, key, val) C.glTexParameteri(C.GL_TEXTURE_2D, key, val)
} }
pub fn enable(val int) { pub fn enable(val int) {

View File

@ -6,8 +6,8 @@ module glfw
import gl import gl
#flag -I @VROOT/thirdparty/glfw #flag -I @VROOT/thirdparty/glfw
#flag -L @VROOT/thirdparty/glfw #flag -L @VROOT/thirdparty/glfw
// Debugging a custom build // Debugging a custom build
//-#flag darwin -L/var/tmp/glfw/src/ //-#flag darwin -L/var/tmp/glfw/src/
@ -17,7 +17,7 @@ import gl
#flag darwin -lglfw #flag darwin -lglfw
#flag linux -lglfw #flag linux -lglfw
#flag windows -lglfw3 #flag windows -lglfw3
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
// #flag darwin -framework Carbon // #flag darwin -framework Carbon
// #flag darwin -framework Cocoa // #flag darwin -framework Cocoa
@ -28,65 +28,9 @@ const (
DECORATED = 2 DECORATED = 2
) )
import const (
GLFW_RESIZABLE
GLFW_DECORATED
GLFW_FLOATING
)
import const (
GLFW_KEY_SPACE
GLFW_KEY_A
GLFW_KEY_B
GLFW_KEY_P
GLFW_KEY_F
GLFW_KEY_M
GLFW_KEY_L
GLFW_KEY_V
GLFW_KEY_R
GLFW_KEY_D
GLFW_KEY_7
GLFW_KEY_Z
GLFW_KEY_UP
GLFW_KEY_DOWN
GLFW_KEY_LEFT
GLFW_KEY_RIGHT
GLFW_KEY_BACKSPACE
GLFW_KEY_ENTER
GLFW_KEY_ESCAPE
GLFW_KEY_N
GLFW_KEY_PERIOD
GLFW_KEY_SLASH
GLFW_KEY_F5
GLFW_KEY_F6
GLFW_KEY_MINUS
GLFW_KEY_EQUAL
GLFW_KEY_C
GLFW_KEY_G
GLFW_KEY_I
GLFW_KEY_J
GLFW_KEY_E
GLFW_KEY_K
GLFW_KEY_O
GLFW_KEY_T
GLFW_KEY_H
GLFW_KEY_L
GLFW_KEY_N
GLFW_KEY_U
GLFW_KEY_X
GLFW_KEY_W
GLFW_KEY_Y
GLFW_KEY_Q
GLFW_KEY_RIGHT_BRACKET
GLFW_KEY_LEFT_BRACKET
GLFW_KEY_8
GLFW_KEY_TAB
GLFW_KEY_COMMA
GLFW_KEY_QUESTION
)
const ( const (
KEY_ESCAPE = 256 KEY_ESCAPE = 256
key_space = 32
KEY_LEFT_SUPER = 343 KEY_LEFT_SUPER = 343
) )
@ -106,7 +50,7 @@ struct WinCfg {
is_modal int is_modal int
is_browser bool is_browser bool
url string url string
always_on_top bool always_on_top bool
} }
// data *C.GLFWwindow // data *C.GLFWwindow
@ -134,10 +78,10 @@ type clickpubfn fn (window voidptr, button, action, mods int)
pub fn init() { pub fn init() {
C.glfwInit() C.glfwInit()
C.glfwWindowHint(C.GLFW_CONTEXT_VERSION_MAJOR, 3) C.glfwWindowHint(C.GLFW_CONTEXT_VERSION_MAJOR, 3)
C.glfwWindowHint(C.GLFW_CONTEXT_VERSION_MINOR, 3) C.glfwWindowHint(C.GLFW_CONTEXT_VERSION_MINOR, 3)
C.glfwWindowHint(C.GLFW_OPENGL_FORWARD_COMPAT, C.GL_TRUE) C.glfwWindowHint(C.GLFW_OPENGL_FORWARD_COMPAT, C.GL_TRUE)
C.glfwWindowHint(C.GLFW_OPENGL_PROFILE, C.GLFW_OPENGL_CORE_PROFILE) C.glfwWindowHint(C.GLFW_OPENGL_PROFILE, C.GLFW_OPENGL_CORE_PROFILE)
} }
pub fn (w &Window) destroy() { pub fn (w &Window) destroy() {
@ -159,14 +103,14 @@ pub fn window_hint(key, val int) {
pub fn create_window(c WinCfg) *Window { pub fn create_window(c WinCfg) *Window {
if c.borderless { if c.borderless {
window_hint(GLFW_RESIZABLE, 0) window_hint(C.GLFW_RESIZABLE, 0)
window_hint(GLFW_DECORATED, 0) window_hint(C.GLFW_DECORATED, 0)
} }
if c.always_on_top { if c.always_on_top {
window_hint(GLFW_FLOATING, 1) window_hint(C.GLFW_FLOATING, 1)
} }
cwindow := C.glfwCreateWindow(c.width, c.height, c.title.str, 0, 0) cwindow := C.glfwCreateWindow(c.width, c.height, c.title.str, 0, 0)
if isnil(cwindow) { if isnil(cwindow) {
println('failed to create glfw window') println('failed to create glfw window')
C.glfwTerminate() C.glfwTerminate()
} }
@ -248,7 +192,7 @@ pub fn get_time() f64 {
} }
pub fn key_pressed(wnd voidptr, key int) bool { pub fn key_pressed(wnd voidptr, key int) bool {
return int(C.glfwGetKey(wnd, key)) == C.GLFW_PRESS return int(C.glfwGetKey(wnd, key)) == C.GLFW_PRESS
} }
pub fn (w &Window) get_clipboard_text() string { pub fn (w &Window) get_clipboard_text() string {
@ -279,15 +223,15 @@ pub fn (w &Window) set_user_ptr(ptr voidptr) {
struct C.GLFWvidmode { struct C.GLFWvidmode {
width int width int
height int height int
} }
pub fn C.glfwGetVideoMode() *C.GLFWvidmode pub fn C.glfwGetVideoMode() *C.GLFWvidmode
pub fn get_monitor_size() Size { pub fn get_monitor_size() Size {
//# GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); //# GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
mode := C.glfwGetVideoMode(C.glfwGetPrimaryMonitor()) mode := C.glfwGetVideoMode(C.glfwGetPrimaryMonitor())
return Size{mode.width, mode.height} return Size{mode.width, mode.height}
} }
pub fn (size Size) str() string { pub fn (size Size) str() string {

View File

@ -8,24 +8,6 @@ pub:
proto int proto int
} }
import const (
AF_INET
AF_INET6
AF_UNSPEC
SOCK_STREAM
SOCK_DGRAM
IPPROTO_UDP
SOL_SOCKET
SO_REUSEADDR
SO_REUSEPORT
INADDR_ANY
AI_PASSIVE
SHUT_RD
SHUT_WR
SHUT_RDWR
SD_BOTH
)
struct C.WSAData { struct C.WSAData {
mut: mut:
wVersion u16 wVersion u16
@ -81,10 +63,10 @@ pub fn socket(family int, _type int, proto int) ?Socket {
} }
sockfd := C.socket(family, _type, proto) sockfd := C.socket(family, _type, proto)
one:=1 one:=1
// This is needed so that there are no problems with reusing the // This is needed so that there are no problems with reusing the
// same port after the application exits. // same port after the application exits.
C.setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int)) C.setsockopt(sockfd, C.SOL_SOCKET, C.SO_REUSEADDR, &one, sizeof(int))
if sockfd == 0 { if sockfd == 0 {
return error('socket: init failed') return error('socket: init failed')
} }
@ -98,7 +80,7 @@ pub fn socket(family int, _type int, proto int) ?Socket {
} }
pub fn socket_udp() ?Socket { pub fn socket_udp() ?Socket {
return socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) return socket(C.AF_INET, C.SOCK_DGRAM, C.IPPROTO_UDP)
} }
// set socket options // set socket options
@ -115,7 +97,7 @@ pub fn (s Socket) bind(port int) ?int {
mut addr := C.sockaddr_in{} mut addr := C.sockaddr_in{}
addr.sin_family = s.family addr.sin_family = s.family
addr.sin_port = C.htons(port) addr.sin_port = C.htons(port)
addr.sin_addr.s_addr = C.htonl(INADDR_ANY) addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
size := 16 // sizeof(C.sockaddr_in) size := 16 // sizeof(C.sockaddr_in)
res := int(C.bind(s.sockfd, &addr, size)) res := int(C.bind(s.sockfd, &addr, size))
if res < 0 { if res < 0 {
@ -127,14 +109,14 @@ pub fn (s Socket) bind(port int) ?int {
// put socket into passive mode and wait to receive // put socket into passive mode and wait to receive
pub fn (s Socket) listen() ?int { pub fn (s Socket) listen() ?int {
backlog := 128 backlog := 128
res := int(C.listen(s.sockfd, backlog)) res := int(C.listen(s.sockfd, backlog))
if res < 0 { if res < 0 {
return error('socket: listen failed') return error('socket: listen failed')
} }
$if debug { $if debug {
println('listen res = $res') println('listen res = $res')
} }
return res return res
} }
// put socket into passive mode with user specified backlog and wait to receive // put socket into passive mode with user specified backlog and wait to receive
@ -155,7 +137,7 @@ pub fn listen(port int) ?Socket {
$if debug { $if debug {
println('net.listen($port)') println('net.listen($port)')
} }
s := socket(AF_INET, SOCK_STREAM, 0) or { s := socket(C.AF_INET, C.SOCK_STREAM, 0) or {
return error(err) return error(err)
} }
bind_res := s.bind(port) or { bind_res := s.bind(port) or {
@ -190,9 +172,9 @@ pub fn (s Socket) accept() ?Socket {
// connect to given addrress and port // connect to given addrress and port
pub fn (s Socket) connect(address string, port int) ?int { pub fn (s Socket) connect(address string, port int) ?int {
mut hints := C.addrinfo{} mut hints := C.addrinfo{}
hints.ai_family = AF_UNSPEC hints.ai_family = C.AF_UNSPEC
hints.ai_socktype = SOCK_STREAM hints.ai_socktype = C.SOCK_STREAM
hints.ai_flags = AI_PASSIVE hints.ai_flags = C.AI_PASSIVE
info := &C.addrinfo{!} info := &C.addrinfo{!}
sport := '$port' sport := '$port'
@ -209,7 +191,7 @@ pub fn (s Socket) connect(address string, port int) ?int {
// helper method to create socket and connect // helper method to create socket and connect
pub fn dial(address string, port int) ?Socket { pub fn dial(address string, port int) ?Socket {
s := socket(AF_INET, SOCK_STREAM, 0) or { s := socket(C.AF_INET, C.SOCK_STREAM, 0) or {
return error(err) return error(err)
} }
res := s.connect(address, port) or { res := s.connect(address, port) or {
@ -251,10 +233,10 @@ pub fn (s Socket) crecv( buffer byteptr, buffersize int ) int {
pub fn (s Socket) close() ?int { pub fn (s Socket) close() ?int {
mut shutdown_res := 0 mut shutdown_res := 0
$if windows { $if windows {
shutdown_res = C.shutdown(s.sockfd, SD_BOTH) shutdown_res = C.shutdown(s.sockfd, C.SD_BOTH)
} }
$else { $else {
shutdown_res = C.shutdown(s.sockfd, SHUT_RDWR) shutdown_res = C.shutdown(s.sockfd, C.SHUT_RDWR)
} }
// TODO: should shutdown throw an error? close will // TODO: should shutdown throw an error? close will
// continue even if shutdown failed // continue even if shutdown failed
@ -276,14 +258,14 @@ pub fn (s Socket) close() ?int {
return 0 return 0
} }
const ( const (
MAX_READ = 400 MAX_READ = 400
) )
pub fn (s Socket) write(str string) { pub fn (s Socket) write(str string) {
line := '$str\r\n' line := '$str\r\n'
C.write(s.sockfd, line.str, line.len) C.write(s.sockfd, line.str, line.len)
} }
pub fn (s Socket) read_line() string { pub fn (s Socket) read_line() string {
mut res := '' mut res := ''
for { for {
@ -307,7 +289,7 @@ pub fn (s Socket) read_line() string {
} }
// println('resp len=$numbytes') // println('resp len=$numbytes')
buf[n] = `\0` buf[n] = `\0`
// C.printf('!!buf= "%s" n=%d\n', buf,n) // C.printf('!!buf= "%s" n=%d\n', buf,n)
line := string(buf) line := string(buf)
res += line res += line
// Reached a newline. That's an end of an IRC message // Reached a newline. That's an end of an IRC message
@ -324,4 +306,4 @@ pub fn (s Socket) read_line() string {
return res return res
} }

View File

@ -18,10 +18,6 @@ pub:
vals []string vals []string
} }
import const (
CONNECTION_OK
)
struct C.PGResult { } struct C.PGResult { }
struct Config { struct Config {
@ -41,7 +37,7 @@ pub fn connect(config pg.Config) DB {
conninfo := 'host=$config.host user=$config.user dbname=$config.dbname' conninfo := 'host=$config.host user=$config.user dbname=$config.dbname'
conn:=C.PQconnectdb(conninfo.str) conn:=C.PQconnectdb(conninfo.str)
status := C.PQstatus(conn) status := C.PQstatus(conn)
if status != CONNECTION_OK { if status != C.CONNECTION_OK {
error_msg := C.PQerrorMessage(conn) error_msg := C.PQerrorMessage(conn)
eprintln('Connection to a PG database failed: ' + string(error_msg)) eprintln('Connection to a PG database failed: ' + string(error_msg))
exit(1) exit(1)

View File

@ -20,14 +20,6 @@ mut:
ext string ext string
} }
import const (
GL_RGBA
GL_RGB
GL_UNSIGNED_BYTE
GL_TEXTURE_2D
STBI_rgb_alpha
)
pub fn load(path string) Image { pub fn load(path string) Image {
ext := path.all_after('.') ext := path.all_after('.')
mut res := Image { mut res := Image {
@ -35,14 +27,10 @@ pub fn load(path string) Image {
ext: ext ext: ext
data: 0 data: 0
} }
if ext == 'png' { flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 }
res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, STBI_rgb_alpha) res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, flag)
}
else {
res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, 0)
}
if isnil(res.data) { if isnil(res.data) {
println('stbi cant load') println('stbi image failed to load')
exit(1) exit(1)
} }
return res return res
@ -53,12 +41,12 @@ pub fn (img Image) free() {
} }
pub fn (img Image) tex_image_2d() { pub fn (img Image) tex_image_2d() {
mut rgb_flag := GL_RGB mut rgb_flag := C.GL_RGB
if img.ext == 'png' { if img.ext == 'png' {
rgb_flag = GL_RGBA rgb_flag = C.GL_RGBA
} }
C.glTexImage2D(GL_TEXTURE_2D, 0, rgb_flag, img.width, img.height, 0, rgb_flag, GL_UNSIGNED_BYTE, C.glTexImage2D(C.GL_TEXTURE_2D, 0, rgb_flag, img.width, img.height, 0,
img.data) rgb_flag, C.GL_UNSIGNED_BYTE, img.data)
} }
pub fn set_flip_vertically_on_load(val bool) { pub fn set_flip_vertically_on_load(val bool) {