A bit more code cleanup
This commit is contained in:
@ -1,20 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "animation.h"
|
||||
|
||||
float animForwardAndBackward(float t) {
|
||||
return (t < 0.5 ? t : 1 - t);
|
||||
}
|
||||
|
||||
float animForwardAndBackwardScaled(float t) {
|
||||
return animForwardAndBackward(t) * 2.0f;
|
||||
}
|
||||
|
||||
float animTimeScaleFromFrameTime(int32_t frames, float time) {
|
||||
return 1.0f / (float)frames / time;
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
|
||||
/**
|
||||
* Animation tool for converting 0-1 space into a 0-0.5 back to zero space. This
|
||||
* is intended to make a "Forward then backwards" effect for animation. This
|
||||
* method will not scale t.
|
||||
* @param t Time in space to back and fourth on between 0 and 1.
|
||||
* @returns Forward and backwards time. 0 to 0.5 are as such, 0.5 to 1 are from
|
||||
* 0.5 to 0.
|
||||
*/
|
||||
float animForwardAndBackward(float t);
|
||||
|
||||
/**
|
||||
* Animation tool for converting 0-1 space into a 0-1-0 space. Scaled version of
|
||||
* animForwardAndBackward().
|
||||
* @param t Time in space to back and fourth on between 0 and 1.
|
||||
* @returns Forward and backwards time.
|
||||
*/
|
||||
float animForwardAndBackwardScaled(float t);
|
||||
|
||||
/**
|
||||
* Returns the time scale (speed to multiply time range by) for a given frame
|
||||
* time and frame count. E.g. 3 frames at 0.5 time each would have a time scale
|
||||
* of 1.5.
|
||||
*
|
||||
* @param frames Frames to get the scale of.
|
||||
* @param time Time to get the scale of.
|
||||
* @return The time scale.
|
||||
*/
|
||||
float animTimeScaleFromFrameTime(int32_t frames, float time);
|
@ -1,60 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "easing.h"
|
||||
|
||||
float easeLinear(float t) {
|
||||
return t;
|
||||
}
|
||||
|
||||
float easeInQuad(float t) {
|
||||
return t * t;
|
||||
}
|
||||
|
||||
float easeOutQuad(float t) {
|
||||
return t * (2 - t);
|
||||
}
|
||||
|
||||
float easeInOutQuad(float t) {
|
||||
return t < .5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
|
||||
}
|
||||
|
||||
float easeInCubic(float t) {
|
||||
return t * t * t;
|
||||
}
|
||||
|
||||
float easeOutCubic(float t) {
|
||||
return (t - 1) * (t - 1) * (t - 1) + 1;
|
||||
}
|
||||
|
||||
float easeInOutCubic(float t) {
|
||||
return t < .5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
|
||||
}
|
||||
|
||||
float easeInQuart(float t) {
|
||||
return t * t * t * t;
|
||||
}
|
||||
|
||||
float easeOutQuart(float t) {
|
||||
return 1 - (t-1)*(t-1)*(t-1)*(t-1);
|
||||
}
|
||||
|
||||
float easeInOutQuart(float t) {
|
||||
return t < .5 ? 8*t*t*t*t : 1-8*(t-1)*(t-1)*(t-1)*(t-1);
|
||||
}
|
||||
|
||||
float easeInQuint(float t) {
|
||||
return t*t*t*t*t;
|
||||
}
|
||||
|
||||
float easeOutQuint(float t) {
|
||||
return 1 + (t-1)*(t-1)*(t-1)*(t-1)*(t-1);
|
||||
}
|
||||
|
||||
float easeInOutQuint(float t) {
|
||||
return t<.5 ? 16*t*t*t*t*t : 1+16*(t-1)*(t-1)*(t-1)*(t-1)*(t-1);
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
typedef float easefunction_t(float t);
|
||||
|
||||
/**
|
||||
* Returns the ease time for a given real time duration span.
|
||||
* @param start At what point in time the animation started
|
||||
* @param current The current point in time the animation is at.
|
||||
* @param duration The total duration on the animation.
|
||||
* @returns The easing time (0-1 time) that the animation is at.
|
||||
*/
|
||||
#define easeTimeToEase(start, current, duration) ((current-start)/duration)
|
||||
|
||||
float easeLinear(float t);
|
||||
float easeInQuad(float t);
|
||||
float easeOutQuad(float t);
|
||||
float easeInOutQuad(float t);
|
||||
float easeInCubic(float t);
|
||||
float easeOutCubic(float t);
|
||||
float easeInOutCubic(float t);
|
||||
float easeInQuart(float t);
|
||||
float easeOutQuart(float t);
|
||||
float easeInOutQuart(float t);
|
||||
float easeInQuint(float t);
|
||||
float easeOutQuint(float t);
|
||||
float easeInOutQuint(float t);
|
@ -1,97 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
void queueInit(queue_t *queue) {
|
||||
queue->timeline = 0;
|
||||
queue->count = 0;
|
||||
queue->current = ANIMATION_QUEUE_START;
|
||||
}
|
||||
|
||||
queueaction_t * queueNext(queue_t *queue) {
|
||||
queueaction_t *action;
|
||||
|
||||
// Is there a currently running action? If so, end it.
|
||||
if(queue->current != ANIMATION_QUEUE_START) {
|
||||
action = queue->items + queue->current;
|
||||
if(action->onEnd != NULL) action->onEnd(queue, action, queue->current);
|
||||
}
|
||||
|
||||
// Prepare to go to next action if there is a next action.
|
||||
queue->current++;
|
||||
queue->actionStarted = queue->timeline;
|
||||
if(queue->current >= queue->count) return NULL;
|
||||
|
||||
// Go to next action, start it.
|
||||
action = queue->items + queue->current;
|
||||
if(action->onStart != NULL) action->onStart(queue, action, queue->current);
|
||||
return action;
|
||||
}
|
||||
|
||||
queueaction_t * queueAdd(queue_t *queue) {
|
||||
queueaction_t *action;
|
||||
action = queue->items + queue->count;
|
||||
|
||||
action->index = queue->count;
|
||||
action->data = NULL;
|
||||
action->onStart = NULL;
|
||||
action->onUpdate = NULL;
|
||||
action->onEnd = NULL;
|
||||
|
||||
queue->count++;
|
||||
return action;
|
||||
}
|
||||
|
||||
void queueUpdate(queue_t *queue, float delta) {
|
||||
queueaction_t *action;
|
||||
queue->timeline += delta;
|
||||
if(queue->current >= queue->count) return;
|
||||
|
||||
action = queue->items + queue->current;
|
||||
if(action->onUpdate != NULL) {
|
||||
action->onUpdate(queue, action, queue->current);
|
||||
}
|
||||
}
|
||||
|
||||
void queueDispose(queue_t *queue) {
|
||||
queueaction_t *action;
|
||||
if(queue->current >= queue->count) return;
|
||||
action = queue->items + queue->current;
|
||||
if(action->onEnd != NULL) action->onEnd(queue, action, queue->current);
|
||||
}
|
||||
|
||||
void queueRestack(queue_t *queue) {
|
||||
uint8_t i;
|
||||
|
||||
// Rewind the array.
|
||||
arrayRewind(sizeof(queueaction_t), queue->items, ANIMATION_QUEUE_START,
|
||||
queue->current, queue->count
|
||||
);
|
||||
|
||||
// Now rewind the stack
|
||||
queue->count -= queue->current;
|
||||
queue->current = 0;
|
||||
|
||||
// Now fix indexes
|
||||
for(i = 0; i < queue->count; i++) {
|
||||
queue->items[i].index = i;
|
||||
}
|
||||
}
|
||||
|
||||
void _queueDelayUpdate(queue_t *queue, queueaction_t *action, uint8_t i) {
|
||||
float n = queue->timeline - queue->actionStarted;
|
||||
if(n < queue->delays[i]) return;
|
||||
queueNext(queue);
|
||||
}
|
||||
|
||||
queueaction_t * queueDelay(queue_t *queue, float delay) {
|
||||
queueaction_t *action = queueAdd(queue);
|
||||
queue->delays[action->index] = delay;
|
||||
action->onUpdate = &_queueDelayUpdate;
|
||||
return action;
|
||||
}
|
@ -1,113 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "../../util/array.h"
|
||||
|
||||
#define ANIMATION_QUEUE_ITEM_MAX 128
|
||||
#define ANIMATION_QUEUE_START 0xFF
|
||||
|
||||
typedef struct _queueaction_t queueaction_t;
|
||||
typedef struct _queue_t queue_t;
|
||||
|
||||
/**
|
||||
* Callback for queue events.
|
||||
* @param conversation Conversation this text is attached to.
|
||||
* @param text Text item that is being used in the callback
|
||||
* @param i Index of the item in the queue.
|
||||
*/
|
||||
typedef void queuecallback_t(queue_t *queue, queueaction_t *action, uint8_t i);
|
||||
|
||||
typedef struct _queueaction_t {
|
||||
/** Index that the action is within the queue */
|
||||
uint8_t index;
|
||||
|
||||
/** Pointer to any custom user data */
|
||||
void *data;
|
||||
|
||||
/** Callback to fire the moment the action is active in the queue */
|
||||
queuecallback_t *onStart;
|
||||
|
||||
/** Callback to fire when this action has ended */
|
||||
queuecallback_t *onEnd;
|
||||
|
||||
/** Callback to fire every update of this queue while action is active. */
|
||||
queuecallback_t *onUpdate;
|
||||
} queueaction_t;
|
||||
|
||||
typedef struct _queue_t {
|
||||
/** Array of items within the queue. */
|
||||
queueaction_t items[ANIMATION_QUEUE_ITEM_MAX];
|
||||
uint8_t count;
|
||||
|
||||
/** Current index within the array of actions that is currently processing */
|
||||
uint8_t current;
|
||||
|
||||
/** Internal timeline tracking */
|
||||
float timeline;
|
||||
|
||||
/** Time that the current aciton started */
|
||||
float actionStarted;
|
||||
|
||||
/** Delay Queue Item Storage */
|
||||
float delays[ANIMATION_QUEUE_ITEM_MAX];
|
||||
} queue_t;
|
||||
|
||||
/**
|
||||
* Initialize the queue set.
|
||||
* @param queue Queue to initialize.
|
||||
*/
|
||||
void queueInit(queue_t *queue);
|
||||
|
||||
/**
|
||||
* Goes to the next action, or start the queue if this is the first action.
|
||||
* @param queue Queue to skip to the next action.
|
||||
* @returns Action that was just started.
|
||||
*/
|
||||
queueaction_t * queueNext(queue_t *queue);
|
||||
|
||||
/**
|
||||
* Add a queue action to the queue.
|
||||
* @param convo Queue to add to.
|
||||
* @return Pointer to the queue action that was added.
|
||||
*/
|
||||
queueaction_t * queueAdd(queue_t *queue);
|
||||
|
||||
/**
|
||||
* Updates the queue logic.
|
||||
* @param convo Queue to update.
|
||||
* @param delta Time delta to tick the queue by.
|
||||
*/
|
||||
void queueUpdate(queue_t *queue, float delta);
|
||||
|
||||
/**
|
||||
* Dispose the queue when finished.
|
||||
* @param queue Queue to dispose.
|
||||
*/
|
||||
void queueDispose(queue_t *queue);
|
||||
|
||||
/**
|
||||
* Restacks the queue. The restack process essentially rewinds the queue so that
|
||||
* the current items move back to position 0, and allows you to add more items
|
||||
* to the queue again. Because the memory does shift, and the indexes do change
|
||||
* a lot of your pointers will break, make sure you re-reference all your
|
||||
* pointers.
|
||||
*
|
||||
* @param queue Queue to restack.
|
||||
*/
|
||||
void queueRestack(queue_t *queue);
|
||||
|
||||
/** Callbacks for Queue Delay Action */
|
||||
void _queueDelayUpdate(queue_t *queue, queueaction_t *action, uint8_t i);
|
||||
/**
|
||||
* Adds a delay action to a queue.
|
||||
* @param queue Queue to add to.
|
||||
* @param delay Delay time (in seconds) to have.
|
||||
* @return Pointer to the action added to the queue.
|
||||
*/
|
||||
queueaction_t * queueDelay(queue_t *queue, float delay);
|
@ -1,141 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "timeline.h"
|
||||
|
||||
void _timelineActionDeltaUpdateStart(
|
||||
timeline_t *timeline, timelineaction_t *action, uint8_t i
|
||||
) {
|
||||
if(action->data == NULL) return;
|
||||
*((float *)action->data) = timeline->initials[i];
|
||||
}
|
||||
|
||||
void _timelineActionDeltaUpdateDuration(
|
||||
timeline_t *timeline, timelineaction_t *action, uint8_t i
|
||||
) {
|
||||
float n;
|
||||
if(action->data == NULL) return;
|
||||
n = timeline->easings[i](
|
||||
timelineActionGetTimeRaw(timeline, action)
|
||||
);
|
||||
*((float *)action->data) = timeline->initials[i] + (timeline->deltas[i] * n);
|
||||
}
|
||||
|
||||
void _timelineActionDeltaUpdateEnd(
|
||||
timeline_t *timeline, timelineaction_t *action, uint8_t i
|
||||
) {
|
||||
if(action->data == NULL) return;
|
||||
*((float *)action->data) = timeline->initials[i] + timeline->deltas[i];
|
||||
}
|
||||
|
||||
|
||||
void timelineInit(timeline_t *timeline) {
|
||||
timeline->current = 0;
|
||||
timeline->diff = 0;
|
||||
timeline->previous = 0;
|
||||
timeline->user = 0;
|
||||
timeline->actionCount = 0;
|
||||
}
|
||||
|
||||
void timelineUpdate(timeline_t *timeline, float delta) {
|
||||
uint8_t i;
|
||||
timelineaction_t *action;
|
||||
float full;
|
||||
|
||||
timeline->diff = delta;
|
||||
timeline->previous = timeline->current;
|
||||
timeline->current = timeline->current + delta;
|
||||
|
||||
// Find all actions that would have started or ended in this timespan.
|
||||
for(i = 0; i < timeline->actionCount; i++) {
|
||||
action = timeline->actions +i;
|
||||
|
||||
// Has the action started yet?
|
||||
if(action->start > timeline->current) continue;
|
||||
|
||||
// Did we start this frame?
|
||||
if(action->start > timeline->previous && action->onStart != NULL) {
|
||||
action->onStart(timeline, action, i);
|
||||
}
|
||||
|
||||
// Durations of 0 only fire starts, never ends or durations.
|
||||
if(action->duration == 0) continue;
|
||||
|
||||
// Is the end still in the future? Durations in negatives go forever
|
||||
full = action->start+action->duration;
|
||||
if(action->duration < 0 || full > timeline->current) {
|
||||
if(action->onDuration != NULL) action->onDuration(timeline, action, i);
|
||||
} else if(full > timeline->previous) {// Did we end this frame?
|
||||
if(action->onEnd != NULL) action->onEnd(timeline, action, i);
|
||||
if(action->loop) action->start = timeline->current;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool timelineIsFinished(timeline_t *timeline) {
|
||||
uint8_t i;
|
||||
timelineaction_t *action;
|
||||
|
||||
for(i = 0; i < timeline->actionCount; i++) {
|
||||
action = timeline->actions +i;
|
||||
if(action->start > timeline->current) return false;
|
||||
if(action->duration < 0) return false;
|
||||
if(action->duration == 0) continue;
|
||||
if(action->start+action->duration > timeline->current) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
timelineaction_t * timelineAddAction(timeline_t *timeline, float start,
|
||||
float duration
|
||||
) {
|
||||
timelineaction_t *action = timeline->actions + (timeline->actionCount++);
|
||||
action->loop = false;
|
||||
action->start = start, action->duration = duration;
|
||||
action->onStart = action->onEnd = action->onDuration = NULL;
|
||||
return action;
|
||||
}
|
||||
|
||||
timelineaction_t * timelineAddDeltaAction(timeline_t *timeline,
|
||||
float start, float duration, float initial, float delta,
|
||||
easefunction_t *easing, float *destination
|
||||
) {
|
||||
timelineaction_t *action;
|
||||
timeline->initials[timeline->actionCount] = initial;
|
||||
timeline->deltas[timeline->actionCount] = delta;
|
||||
timeline->easings[timeline->actionCount] = (
|
||||
easing == NULL ? &easeLinear : easing
|
||||
);
|
||||
action = timelineAddAction(timeline, start, duration);
|
||||
action->data = destination;
|
||||
action->onStart = &_timelineActionDeltaUpdateStart;
|
||||
action->onDuration = &_timelineActionDeltaUpdateDuration;
|
||||
action->onEnd = &_timelineActionDeltaUpdateEnd;
|
||||
return action;
|
||||
}
|
||||
|
||||
timelineaction_t * timelineAddDeltaActionTo(timeline_t *timeline,
|
||||
float start, float duration, float initial, float end,
|
||||
easefunction_t *easing, float *destination
|
||||
) {
|
||||
return timelineAddDeltaAction(
|
||||
timeline, start, duration, initial, end-initial, easing, destination
|
||||
);
|
||||
}
|
||||
|
||||
float timelineActionGetTimeRaw(timeline_t *timeline, timelineaction_t *action) {
|
||||
return (timeline->current - action->start) / action->duration;
|
||||
}
|
||||
|
||||
float timelineActionGetTime(timeline_t *tl, timelineaction_t *at) {
|
||||
return mathClamp(timelineActionGetTimeRaw(tl, at), 0, 1);
|
||||
}
|
||||
|
||||
void timelineClear(timeline_t *timeline) {
|
||||
timeline->actionCount = 0;
|
||||
}
|
@ -1,187 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "../../util/math.h"
|
||||
#include "easing.h"
|
||||
|
||||
/** Maximum number of actions a timeline can support, smaller than 0xFF */
|
||||
#define TIMELINE_ACTION_COUNT_MAX 128
|
||||
|
||||
/** Type forwarders */
|
||||
typedef struct _timeline_t timeline_t;
|
||||
typedef struct _timelineaction_t timelineaction_t;
|
||||
|
||||
/**
|
||||
* Callback for when a timeline event occurs
|
||||
* @param timeline The timeline that fired this callback.
|
||||
* @param action The action that this callback is attached to.
|
||||
* @param i The index that this action is within the timeline.
|
||||
*/
|
||||
typedef void timelinecallback_t(timeline_t *timeline, timelineaction_t *action,
|
||||
uint8_t i
|
||||
);
|
||||
|
||||
typedef struct _timelineaction_t {
|
||||
/** Pointer to any custom user data the timeline action wants to use. */
|
||||
void *data;
|
||||
|
||||
/**
|
||||
* The time that this action should occur within the timeline
|
||||
* set to 0 or less to start immediately.
|
||||
*/
|
||||
float start;
|
||||
|
||||
/**
|
||||
* The duration of the action, this will decide for how long onDuration will
|
||||
* be called for, and when onEnd should be called.
|
||||
* Set to a negative number to have this continue forever.
|
||||
* Set to 0 to only fire the onStart.
|
||||
* onStart can fire with either onDuration and onEnd on the same frame, but
|
||||
* onEnd and onDuration cannot fire the same frame.
|
||||
*/
|
||||
float duration;
|
||||
|
||||
/**
|
||||
* Enables animation looping. This works by forcing start to be equal to the
|
||||
* current time at the point in time that onEnd is called. This will also stop
|
||||
* onStart being called so ensure that your onStart and onEnd logic works.
|
||||
*/
|
||||
bool loop;
|
||||
|
||||
timelinecallback_t *onStart;
|
||||
|
||||
timelinecallback_t *onDuration;
|
||||
|
||||
timelinecallback_t *onEnd;
|
||||
} timelineaction_t;
|
||||
|
||||
typedef struct _timeline_t {
|
||||
/** The current time as far as the timeline is concerned */
|
||||
float current;
|
||||
|
||||
/** The time of the last "frame" as far as the timeline is concerned */
|
||||
float previous;
|
||||
|
||||
/** The frame time diff, essentially current = previous + diff */
|
||||
float diff;
|
||||
|
||||
/** User pointer, allows you to point to some other data */
|
||||
void *user;
|
||||
|
||||
/** Actions within the timeline */
|
||||
timelineaction_t actions[TIMELINE_ACTION_COUNT_MAX];
|
||||
|
||||
/** For delta actions, storage of the initial values */
|
||||
float initials[TIMELINE_ACTION_COUNT_MAX];
|
||||
/** For delta actions, storage of their deltas. */
|
||||
float deltas[TIMELINE_ACTION_COUNT_MAX];
|
||||
/** Easing functions to use for delta functions */
|
||||
easefunction_t *easings[TIMELINE_ACTION_COUNT_MAX];
|
||||
|
||||
uint8_t actionCount;
|
||||
} timeline_t;
|
||||
|
||||
|
||||
/**
|
||||
* Initializes a timeline back to its default state.
|
||||
*
|
||||
* @param timeline Timeline to initialize.
|
||||
*/
|
||||
void timelineInit(timeline_t *timeline);
|
||||
|
||||
/**
|
||||
* Ticks the timeline. This can be done using any delta.
|
||||
*
|
||||
* @param timeline Timeline to tick
|
||||
* @param delta Delta to tick.
|
||||
*/
|
||||
void timelineUpdate(timeline_t *timeline, float delta);
|
||||
|
||||
/**
|
||||
* Returns true if every action in the timeline has finished.
|
||||
*
|
||||
* @param timeline Timeline to check
|
||||
* @return True if finished, otherwise false.
|
||||
*/
|
||||
bool timelineIsFinished(timeline_t *timeline);
|
||||
|
||||
/**
|
||||
* Adds an action to the timeline. This will initialize the action callbacks to
|
||||
* NULL, you will need to initialize your own callbacks.
|
||||
*
|
||||
* @param timeline Timeline to add to.
|
||||
* @param start Start time.
|
||||
* @param duration Duration time
|
||||
* @return Pointer to the timeline action or NULL if the list is full.
|
||||
*/
|
||||
timelineaction_t * timelineAddAction(timeline_t *timeline, float start,
|
||||
float duration
|
||||
);
|
||||
|
||||
/**
|
||||
* Add a special action kind that can treat delta style animations. These are
|
||||
* animations that have a start, and an end value, that will be tracked for you
|
||||
* and keep you up to date.
|
||||
*
|
||||
* @param timeline Timeline to add to.
|
||||
* @param start Starting time.
|
||||
* @param duration Animation duration.
|
||||
* @param initial Initial value for the animation.
|
||||
* @param delta Delta value for the animation.
|
||||
* @param easing Pointer to an easing function to use, set to NULL for linear.
|
||||
* @param destination A constant floating point to update.
|
||||
* @return The queued timeline action.
|
||||
*/
|
||||
timelineaction_t * timelineAddDeltaAction(timeline_t *timeline,
|
||||
float start, float duration, float initial, float delta,
|
||||
easefunction_t *easing, float *destination
|
||||
);
|
||||
|
||||
/**
|
||||
* Shorthand for timelineAddDeltaAction that will calculate the delta based on
|
||||
* and end position.
|
||||
*
|
||||
* @param timeline Timeline to add to.
|
||||
* @param start Starting time.
|
||||
* @param duration Animation duration.
|
||||
* @param initial Initial value for the animation.
|
||||
* @param end End value for the animation.
|
||||
* @param easing Pointer to an easing function to use, set to NULL for linear.
|
||||
* @param destination A constant floating point to update.
|
||||
* @return The queued timeline action.
|
||||
*/
|
||||
timelineaction_t * timelineAddDeltaActionTo(timeline_t *timeline,
|
||||
float start, float duration, float initial, float end,
|
||||
easefunction_t *easing, float *destination
|
||||
);
|
||||
|
||||
/**
|
||||
* Gets the timeline action's animation time. This is a representation of
|
||||
* 0 - 1 where 0 means the animation is at the start, and 1 meaning the
|
||||
* animation is at full completion. This is not clamped and may exceed 1.
|
||||
*
|
||||
* @param timeline Timeline the action belongs to.
|
||||
* @param action Action itself.
|
||||
* @return 0 - 1+, where 0 = start, 1 = duration, >1 is time since duration.
|
||||
*/
|
||||
float timelineActionGetTimeRaw(timeline_t *timeline, timelineaction_t *action);
|
||||
|
||||
/**
|
||||
* Gets the timeline action's animation time between 0 - 1, clamped.
|
||||
*
|
||||
* @param tl Timeline to get delta from.
|
||||
* @param at Timeline action.
|
||||
* @return 0 - 1, where 0 = start, 1 = duration.
|
||||
*/
|
||||
float timelineActionGetTime(timeline_t *tl, timelineaction_t *at);
|
||||
|
||||
/**
|
||||
* Removes all actions from the timeline.
|
||||
*
|
||||
* @param timeline Timeline to clear.
|
||||
*/
|
||||
void timelineClear(timeline_t *timeline);
|
@ -1,120 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "bitmapfont.h"
|
||||
|
||||
tilesetdiv_t * bitmapFontGetCharacterDivision(tileset_t *tileset,
|
||||
char character
|
||||
) {
|
||||
int32_t i = ((int32_t)character) - BITMAP_FONT_CHAR_START;
|
||||
return tileset->divisions + i;
|
||||
}
|
||||
|
||||
bitmapfontmeasure_t bitmapFontMeasure(char *string,
|
||||
float charWidth, float charHeight
|
||||
) {
|
||||
int32_t i;
|
||||
float x, y;
|
||||
char c;
|
||||
bitmapfontmeasure_t measure = {
|
||||
.height = 0, .lines = 1, .width = 0
|
||||
};
|
||||
|
||||
i = 0;
|
||||
y = 0;
|
||||
x = 0;
|
||||
|
||||
while(true) {
|
||||
c = string[i];
|
||||
if(c == '\0') break;
|
||||
i++;
|
||||
|
||||
if(c == '\n') {
|
||||
measure.width = mathMax(x, measure.width);
|
||||
x = 0;
|
||||
y += charHeight;
|
||||
measure.lines++;
|
||||
continue;
|
||||
} else if(c == ' ') {
|
||||
x += charWidth;
|
||||
continue;
|
||||
}
|
||||
|
||||
x += charWidth;
|
||||
}
|
||||
|
||||
measure.width = mathMax(x, measure.width);
|
||||
measure.height = y + charHeight;
|
||||
|
||||
return measure;
|
||||
}
|
||||
|
||||
bitmapfontmeasure_t bitmapFontSpriteBatchBuffer(
|
||||
spritebatch_t *batch, tileset_t *tileset,
|
||||
char *string, float x, float y, float z, float charWidth, float charHeight
|
||||
) {
|
||||
int32_t i;
|
||||
char c;
|
||||
tilesetdiv_t *div;
|
||||
float cx, cy;
|
||||
bitmapfontmeasure_t measure;
|
||||
|
||||
// Detect char dimensions
|
||||
if(charWidth == -1) charWidth = tileset->divX * (charHeight / tileset->divY);
|
||||
if(charHeight == -1) charHeight = tileset->divY * (charWidth / tileset->divX);
|
||||
|
||||
// Position the text.
|
||||
if(x == BITMAP_FONT_CENTER_X ||
|
||||
y == BITMAP_FONT_CENTER_Y ||
|
||||
x == BITMAP_FONT_RIGHT_X
|
||||
) {
|
||||
measure = bitmapFontMeasure(string, charWidth, charHeight);
|
||||
if(x == BITMAP_FONT_CENTER_X) {
|
||||
x = -(measure.width/2);
|
||||
} else if(x == BITMAP_FONT_RIGHT_X) {
|
||||
x = -measure.width;
|
||||
}
|
||||
if(y == BITMAP_FONT_CENTER_Y) y = -(measure.height/2);
|
||||
}
|
||||
|
||||
// Begin buffering the sprite batch
|
||||
measure.width = 0;
|
||||
measure.height = 0;
|
||||
measure.lines = 1;
|
||||
i = 0;
|
||||
cx = x, cy = y;
|
||||
|
||||
while(true) {
|
||||
c = string[i];
|
||||
if(c == '\0') break;
|
||||
i++;
|
||||
|
||||
// Special chars
|
||||
if(c == '\n') {
|
||||
measure.width = mathMax(cx-x, measure.width);
|
||||
cx = x;
|
||||
cy += charHeight;
|
||||
measure.lines++;
|
||||
continue;
|
||||
} else if(c == ' ') {
|
||||
cx += charWidth;
|
||||
continue;
|
||||
}
|
||||
|
||||
div = bitmapFontGetCharacterDivision(tileset, c);
|
||||
spriteBatchQuad(batch, -1,
|
||||
cx, cy, z, charWidth, charHeight,
|
||||
div->x0, div->y1, div->x1, div->y0
|
||||
);
|
||||
cx += charWidth;
|
||||
}
|
||||
|
||||
measure.width = mathMax(cx-x, measure.width);
|
||||
measure.height = cy-y + charHeight;
|
||||
|
||||
return measure;
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "spritebatch.h"
|
||||
#include "tileset.h"
|
||||
#include "../util/math.h"
|
||||
|
||||
/** Which is the first character within the font tilemap */
|
||||
#define BITMAP_FONT_CHAR_START 33
|
||||
|
||||
/** Center a font along the X axis */
|
||||
#define BITMAP_FONT_CENTER_X 9876543
|
||||
|
||||
/** Center a font along the Y axis */
|
||||
#define BITMAP_FONT_CENTER_Y -BITMAP_FONT_CENTER_X
|
||||
|
||||
/** Align right edge of font to origin */
|
||||
#define BITMAP_FONT_RIGHT_X (BITMAP_FONT_CENTER_X-1)
|
||||
|
||||
typedef struct {
|
||||
float width, height;
|
||||
int32_t lines;
|
||||
} bitmapfontmeasure_t;
|
||||
|
||||
/**
|
||||
* Get the division for a given character.
|
||||
*
|
||||
* @param tileset Tileset to get the division from.
|
||||
* @param character Character to get the division for.
|
||||
* @return The division from the tileset for the character.
|
||||
*/
|
||||
tilesetdiv_t * bitmapFontGetCharacterDivision(tileset_t *tileset,
|
||||
char character
|
||||
);
|
||||
|
||||
/**
|
||||
* Measures a string's fully rendered size.
|
||||
*
|
||||
* @param string The string to measure
|
||||
* @param charWidth The width of each character.
|
||||
* @param charHeight The height of each character.
|
||||
* @return The measured string.
|
||||
*/
|
||||
bitmapfontmeasure_t bitmapFontMeasure(char *string,
|
||||
float charWidth, float charHeight
|
||||
);
|
||||
|
||||
/**
|
||||
* Renders a set of font characters to the sprite. Coordinates are anchored to
|
||||
* the top left (0,0) origin.
|
||||
*
|
||||
* @param batch Sprite Batch to render to.
|
||||
* @param tileset Tileset for the font.
|
||||
* @param string String to render.
|
||||
* @param x Position in X space.
|
||||
* @param y Position in Y space.
|
||||
* @param z Position in Z space.
|
||||
* @param charWidth Width of each character. Set to -1 to use the height ratio.
|
||||
* @param charHeight Height of each character. Set to -1 to be the width ratio.
|
||||
* @returns The string measurement.
|
||||
*/
|
||||
bitmapfontmeasure_t bitmapFontSpriteBatchBuffer(
|
||||
spritebatch_t *batch, tileset_t *tileset,
|
||||
char *string, float x, float y, float z, float charWidth, float charHeight
|
||||
);
|
@ -1,55 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Msters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "camera.h"
|
||||
|
||||
void cameraLookAt(camera_t *camera,
|
||||
float x, float y, float z,
|
||||
float targetX, float targetY, float targetZ
|
||||
) {
|
||||
matrixIdentity(&camera->view);
|
||||
matrixLookAt(&camera->view, x, y, z, targetX, targetY, targetZ, 0, 1, 0);
|
||||
}
|
||||
|
||||
void cameraLookAtStruct(camera_t *camera, cameralookat_t look) {
|
||||
cameraLookAt(camera,
|
||||
look.x, look.y, look.z, look.lookX, look.lookY, look.lookZ
|
||||
);
|
||||
}
|
||||
|
||||
void cameraLook(camera_t *camera,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll
|
||||
) {
|
||||
matrixIdentity(&camera->view);
|
||||
matrixLook(&camera->view, x, y, z, pitch, yaw, roll, 0, 1, 0);
|
||||
}
|
||||
|
||||
void cameraPerspective(camera_t *camera,
|
||||
float fov, float aspect, float camNear, float camFar
|
||||
) {
|
||||
matrixIdentity(&camera->projection);
|
||||
matrixPerspective(&camera->projection,mathDeg2Rad(fov),aspect,camNear,camFar);
|
||||
}
|
||||
|
||||
void cameraOrtho(camera_t *camera,
|
||||
float left, float right, float bottom, float top, float camNear, float camFar
|
||||
) {
|
||||
matrixIdentity(&camera->projection);
|
||||
matrixOrtho(&camera->projection, left, right, bottom, top, camNear, camFar);
|
||||
}
|
||||
|
||||
void cameraOrbit(camera_t *camera,
|
||||
float distance, float yaw, float pitch,
|
||||
float targetX, float targetY, float targetZ
|
||||
) {
|
||||
float cy = cosf(pitch);
|
||||
float x = distance * sinf(yaw) * cy;
|
||||
float y = distance * sinf(pitch);
|
||||
float z = distance * cosf(yaw) * cy;
|
||||
cameraLookAt(camera, x, y, z, targetX, targetY, targetZ);
|
||||
}
|
@ -1,106 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "../util/math.h"
|
||||
#include "matrix.h"
|
||||
|
||||
/** The math for the camera is stored here. */
|
||||
typedef struct {
|
||||
/** View Matrix (Where the camera looks) */
|
||||
matrix_t view;
|
||||
|
||||
/** Projection Matrix (How the camera looks) */
|
||||
matrix_t projection;
|
||||
} camera_t;
|
||||
|
||||
typedef struct {
|
||||
float x, y, z, lookX, lookY, lookZ;
|
||||
} cameralookat_t;
|
||||
|
||||
/**
|
||||
* Make a camera look at a position in world space while itself being positioned
|
||||
* within world space.
|
||||
*
|
||||
* @param camera The camera to position.
|
||||
* @param x The X position in world space of the camera.
|
||||
* @param y The Y position in world space of the camera.
|
||||
* @param z The Z position in world space of the camera.
|
||||
* @param targetX The Target X position in world space of the camera.
|
||||
* @param targetY The Target Y position in world space of the camera.
|
||||
* @param targetZ The Target Z position in world space of the camera.
|
||||
*/
|
||||
void cameraLookAt(camera_t *camera,
|
||||
float x, float y, float z,
|
||||
float targetX, float targetY, float targetZ
|
||||
);
|
||||
|
||||
/**
|
||||
* Shorthand for look at that uses the look struct.
|
||||
*
|
||||
* @param camera Camera to position.
|
||||
* @param look Look vectors.
|
||||
*/
|
||||
void cameraLookAtStruct(camera_t *camera, cameralookat_t look);
|
||||
|
||||
/**
|
||||
* Make a camera look in a direction based on a rotation direction.
|
||||
*
|
||||
* @param camera The camera to position.
|
||||
* @param x The X position in world space of the camera.
|
||||
* @param y The Y position in world space of the camera.
|
||||
* @param z The Z position in world space of the camera.
|
||||
* @param pitch The pitch of the camera.
|
||||
* @param yaw The yaw of the camera.
|
||||
* @param roll The roll of the camera.
|
||||
*/
|
||||
void cameraLook(camera_t *camera,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll
|
||||
);
|
||||
|
||||
/**
|
||||
* Make a camera's projection be a 3D Perspective view.
|
||||
*
|
||||
* @param camera The camera to project.
|
||||
* @param fov The field of view of the camera (in degrees).
|
||||
* @param aspect The aspect ratio of the camera (w / h)
|
||||
* @param camNear The near plane clip.
|
||||
* @param camFar the far plane clip.
|
||||
*/
|
||||
void cameraPerspective(camera_t *camera,
|
||||
float fov, float aspect, float camNear, float camFar
|
||||
);
|
||||
|
||||
/**
|
||||
* Defines an orthorgonal camera matrix.
|
||||
*
|
||||
* @param camera Camera to position.
|
||||
* @param left The left side of the viewport.
|
||||
* @param right The right side of the viewport.
|
||||
* @param bottom The bottom side of the viewport.
|
||||
* @param camNear The near plane clip.
|
||||
* @param camFar the far plane clip.
|
||||
*/
|
||||
void cameraOrtho(camera_t *camera,
|
||||
float left, float right, float bottom, float top, float camNear, float camFar
|
||||
);
|
||||
|
||||
/**
|
||||
* Update a camera to orbit around a point.
|
||||
*
|
||||
* @param camera Camera to update
|
||||
* @param distance Distance from the point
|
||||
* @param yaw Yaw (Y axis) rotation.
|
||||
* @param pitch Pitch (X/Z axis) rotation.
|
||||
* @param targetX X point to orbit around.
|
||||
* @param targetY Y point to orbit around.
|
||||
* @param targetZ Z point to orbit around.
|
||||
*/
|
||||
void cameraOrbit(camera_t *camera,
|
||||
float distance, float yaw, float pitch,
|
||||
float targetX, float targetY, float targetZ
|
||||
);
|
@ -1,209 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "font.h"
|
||||
|
||||
// Due to some compiler bullshit, this is here.
|
||||
#ifndef STB_TRUETYPE_IMPLEMENTATION
|
||||
#define STB_TRUETYPE_IMPLEMENTATION
|
||||
#include <stb_truetype.h>
|
||||
#endif
|
||||
|
||||
void fontInit(font_t *font, char *data) {
|
||||
int32_t i, s;
|
||||
s = FONT_TEXTURE_WIDTH * FONT_TEXTURE_HEIGHT;
|
||||
|
||||
uint8_t *bitmapData = malloc(sizeof(uint8_t) * s);
|
||||
pixel_t *pixels = malloc(sizeof(pixel_t) * s);
|
||||
|
||||
// STBTT Loads fonts as single channel values only.
|
||||
stbtt_BakeFontBitmap(
|
||||
data, 0, FONT_TEXTURE_SIZE, bitmapData,
|
||||
FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
|
||||
FONT_FIRST_CHAR, FONT_NUM_CHARS,
|
||||
font->characterData
|
||||
);
|
||||
|
||||
for(i = 0; i < FONT_TEXTURE_WIDTH * FONT_TEXTURE_HEIGHT; i++) {
|
||||
pixels[i].r = 0xFF;
|
||||
pixels[i].g = 0xFF;
|
||||
pixels[i].b = 0xFF;
|
||||
pixels[i].a = bitmapData[i];
|
||||
}
|
||||
textureInit(&font->texture, FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT, pixels);
|
||||
|
||||
free(bitmapData);
|
||||
free(pixels);
|
||||
}
|
||||
|
||||
void fontDispose(font_t *font) {
|
||||
textureDispose(&font->texture);
|
||||
}
|
||||
|
||||
float fontGetScale(float fontSize) {
|
||||
return fontSize / FONT_SIZE_DEFAULT * FONT_GLOBAL_SCALE;
|
||||
}
|
||||
|
||||
bool _fontTextBufferAddLine(fonttextinfo_t *info, int32_t start, int32_t len) {
|
||||
info->lineCount++;
|
||||
|
||||
if(info->lineCount >= FONT_TEXT_INFO_LINES_MAX) {
|
||||
return false;
|
||||
}
|
||||
|
||||
info->lines[info->lineCount].start = start;
|
||||
info->lines[info->lineCount].length = len;
|
||||
return true;
|
||||
}
|
||||
|
||||
void fontTextBuffer(
|
||||
font_t *font, primitive_t *primitive, fonttextinfo_t *info, char *text,
|
||||
float maxWidth, float fontSize
|
||||
) {
|
||||
int32_t i, j, wordStart;
|
||||
char c;
|
||||
float x, y, wordX, scale;
|
||||
stbtt_aligned_quad *quads;
|
||||
stbtt_aligned_quad *quad;
|
||||
|
||||
// Make some space
|
||||
quads = malloc(sizeof(stbtt_aligned_quad) * strlen(text));
|
||||
|
||||
// Get the font scale
|
||||
scale = fontGetScale(fontSize);
|
||||
|
||||
// Adjust the max width to match the scale, and allow "no max width".
|
||||
maxWidth = maxWidth == -1 ? 9999999 : maxWidth * (1 / scale);
|
||||
|
||||
/** Which index in the original text var is the current word from */
|
||||
wordStart = 0;
|
||||
|
||||
// Setup Scales
|
||||
info->length = 0;
|
||||
info->realLength = 0;
|
||||
info->lineCount = 0;
|
||||
|
||||
// Prepare the line counters
|
||||
info->lines[0].start = 0;
|
||||
info->lines[0].length = 0;
|
||||
|
||||
// Reset Dimensions
|
||||
info->width = 0, info->height = 0;
|
||||
|
||||
// Setup the initial loop state, and X/Y coords for the quad.
|
||||
i = 0;
|
||||
x = 0;
|
||||
y = FONT_INITIAL_LINE;
|
||||
wordX = 0;
|
||||
while(c = text[i++]) {
|
||||
if(info->lineCount >= FONT_TEXT_INFO_LINES_MAX) break;
|
||||
info->length++;
|
||||
|
||||
// When space, start of new word about to begin
|
||||
if(c == FONT_SPACE) {
|
||||
x += FONT_SPACE_SIZE * scale;
|
||||
|
||||
// Did this space cause a newline?
|
||||
if(x > maxWidth) {
|
||||
_fontTextBufferAddLine(info, info->realLength, 0);
|
||||
y += FONT_LINE_HEIGHT;
|
||||
x = 0;
|
||||
}
|
||||
wordX = x;
|
||||
wordStart = info->realLength;
|
||||
continue;
|
||||
}
|
||||
|
||||
// New line.
|
||||
if(c == FONT_NEWLINE) {
|
||||
_fontTextBufferAddLine(info, info->realLength, 0);
|
||||
wordStart = info->realLength;
|
||||
y += FONT_LINE_HEIGHT;
|
||||
x = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Generate the quad.
|
||||
quad = quads + info->realLength;
|
||||
stbtt_GetBakedQuad(font->characterData,
|
||||
FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
|
||||
((int32_t)c)-FONT_FIRST_CHAR, &x, &y, quad, FONT_FILL_MODE
|
||||
);
|
||||
|
||||
// Now measure the width of the line (take the right side of that quad)
|
||||
if(quad->x1 > maxWidth) {
|
||||
// We've exceeded the edge, go back to the start of the word and newline.
|
||||
x = quad->x1 - wordX;
|
||||
for(j = wordStart; j <= info->realLength; j++) {
|
||||
quad = quads + j;
|
||||
quad->x0 -= wordX;
|
||||
quad->x1 -= wordX;
|
||||
quad->y0 += FONT_LINE_HEIGHT;
|
||||
quad->y1 += FONT_LINE_HEIGHT;
|
||||
}
|
||||
|
||||
// Go back to the previous (still current) line and remove the chars
|
||||
info->lines[info->lineCount].length -= info->realLength - wordStart;
|
||||
|
||||
// Next line begins with this word
|
||||
y += FONT_LINE_HEIGHT;
|
||||
if(!_fontTextBufferAddLine(info, wordStart, info->realLength-wordStart)) {
|
||||
continue;
|
||||
}
|
||||
wordX = 0;
|
||||
}
|
||||
|
||||
info->lines[info->lineCount].length++;
|
||||
info->realLength++;
|
||||
}
|
||||
|
||||
info->lineCount++;
|
||||
|
||||
// Initialize primitive
|
||||
primitiveInit(primitive,
|
||||
QUAD_VERTICE_COUNT * info->realLength,
|
||||
QUAD_INDICE_COUNT * info->realLength
|
||||
);
|
||||
|
||||
for(j = 0; j < info->realLength; j++) {
|
||||
quad = quads + j;
|
||||
|
||||
// Scale the Quad
|
||||
if(scale != 1.0) {
|
||||
quad->x0 *= scale;
|
||||
quad->x1 *= scale;
|
||||
quad->y0 *= scale;
|
||||
quad->y1 *= scale;
|
||||
}
|
||||
|
||||
// Update the dimensions.
|
||||
info->width = mathMax(info->width, quad->x1);
|
||||
info->height = mathMax(info->height, quad->y1);
|
||||
|
||||
// Buffer the quad.
|
||||
quadBuffer(primitive, 0,
|
||||
quad->x0, quad->y0, quad->s0, quad->t0,
|
||||
quad->x1, quad->y1, quad->s1, quad->t1,
|
||||
j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT
|
||||
);
|
||||
}
|
||||
|
||||
// Free up
|
||||
free(quads);
|
||||
}
|
||||
|
||||
int32_t fontGetLineCharCount(fonttextinfo_t *info,int32_t start,int32_t count) {
|
||||
int32_t charCount, i, m;
|
||||
m = mathMin(start+count, info->lineCount);
|
||||
|
||||
charCount = 0;
|
||||
for(i = start; i < m; i++) {
|
||||
charCount += info->lines[i].length;
|
||||
}
|
||||
|
||||
return charCount;
|
||||
}
|
@ -1,138 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "texture.h"
|
||||
#include "primitive/primitive.h"
|
||||
#include "primitive/quad.h"
|
||||
#include "../util/mem.h"
|
||||
#include "../util/math.h"
|
||||
|
||||
/** Which character (ASCII) to start the font from */
|
||||
#define FONT_FIRST_CHAR 32
|
||||
|
||||
/** How many characters (after the first char) to generate */
|
||||
#define FONT_NUM_CHARS 96
|
||||
|
||||
/** Width of the loaded font texture */
|
||||
#define FONT_TEXTURE_WIDTH 1024
|
||||
|
||||
/** Height of the loaded font texture */
|
||||
#define FONT_TEXTURE_HEIGHT FONT_TEXTURE_WIDTH
|
||||
|
||||
/** Refer to STBTT docs for OpenGL Fill Mode v d3d Fill Modes */
|
||||
#define FONT_FILL_MODE 1
|
||||
|
||||
/** Passed to STBTT for scaling the font, essentially the font resolution */
|
||||
#define FONT_TEXTURE_SIZE 64.0f
|
||||
|
||||
/** The global scale, just used to provide fine control of font sizes */
|
||||
#define FONT_GLOBAL_SCALE 0.5f;
|
||||
|
||||
/** Default Font Size (on which all font scales are based) */
|
||||
#define FONT_SIZE_DEFAULT 16.0f
|
||||
|
||||
// Chars
|
||||
#define FONT_NEWLINE '\n'
|
||||
#define FONT_SPACE ' '
|
||||
|
||||
// Heights
|
||||
#define FONT_LINE_HEIGHT FONT_TEXTURE_SIZE * 0.75f
|
||||
#define FONT_INITIAL_LINE FONT_LINE_HEIGHT * 0.75f
|
||||
#define FONT_SPACE_SIZE FONT_TEXTURE_SIZE * 0.45f
|
||||
|
||||
/** Maximum number of newlines that a font text info can hold. */
|
||||
#define FONT_TEXT_INFO_LINES_MAX 16
|
||||
|
||||
/** Representation of a font that can be used to render text */
|
||||
typedef struct {
|
||||
texture_t texture;
|
||||
stbtt_bakedchar characterData[FONT_NUM_CHARS];
|
||||
} font_t;
|
||||
|
||||
typedef struct {
|
||||
/** What (real character) index the line starts at */
|
||||
int32_t start;
|
||||
/** How many (real) characters the line is in length */
|
||||
int32_t length;
|
||||
} fonttextinfoline_t;
|
||||
|
||||
typedef struct {
|
||||
/** How many raw chars are in the string */
|
||||
int32_t length;
|
||||
|
||||
/** How many real characters (non whitespace) are in the string */
|
||||
int32_t realLength;
|
||||
|
||||
/** How many lines is the string? Trailing newlines will count */
|
||||
int32_t lineCount;
|
||||
|
||||
/** The real character info for each line */
|
||||
fonttextinfoline_t lines[FONT_TEXT_INFO_LINES_MAX];
|
||||
|
||||
/** Dimensions of the string */
|
||||
float width, height;
|
||||
} fonttextinfo_t;
|
||||
|
||||
/**
|
||||
* Initializes Font from raw TTF data.
|
||||
* @param font Font to initialize
|
||||
* @param data Data to intialize for.
|
||||
*/
|
||||
void fontInit(font_t *font, char *data);
|
||||
|
||||
/**
|
||||
* Clean up a previously loaded font
|
||||
* @param font Loaded font.
|
||||
*/
|
||||
void fontDispose(font_t *Font);
|
||||
|
||||
/**
|
||||
* Convert a Font's Size into a Font Scale. The scale is based on the font's
|
||||
* default size for the given texture size (Refer to FONT_SIZE_DEFAULT).
|
||||
*
|
||||
* @param fontSize Font size to convert.
|
||||
* @return The times scale that the font size is to the textured default.
|
||||
*/
|
||||
float fontGetScale(float fontSize);
|
||||
|
||||
/**
|
||||
* Internal method that adds a line to the text buffer process.
|
||||
*
|
||||
* @param info Info to add to.
|
||||
* @param start Start character index for the next line.
|
||||
* @param len Length of the next line.
|
||||
* @return True if added a line successfully, otherwise false.
|
||||
*/
|
||||
bool _fontTextBufferAddLine(fonttextinfo_t *info, int32_t start, int32_t len);
|
||||
|
||||
/**
|
||||
* Clamps text to a max width, inserting newlines where possible.
|
||||
*
|
||||
* @param font Font to use
|
||||
* @param primitive Primitive to buffer the text to.
|
||||
* @param info Font text info to clamp into.
|
||||
* @param text Text to clamp.
|
||||
* @param maxWidth Max width (pixels) to clamp the text to, -1 for no max width.
|
||||
* @param fontSize Font Size of the text.
|
||||
*/
|
||||
void fontTextBuffer(
|
||||
font_t *font, primitive_t *primitive, fonttextinfo_t *info, char *text,
|
||||
float maxWidth, float fontSize
|
||||
);
|
||||
|
||||
/**
|
||||
* Returns the number of real characters on the lines specified. Useful when
|
||||
* attempting to clamp to a set of lines.
|
||||
*
|
||||
* @param info Info to get the character counts from.
|
||||
* @param start Starting line index.
|
||||
* @param count Count of lines, this method will clamp to info's line length.
|
||||
* @returns The count of characters in those lines summed.
|
||||
*/
|
||||
int32_t fontGetLineCharCount(fonttextinfo_t *info, int32_t start,int32_t count);
|
@ -1,76 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "framebuffer.h"
|
||||
|
||||
void frameBufferInit(framebuffer_t *fb, int32_t w, int32_t h) {
|
||||
// At least one pixel
|
||||
if(w <= 0) w = 1;
|
||||
if(h <= 0) h = 1;
|
||||
|
||||
// Create Color Attachment texture.
|
||||
textureInit(&fb->texture, w, h, NULL);
|
||||
|
||||
// Create Frame Buffer
|
||||
glGenFramebuffers(1, &fb->fboId);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fb->fboId);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
||||
GL_TEXTURE_2D, fb->texture.id, 0
|
||||
);
|
||||
|
||||
// Render Buffer
|
||||
glGenRenderbuffers(1, &fb->rboId);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, fb->rboId);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, w, h);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
|
||||
GL_RENDERBUFFER, fb->rboId
|
||||
);
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||
uint32_t error;
|
||||
error = 0;
|
||||
}
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
void frameBufferUse(framebuffer_t *frameBuffer, bool clear) {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer->fboId);
|
||||
glViewport(0, 0, frameBuffer->texture.width, frameBuffer->texture.height);
|
||||
|
||||
if(clear) {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void frameBufferResize(framebuffer_t *frameBuffer,int32_t width,int32_t height){
|
||||
if((
|
||||
frameBuffer->texture.width == width &&
|
||||
frameBuffer->texture.height == height
|
||||
)) return;
|
||||
|
||||
frameBufferDispose(frameBuffer);
|
||||
frameBufferInit(frameBuffer, width, height);
|
||||
}
|
||||
|
||||
void frameBufferUnbind(float viewWidth, float viewHeight, bool clear) {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glViewport(0, 0, (GLsizei)viewWidth, (GLsizei)viewHeight);
|
||||
|
||||
if(clear) {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void frameBufferDispose(framebuffer_t *frameBuffer) {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||
glDeleteRenderbuffers(1, &frameBuffer->rboId);
|
||||
glDeleteFramebuffers(1, &frameBuffer->fboId);
|
||||
textureDispose(&frameBuffer->texture);
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "texture.h"
|
||||
|
||||
typedef struct {
|
||||
GLuint fboId;
|
||||
GLuint rboId;
|
||||
texture_t texture;
|
||||
} framebuffer_t;
|
||||
|
||||
/**
|
||||
* Initializes frame buffer that can be rendered to.
|
||||
* @param frameBuffer Frame buffer to initialize.
|
||||
* @param width Width of the frame buffer (in pixels).
|
||||
* @param height Height of the frame buffer (in pixels).
|
||||
* @return A renderable frame buffer.
|
||||
*/
|
||||
void frameBufferInit(framebuffer_t *buffer, int32_t width, int32_t height);
|
||||
|
||||
/**
|
||||
* Use a given frame buffer as the current rendering context.
|
||||
*
|
||||
* @param frameBuffer Frame buffer to use.
|
||||
* @param clear Whether or not to clear the frame buffer prior to rendering.
|
||||
*/
|
||||
void frameBufferUse(framebuffer_t *frameBuffer, bool clear);
|
||||
|
||||
/**
|
||||
* Resize an existing frame buffer. This will do the check if resizing is even
|
||||
* necessary for you.
|
||||
*
|
||||
* @param frameBuffer Frame buffer to resize.
|
||||
* @param width New width of the frame buffer.
|
||||
* @param height New height of the frame buffer.
|
||||
*/
|
||||
void frameBufferResize(framebuffer_t *frameBuffer,int32_t width,int32_t height);
|
||||
|
||||
/**
|
||||
* Unbind the currently bound frame buffer.
|
||||
*
|
||||
* @param viewWidth Viewport width.
|
||||
* @param viewHeight Viewport height.
|
||||
* @param clear Whether or not to clear the back buffer.
|
||||
*/
|
||||
void frameBufferUnbind(float viewWidth, float viewHeight, bool clear);
|
||||
|
||||
/**
|
||||
* Dispose/cleanup a previously created frame buffer.
|
||||
*
|
||||
* @param frameBuffer Frame Buffer to clean.
|
||||
*/
|
||||
void frameBufferDispose(framebuffer_t *frameBuffer);
|
@ -1,62 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "matrix.h"
|
||||
|
||||
void matrixIdentity(matrix_t *matrix) {
|
||||
glm_mat4_identity(matrix->internalMatrix);
|
||||
}
|
||||
|
||||
void matrixLookAt(matrix_t *matrix,
|
||||
float x,float y,float z,
|
||||
float tx,float ty, float tz,
|
||||
float ux, float uy, float uz
|
||||
) {
|
||||
glm_lookat(
|
||||
(vec3){ x, y, z },
|
||||
(vec3){ tx, ty, tz },
|
||||
(vec3){ ux, uy, uz },
|
||||
matrix->internalMatrix
|
||||
);
|
||||
}
|
||||
|
||||
void matrixLook(matrix_t *matrix,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll,
|
||||
float ux, float uy, float uz
|
||||
) {
|
||||
glm_look(
|
||||
(vec3){ x, y, z },
|
||||
(vec3){ pitch, yaw, roll },
|
||||
(vec3){ ux, uy, uz },
|
||||
matrix->internalMatrix
|
||||
);
|
||||
}
|
||||
|
||||
void matrixPerspective(matrix_t *matrix,
|
||||
float fov, float aspect, float camNear, float camFar
|
||||
) {
|
||||
glm_perspective(fov, aspect, camNear, camFar, matrix->internalMatrix);
|
||||
}
|
||||
|
||||
void matrixOrtho(matrix_t *matrix,
|
||||
float left, float right, float bottom, float top, float camNear, float camFar
|
||||
) {
|
||||
glm_ortho(left, right, bottom, top, camNear, camFar, matrix->internalMatrix);
|
||||
}
|
||||
|
||||
void matrixTranslate(matrix_t *matrix, float x, float y, float z) {
|
||||
glm_translate(matrix->internalMatrix, (vec3){ x, y, z });
|
||||
}
|
||||
|
||||
void matrixRotate(matrix_t *matrix, float angle, float x, float y, float z) {
|
||||
glm_rotate(matrix->internalMatrix, angle, (vec3){ x, y, z });
|
||||
}
|
||||
|
||||
void matrixScale(matrix_t *matrix, float x, float y, float z) {
|
||||
glm_scale(matrix->internalMatrix, (vec3){ x, y, z });
|
||||
}
|
@ -1,123 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
|
||||
/**
|
||||
* Representation for a matrix. Used as a highlevel wrapper for the math
|
||||
* functions that sit underneath this API.
|
||||
*/
|
||||
typedef struct {
|
||||
/** Internal Matrix API */
|
||||
mat4 internalMatrix;
|
||||
} matrix_t;
|
||||
|
||||
/**
|
||||
* Makes matrix identity.
|
||||
* @param matrix Matrix to identify.
|
||||
*/
|
||||
void matrixIdentity(matrix_t *matrix);
|
||||
|
||||
/**
|
||||
* Applies a look at point vector to a matrix.
|
||||
*
|
||||
* @param matrix Matrix to apply to.
|
||||
* @param x Camera source X.
|
||||
* @param y Camera source Y.
|
||||
* @param z Camera source Z.
|
||||
* @param tx Camera target X.
|
||||
* @param ty Camera target Y.
|
||||
* @param tz Camera target Z.
|
||||
* @param ux Camera up vector X.
|
||||
* @param uy Camera up vector Y.
|
||||
* @param uz Camera up vector Z.
|
||||
*/
|
||||
void matrixLookAt(matrix_t *matrix,
|
||||
float x,float y,float z,
|
||||
float tx,float ty, float tz,
|
||||
float ux, float uy, float uz
|
||||
);
|
||||
|
||||
/**
|
||||
* Applies a look vector to a matrix.
|
||||
*
|
||||
* @param matrix Matrix to apply to.
|
||||
* @param x Camera source X.
|
||||
* @param y Camera source Y.
|
||||
* @param z Camera source Z.
|
||||
* @param pitch Camera pitch
|
||||
* @param yaw Camera yaw.
|
||||
* @param roll Camera roll.
|
||||
* @param ux Camera up vector X.
|
||||
* @param uy Camera up vector Y.
|
||||
* @param uz Camera up vector Z.
|
||||
*/
|
||||
void matrixLook(matrix_t *matrix,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll,
|
||||
float ux, float uy, float uz
|
||||
);
|
||||
|
||||
/**
|
||||
* Applies a perspective projection to a matrix.
|
||||
*
|
||||
* @param matrix Matrix to apply to.
|
||||
* @param fov Field of View (in radians) to use.
|
||||
* @param aspect Aspect ratio (w/h) of the viewport.
|
||||
* @param camNear Near vector, > 0.
|
||||
* @param camFar Far view vector.
|
||||
*/
|
||||
void matrixPerspective(matrix_t *matrix,
|
||||
float fov,float aspect, float camNear, float camFar
|
||||
);
|
||||
|
||||
/**
|
||||
* Applies an orthogonal projection to a matrix.
|
||||
*
|
||||
* @param matrix Matrix to apply to.
|
||||
* @param left Left view position.
|
||||
* @param right Right view position.
|
||||
* @param bottom Bottom view position.
|
||||
* @param top Top view position.
|
||||
* @param camNear Near vector, > 0.
|
||||
* @param camFar Far view vector.
|
||||
*/
|
||||
void matrixOrtho(matrix_t *matrix,
|
||||
float left, float right, float bottom, float top, float camNear, float camFar
|
||||
);
|
||||
|
||||
/**
|
||||
* Performs a matrix translation.
|
||||
*
|
||||
* @param matrix Matrix to translate.
|
||||
* @param x X coordinate to translate on.
|
||||
* @param y Y coordinate to translate on.
|
||||
* @param z Z coordinate to translate on.
|
||||
*/
|
||||
void matrixTranslate(matrix_t *matrix, float x, float y, float z);
|
||||
|
||||
/**
|
||||
* Applies a rotation vector to a matrix.
|
||||
*
|
||||
* @param matrix Matrix to rotate.
|
||||
* @param angle Angle (in radians) to rotate.
|
||||
* @param x X vector to rotate on.
|
||||
* @param y Y vector to rotate on.
|
||||
* @param z Z vector to rotate on.
|
||||
*/
|
||||
void matrixRotate(matrix_t *matrix, float angle, float x, float y, float z);
|
||||
|
||||
/**
|
||||
* Scales a matrix.
|
||||
*
|
||||
* @param matrix Matrix to scale
|
||||
* @param x X vector to scale.
|
||||
* @param y Y vector to scale.
|
||||
* @param z Z vector to scale.
|
||||
*/
|
||||
void matrixScale(matrix_t *matrix, float x, float y, float z);
|
@ -1,111 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "cube.h"
|
||||
|
||||
void cubeBuffer(primitive_t *prim,
|
||||
float x, float y, float z,
|
||||
float w, float h, float d,
|
||||
int32_t verticeStart, int32_t indiceStart
|
||||
) {
|
||||
vertice_t *vertices = malloc(sizeof(vertice_t) * CUBE_VERTICE_COUNT);
|
||||
indice_t *indices = malloc(sizeof(indice_t) * CUBE_INDICE_COUNT);
|
||||
|
||||
vertices[0].x = x, vertices[0].y = y, vertices[0].z = z;
|
||||
vertices[0].u = 0, vertices[0].v = 0;
|
||||
|
||||
vertices[1].x = x+w, vertices[1].y = y, vertices[1].z = z;
|
||||
vertices[1].u = 1, vertices[1].v = 0;
|
||||
|
||||
vertices[2].x = x, vertices[2].y = y+h, vertices[2].z = z;
|
||||
vertices[2].u = 0, vertices[2].v = 1;
|
||||
|
||||
vertices[3].x = x+w, vertices[3].y = y+h, vertices[3].z = z;
|
||||
vertices[3].u = 1, vertices[3].v = 1;
|
||||
|
||||
vertices[4].x = x, vertices[4].y = y, vertices[4].z = z+d;
|
||||
vertices[4].u = 0, vertices[4].v = 0;
|
||||
|
||||
vertices[5].x = x+w, vertices[5].y = y, vertices[5].z = z+d;
|
||||
vertices[5].u = 1, vertices[5].v = 0;
|
||||
|
||||
vertices[6].x = x, vertices[6].y = y+h, vertices[6].z = z+d;
|
||||
vertices[6].u = 0, vertices[6].v = 1;
|
||||
|
||||
vertices[7].x = x+w, vertices[7].y = y+h, vertices[7].z = z+d;
|
||||
vertices[7].u = 1, vertices[7].v = 1;
|
||||
|
||||
// Back
|
||||
indices[ 0] = (indice_t)(verticeStart + 0);
|
||||
indices[ 1] = (indice_t)(verticeStart + 1);
|
||||
indices[ 2] = (indice_t)(verticeStart + 3);
|
||||
|
||||
indices[ 3] = (indice_t)(verticeStart + 0);
|
||||
indices[ 4] = (indice_t)(verticeStart + 2);
|
||||
indices[ 5] = (indice_t)(verticeStart + 3);
|
||||
|
||||
// Right
|
||||
indices[ 6] = (indice_t)(verticeStart + 1);
|
||||
indices[ 7] = (indice_t)(verticeStart + 5);
|
||||
indices[ 8] = (indice_t)(verticeStart + 7);
|
||||
|
||||
indices[ 9] = (indice_t)(verticeStart + 1);
|
||||
indices[10] = (indice_t)(verticeStart + 3);
|
||||
indices[11] = (indice_t)(verticeStart + 7);
|
||||
|
||||
// Left
|
||||
indices[12] = (indice_t)(verticeStart + 4);
|
||||
indices[13] = (indice_t)(verticeStart + 0);
|
||||
indices[14] = (indice_t)(verticeStart + 2);
|
||||
|
||||
indices[15] = (indice_t)(verticeStart + 4);
|
||||
indices[16] = (indice_t)(verticeStart + 6);
|
||||
indices[17] = (indice_t)(verticeStart + 2);
|
||||
|
||||
// Front
|
||||
indices[18] = (indice_t)(verticeStart + 5);
|
||||
indices[19] = (indice_t)(verticeStart + 4);
|
||||
indices[20] = (indice_t)(verticeStart + 6);
|
||||
|
||||
indices[21] = (indice_t)(verticeStart + 5);
|
||||
indices[22] = (indice_t)(verticeStart + 7);
|
||||
indices[23] = (indice_t)(verticeStart + 6);
|
||||
|
||||
// Top
|
||||
indices[24] = (indice_t)(verticeStart + 7);
|
||||
indices[25] = (indice_t)(verticeStart + 2);
|
||||
indices[26] = (indice_t)(verticeStart + 6);
|
||||
|
||||
indices[27] = (indice_t)(verticeStart + 7);
|
||||
indices[28] = (indice_t)(verticeStart + 3);
|
||||
indices[29] = (indice_t)(verticeStart + 2);
|
||||
|
||||
// Bottom
|
||||
indices[30] = (indice_t)(verticeStart + 1);
|
||||
indices[31] = (indice_t)(verticeStart + 0);
|
||||
indices[32] = (indice_t)(verticeStart + 4);
|
||||
|
||||
indices[33] = (indice_t)(verticeStart + 1);
|
||||
indices[34] = (indice_t)(verticeStart + 4);
|
||||
indices[35] = (indice_t)(verticeStart + 5);
|
||||
|
||||
|
||||
primitiveBufferVertices(prim, verticeStart, CUBE_VERTICE_COUNT, vertices);
|
||||
primitiveBufferIndices(prim, indiceStart, CUBE_INDICE_COUNT, indices);
|
||||
|
||||
free(vertices);
|
||||
free(indices);
|
||||
}
|
||||
|
||||
void cubeInit(primitive_t *primitive, float w, float h, float d) {
|
||||
primitiveInit(primitive, CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
|
||||
cubeBuffer(primitive,
|
||||
-w/2, -h/2, -d/2,
|
||||
w, h, d,
|
||||
0, 0
|
||||
);
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "primitive.h"
|
||||
|
||||
#define CUBE_VERTICE_COUNT 8
|
||||
#define CUBE_INDICE_COUNT 36
|
||||
|
||||
/**
|
||||
* Buffer the vertices and indices of a cube onto a primitive.
|
||||
* @param primitive Primitive to buffer to.
|
||||
* @param x X position of the cube.
|
||||
* @param y Y position of the cube.
|
||||
* @param z Z position of the cube.
|
||||
* @param w Width of cube.
|
||||
* @param h Height of cube.
|
||||
* @param d Depth of cube.
|
||||
* @param verticeStart The position of the vertex buffer to buffer into.
|
||||
* @param indiceStart The position of the index buffer to buffer into.
|
||||
*/
|
||||
void cubeBuffer(primitive_t *primitive,
|
||||
float x, float y, float z,
|
||||
float w, float h, float d,
|
||||
int32_t verticeStart, int32_t indiceStart
|
||||
);
|
||||
|
||||
/**
|
||||
* Creates a cube primitive of given size.
|
||||
* @param primitive Primitive to create into a cube.
|
||||
* @param w Width of cube.
|
||||
* @param h Height of cube.
|
||||
* @param d Depth of cube.
|
||||
*/
|
||||
void cubeInit(primitive_t *primitive, float w, float h, float d);
|
@ -1,132 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "primitive.h"
|
||||
|
||||
void primitiveInit(primitive_t *primitive,
|
||||
int32_t verticeCount, int32_t indiceCount
|
||||
) {
|
||||
primitive->verticeCount = verticeCount;
|
||||
primitive->indiceCount = indiceCount;
|
||||
|
||||
// size_t sizeIndices = sizeof(uint32_t) * verticeCount;
|
||||
size_t sizePositions = sizeof(float) * verticeCount * PRIMITIVE_POSITIONS_PER_VERTICE;
|
||||
size_t sizeCoordinates = sizeof(float) * verticeCount * PRIMITIVE_COORDINATES_PER_VERTICE;
|
||||
size_t sizeIndices = sizeof(indice_t) * indiceCount;
|
||||
|
||||
// Create some buffers, one for the vertex data, one for the indices
|
||||
GLuint *buffer = malloc(sizeof(GLuint) * 2);
|
||||
glGenBuffers(2, buffer);
|
||||
|
||||
primitive->vertexBuffer = buffer[0];
|
||||
primitive->indexBuffer = buffer[1];
|
||||
free(buffer);
|
||||
|
||||
// Buffer an empty set of data then buffer each component
|
||||
glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizePositions+sizeCoordinates, 0, GL_DYNAMIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeIndices, 0, GL_DYNAMIC_DRAW);
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
// Setup the attrib pointers
|
||||
glVertexAttribPointer(0, PRIMITIVE_POSITIONS_PER_VERTICE, GL_FLOAT,
|
||||
GL_FALSE, 0, (void *)offset
|
||||
);
|
||||
glEnableVertexAttribArray(0);
|
||||
offset += sizePositions;
|
||||
glVertexAttribPointer(1, PRIMITIVE_COORDINATES_PER_VERTICE, GL_FLOAT,
|
||||
GL_FALSE, 0, (void *)offset
|
||||
);
|
||||
glEnableVertexAttribArray(1);
|
||||
}
|
||||
|
||||
void primitiveBufferVertices(primitive_t *primitive,
|
||||
int32_t position, int32_t count, vertice_t *vertices
|
||||
) {
|
||||
// Memory
|
||||
size_t lengthPositions, lengthCoordinates, offsetPositions, offsetCoordinates;
|
||||
float *positions, *coordinates;
|
||||
int32_t i;
|
||||
|
||||
// Setup the size of the memory that the positions and coordinates will use
|
||||
lengthPositions = sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * count;
|
||||
offsetPositions = sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * position;
|
||||
|
||||
lengthCoordinates = sizeof(float) * PRIMITIVE_COORDINATES_PER_VERTICE * count;
|
||||
offsetCoordinates = (
|
||||
(sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * primitive->verticeCount)+
|
||||
(sizeof(float) * PRIMITIVE_COORDINATES_PER_VERTICE * position)
|
||||
);
|
||||
|
||||
// Create some memory
|
||||
positions = malloc(lengthPositions);
|
||||
coordinates = malloc(lengthCoordinates);
|
||||
|
||||
// Now copy the positions and coordinates from the vertices into the buffer
|
||||
for(i = 0; i < count; i++) {
|
||||
positions[i * PRIMITIVE_POSITIONS_PER_VERTICE] = vertices[i].x;
|
||||
positions[i * PRIMITIVE_POSITIONS_PER_VERTICE + 1] = vertices[i].y;
|
||||
positions[i * PRIMITIVE_POSITIONS_PER_VERTICE + 2] = vertices[i].z;
|
||||
|
||||
coordinates[i * PRIMITIVE_COORDINATES_PER_VERTICE] = vertices[i].u;
|
||||
coordinates[i * PRIMITIVE_COORDINATES_PER_VERTICE + 1] = vertices[i].v;
|
||||
}
|
||||
|
||||
// Buffer the data into the GPU
|
||||
glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, offsetPositions, lengthPositions, positions);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, offsetCoordinates, lengthCoordinates, coordinates);
|
||||
|
||||
// Free the vertices.
|
||||
free(positions);
|
||||
free(coordinates);
|
||||
}
|
||||
|
||||
void primitiveBufferIndices(primitive_t *primitive,
|
||||
int32_t position, int32_t count, indice_t *indices
|
||||
) {
|
||||
size_t length, offset;
|
||||
offset = position * sizeof(indice_t);
|
||||
length = count * sizeof(indice_t);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer);
|
||||
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, length, indices);
|
||||
}
|
||||
|
||||
void primitiveDraw(primitive_t *primitive, int32_t start, int32_t count) {
|
||||
if(count == -1) count = primitive->indiceCount;
|
||||
|
||||
// Re-Bind the buffers
|
||||
glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, primitive->indexBuffer);
|
||||
|
||||
// Re-Calculate the attrib pointers.
|
||||
size_t offset = 0;
|
||||
glVertexAttribPointer(0, PRIMITIVE_POSITIONS_PER_VERTICE, GL_FLOAT,
|
||||
GL_FALSE, 0, (void *)offset
|
||||
);
|
||||
glEnableVertexAttribArray(0);
|
||||
offset += sizeof(float) * primitive->verticeCount * PRIMITIVE_POSITIONS_PER_VERTICE;
|
||||
glVertexAttribPointer(1, PRIMITIVE_COORDINATES_PER_VERTICE, GL_FLOAT,
|
||||
GL_FALSE, 0, (void *)offset
|
||||
);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// Render the elements.
|
||||
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (void *)(
|
||||
sizeof(indice_t)*start
|
||||
));
|
||||
}
|
||||
|
||||
void primitiveDispose(primitive_t *primitive) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glDeleteBuffers(1, &primitive->vertexBuffer);
|
||||
glDeleteBuffers(1, &primitive->indexBuffer);
|
||||
}
|
@ -1,82 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
|
||||
#define PRIMITIVE_POSITIONS_PER_VERTICE 3
|
||||
#define PRIMITIVE_COORDINATES_PER_VERTICE 2
|
||||
|
||||
/** Structure containing information about a primitive */
|
||||
typedef struct {
|
||||
/** How many vertices are in the primitive */
|
||||
int32_t verticeCount;
|
||||
/** How many indices are in the primitive */
|
||||
int32_t indiceCount;
|
||||
|
||||
/** Pointer to the vertex buffer on the GPU */
|
||||
GLuint vertexBuffer;
|
||||
/** Pointer to the index buffer on the GPU */
|
||||
GLuint indexBuffer;
|
||||
} primitive_t;
|
||||
|
||||
/** Structure containing vertice position information */
|
||||
typedef struct {
|
||||
/** Coordinates */
|
||||
float x, y, z;
|
||||
/** Texture UVs */
|
||||
float u, v;
|
||||
} vertice_t;
|
||||
|
||||
/** Indice that references a specific vertice */
|
||||
typedef unsigned int indice_t;
|
||||
|
||||
/**
|
||||
* Creates a new primitive.
|
||||
* @param primitive Primitive to initialize.
|
||||
* @param verticeCount How many vertices can the primitive hold.
|
||||
* @param indiceCount How many indices can the primitive hold.
|
||||
*/
|
||||
void primitiveInit(primitive_t *primitive,
|
||||
int32_t verticeCount, int32_t indiceCount
|
||||
);
|
||||
|
||||
/**
|
||||
* Buffer Vertices to a primitive for use in rendering.
|
||||
* @param primitive The primitive to buffer vertices into.
|
||||
* @param position The position (index) to overwrite the vertices of.
|
||||
* @param count The count of vertices to buffer.
|
||||
* @param vertices Array of vertices to buffer into the primitive.
|
||||
*/
|
||||
void primitiveBufferVertices(primitive_t *primitive,
|
||||
int32_t position, int32_t count, vertice_t *vertices
|
||||
);
|
||||
|
||||
/**
|
||||
* Buffer Indices to a primitive for use in rendering.
|
||||
* @param primitive The primitive to buffer indices into.
|
||||
* @param position The position (index) to overwrite the indices of.
|
||||
* @param count The count of indices to buffer.
|
||||
* @param indices Array of indices to buffer into the primitive.
|
||||
*/
|
||||
void primitiveBufferIndices(primitive_t *primitive,
|
||||
int32_t position, int32_t count, indice_t *indices
|
||||
);
|
||||
|
||||
/**
|
||||
* Draw a primitive. Primitives are drawn by their indices.
|
||||
* @param primitive Primitive to draw.
|
||||
* @param start Start indice (index) to draw.
|
||||
* @param count Count of indices to draw. Use -1 to draw all.
|
||||
*/
|
||||
void primitiveDraw(primitive_t *primitive, int32_t start, int32_t count);
|
||||
|
||||
/**
|
||||
* Cleanup a previously initialized primitive.
|
||||
* @param primitive Primitive to cleanup.
|
||||
*/
|
||||
void primitiveDispose(primitive_t *primitive);
|
@ -1,56 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "quad.h"
|
||||
|
||||
void quadBuffer(primitive_t *primitive, float z,
|
||||
float x0, float y0, float u0, float v0,
|
||||
float x1, float y1, float u1, float v1,
|
||||
int32_t verticeStart, int32_t indiceStart
|
||||
) {
|
||||
vertice_t *vertices = malloc(sizeof(vertice_t) * QUAD_VERTICE_COUNT);
|
||||
indice_t *indices = malloc(sizeof(indice_t) * QUAD_INDICE_COUNT);
|
||||
|
||||
vertices[0].x = x0, vertices[0].y = y0, vertices[0].z = z;
|
||||
vertices[0].u = u0, vertices[0].v = v0;
|
||||
|
||||
vertices[1].x = x1, vertices[1].y = y0, vertices[1].z = z;
|
||||
vertices[1].u = u1, vertices[1].v = v0;
|
||||
|
||||
vertices[2].x = x0, vertices[2].y = y1, vertices[2].z = z;
|
||||
vertices[2].u = u0, vertices[2].v = v1;
|
||||
|
||||
vertices[3].x = x1, vertices[3].y = y1, vertices[3].z = z;
|
||||
vertices[3].u = u1, vertices[3].v = v1;
|
||||
|
||||
indices[0] = (indice_t)(verticeStart + 0);
|
||||
indices[1] = (indice_t)(verticeStart + 1);
|
||||
indices[2] = (indice_t)(verticeStart + 2);
|
||||
|
||||
indices[3] = (indice_t)(verticeStart + 1);
|
||||
indices[4] = (indice_t)(verticeStart + 2);
|
||||
indices[5] = (indice_t)(verticeStart + 3);
|
||||
|
||||
primitiveBufferVertices(primitive,verticeStart,QUAD_VERTICE_COUNT,vertices);
|
||||
primitiveBufferIndices( primitive,indiceStart, QUAD_INDICE_COUNT, indices );
|
||||
|
||||
free(vertices);
|
||||
free(indices);
|
||||
}
|
||||
|
||||
void quadInit(primitive_t *primitive, float z,
|
||||
float x0, float y0, float u0, float v0,
|
||||
float x1, float y1, float u1, float v1
|
||||
) {
|
||||
primitiveInit(primitive, QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
|
||||
|
||||
quadBuffer(primitive, z,
|
||||
x0, y0, u0, v0,
|
||||
x1, y1, u1, v1,
|
||||
0, 0
|
||||
);
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "primitive.h"
|
||||
|
||||
#define QUAD_VERTICE_COUNT 4
|
||||
#define QUAD_INDICE_COUNT 6
|
||||
#define QUAD_INDICE_PER_QUAD 2
|
||||
|
||||
/**
|
||||
* Buffers the vertices of a quad onto a primitive.
|
||||
*
|
||||
* @param primitive The primitive to buffer to.
|
||||
* @param z The Z axis coordinate of the quad.
|
||||
* @param x0 The X lower coordinate.
|
||||
* @param y0 The Y lower coordinate.
|
||||
* @param u0 The X lower texture coordinate.
|
||||
* @param v0 The Y lower texture coordinate.
|
||||
* @param x1 The X higher coordinate.
|
||||
* @param y1 The Y higher coordinate.
|
||||
* @param u1 The X higher texture coordinate.
|
||||
* @param v1 The Y higher texture coordinate.
|
||||
* @param verticeStart Start vertice to buffer to.
|
||||
* @param indiceStart Start indice to buffer to.
|
||||
*/
|
||||
void quadBuffer(primitive_t *primitive, float z,
|
||||
float x0, float y0, float u0, float v0,
|
||||
float x1, float y1, float u1, float v1,
|
||||
int32_t verticeStart, int32_t indiceStart
|
||||
);
|
||||
|
||||
/**
|
||||
* Creates a new quad primitive.
|
||||
*
|
||||
* @param primitive Primitive to turn into a quad.
|
||||
* @param z The Z axis coordinate of the quad.
|
||||
* @param x0 The X lower coordinate.
|
||||
* @param y0 The Y lower coordinate.
|
||||
* @param u0 The X lower texture coordinate.
|
||||
* @param v0 The Y lower texture coordinate.
|
||||
* @param x1 The X higher coordinate.
|
||||
* @param y1 The Y higher coordinate.
|
||||
* @param u1 The X higher texture coordinate.
|
||||
* @param v1 The Y higher texture coordinate.
|
||||
*/
|
||||
void quadInit(primitive_t *primitive, float z,
|
||||
float x0, float y0, float u0, float v0,
|
||||
float x1, float y1, float u1, float v1
|
||||
);
|
@ -1,58 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "skywall.h"
|
||||
|
||||
void skywallInit(primitive_t *primitive) {
|
||||
primitiveInit(primitive, SKYWALL_VERTICE_COUNT, SKYWALL_INDICE_COUNT);
|
||||
|
||||
vertice_t vertices[SKYWALL_VERTICE_COUNT];
|
||||
indice_t indices[SKYWALL_INDICE_COUNT];
|
||||
int32_t n, i, j;
|
||||
float x, y, z, p, r;
|
||||
|
||||
// For each slice. We iterate slices+1 to do the wrapping mentioned below.
|
||||
for(i = 0; i < SKYWALL_SLICE_COUNT+1; i++) {
|
||||
// Get the "percentage" of the current slice, slice 0 is 0, slice n is 1
|
||||
p = (float)i/(float)SKYWALL_SLICE_COUNT;
|
||||
|
||||
// Because we we are to "seal the cylinder" we wrap around back to 0 on the
|
||||
// last slice to have the last vertice meet the first.
|
||||
if(SKYWALL_SLICE_COUNT == i) {
|
||||
r = 0;
|
||||
} else {
|
||||
r = p * MATH_PI * 2.0f;// Convert % to radians
|
||||
}
|
||||
|
||||
r += mathDeg2Rad(90);
|
||||
|
||||
// Determine the X/Z for the given radian
|
||||
x = SKYWALL_SIZE * (float)cos(r);
|
||||
z = SKYWALL_SIZE * (float)sin(r);
|
||||
y = SKYWALL_SIZE * 1;
|
||||
|
||||
// Get the start index for the ertices
|
||||
n = i * SKYWALL_VERTICES_PER_SLICE;
|
||||
vertices[n].x = x, vertices[n].y = -y, vertices[n].z = z;
|
||||
vertices[n].u = p, vertices[n].v = 1;
|
||||
vertices[n+1].x = x, vertices[n+1].y = y, vertices[n+1].z = z;
|
||||
vertices[n+1].u = p, vertices[n+1].v = 0;
|
||||
|
||||
if(i == SKYWALL_SLICE_COUNT) continue;
|
||||
j = i * SKYWALL_INDICES_PER_SLICE;
|
||||
indices[j] = (indice_t)n;
|
||||
indices[j+1] = (indice_t)n+1;
|
||||
indices[j+2] = (indice_t)n+2;
|
||||
|
||||
indices[j+3] = (indice_t)n+3;
|
||||
indices[j+4] = (indice_t)n+2;
|
||||
indices[j+5] = (indice_t)n+1;
|
||||
}
|
||||
|
||||
primitiveBufferVertices(primitive, 0, SKYWALL_VERTICE_COUNT, vertices);
|
||||
primitiveBufferIndices(primitive, 0, SKYWALL_INDICE_COUNT, indices);
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "../../util/math.h"
|
||||
#include "primitive.h"
|
||||
|
||||
/** How many slices in each cylinder. */
|
||||
#define SKYWALL_SLICE_COUNT 40
|
||||
|
||||
/** How many vertices per slice */
|
||||
#define SKYWALL_VERTICES_PER_SLICE 2
|
||||
|
||||
/** How many indices per slice */
|
||||
#define SKYWALL_INDICES_PER_SLICE 6
|
||||
|
||||
/** How many vertices in the cylinder, +1 to have the cylinder "wrap" */
|
||||
#define SKYWALL_VERTICE_COUNT (SKYWALL_SLICE_COUNT+1)*SKYWALL_VERTICES_PER_SLICE
|
||||
|
||||
/** How many indices in the cylinder */
|
||||
#define SKYWALL_INDICE_COUNT SKYWALL_INDICES_PER_SLICE*SKYWALL_SLICE_COUNT
|
||||
|
||||
/** How big the skywall cylinder is */
|
||||
#define SKYWALL_SIZE 10
|
||||
|
||||
void skywallInit(primitive_t *primitive);
|
@ -1,41 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "render.h"
|
||||
|
||||
void renderInit() {
|
||||
// Enable GL things.
|
||||
glEnable(GL_BLEND);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
// Setup the alpha blend function.
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glDepthMask(GL_TRUE);
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
|
||||
glClearColor(0,0,0, 0.0);
|
||||
}
|
||||
|
||||
void renderFrameStart(render_t *render) {
|
||||
// Clear the frame buffer.
|
||||
renderResetFramebuffer(render);
|
||||
}
|
||||
|
||||
void renderDispose() {
|
||||
|
||||
}
|
||||
|
||||
void renderSetResolution(render_t *render, float width, float height) {
|
||||
render->width = width;
|
||||
render->height = height;
|
||||
}
|
||||
|
||||
void renderResetFramebuffer(render_t *render) {
|
||||
frameBufferUnbind(render->width, render->height, true);
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "framebuffer.h"
|
||||
|
||||
/**
|
||||
* Contains information about the current render state, can be used for querying
|
||||
* how the renderer is currently set up.
|
||||
*/
|
||||
typedef struct {
|
||||
/** Resolution (in pixels). Floats to allow subpixels in future. */
|
||||
float width, height;
|
||||
} render_t;
|
||||
|
||||
/**
|
||||
* Initialize the renderer.
|
||||
*/
|
||||
void renderInit();
|
||||
|
||||
/**
|
||||
* Render a single frame of the render loop. The renderer is not (currently)
|
||||
* responsible for render looping.
|
||||
* @param render The render manager
|
||||
*/
|
||||
void renderFrameStart(render_t *render);
|
||||
|
||||
/**
|
||||
* Cleanup a render context.
|
||||
*/
|
||||
void renderDispose();
|
||||
|
||||
/**
|
||||
* Sets the internal display resolution.
|
||||
*
|
||||
* @param render Render context to resize.
|
||||
* @param width Width of the display (in pixels).
|
||||
* @param height Height of the display (in pixels).
|
||||
*/
|
||||
void renderSetResolution(render_t *render, float width, float height);
|
||||
|
||||
/**
|
||||
* Reset the framebuffer back to the original backbuffer.
|
||||
*
|
||||
* @param render Render to reset the backbuffer to.
|
||||
*/
|
||||
void renderResetFramebuffer(render_t *render);
|
@ -1,125 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "renderlist.h"
|
||||
|
||||
void renderListInit(renderlist_t *list,int32_t width, int32_t height) {
|
||||
frameBufferInit(&list->frame, width, height);
|
||||
quadInit(&list->quad, 0, 0,0,0,0, 1,1,1,1);
|
||||
list->passCount = 0;
|
||||
}
|
||||
|
||||
renderpass_t * renderListGetPass(renderlist_t *list, uint8_t pass) {
|
||||
return list->passes + pass;
|
||||
}
|
||||
|
||||
uint8_t renderPassAdd(renderlist_t *list, shader_t *shader) {
|
||||
uint8_t i;
|
||||
renderpass_t *pass;
|
||||
|
||||
i = list->passCount++;
|
||||
pass = renderListGetPass(list, i);
|
||||
|
||||
pass->shader = shader;
|
||||
frameBufferInit(&pass->frame,
|
||||
list->frame.texture.width, list->frame.texture.height
|
||||
);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
renderpass_t * renderListRenderPass(
|
||||
renderlist_t *list, engine_t *engine, uint8_t pass
|
||||
) {
|
||||
renderpass_t *renderPass;
|
||||
|
||||
renderPass = renderListGetPass(list, pass);
|
||||
|
||||
// Bind the shader.
|
||||
frameBufferUse(&renderPass->frame, true);
|
||||
shaderUse(renderPass->shader);
|
||||
|
||||
return renderPass;
|
||||
}
|
||||
|
||||
void renderListRender(
|
||||
renderlist_t *list, shader_t *shader, shaderuniform_t *uniforms
|
||||
) {
|
||||
camera_t camera;
|
||||
int32_t i;
|
||||
renderpass_t *pass;
|
||||
|
||||
// Setup the camera
|
||||
cameraLookAt(&camera, 0,0,1, 0,0,0);
|
||||
cameraOrtho(&camera, 0,1, 0,1, 0.5f, 1.5f);
|
||||
|
||||
// Bind the framebuffer
|
||||
frameBufferUse(&list->frame, true);
|
||||
|
||||
// Set the shader
|
||||
shaderUse(shader);
|
||||
shaderUseCamera(shader, uniforms[0], uniforms[1], &camera);
|
||||
shaderUsePosition(shader, uniforms[2], 0,0,0, 0,0,0);
|
||||
|
||||
// Render each pass.
|
||||
for(i = 0; i < list->passCount; i++) {
|
||||
pass = renderListGetPass(list, i);
|
||||
shaderUseTexture(shader, uniforms[3+i], &pass->frame.texture);
|
||||
primitiveDraw(&list->quad, 0, -1);
|
||||
}
|
||||
}
|
||||
|
||||
void renderListAsBackbuffer(
|
||||
renderlist_t *list, engine_t *engine, shader_t *shader,
|
||||
shaderuniform_t *uniforms
|
||||
) {
|
||||
camera_t camera;
|
||||
|
||||
// Reset to backbuffer
|
||||
renderResetFramebuffer(&engine->render);
|
||||
|
||||
// Setup camera to look right at teh quad.
|
||||
cameraLookAt(&camera, 0,0,1, 0,0,0);
|
||||
cameraOrtho(&camera, 0,1, 0,1, 0.5f, 1.5f);
|
||||
|
||||
// Set up the shader.
|
||||
shaderUse(shader);
|
||||
shaderUseCamera(shader, uniforms[0], uniforms[1], &camera);
|
||||
shaderUsePosition(shader, uniforms[2], 0,0,0, 0,0,0);
|
||||
shaderUseTexture(shader, uniforms[3], &list->frame.texture);
|
||||
|
||||
// Render the quad to the back buffer.
|
||||
primitiveDraw(&list->quad, 0, -1);
|
||||
}
|
||||
|
||||
void renderListResize(renderlist_t *list, int32_t width, int32_t height) {
|
||||
uint8_t i;
|
||||
|
||||
if(
|
||||
width == list->frame.texture.width &&
|
||||
height == list->frame.texture.height
|
||||
) return;
|
||||
|
||||
frameBufferDispose(&list->frame);
|
||||
frameBufferInit(&list->frame, width, height);
|
||||
|
||||
for(i = 0; i < list->passCount; i++) {
|
||||
frameBufferDispose(&list->passes[i].frame);
|
||||
frameBufferInit(&list->passes[i].frame, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
void renderListDispose(renderlist_t *list) {
|
||||
uint8_t i;
|
||||
|
||||
for(i = 0; i < list->passCount; i++) {
|
||||
frameBufferDispose(&list->passes[i].frame);
|
||||
}
|
||||
|
||||
frameBufferDispose(&list->frame);
|
||||
primitiveDispose(&list->quad);
|
||||
}
|
@ -1,126 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "framebuffer.h"
|
||||
#include "primitive/primitive.h"
|
||||
#include "shader.h"
|
||||
#include "camera.h"
|
||||
#include "../engine/engine.h"
|
||||
#include "primitive/quad.h"
|
||||
#include "../util/dynarray.h"
|
||||
#include "render.h"
|
||||
|
||||
#define RENDER_PASSES_MAX 8
|
||||
|
||||
typedef struct {
|
||||
framebuffer_t frame;
|
||||
shader_t *shader;
|
||||
} renderpass_t;
|
||||
|
||||
typedef struct {
|
||||
framebuffer_t frame;
|
||||
primitive_t quad;
|
||||
|
||||
renderpass_t passes[RENDER_PASSES_MAX];
|
||||
uint8_t passCount;
|
||||
} renderlist_t;
|
||||
|
||||
typedef struct {
|
||||
shader_t shader;
|
||||
shaderuniform_t uniformTexture;
|
||||
shaderuniform_t uniformView;
|
||||
shaderuniform_t uniformProjection;
|
||||
} renderlistbackbuffershader_t;
|
||||
|
||||
/**
|
||||
* Initialize a render pass list.
|
||||
*
|
||||
* @param list List to initialize.
|
||||
* @param width Width of the frame buffer(s).
|
||||
* @param height Height of the frame buffer(s).
|
||||
*/
|
||||
void renderListInit(renderlist_t *list, int32_t width, int32_t height);
|
||||
|
||||
/**
|
||||
* Retrieve the render pass at the given index.
|
||||
*
|
||||
* @param list List to get the pass from.
|
||||
* @param pass Render pass index to get.
|
||||
* @return The render pass at the given index.
|
||||
*/
|
||||
renderpass_t * renderListGetPass(renderlist_t *list, uint8_t pass);
|
||||
|
||||
/**
|
||||
* Adds a render pass to the render list.
|
||||
*
|
||||
* @param list Render list to add the pass to.
|
||||
* @param shader Shader to use for the render pass.
|
||||
* @return The render pass index.
|
||||
*/
|
||||
uint8_t renderPassAdd(renderlist_t *list, shader_t *shader);
|
||||
|
||||
/**
|
||||
* Prepare the rendering for a specific render pass. This will set up the
|
||||
* render pass framebuffer, shader and prepare it for rendering.
|
||||
*
|
||||
* @param list List to get the render pass from.
|
||||
* @param engine Game engine for the render pass.
|
||||
* @param pass Pass index to render.
|
||||
* @return The pointer to the render pass that is now active.
|
||||
*/
|
||||
renderpass_t * renderListRenderPass(
|
||||
renderlist_t *list, engine_t *engine, uint8_t pass
|
||||
);
|
||||
|
||||
/**
|
||||
* Render a render list. The render list will provide the sum of the textures to
|
||||
* the given shader as uniforms and then call a single render against a textured
|
||||
* quad. The given shader will need to decide how to multiply the provided
|
||||
* texture indexes.
|
||||
*
|
||||
* @param list List to render.
|
||||
* @param shader Shader to use while rendering.
|
||||
* @param uniforms Uniforms for the render list. [ view, proj, model, ...text ]
|
||||
*/
|
||||
void renderListRender(
|
||||
renderlist_t *list, shader_t *shader, shaderuniform_t *uniforms
|
||||
);
|
||||
|
||||
/**
|
||||
* Takes a previously rendered render list and renders it to the backbuffer.
|
||||
* You could do this manually, but this method makes the assumption that the
|
||||
* render list is the only thing to be rendered to the backbuffer (currently).
|
||||
*
|
||||
* @param list Render list to render to the backbuffer
|
||||
* @param engine Engine to use when rendering.
|
||||
* @param shader Shader to use to render to the backbuffer.
|
||||
* @param uniforms Uniforms for the back buffer. [ view, proj, model, text ]
|
||||
*/
|
||||
void renderListAsBackbuffer(
|
||||
renderlist_t *list, engine_t *engine, shader_t *shader,
|
||||
shaderuniform_t *uniforms
|
||||
);
|
||||
|
||||
/**
|
||||
* Resize an existing render list and all its render pass frame buffers. This
|
||||
* will dispose (and clear) the existing frame buffers for both the list and
|
||||
* each of the passes. Resizing to the same size won't cause an update to occur.
|
||||
*
|
||||
* @param list List to update.
|
||||
* @param width New render list width.
|
||||
* @param height New render list height.
|
||||
*/
|
||||
void renderListResize(renderlist_t *list, int32_t width, int32_t height);
|
||||
|
||||
/**
|
||||
* Dispose a render list entirely.
|
||||
*
|
||||
* @param list Render list to dispose.
|
||||
*/
|
||||
void renderListDispose(renderlist_t *list);
|
@ -1,196 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Msters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "shader.h"
|
||||
|
||||
void shaderInit(shader_t *shader,
|
||||
char *vertexShaderSource, char* fragmentShaderSource
|
||||
) {
|
||||
int32_t isSuccess, maxLength, i, texture;
|
||||
char *error;
|
||||
GLuint shaderVertex, shaderFragment, shaderProgram;
|
||||
GLint size; // size of the variable
|
||||
GLsizei length; // name length
|
||||
|
||||
GLchar const* filesVertex[] = { vertexShaderSource };
|
||||
GLchar const* filesFragment[] = { fragmentShaderSource };
|
||||
|
||||
// Load the vertex shader first
|
||||
shaderVertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(shaderVertex, 1, filesVertex, 0);
|
||||
glCompileShader(shaderVertex);
|
||||
|
||||
// Validate
|
||||
glGetShaderiv(shaderVertex, GL_COMPILE_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetShaderiv(shaderVertex, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
error = malloc(maxLength);
|
||||
glGetShaderInfoLog(shaderVertex, maxLength, &maxLength, error);
|
||||
printf("Failed to compile vertex shader %s\n", error);
|
||||
free(error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Now load the Frag shader
|
||||
shaderFragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(shaderFragment, 1, filesFragment, 0);
|
||||
glCompileShader(shaderFragment);
|
||||
glGetShaderiv(shaderFragment, GL_COMPILE_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetShaderiv(shaderFragment, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
error = malloc(maxLength);
|
||||
glGetShaderInfoLog(shaderFragment, maxLength, &maxLength, error);
|
||||
printf("Failed to compile fragment shader %s\n", error);
|
||||
free(error);
|
||||
glDeleteShader(shaderVertex);
|
||||
return;
|
||||
}
|
||||
|
||||
// Now create the shader program.
|
||||
shaderProgram = glCreateProgram();
|
||||
glAttachShader(shaderProgram, shaderVertex);
|
||||
glAttachShader(shaderProgram, shaderFragment);
|
||||
|
||||
//Bind, Verify & Use the shader program
|
||||
glLinkProgram(shaderProgram);
|
||||
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
error = malloc(maxLength);
|
||||
glGetProgramInfoLog(shaderProgram, maxLength, &maxLength, error);
|
||||
printf("Failed to load shader program %s\n", error);
|
||||
free(error);
|
||||
glDeleteShader(shaderVertex);
|
||||
glDeleteShader(shaderFragment);
|
||||
return;
|
||||
}
|
||||
// Everything is okay, let's create the encapsulated shader.
|
||||
shader->shaderVertex = shaderVertex;
|
||||
shader->shaderFrag = shaderFragment;
|
||||
shader->shaderProgram = shaderProgram;
|
||||
|
||||
// Extract uniforms
|
||||
glGetProgramiv(shaderProgram, GL_ACTIVE_UNIFORMS, &shader->uniformCount);
|
||||
|
||||
texture = 0;
|
||||
for(i = 0; i < shader->uniformCount; i++) {
|
||||
shader->uniformNames[i] = (
|
||||
shader->uniformBuffer + (i * SHADER_UNIFORM_NAME_MAX)
|
||||
);
|
||||
|
||||
glGetActiveUniform(
|
||||
shaderProgram, (GLuint)i, SHADER_UNIFORM_NAME_MAX,
|
||||
&length, &size, shader->types + i, shader->uniformNames[i]
|
||||
);
|
||||
|
||||
// TODO: Reset uniforms to zero.
|
||||
if(shader->types[i] == GL_SAMPLER_2D) shader->textureSlots[i] = texture++;
|
||||
}
|
||||
|
||||
// Bind the shader
|
||||
shaderUse(shader);
|
||||
}
|
||||
|
||||
shaderuniform_t shaderGetUniform(shader_t *shader, char *name) {
|
||||
int32_t i;
|
||||
for(i = 0; i < shader->uniformCount; i++) {
|
||||
if(strcmp(shader->uniformNames[i], name) == 0) return i;
|
||||
}
|
||||
return (shaderuniform_t)0xFFFFFFFF;
|
||||
}
|
||||
|
||||
void shaderGetUniformArray(
|
||||
shader_t *shader, shaderuniform_t *uniformSet,
|
||||
char **uniforms, int32_t uniformCount
|
||||
) {
|
||||
int32_t i;
|
||||
for(i = 0; i < uniformCount; i++) {
|
||||
uniformSet[i] = shaderGetUniform(shader, uniforms[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void shaderDispose(shader_t *shader) {
|
||||
glDeleteProgram(shader->shaderProgram);
|
||||
glDeleteShader(shader->shaderVertex);
|
||||
glDeleteShader(shader->shaderFrag);
|
||||
}
|
||||
|
||||
void shaderUse(shader_t *shader) {
|
||||
glUseProgram(shader->shaderProgram);
|
||||
}
|
||||
|
||||
void shaderUseTexture(
|
||||
shader_t *shader, shaderuniform_t uniform, texture_t *texture
|
||||
) {
|
||||
int32_t i = shader->textureSlots[(int32_t)uniform];
|
||||
// TODO: I need to be able to get the texture ID
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
glBindTexture(GL_TEXTURE_2D, texture->id);
|
||||
glUniform1i(uniform, i);
|
||||
}
|
||||
|
||||
void shaderUseMatrix(
|
||||
shader_t *shader, shaderuniform_t uniform, matrix_t *matrix
|
||||
) {
|
||||
glUniformMatrix4fv(uniform, 1, GL_FALSE, matrix->internalMatrix[0]);
|
||||
}
|
||||
|
||||
void shaderUseColor(shader_t *shader, shaderuniform_t uniform, pixel_t color) {
|
||||
glUniform4f(uniform,
|
||||
(float)color.r / 255.0f,
|
||||
(float)color.g / 255.0f,
|
||||
(float)color.b / 255.0f,
|
||||
(float)color.a / 255.0f
|
||||
);
|
||||
}
|
||||
|
||||
void shaderUsePosition(
|
||||
shader_t *shader, shaderuniform_t uniform,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll
|
||||
) {
|
||||
matrix_t matrix;
|
||||
|
||||
matrixIdentity(&matrix);
|
||||
matrixTranslate(&matrix, x, y, z);
|
||||
|
||||
// Rotation (YZX order)
|
||||
matrixRotate(&matrix, yaw, 0, 1, 0);
|
||||
matrixRotate(&matrix, roll, 0, 0, 1);
|
||||
matrixRotate(&matrix, pitch, 1, 0, 0);
|
||||
shaderUseMatrix(shader, uniform, &matrix);
|
||||
}
|
||||
|
||||
void shaderUsePositionAndScale(
|
||||
shader_t *shader, shaderuniform_t uniform,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll,
|
||||
float scaleX, float scaleY, float scaleZ
|
||||
) {
|
||||
matrix_t matrix;
|
||||
|
||||
matrixIdentity(&matrix);
|
||||
matrixTranslate(&matrix, x, y, z);
|
||||
|
||||
// Rotation (YZX order)
|
||||
matrixRotate(&matrix, yaw, 0, 1, 0);
|
||||
matrixRotate(&matrix, roll, 0, 0, 1);
|
||||
matrixRotate(&matrix, pitch, 1, 0, 0);
|
||||
|
||||
matrixScale(&matrix, scaleX, scaleY, scaleZ);
|
||||
|
||||
shaderUseMatrix(shader, uniform, &matrix);
|
||||
}
|
||||
|
||||
void shaderUseCamera(
|
||||
shader_t *shader,
|
||||
shaderuniform_t uniformView, shaderuniform_t uniformProjection,
|
||||
camera_t *camera
|
||||
) {
|
||||
shaderUseMatrix(shader, uniformView, &camera->view);
|
||||
shaderUseMatrix(shader, uniformProjection, &camera->projection);
|
||||
}
|
@ -1,178 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Msters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <dawn/libs.h>
|
||||
#include "matrix.h"
|
||||
#include "camera.h"
|
||||
#include "texture.h"
|
||||
#include "../util/array.h"
|
||||
|
||||
#define SHADER_UNIFORM_NAME_MAX 24
|
||||
#define SHADER_UNIFORM_COUNT 16
|
||||
|
||||
/** Representation of a shader uniform */
|
||||
typedef GLuint shaderuniform_t;
|
||||
|
||||
/**
|
||||
* Structure containing information about an OpenGL Shader. For simplicity sake
|
||||
* we demand certain uninforms to be present on the shader target.
|
||||
*/
|
||||
typedef struct {
|
||||
/** Pointer to an uploaded vertex shader program */
|
||||
GLuint shaderVertex;
|
||||
|
||||
/** Pointer to an uploaded fragment shader program */
|
||||
GLuint shaderFrag;
|
||||
|
||||
/** Pointer to an uploaded shader program linked */
|
||||
GLuint shaderProgram;
|
||||
|
||||
/** Buffer of chars where we store the uniform names */
|
||||
char uniformBuffer[SHADER_UNIFORM_NAME_MAX * SHADER_UNIFORM_COUNT];
|
||||
|
||||
/** Array of strings (pointers to the above buffer) of the uniform names */
|
||||
char *uniformNames[SHADER_UNIFORM_COUNT];
|
||||
int32_t uniformCount;
|
||||
|
||||
/** Type of each uniform */
|
||||
GLenum types[SHADER_UNIFORM_COUNT];
|
||||
|
||||
/** Texture Slots (which texture slot for GL to use for each uniform) */
|
||||
uint8_t textureSlots[SHADER_UNIFORM_COUNT];
|
||||
} shader_t;
|
||||
|
||||
/**
|
||||
* Compiles a shader from vertex and fragment shader code.
|
||||
* @param shader Shader to compile into.
|
||||
* @param vertexShaderSource The raw vertex shader code.
|
||||
* @param fragmentShaderSource The raw fragment shader code.
|
||||
*/
|
||||
void shaderInit(shader_t *shader,
|
||||
char *vertexShaderSource, char* fragmentShaderSource
|
||||
);
|
||||
|
||||
/**
|
||||
* Return the shaderuniform_t for a given shader uniform name.
|
||||
*
|
||||
* @param shader Shader to get from
|
||||
* @param name Name to look for
|
||||
* @return The shader uniform, or -1 if not found.
|
||||
*/
|
||||
shaderuniform_t shaderGetUniform(shader_t *shader, char *name);
|
||||
|
||||
/**
|
||||
* Return an array of shaderuniform_t's into an array for a given string array.
|
||||
*
|
||||
* @param shader Shader to get the uniforms from.
|
||||
* @param uniformSet Uniform array to get.
|
||||
* @param uniforms Uniform strings to get.
|
||||
* @param uniformCount Count of uniforms you're getting.
|
||||
*/
|
||||
void shaderGetUniformArray(
|
||||
shader_t *shader, shaderuniform_t *uniformSet,
|
||||
char **uniforms, int32_t uniformCount
|
||||
);
|
||||
|
||||
/**
|
||||
* Cleanup and unload a previously loaded shader.
|
||||
* @param shader The shader to unload
|
||||
*/
|
||||
void shaderDispose(shader_t *shader);
|
||||
|
||||
/**
|
||||
* Attaches the supplied shader as the current shader.
|
||||
* @param shader The shader to attach
|
||||
*/
|
||||
void shaderUse(shader_t *shader);
|
||||
|
||||
/**
|
||||
* Attaches a texture to the shader.
|
||||
*
|
||||
* @param shader Shader to attach to.
|
||||
* @param uniform Uniform on the shader to set.
|
||||
* @param texture Texture to attach.
|
||||
*/
|
||||
void shaderUseTexture(
|
||||
shader_t *shader, shaderuniform_t uniform, texture_t *texture
|
||||
);
|
||||
|
||||
/**
|
||||
* Set's a specific shader uniform to a matrix.
|
||||
*
|
||||
* @param shader Shader to apply to.
|
||||
* @param uniform Uniform on the shader to set.
|
||||
* @param matrix Matrix to apply.
|
||||
*/
|
||||
void shaderUseMatrix(
|
||||
shader_t *shader, shaderuniform_t uniform, matrix_t *matrix
|
||||
);
|
||||
|
||||
/**
|
||||
* Set's a specific shader uniform to a color.
|
||||
*
|
||||
* @param shader Shader to apply to.
|
||||
* @param uniform Uniform on the shader to set.
|
||||
* @param color Color to set on to the uniform.
|
||||
*/
|
||||
void shaderUseColor(shader_t *shader, shaderuniform_t uniform, pixel_t color);
|
||||
|
||||
/**
|
||||
* Set's the current translation matrix onto the shader for the next
|
||||
* render to use. Rotation order is set to YZX.
|
||||
*
|
||||
* @param shader Shader to attach to.
|
||||
* @param uniform Uniform on the shader to set.
|
||||
* @param x X coordinate (world space).
|
||||
* @param y Y coordinate (world space).
|
||||
* @param z Z coordinate (world space).
|
||||
* @param pitch Pitch of the object (local space).
|
||||
* @param yaw Yaw of the object (local space).
|
||||
* @param roll Roll of the object (local space).
|
||||
*/
|
||||
void shaderUsePosition(
|
||||
shader_t *shader, shaderuniform_t uniform,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll
|
||||
);
|
||||
|
||||
/**
|
||||
* Set's the current translation matrix onto the shader for the next
|
||||
* render to use. Also provides scaling controls.
|
||||
*
|
||||
* @param shader Shader to attach to.
|
||||
* @param uniform Uniform on the shader to set.
|
||||
* @param x X coordinate (world space).
|
||||
* @param y Y coordinate (world space).
|
||||
* @param z Z coordinate (world space).
|
||||
* @param pitch Pitch of the object (local space).
|
||||
* @param yaw Yaw of the object (local space).
|
||||
* @param roll Roll of the object (local space).
|
||||
* @param scaleX X scale of model (1 being 100% scaled).
|
||||
* @param scaleY Y scale of model (1 being 100% scaled).
|
||||
* @param scaleZ Z scale of model (1 being 100% scaled).
|
||||
*/
|
||||
void shaderUsePositionAndScale(
|
||||
shader_t *shader, shaderuniform_t uniform,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll,
|
||||
float scaleX, float scaleY, float scaleZ
|
||||
);
|
||||
|
||||
/**
|
||||
* Attaches a camera to the shader.
|
||||
*
|
||||
* @param shader Shader to attach to.
|
||||
* @param uniformView Shader Uniform for the view matrix.
|
||||
* @param uniformProjection Shader Uniform for the view matrix.
|
||||
* @param camera Camera to attach.
|
||||
*/
|
||||
void shaderUseCamera(
|
||||
shader_t *shader,
|
||||
shaderuniform_t uniformView, shaderuniform_t uniformProjection,
|
||||
camera_t *camera
|
||||
);
|
@ -1,49 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "spritebatch.h"
|
||||
|
||||
void spriteBatchInit(spritebatch_t *batch, int32_t maxSprites) {
|
||||
batch->maxSprites = maxSprites;
|
||||
batch->currentSprite = 0;
|
||||
|
||||
primitiveInit(&batch->primitive,
|
||||
maxSprites*QUAD_VERTICE_COUNT, maxSprites*QUAD_INDICE_COUNT
|
||||
);
|
||||
}
|
||||
|
||||
void spriteBatchQuad(spritebatch_t *spriteBatch, int32_t index,
|
||||
float x, float y, float z, float width, float height,
|
||||
float u0, float v0, float u1, float v1
|
||||
) {
|
||||
if(index == -1) {
|
||||
index = spriteBatch->currentSprite++;
|
||||
} else {
|
||||
spriteBatch->currentSprite = mathMax(index, spriteBatch->currentSprite);
|
||||
}
|
||||
|
||||
quadBuffer(&spriteBatch->primitive, z,
|
||||
x, y, u0, v0,
|
||||
x+width, y+height, u1, v1,
|
||||
index*QUAD_VERTICE_COUNT, index*QUAD_INDICE_COUNT
|
||||
);
|
||||
}
|
||||
|
||||
void spriteBatchFlush(spritebatch_t *spriteBatch) {
|
||||
spriteBatch->currentSprite = 0;
|
||||
}
|
||||
|
||||
void spriteBatchDraw(spritebatch_t *spriteBatch, int32_t index, int32_t count) {
|
||||
if(count == -1) count = spriteBatch->currentSprite;
|
||||
primitiveDraw(&spriteBatch->primitive,
|
||||
index*QUAD_INDICE_COUNT, count*QUAD_INDICE_COUNT
|
||||
);
|
||||
}
|
||||
|
||||
void spriteBatchDispose(spritebatch_t *spriteBatch) {
|
||||
primitiveDispose(&spriteBatch->primitive);
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "../util/math.h"
|
||||
#include "primitive/primitive.h"
|
||||
#include "primitive/quad.h"
|
||||
|
||||
/** Definition of a Sprite Batch. */
|
||||
typedef struct {
|
||||
/** Maximum sprites the batch can hold. */
|
||||
int32_t maxSprites;
|
||||
|
||||
/** The current/next sprite index. */
|
||||
int32_t currentSprite;
|
||||
|
||||
/** Internal primitive */
|
||||
primitive_t primitive;
|
||||
} spritebatch_t;
|
||||
|
||||
/**
|
||||
* Creates a new Sprite Batch made of standard quads.
|
||||
*
|
||||
* @param batch Sprite batch to init.
|
||||
* @param maxSprites The maxiumum number of sprites the batch can hold.
|
||||
*/
|
||||
void spriteBatchInit(spritebatch_t *batch, int32_t maxSprites);
|
||||
|
||||
/**
|
||||
* Renders a sprite onto a given Sprite Batch.
|
||||
*
|
||||
* @param spriteBatch The sprite batch to render to.
|
||||
* @param index The index within the sprite batch. Set to -1 to select "next".
|
||||
* @param x X coordinate of the sprite.
|
||||
* @param y Y coordinate of the sprite.
|
||||
* @param width Width of the sprite.
|
||||
* @param height Height of the sprite.
|
||||
* @param u0 Texture U coordinate (min).
|
||||
* @param v0 Texture V coordinate (min).
|
||||
* @param u1 Texture U coordinate (max).
|
||||
* @param v1 Texture V coordinate (max).
|
||||
*/
|
||||
void spriteBatchQuad(spritebatch_t *spriteBatch, int32_t index,
|
||||
float x, float y, float z, float width, float height,
|
||||
float u0, float v0, float u1, float v1
|
||||
);
|
||||
|
||||
/**
|
||||
* Flushes a sprite batch to reset the indexes.
|
||||
* @param spriteBatch The batch.
|
||||
*/
|
||||
void spriteBatchFlush(spritebatch_t *spriteBatch);
|
||||
|
||||
/**
|
||||
* Draws the Sprite Batch.
|
||||
*
|
||||
* @param spriteBatch The sprite batch to render.
|
||||
* @param start Start index to render from.
|
||||
* @param count Count of sprites to render. Set to -1 to render to the current.
|
||||
*/
|
||||
void spriteBatchDraw(spritebatch_t *spriteBatch, int32_t start, int32_t count);
|
||||
|
||||
/**
|
||||
* Disposes a previously created Sprite Batch.
|
||||
*
|
||||
* @param spriteBatch The sprite batch to dispose.
|
||||
*/
|
||||
void spriteBatchDispose(spritebatch_t *spriteBatch);
|
@ -1,64 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "texture.h"
|
||||
|
||||
// Due to some compiler bullshit, this is here.
|
||||
#ifndef STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
#endif
|
||||
|
||||
void textureInit(texture_t *texture, int32_t width, int32_t height,
|
||||
pixel_t *pixels
|
||||
) {
|
||||
texture->width = width;
|
||||
texture->height = height;
|
||||
|
||||
// Generate a texture ID and bind.
|
||||
glGenTextures(1, &texture->id);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture->id);
|
||||
|
||||
// Setup our preferred texture params
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
// Start by buffering all transparent black pixels.
|
||||
if(pixels == NULL) {
|
||||
// TODO: I can optimize this, I think the GPU can default this somehow
|
||||
pixels = calloc(width * height, sizeof(pixel_t));
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, pixels
|
||||
);
|
||||
free(pixels);
|
||||
} else {
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, pixels
|
||||
);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void textureBufferPixels(texture_t *texture,
|
||||
int32_t x, int32_t y, int32_t width, int32_t height, pixel_t *pixels
|
||||
) {
|
||||
glBindTexture(GL_TEXTURE_2D, texture->id);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0,
|
||||
x, y, width, height,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, pixels
|
||||
);
|
||||
}
|
||||
|
||||
void textureDispose(texture_t *texture) {
|
||||
glDeleteTextures(1, &texture->id);
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
|
||||
/**
|
||||
* Structure detailing information about a texture.
|
||||
* Because we plan to upload the pixels of a texture into the GPU, we don't
|
||||
* store the pixels in memory because we don't need to!
|
||||
*/
|
||||
typedef struct {
|
||||
/** Width (in pixels) of the texture */
|
||||
int32_t width;
|
||||
/** Height (in pixels) of the texture */
|
||||
int32_t height;
|
||||
/** Texture ID on the GPU */
|
||||
GLuint id;
|
||||
} texture_t;
|
||||
|
||||
/** Information about a single pixel. */
|
||||
typedef struct {
|
||||
/** RGBA Color values */
|
||||
uint8_t r, g, b, a;
|
||||
} pixel_t;
|
||||
|
||||
#define PIXEL_COLOR_WHITE ((pixel_t){ .r = 255, .g = 255, .b = 255, .a = 255 })
|
||||
#define PIXEL_COLOR_RED ((pixel_t){ .r = 255, .g = 0, .b = 0, .a = 255 })
|
||||
#define PIXEL_COLOR_GREEN ((pixel_t){ .r = 0, .g = 255, .b = 0, .a = 255 })
|
||||
#define PIXEL_COLOR_BLUE ((pixel_t){ .r = 0, .g = 0, .b = 255, .a = 255 })
|
||||
#define PIXEL_COLOR_BLACK ((pixel_t){ .r = 0, .g = 0, .b = 0, .a = 255 })
|
||||
#define PIXEL_COLOR_TRANSPARENT ((pixel_t){ .r = 0, .g = 0, .b = 0, .a = 0 })
|
||||
|
||||
/**
|
||||
* Initializes a texture that can be written in to.
|
||||
*
|
||||
* @param texture Texture to initialize.
|
||||
* @param width Width of the texture (in pixels).
|
||||
* @param height Height of the texture (in pixels).
|
||||
* @param pixels Default pixel array, set to NULL to set all pixel data to 0.
|
||||
*/
|
||||
void textureInit(texture_t *texture, int32_t width, int32_t height,
|
||||
pixel_t *pixels
|
||||
);
|
||||
|
||||
/**
|
||||
* Buffer pixel data onto the GPU. Pixel buffering is rather costly so avoid
|
||||
* doing this too often.
|
||||
*
|
||||
* @param texture Texture to buffer in to.
|
||||
* @param x X coordinate in texture space to render to.
|
||||
* @param y Y coordinate in texture space to render to.
|
||||
* @param width Width of the pixel region you're buffering.
|
||||
* @param height Height of the pixel region you're buffering.
|
||||
* @param pixels Array of pixels to buffer onto the GPU.
|
||||
*/
|
||||
void textureBufferPixels(texture_t *texture,
|
||||
int32_t x, int32_t y, int32_t width, int32_t height, pixel_t *pixels
|
||||
);
|
||||
|
||||
/**
|
||||
* Clean a previously created texture.
|
||||
*
|
||||
* @param texture Texture to clean up.
|
||||
*/
|
||||
void textureDispose(texture_t *texture);
|
@ -1,29 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "texture.h"
|
||||
|
||||
#define MANAGED_TEXTURE_SCALE_MAX 4
|
||||
|
||||
typedef struct {
|
||||
int16_t width;
|
||||
int16_t height;
|
||||
uint8_t scale;
|
||||
} texturescalescale_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t channels;
|
||||
char *file;
|
||||
char *path;
|
||||
int16_t baseWidth;
|
||||
int16_t baseHeight;
|
||||
|
||||
texturescalescale_t scales[MANAGED_TEXTURE_SCALE_MAX];
|
||||
uint8_t scaleCount;
|
||||
} texturescale_t;
|
@ -1,62 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "tileset.h"
|
||||
|
||||
void tilesetInit(tileset_t *tileset,
|
||||
int32_t columns, int32_t rows,
|
||||
int32_t width, int32_t height,
|
||||
int32_t gapX, int32_t gapY,
|
||||
int32_t borderX, int32_t borderY
|
||||
) {
|
||||
float tdivX, tdivY;
|
||||
int32_t x, y, i;
|
||||
|
||||
tileset->count = rows * columns;
|
||||
tileset->divisions = malloc(sizeof(tilesetdiv_t) * tileset->count);
|
||||
|
||||
tileset->columns = columns;
|
||||
tileset->rows = rows;
|
||||
|
||||
// Calculate division sizes (pixels)
|
||||
tileset->divX = (
|
||||
(float)width - ((float)borderX * 2.0f) - ((float)gapX * ((float)columns-1))
|
||||
) / columns;
|
||||
tileset->divY = (
|
||||
(float)height - ((float)borderY * 2.0f) - ((float)gapY * ((float)rows - 1))
|
||||
) / rows;
|
||||
|
||||
// Calculate the division sizes (units)
|
||||
tdivX = tileset->divX / width;
|
||||
tdivY = tileset->divY / height;
|
||||
|
||||
// Calculate the divisions (in units)
|
||||
i = -1;
|
||||
for(y = 0; y < rows; y++) {
|
||||
for(x = 0; x < columns; x++) {
|
||||
tileset->divisions[++i].x0 = (
|
||||
borderX + (tileset->divX * x) + (gapX * x)
|
||||
) / width;
|
||||
tileset->divisions[i].x1 = tileset->divisions[i].x0 + tdivX;
|
||||
|
||||
tileset->divisions[i].y0 = (
|
||||
borderY + (tileset->divY * y) + (gapY * y)
|
||||
) / height;
|
||||
tileset->divisions[i].y1 = tileset->divisions[i].y0 + tdivY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tilesetdiv_t tilesetGetDivision(tileset_t *tileset,int32_t column,int32_t row) {
|
||||
return tileset->divisions[
|
||||
(column % tileset->columns) + (row * tileset->columns)
|
||||
];
|
||||
}
|
||||
|
||||
void tilesetDispose(tileset_t *tileset) {
|
||||
free(tileset->divisions);
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
|
||||
/** Division of a texture */
|
||||
typedef struct {
|
||||
float x0, y0, x1, y1;
|
||||
} tilesetdiv_t;
|
||||
|
||||
/** Definition of a Tileset */
|
||||
typedef struct {
|
||||
/** Count of X/Y divisions */
|
||||
int32_t columns, rows;
|
||||
|
||||
/** Size of each divison (in pixels) */
|
||||
float divX, divY;
|
||||
|
||||
/** Count of divisions (unused) */
|
||||
int32_t count;
|
||||
|
||||
/** Division information */
|
||||
tilesetdiv_t *divisions;
|
||||
} tileset_t;
|
||||
|
||||
/**
|
||||
* Create a tileset. Tilesets will be pre-divided to save performance later.
|
||||
*
|
||||
* @param tileset Tileset to init into.
|
||||
* @param columns Count of columns.
|
||||
* @param rows Count of rows.
|
||||
* @param width Width of the tileset.
|
||||
* @param height Height of the tileset.
|
||||
* @param gapX Space between each column.
|
||||
* @param gapY Space between each row.
|
||||
* @param borderX Space around the edge of the tileset.
|
||||
* @param borderY Space around the edge of the tileset.
|
||||
*/
|
||||
void tilesetInit(tileset_t *tileset,
|
||||
int32_t columns, int32_t rows,
|
||||
int32_t width, int32_t height,
|
||||
int32_t gapX, int32_t gapY,
|
||||
int32_t borderX, int32_t borderY
|
||||
);
|
||||
|
||||
/**
|
||||
* Retreive the division for a given tileset coordinate.
|
||||
*
|
||||
* @param tileset Tileset to retreive from.
|
||||
* @param column X axis of the tileset.
|
||||
* @param row Y axis of the tileset.
|
||||
* @returns The Tileset division.
|
||||
*/
|
||||
tilesetdiv_t tilesetGetDivision(tileset_t *tileset,int32_t column, int32_t row);
|
||||
|
||||
/**
|
||||
* Cleans a previously created tileset
|
||||
*
|
||||
* @param tileset Cleanup the tileset.
|
||||
*/
|
||||
void tilesetDispose(tileset_t *tileset);
|
Reference in New Issue
Block a user