From e5cf17a1a68ceea1c67675d4351ba4b8b547e756 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Thu, 12 Jul 2018 20:18:23 +0200 Subject: [PATCH] utils: add Fract util function and move math utils to separate unit --- src/CMakeLists.txt | 21 ++++++++-------- src/libsuperderpy.h | 1 + src/maths.c | 58 +++++++++++++++++++++++++++++++++++++++++++++ src/maths.h | 36 ++++++++++++++++++++++++++++ src/utils.c | 34 -------------------------- src/utils.h | 7 ------ 6 files changed, 106 insertions(+), 51 deletions(-) create mode 100644 src/maths.c create mode 100644 src/maths.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5d64d3e..986f650 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,14 +1,15 @@ SET(SRC_LIST - utils.c - config.c - timeline.c - gamestate.c - libsuperderpy.c - character.c - internal.c - tween.c - shader.c - ) + utils.c + maths.c + config.c + timeline.c + gamestate.c + libsuperderpy.c + character.c + internal.c + tween.c + shader.c +) if (EMSCRIPTEN) list(APPEND SRC_LIST emscripten-audio-stream.c) endif() diff --git a/src/libsuperderpy.h b/src/libsuperderpy.h index edd2f3e..7922b0f 100644 --- a/src/libsuperderpy.h +++ b/src/libsuperderpy.h @@ -48,6 +48,7 @@ struct GamestateResources; #include "character.h" #include "config.h" #include "gamestate.h" +#include "maths.h" #include "shader.h" #include "timeline.h" #include "tween.h" diff --git a/src/maths.c b/src/maths.c new file mode 100644 index 0000000..8ced41f --- /dev/null +++ b/src/maths.c @@ -0,0 +1,58 @@ +/*! \file maths.c + * \brief Math helper functions. + */ +/* + * Copyright (c) Sebastian Krzyszkowiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "maths.h" +#include "config.h" +#include "internal.h" +#include +#include +#include +#include +#include + +SYMBOL_EXPORT double VectorLength(double x, double y, double z) { + return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)); +} + +SYMBOL_EXPORT double Wrap(double left, double right, double val) { + return left + fmod(val - left, right - left); +} + +SYMBOL_EXPORT double Clamp(double left, double right, double val) { + if (val > right) { + return right; + } + if (val < left) { + return left; + } + return val; +} + +SYMBOL_EXPORT double Lerp(double left, double right, double pos) { + return left + (right - left) * pos; +} + +SYMBOL_EXPORT double Sign(double val) { + return val / fabs(val); +} + +SYMBOL_EXPORT double Fract(double val) { + return val - floor(val); +} diff --git a/src/maths.h b/src/maths.h new file mode 100644 index 0000000..78efc64 --- /dev/null +++ b/src/maths.h @@ -0,0 +1,36 @@ +/*! \file maths.h + * \brief Headers of math helper functions. + */ +/* + * Copyright (c) Sebastian Krzyszkowiak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LIBSUPERDERPY_MATHS_H +#define LIBSUPERDERPY_MATHS_H + +#include "libsuperderpy.h" +#include +#include + +double DotProduct(const double v[], const double u[], int n); +double VectorLength(double x, double y, double z); +double Wrap(double left, double right, double val); +double Clamp(double left, double right, double val); +double Lerp(double left, double right, double pos); +double Sign(double val); +double Fract(double val); + +#endif diff --git a/src/utils.c b/src/utils.c index 5869b56..57f7494 100644 --- a/src/utils.c +++ b/src/utils.c @@ -132,40 +132,6 @@ SYMBOL_EXPORT void DrawCenteredTintedScaled(ALLEGRO_BITMAP* bitmap, ALLEGRO_COLO x, y, sx, sy, 0, flags); } -SYMBOL_EXPORT double DotProduct(const double v[], const double u[], int n) { - float result = 0.0; - for (int i = 0; i < n; i++) { - result += v[i] * u[i]; - } - return result; -} - -SYMBOL_EXPORT double VectorLength(double x, double y, double z) { - return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)); -} - -SYMBOL_EXPORT double Wrap(double left, double right, double val) { - return left + fmod(val - left, right - left); -} - -SYMBOL_EXPORT double Clamp(double left, double right, double val) { - if (val > right) { - return right; - } - if (val < left) { - return left; - } - return val; -} - -SYMBOL_EXPORT double Lerp(double left, double right, double pos) { - return left + (right - left) * pos; -} - -SYMBOL_EXPORT double Sign(double val) { - return val / fabs(val); -} - /* linear filtering code written by SiegeLord */ SYMBOL_EXPORT ALLEGRO_COLOR InterpolateColor(ALLEGRO_COLOR c1, ALLEGRO_COLOR c2, float frac) { return al_map_rgba_f(c1.r + frac * (c2.r - c1.r), diff --git a/src/utils.h b/src/utils.h index a241210..9440f6b 100644 --- a/src/utils.h +++ b/src/utils.h @@ -58,13 +58,6 @@ void DrawCentered(ALLEGRO_BITMAP* bitmap, int x, int y, int flags); void DrawCenteredScaled(ALLEGRO_BITMAP* bitmap, int x, int y, double sx, double sy, int flags); void DrawCenteredTintedScaled(ALLEGRO_BITMAP* bitmap, ALLEGRO_COLOR tint, int x, int y, double sx, double sy, int flags); -double DotProduct(const double v[], const double u[], int n); -double VectorLength(double x, double y, double z); -double Wrap(double left, double right, double val); -double Clamp(double left, double right, double val); -double Lerp(double left, double right, double pos); -double Sign(double val); - ALLEGRO_COLOR InterpolateColor(ALLEGRO_COLOR c1, ALLEGRO_COLOR c2, float frac); void ScaleBitmap(ALLEGRO_BITMAP* source, int width, int height);