raylib headers

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Post by paul doe »

mrToad wrote:...
New Example:
It would be nice to have a fb-translated example of scarfy as part of the examples collection, to show loading and animating sprites from a texture. I will try to do this and contribute it.
...

Code: Select all

/'******************************************************************************************
*
*   raylib [textures] example - Texture loading and drawing a part defined by a rectangle
*
*   This example has been created using raylib 1.3 (www.raylib.com)
*   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
*   Copyright (c) 2014 Ramon Santamaria (@raysan5)
*
*******************************************************************************************'/

#include once "../raylib.bi"

/'
  This is required only by my custom headers, otherwise the names clash
  with some definitions in the OpenGL headers for FreeBasic.
  
  Not needed if you aren't going to use/reimplement more advanced
  features like custom shader support.
  
  Nevertheless, they are somewhat similar to those from IchMagBier (it defines
  constructors for the most common types like VectorX, Rectangle and Color).
'/
#ifndef RAYRED
  #define RAYRED RED
#endif

#ifndef RAYGREEN
  #define RAYGREEN GREEN
#endif

#ifndef RAYBLUE
  #define RAYBLUE BLUE
#endif

/'
  Example follows
'/
#define MAX_FRAME_SPEED     15
#define MIN_FRAME_SPEED      1

dim as const integer _
  screenWidth => 800, _
  screenHeight => 450

InitWindow( _
  screenWidth, screenHeight, _
  "raylib [texture] example - texture rectangle" )

'' NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
dim as Texture2D _
  scarfy => LoadTexture( "resources/scarfy.png" )

var _
  position => Vector2( 350.0f, 280.0f ), _
  frameRec => Rectangle( 0.0f, 0.0f, scarfy.width / 6, scarfy.height )

dim as integer _
  currentFrame => 0, _
  framesCounter => 0, _
  framesSpeed => 8 '' Number of spritesheet frames shown by second

'' Set our game to run at 60 frames-per-second
SetTargetFPS( 60 )

do while( not WindowShouldClose() )
  framesCounter +=> 1

  if ( framesCounter >= ( 60 / framesSpeed ) ) then
      framesCounter => 0
      currentFrame +=> 1
      
      if (currentFrame > 5) then
        currentFrame = 0
      end if
      
      frameRec.x => currentFrame * scarfy.width / 6
  end if
  
  if( IsKeyPressed( KEY_RIGHT ) ) then
    framesSpeed +=> 1
  elseif( IsKeyPressed( KEY_LEFT ) ) then
    framesSpeed -=> 1
  end if
  
  if( framesSpeed > MAX_FRAME_SPEED ) then
    framesSpeed => MAX_FRAME_SPEED
  elseif( framesSpeed < MIN_FRAME_SPEED ) then
    framesSpeed => MIN_FRAME_SPEED
  end if
  
  '' Draw
  BeginDrawing()
    ClearBackground( RAYWHITE )
    
    DrawTexture( scarfy, 15, 40, WHITE )
    DrawRectangleLines( 15, 40, scarfy.width, scarfy.height, LIME )
    DrawRectangleLines( 15 + frameRec.x, 40 + frameRec.y, frameRec.width, frameRec.height, RAYRED )
    
    DrawText( "FRAME SPEED: ", 165, 210, 10, DARKGRAY )
    DrawText( FormatText( "%02i FPS", framesSpeed ), 575, 210, 10, DARKGRAY )
    DrawText( "PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, DARKGRAY )
    
    for i as integer => 0 to MAX_FRAME_SPEED - 1
      if( i < framesSpeed ) then
        DrawRectangle( 250 + 21 * i, 205, 20, 20, RAYRED )
      end if
      
      DrawRectangleLines( 250 + 21 * i, 205, 20, 20, MAROON )
    next
    
    '' Draw part of the texture
    DrawTextureRec( scarfy, frameRec, position, WHITE )
    DrawText( "(c) Scarfy sprite by Eiden Marsal", _
      screenWidth - 200, screenHeight - 20, 10, GRAY )
  EndDrawing()
loop

UnloadTexture( scarfy )

CloseWindow()
Image
Just make sure to change all paths as appropriate and you're set.
Last edited by paul doe on Sep 02, 2020 23:38, edited 1 time in total.
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Post by paul doe »

mrToad wrote:...
Important Addition:
Something I think very important for raylib to fit into the Basic spirit here, a function to get an FB.Image to GPU, like EasyGL2D had. The easiest solution I could think of was a conversion of FB.Image to raylib Image, but it seems beyond my capabilities.
...
It is trivial, but it does have its quirks:

Code: Select all

#include once "fbgfx.bi"
#include "../raylib.bi"

dim as const integer _
  screenWidth => 800, _
  screenHeight => 450

function ToRaylibImage( _
  byval fbImg as Fb.Image ptr ) _
  as Image
  
  '' Create a fully transparent image
  var _
    rlImg => GenImageColor( fbImg->width, fbImg->height, BLANK )
  
  '' Fetch a pointer to the start of the Fb.Image pixel area, skipping header
  dim as ulong ptr _
    fbPx => cptr( ulong ptr, fbImg ) + sizeOf( Fb.Image ) \ sizeOf( ulong )
  
  '' Fetch a pointer to the Raylib Image pixel area
  dim as ulong ptr _
    rlPx => cptr( ulong ptr, rlImg.data )
  
  /'
    FreeBasic image buffers are padded to a paragraph (16 bytes) boundary. This
    means we need to take this into account when addressing each pixel.
  '/
  dim as integer _
    pitchInPixels => fbImg->pitch \ sizeOf( ulong )
  
  '' Then we just transfer the pixels
  for _
    y as integer => 0 to rlImg.height - 1
    
    for _
      x as integer => 0 to rlImg.width - 1
      
      dim as ulong _
        c => fbPx[ y * pitchInPixels + x ]
      
      '' Do note that I'm swapping the red and blue components here!
      rlPx[ y * rlImg.width + x ] => rgba( _
        c and 255, _
        c shr 8 and 255, _
        c shr 16 and 255, _
        c shr 24 )
    next
  next
  
  return( rlImg )
end function

'' Just creates an example Fb.Image
function exampleImage() as Fb.Image ptr
  dim as Fb.Image ptr _
    img => imageCreate( 200, 200 )
  
  dim as single _
    cx => 255 / img->width, _
    cy => 255 / img->height
  
  for _
    y as integer => 0 to img->height - 1
    
    for _
      x as integer => 0 to img->width - 1
      
      pset img, ( x, y ), rgb( cx * x, cy * y, cx * 255 )
    next
  next
  
  circle img, ( 0, 0 ), 50, rgba( 255, 0, 0, 255 ), , , , f
  circle img, ( img->width - 1, 0 ), 50, rgba( 0, 255, 0, 255 ), , , , f
  circle img, ( 0, img->height - 1 ), 50, rgba( 0, 0, 255, 255 ), , , , f
  circle img, ( img->width - 1, img->height - 1 ), 50, rgba( 255, 255, 255, 255 ), , , , f
  
  dim as string _
    msg => "Hello Raylib!"
  
  draw string _
    img, ( ( img->width - len( msg ) * 8 ) \ 2, ( img->height - 8 ) \ 2 ), msg, rgba( 255, 255, 255, 255 )
  
  return( img )
end function

/'
  This call is important. It tells FBGFX that you're going to do your own
  rendering, but that you'll still want to use primitives and buffers.
  The width and height of the 'screen' doesn't matter if you're going to
  do rendering with Raylib, but the bytes per pixel do.
'/
screenRes( 1, 1, 32, , Fb.GFX_NULL )

/'
  And the main code looks like this
'/
InitWindow( _
  screenWidth, screenHeight, _
  "raylib [textures] example - converting a FreeBasic image buffer into a texture" )

dim as Fb.Image ptr _
  fbImage => exampleImage()

var _
  img => ToRaylibImage( fbImage )

dim as Texture2D _
  texture => LoadTextureFromImage( img )

do while( not WindowShouldClose() )
  BeginDrawing()
    ClearBackground( RAYWHITE )
    
    DrawTexture( _
      texture, screenWidth / 2 - texture.width / 2, screenHeight / 2 - texture.height / 2, WHITE )
    
    DrawText("this is a texture created with FreeBasic primitives!", 360, 370, 10, GRAY )
  EndDrawing()
loop

UnloadTexture( texture )
UnloadImage( img )

'' The image can also be destroyed as soon as it is converted
imageDestroy( fbImage )

CloseWindow()
Image
Not particularly efficient, just shows what you need to do.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: raylib headers

Post by D.J.Peters »

@mrToad can you please remove the dead link from your quoted post please ?

Thank you

Joshy
mrToad
Posts: 430
Joined: Jun 07, 2005 23:03
Location: USA
Contact:

Re: raylib headers

Post by mrToad »

D.J.Peters wrote:@mrToad can you please remove the dead link from your quoted post please ?
Thank you
Joshy
Sorry, got it off now. :) I'll check out your thread more when I can.
Haubitze
Posts: 44
Joined: May 20, 2016 8:42

Re: raylib headers

Post by Haubitze »

i have ported the raygui lib so you can fast implement a gui or buttons for games ;)
Image
here the files
https://users.freebasic-portal.de/haubitze/raygui.7z

have fun
Imortis
Moderator
Posts: 1923
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: raylib headers

Post by Imortis »

I somehow missed this when you posted it the other day. This is very cool. I have been meaning to play around with raylib when I can find some spare time.
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Post by paul doe »

I've created a new repo with raylib 3.5 headers and examples:

https://github.com/glasyalabolas/fb-raylib

Still porting the examples, though, so consider it a WIP. Some APIs also changed so the headers may need some revision, and some constants were changed (RED => RAYRED, GREEN => RAYGREEN, BLUE => RAYBLUE) to be able to use them along FreeBasic's OpenGL headers.
mrToad
Posts: 430
Joined: Jun 07, 2005 23:03
Location: USA
Contact:

Re: raylib headers

Post by mrToad »

paul doe wrote:I've created a new repo with raylib 3.5 headers and examples:

https://github.com/glasyalabolas/fb-raylib

Still porting the examples, though, so consider it a WIP. Some APIs also changed so the headers may need some revision, and some constants were changed (RED => RAYRED, GREEN => RAYGREEN, BLUE => RAYBLUE) to be able to use them along FreeBasic's OpenGL headers.
Wow, you really did this! Look at all the good stuff packed in here. =O And complete with a License. What a gift, thank you paul doe.

@Haubitze, and thank you for porting the GUI ! Awesome.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: raylib headers

Post by BasicCoder2 »

Have been trying for hours tonight to figure out how to download and use raylib with FreeBASIC but with no luck.
The problem is I think not having the in depth knowledge behind all this configuration stuff.
There is so much I will have to learn but I am not sure where to start?
Perhaps I will just spend some weeks on the video tutorials for using it with C++ but again I suspect these tutorials will assume knowledge of how to set up libraries to work with a C++ IDE. I have CodeBlocks and have just downloaded Notepad++ but I can see a steep learning curve ahead!
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Post by paul doe »

@mrToad: thanks. Be sure to check from time to time, as I'm still porting examples. The last thing I'll port will be the rlgl headers, so we can have all of raylib ported.
@BasicCoder2: ok, let's go step by step here, to shave some weeks from your steep learning curve. I'm going to assume that you already downloaded the repo, and are ready to test the examples. Also, I'll assume you want to compile them in 32-bit, so we're going to use the raylib-3.5.0_win32_mingw-w64.zip binary from here.

Follow these steps carefully:
  • Download the aforementioned zip, extract it and look for the binary (the DLL) there.
  • Place a copy of it in the folder where the examples are.
After all this deep knowledge has settled, you should be able to load and compile the examples in the IDE of your choice (I use FbEdit). If you fear that you'll eventually forget all this, just make a copy of it on, say, a piece of paper, and put it somewhere visible, like the door of your fridge. This method, while admittedly convoluted, can also be used for other libs.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: raylib headers

Post by dodicat »

I put the 32 bit raylib.dll into the examples folder.
...
Application Error
The application was unable to start correctly (0xc00000007b)
Click OK to close the application.
...
Then I popped in the libraylib.dll.a in along with the dll.
Same error.
I also tried the static lib, libraylib.a, but it gives many errors, so I guess the dll is the best bet If I can get round this application Error.

Win 10
fb version 1.07.1
fbide.
you are not alone basiccoder2..
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: raylib headers

Post by BasicCoder2 »

@Paul Doe
Actually I did all that before. I copied the raylib.dll into the examples folder.
But when I try and compile I get this:
Image
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Post by paul doe »

BasicCoder2 wrote:...
But when I try and compile I get this:
Image
You can see, from your picture, that the linker is trying to link to an older version of the lib, which you placed in its 'libs' folder, NOT the actual DLL. Remove whatever you have in the fbc 'libs' folder and try again (or update the files you have in the 'libs' folder, whichever is fine).

@dodicat: I get that error, but for the 64-bit mingw-w64 one (it's looking for libpthread, which wasn't statically linked, apparently). Try with the other version.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: raylib headers

Post by BasicCoder2 »

@paul doe
You can see, from your picture, that the linker is trying to link to an older version of the lib, which you placed in its 'libs' folder, NOT the actual DLL. Remove whatever you have in the fbc 'libs' folder and try again (or update the files you have in the 'libs' folder, whichever is fine).
Well I will try and do that.

But you see this is where someone with your skill set understands what the linker is saying. You can figure it out because you have that knowledge. When you write "update the files in the libs folder" you don't explain how or with what files because for you it is obvious. I see that all the time, do X without explaining how to do X.
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: raylib headers

Post by UEZ »

@paul doe: I was able to compile and run some of the examples. For some of them, e.g. core_window_flags.bas, I get also error messages:

Raylib\examples\core_window_flags.o:fake:(.text+0x4fb): undefined reference to `IsWindowState'
...
Raylib\examples\core_window_flags.o:fake:(.text+0xb42): more undefined references to `IsWindowState' follow
Post Reply