raylib headers

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
IchMagBier
Posts: 52
Joined: Jan 13, 2018 8:47
Location: Germany
Contact:

raylib headers

Postby IchMagBier » Mar 09, 2020 16:56

Hello there :)

I have ported the raylib headers and some examples to FreeBasic. The code is on my GitHub:
https://github.com/IchMagBier/raylib-fb
raylib is an easy to use graphics and audio library based on OpenGL and -AL.
raylib.com wrote:raylib features
  • NO external dependencies, all required libraries included with raylib
  • Multiplatform: Windows, Linux, MacOS, Android, HTML5... and more!
  • Written in plain C code (C99) in PascalCase/camelCase notation
  • Hardware accelerated with OpenGL (1.1, 2.1, 3.3 or ES 2.0)
  • Unique OpenGL abstraction layer: rlgl
  • Powerful Fonts module (XNA SpriteFonts, BMfonts, TTF, SDF)
  • Outstanding texture formats support, including compressed formats (DXT, ETC, ASTC)
  • Full 3d support for 3d Shapes, Models, Billboards, Heightmaps and more!
  • Flexible Materials system, supporting classic maps and PBR maps
  • Animated 3d models supported (skeletal bones animation)
  • Shaders support, including Model shaders and Postprocessing shaders
  • Powerful math module for Vector, Matrix and Quaternion operations
  • Audio loading and playing with streaming support (WAV, OGG, MP3, FLAC, XM, MOD)
  • VR stereo rendering support with configurable HMD device parameters
  • Huge examples collection with +100 code examples!
  • Bindings to +50 programming languages!
  • Free and open source. Check [LICENSE].


Example code:

Code: Select all

#include once "raylib.bi"

const screenWidth = 800
const screenHeight = 450
InitWindow(screenWidth, screenHeight, "Hello World")
SetTargetFPS(60)
while not WindowShouldClose()
   BeginDrawing()
      ClearBackground(RAYWHITE)
      DrawText("Hello World from raylib and FB!", 230, 200, 20, GRAY)
   EndDrawing()
wend
CloseWindow()


I have only tested it on Linux with the 64bit compiler. Be sure to tell me, if it works on Windows.
VANYA
Posts: 1370
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: raylib headers

Postby VANYA » Mar 09, 2020 17:53

Very cool library!
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Postby paul doe » Mar 09, 2020 18:38

My, what a cute nice little lib we have there! Tested on Win64 64-bit. No fuss.
Image Image Image Image

Binaries for the latest release here. This might serve as a drop-in replacement for those folks that wanted to replace EasyGL2D! I'll play around with it when I have a little more time and help you port some examples. Nice work!
Last edited by paul doe on Mar 09, 2020 19:47, edited 1 time in total.
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Postby paul doe » Mar 09, 2020 19:46

Couldn't resist ;)

Ported the TTF example from raylib:

Code: Select all

/'
  *******************************************************************************************
  *
  *   raylib [text] example - TTF loading and usage
  *
  *   This example has been created using raylib 1.3.0 (www.raylib.com)
  *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  *
  *   Copyright (c) 2015 Ramon Santamaria (@raysan5)
  *
  ********************************************************************************************
'/

#include once "raylib.bi"

#if defined( PLATFORM_DESKTOP )
  #define GLSL_VERSION 330
#else '' PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  #define GLSL_VERSION 100
#endif

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

InitWindow( _
  screenWidth, _
  screenHeight, "raylib [text] example - SDF fonts" )

dim as string _
  msg => "Signed Distance Fields"

'const char msg[50] = "Signed Distance Fields";

'' Default font generation from TTF font
dim as Font _
  fontDefault

with fontDefault
  .baseSize => 16
  .charsCount => 95
 
  /'
    Parameters:
      font size: 16
      no chars array provided (0)
      chars count: 95 (autogenerate chars array)
  '/
  .chars => LoadFontData( _
    "fonts/resources/AnonymousPro-Bold.ttf", _
    16, 0, 95, FONT_DEFAULT )
 
  /'
    Parameters:
      chars count: 95
      font size: 16
      chars padding in image: 4 px
      pack method: 0 (default)
  '/
  dim as Image _
    atlas => GenImageFontAtlas( _
      fontDefault.chars, _
      @fontDefault.recs, _
      95, 16, 4, 0)
  .texture => LoadTextureFromImage( atlas )
 
  UnloadImage( atlas )
end with

  '' SDF font generation from TTF font
dim as Font _
  fontSDF

with fontSDF
  .baseSize => 16
  .charsCount => 95
 
  /'
    Parameters:
      font size: 16
      no chars array provided (0)
      chars count: 0 (defaults to 95)
  '/
  .chars => LoadFontData( _
    "fonts/resources/AnonymousPro-Bold.ttf", _
    16, 0, 0, FONT_SDF )
 
  /'
    Parameters:
      chars count: 95
      font size: 16
      chars padding in image: 0 px
      pack method: 1 (Skyline algorythm)
  '/
  dim as Image _
    atlas => GenImageFontAtlas( _
      .chars, _
      @.recs, _
      95, 16, 0, 1 )
 
  .texture => LoadTextureFromImage( atlas )
 
  UnloadImage( atlas )
end with

'' Load SDF required shader (we use default vertex shader)
dim as Shader _
  shader => LoadShader( _
    0, _
    FormatText( _
      "fonts/resources/shaders/glsl%i/sdf.fs", GLSL_VERSION ) )

'' Required for SDF font
SetTextureFilter( _
  fontSDF.texture, FILTER_BILINEAR)

var _
  fontPosition => Vector2( _
    40.0!, screenHeight / 2 - 50 ), _
  textSize => Vector2( 0.0!, 0.0! )

dim as single _
  fontSize => 16.0!

'' 0 - fontDefault, 1 - fontSDF
dim as integer _
  currentFont => 0           

SetTargetFPS( 60 )

do while( not WindowShouldClose() )
  '' Update
  fontSize +=> GetMouseWheelMove() * 8.0!
 
  fontSize => iif( fontSize < 6.0!, 6.0!, fontSize )
  currentFont => iif( IsKeyDown( KEY_SPACE ), 1, 0 )
 
  if( currentFont = 0 ) then
    textSize => MeasureTextEx( fontDefault, strPtr( msg ), fontSize, 0 )
  else
    textSize => MeasureTextEx( fontSDF, strPtr( msg ), fontSize, 0 )
  end if
 
  fontPosition.x => GetScreenWidth() / 2 - textSize.x / 2
  fontPosition.y => GetScreenHeight() / 2 - textSize.y / 2 + 80
 
  '' Render
  BeginDrawing()
 
  ClearBackground( RAYWHITE )
 
  if( currentFont = 1 ) then
    /'
      NOTE: SDF fonts require a custom SDf shader to compute fragment color
      Activate SDF font shader
    '/
    BeginShaderMode( shader )
    DrawTextEx( _
      fontSDF, strPtr( msg ), fontPosition, fontSize, 0, BLACK )
   
    '' Activate our default shader for next drawings
    EndShaderMode()
   
    DrawTexture( _
      fontSDF.texture, 10, 10, BLACK )
  else
    DrawTextEx( _
      fontDefault, strPtr( msg ), fontPosition, fontSize, 0, BLACK )
    DrawTexture( _
      fontDefault.texture, 10, 10, BLACK )
  end if
 
  if( currentFont = 1 ) then
    DrawText( "SDF!", 320, 20, 80, RED )
  else
    DrawText( "default font", 315, 40, 30, GRAY )
  end if
 
  DrawText( _
    "FONT SIZE: 16.0", _
    GetScreenWidth() - 240, _
    20, 20, DARKGRAY )
  DrawText( _
    FormatText( "RENDER SIZE: %02.02f", fontSize ), _
    GetScreenWidth() - 240, _
    50, 20, DARKGRAY )
  DrawText( _
    "Use MOUSE WHEEL to SCALE TEXT!", _
    GetScreenWidth() - 240, _
    90, 10, DARKGRAY )
  DrawText( _
    "HOLD SPACE to USE SDF FONT VERSION!", _
    340, GetScreenHeight() - 30, 20, MAROON )
 
  EndDrawing()
