FreeRTOS Tetris
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
TUM_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 <limits.h>
#include <stdlib.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_image.h>
#include <pthread.h>
#include "TUM_Draw.h"
#include "TUM_Font.h"
#include "TUM_Utils.h"
Include dependency graph for TUM_Draw.c:

Data Structures

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

static uint32_t SwapBytes (unsigned int x)
 
void setErrorMessage (char *msg)
 
static draw_job_tpushDrawJob (void)
 
static draw_job_tpopDrawJob (void)
 
static int _clearDisplay (unsigned int colour)
 
static int _drawRectangle (signed short x, signed short y, signed short w, signed short h, unsigned int colour)
 
static int _drawFilledRectangle (signed short x, signed short y, signed short w, signed short h, unsigned int colour)
 
static int _drawArc (signed short x, signed short y, signed short radius, signed short start, signed short end, unsigned int colour)
 
static int _drawEllipse (signed short x, signed short y, signed short rx, signed short ry, unsigned int colour)
 
static int _drawCircle (signed short x, signed short y, signed short radius, unsigned int colour)
 
static int _drawLine (signed short x1, signed short y1, signed short x2, signed short y2, unsigned char thickness, unsigned int colour)
 
static int _drawPoly (coord_t *points, unsigned int n, int x_offset, int y_offset, signed short colour)
 
static int _drawTriangle (coord_t *points, int x_offset, int y_offset, unsigned int colour)
 
static SDL_Texture * loadImage (char *filename, SDL_Renderer *ren)
 
static int _renderCroppedImage (SDL_Texture *tex, SDL_Renderer *ren, signed short x, signed short y, signed short c_x, signed short c_y, int w, int h)
 
static int _renderScaledImage (SDL_Texture *tex, SDL_Renderer *ren, signed short x, signed short y, int w, int h)
 
static int _getImageSize (char *filename, int *w, int *h)
 
animation_handle_t tumDrawAnimationCreate (image_handle_t spritesheet, unsigned sprite_cols, unsigned sprite_rows)
 Creates an animation object with an attached spritesheet that must be loaded prior as an image. More...
 
int tumDrawAnimationAddSequence (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...
 
sequence_handle_t tumDrawAnimationSequenceInstantiate (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...
 
static int freeLoadedImage (loaded_image_t **img)
 
static void vPutLoadedImage (image_handle_t img)
 
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)
 
static int _drawScaledImage (SDL_Texture *tex, SDL_Renderer *ren, signed short x, signed short y, float scale)
 
static int _drawImage (SDL_Texture *tex, SDL_Renderer *ren, signed short x, signed short y)
 
static int _drawText (char *string, signed short x, signed short y, unsigned int colour, TTF_Font *font)
 
static int _getTextSize (char *string, int *width, int *height)
 
static int _drawArrow (signed short x1, signed short y1, signed short x2, signed short y2, signed short head_length, unsigned char thickness, unsigned int colour)
 
static int vHandleDrawJob (draw_job_t *job)
 
static void logCriticalError (char *msg)
 
int tumDrawUpdateScreen (void)
 Executes the queued draw jobs. More...
 
char * tumGetErrorMessage (void)
 Returns a string error message from the TUM Draw back end. More...
 
int tumDrawInit (char *path)
 Initializes the TUM Draw backend. More...
 
int tumDrawBindThread (void)
 Transfers the drawing ability to the calling thread/taskd. More...
 
void tumDrawExit (void)
 Exits the TUM Draw backend. More...
 
int tumDrawText (char *str, signed short x, signed short y, unsigned int colour)
 Prints a string to the screen. More...
 
int tumGetTextSize (char *str, int *width, int *height)
 Finds the width and height of a strings bounding box. More...
 
int tumDrawEllipse (signed short x, signed short y, signed short rx, signed short ry, unsigned int colour)
 Draws an ellipse on the screen. More...
 
int tumDrawArc (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 tumDrawFilledBox (signed short x, signed short y, signed short w, signed short h, unsigned int colour)
 Draws an unfilled box on the screen. More...
 
int tumDrawBox (signed short x, signed short y, signed short w, signed short h, unsigned int colour)
 Draws a filled box on the screen. More...
 
void tumDrawDuplicateBuffer (void)
 
int tumDrawClear (unsigned int colour)
 Sets the screen to a solid colour. More...
 
int tumDrawCircle (signed short x, signed short y, signed short radius, unsigned int colour)
 Draws a filled circle on the screen. More...
 
int tumDrawLine (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 tumDrawPoly (coord_t *points, int n, unsigned int colour)
 Draws a polygon on the screen. More...
 
int tumDrawTriangle (coord_t *points, unsigned int colour)
 Draws a triangle on the screen. More...
 
image_handle_t tumDrawLoadScaledImage (char *filename, float scale)
 Loads an image from disk and scales the image, loaded image file can be closed using tumDrawFreeLoadedImage(). Note that scaled images have large overheads compared to manually scaled images (changing image file's dimensions) More...
 
image_handle_t tumDrawLoadImage (char *filename)
 Loads an image file from disk, loaded image file can be closed using tumDrawFreeLoadedImage() More...
 
int tumDrawFreeLoadedImage (image_handle_t *img)
 Closes a loaded image and frees all memory used by the image structure. More...
 
int tumDrawLoadedImage (image_handle_t img, signed short x, signed short y)
 Draws a loaded image to the screen. More...
 
int tumDrawSetLoadedImageScale (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 tumDrawGetLoadedImageScale (image_handle_t img)
 Retrieves the current scaling factor of an image. More...
 
int tumDrawGetLoadedImageWidth (image_handle_t img)
 Retrieves the image's width when drawn to screen, ie. after scaling. More...
 
int tumDrawGetLoadedImageHeight (image_handle_t img)
 Retrieves the image's height when drawn to screen, ie. after scaling. More...
 
int tumDrawGetLoadedImageSize (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__ tumDrawImage (char *filename, signed short x, signed short y)
 Draws an image on the screen. More...
 
int __attribute_deprecated__ tumGetImageSize (char *filename, int *w, int *h)
 Gets the width and height of an image. More...
 
int __attribute_deprecated__ tumDrawScaledImage (char *filename, signed short x, signed short y, float scale)
 Draws a scaled image on the screen. More...
 
int tumDrawArrow (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...
 
int tumDrawAnimationDrawFrame (sequence_handle_t sequence, unsigned ms_timestep, int x, int y)
 Draws the target intantiated animation sequence at a given location. More...
 
int tumDrawSetGlobalXOffset (int offset)
 Sets the global draw position offset's X axis value. More...
 
int tumDrawSetGlobalYOffset (int offset)
 Sets the global draw position offset's Y axis value. More...
 
int tumDrawGetGlobalXOffset (int *offset)
 Retrieves a copy of the current global X axis drawing offset. More...
 
int tumDrawGetGlobalYOffset (int *offset)
 Retrieves a copy of the current global X axis drawing offset. More...
 

Variables

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

◆ 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;

◆ PRINT_SDL_ERROR

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

Variable Documentation

◆ global_offset

struct global_offsets global_offset
Initial value:
= {
.lock = PTHREAD_MUTEX_INITIALIZER,
}
data_u
Definition: TUM_Draw.c:220
draw_job
Definition: TUM_Draw.c:237