FreeRTOS Emulator with SDL2 Based Graphics and Input Library  1.0
A POSIX wrapper to run FreeRTOS on an x86 machine with some basic input and output libraries aimed at making teaching FreeRTOS simpler.
gfx_draw.c File Reference

A SDL2 based library to implement work queue based drawing of graphical elements. Allows for drawing using SDL2 from multiple threads. More...

#include <linux/limits.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_image.h>
#include <pthread.h>
#include "gfx_draw.h"
#include "gfx_font.h"
#include "gfx_utils.h"
#include "gfx_print.h"

Classes

struct  loaded_image
 
struct  loaded_image_crop
 
struct  spritesheet_sequence
 
struct  spritesheet
 
struct  animated_image
 
struct  animated_sequence_instance
 
struct  clear_data
 
struct  arc_data
 
struct  ellipse_data
 
struct  rect_data
 
struct  circle_data
 
struct  line_data
 
struct  poly_data
 
struct  triangle_data
 
struct  image_data
 
struct  loaded_image_data
 
struct  scaled_image_data
 
struct  text_data
 
struct  arrow_data
 
union  data_u
 
struct  draw_job
 
struct  global_offsets
 

Macros

#define ONE_BYTE   8
 
#define TWO_BYTES   16
 
#define THREE_BYTES   24
 
#define MAX_8_BIT   255
 
#define ALPHA_SOLID   MAX_8_BIT
 
#define FIRST_BYTE   0x000000ff
 
#define SECOND_BYTE   0x0000ff00
 
#define THIRD_BYTE   0x00ff0000
 
#define FOURTH_BYTE   0xff000000
 
#define RED_PORTION(COLOUR)   (COLOUR & 0xFF0000) >> TWO_BYTES
 
#define GREEN_PORTION(COLOUR)   (COLOUR & 0x00FF00) >> ONE_BYTE
 
#define BLUE_PORTION(COLOUR)   (COLOUR & 0x0000FF)
 
#define ZERO_ALPHA   0
 
#define PRINT_SDL_ERROR(msg, ...)
 
#define INIT_JOB(JOB, TYPE)
 
#define NS_IN_SECOND   1000000000.0
 
#define MS_IN_SECOND   1000.0
 
#define NS_IN_MS   1000000.0
 

Typedefs

typedef struct loaded_image loaded_image_t
 
typedef struct loaded_image_crop loaded_image_crop_t
 
typedef struct spritesheet_sequence spritesheet_sequence_t
 
typedef struct spritesheet spritesheet_t
 
typedef struct animated_image animated_image_t
 
typedef struct animated_sequence_instance animated_sequence_instance_t
 
typedef struct clear_data clear_data_t
 
typedef struct arc_data arc_data_t
 
typedef struct ellipse_data ellipse_data_t
 
typedef struct rect_data rect_data_t
 
typedef struct circle_data circle_data_t
 
typedef struct line_data line_data_t
 
typedef struct poly_data poly_data_t
 
typedef struct triangle_data triangle_data_t
 
typedef struct image_data image_data_t
 
typedef struct loaded_image_data loaded_image_data_t
 
typedef struct scaled_image_data scaled_image_data_t
 
typedef struct text_data text_data_t
 
typedef struct arrow_data arrow_data_t
 
typedef struct draw_job draw_job_t
 

Enumerations

enum  draw_job_type_t {
  DRAW_NONE = 0 , DRAW_CLEAR , DRAW_ARC , DRAW_ELLIPSE ,
  DRAW_TEXT , DRAW_RECT , DRAW_FILLED_RECT , DRAW_CIRCLE ,
  DRAW_LINE , DRAW_POLY , DRAW_TRIANGLE , DRAW_IMAGE ,
  DRAW_LOADED_IMAGE , DRAW_LOADED_IMAGE_CROP , DRAW_SCALED_IMAGE , DRAW_ARROW
}
 

Functions

void _setErrorMessage (char *msg)
 
gfx_animation_handle_t gfxDrawAnimationCreate (gfx_spritesheet_handle_t spritesheet)
 Creates an animation object with an attached spritesheet that must be loaded prior as an image. More...
 
