JMGraphics
Loading...
Searching...
No Matches
JMGraphics Class Reference

#include <JMGraphics.h>

Classes

class  Buffer
 has an fbo and a linked texture. Anything drawn between the buffer.beginDraw() and buffer.endDraw() functions is drawn to the buffer instead of the window. More...
class  Buffer3D
 similar to the Buffer object, but for 3D graphics. More...
class  Font
 object that loads and stores a font. More...
class  Texture
 Object that loads and stores an image texture. More...
class  LineArray
 Object that stores an array of lines and can render them much more efficiently than calling line() multiple times. More...
class  Mesh
 Object that contains and can render mesh data. More...
class  Model
 Object that loads, stores and renders meshes. More...

Public Member Functions

 JMGraphics (int windowWidth, int windowHeight, const char *title)
 JMGraphics (int windowWidth, int windowHeight, const char *title, GLFWmonitor *monitor)
 JMGraphics (int windowWidth, int windowHeight, const char *title, GLFWwindow *parentObject)
 JMGraphics (int windowWidth, int windowHeight, const char *title, GLFWmonitor *monitor, GLFWwindow *parentObject)
 JMGraphics (const JMGraphics &)=delete
JMGraphicsoperator= (const JMGraphics &)=delete
 JMGraphics (JMGraphics &&)=default
JMGraphicsoperator= (JMGraphics &&)=default
bool windowOpen () const
 returns true if the window is open.
void beginDraw ()
 call once each loop before calling other functions.
void endDraw ()
 call once each draw loop at the end of the loop.
Get window properties
bool isPaused () const
 returns true if paused at the end of the loop
bool isRendering () const
 returns true if the object is currently in the render loop.
float width () const
 returns the frame buffer width.
float height () const
 returns the frame buffer height.
int windowHeight ()
 returns the window height including any OS elements such as the title bar.
int windowWidth ()
 returns the window width including any OS elements such as the title bar.
bool mouseMoved () const
 returns true if the mouse was moved in the last frame
float mouseX () const
 returns the mouses window position on the x axis.
float mouseY () const
 returns the mouses window position on the x axis.
float realMouseX () const
 returns the real mouse position (not taking scale factor into account) on the X axis.
float realMouseY () const
 returns the real mouse position (not taking scale factor into account) on the Y axis.
int globalMouseX () const
 returns the mouse position relative to the screen on the x axis
int globalMouseY () const
 returns the mouse position relative to the screen on the x axis
float frameRate () const
 returns the frame rate in frames per second, averaged over the last two frames.
float frameTime () const
 returns the amount of time the last frame took in milliseconds.
int displayWidth () const
 returns the width of the primary display in pixels.
int displayHeight () const
 returns the height of the primary display in pixels.
int displayWidth (int monitor) const
 returns the width of a specified display in pixels.
int displayHeight (int monitor) const
 returns the height of a specified display in pixels.
int displayWidth (GLFWmonitor *monitor) const
 returns the width of a specified display in pixels.
int displayHeight (GLFWmonitor *monitor) const
 returns the height of a specified display in pixels.
int workAreaWidth () const
 returns the width of the work area (display size minus task bar and other ui elements) of the primary display.
int workAreaHeight () const
 returns the height of the work area (display size minus task bar and other ui elements) of the primary display.
int workAreaWidth (int monitor) const
 returns the width of the work area (display size minus task bar and other ui elements) of a specified display.
int workAreaHeight (int monitor) const
 returns the height of the work area (display size minus task bar and other ui elements) of a specified display.
int workAreaWidth (GLFWmonitor *monitor) const
 returns the width of the work area (display size minus task bar and other ui elements) of a specified display.
int workAreaHeight (GLFWmonitor *monitor) const
 returns the wheightidth of the work area (display size minus task bar and other ui elements) of a specified display.
bool windowResized () const
 returns true if the window was resized from the last frame
int monitorNum () const
 returns the number of monitors that are available.
bool scaleFactorChanged () const
glm::mat4 transformationMatrix () const
 returns the current transformation matrix. The matrix is transformed in pixel coordinates, not NDC.
glm::mat4 screenCoordMatrix () const
 returns the matrix used to convert from pixel coordinates to NDC.
Set window properties
void pauseInLoop ()
void resume ()
void forceResume ()
 Forces the thread to resume even if pause was called multiple times.
bool size (int x, int y) const
 sets the window size. Returns true if successfull.
void windowPosition (int x, int y) const
 sets the window position on the display
void maximize () const
 maximizes the window
void minimize () const
 minimizes the window
void fullscreen ()
 sets the window to fullscreen
void fullscreen (int monitor)
 makes the window fullscreen on a specific monitor
void fullscreen (GLFWmonitor *monitor) const
 makes the window fullscreen on a specific monitor
void fullscreen (GLFWmonitor *monitor, int width, int height) const
 makes the window fullscreen on a specific monitor with a specific resolution
void isResizeable (bool resizable) const
 define if the user can resize the monitor
void limitFrameRate (int rate)
 applies an upper frame rate limit. set to zero for unlimited frame rate.
void closeWindow ()
 Sets the window to close at the end of this draw loop.
Matrix transformation
void pushMatrix ()
 pushes the transfromation matrix to the stack
void popMatrix ()
 pops the transformation matrix from the stack
void resetMatrix ()
 resets the transformation matrix
void multiplyMatrix (glm::mat4 matrix)
 Multiplies the transformation matrix by another matrix.
void rotate (float angle)
 applies 2d rotation to the transformation matrix
void rotate (float X, float Y, float Z)
 applies 3d rotation to the transformation matrix in the form of Euler angles.
void rotate (float angle, glm::vec3 axis)
 applies 3d rotation to the transformation matrix in the form of a rotation axis and angle.
void rotateX (float angle)
void rotateY (float angle)
void rotateZ (float angle)
void translate (float x, float y, float z)
 applies translation to the transformation matrix
void translate (float x, float y)
 applies translation to the transformation matrix
void scale (float x, float y, float z)
 applies scale to the transformation matrix
void scale (float x, float y)
 applies scale to the transformation matrix
shape drawing functions
void rect (float posX, float posY, float sizeX, float sizeY)
 draws a rectangle centered on the posX and posY coordinates.
void rect (float x, float y, float sizeX, float sizeY, float radius)
 draws a rectangle with rounded corners centered on the x and y coordinates. radius specifies the radius of the corners.
void gradient (float posX, float posY, float sizeX, float sizeY, glm::vec4 color1, glm::vec4 color2)
 draws a rectangle filled with a gradient. the first color parameter is the left side, the second is the right
void gradient (float posX, float posY, float sizeX, float sizeY, glm::vec3 color1, glm::vec3 color2)
 draws a rectangle filled with a gradient. the first color parameter is the left side, the second is the right
void gradient (float posX, float posY, float sizeX, float sizeY, float color1, float color2)
 draws a rectangle filled with a gradient. the first color parameter is the left side, the second is the right
void ellipse (float x, float y, float sizeX, float sizeY)
 draws an ellipse centered on the x and y coordinates.
void ellipse (float x, float y, float diameter)
 draws a circle centered on the x and y coordinates.
void polygon (float x, float y, float sizeX, float sizeY, int sides)
 draws a symetrical polygin with a specified number of sides, centered on the x and y coordinates.
void polygon (float x, float y, float radius, int sides)
 draws a symetrical polygin with a specified number of sides, centered on the x and y coordinates.
void line (float x1, float y1, float x2, float y2)
 draws a line between two 2D points.
void line (float x1, float y1, float z1, float x2, float y2, float z2)
 draw a line between two 3D points.
void dashedLine (float x1, float y1, float x2, float y2, float dashLength, float gapLength)
 draws a dashed line between two 2D points with specified dash length and gap length.
void dashedLine (float x1, float y1, float z1, float x2, float y2, float z2, float dashLength, float gapLength)
 draws a dashed line between two 3D points with specified dash length and gap length.
void point (float x, float y)
 draws a point at a specified position.
void box (float x, float y, float z)
 draws a 3D box with specified sizes on the x, y and z axis. Draws centered at coordinates 0, 0, 0 so use translate to set position.
void cube (float size)
 draws a cube with a specified side length. Draws centered at coordinates 0, 0, 0 so use translate to set position.
void arc (float x, float y, float sizeX, float sizeY, float startAngle, float endAngle, bool pie)
 draws an arc as a portion of a circle centered on the x and y coordinates. pie boolean sets weather or not to draw the outlines on the inner edges of the arc ends.
void arc (float x, float y, float sizeX, float sizeY, float startAngle, float endAngle)
 draws an arc as a portion of a circle centered on the x and y coordinates.
void image (Buffer *buffer, float x, float y, float sizeX, float sizeY)
 draws the contents of a buffer object centered on the x and y coordinates scaled to a specified x and y size.
