Author Topic: Raylib Wrapper Help  (Read 4597 times)

0 Members and 1 Guest are viewing this topic.

Offline Lefty

  • Newbie
  • Posts: 9
    • View Profile
Raylib Wrapper Help
« on: August 13, 2019, 06:52:59 pm »
Hello All,

I am trying to make a wrapper for QB64 with Raylib. Its going along smoothly except for getting the color part right. When I run the program, I get a red screen, so I'm on the right path, just can't figure out what I'm doing wrong. I assume there's a way to convert bytes to ints, but I'm unsure how in QB64. I know most other coding languages have a simple function, is there some steps for doing how? I want the background to be green. Also, on a side note how would I disable the default screen from coming up when calling a window from an external library?

EDIT: I thought I should probably mention in the raylib.h file, the Color struct are defined as unsigned chars. Like this:

typedef struct Color {
    unsigned char r;
    unsigned char g;
    unsigned char b;
    unsigned char a;
} Color;

'Bi File (Flags)
Code: QB64: [Select]
  1. TYPE rColor
  2.  
  3.  

'Bi file (Function)
Code: QB64: [Select]
  1.   SUB ClearBackground (BYVAL rColor AS _UNSIGNED _BYTE)  
  2.  

'Sample Program
Code: QB64: [Select]
  1. 'Raylib Basic Simple Window
  2. REM $include: 'raylib.bi'
  3.  
  4. LET w = 800
  5. LET h = 600
  6.  
  7. CALL InitWindow(w, h, "Simple")
  8.  
  9. SetTargetFPS (60)
  10.  
  11. DIM SHARED Col AS rColor
  12.  
  13. Col.r = 0
  14. Col.g = 255
  15. Col.b = 0
  16. Col.a = 255
  17.  
  18.     BeginDrawing
  19.  
  20.     CALL ClearBackground(Col.g)
  21.  
  22.     'CALL DrawText("Hello World" + CHR$(0), 10, 50, 20, 255, 255, 255, 255) 'ignore for now
  23.  
  24.     CALL DrawFPS(10, 10)
  25.  
  26.     EndDrawing
  27.  
  28. LOOP UNTIL WindowShouldClose
  29.  
  30. CloseWindow
  31.  
  32.  
« Last Edit: August 13, 2019, 08:39:16 pm by Lefty »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Raylib Wrapper Help
« Reply #1 on: August 14, 2019, 11:33:30 am »
Hi.

32bit colors are _Unsigned _Long type. (4 bytes). Use _RED32, _GREEN32, _BLUE32, _ALPHA32 for read some bytes, or MEM functions. Then if you use mem, then is byte order ARGB.

For saving values to memory use _MEMFILL or _MEMPUT or PSET, LINE.... for reading values use _MEMGET or POINT (or very old GET method)...

You write color values with C++, right? If is so, try shift bytes order to ARGB.

The color background in QB64 would simply be as follows (pseudo code):

SCREEN _NEWIMAGE (1024,768,32)
CLS,  _RGBA32 (0, 255, 0, 255)

or the same thing else

CLS, &HFF00FF00 'alpha, red, green, blue hexadecimal

« Last Edit: August 14, 2019, 11:44:34 am by Petr »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Raylib Wrapper Help
« Reply #2 on: August 14, 2019, 12:00:10 pm »
Hi.

32bit colors are _Unsigned _Long type. (4 bytes). Use _RED32, _GREEN32, _BLUE32, _ALPHA32 for read some bytes, or MEM functions. Then if you use mem, then is byte order ARGB.

For saving values to memory use _MEMFILL or _MEMPUT or PSET, LINE.... for reading values use _MEMGET or POINT (or very old GET method)...

You write color values with C++, right? If is so, try shift bytes order to ARGB.

The color background in QB64 would simply be as follows (pseudo code):

SCREEN _NEWIMAGE (1024,768,32)
CLS,  _RGBA32 (0, 255, 0, 255)

or the same thing else

CLS, &HFF00FF00 'alpha, red, green, blue hexadecimal

If you read memory directly, the colors are stored Blue-Green-Red-Alpha.  If you get them as a long (or unsigned long), it doesn’t matter, but if you want to work with them byte by byte, it does.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Lefty

  • Newbie
  • Posts: 9
    • View Profile
Re: Raylib Wrapper Help
« Reply #3 on: August 14, 2019, 03:10:32 pm »
Thanks for the tips, but I still can't get the screen to color correctly(I want the background to change green).

Code: QB64: [Select]
  1. TYPE rColor
  2.     r AS _UNSIGNED LONG
  3.     g AS _UNSIGNED LONG
  4.     b AS _UNSIGNED LONG
  5.     a AS _UNSIGNED LONG
  6.  

Code: QB64: [Select]
  1.    SUB ClearBackground (BYVAL rColor AS _UNSIGNED LONG)    
  2.  

Code: QB64: [Select]
  1. 'Raylib Basic Simple Window
  2. REM $include: 'raylib.bi'
  3.  
  4. LET w = 800
  5. LET h = 600
  6.  
  7. CALL InitWindow(w, h, "Simple") 'this brings up the window from Raylib
  8.  
  9. SetTargetFPS (60)
  10.  
  11. DIM SHARED Col AS rColor
  12.  
  13. Col.r = 0
  14. Col.g = 255
  15. Col.b = 0
  16. Col.a = 255
  17.  
  18.     BeginDrawing
  19.  
  20.     CALL ClearBackground(Col.r+Col.g+col.b+col.a) 'Still not changing the screent o green
  21.  
  22.     'CALL DrawText("Hello World" + CHR$(0), 10, 50, 20, 255, 255, 255, 255)
  23.  
  24.     CALL DrawFPS(10, 10)
  25.  
  26.     EndDrawing
  27.  
  28. LOOP UNTIL WindowShouldClose
  29.  
  30. CloseWindow
  31.  
  32.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Raylib Wrapper Help
« Reply #4 on: August 14, 2019, 04:34:00 pm »
Is there a DECLARE LIBRARY statement missing?  I don’t see where you’re using the Raylib library at all.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Raylib Wrapper Help
« Reply #5 on: August 14, 2019, 04:35:59 pm »
Hi again. Please return your definitions for TYPE (struct) rColor all as _UNSIGNED _BYTE (one color R or G or B or Alpha, one byte, max value = 255). This all colors in one are defined as _UNSIGNED LONG. So my next tip is try:

   CALL ClearBackground (_RGBA32(0,255,0,255)) if this create green background.

Maybe help you this example:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32) 'create screen
  2. LINE (300, 200)-(500, 400), _RGBA32(0, 255, 0, 255), BF
  3. P = POINT(400, 300)
  4. 'next four colors all are _UNSIGNED _BYTE TYPE
  5. PRINT "Alpha channel for point in 400,300 is"; _ALPHA32(P)
  6. PRINT "RED channel for point in 400,300 is"; _RED32(P)
  7. PRINT "GREEN channel for point in 400,300 is"; _GREEN32(P)
  8. PRINT "BLUE channel for point in 400,300 is"; _BLUE32(P)
  9. 'this is the same color for all channels in one and is _UNSIGNED LONG TYPE
  10. PRINT "Unsigned Long color for point 400,300 is:"; P
  11. PRINT "This color can be also written as "; _RGBA32(_RED32(P), _GREEN32(P), _BLUE32(P), _ALPHA32(P))
  12.  

Offline Lefty

  • Newbie
  • Posts: 9
    • View Profile
Re: Raylib Wrapper Help
« Reply #6 on: August 14, 2019, 05:11:19 pm »
Hi again. Please return your definitions for TYPE (struct) rColor all as _UNSIGNED _BYTE (one color R or G or B or Alpha, one byte, max value = 255). This all colors in one are defined as _UNSIGNED LONG. So my next tip is try:

   CALL ClearBackground (_RGBA32(0,255,0,255)) if this create green background.

Maybe help you this example:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32) 'create screen
  2. LINE (300, 200)-(500, 400), _RGBA32(0, 255, 0, 255), BF
  3. P = POINT(400, 300)
  4. 'next four colors all are _UNSIGNED _BYTE TYPE
  5. PRINT "Alpha channel for point in 400,300 is"; _ALPHA32(P)
  6. PRINT "RED channel for point in 400,300 is"; _RED32(P)
  7. PRINT "GREEN channel for point in 400,300 is"; _GREEN32(P)
  8. PRINT "BLUE channel for point in 400,300 is"; _BLUE32(P)
  9. 'this is the same color for all channels in one and is _UNSIGNED LONG TYPE
  10. PRINT "Unsigned Long color for point 400,300 is:"; P
  11. PRINT "This color can be also written as "; _RGBA32(_RED32(P), _GREEN32(P), _BLUE32(P), _ALPHA32(P))
  12.  

Thanks. After trying this, I got it!

NOTE: I have the Declare Library function for Raylib inside of another .bi file.

Code: QB64: [Select]
  1. 'Raylib Basic Simple Window
  2. REM $include: 'raylib.bi'
  3.  
  4. LET w = 800
  5. LET h = 600
  6.  
  7. CALL InitWindow(w, h, "Simple")
  8.  
  9. SetTargetFPS (60)
  10.  
  11. DIM SHARED Col AS rColor
  12.  
  13. Col.r = 0
  14. Col.g = 255
  15. Col.b = 0
  16. Col.a = 255
  17.  
  18.     BeginDrawing
  19.  
  20.     CALL ClearBackground(_RGBA32(Col.r, Col.g, Col.b, Col.a))
  21.  
  22.     'CALL DrawText("Hello World" + CHR$(0), 10, 50, 20, 255, 255, 255, 255)
  23.  
  24.     CALL DrawFPS(10, 10)
  25.  
  26.     EndDrawing
  27.  
  28. LOOP UNTIL WindowShouldClose
  29.  
  30. CloseWindow
  31.  
  32.  
« Last Edit: August 14, 2019, 05:12:53 pm by Lefty »