Raylib Game

Game development specific discussions.
Post Reply
neil
Posts: 594
Joined: Mar 17, 2022 23:26

Raylib Game

Post by neil »

I found a new Freebasic game that compiled fine; it requires raylib.
Raylib looks promising for developing FreeBasic games.

Here's a link to raylib.
https://github.com/raysan5/raylib/releases

Here's the FreeBasic headers.
https://github.com/glasyalabolas/fb-raylib

Here's a link to the FreeBasic raylib game.
https://github.com/SomeUnusualGames/Flag-Wars

Here's a video link to the game demo.
https://www.youtube.com/watch?v=bECgXa9nk4Q

Use this code to test your PC to see if raylib is installed.

Code: Select all

' Raylib test code

#include "raylib.bi"
#define PLATFORM_DESKTOP

InitWindow(800, 600, "Test")
'' If we were compiling for the web, we don't want to set 60 FPS
#ifdef PLATFORM_DESKTOP
SetTargetFPS(60)
#endif

Sub GetX(ByRef X As Integer)
    X = GetRandomValue(0, 100)
End Sub

Dim As Integer X = 0
While (Not WindowShouldClose())
    BeginDrawing()
    ClearBackground(BLACK)
    GetX(X)
    DrawText("Hello world", X, 200, 20, WHITE)
    EndDrawing()    
Wend

CloseWindow()
Here's a raylib bouncing ball demo.

Code: Select all

/'*******************************************************************************************
*
*   raylib [shapes] example - bouncing ball
*
*   This example has been created using raylib 1.0 (www.raylib.com)
*   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
*   Copyright (c) 2013 Ramon Santamaria (@raysan5)
*
********************************************************************************************'/

#include once "raylib.bi"

'' Initialization
const as long _
  screenWidth = 800, screenHeight = 450

SetConfigFlags( FLAG_MSAA_4X_HINT ) '' Enable anti-aliasing if available
InitWindow( screenWidth, screenHeight, "raylib [shapes] example - bouncing ball" )

var _
  ballPosition = Vector2( GetScreenWidth() / 2, GetScreenHeight() / 2 ), _
  ballSpeed = Vector2( 5.0f, 4.0f )

dim as long ballRadius = 20

dim as boolean pause = 0
dim as long framesCounter = 0

SetTargetFPS( 60 )

'' Main game loop
do while( not WindowShouldClose() )
  '' Update
  if( IsKeyPressed( KEY_SPACE ) ) then pause xor= true
  
  if( not pause ) then
    ballPosition.x += ballSpeed.x
    ballPosition.y += ballSpeed.y
    
    '' Check walls collision for bouncing
    if( ( ballPosition.x >= ( GetScreenWidth() - ballRadius ) ) orElse ( ballPosition.x <= ballRadius ) ) then ballSpeed.x *= -1.0f
    if( ( ballPosition.y >= ( GetScreenHeight() - ballRadius ) ) orElse ( ballPosition.y <= ballRadius ) ) then ballSpeed.y *= -1.0f
  else
    framesCounter += 1
  end if
  
  '' Draw
  BeginDrawing()
    ClearBackground( RAYWHITE )
    
    DrawCircleV( ballPosition, ballRadius, MAROON )
    DrawText( "PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY )
    
    '' On pause, we draw a blinking message
    if( pause andAlso ( ( framesCounter / 30 ) mod 2 = 0 ) ) then DrawText( "PAUSED", 350, 200, 30, GRAY )
    
    DrawFPS( 10, 10 )
  EndDrawing()
loop

'' De-Initialization
CloseWindow()
mrToad
Posts: 432
Joined: Jun 07, 2005 23:03
Location: USA
Contact:

Re: Raylib Game

Post by mrToad »

Raylib is amazing. Latest release is Raylib 5 (released Dec, 2023). I just found today raylib-freebasic -> v5.0 in the bindings and wrappers list. I'm excited to see it, although I don't see rlgl.bi and I wonder if the Flag-Wars game in the above post is using rlgl? I heard there is now an SDL option included in Raylib but I don't think the Flag-Wars game is using that.

1) My questions are, is Raylib 5 fully working in latest version(s) of Freebasic? (If not, what is missing?)
2) And, as described below, is the "pseudo-OpenGL 1.1 immediate-mode style API" just an optional feature if you want to work with Raylib in the old GL FFP style, such as Easy GL2D uses? (Although Easy GL2D is only FPP).

