Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only one texture can be present at a time #3

Closed
glowiak opened this issue Dec 17, 2023 · 8 comments
Closed

Only one texture can be present at a time #3

glowiak opened this issue Dec 17, 2023 · 8 comments

Comments

@glowiak
Copy link

glowiak commented Dec 17, 2023

I was writing a flappy bird clone, and I noticed that there can be only one texture2d_type in a module (or maybe in the whole program).

When I add a second one, the first one gets zeroed out.

@glowiak
Copy link
Author

glowiak commented Dec 17, 2023

Uh, this applies to any type from the binding it seems.

When I added a color_type variable, the single texture I had stopped rendering.

@interkosmos
Copy link
Owner

Is the behaviour related to this raylib issue? Does it occur with raylib 4.5 and 5.x alike?

@glowiak
Copy link
Author

glowiak commented Dec 17, 2023

@interkosmos It only occurs in your binding.

I am using it for a project of making a bunnymark in each binding, and it is the only binding I tried that has this problem (and I have tried like 10 of them now).

@glowiak
Copy link
Author

glowiak commented Dec 17, 2023

The binding is set to use my system raylib in /usr/lib/libraylib.so, which is 5.1-dev, and in other bindings and in C it works fine.

@interkosmos
Copy link
Owner

I cannot reproduce your issue. Could you provide more information, for instance, a minimal example?

@glowiak
Copy link
Author

glowiak commented Dec 18, 2023

! Shark Jumpy - a Flappy Bird, but reversed

! CODE TAKEN FROM https://masuday.github.io/fortran_tutorial/random.html
! modified a bit

subroutine random_stduniform(u)
   implicit none
   real,intent(out) :: u
   real :: r
   call random_number(r)
   u = 1 - r
end subroutine random_stduniform

real function randint(a,b)
   implicit none
   real,intent(in) :: a,b
   real :: u
   call random_stduniform(u)
   randint = (b-a)*u + a
end function randint

! END BORROWED CODE

module game_data
    use :: raylib

    type :: t_entity
        real :: x
        real :: y
        integer :: width
        integer :: height
    end type t_entity

    type, extends(t_entity) :: t_player
        real :: speed
        real :: gravity
        type(texture2d_type) :: tex
        type(texture2d_type) :: tex_flap
    end type t_player

    type, extends(t_entity) :: t_pipe
        real :: speed
        type(color_type) :: colour
    end type t_pipe

    type :: game
        integer :: width = 800
        integer :: height = 600
        integer :: score = 0
        character(15) :: caption = "Jumpy Shark"
        type(t_player) :: thePlayer
        type(t_pipe) :: pipes(16)
        integer :: pipes_len = 16
    end type game

    contains

    ! player
    subroutine player_init(this)
        type(t_player), intent(out) :: this

        this%x = 50
        this%y = 50

        this%width = 32
        this%height = 32

        this%speed = 4.2
        this%gravity = -4.0

        ! load the texture
        this%tex = load_texture("player.png")
        this%tex_flap = load_texture("playerFlap.png")
    end subroutine player_init

    subroutine player_update(this)
        type(t_player), intent(inout) :: this

        this%gravity = this%gravity - 0.5
        this%y = this%y - this%gravity

        if (is_key_pressed(KEY_SPACE)) then
            this%gravity = 10.0
        end if
    end subroutine player_update

    subroutine player_paint(this)
        type(t_player), intent(in) :: this

        if (this%gravity < 0.0) then
            call draw_texture(this%tex, int(this%x), int(this%y), WHITE)
        end if
        if (this%gravity > 0.0) then
            call draw_texture(this%tex_flap, int(this%x), int(this%y), WHITE)
        end if

        !call draw_rectangle(int(this%x), int(this%y), this%width, this%height, RED)
    end subroutine player_paint

    subroutine player_unload(this)
        type(t_player), intent(in) :: this

        call unload_texture(this%tex)
        call unload_texture(this%tex_flap)
    end subroutine player_unload

    ! pipe

    subroutine pipe_init(this, x, y, is_up, colour)
        type(t_pipe), intent(inout) :: this
        real, intent(in) :: x
        real, intent(in) :: y
        integer, intent(in) :: is_up
        type(color_type), intent(in) :: colour

        this%x = x
        this%y = y
        this%colour = colour

        this%width = 40
        if (is_up == 1) then ! true
            this%height = int(randint(100.0, 150.0))
        end if
            this%height = 350
        if (is_up == 0) then ! false
        end if
    end subroutine pipe_init

    subroutine pipe_update(this)
        type(t_pipe), intent(inout) :: this
    end subroutine pipe_update

    subroutine pipe_paint(this)
        type(t_pipe), intent(inout) :: this

        call draw_rectangle(int(this%x), int(this%y), this%width, this%height, this%colour)
    end subroutine pipe_paint

    subroutine pipe_unload(this)
        type(t_pipe), intent(in) :: this
    end subroutine pipe_unload

    ! utility functions
    function strtol(i) result(res)
        integer, intent(in) :: i
        character(10) :: res
        write(res, '(i5)') i
    end function strtol