int gfxDrawAnimationAddSequence (gfx_animation_handle_t animation, char *name, unsigned start_row, unsigned start_col, enum sprite_sequence_direction sprite_step_direction, unsigned frames)
 Adds an animation sequence to a previously created animation. More...
 
gfx_sequence_handle_t gfxDrawAnimationSequenceInstantiate (gfx_animation_handle_t animation, char *sequence_name, unsigned frame_period_ms)
 Creates an instance of an animation from a loaded animation object and a sequence name of a sequence previously added to the animation object. More...
 
int xDrawLoadedImageCropped (loaded_image_t *img, SDL_Renderer *ren, signed short x, signed short y, signed short c_x, signed short c_y, signed short c_w, signed short c_h)
 
int xDrawLoadedImage (loaded_image_t *img, SDL_Renderer *ren, signed short x, signed short y)
 
int gfxDrawUpdateScreen (void)
 Executes the queued draw jobs. More...
 
char * gfxGetErrorMessage (void)
 Returns a string error message from the gfx_draw back end. More...
 
int gfxDrawInit (char *path)
 Initializes the gfx_draw backend. More...
 
int gfxDrawBindThread (void)
 Transfers the drawing ability to the calling thread/taskd. More...
 
void gfxDrawExit (void)
 Exits the GFX Draw backend. More...
 
int gfxDrawText (char *str, signed short x, signed short y, unsigned int colour)
 Prints a string to the screen. More...
 
int gfxGetTextSize (char *str, int *width, int *height)
 Finds the width and height of a strings bounding box. More...
 
int gfxDrawCenteredText (char *str, signed short x, signed short y, unsigned int colour)
 Prints a string to the screen. More...
 
int gfxDrawEllipse (signed short x, signed short y, signed short rx, signed short ry, unsigned int colour)
 Draws an ellipse on the screen. More...
 
int gfxDrawArc (signed short x, signed short y, signed short radius, signed short start, signed short end, unsigned int colour)
 Draws an arc on the screen. More...
 
int gfxDrawFilledBox (signed short x, signed short y, signed short w, signed short h, unsigned int colour)
 Draws an unfilled box on the screen. More...
 
int gfxDrawBox (signed short x, signed short y, signed short w, signed short h, unsigned int colour)
 Draws a filled box on the screen. More...
 
void gfxDrawDuplicateBuffer (void)
 
int gfxDrawClear (unsigned int colour)
 Sets the screen to a solid colour. More...
 
int gfxDrawCircle (signed short x, signed short y, signed short radius, unsigned int colour)
 Draws a filled circle on the screen. More...
 
int gfxDrawLine (signed short x1, signed short y1, signed short x2, signed short y2, unsigned char thickness, unsigned int colour)
 Draws a line on the screen. More...
 
int gfxDrawPoly (coord_t *points, int n, unsigned int colour)
 Draws a polygon on the screen. More...
 
int gfxDrawTriangle (coord_t *points, unsigned int colour)
 Draws a triangle on the screen. More...
 
gfx_image_handle_t gfxDrawLoadScaledImage (char *filename, float scale)
 Loads an image from disk and scales the image, loaded image file can be closed using gfxDrawFreeLoadedImage(). Note that scaled images have large overheads compared to manually scaled images (changing image file's dimensions) More...
 
gfx_image_handle_t gfxDrawLoadImage (char *filename)
 Loads an image file from disk, loaded image file can be closed using gfxDrawFreeLoadedImage() More...
 
int gfxDrawFreeLoadedImage (gfx_image_handle_t *img)
 Closes a loaded image and frees all memory used by the image structure. More...
 
int gfxDrawLoadedImage (gfx_image_handle_t img, signed short x, signed short y)
 Draws a loaded image to the screen. More...
 
int gfxDrawSetLoadedImageScale (gfx_image_handle_t img, float scale)
 Scales a loaded image, the scale is a value where, for example, 1.0 represents the original image's size. The scaling factor scales the image relative to the image file's dimensions on disk. More...
 
float gfxDrawGetLoadedImageScale (gfx_image_handle_t img)
 Retrieves the current scaling factor of an image. More...
 
int gfxDrawGetLoadedImageWidth (gfx_image_handle_t img)
 Retrieves the image's width when drawn to screen, ie. after scaling. More...
 
int gfxDrawGetLoadedImageHeight (gfx_image_handle_t img)
 Retrieves the image's height when drawn to screen, ie. after scaling. More...
 
int gfxDrawGetLoadedImageSize (gfx_image_handle_t img, int *w, int *h)
 Retrieves bother the image's width and height when drawn to screen, ie. after scaling. More...
 
int __attribute_deprecated__ gfxDrawImage (char *filename, signed short x, signed short y)
 Draws an image on the screen. More...
 
spritesheet_tgfxDrawInitSpritesheet (gfx_image_handle_t img)
 
void gfxDrawSpritesheetSetBoundingBox (spritesheet_t *spritesheet, unsigned bounding_x, unsigned bounding_y, unsigned bounding_w, unsigned bounding_h)
 
void gfxDrawSpritesheetSetPadding (spritesheet_t *spritesheet, unsigned padding_x, unsigned padding_y)
 
void gfxDrawSpritesheetSetDivisions (spritesheet_t *spritesheet, unsigned cols, unsigned rows)
 Sets the number of cols and rows and in turn the sprite width and height. Should be called AFTER setting the padding and bounding box. More...
 
gfx_spritesheet_handle_t gfxDrawLoadSpritesheetFromEntireImageUnpadded (gfx_image_handle_t img, unsigned sprite_cols, unsigned sprite_rows)
 Creates a spritesheet object from a loaded image where each sprite is not padded and the sprite sheet is divided into an number of equal sized rows and columns. More...
 
gfx_spritesheet_handle_t gfxDrawLoadSpritesheetFromEntireImagePadded (gfx_image_handle_t img, unsigned sprite_cols, unsigned sprite_rows, unsigned sprite_padding_x, unsigned sprite_padding_y)
 Creates a spritesheet object from a loaded image where sprites are padded. More...
 
gfx_spritesheet_handle_t gfxDrawLoadSpritesheetFromEntireImagePaddedSpacing (gfx_image_handle_t img, unsigned sprite_cols, unsigned sprite_rows, unsigned sprite_spacing_x, unsigned sprite_spacing_y)
 Creates a spritesheet object from a loaded image where sprites are padded. More...
 
gfx_spritesheet_handle_t gfxDrawLoadSpritesheetFromPortionOfImageUnpadded (gfx_image_handle_t img, unsigned sprite_cols, unsigned sprite_rows, unsigned sprite_width, unsigned sprite_height, unsigned bounding_box_left_x_pixel, unsigned bounding_box_top_y_pixel)
 Creates a spritesheet object from a portion of a loaded image where sprites are unpadded. More...
 
gfx_spritesheet_handle_t gfxDrawLoadSpritesheetFromPortionOfImagePadded (gfx_image_handle_t img, unsigned sprite_cols, unsigned sprite_rows, unsigned sprite_width, unsigned sprite_height, unsigned sprite_padding_x, unsigned sprite_padding_y, unsigned bounding_box_left_x_pixel, unsigned bounding_box_top_y_pixel)
 Creates a spritesheet object from a portion of loaded image where sprites are padded. More...
 
gfx_spritesheet_handle_t gfxDrawLoadSpritesheetFromPortionOfImagePaddedSpacing (gfx_image_handle_t img, unsigned sprite_cols, unsigned sprite_rows, unsigned sprite_width, unsigned sprite_height, unsigned sprite_spacing_x, unsigned sprite_spacing_y, unsigned bounding_box_left_x_pixel, unsigned bounding_box_top_y_pixel)
 Creates a spritesheet object from a portion of loaded image where sprites are padded. More...
 
int gfxDrawSprite (gfx_spritesheet_handle_t spritesheet, char column, char row, signed short x, signed short y)
 Draws a sprite from a spritesheet. More...
 
int __attribute_deprecated__ gfxGetImageSize (char *filename, int *w, int *h)
 Gets the width and height of an image. More...
 
int __attribute_deprecated__ gfxDrawScaledImage (char *filename, signed short x, signed short y, float scale)
 Draws a scaled image on the screen. More...
 
int gfxDrawArrow (signed short x1, signed short y1, signed short x2, signed short y2, signed short head_length, unsigned char thickness, unsigned int colour)
 Draws an arrow on the screen. More...
 
void gfxDrawAnimationReset (gfx_sequence_handle_t sequence)
 Resets the time and frame of an animation back to its initial configuration. More...
 
int gfxDrawAnimationDrawFrame (gfx_sequence_handle_t sequence, unsigned ms_timestep, int x, int y)
 Draws the target intantiated animation sequence at a given location. More...
 
int gfxDrawSetGlobalXOffset (int offset)
 Sets the global draw position offset's X axis value. More...
 
int gfxDrawSetGlobalYOffset (int offset)
 Sets the global draw position offset's Y axis value. More...
 
int gfxDrawGetGlobalXOffset (int *offset)
 Retrieves a copy of the current global X axis drawing offset. More...
 
int gfxDrawGetGlobalYOffset (int *offset)
 Retrieves a copy of the current global X axis drawing offset. More...
 

Variables

pthread_mutex_t job_list_lock = PTHREAD_MUTEX_INITIALIZER
 
draw_job_t job_list_head = { 0 }
 
struct global_offsets global_offset
 
pthread_mutex_t loaded_images_lock = PTHREAD_MUTEX_INITIALIZER
 
loaded_image_t loaded_images_list = { 0 }
 
const int screen_height = SCREEN_HEIGHT
 
const int screen_width = SCREEN_WIDTH
 
SDL_Window * window = NULL
 
SDL_Renderer * renderer = NULL
 
SDL_GLContext context = NULL
 
char * error_message = NULL
 

Detailed Description

A SDL2 based library to implement work queue based drawing of graphical elements. Allows for drawing using SDL2 from multiple threads.

Author
Alex Hoffman
Date
27 August 2019
   ----------------------------------------------------------------------
    Copyright (C) Alexander Hoffman, 2019
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   ----------------------------------------------------------------------

Macro Definition Documentation

◆ ALPHA_SOLID

#define ALPHA_SOLID   MAX_8_BIT

◆ BLUE_PORTION

#define BLUE_PORTION (   COLOUR)    (COLOUR & 0x0000FF)

◆ FIRST_BYTE

#define FIRST_BYTE   0x000000ff

◆ FOURTH_BYTE

#define FOURTH_BYTE   0xff000000

◆ GREEN_PORTION

#define GREEN_PORTION (   COLOUR)    (COLOUR & 0x00FF00) >> ONE_BYTE

◆ INIT_JOB

#define INIT_JOB (   JOB,
  TYPE 
)
Value:
draw_job_t *JOB = _pushDrawJob(); \
if (!JOB) \
return -1; \
union data_u *data = calloc(1, sizeof(union data_u)); \
if (data == NULL) \
logCriticalError("job->data alloc"); \
JOB->data = data; \
JOB->type = TYPE;
Definition: gfx_draw.c:245
Definition: gfx_draw.c:228

