Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review standalone usage mechanism #12

Closed
raysan5 opened this issue Jul 17, 2018 · 5 comments
Closed

Review standalone usage mechanism #12

raysan5 opened this issue Jul 17, 2018 · 5 comments

Comments

@raysan5
Copy link
Owner

raysan5 commented Jul 17, 2018

Right now raygui is quite tied to raylib, using a specific set of raylib functions.

Despite being possible to hook other library functions to raygui, I feel current mechanism is not easy enough... or at least, has not been properly tested. It should be reviewed.

raygui required functions include:

  • Keyboard and Mouse inputs
  • Basic shapes drawing (lines, rectangles)
  • Textures and text drawing

The idea is to expose and minimize external required hooks to cover the basic raygui needs.

@raysan5
Copy link
Owner Author

raysan5 commented Feb 28, 2019

Some work on this issue on commit bbcf2e9

To use raygui with other graphic libraries than raylib, following functions should be provided by the user:

// Input required functions
//-------------------------------------------------------------------------------
static Vector2 GetMousePosition(void) { return (Vector2){ 0, 0 }; }
static int GetMouseWheelMove(void) { return 0; }
static bool IsMouseButtonDown(int button) { return false; }
static bool IsMouseButtonPressed(int button) { return false; }
static bool IsMouseButtonReleased(int button) { return false; }

static bool IsKeyDown(int key) { return false; }
static bool IsKeyPressed(int key) { return false; }
static int GetKeyPressed(void) { return 0; }                     // -- GuiTextBox()
//-------------------------------------------------------------------------------

// Drawing required functions
//-------------------------------------------------------------------------------
static void DrawRectangle(int x, int y, int width, int height, Color color) { /* TODO */ }
static void DrawRectangleRec(Rectangle rec, Color color) { DrawRectangle(rec.x, rec.y, rec.width, rec.height, color); }

static void DrawRectangleLinesEx(Rectangle rec, int lineThick, Color color) { /* TODO */ }

static void DrawRectangleLines(int x, int y, int width, int height, Color color) { /* TODO */ }             // -- GuiColorPicker()
static void DrawRectangleGradientV(int posX, int posY, int width, int height, Color color1, Color color2);  // -- GuiColorPicker()
static void DrawRectangleGradientH(int posX, int posY, int width, int height, Color color1, Color color2);  // -- GuiColorPicker()
static void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, Color col4);         // -- GuiColorPicker()

static void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color) { /* TODO */ }                    // -- GuiDropdownBox()
static void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color) { /* TODO */ }           // -- GuiScrollBar()

static void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Color tint) { }        // -- GuiImageButtonEx()
//-------------------------------------------------------------------------------

// Text required functions
//-------------------------------------------------------------------------------
static Font GetFontDefault(void);   // --  GetTextWidth()

static Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing) { return (Vector2){ 0.0f }; }  // Measure text size depending on font
static void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint) {  }   // Draw text using font and additional parameters
//-------------------------------------------------------------------------------

Note that some of those functions are only used by some specific control, in case that control is not required, it could be removed with the function dependency.

Also note some drawing functions could probably be removed or simplified to use some other function, i.e. Rectangle drawing functions could all be simplified to only one function usage.

Main issue comes when dealing with Font and Texture2D, those structs are very raylib specific and they should be mapped to similar implementations on other libraries, it could take some work.

Next steps on this issue would be:

  • Simplify shapes drawing functions required, a generic DrawRectangle() should be enough. Non-squared-shape elements could use rIcons or text characters.
  • Find a proper way to deal with Font and Texture2D structs, maybe just providing the implementation os some minimal API to deal with it...
  • Create a raygui standalone usage example with SDL.

Keep working on it. Feedback is welcome!

@simonwashere
Copy link
Contributor

I think the current system is working well, I found it quite easy to integrate into my engine.

It would probably be a mistake to provide an implementation of the Font and Texture2D structures, they need to be whatever is required by the engine that is being used.

An example project with SDL would be nice, and simplifying the required drawing functions would help, but really I don't think it is difficult at the moment anyway.

One minor issue is that in a recent commit you added this line to GuiLoadStyle()
Font font = LoadFontEx(FormatText("%s/%s", GetDirectoryPath(fileName), fontFileName), fontSize, NULL, 0);
and it uses three new functions from raylib that weren't needed before. Maybe have a define to not use styles?

@raysan5
Copy link
Owner Author

raysan5 commented Jun 28, 2019

@simonwashere thanks for reporting!

Agree that an SDL sample would be great, actually, it has been on my raygui TODO list for long time...

Also aware of GuiLoadStyle() new dependencies, it was a quick addition for the talk I gave... need to find a better alternative, probably font data could be embedded in the .rgs text file, in a similar way I do with the style.h code generation... I'd think about it... fonts are always too dependant on underlying technology...

@tinyBigGAMES
Copy link

tinyBigGAMES commented Jul 4, 2019

  1. Map those routines to function pointers (make them exportable, in standalone mode)
  2. Use a pointer to the font struct instead.
  3. Now one could set standalone mode, everything gets exported, then map the routines on the host side and since the font struct is a pointer its implemented on the host side without issue.
  4. I did this before in an older version (1.x raylib time frame, I think 1.5 or 1.6 I believe) and was able to use it from Delphi. I need to find those sources.

Ahh, here is how I got it working back then. All the myXXX function type where avail on the host side, in my case delphi:

typedef int  (*myMeasureText)(const char *text, int fontSize);
typedef void (*myDrawText)(const char *text, int posX, int posY, int fontSize, Color color);
typedef void (*myDrawRectangle)(int x, int y, int width, int height, Color color);
typedef Vector2 (*myGetMousePosition)(void);
typedef bool (*myIsMouseButtonDown)(int button);
typedef bool (*myIsMouseButtonReleased)(int button);
typedef bool (*myIsMouseButtonUp)(int button);
typedef int (*myGetKeyPressed)(void);
typedef bool (*myIsKeyPressed)(int key);
typedef bool (*myIsKeyDown)(int key);
typedef void (*myDrawRectangleLines)(int x, int y, int width, int height, Color color);
typedef void (*myDrawRectangleGradientV)(int posX, int posY, int width, int height, Color color1, Color color2);
typedef void (*myDrawRectangleGradientH)(int posX, int posY, int width, int height, Color color1, Color color2);
typedef void (*myDrawRectangleGradientEx)(Rectangle rec, Color col1, Color col2, Color col3, Color col4);
typedef void (*myDrawTexture)(Texture2D texture, int posX, int posY, Color tint);

myMeasureText MeasureTextFunc = NULL;
myDrawText DrawTextFunc = NULL;
myDrawRectangle DrawRectangleFunc = NULL;
myGetMousePosition GetMousePositionFunc = NULL;
myIsMouseButtonDown IsMouseButtonDownFunc = NULL;
myIsMouseButtonReleased IsMouseButtonReleasedFunc = NULL;
myIsMouseButtonUp IsMouseButtonUpFunc = NULL;
myGetKeyPressed GetKeyPressedFunc = NULL;
myIsKeyPressed IsKeyPressedFunc = NULL;
myIsKeyDown IsKeyDownFunc = NULL;
myDrawRectangleLines DrawRectangleLinesFunc = NULL;
myDrawRectangleGradientV DrawRectangleGradientVFunc = NULL;
myDrawRectangleGradientH DrawRectangleGradientHFunc = NULL;
myDrawRectangleGradientEx DrawRectangleGradientExFunc = NULL;
myDrawTexture DrawTextureFunc = NULL;

//---

static int MeasureText(const char *text, int fontSize) { MeasureTextFunc(text, fontSize); }
static void DrawText(const char *text, int posX, int posY, int fontSize, Color color) { DrawTextFunc(text, posX, posY, fontSize, color); }
static void DrawRectangle(int x, int y, int width, int height, Color color) { DrawRectangleFunc(x, y, width, height, color); }
static void DrawRectangleRec(Rectangle rec, Color color) { DrawRectangle(rec.x, rec.y, rec.width, rec.height, color); }

// Input related functions
static Vector2 GetMousePosition(void) { return GetMousePositionFunc(); }
static bool IsMouseButtonDown(int button) {return IsMouseButtonDownFunc(button); }
static bool IsMouseButtonReleased(int button) {return IsMouseButtonReleasedFunc(button); }

static bool IsMouseButtonUp(int button) {return IsMouseButtonUpFunc(button);}
static int GetKeyPressed(void) {return GetKeyPressedFunc();}
static bool IsKeyPressed(int key) {return IsKeyPressedFunc(key); }
static bool IsKeyDown(int key) {return IsKeyDownFunc(key); }

// Control specific functions
static void DrawRectangleLines(int x, int y, int width, int height, Color color) { DrawRectangleLinesFunc(x, y, width, height, color); }
static void DrawRectangleGradientV(int posX, int posY, int width, int height, Color color1, Color color2) { DrawRectangleGradientVFunc(posX, posY, width, height, color1, color2); }
static void DrawRectangleGradientH(int posX, int posY, int width, int height, Color color1, Color color2) { DrawRectangleGradientHFunc(posX, posY, width, height, color1, color2); }
static void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, Color col4) { DrawRectangleGradientExFunc(rec, col1, col2, col3, col4); }
static void DrawTexture(Texture2D texture, int posX, int posY, Color tint) { DrawTextureFunc(texture, posX, posY, tint); }

@raysan5
Copy link
Owner Author

raysan5 commented Sep 23, 2019

Closing the issue. Current mechanism looks ok for me.

@raysan5 raysan5 closed this as completed Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants