utils: add Fract util function and move math utils to separate unit

This commit is contained in:
Sebastian Krzyszkowiak 2018-07-12 20:18:23 +02:00
parent d6f6f32e00
commit e5cf17a1a6
6 changed files with 106 additions and 51 deletions

View file

@ -1,5 +1,6 @@
SET(SRC_LIST
utils.c
maths.c
config.c
timeline.c
gamestate.c

View file

@ -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"

58
src/maths.c Normal file
View file

@ -0,0 +1,58 @@
/*! \file maths.c
* \brief Math helper functions.
*/
/*
* Copyright (c) Sebastian Krzyszkowiak <dos@dosowisko.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "maths.h"
#include "config.h"
#include "internal.h"
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_ttf.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
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);
}

36
src/maths.h Normal file
View file

@ -0,0 +1,36 @@
/*! \file maths.h
* \brief Headers of math helper functions.
*/
/*
* Copyright (c) Sebastian Krzyszkowiak <dos@dosowisko.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef LIBSUPERDERPY_MATHS_H
#define LIBSUPERDERPY_MATHS_H
#include "libsuperderpy.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
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

View file

@ -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),

View file

@ -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);