mirror of
https://gitlab.com/dosowisko.net/libsuperderpy.git
synced 2025-02-08 06:06:43 +01:00
utils: add Fract util function and move math utils to separate unit
This commit is contained in:
parent
d6f6f32e00
commit
e5cf17a1a6
6 changed files with 106 additions and 51 deletions
|
@ -1,14 +1,15 @@
|
||||||
SET(SRC_LIST
|
SET(SRC_LIST
|
||||||
utils.c
|
utils.c
|
||||||
config.c
|
maths.c
|
||||||
timeline.c
|
config.c
|
||||||
gamestate.c
|
timeline.c
|
||||||
libsuperderpy.c
|
gamestate.c
|
||||||
character.c
|
libsuperderpy.c
|
||||||
internal.c
|
character.c
|
||||||
tween.c
|
internal.c
|
||||||
shader.c
|
tween.c
|
||||||
)
|
shader.c
|
||||||
|
)
|
||||||
if (EMSCRIPTEN)
|
if (EMSCRIPTEN)
|
||||||
list(APPEND SRC_LIST emscripten-audio-stream.c)
|
list(APPEND SRC_LIST emscripten-audio-stream.c)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
@ -48,6 +48,7 @@ struct GamestateResources;
|
||||||
#include "character.h"
|
#include "character.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "gamestate.h"
|
#include "gamestate.h"
|
||||||
|
#include "maths.h"
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
#include "timeline.h"
|
#include "timeline.h"
|
||||||
#include "tween.h"
|
#include "tween.h"
|
||||||
|
|
58
src/maths.c
Normal file
58
src/maths.c
Normal 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
36
src/maths.h
Normal 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
|
34
src/utils.c
34
src/utils.c
|
@ -132,40 +132,6 @@ SYMBOL_EXPORT void DrawCenteredTintedScaled(ALLEGRO_BITMAP* bitmap, ALLEGRO_COLO
|
||||||
x, y, sx, sy, 0, flags);
|
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 */
|
/* linear filtering code written by SiegeLord */
|
||||||
SYMBOL_EXPORT ALLEGRO_COLOR InterpolateColor(ALLEGRO_COLOR c1, ALLEGRO_COLOR c2, float frac) {
|
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),
|
return al_map_rgba_f(c1.r + frac * (c2.r - c1.r),
|
||||||
|
|
|
@ -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 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);
|
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);
|
ALLEGRO_COLOR InterpolateColor(ALLEGRO_COLOR c1, ALLEGRO_COLOR c2, float frac);
|
||||||
void ScaleBitmap(ALLEGRO_BITMAP* source, int width, int height);
|
void ScaleBitmap(ALLEGRO_BITMAP* source, int width, int height);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue