diff options
Diffstat (limited to 'src/libjin/utils')
-rw-r--r-- | src/libjin/utils/macros.h | 9 | ||||
-rw-r--r-- | src/libjin/utils/math.h | 8 | ||||
-rw-r--r-- | src/libjin/utils/matrix.cpp | 177 | ||||
-rw-r--r-- | src/libjin/utils/matrix.h | 153 | ||||
-rw-r--r-- | src/libjin/utils/unittest.cpp | 34 | ||||
-rw-r--r-- | src/libjin/utils/utils.h | 4 |
6 files changed, 42 insertions, 343 deletions
diff --git a/src/libjin/utils/macros.h b/src/libjin/utils/macros.h index 2ca8a9a..cdec403 100644 --- a/src/libjin/utils/macros.h +++ b/src/libjin/utils/macros.h @@ -1,9 +1,12 @@ #ifndef __JIN_MACROS_H #define __JIN_MACROS_H +#include <cstring> -#define shared +#define shared // ķ -#define CallOnce(func) static char __dummy__=(func, 1) // ֻܵһ -#define onlyonce // ֻܵһ +#define CallOnce(func) static char __dummy__=(func, 1) // ֻһ +#define onlyonce // ֻһ + +#define zero(mem) memset(&mem, 0, sizeof(mem)) #endif
\ No newline at end of file diff --git a/src/libjin/utils/math.h b/src/libjin/utils/math.h deleted file mode 100644 index 5e44ce7..0000000 --- a/src/libjin/utils/math.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __JIN_UTILS_MATH_H -#define __JIN_UTILS_MATH_H - -#include <math.h> - -#define PI 3.1415926f - -#endif
\ No newline at end of file diff --git a/src/libjin/utils/matrix.cpp b/src/libjin/utils/matrix.cpp deleted file mode 100644 index b970ec0..0000000 --- a/src/libjin/utils/matrix.cpp +++ /dev/null @@ -1,177 +0,0 @@ -#include "Matrix.h" - -#include <cstring> // memcpy -#include <cmath> - -namespace jin -{ -namespace util -{ - - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - Matrix::Matrix() - { - setIdentity(); - } - - Matrix::~Matrix() - { - } - - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - Matrix Matrix::operator * (const Matrix & m) const - { - Matrix t; - - t.e[0] = (e[0] * m.e[0]) + (e[4] * m.e[1]) + (e[8] * m.e[2]) + (e[12] * m.e[3]); - t.e[4] = (e[0] * m.e[4]) + (e[4] * m.e[5]) + (e[8] * m.e[6]) + (e[12] * m.e[7]); - t.e[8] = (e[0] * m.e[8]) + (e[4] * m.e[9]) + (e[8] * m.e[10]) + (e[12] * m.e[11]); - t.e[12] = (e[0] * m.e[12]) + (e[4] * m.e[13]) + (e[8] * m.e[14]) + (e[12] * m.e[15]); - - t.e[1] = (e[1] * m.e[0]) + (e[5] * m.e[1]) + (e[9] * m.e[2]) + (e[13] * m.e[3]); - t.e[5] = (e[1] * m.e[4]) + (e[5] * m.e[5]) + (e[9] * m.e[6]) + (e[13] * m.e[7]); - t.e[9] = (e[1] * m.e[8]) + (e[5] * m.e[9]) + (e[9] * m.e[10]) + (e[13] * m.e[11]); - t.e[13] = (e[1] * m.e[12]) + (e[5] * m.e[13]) + (e[9] * m.e[14]) + (e[13] * m.e[15]); - - t.e[2] = (e[2] * m.e[0]) + (e[6] * m.e[1]) + (e[10] * m.e[2]) + (e[14] * m.e[3]); - t.e[6] = (e[2] * m.e[4]) + (e[6] * m.e[5]) + (e[10] * m.e[6]) + (e[14] * m.e[7]); - t.e[10] = (e[2] * m.e[8]) + (e[6] * m.e[9]) + (e[10] * m.e[10]) + (e[14] * m.e[11]); - t.e[14] = (e[2] * m.e[12]) + (e[6] * m.e[13]) + (e[10] * m.e[14]) + (e[14] * m.e[15]); - - t.e[3] = (e[3] * m.e[0]) + (e[7] * m.e[1]) + (e[11] * m.e[2]) + (e[15] * m.e[3]); - t.e[7] = (e[3] * m.e[4]) + (e[7] * m.e[5]) + (e[11] * m.e[6]) + (e[15] * m.e[7]); - t.e[11] = (e[3] * m.e[8]) + (e[7] * m.e[9]) + (e[11] * m.e[10]) + (e[15] * m.e[11]); - t.e[15] = (e[3] * m.e[12]) + (e[7] * m.e[13]) + (e[11] * m.e[14]) + (e[15] * m.e[15]); - - return t; - } - - void Matrix::operator *= (const Matrix & m) - { - Matrix t = (*this) * m; - memcpy((void*)this->e, (void*)t.e, sizeof(float) * 16); - } - - const float * Matrix::getElements() const - { - return e; - } - - void Matrix::setIdentity() - { - memset(e, 0, sizeof(float) * 16); - e[0] = e[5] = e[10] = e[15] = 1; - } - - void Matrix::setTranslation(float x, float y) - { - setIdentity(); - e[12] = x; - e[13] = y; - } - - void Matrix::setRotation(float rad) - { - setIdentity(); - float c = cos(rad), s = sin(rad); - e[0] = c; e[4] = -s; - e[1] = s; e[5] = c; - } - - void Matrix::setScale(float sx, float sy) - { - setIdentity(); - e[0] = sx; - e[5] = sy; - } - - void Matrix::setShear(float kx, float ky) - { - setIdentity(); - e[1] = ky; - e[4] = kx; - } - - void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy) - { - memset(e, 0, sizeof(float) * 16); // zero out matrix - float c = cos(angle), s = sin(angle); - // matrix multiplication carried out on paper: - // |1 x| |c -s | |sx | |1 -ox| - // | 1 y| |s c | | sy | | 1 -oy| - // | 1 | | 1 | | 1 | | 1 | - // | 1| | 1| | 1| | 1 | - // move rotate scale origin - e[10] = e[15] = 1.0f; - e[0] = c * sx ; // = a - e[1] = s * sx ; // = b - e[4] = - s * sy; // = c - e[5] = c * sy; // = d - e[12] = x - ox * e[0] - oy * e[4]; - e[13] = y - ox * e[1] - oy * e[5]; - } - - void Matrix::translate(float x, float y) - { - Matrix t; - t.setTranslation(x, y); - this->operator *=(t); - } - - void Matrix::rotate(float rad) - { - Matrix t; - t.setRotation(rad); - this->operator *=(t); - } - - void Matrix::scale(float sx, float sy) - { - Matrix t; - t.setScale(sx, sy); - this->operator *=(t); - } - - void Matrix::shear(float kx, float ky) - { - Matrix t; - t.setShear(kx, ky); - this->operator *=(t); - } - - // | x | - // | y | - // | 0 | - // | 1 | - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - void Matrix::transform(vertex * dst, const vertex * src, int size) const - { - for (int i = 0; i<size; ++i) - { - // Store in temp variables in case src = dst - float x = (e[0] * src[i].x) + (e[4] * src[i].y) + (0) + (e[12]); - float y = (e[1] * src[i].x) + (e[5] * src[i].y) + (0) + (e[13]); - - dst[i].x = x; - dst[i].y = y; - } - } - -} -}
\ No newline at end of file diff --git a/src/libjin/utils/matrix.h b/src/libjin/utils/matrix.h deleted file mode 100644 index 51d7980..0000000 --- a/src/libjin/utils/matrix.h +++ /dev/null @@ -1,153 +0,0 @@ -#ifndef __JIN_MATRIX_H -#define __JIN_MATRIX_H -#include <math.h> -namespace jin -{ -namespace util -{ - - struct vertex - { - unsigned char r, g, b, a; - float x, y; - float s, t; - }; - /** - * This class is the basis for all transformations in LOVE. Althought not - * really needed for 2D, it contains 4x4 elements to be compatible with - * OpenGL without conversions. - **/ - class Matrix - { - private: - - /** - * | e0 e4 e8 e12 | - * | e1 e5 e9 e13 | - * | e2 e6 e10 e14 | - * | e3 e7 e11 e15 | - **/ - float e[16]; - - public: - - /** - * Creates a new identity matrix. - **/ - Matrix(); - - /** - * Destructor. - **/ - ~Matrix(); - - /** - * Multiplies this Matrix with another Matrix, changing neither. - * @param m The Matrix to multiply with this Matrix. - * @return The combined matrix. - **/ - Matrix operator * (const Matrix & m) const; - - /** - * Multiplies a Matrix into this Matrix. - * @param m The Matrix to combine into this Matrix. - **/ - void operator *= (const Matrix & m); - - /** - * Gets a pointer to the 16 array elements. - * @return The array elements. - **/ - const float * getElements() const; - - /** - * Resets this Matrix to the identity matrix. - **/ - void setIdentity(); - - /** - * Resets this Matrix to a translation. - * @param x Translation along x-axis. - * @param y Translation along y-axis. - **/ - void setTranslation(float x, float y); - - /** - * Resets this Matrix to a rotation. - * @param r The angle in radians. - **/ - void setRotation(float r); - - /** - * Resets this Matrix to a scale transformation. - * @param sx Scale factor along the x-axis. - * @param sy Scale factor along the y-axis. - **/ - void setScale(float sx, float sy); - - /** - * Resets this Matrix to a shear transformation. - * @param kx Shear along x-axis. - * @param ky Shear along y-axis. - **/ - void setShear(float kx, float ky); - - /** - * Creates a transformation with a certain position, orientation, scale - * and offset. Perfect for Drawables -- what a coincidence! - * - * @param x The translation along the x-axis. - * @param y The translation along the y-axis. - * @param angle The rotation (rad) around the center with offset (ox,oy). - * @param sx Scale along x-axis. - * @param sy Scale along y-axis. - * @param ox The offset for rotation along the x-axis. - * @param oy The offset for rotation along the y-axis. - * @param kx Shear along x-axis - * @param ky Shear along y-axis - **/ - void setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy); - - /** - * Multiplies this Matrix with a translation. - * @param x Translation along x-axis. - * @param y Translation along y-axis. - **/ - void translate(float x, float y); - - /** - * Multiplies this Matrix with a rotation. - * @param r Angle in radians. - **/ - void rotate(float r); - - /** - * Multiplies this Matrix with a scale transformation. - * @param sx Scale factor along the x-axis. - * @param sy Scale factor along the y-axis. - **/ - void scale(float sx, float sy); - - /** - * Multiplies this Matrix with a shear transformation. - * @param kx Shear along the x-axis. - * @param ky Shear along the y-axis. - **/ - void shear(float kx, float ky); - - /** - * Transforms an array of vertices by this Matrix. The sources and - * destination arrays may be the same. - * - * @param dst Storage for the transformed vertices. - * @param src The source vertices. - * @param size The number of vertices. - **/ - void transform(vertex * dst, const vertex * src, int size) const; - - }; - -} -} - -#endif diff --git a/src/libjin/utils/unittest.cpp b/src/libjin/utils/unittest.cpp new file mode 100644 index 0000000..764a2bd --- /dev/null +++ b/src/libjin/utils/unittest.cpp @@ -0,0 +1,34 @@ +#include "utils.h" +#if UNITTEST + +#include <stdio.h> +#include "../audio/audio.h" + +using namespace jin::audio; + +void fill_audio(void *udata, Uint8 *stream, int len) +{ + printf("%d\n", len); + memset(stream, 0x11, len); +} + +int main(int argc, char* argv[]) +{ + Audio* audio = Audio::get(); + + AudioSetting setting; + setting.freq = 22050; + setting.format = AUDIO_S16; + setting.channels = 2; + setting.callback = fill_audio; + setting.samples = 1024; + setting.userdata = NULL; + audio->init(&setting); + while (true) + { + SDL_Delay(100); + } + return 0; +} + +#endif diff --git a/src/libjin/utils/utils.h b/src/libjin/utils/utils.h index 45c8ff9..1a4ef35 100644 --- a/src/libjin/utils/utils.h +++ b/src/libjin/utils/utils.h @@ -10,7 +10,7 @@ #include "macros.h" #include "endian.h" -#include "math.h" -#include "matrix.h" + +#define UNITTEST 1 #endif
\ No newline at end of file |