end module game_data

! program functions

subroutine init(jumpy)
    use :: game_data
    use :: raylib
    type(game), intent(inout) :: jumpy
    integer :: i = 0

    ! initialize the window
    call set_trace_log_level(LOG_ERROR)
    call init_window(jumpy%width, jumpy%height, jumpy%caption)
    call set_target_fps(60)
    call set_exit_key(0)

    ! initialize the player
    call player_init(jumpy%thePlayer)

    ! initialize the pipes
    do while (i < pipes_len)
        i = i + 1
    end do
end subroutine init

subroutine unload(jumpy)
    use :: game_data
    use :: raylib
    type(game), intent(inout) :: jumpy

    call close_window()
end subroutine unload

subroutine update(jumpy)
    use :: game_data
    type(game), intent(inout) :: jumpy
    integer :: i = 0

    call player_update(jumpy%thePlayer)
end subroutine update

subroutine paint(jumpy)
    use :: game_data
    use :: raylib
    type(game), intent(inout) :: jumpy
    integer :: i

    call clear_background(RAYWHITE)

    call player_paint(jumpy%thePlayer)

    call draw_text(strtol(jumpy%score), jumpy%width / 2 - 100, 15, 50, RED)
end subroutine

program shark
    use, intrinsic :: iso_c_binding, only: c_null_char
    use :: raylib
    use :: game_data
    implicit none

    type(game) :: jumpy

    call init(jumpy)

    do while (.not. window_should_close())
        call update(jumpy)

        call begin_drawing()
            call paint(jumpy)
        call end_drawing()
    end do

    call unload(jumpy)
end program shark

With each texture being a 32x32 png image. When only one of them is present, it works perfectly. With two, it does not render.

@interkosmos
Copy link
Owner

You have forgotten to null-terminate the strings you pass to raylib, as written in the compatibility section of the README:

  • Make sure to properly null-terminate all character strings passed to raylib with c_null_char from module iso_c_binding or simply achar(0).

For texture loading:

        use, intrinsic :: iso_c_binding, only: c_null_char
        type(t_player), intent(out) :: this

        ! [...]

        ! load the texture
        this%tex = load_texture('player.png' // c_null_char)
        this%tex_flap = load_texture('playerFlap.png' // c_null_char)

And for window creation:

    use, intrinsic :: iso_c_binding, only: c_null_char

    ! [...]

    ! initialize the window
    call set_trace_log_level(LOG_ERROR)
    call init_window(jumpy%width, jumpy%height, jumpy%caption // c_null_char)

@glowiak
Copy link
Author

glowiak commented Dec 19, 2023

Oh thanks now it works.

@glowiak glowiak closed this as completed Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants