Vulkan2D
2D renderer written in C using Vulkan and SDL2
Renderer.h
Go to the documentation of this file.
1 #pragma once
67 #include "VK2D/Structs.h"
68 #include <SDL2/SDL.h>
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
98 VK2DResult vk2dRendererInit(SDL_Window *window, VK2DRendererConfig config, VK2DStartupOptions *options);
99 
104 
107 
111 VK2DRenderer vk2dRendererGetPointer();
112 
120 
126 
133 
138 void vk2dRendererStartFrame(const vec4 clearColour);
139 
143 
146 VK2DLogicalDevice vk2dRendererGetDevice();
147 
154 void vk2dRendererSetTarget(VK2DTexture target);
155 
159 
163 
167 
173 
182 void vk2dRendererSetTextureCamera(bool useCameraOnTextures);
183 
187 
195 
199 
203 
206 
209 
212 
216 
225 void vk2dRendererDrawRectangle(float x, float y, float w, float h, float r, float ox, float oy);
226 
236 void vk2dRendererDrawRectangleOutline(float x, float y, float w, float h, float r, float ox, float oy, float lineWidth);
237 
242 void vk2dRendererDrawCircle(float x, float y, float r);
243 
249 void vk2dRendererDrawCircleOutline(float x, float y, float r, float lineWidth);
250 
256 void vk2dRendererDrawLine(float x1, float y1, float x2, float y2);
257 
271 void vk2dRendererDrawTexture(VK2DTexture tex, float x, float y, float xscale, float yscale, float rot, float originX,
272  float originY, float xInTex, float yInTex, float texWidth, float texHeight);
273 
282 void vk2dRendererAddBatch(VK2DDrawCommand *commands, uint32_t count);
283 
299 void
300 vk2dRendererDrawShader(VK2DShader shader, void *data, VK2DTexture tex, float x, float y, float xscale, float yscale,
301  float rot, float originX, float originY, float xInTex, float yInTex, float texWidth,
302  float texHeight);
303 
316 void vk2dRendererDrawPolygon(VK2DPolygon polygon, float x, float y, bool filled, float lineWidth, float xscale, float yscale, float rot, float originX, float originY);
317 
334 void vk2dRendererDrawGeometry(VK2DVertexColour *vertices, int count, float x, float y, bool filled, float lineWidth, float xscale, float yscale, float rot, float originX, float originY);
335 
340 void vk2dRendererDrawShadows(VK2DShadowEnvironment shadowEnvironment, vec4 colour, vec2 lightSource);
341 
361 void
362 vk2dRendererDrawModel(VK2DModel model, float x, float y, float z, float xscale, float yscale, float zscale, float rot,
363  vec3 axis, float originX, float originY, float originZ);
364 
382 void vk2dRendererDrawWireframe(VK2DModel model, float x, float y, float z, float xscale, float yscale, float zscale,
383  float rot, vec3 axis, float originX, float originY, float originZ, float lineWidth);
384 
388 void vk2dColourHex(vec4 dst, const char *hex);
389 
393 void vk2dColourInt(vec4 dst, uint32_t colour);
394 
401 void vk2dColourRGBA(vec4 dst, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
402 
416 
425 float vk2dRandom(float min, float max);
426 
441 void vk2dAssetsLoad(VK2DAssetLoad *assets, uint32_t count);
442 
450 
455 
459 
463 void vk2dAssetsFree(VK2DAssetLoad *assets, uint32_t count);
464 
470 void vk2dAssetsSetTextureFile(VK2DAssetLoad *array, int index, const char *filename, VK2DTexture *outVar);
471 
478 void vk2dAssetsSetTextureMemory(VK2DAssetLoad *array, int index, void *buffer, int size, VK2DTexture *outVar);
479 
486 void vk2dAssetsSetModelFile(VK2DAssetLoad *array, int index, const char *filename, VK2DTexture *texture, VK2DModel *outVar);
487 
495 void vk2dAssetsSetModelMemory(VK2DAssetLoad *array, int index, void *buffer, int size, VK2DTexture *texture, VK2DModel *outVar);
496 
504 void vk2dAssetsSetShaderFile(VK2DAssetLoad *array, int index, const char *vertexFilename, const char *fragmentFilename, uint32_t uniformBufferSize, VK2DShader *outVar);
505 
515 void vk2dAssetsSetShaderMemory(VK2DAssetLoad *array, int index, void *vertexBuffer, int vertexBufferSize, void *fragmentBuffer, int fragmentBufferSize, uint32_t uniformBufferSize, VK2DShader *outVar);
516 
522 void vk2dSleep(double seconds);
523 
527 
537 
544 const char *vk2dStatusMessage();
545 
549 const char *vk2dHostInformation();
550 
551 /************************* Shorthand for simpler drawing at no performance cost *************************/
552 
554 #define vk2dDrawRectangle(x, y, w, h) vk2dRendererDrawRectangle(x, y, w, h, 0, 0, 0)
555 
557 #define vk2dDrawRectangleOutline(x, y, w, h, lw) vk2dRendererDrawRectangleOutline(x, y, w, h, 0, 0, 0, lw)
558 
560 #define vk2dDrawCircle(x, y, r) vk2dRendererDrawCircle(x, y, r)
561 
563 #define vk2dDrawCircleOutline(x, y, r, w) vk2dRendererDrawCircleOutline(x, y, r, w)
564 
566 #define vk2dDrawLine(x1, y1, x2, y2) vk2dRendererDrawLine(x1, y1, x2, y2)
567 
569 #define vk2dDrawShader(shader, data, texture, x, y) vk2dRendererDrawShader(shader, data, texture, x, y, 1, 1, 0, 0, 0, 0, 0, vk2dTextureWidth(texture), vk2dTextureHeight(texture))
570 
572 #define vk2dDrawTexture(texture, x, y) vk2dRendererDrawTexture(texture, x, y, 1, 1, 0, 0, 0, 0, 0, vk2dTextureWidth(texture), vk2dTextureHeight(texture))
573 
575 #define vk2dDrawTextureExt(texture, x, y, xscale, yscale, rot, originX, originY) vk2dRendererDrawTexture(texture, x, y, xscale, yscale, rot, originX, originY, 0, 0, vk2dTextureWidth(texture), vk2dTextureHeight(texture))
576 
578 #define vk2dDrawTexturePart(texture, x, y, xPos, yPos, w, h) vk2dRendererDrawTexture(texture, x, y, 1, 1, 0, 0, 0, xPos, yPos, w, h)
579 
581 #define vk2dDrawPolygonOutline(polygon, x, y, width) vk2dRendererDrawPolygon(polygon, x, y, false, width, 1, 1, 0, 0, 0)
582 
584 #define vk2dDrawPolygon(polygon, x, y) vk2dRendererDrawPolygon(polygon, x, y, true, 0, 1, 1, 0, 0, 0)
585 
587 #define vk2dDrawModel(model, x, y, z) vk2dRendererDrawModel(model, x, y, z, 1, 1, 1, 0, 1, 0, 0, 0)
588 
590 #define vk2dDrawModelExt(model, x, y, z, xscale, yscale, zscale) vk2dRendererDrawModel(model, x, y, z, xscale, yscale, zscale, 0, 1, 0, 0, 0)
591 
593 #define vk2dDrawWireframe(model, x, y, z) vk2dRendererDrawWireframe(model, x, y, z, 1, 1, 1, 0, 1, 0, 0, 0, 1)
594 
596 #define vk2dDrawWireframeExt(model, x, y, z, xscale, yscale, zscale, lineWidth) vk2dRendererDrawModel(model, x, y, z, xscale, yscale, zscale, 0, 1, 0, 0, 0, lineWidth)
597 
598 #ifdef __cplusplus
599 }
600 #endif
float vk2dAssetsLoadStatus()
Returns the loading status as a percentage from 0-1.
void vk2dRendererUnlockCameras()
Unlocking cameras means that all cameras will be drawn to again.
void vk2dRendererDrawPolygon(VK2DPolygon polygon, float x, float y, bool filled, float lineWidth, float xscale, float yscale, float rot, float originX, float originY)
Renders a polygon.
void vk2dRendererDrawWireframe(VK2DModel model, float x, float y, float z, float xscale, float yscale, float zscale, float rot, vec3 axis, float originX, float originY, float originZ, float lineWidth)
Renders a 3D model as a wireframe.
void vk2dRendererLockCameras(VK2DCameraIndex cam)
Locking cameras means that only the specified camera will be drawn to and all others ignored.
void vk2dColourHex(vec4 dst, const char *hex)
Converts a Hex colour into a vec4 normalized colour.
void vk2dAssetsSetTextureMemory(VK2DAssetLoad *array, int index, void *buffer, int size, VK2DTexture *outVar)
Sets up a VK2DAssetLoad array entry for a TextureMemory entry.
double vk2dRendererGetAverageFrameTime()
Gets the average amount of time frames are taking to process from the start of vk2dRendererStartFrame...
void vk2dRendererDrawLine(float x1, float y1, float x2, float y2)
Draws a line using the current rendering colour.
VK2DResult vk2dRendererInit(SDL_Window *window, VK2DRendererConfig config, VK2DStartupOptions *options)
Initializes VK2D's renderer.
void vk2dRendererAddBatch(VK2DDrawCommand *commands, uint32_t count)
Adds a user-provided draw batch to the current draw batch, flushing if necessary.
void vk2dRendererSetColourMod(const vec4 mod)
Sets the current colour modifier (Colour all pixels are blended with)
void vk2dRendererDrawRectangle(float x, float y, float w, float h, float r, float ox, float oy)
Draws a rectangle using the current rendering colour.
void vk2dRendererEmpty()
Clears the content so that every pixel in the target is set to be complete transparent (useful for ne...
void vk2dAssetsLoad(VK2DAssetLoad *assets, uint32_t count)
Loads a number of assets in a background thread.
bool vk2dStatusFatal()
Returns true if the current status code should be considered fatal.
void vk2dAssetsSetModelFile(VK2DAssetLoad *array, int index, const char *filename, VK2DTexture *texture, VK2DModel *outVar)
Sets up a VK2DAssetLoad array entry for a ModelFile entry.
void vk2dAssetsSetShaderMemory(VK2DAssetLoad *array, int index, void *vertexBuffer, int vertexBufferSize, void *fragmentBuffer, int fragmentBufferSize, uint32_t uniformBufferSize, VK2DShader *outVar)
Sets up a VK2DAssetLoad array entry for a ShaderMemory entry.
VK2DCameraSpec vk2dRendererGetCamera()
Returns the camera spec of the default camera, this is equivalent to calling vk2dCameraGetSpec(VK2D_D...
void vk2dSleep(double seconds)
Combines busy loops and SDL_Delay for a more accurate sleep function.
void vk2dRendererDrawShader(VK2DShader shader, void *data, VK2DTexture tex, float x, float y, float xscale, float yscale, float rot, float originX, float originY, float xInTex, float yInTex, float texWidth, float texHeight)
Renders a texture.
void vk2dRendererResetSwapchain()
Forces the renderer to rebuild itself (VK2D does this automatically)
void vk2dRendererDrawCircleOutline(float x, float y, float r, float lineWidth)
Draws a circle using the current rendering colour.
void vk2dRendererSetBlendMode(VK2DBlendMode blendMode)
Sets the rendering blend mode (does nothing if VK2D_GENERATE_BLEND_MODES is disabled)
void vk2dAssetsSetShaderFile(VK2DAssetLoad *array, int index, const char *vertexFilename, const char *fragmentFilename, uint32_t uniformBufferSize, VK2DShader *outVar)
Sets up a VK2DAssetLoad array entry for a ShaderFile entry.
float vk2dRandom(float min, float max)
Returns a random number between min and max, thread safe.
void vk2dRendererDrawModel(VK2DModel model, float x, float y, float z, float xscale, float yscale, float zscale, float rot, vec3 axis, float originX, float originY, float originZ)
Renders a 3D model.
VK2DLogicalDevice vk2dRendererGetDevice()
Returns the logical device being used by the renderer.
void vk2dAssetsWait()
Waits until all of the assets provided to vk2dAssetsLoad have been loaded.
bool vk2dAssetsLoadComplete()
Returns true if the assets thread is done loading assets.
VK2DResult vk2dRendererEndFrame()
Completes the end-of-frame drawing tasks.
void vk2dRendererDrawTexture(VK2DTexture tex, float x, float y, float xscale, float yscale, float rot, float originX, float originY, float xInTex, float yInTex, float texWidth, float texHeight)
Renders a texture.
void vk2dAssetsSetTextureFile(VK2DAssetLoad *array, int index, const char *filename, VK2DTexture *outVar)
Sets up a VK2DAssetLoad array entry for a TextureFile entry.
void vk2dRendererGetColourMod(vec4 dst)
Gets the current colour modifier.
VK2DRenderer vk2dRendererGetPointer()
Gets the internal renderer's pointer.
VK2DBlendMode vk2dRendererGetBlendMode()
Gets the current blend mode (will return whatever was set with vk2dRendererSetBlendMode regardless of...
void vk2dRendererClear()
Clears the current render target to the current renderer colour.
void vk2dRendererSetTextureCamera(bool useCameraOnTextures)
Allows you to enable or disable the use of the renderer's camera when drawing to textures.
void vk2dRendererSetConfig(VK2DRendererConfig config)
Resets the renderer with a new configuration.
void vk2dRendererSetTarget(VK2DTexture target)
Changes the render target to a texture or the screen.
void vk2dColourInt(vec4 dst, uint32_t colour)
Converts a Int colour into a vec4 normalized colour.
void vk2dRendererFlushSpriteBatch()
Forces the renderer to flush the current sprite batch The renderer automatically does this in a few c...
void vk2dRendererQuit()
Frees resources used by the renderer.
void vk2dRendererDrawGeometry(VK2DVertexColour *vertices, int count, float x, float y, bool filled, float lineWidth, float xscale, float yscale, float rot, float originX, float originY)
Draws arbitrary geometry without needing to pre-allocate a polygon.
void vk2dRendererStartFrame(const vec4 clearColour)
Sets up the renderer to prepare for drawing.
const char * vk2dStatusMessage()
Returns the current renderer status message, generally use this if vk2dGetStatus() returns something ...
void vk2dRendererWait()
Waits until current GPU tasks are done before moving on.
void vk2dAssetsSetModelMemory(VK2DAssetLoad *array, int index, void *buffer, int size, VK2DTexture *texture, VK2DModel *outVar)
Sets up a VK2DAssetLoad array entry for a ModelMemory entry.
void vk2dRendererSetCamera(VK2DCameraSpec camera)
Sets the current camera settings.
void vk2dRendererDrawRectangleOutline(float x, float y, float w, float h, float r, float ox, float oy, float lineWidth)
Draws a rectangle using the current rendering colour.
VK2DRendererLimits vk2dRendererGetLimits()
Returns the limits of the renderer on the current host.
void vk2dAssetsFree(VK2DAssetLoad *assets, uint32_t count)
Uses the same asset list you passed to vk2dAssetsLoad to free all the assets in one call.
VK2DStatus vk2dStatus()
Gets the renderer's current status code.
void vk2dRendererDrawShadows(VK2DShadowEnvironment shadowEnvironment, vec4 colour, vec2 lightSource)
Draws a shadow environment.
VK2DRendererConfig vk2dRendererGetConfig()
Gets the current user configuration of the renderer.
void vk2dColourRGBA(vec4 dst, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Converts a RGBA colour into a vec4 normalized colour.
const char * vk2dHostInformation()
Returns a string detailing information about the host machine.
void vk2dRendererDrawCircle(float x, float y, float r)
Draws a circle using the current rendering colour.
Forward declares struct typedefs.
float vec2[2]
2D vector of floats
Definition: Structs.h:170
int32_t VK2DCameraIndex
Type used for referencing cameras.
Definition: Structs.h:185
VK2DBlendMode
Blend modes that can be used to render if VK2D_GENERATE_BLEND_MODES is enabled.
Definition: Structs.h:38
float vec3[3]
3D vector of floats
Definition: Structs.h:173
VK2DResult
Return codes through the renderer.
Definition: Structs.h:115
VK2DStatus
Status codes for logging/error reporting.
Definition: Structs.h:122
float vec4[4]
4D vector of floats
Definition: Structs.h:176
Information needed to queue an asset loading off-thread.
Definition: Structs.h:349
Camera information.
Definition: Structs.h:272
Represents a user's draw command which will later be processed into an instance.
Definition: Structs.h:320
User configurable settings.
Definition: Structs.h:265
Renderer limitations for the host.
Definition: Structs.h:298
Startup options that dictate some basic VK2D stuff.
Definition: Structs.h:248
Vertex data for rendering shapes.
Definition: Structs.h:188