◆ MAX_8_BIT

#define MAX_8_BIT   255

◆ MS_IN_SECOND

#define MS_IN_SECOND   1000.0

◆ NS_IN_MS

#define NS_IN_MS   1000000.0

◆ NS_IN_SECOND

#define NS_IN_SECOND   1000000000.0

◆ ONE_BYTE

#define ONE_BYTE   8

◆ PRINT_SDL_ERROR

#define PRINT_SDL_ERROR (   msg,
  ... 
)
Value:
PRINT_ERROR("[SDL Error] %s\n" #msg, (char *)SDL_GetError(), \
##__VA_ARGS__)
#define PRINT_ERROR(msg,...)
Definition: gfx_print.h:76

◆ RED_PORTION

#define RED_PORTION (   COLOUR)    (COLOUR & 0xFF0000) >> TWO_BYTES

◆ SECOND_BYTE

#define SECOND_BYTE   0x0000ff00

◆ THIRD_BYTE

#define THIRD_BYTE   0x00ff0000

◆ THREE_BYTES

#define THREE_BYTES   24

◆ TWO_BYTES

#define TWO_BYTES   16

◆ ZERO_ALPHA

#define ZERO_ALPHA   0

Typedef Documentation

◆ animated_image_t

◆ animated_sequence_instance_t

◆ arc_data_t

typedef struct arc_data arc_data_t

◆ arrow_data_t

typedef struct arrow_data arrow_data_t

◆ circle_data_t

typedef struct circle_data circle_data_t

◆ clear_data_t

typedef struct clear_data clear_data_t

◆ draw_job_t

typedef struct draw_job draw_job_t

◆ ellipse_data_t

typedef struct ellipse_data ellipse_data_t

◆ image_data_t

typedef struct image_data image_data_t

◆ line_data_t

typedef struct line_data line_data_t

◆ loaded_image_crop_t

◆ loaded_image_data_t

◆ loaded_image_t

typedef struct loaded_image loaded_image_t

◆ poly_data_t

typedef struct poly_data poly_data_t

◆ rect_data_t

typedef struct rect_data rect_data_t

◆ scaled_image_data_t

◆ spritesheet_sequence_t

◆ spritesheet_t

typedef struct spritesheet spritesheet_t

◆ text_data_t

typedef struct text_data text_data_t

◆ triangle_data_t

Enumeration Type Documentation

◆ draw_job_type_t

Enumerator
DRAW_NONE 
DRAW_CLEAR 
DRAW_ARC 
DRAW_ELLIPSE 
DRAW_TEXT 
DRAW_RECT 
DRAW_FILLED_RECT 
DRAW_CIRCLE 
DRAW_LINE 
DRAW_POLY 
DRAW_TRIANGLE 
DRAW_IMAGE 
DRAW_LOADED_IMAGE 
DRAW_LOADED_IMAGE_CROP 
DRAW_SCALED_IMAGE 
DRAW_ARROW 

Function Documentation

◆ _setErrorMessage()

void _setErrorMessage ( char *  msg)

◆ gfxDrawInitSpritesheet()

spritesheet_t* gfxDrawInitSpritesheet ( gfx_image_handle_t  img)

◆ gfxDrawSpritesheetSetBoundingBox()

void gfxDrawSpritesheetSetBoundingBox ( spritesheet_t spritesheet,
unsigned  bounding_x,
unsigned  bounding_y,
unsigned  bounding_w,
unsigned  bounding_h 
)

◆ gfxDrawSpritesheetSetDivisions()

void gfxDrawSpritesheetSetDivisions ( spritesheet_t spritesheet,
unsigned  cols,
unsigned  rows 
)

Sets the number of cols and rows and in turn the sprite width and height. Should be called AFTER setting the padding and bounding box.

Parameters
spritesheetSprite sheet to be set
colsNumber of columns on the spritesheet
rowsNumber of rows on the spritesheet

◆ gfxDrawSpritesheetSetPadding()

void gfxDrawSpritesheetSetPadding ( spritesheet_t spritesheet,
unsigned  padding_x,
unsigned  padding_y 
)

◆ xDrawLoadedImage()

int xDrawLoadedImage ( loaded_image_t img,
SDL_Renderer *  ren,
signed short  x,
signed short  y 
)

◆ xDrawLoadedImageCropped()

int xDrawLoadedImageCropped ( loaded_image_t img,
SDL_Renderer *  ren,
signed short  x,
signed short  y,
signed short  c_x,
signed short  c_y,
signed short  c_w,
signed short  c_h 
)

Variable Documentation

◆ context

SDL_GLContext context = NULL

◆ error_message

char* error_message = NULL

◆ global_offset

struct global_offsets global_offset
Initial value:
= {
.lock = PTHREAD_MUTEX_INITIALIZER,
}

◆ job_list_head

draw_job_t job_list_head = { 0 }

◆ job_list_lock

pthread_mutex_t job_list_lock = PTHREAD_MUTEX_INITIALIZER

◆ loaded_images_list

loaded_image_t loaded_images_list = { 0 }

◆ loaded_images_lock

pthread_mutex_t loaded_images_lock = PTHREAD_MUTEX_INITIALIZER

◆ renderer

SDL_Renderer* renderer = NULL

◆ screen_height

const int screen_height = SCREEN_HEIGHT

◆ screen_width

const int screen_width = SCREEN_WIDTH

◆ window

SDL_Window* window = NULL