loop

UnloadFont( fontDefault )
UnloadFont( fontSDF )
UnloadShader( shader )

CloseWindow()

Image
Note: you need resources to run this sample. These are located in the repo, in the /examples/text/resources folder. Download the repo and just dump the folder on a local /fonts/resources one. Or simply change the code to point to the one on the repo. Whichever is fine.
D.J.Peters
Posts: 8023
Joined: May 28, 2005 3:28
Contact:

Re: raylib headers

Postby D.J.Peters » Mar 09, 2020 20:03

Thank you for sharing of course I tested raylib before but isn't professional but ok for beginners.

test shadows :lol: (OpenB3D or Horde3d no problems)
mix 50 MP3 channels (to much CPU usage) fbsound only 2-3%
and so on ...

But again it's nice for beginners and has a lot of language and platform bindings
and you will get first results in minutes without a learning curve of pro stuff.

Joshy
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Postby paul doe » Mar 09, 2020 20:10

Another one, shaders and shapes:

Code: Select all

/'
  ********************************************************************************************
  *
  *   raylib [shaders] example - Apply a shader to some shape or texture
  *
  *   NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
  *         OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
  *
  *   NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example
  *         on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
  *         raylib comes with shaders ready for both versions, check raylib/shaders install folder
  *
  *   This example has been created using raylib 1.7 (www.raylib.com)
  *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  *
  *   Copyright (c) 2015 Ramon Santamaria (@raysan5)
  *
  ********************************************************************************************
'/
#include once "raylib.bi"

#if defined( PLATFORM_DESKTOP )
  #define GLSL_VERSION  330
#else '' PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  #define GLSL_VERSION  100
#endif

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

InitWindow( _
  screenWidth, _
  screenHeight, _
  "raylib [shaders] example - shapes and texture shaders" )

dim as Texture2D _
  fudesumi => LoadTexture( "shaders/resources/fudesumi.png" )

dim as Shader _
  shader => LoadShader( _
    0, FormatText( _
      "shaders/resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION ) )

SetTargetFPS( 60 )

do while( not WindowShouldClose() )
  '' Render
  BeginDrawing()
 
  ClearBackground( RAYWHITE )
 
  '' Start drawing with default shader
  DrawText( _
    "USING DEFAULT SHADER", 20, 40, 10, RED )
  DrawCircle( _
    80, 120, 35, DARKBLUE )
  DrawCircleGradient( _
    80, 220, 60, GREEN, SKYBLUE )
  DrawCircleLines( _
    80, 340, 80, DARKBLUE )
 
  '' Activate our custom shader to be applied on next shapes/textures drawings
  BeginShaderMode( shader )
 
  DrawText( _
    "USING CUSTOM SHADER", 190, 40, 10, RED )
  DrawRectangle( _
    250 - 60, 90, 120, 60, RED )
  DrawRectangleGradientH( _
    250 - 90, 170, 180, 130, MAROON, GOLD )
  DrawRectangleLines( _
    250 - 40, 320, 80, 60, ORANGE )
 
  '' Activate our default shader for next drawings
  EndShaderMode()
 
  DrawText( _
    "USING DEFAULT SHADER", 370, 40, 10, RED )
 
  DrawTriangle( _
    Vector2( 430, 80 ), _
    Vector2( 430 - 60, 150 ), _
    Vector2( 430 + 60, 150 ), _
    VIOLET )
 
  DrawTriangleLines( _
    Vector2( 430, 160 ), _
    Vector2( 430 - 20, 230 ), _
    Vector2( 430 + 20, 230 ), _
    DARKBLUE )
 
  DrawPoly( _
    Vector2( 430, 320 ), 6, 80, 0, BROWN )
 
  '' Activate our custom shader to be applied on next shapes/textures drawings
  BeginShaderMode( shader )
 
  '' Using custom shader
  DrawTexture( _
    fudesumi, 500, -30, WHITE )   
 
  '' Activate our default shader for next drawings
  EndShaderMode()
 
  DrawText( _
    "(c) Fudesumi sprite by Eiden Marsal", _
    380, screenHeight - 20, 10, GRAY )
 
  EndDrawing()
loop

UnloadShader( shader )
UnloadTexture( fudesumi )

CloseWindow()

Image

Again, you need resources for this one (almost all examples need it). These are located in the repo's /examples/shaders/resources, so dump them in a local folder and/or change the code paths to it.

@IchMagBier: do you think it will be possible to include the resources used by the examples on your repo, to have them handy? That way, upon downloading your repo, the examples can work right away...
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Postby paul doe » Mar 09, 2020 20:14

D.J.Peters wrote:...
But again it's nice for beginners and has a lot of language and platform bindings
and you will get first results in minutes without a learning curve of pro stuff.
...

Indeed, it has that kind of 'BASIC' flair that most folks here seem to dig (and with a consistent interface, unlike BASIC).
IchMagBier
Posts: 52
Joined: Jan 13, 2018 8:47
Location: Germany
Contact:

Re: raylib headers

Postby IchMagBier » Mar 09, 2020 20:28

paul doe wrote:Another one, shaders and shapes:

Nice work! Take a look at the "shaders_raymarching.c" example, really amazing stuff there.

paul doe wrote:@IchMagBier: do you think it will be possible to include the resources used by the examples on your repo, to have them handy? That way, upon downloading your repo, the examples can work right away...

I already asked the raylib-developers and I am waiting for a response at the moment. I am confident they will agree. They already added FreeBasic to their bindings-list: https://github.com/raysan5/raylib/blob/master/BINDINGS.md
When I have the resources-folder, can I add your ported examples to my repository? Or maybe you could commit them, when you want to.

D.J.Peters wrote:Thank you for sharing of course I tested raylib before but isn't professional but ok for beginners.

I really like raylibs simplicity and straightforwardness. Generally I would always prefer uncomplicated solutions over unnecessary complex things, when I want to be productive. This is also the main reason, why I mostly code with FreeBasic instead of C or C# when working on my own projects.
D.J.Peters
Posts: 8023
Joined: May 28, 2005 3:28
Contact:

Re: raylib headers

Postby D.J.Peters » Mar 09, 2020 21:03

The rayLib shaders does not need OpenGL 3.3 at all it runs on OpenGL 2.x also !
(or it was changed in the past I tested it with OenGL 2.1 also)

Joshy
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Postby paul doe » Mar 09, 2020 21:07

IchMagBier wrote:...
Nice work! Take a look at the "shaders_raymarching.c" example, really amazing stuff there.
...

Will have a look, thanks.
IchMagBier wrote:...
I already asked the raylib-developers and I am waiting for a response at the moment. I am confident they will agree. They already added FreeBasic to their bindings-list: https://github.com/raysan5/raylib/blob/master/BINDINGS.md

That would be nice. Not really 'practical' (since it will bloat your repo), but handy so that the examples could be run quickly and without fuss.
IchMagBier wrote:...
When I have the resources-folder, can I add your ported examples to my repository? Or maybe you could commit them, when you want to.
...

My, naturally. Whichever is fine to me.
IchMagBier wrote:...
paul doe wrote:Thank you for sharing of course I tested raylib before but isn't professional but ok for beginners.

I really like raylibs simplicity and straightforwardness. Generally I would always prefer uncomplicated solutions over unnecessary complex things, when I want to be productive. This is also the main reason, why I mostly code with FreeBasic instead of C or C# when working on my own projects.

Joshy said that, not me XD

Indeed, it may come in handy for many folks here, especially those that were looking for a modern replacement for EasyGL2D by Rel: it has more features and they're very easy to use. Thanks for sharing!
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Postby paul doe » Mar 09, 2020 21:53

Another one: particles and blending.

Code: Select all

/'
  ********************************************************************************************
  *
  *   raylib example - particles blending
  *
  *   This example has been created using raylib 1.7 (www.raylib.com)
  *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  *
  *   Copyright (c) 2017 Ramon Santamaria (@raysan5)
  *
  ********************************************************************************************
'/

#include once "raylib.bi"

#define MAX_PARTICLES 200

'' Particle structure with basic data
type _
  Particle
 
  as Vector2 _
    position
  as Color _
    color
  as single _
    alpha, _
    size, _
    rotation
  as boolean _
    active
end type

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

InitWindow( _
  screenWidth, _
  screenHeight, _
  "raylib [textures] example - particles blending" )

'' Particles pool, reuse them!
dim as Particle _
  mouseTail( 0 to MAX_PARTICLES - 1 )

'' Initialize particles
for _
  i as integer => 0 to MAX_PARTICLES - 1
 
  with mouseTail( i )
    .position => Vector2( 0, 0 )
    .color => Color( _
      GetRandomValue( 0, 255 ), _
      GetRandomValue( 0, 255 ), _
      GetRandomValue( 0, 255 ), 255 )
    .alpha => 1.0!
    .size => GetRandomValue( 1, 30 ) / 20.0!
    .rotation => GetRandomValue( 0, 360 )
    .active => false
  end with
next

dim as single _
  gravity => 3.0!

dim as Texture2D _
  smoke => LoadTexture( "texture/resources/smoke.png" )

dim as long _
  blending => BLEND_ALPHA

SetTargetFPS( 60 )

do while( not WindowShouldClose() )
  /'
    Activate one particle every frame and Update active particles.
   
    NOTE: Particles initial position should be mouse position when activated
    NOTE: Particles fall down with gravity and rotation... and disappear after 2 seconds (alpha = 0)
    NOTE: When a particle disappears, active = false and it can be reused.
  '/
  for _
    i as integer => 0 to MAX_PARTICLES - 1
      with mouseTail( i )
        if( not .active ) then
          .active => true
          .alpha => 1.0!
          .position => GetMousePosition()
         
          exit for
        end if
      end with
  next
 
  for _
    i as integer => 0 to MAX_PARTICLES - 1
      with mouseTail( i )
        if( .active ) then
          .position.y +=> gravity
          .alpha -= 0.01!
         
          if( .alpha <= 0.0! ) then
            .active => false
          end if
         
          .rotation +=> 5.0!
        end if
      end with
  next
 
  if( IsKeyPressed( KEY_SPACE ) ) then
    blending => iif( blending = BLEND_ALPHA, _
      BLEND_ADDITIVE, BLEND_ALPHA )
  end if
 
  '' Render
  BeginDrawing()
  ClearBackground( DARKGRAY )
 
  BeginBlendMode( blending )
 
  '' Draw active particles
  for _
    i as integer => 0 to MAX_PARTICLES - 1
   
    with mouseTail( i )
      if( .active ) then
        DrawTexturePro( _
          smoke, _
          Rectangle( _
            0.0!, 0.0!, _
            smoke.width, smoke.height ), _
          Rectangle( _
            .position.x, .position.y, _
            smoke.width * .size, smoke.height * .size ), _
          Vector2( _
            smoke.width * .size / 2.0!, _
            smoke.height * .size / 2.0! ), _
          .rotation, _
          Fade( .color, .alpha ) )
      end if
    end with
  next
 
  EndBlendMode()
 
  DrawText( _
    "PRESS SPACE to CHANGE BLENDING MODE", _
    180, 20, 20, BLACK )
 
  if( blending = BLEND_ALPHA) then
    DrawText( _
      "ALPHA BLENDING", _
      290, screenHeight - 40, 20, BLACK )
  else
    DrawText( _
      "ADDITIVE BLENDING", _
      280, screenHeight - 40, 20, RAYWHITE )
  end if

  EndDrawing()
loop

UnloadTexture( smoke )

CloseWindow()


Image

Resources for this one in the raylib's repo /examples/textures/resources folder.
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Postby paul doe » Mar 09, 2020 23:38

Parallax scrolling example (this is a nice one):

Code: Select all

/'
  ********************************************************************************************
  *
  *   raylib [textures] example - Background scrolling
  *
  *   This example has been created using raylib 2.0 (www.raylib.com)
  *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  *
  *   Copyright (c) 2019 Ramon Santamaria (@raysan5)
  *
  ********************************************************************************************
'/
#include once "raylib.bi"

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

InitWindow( _
  screenWidth, _
  screenHeight, _
  "raylib [textures] example - background scrolling" )

/'
  NOTE: Be careful, background width must be equal or bigger than screen width
  if not, texture should be draw more than two times for scrolling effect
'/
dim as Texture2D _
  background => LoadTexture( _
    "texture/resources/cyberpunk_street_background.png" ), _
  midground => LoadTexture( _
    "texture/resources/cyberpunk_street_midground.png" ), _
  foreground => LoadTexture( _
    "texture/resources/cyberpunk_street_foreground.png" )

dim as single _
  scrollingBack => 0.0!, _
  scrollingMid => 0.0!, _
  scrollingFore => 0.0!

SetTargetFPS( 60 )

do while( not WindowShouldClose() )
  scrollingBack -=> 0.1!
  scrollingMid -=> 0.5!
  scrollingFore -=> 1.0!

  '' NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
  if( scrollingBack <= -background.width * 2 ) then
    scrollingBack => 0
  end if
 
  if( scrollingMid <= -midground.width * 2 ) then
    scrollingMid => 0
  end if
 
  if( scrollingFore <= -foreground.width * 2 ) then
    scrollingFore => 0
  end if
 
  '' Render
  BeginDrawing()

  ClearBackground( GetColor( &h052c46ff ) )

  /'
    Draw background image twice.
   
    NOTE: Texture is scaled twice its size
  '/
  DrawTextureEx( _
    background, _
    Vector2( scrollingBack, 20 ), _
    0.0!, 2.0!, WHITE )
  DrawTextureEx( _
    background, _
    Vector2( background.width * 2 + scrollingBack, 20 ), _
    0.0!, 2.0!, WHITE )

  '' Draw midground image twice
  DrawTextureEx( _
    midground, Vector2( scrollingMid, 20 ), _
    0.0!, 2.0!, WHITE )
  DrawTextureEx( _
    midground, _
    Vector2( midground.width * 2 + scrollingMid, 20 ), _
    0.0!, 2.0!, WHITE )

  '' Draw foreground image twice
  DrawTextureEx( _
    foreground, _
    Vector2( scrollingFore, 70 ), _
    0.0!, 2.0!, WHITE )
  DrawTextureEx( _
    foreground, _
    Vector2( foreground.width * 2 + scrollingFore, 70 ), _
    0.0!, 2.0!, WHITE )

  DrawText( _
    "BACKGROUND SCROLLING & PARALLAX", _
    10, 10, 20, RED )
  DrawText( _
    "(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", _
    screenWidth - 330, screenHeight - 20, 10, RAYWHITE )

  EndDrawing()
loop

UnloadTexture( background )
UnloadTexture( midground )
UnloadTexture( foreground )

CloseWindow()

Image

Uses the same resource folder as the above one.
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Postby paul doe » Mar 10, 2020 3:35

Batched rendering (this is controlled automatically by the lib):

Code: Select all

/'
  ********************************************************************************************
  *
  *   raylib [textures] example - Bunnymark
  *
  *   This example has been created using raylib 1.6 (www.raylib.com)
  *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  *
  *   Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
  *
  ********************************************************************************************
'/
#include once "raylib.bi"

#define MAX_BUNNIES 50000

/'
  This is the maximum amount of elements (quads) per batch
  NOTE: This value is defined in [rlgl] module and can be changed there
'/
#define MAX_BATCH_ELEMENTS  8192

type _
  Bunny
 
  as Vector2 _
    position, _
    speed
  as Color _
    color
end type

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

InitWindow( _
  screenWidth, _
  screenHeight, _
  "raylib [textures] example - bunnymark" )

dim as Texture2D _
  texBunny => LoadTexture( "texture/resources/wabbit_alpha.png" )

dim as Bunny ptr _
  bunnies => allocate( MAX_BUNNIES * sizeOf( Bunny ) )

dim as integer _
  bunniesCount => 0

SetTargetFPS( 60 )

do while( not WindowShouldClose() )
  if( IsMouseButtonDown( MOUSE_LEFT_BUTTON ) ) then
    '' Create more bunnies
    for _
      i as integer => 1 to 100
     
      if( bunniesCount < MAX_BUNNIES ) then
        with bunnies[ bunniesCount ]
          .position => GetMousePosition()
          .speed.x => GetRandomValue( -250, 250 ) / 60.0!
          .speed.y => GetRandomValue( -250, 250 ) / 60.0!
          .color => Color( _
            GetRandomValue( 50, 240 ), _
            GetRandomValue( 80, 240 ), _
            GetRandomValue( 100, 240 ), _
            255 )
        end with
       
        bunniesCount +=> 1
      end if
    next
  end if

  '' Update bunnies
  for _
    i as integer => 0 to bunniesCount - 1
   
    with bunnies[ i ]
      .position.x +=> .speed.x
      .position.y +=> .speed.y
     
      if( _
        ( ( .position.x + texBunny.width / 2 ) > GetScreenWidth() ) orElse _
        ( ( .position.x + texBunny.width / 2 ) < 0 ) ) then
       
        .speed.x *=> -1
      end if
     
      if( _
        ( ( .position.y + texBunny.height / 2) > GetScreenHeight() ) orElse _
        ( ( .position.y + texBunny.height / 2 - 40 ) < 0 ) ) then
       
        .speed.y *=> -1
      end if
    end with
  next
 
  '' Render
  BeginDrawing()
  ClearBackground( RAYWHITE )
 
  for _
    i as integer => 0 to bunniesCount - 1
    /'
      NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS),
      a draw call is launched and buffer starts being filled again;
      before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU...
      Process of sending data is costly and it could happen that GPU data has not been completely
      processed for drawing while new data is tried to be sent (updating current in-use buffers)
      it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies
    '/
    with bunnies[ i ]
      DrawTexture( _
        texBunny, _
        .position.x, _
        .position.y, _
        .color )
    end with
  next

  DrawRectangle( _
    0, 0, _
    screenWidth, 40, _
    BLACK )
  DrawText( _
    FormatText( "bunnies: %i", bunniesCount ), _
    120, 10, 20, GREEN )
  DrawText( _
    FormatText( "batched draw calls: %i", _
      1 + bunniesCount \ MAX_BATCH_ELEMENTS ), _
    320, 10, 20, MAROON )
 
  DrawFPS( 10, 10 )
 
  EndDrawing()
loop

deallocate( bunnies )

UnloadTexture( texBunny )
CloseWindow()

Image

Same resources folder as above.

EDIT: Fixed operator issue; number of batched calls wasn't being displayed correctly.
Last edited by paul doe on Apr 27, 2020 21:31, edited 1 time in total.
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: raylib headers

Postby paul doe » Mar 10, 2020 5:53

3D model loading and rendering:

Code: Select all

/'
  ********************************************************************************************
  *
  *   raylib [models] example - Models loading
  *
  *   raylib supports multiple models file formats:
  *
  *     - OBJ > Text file, must include vertex position-texcoords-normals information,
  *             if files references some .mtl materials file, it will be loaded (or try to)
  *     - GLTF > Modern text/binary file format, includes lot of information and it could
  *              also reference external files, raylib will try loading mesh and materials data
  *     - IQM > Binary file format including mesh vertex data but also animation data,
  *             raylib can load .iqm animations. 
  *
  *   This example has been created using raylib 2.6 (www.raylib.com)
  *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  *
  *   Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
  *
  ********************************************************************************************
'/
#include once "raylib.bi"

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

InitWindow( _
  screenWidth, _
  screenHeight, _
  "raylib [models] example - models loading" )

'' Define the camera to look into our 3d world
dim as Camera _
  camera

with camera
  .position => Vector3( 50.0!, 50.0!, 50.0! )
  .target => Vector3( 0.0!, 10.0!, 0.0! )
  .up => Vector3( 0.0!, 1.0!, 0.0! )
  .fovy => 45.0!
  .type => CAMERA_PERSPECTIVE
end with

const as string _
  resPath => "models/resources/"

dim as Model _
  model => LoadModel( resPath + "models/castle.obj" )
dim as Texture2D _
  texture => LoadTexture( resPath + "models/castle_diffuse.png" )

model.materials[ 0 ].maps[ MAP_DIFFUSE ].texture => texture

var _
  position => Vector3( 0.0!, 0.0!, 0.0! )

dim as BoundingBox _
  bounds => MeshBoundingBox( model.meshes[ 0 ] )

/'
  NOTE: bounds are calculated from the original size of the model,
  if model is scaled on drawing, bounds must be also scaled
'/
SetCameraMode( camera, CAMERA_FREE )

dim as boolean _
  selected => false

SetTargetFPS( 60 )

do while( not WindowShouldClose() )
  '' Update
  UpdateCamera( @camera )
 
  '' Load new models/textures on drag&drop
  if( IsFileDropped() ) then
    dim as long _
      count => 0
    dim as zstring ptr ptr _
      droppedFiles => GetDroppedFiles( @count )
   
    '' Only support one file dropped
    if( count = 1 ) then
      '' Model file formats supported
      if( _
        IsFileExtension( droppedFiles[ 0 ], ".obj" ) orElse _
        IsFileExtension( droppedFiles[ 0 ], ".gltf" ) orElse _
        IsFileExtension( droppedFiles[ 0 ], ".iqm" ) ) then
       
        UnloadModel( model )
        model => LoadModel( droppedFiles[ 0 ] )
        model.materials[ 0 ].maps[ MAP_DIFFUSE ].texture => texture
       
        bounds => MeshBoundingBox( model.meshes[ 0 ] )
      elseif( IsFileExtension( droppedFiles[ 0 ], ".png" ) ) then
        '' Unload current model texture and load new one
        UnloadTexture( texture )
        texture => LoadTexture( droppedFiles[ 0 ] )
        model.materials[ 0 ].maps[ MAP_DIFFUSE ].texture => texture
      end if
    end if
   
    '' Clear internal buffers
    ClearDroppedFiles()
  end if
 
  '' Select model on mouse click
  if( IsMouseButtonPressed( MOUSE_LEFT_BUTTON ) ) then
    '' Check collision between ray and box
    if( CheckCollisionRayBox( _
      GetMouseRay( _
        GetMousePosition(), camera ), _
      bounds ) ) then _
     
      selected => not selected
    else
      selected => false
    end if
  end if
 
  '' Render
  BeginDrawing()
  ClearBackground( RAYWHITE )
 
  BeginMode3D( camera )
 
  DrawModel( model, position, 1.0!, WHITE )
  DrawGrid( 20, 10.0! )
 
  if( selected ) then _
    '' Draw selection box
    DrawBoundingBox( bounds, GREEN )
  end if
 
  EndMode3D()
 
  DrawText( _
    "Drag & drop model to load mesh/texture.", _
    10, GetScreenHeight() - 20, 10, DARKGRAY )
 
  if( selected ) then
    DrawText( _
      "MODEL SELECTED", _
      GetScreenWidth() - 110, 10, 10, GREEN )
  end if
 
  DrawText( _
    "(c) Castle 3D model by Alberto Cano", _
    screenWidth - 200, screenHeight - 20, 10, GRAY )
 
  DrawFPS( 10, 10 )
 
  EndDrawing()
loop

UnloadTexture( texture )
UnloadModel( model )

CloseWindow()

Image

Drag and drop .obj files on the window to load a new model. Also, drag a .png file to change the model's albedo (it's 'texture').
Resources for this example are located in the /examples/models/resources folder.

PS: my, ain't it nice from the developers to not only focus on a game engine, but also on the tooling aspects? There's support for clipboard, too...
Last edited by paul doe on Mar 11, 2020 3:14, edited 1 time in total.
IchMagBier
Posts: 52
Joined: Jan 13, 2018 8:47
Location: Germany
Contact:

Re: raylib headers

Postby IchMagBier » Mar 10, 2020 7:16

I ported the raymarching example:
https://github.com/IchMagBier/raylib-fb/blob/master/examples/3d_shaders.bas
Image
I have added your examples to my repo aswell. I changed the formatting a bit to fit with the existing examples' style. I also included the files from the resources-folder, which are needed to run the examples.

paul doe wrote:Joshy said that, not me XD

Ya, I have already edited the post, sorry. :)

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 5 guests