GrooveStomp's 3D Software Renderer  0.1.0
Classes | Functions
graphics.c File Reference
#include <string.h>
#include <stdio.h>
#include "SDL2/SDL.h"
#include "math.h"
#include "graphics.h"
#include "texture.h"
#include "color.h"
Include dependency graph for graphics.c:

Classes

struct  graphics
 Graphics state. More...
 

Functions

void swap_generic (void *v1, void *v2, size_t size)
 A mostly generic implementation of swap. More...
 
struct graphicsGraphicsInit (char *title, int width, int height, int scale)
 Creates and initializes a new graphics object isntance. More...
 
void GraphicsDeinit (struct graphics *g)
 De-initializes and frees memory for the given graphics object. More...
 
void GraphicsBegin (struct graphics *graphics)
 Initializes the graphics subsystem for drawing routines. More...
 
void GraphicsEnd (struct graphics *graphics)
 Prepares the graphics subsystem for presentation, then presents. More...
 
void GraphicsClearScreen (struct graphics *graphics, unsigned int color)
 Sets all pixels in the screen to the given color. More...
 
void PutPixelScaled (struct graphics *graphics, int x, int y, unsigned int color)
 Scale the pixel being drawn. More...
 
void PutPixel (struct graphics *graphics, int x, int y, unsigned int color)
 Put a pixel into the graphics buffer. More...
 
void GraphicsDrawLine (struct graphics *graphics, int x1, int y1, int x2, int y2, unsigned int color)
 Draws a line from (x1,y1) to (x2,y2) More...
 
void GraphicsTriangleTextured (struct graphics *graphics, struct triangle tri, struct texture *texture)
 Draw a textured triangle with the given set of x and y coordinates. More...
 
void GraphicsTriangleWireframe (struct graphics *graphics, struct triangle triangle, unsigned int color)
 Draw a triangle with the given set of x and y coordinates. More...
 
void TriangleSolidDrawLine (struct graphics *graphics, int xmin, int xmax, int y, unsigned int color)
 Used internally by GraphicsTriangleSolid()
 
void GraphicsTriangleSolid (struct graphics *graphics, struct triangle triangle, unsigned int color)
 Draw a triangle with the given set of x and y coordinates. More...
 

Function Documentation

◆ GraphicsBegin()

void GraphicsBegin ( struct graphics graphics)

Initializes the graphics subsystem for drawing routines.

Internally locks streaming texture for direct manipulation.

Parameters
[in,out]graphicsGraphics state to be manipulated

◆ GraphicsClearScreen()

void GraphicsClearScreen ( struct graphics graphics,
unsigned int  color 
)

Sets all pixels in the screen to the given color.

Parameters
[in,out]graphicsGraphics state to be manipulated
[in]color32-bit color with 8-bits per component: (R,G,B,A)

◆ GraphicsDeinit()

void GraphicsDeinit ( struct graphics graphics)

De-initializes and frees memory for the given graphics object.

Parameters
[in,out]graphicsThe initialized opcode object to be cleaned and reclaimed

◆ GraphicsDrawLine()

void GraphicsDrawLine ( struct graphics graphics,
int  x1,
int  y1,
int  x2,
int  y2,
unsigned int  color 
)

Draws a line from (x1,y1) to (x2,y2)

Used by GraphicsTriangleWireframe() and GraphicsTriangleSolid()

Parameters
[in,out]graphicsGraphics state to be manipulated
[in]x1horizontal position of the line start.
[in]y1vertical position of the line start.
[in]x2horizontal position of the line end.
[in]y2vertical position of the line end.
[in]colorcolor to render the line with.

◆ GraphicsEnd()

void GraphicsEnd ( struct graphics graphics)

Prepares the graphics subsystem for presentation, then presents.

Internally unlocks streaming texture then calls presentation routines.

Parameters
[in,out]graphicsGraphics state to be manipulated.

◆ GraphicsInit()

struct graphics* GraphicsInit ( char *  title,
int  width,
int  height,
int  scale 
)

Creates and initializes a new graphics object isntance.

Scale can be specified as a non-negative number. This value is used to multiply both the width and the height and the pixel size of any drawing operations.

For example, specifying a scale of 2 would multiply the width by 2, the height by 2, and every pixel would be 2x2; so the total scale factor ends up being scale^2

Parameters
[in]titleThe title displayed in the window titlebar
[in]widthWidth of the display area of the window, in pixels
[in]heightHeight of the display are of the window, in pixels
[in]scaleSize and rendering scale, natural number multiple
Returns
The initialized graphics object

◆ GraphicsTriangleSolid()

void GraphicsTriangleSolid ( struct graphics graphics,
struct triangle  triangle,
unsigned int  color 
)

Draw a triangle with the given set of x and y coordinates.

Fills the specified polygon with the given color.

Parameters
[in,out]graphicsGraphics state to be changed
[in]triangleThe triangle to draw
[in]colorWhat color the solid triangle should be rendered with
See also
Source: http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html

◆ GraphicsTriangleTextured()

void GraphicsTriangleTextured ( struct graphics graphics,
struct triangle  tri,
struct texture texture 
)

Draw a textured triangle with the given set of x and y coordinates.

Fills the specified polygon with the given texture.

Parameters
[in,out]graphicsGraphics state to be changed
[in]triThe triangle to draw
[in]textureWhat texture to sample while drawing the solid triangle
See also
Source: https://github.com/OneLoneCoder/videos/blob/master/OneLoneCoder_olcEngine3D_Part4.cpp

◆ GraphicsTriangleWireframe()

void GraphicsTriangleWireframe ( struct graphics graphics,
struct triangle  triangle,
unsigned int  color 
)

Draw a triangle with the given set of x and y coordinates.

Only draws the lines, doesn't fill the polygon.

Parameters
[in,out]graphicsGraphics state to be changed
[in]triangleThe triangle to draw
[in]colorWhat color the wireframe should be rendered with

◆ PutPixel()

void PutPixel ( struct graphics graphics,
int  x,
int  y,
unsigned int  color 
)

Put a pixel into the graphics buffer.

Parameters
[in,out]graphicsGraphics state to be manipulated
[in]xhorizontal position in display buffer (assuming no scaling)
[in]yvertical position in display buffer (assuming no scaling)
[in]colorColor to put into display buffer

◆ PutPixelScaled()

void PutPixelScaled ( struct graphics graphics,
int  x,
int  y,
unsigned int  color 
)

Scale the pixel being drawn.

This renders the given pixel, scaled as per graphics->scale.

When the graphics state is initialized, a width and height are specified.

Internally the graphics state multiplies both of these values by the scale and stores a buffer of the resulting size. This function allows us to treat the resulting scaled buffer as if it were the original size requested.

Parameters
[in,out]graphicsGraphics state to be manipulated
[in]xhorizontal position in display buffer (assuming no scaling)
[in]yvertical position in display buffer (assuming no scaling)
[in]colorColor to put into display buffer

◆ swap_generic()

void swap_generic ( void *  v1,
void *  v2,
size_t  size 
)

A mostly generic implementation of swap.

Both v1 and v2 must point to data that is the same size, as specified in the size parameter.

Parameters
[in,out]v1first value
[in,out]v2second value
[in]sizev1 and v2 must each be this size