irishgreencitrus / raylib.v

A simple wrapper for raylib in V
zlib License
61 stars 13 forks source link

3D Camera Issue: conversion to non-scalar type requested #15

Closed MatejMagat305 closed 1 year ago

MatejMagat305 commented 1 year ago

as I mension in https://github.com/vlang/v/issues/17401, I have problem compile my program and one error seems like that it is error of compiler, but it can be error of library,

...
/tmp/v_1000/own_struct_vcl.17600315336470555054.tmp.c: In function ‘main__draw’:
/tmp/v_1000/own_struct_vcl.17600315336470555054.tmp.c:17501:17: error: conversion to non-scalar type requested
17501 |                 irishgreencitrus__raylibv__begin_mode_3d(((irishgreencitrus__raylibv__Camera3D)(camera)));
...

it corespondent with line 32

irishgreencitrus commented 1 year ago

Please create a minimal reproducible example (the least amount of code that still causes the error), otherwise I cannot be sure that the problem is with this library and not with vcl / v itself.

Can you also specify the output of v doctor please.

MatejMagat305 commented 1 year ago

Minimal reproducible example:

module main

import irishgreencitrus.raylibv as r

const (
    screen_width  = 800
    screen_height = 450
)

fn main() {
    r.init_window(screen_width, screen_height, 'draw.v [core] example - basic window'.str)
    r.set_target_fps(60)
    mut camera := r.Camera{
        position: r.Vector3{0.0, 0.0, 10.0}
        target: r.Vector3{0.0, 0.0, 0.0}
        up: r.Vector3{0.0, 0.0, 0.0}
        fovy: 45.0
        projection: r.camera_perspective
    }
    r.set_camera_mode(camera, r.camera_free)
    draw(mut camera)
}

fn draw(mut camera r.Camera) {
    for !r.window_should_close() {
        care_camera(mut &camera)
        r.begin_drawing()
        r.clear_background(r.raywhite)
        r.begin_mode_3d(r.Camera3D(camera))
        r.draw_grid(100, 1.0)
        r.draw_circle_3d(r.Vector3{3,3,3}, 5, r.Vector3{(0),0,0}, 0,  r.Color{u8(100),255,100,100})
        r.end_mode_3d()
        r.end_drawing()
    }
}

fn care_camera(mut camera &r.Camera) {
    r.update_camera(camera)
    if r.is_key_down(r.key_z) {
        (*camera).target = r.Vector3{0.0, 0.0, 0.0}
    } else if r.is_key_down(r.key_left) {
        (*camera).target.x--
    } else if r.is_key_down(r.key_right) {
        (*camera).target.x++
    } else if r.is_key_down(r.key_up) {
        (*camera).target.y--
    } else if r.is_key_down(r.key_down) {
        (*camera).target.y++
    } else if r.is_key_down(r.key_minus) {
        (*camera).target.z--
    } else if r.is_key_down(r.key_kp_add) {
        (*camera).target.z++
    }
}

Is it because I am casting to Camera3D?

irishgreencitrus commented 1 year ago

The mut modifier (confusingly) casts camera to a pointer. The fixed code snippet from what is causing the error is

r.begin_mode_3d(r.Camera3D(*camera))

This should be a compile time error in V, its not an error in this library.

MatejMagat305 commented 1 year ago

thank and sorry