void image (Buffer *buffer, float x, float y)
 draws the contents of a buffer object centered on the x and y coordinates.
void image (Buffer3D *buffer, float x, float y, float sizeX, float sizeY)
 draws the contents of a buffer object centered on the x and y coordinates scaled to a specified x and y size.
void image (Buffer3D *buffer, float x, float y)
 draws the contents of a buffer object centered on the x and y coordinates.
void image (Texture *texture, float x, float y, float sizeX, float sizeY)
 draws an image texture centered on the x and y coordinates scaled to a specified x and y size.
void image (Texture *texture, float x, float y)
 draws an image texture centered on the x and y coordinates.
void displayTexture (GLuint texture, float x, float y, float sizeX, float sizeY)
 draws an opengl texture to the screen on a quad.
void text (std::string text, float posX, float posY)
 draws a string of text to the window. Must load and set a font first!
void text (std::string text, float posX, float posY, glm::vec2 &textSize)
 draws a string of text to the window. The size of the text is stored in the textSize vector. Must load and set a font first!
void text (std::string text, float posX, float posY, bool leftJustify)
 draws a string of text to the window. Must load and set a font first!
void text (std::string text, float posX, float posY, bool leftJustify, glm::vec2 &textSize)
 draws a string of text to the window. The size of the text is stored in the textSize vector. Must load and set a font first!

Public Attributes

GLFWwindow * window = nullptr
 The glfw window object. Be careful accessing this directly.
bool roundedStroke = true
 adds rounded corners to the outlines of 2D shapes if true. slightly impacts performance.
bool bufferNearestFilter = false
 If set to true, buffers will use GL_NEAREST for min and mag filters when created.
float scaleFactor = 1.0f
 scales all the graphics including coordinates. if not set to 1.0, 1.0 will not correlate to one pixal. width(), height() and mouse coordinates will also be scaled, but windowWidth() and windowHeight() will not be.
float minWindowHeight = 10.0f
float maxWindowHeight = 8000.0f
float minWindowWidth = 10.0f
float maxWindowWidth = 8000.0f
std::string typedCharacters = ""
 stores all the characters that were typed in the last frame. Resets at the end of each frame.

Other


float smoothIncrease (float input, float scaler) const
 smoothly increases the input value to 1.0f taking the frame rate into account.
float smoothDecrease (float input, float scaler) const
 smoothly decreases the input value to 0.0f taking the frame rate into account.
float linearIncrease (float input, float scaler) const
 linearly increases the input value to 1.0f taking the frame rate into account.
float linearDecrease (float input, float scaler) const
 linearly decreases the input value to 0.0f taking the frame rate into account.

Set shape drawing properties

void fill (float r, float g, float b, float a)
 sets the color used to fill in shaapes. inputs need to be between 0.0 and 1.0.
void stroke (float r, float g, float b, float a)
 sets the color used to outline shapes. inputs need to be between 0.0 and 1.0.
void fill (glm::vec4 color)
 sets the color used to fill in shaapes. inputs need to be between 0.0 and 1.0.
void fill (glm::vec3 color)
 sets the color used to fill in shaapes. inputs need to be between 0.0 and 1.0.
void stroke (glm::vec4 color)
 sets the color used to outline shapes. inputs need to be between 0.0 and 1.0.
void stroke (glm::vec3 color)
 sets the color used to outline shapes. inputs need to be between 0.0 and 1.0.
void fill (float r, float g, float b)
 sets the color used to fill in shapes. inputs need to be between 0.0 and 1.0.
void stroke (float r, float g, float b)
 sets the color used to outline shapes. inputs need to be between 0.0 and 1.0.
void fill (float v)
 sets the color used to fill in shapes. inputs need to be between 0.0 and 1.0.
void stroke (float v)
 sets the color used to outline shapes. inputs need to be between 0.0 and 1.0.
void strokeWeight (float weight)
 sets the width of the outlines on shapes
void noFill ()
 shapes will not be filled in
void noStroke ()
 shapes will have no outline
void setFont (Font *font)
 sets which font to use when drawing text
void textSize (float size)
 scales the font. 1.0 makes it equal to the scale the font was loaded in at. if a font is loaded at a small size and scaled up, quality will be bad.
void background (float r, float g, float b, float a)
 fills the whole window with a background of a specified color. inputs need to be between 0.0 and 1.0.
void background (float r, float g, float b)
 fills the whole window with a background of a specified color. inputs need to be between 0.0 and 1.0.
