Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raylib Wrapper Help
#1
Hello all,

I am currently working on a Raylib wrapper. However I can't get it to show a image on the screen. Every time I do, it crashes the program.

Color issue is figured out. Need to figure out how to load and draw image functions aren't working properly.

Full wrapper code: https://github.com/gAndy50/Qb64Wrappers/...aylib42.bi

Wrapper code
Code: (Select All)
TYPE fImage
    xdata AS _OFFSET
    xwidth AS INTEGER
    height AS INTEGER
    mipmaps AS INTEGER
    format AS INTEGER
END TYPE

  FUNCTION LoadTexture& (fName AS STRING)

 SUB DrawTexture (BYVAL fTexture AS _OFFSET, BYVAL x AS INTEGER, BYVAL y AS INTEGER, BYVAL fColor AS _UNSIGNED LONG)
   

Program Code
Code: (Select All)
REM $include: 'raylib42.bi'

LET w = 800
LET h = 600

CALL InitWindow(w, h, "Hello")

DIM SHARED Col AS fColor

'R and G are backwards?
Col.r = 255 'this is blue?
Col.g = 0
Col.b = 0 'this is red? (if value is 255)
Col.a = 255

SetTargetFPS (60)

DIM SHARED tex AS fTexture
tex.id = 0
tex.xwidth = 50
tex.height = 50
tex.mipmaps = 0
tex.format = 0

tex& = LoadTexture("dw2.png")

DO

    BeginDrawing

    CALL ClearBackground(_RGBA32(Col.r, Col.g, Col.b, Col.a))

    CALL DrawFPS(1, 1)

    CALL DrawTexture(tex&, 10, 10, _RGBA32(255, 255, 255, 255))

    'CALL DrawLine(10, 20, 50, 50, _RGBA32(0, 0, 255, 255))

    EndDrawing

LOOP UNTIL WindowShouldClose
Reply
#2
I don't know anything much about RayLib, but I can help with the color issue:

Code: (Select All)
'R and G are backwards?
Col.r = 255 'this is blue?
Col.g = 0
Col.b = 0 'this is red? (if value is 255)
Col.a = 255

In memory, color is *always* stored in Alpha, Red, Green, Blue format.
It's easy to see with hex values: &HAARRGGBB

&HFFFF0000 is bright red... 255 alpha, 255 red
&HFF00FF00 is bright green... 255 alpha, 255 green
&HFF0000FF is bright blue... 255 alpha, 255 blue

Depending on how you're writing your data (little endian or big endian), it'll always be in either ARGB or BGRA format.

Yours appears, from your comments, to be in BGRA format here. Wink
Reply
#3
Thanks. I have the color issue figured out. Its the loading image and drawing the image that I'm having issues with.
Reply
#4
I haven't read all of your code yet but I'd be willing to bet that some issues will arise from incorrect data types being used in your external function declarations. Every FUNCTION you've declared from the external libraries are set to use SINGLE as the return type. Also, according to the Wiki, C/C++ float is a SINGLE in QB64 so you will need to clean up a lot of TYPE and function declarations to use the proper data type. I see a lot of declarations using INTEGER. In C/C++, an int corresponds to a LONG in QB64. I'm not sure what the original declarations were but if they were int then they should be LONG. If they were int16 then you're fine. I haven't checked, but make sure any string passed to an external function is ended with a CHR$(0) so as to null-terminate the string. Before you move any further with the project, I'd check and fix the above to make sure these aren't hindering your progress.

Good luck!

P.S.
I just noticed that you're passing the value from RGBA32 to your fColor argument. The DrawTexture function expects a struct of Color but you're passing an UNSIGNED LONG value. https://www.raylib.com/cheatsheet/cheatsheet.html

P.P.S
If I have free time and I don't forget, I might download your BI file and try running it myself. If I get real ambitious, I'll make any change that would make your code be consistent with how I'd declare libraries and see if it resolves your issues. No promises, though.

P.P.P.S
"Option Explicit" is your friend. Make sure you explicitly declare each and every variable you use. This might seem inconvenient for a BASIC language but it prevents many hiccups. I don't write any code without it and I declare every variable even if it's a temp variable that will be disposed in the very next line.

P.P.P.P.S
I see that your fColor struct declaration is using UNSIGNED LONG as the data type for its members. You should instead be using UNSIGNED BYTE as the min value would be zero and the max value should not exceed 255. Your struct, as it currently stands, is 4 times as large as it should be.
Tread on those who tread on you

Reply
#5
Because I was tired and bored, I decided to take a dive into this. I think I'll have a new BI file to give to you by the end of the day. However, it will require you to rewrite any sample code you wrote to test what you have so far since the declarations will have drastically changed.
Tread on those who tread on you

Reply
#6
(08-29-2022, 07:13 PM)Spriggsy Wrote: Because I was tired and bored, I decided to take a dive into this. I think I'll have a new BI file to give to you by the end of the day. However, it will require you to rewrite any sample code you wrote to test what you have so far since the declarations will have drastically changed.

Thanks so much. I did my best to try and make a wrapper for Raylib. The variable convertion and structs were kinda getting at me, but I tried to get it as QB as compatible as I thought it could be.
Reply
#7
I'm still working on building this BI. Can't guarantee that all functions will work since some return structs. I've declared these as returning offsets with the hope that we can use MEM to retrieve the struct that way. That will require me helping you with some of the functions when it comes time. I've got about 200 more lines in this header file to recreate in my BI file. I'll upload it here as soon as I'm done.

By the way, I notice that Raylib uses UTF-8 encoding in some functions so you might need to use my UnicodeToAnsi library so you can convert to and from UTF-8 on the fly.
Tread on those who tread on you

Reply
#8
(08-30-2022, 02:43 AM)Spriggsy Wrote: I'm still working on building this BI. Can't guarantee that all functions will work since some return structs. I've declared these as returning offsets with the hope that we can use MEM to retrieve the struct that way. That will require me helping you with some of the functions when it comes time. I've got about 200 more lines in this header file to recreate in my BI file. I'll upload it here as soon as I'm done.

By the way, I notice that Raylib uses UTF-8 encoding in some functions so you might need to use my UnicodeToAnsi library so you can convert to and from UTF-8 on the fly.

Thanks again. Raylib is written in C99, so I didn't think it'd be super hard to wrap. I didn't have really any trouble when I made a wrapper for Sigil for QB64. Then again QB64 isn't my main programming language, but any programming is good for practice.
Reply
#9
Here is my version of the BI file. Took forever to make. Again, can't guarantee how much will actually work. There's just far too much to test. But you can revise your sample code and run it against my version and I do believe you'll get some better results.


Attached Files
.bi   Raylib.bi (Size: 110.67 KB / Downloads: 69)
Tread on those who tread on you

Reply
#10
my two cents

Code: (Select All)
DIM SHARED tex AS fTexture
tex.id = 0
tex.xwidth = 50
tex.height = 50
tex.mipmaps = 0
tex.format = 0

tex& = LoadTexture("dw2.png")

here I see 2 kind of variable with the same name...
it is not a good practice to do this...

1 please use _Option Explicit, it saves your time and concentration at the price to declare every variables
2 correct the correlate data type as Spriggs pointed out (I fall often in this kind of misunderstandment because I use too few C language)
3 Does the raylib work like OpenGL (you must redraw everytime the screen)?  Otherwise I should prefer to put out of the loop DO the calling for initialization and deallocation of resources  OR to put a _LIMIT to the loop.

Good luck
Reply




Users browsing this thread: 1 Guest(s)