From Raylib's rlgl.h:
* rlgl v5.0 - A multi-OpenGL abstraction layer with an immediate-mode style API
*
* DESCRIPTION:
* An abstraction layer for multiple OpenGL versions (1.1, 2.1, 3.3 Core, 4.3 Core, ES 2.0)
* that provides a pseudo-OpenGL 1.1 immediate-mode style API (rlVertex, rlTranslate, rlRotate...)
3) Does Raylib make working with OpenGL and shaders easier?
4) I don't quite understand the Generic Uber Shader he includes yet. What does this do?

Thanks for help with any of the above, I'm still considering if it's possible (and how necessary) to move to Raylib from GL's fixed-function pipeline.
paul doe
Moderator
Posts: 1793
Joined: Jul 25, 2017 17:22
Location: Argentina
Contact:

Re: Raylib Game

Post by paul doe »

Those headers are for raylib 3.5; I still haven't updated them to v 5 (might do it soon).
mrToad wrote: Sep 02, 2024 18:57 1) My questions are, is Raylib 5 fully working in latest version(s) of Freebasic? (If not, what is missing?)
Just the appropriate headers/supplementary code.
2) And, as described below, is the "pseudo-OpenGL 1.1 immediate-mode style API" just an optional feature if you want to work with Raylib in the old GL FFP style, such as Easy GL2D uses? (Although Easy GL2D is only FPP).

From Raylib's rlgl.h:
* rlgl v5.0 - A multi-OpenGL abstraction layer with an immediate-mode style API
*
* DESCRIPTION:
* An abstraction layer for multiple OpenGL versions (1.1, 2.1, 3.3 Core, 4.3 Core, ES 2.0)
* that provides a pseudo-OpenGL 1.1 immediate-mode style API (rlVertex, rlTranslate, rlRotate...)
I would think so, yes. Haven't checked the new features in a while, but it probably works much like the old FFP.
3) Does Raylib make working with OpenGL and shaders easier?
Yes, and no. Yes because much like fbgfx, it takes care of most of the low-level stuff for you (such as creating a context). No, because you still need to know how to code shaders.
4) I don't quite understand the Generic Uber Shader he includes yet. What does this do?
It does exactly what it explains it does: provide a 'default' shader/framework to work with them in a generic way (ie you don't have to implement everything yourself, and you code the shaders with this 'framework' in mind and they'll integrate seamlessly into their pipeline).
Avata
Posts: 110
Joined: Jan 17, 2021 7:27

Re: Raylib Game

Post by Avata »

VisualFBEditor https://github.com/XusinboyBekchanov/VisualFBEditor , the freeBasic IDE, supports Raylib, enabling you to create 3D forms and compile Raylib games code also.

Combine Code with VisualFBEditor : RayLib basic shapes 2d and VisualFBEditor Form

Code: Select all

'#Region "Form"
	#if defined(__FB_MAIN__) AndAlso Not defined(__MAIN_FILE__)
		#define __MAIN_FILE__
		#ifdef __FB_WIN32__
			#cmdline "Form1.rc"
		#endif
		Const _MAIN_FILE_ = __FILE__
	#endif
	
	#include once "mff/Form.bi"
	#include once "mff/Panel.bi"
	Using My.Sys.Forms
	
	'This is where you define module-level Shared variables and Add include file.
	'The "RenderProj" sub is the main rendering code subroutine.
	'在这儿定义模块级别共享变量和添加引用。“RenderProj”过程是主要的渲染代码主过程。
	Declare Sub RenderProj(Param As Any Ptr)
	
	'' include fbgfx.bi for some useful definitions
	'#include once "fbgfx.bi"
	'Using FB
	
	'if drawing with RayLib
	#include once "inc/raymath.bi"
	#include once "inc/raylib.bi"
	Using RayLib
	'
	'初始化相机
	'Dim Shared As RayLib.Camera3D Camera
	Dim Shared As Single rotation
	Dim Shared As Integer IMAGE_W, IMAGE_H
	#ifdef __USE_WINAPI__
		Dim Shared As HWND HandleRender
	#endif
	
	Type Form1Type Extends Form
		Declare Sub Form_Resize(ByRef Sender As Control, NewWidth As Integer, NewHeight As Integer)
		Declare Sub Form_Create(ByRef Sender As Control)
		Declare Sub Form_Close(ByRef Sender As Form, ByRef Action As Integer)
		Declare Constructor
		
		Dim As Panel PanelRender
	End Type
	
	Constructor Form1Type
		#if _MAIN_FILE_ = __FILE__
			With App
				.CurLanguagePath = ExePath & "/Languages/"
				.CurLanguage = My.Sys.Language
			End With
		#endif
		' Form1
		With This
			.Name = "Form1"
			.Text = "VisualFBEditor-3D"
			.Designer = @This
			.StartPosition = FormStartPosition.CenterScreen
			.OnResize = Cast(Sub(ByRef Designer As My.Sys.Object, ByRef Sender As Control, NewWidth As Integer, NewHeight As Integer), @Form_Resize)
			.OnCreate = Cast(Sub(ByRef Designer As My.Sys.Object, ByRef Sender As Control), @Form_Create)
			.OnClose = Cast(Sub(ByRef Designer As My.Sys.Object, ByRef Sender As Form, ByRef Action As Integer), @Form_Close)
			.SetBounds 0, 0, 350, 300
		End With
		
		' PanelRender
		With PanelRender
			.Name = "PanelRender"
			.Text = "PanelRender"
			.TabIndex = 2
			.BackColor = 8421376
			.Anchor.Top = AnchorStyle.asAnchor
			.Anchor.Right = AnchorStyle.asAnchor
			.Anchor.Left = AnchorStyle.asAnchor
			.Anchor.Bottom = AnchorStyle.asAnchor
			.SetBounds 90, 10, 230, 240
			.Designer = @This
			.Parent = @This
		End With
	End Constructor
	
	Dim Shared Form1 As Form1Type
	
	#if _MAIN_FILE_ = __FILE__
		App.DarkMode = False
		Form1.MainForm = True
		Form1.Show
		' Put the Render code here
		'ThreadCreate_(@RenderProj, 0)
		RenderProj(0)
		App.Run
	#endif
'#End Region

'the main rendering code.  渲染代码主过程。
Sub RenderProj(Param As Any Ptr)
	#ifdef __fbgfx_bi__
		
	#elseif defined(RAYLIB_H)
		While RayLib.WindowShouldClose = False
			Dim t As Double = GetTime()
			 rotation += 0.2  '' Polygon shapes and lines rotation
			''----------------------------------------------------------------------------------
			'' Draw
			''----------------------------------------------------------------------------------
			RayLib.BeginDrawing()
			RayLib.ClearBackground(RAYWHITE)
			RayLib.DrawText("some basic shapes available on raylib", 20, 20, 20, DARKGRAY)
			
			'' Circle shapes and lines
			RayLib.DrawCircle(IMAGE_W / 5, 120, 35, DARKBLUE)
			RayLib.DrawCircleGradient(IMAGE_W / 5, 220, 60, GREEN, SKYBLUE)
			RayLib.DrawCircleLines(IMAGE_W / 5, 340, 80, DARKBLUE)
			
			'' Rectangle shapes and ines
			RayLib.DrawRectangle(Int(IMAGE_W / 4 * 2 - 60), 100, 120, 60, RED)
			RayLib.DrawRectangleGradientH(Int(IMAGE_W / 4 * 2 - 90), 170, 180, 130, MAROON, GOLD)
			RayLib.DrawRectangleLines(Int(IMAGE_W / 4 * 2 - 40), 320, 80, 60, ORANGE)  '' NOTE: Uses QUADS internally, not lines
			
			'' Triangle shapes and lines
			RayLib.DrawTriangle(Type<Vector2>(IMAGE_W / 4.0 * 3.0, 80.0), _
			Type<Vector2>(IMAGE_W / 4.0 * 3.0 - 60.0, 150.0), _
			Type<Vector2>(IMAGE_W / 4.0 * 3.0 + 60.0, 150.0), VIOLET)
			
			RayLib.DrawTriangleLines(Type<Vector2>(IMAGE_W / 4.0 * 3.0, 160.0), _
			Type<Vector2>(IMAGE_W / 4.0 * 3.0 - 20.0, 230.0), _
			Type<Vector2>(IMAGE_W / 4.0 * 3.0 + 20.0, 230.0), DARKBLUE)
			
			'' Polygon shapes and lines
			RayLib.DrawPoly(Type<Vector2>(IMAGE_W / 4.0 * 3, 330), 6, 80, rotation, BROWN)
			RayLib.DrawPolyLines(Type<Vector2>(IMAGE_W / 4.0 * 3, 330 ), 6, 90, rotation, BROWN)
			'DrawPolyLinesEx(Type<Vector2>(IMAGE_W / 4.0 * 3, 330), 6, 85, rotation, 6, BEIGE)
			
			'' Polygon shapes and lines (Alternative :)
			RayLib.DrawPoly(Type<Vector2>(IMAGE_W / 4.0 * 3, 320), 6, 80, rotation, BROWN)
			RayLib.DrawPolyLines(Type<Vector2>(IMAGE_W / 4.0 * 3, 330 ), 6, 90, rotation, BROWN)
			RayLib.DrawPolyLinesEx(Type<Vector2>(IMAGE_W / 4.0 * 3, 320), 6, 80, rotation, 6, BEIGE)
			
			'' NOTE: We draw all LINES based shapes together to optimize internal drawing,
			'' this way, all LINES are rendered in a single draw pass
			RayLib.DrawLine(18, 42, IMAGE_W - 18, 42, BLACK)
			RayLib.EndDrawing()
			'ThreadsEnter
			
			'ThreadsLeave
		Wend
	#endif
End Sub

Private Sub Form1Type.Form_Create(ByRef Sender As Control)
	'Initialize the drawing engine in sub Form_Create or Form_Show. The official freeBasic drawing engine is fbgfx,
	'and third-party drawing engines like RayLib are employed. It cannot be mixed simultaneously.
	'在Form_Create或者Form_Show初始化绘图引擎。freeBasic官方绘图引擎是fbgfx,第三方绘图引擎如RayLib。不能同时混用。
	IMAGE_W = ScaleX(PanelRender.Width)
	IMAGE_H = ScaleY(PanelRender.Height)
	#ifdef __fbgfx_bi__
		ScreenRes IMAGE_W, IMAGE_H, 32
		ScreenControl(2, Cast(Integer, HandleRender))
	#elseif defined(RAYLIB_H)
		RayLib.SetConfigFlags(FLAG_MSAA_4X_HINT) '启用反锯齿
		RayLib.InitWindow(IMAGE_W, IMAGE_H, "RaylibWindows")
		HandleRender = RayLib.GetWindowHandle
		If HandleRender = 0 Then
			Debug.Print("Failed to create RayLib window")
			Return
		End If
		RayLib.SetTargetFPS(60) '设置动画帧率(fps)
		SetExitKey(0)
		With Camera
			.position   = Type(40, 20, 0)
			.target     = Type(0, 0, 0)
			.up         = Type(0, 1, 0)
			.fovy       = 70
			.projection = CAMERA_PERSPECTIVE
		End With
	#endif
	
	'Move the render windows to container PanelRender.
	'将渲染绘画窗口移动到容器PanelRender。
	If HandleRender > 0 Then
		SetParent(HandleRender, PanelRender.Handle)
		SetWindowLongW(HandleRender, GWL_STYLE, WS_VISIBLE)
		MoveWindow(HandleRender, 0, 0, IMAGE_W, IMAGE_H, True)
	End If