void background (float v)
 fills the whole window with a background of a specified color. inputs need to be between 0.0 and 1.0.
void textureFxaa (bool FXAAon)
 sets whether or not to use FXAA (fast approx anti-aliasing) when rendering textures amd graphics buffers.
static glm::vec4 lerpColor (glm::vec4 colorA, glm::vec4 colorB, float factor)
 returns a vector storing the rgba values of a color interpolated from colorA to colorB by the factor parameter. factor must be between 0.0f and 1.0f.
static glm::vec4 lerpColor (glm::vec4 colorA, glm::vec4 colorB, glm::vec4 colorC, float factor)
 returns a vector storing the rgba values of a color interpolated from colorA to colorB to colorC by the factor parameter. factor must be between 0.0f and 1.0f.
static glm::vec4 invertColor (glm::vec4 color)
 returns the inverse of the input color. Alpha will be the same as the input color.
static std::string cleanValString (float val)
 Returns a string of a value without trailing zeros.
static std::string cleanValString (double val)
 Returns a string of a value without trailing zeros.

User input

bool mousePressed () const
 returns true if the left mouse button is currently pressed
bool mousePressedRight () const
 returns true if the right mouse button is currently pressed
bool mouseClicked () const
 returns true if the left mouse button was pressed and released
bool mouseDoubleClicked () const
 returns true if the left mouse button was double clicked
bool mouseRightDoubleClicked () const
 returns true if the right mouse button was double clicked
bool mouseClickedRight () const
 returns true if the right mouse button was pressed and released
bool mousePressedLast () const
 returns true if the left mouse button was pressed on the last frame
bool mouseRightPressedLast () const
 returns true if the right mouse button was pressed on the last frame
float scrollX () const
 returns the amount scrolled in the x direction with the mouse wheel or touchpad during the last frame
float scrollY () const
 returns the amount scrolled in the y direction with the mouse wheel or touchpad during the last frame
bool ScrolledLast () const
 returns true if the mouse wheel was scrolled during the last frame
bool keyDown (int key) const
 returns true if the input key is currently pressed. works while multiple keys are pressed at the same time.
bool keyPressed (int key) const
 returns true if the input key was pressed in the last frame
bool keyReleased (int key) const
 returns true if the input key was released in the last frame
bool controlKeyDown () const
 Returns true if either of the control keys are pressed.
bool altKeyDown () const
 Returns true if either of the alt keys are pressed.
bool shiftKeyDown () const
 Returns true if either of the shift keys are pressed.
void on_key_callback (GLFWwindow *window, int key, int scancode, int action, int mods)
void on_character_callback (GLFWwindow *window, unsigned int codepoint)
void on_framebuffer_size_callback (GLFWwindow *window, int width, int height)
void on_scroll_callback (GLFWwindow *window, double xoffset, double yoffset)
static void key_callback (GLFWwindow *window, int key, int scancode, int action, int mods)
static void character_callback (GLFWwindow *window, unsigned int codepoint)
static void framebuffer_size_callback (GLFWwindow *window, int width, int height)
static void scroll_callback (GLFWwindow *window, double xoffset, double yoffset)

Detailed Description

This class is the core of the graphics engine. One instance of this class will open one window, and calling this instances functions will render to that window.
Example of a minimal draw loop:

JMGraphics* gr = new JMGraphics(1920, 1080, "My window");
while(gr->windowOpen())
{
gr->beginDraw();
//draw stuff here
gr->endDraw();
}
delete(gr);
void beginDraw()
call once each loop before calling other functions.
void endDraw()
call once each draw loop at the end of the loop.
bool windowOpen() const
returns true if the window is open.

Aditional glfwWindowHints may be passed before initialization if reqired.

Member Function Documentation

◆ key_callback()

void JMGraphics::key_callback ( GLFWwindow * window,
int key,
int scancode,
int action,
int mods )
inlinestatic

Access class instance from user pointer

Call member function

◆ pauseInLoop()

void JMGraphics::pauseInLoop ( )

pauses at the end of the draw loop in a safe condidion to use draw funcitons from a different thread. Will not pause immediately! If called multiple times, resume() will have to be called the same number of times to resume. Call forceResume() to resume emediately even if paused multiple times.

◆ resume()

void JMGraphics::resume ( )

Resume the draw loop if paused. Needs to be called the same number of times pause() has been called before resuming. Call forceResume() to resume immediately event if paused multiple times.


The documentation for this class was generated from the following file: