It was pointed out here Tsoding on Fortran - #6 by HugoMVale that the popular library raylib (GitHub - raysan5/raylib: A simple and easy-to-use library to enjoy videogames programming) does not have bindings for Fortran. This simply will not do. It was easy enough to get a graphics window to pop up, but I am stuck feeling that this is going to end up being pretty hacky without a clearer idea of how to properly represent a few things that C has and Fortran doesn’t.
First, this structure: https://github.com/raysan5/raylib/blob/master/src/raylib.h#L235
// Color, 4 components, R8G8B8A8 (32bit)
typedef struct Color {
unsigned char r; // Color red value
unsigned char g; // Color green value
unsigned char b; // Color blue value
unsigned char a; // Color alpha value
} Color;
At the moment, I am using this derived-type color
and accompanying function to set a color based on Fortran default integers:
type, bind(C) :: color
integer(c_signed_char) :: r = int(0, c_signed_char)
integer(c_signed_char) :: g = int(0, c_signed_char)
integer(c_signed_char) :: b = int(0, c_signed_char)
integer(c_signed_char) :: a = int(0, c_signed_char)
end type color
pure function dfray_color(r, g, b, a) result(color_out)
integer, intent(in) :: r, g, b, a
type(color) :: color_out
color_out%r = int(r, c_signed_char)
color_out%g = int(g, c_signed_char)
color_out%b = int(b, c_signed_char)
color_out%a = int(a, c_signed_char)
end function dfray_color
This seems… Non-ideal to say the least. I have not found a nice way to allow a structure constructor to set this up given values 0-255. For whatever reason, compilers are perfectly fine with the function dfray_color
, but complain about integer overflow if the same strategy is used in a structure constructor.
I have also considered faking rgba
to be a single integer(c_int32_t)
and using mvbits, but that can’t be used in the structure constructor either because it is a subroutine.
I would like to be able to use the constructor so that I can define the single derived type and also the following constants (type(color), parameter :: {name}
I suppose) for easy use (raylib - cheatsheet)
// Custom raylib color palette for amazing visuals on WHITE background
#define LIGHTGRAY (Color){ 200, 200, 200, 255 } // Light Gray
#define GRAY (Color){ 130, 130, 130, 255 } // Gray
#define DARKGRAY (Color){ 80, 80, 80, 255 } // Dark Gray
#define YELLOW (Color){ 253, 249, 0, 255 } // Yellow
#define GOLD (Color){ 255, 203, 0, 255 } // Gold
#define ORANGE (Color){ 255, 161, 0, 255 } // Orange
#define PINK (Color){ 255, 109, 194, 255 } // Pink
#define RED (Color){ 230, 41, 55, 255 } // Red
#define MAROON (Color){ 190, 33, 55, 255 } // Maroon
#define GREEN (Color){ 0, 228, 48, 255 } // Green
#define LIME (Color){ 0, 158, 47, 255 } // Lime
#define DARKGREEN (Color){ 0, 117, 44, 255 } // Dark Green
#define SKYBLUE (Color){ 102, 191, 255, 255 } // Sky Blue
#define BLUE (Color){ 0, 121, 241, 255 } // Blue
#define DARKBLUE (Color){ 0, 82, 172, 255 } // Dark Blue
#define PURPLE (Color){ 200, 122, 255, 255 } // Purple
#define VIOLET (Color){ 135, 60, 190, 255 } // Violet
#define DARKPURPLE (Color){ 112, 31, 126, 255 } // Dark Purple
#define BEIGE (Color){ 211, 176, 131, 255 } // Beige
#define BROWN (Color){ 127, 106, 79, 255 } // Brown
#define DARKBROWN (Color){ 76, 63, 47, 255 } // Dark Brown
#define WHITE (Color){ 255, 255, 255, 255 } // White
#define BLACK (Color){ 0, 0, 0, 255 } // Black
#define BLANK (Color){ 0, 0, 0, 0 } // Blank (Transparent)
#define MAGENTA (Color){ 255, 0, 255, 255 } // Magenta
#define RAYWHITE (Color){ 245, 245, 245, 255 } // My own White (raylib logo)