End Sub

Private Sub Form1Type.Form_Resize(ByRef Sender As Control, NewWidth As Integer, NewHeight As Integer)
	#if defined(__fbgfx_bi__)
		
	#elseif defined(RAYLIB_H)
		MoveWindow(HandleRender, 0, 0, ScaleX(PanelRender.Width), ScaleY(PanelRender.Height), True)
	#endif
End Sub

Private Sub Form1Type.Form_Close(ByRef Sender As Form, ByRef Action As Integer)
	'Ending = True
	#ifdef __fbgfx_bi__
		'cairo_destroy(cairoCreate)
		'cairo_surface_destroy(cairoSurface)
		'ImageDestroy image
	#elseif defined(RAYLIB_H)
		RayLib.CloseWindow
	#endif
End Sub
Single Code: Draw basic shapes 2d

Code: Select all

/'*****************************************************************************************
*
*   raylib [shapes] example - Draw basic shapes 2d (rectangle, circle, line...)
*
*   Example originally created with raylib 1.0, last time updated with raylib 4.2
*
*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
*   BSD-like license that allows static linking with closed source software
*
*   Copyright (c) 2014-2022 Ramon Santamaria (@raysan5)
*
*****************************************************************************************'/
'' Converted by: Axle
'' Date: 26/10/2022
'' raylib V4.2.0
'' raygui V3.2
'' WIITD/raylib-freebasic binders 22/10/2022

'' If not included in the raylib.bi or raygui.bi
'' Depends upon the shared library used...
'' raygui stand alone shared lib
'' -DRAYGUI_IMPLEMENTATION -DBUILD_LIBTYPE_SHARED (Win)
'' -DRAYGUI_IMPLEMENTATION -lraylib (nix)
'' #inclib "raygui"  ' Raygui stand alone shared Unix.
'' #inclib "rayguidll"  ' Raygui stand alone shared Win.
''
'' raylib+raygui shared
'' RAYLIB_MODULE_RAYGUI=TRUE RAYLIB_LIBTYPE=SHARED
'' #inclib "raylib"  ' raylib/raylib+raygui combined shared Linux.
'' #inclib "raylibdll"  ' raylib/raylib+raygui combined shared Windows.

#include once "inc/raylib.bi"

''------------------------------------------------------------------------------------
'' Program main entry point
''------------------------------------------------------------------------------------
Declare Function main_procedure() As Integer
main_procedure()

Function main_procedure() As Integer  ' Main procedure
    '' Initialization
    ''--------------------------------------------------------------------------------------
    Const As Long screenWidth = 800, screenHeight = 450
    
    InitWindow(screenWidth, screenHeight, "raylib [shapes] example - basic shapes drawing")
    
    Dim As Single rotation = 0.0
    
    SetTargetFPS(60)               '' Set our game to run at 60 frames-per-second
    ''--------------------------------------------------------------------------------------
    
    '' Main game loop
    While (Not WindowShouldClose())    '' Detect window close button or ESC key
        '' Update
        ''----------------------------------------------------------------------------------
        rotation += 0.2  '' Polygon shapes and lines rotation
        ''----------------------------------------------------------------------------------
        
        '' Draw
        ''----------------------------------------------------------------------------------
        BeginDrawing()
        
        ClearBackground(RAYWHITE)
        
        DrawText("some basic shapes available on raylib", 20, 20, 20, DARKGRAY)
        
        '' Circle shapes and lines
        DrawCircle(screenWidth/5, 120, 35, DARKBLUE)
        DrawCircleGradient(screenWidth/5, 220, 60, GREEN, SKYBLUE)
        DrawCircleLines(screenWidth/5, 340, 80, DARKBLUE)
        
        '' Rectangle shapes and ines
        DrawRectangle(Int(screenWidth/4*2 - 60), 100, 120, 60, RED)
        DrawRectangleGradientH(Int(screenWidth/4*2 - 90), 170, 180, 130, MAROON, GOLD)
        DrawRectangleLines(Int(screenWidth/4*2 - 40), 320, 80, 60, ORANGE)  '' NOTE: Uses QUADS internally, not lines
        
        '' Triangle shapes and lines
        DrawTriangle(Type<Vector2>(screenWidth / 4.0 * 3.0, 80.0), _
        Type<Vector2>(screenWidth / 4.0 * 3.0 - 60.0, 150.0), _
        Type<Vector2>(screenWidth / 4.0 * 3.0 + 60.0, 150.0), VIOLET)
        
        DrawTriangleLines(Type<Vector2>(screenWidth / 4.0 * 3.0, 160.0), _
        Type<Vector2>(screenWidth / 4.0 * 3.0 - 20.0, 230.0), _
        Type<Vector2>(screenWidth / 4.0 * 3.0 + 20.0, 230.0), DARKBLUE)

        '' Polygon shapes and lines
        DrawPoly(Type<Vector2>(screenWidth / 4.0 * 3, 330), 6, 80, rotation, BROWN)
        DrawPolyLines(Type<Vector2>(screenWidth / 4.0 * 3, 330 ), 6, 90, rotation, BROWN)
        'DrawPolyLinesEx(Type<Vector2>(screenWidth / 4.0 * 3, 330), 6, 85, rotation, 6, BEIGE)

        '' Polygon shapes and lines (Alternative :)
        DrawPoly(Type<Vector2>(screenWidth / 4.0 * 3, 320), 6, 80, rotation, BROWN)
        DrawPolyLines(Type<Vector2>(screenWidth / 4.0 * 3, 330 ), 6, 90, rotation, BROWN)
        DrawPolyLinesEx(Type<Vector2>(screenWidth / 4.0 * 3, 320), 6, 80, rotation, 6, BEIGE)
        
        '' NOTE: We draw all LINES based shapes together to optimize internal drawing,
        '' this way, all LINES are rendered in a single draw pass
        DrawLine(18, 42, screenWidth - 18, 42, BLACK)
        EndDrawing()
        ''----------------------------------------------------------------------------------
    Wend
    
    '' De-Initialization
    ''--------------------------------------------------------------------------------------
    CloseWindow()        '' Close window and OpenGL context
    ''--------------------------------------------------------------------------------------
    
    Return 0
End Function  ' END main_procedure <---
mrToad
Posts: 432
Joined: Jun 07, 2005 23:03
Location: USA
Contact:

Re: Raylib Game

Post by mrToad »

My main concern is in getting away from FFP as years are getting on now, in case support is dropped. Also I don't mind learning to write some basic shaders, or have some help with them, if Raylib does make some things easier.
paul doe wrote: Sep 03, 2024 3:22 Those headers are for raylib 3.5; I still haven't updated them to v 5 (might do it soon).
That would be wonderful! Question on that, besides bug fixes (which is my primary interest in updating), I find online only some new added features (which I don't think are of great interest to me) but not sure if version 5 over 3.5 is especially greater in terms of basic 2D OpenGL graphics and shaders.
paul doe wrote: ...but it probably works much like the old FFP.
Do you mean that it actually makes use of the old FFP or that it is a psuedo-code style to mimic the old FFP? In other words, I would rather it behave like the old FFP while it is actually not, so to drop the concern for gfx cards support for FFP.

@Avata, thanks, I'm working with 2D OpenGL and shaders (still not made my own shaders yet) but I will check out that IDE.
Post Reply