From 9ec9111e895f68692535b6c7524b41aeb1068928 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 14 Apr 2021 18:18:48 +0200 Subject: [PATCH] Implemented the remaining methods to Font. --- 03_sdl_basics/16_sdl_ttf/compile.sh | 4 +- 03_sdl_basics/16_sdl_ttf/font.cpp | 170 ++++++++++++++++++++- 03_sdl_basics/16_sdl_ttf/font.h | 94 ++++++------ 03_sdl_basics/16_sdl_ttf/main_scene.cpp | 2 +- 03_sdl_basics/16_sdl_ttf/vector2.cpp | 172 +++++++++++++++++++++ 03_sdl_basics/16_sdl_ttf/vector2.h | 48 ++++++ 03_sdl_basics/16_sdl_ttf/vector3.cpp | 190 ++++++++++++++++++++++++ 03_sdl_basics/16_sdl_ttf/vector3.h | 48 ++++++ 8 files changed, 675 insertions(+), 53 deletions(-) create mode 100755 03_sdl_basics/16_sdl_ttf/vector2.cpp create mode 100755 03_sdl_basics/16_sdl_ttf/vector2.h create mode 100755 03_sdl_basics/16_sdl_ttf/vector3.cpp create mode 100755 03_sdl_basics/16_sdl_ttf/vector3.h diff --git a/03_sdl_basics/16_sdl_ttf/compile.sh b/03_sdl_basics/16_sdl_ttf/compile.sh index 495a53f..2e07c48 100755 --- a/03_sdl_basics/16_sdl_ttf/compile.sh +++ b/03_sdl_basics/16_sdl_ttf/compile.sh @@ -16,6 +16,8 @@ fi g++ -Wall -g -c math.cpp -o obj/math.o g++ -Wall -g $(sdl2-config --cflags) -c rect2.cpp -o obj/rect2.o g++ -Wall -g $(sdl2-config --cflags) -c color.cpp -o obj/color.o +g++ -Wall -g $(sdl2-config --cflags) -c vector2.cpp -o obj/vector2.o +g++ -Wall -g $(sdl2-config --cflags) -c vector3.cpp -o obj/vector3.o g++ -Wall -g -c string.cpp -o obj/string.o g++ -Wall -g $(sdl2-config --cflags) -c renderer.cpp -o obj/renderer.o @@ -34,5 +36,5 @@ g++ -Wall -g $(sdl2-config --cflags) -c main_scene.cpp -o obj/main_scene.o g++ -Wall -g $(sdl2-config --cflags) -c main.cpp -o obj/main.o -g++ -o bin/program obj/math.o obj/rect2.o obj/color.o obj/string.o obj/renderer.o obj/image.o obj/texture.o obj/sprite.o obj/camera.o obj/font.o obj/button.o obj/scene.o obj/application.o obj/main_scene.o obj/main.o $(sdl2-config --libs) -lSDL2_ttf +g++ -o bin/program obj/math.o obj/rect2.o obj/color.o obj/vector2.o obj/vector3.o obj/string.o obj/renderer.o obj/image.o obj/texture.o obj/sprite.o obj/camera.o obj/font.o obj/button.o obj/scene.o obj/application.o obj/main_scene.o obj/main.o $(sdl2-config --libs) -lSDL2_ttf diff --git a/03_sdl_basics/16_sdl_ttf/font.cpp b/03_sdl_basics/16_sdl_ttf/font.cpp index d5dd856..3bc8902 100644 --- a/03_sdl_basics/16_sdl_ttf/font.cpp +++ b/03_sdl_basics/16_sdl_ttf/font.cpp @@ -1,12 +1,176 @@ #include "font.h" +int Font::get_style() { + return TTF_GetFontStyle(_font); +} +void Font::set_style(const int style) { + TTF_SetFontStyle(_font, style); +} -SDL_Surface *Font::render_blended(const String &text, const Color &color) { +int Font::get_outline() { + return TTF_GetFontOutline(_font); +} +void Font::set_outline(const int outline) { + TTF_SetFontOutline(_font, outline); +} + +int Font::get_hinting() { + return TTF_GetFontHinting(_font); +} +void Font::set_hinting(const int hinting) { + TTF_SetFontHinting(_font, hinting); +} + +int Font::get_height() { + return TTF_FontHeight(_font); +} +int Font::get_ascent() { + return TTF_FontAscent(_font); +} +int Font::get_descent() { + return TTF_FontDescent(_font); +} +int Font::get_line_skip() { + return TTF_FontLineSkip(_font); +} + +int Font::get_kerning() { + return TTF_GetFontKerning(_font); +} +void Font::set_kerning(const int allowed) { + TTF_SetFontKerning(_font, allowed); +} + +long Font::get_faces() { + return TTF_FontFaces(_font); +} + +int Font::get_face_is_fixed_width() { + return TTF_FontFaceIsFixedWidth(_font); +} +char *Font::get_face_family_name() { + return TTF_FontFaceFamilyName(_font); +} +char *Font::get_font_face_style_name() { + return TTF_FontFaceStyleName(_font); +} + +bool Font::is_glyph_provided(Uint16 ch) { + return TTF_GlyphIsProvided(_font, ch); +} + +Font::GlyphMetric Font::glyph_metrics(Uint16 ch) { + Font::GlyphMetric g; + + TTF_GlyphMetrics(_font, ch, &g.minx, &g.maxx, &g.miny, &g.maxy, &g.advance); + + return g; +} + +Vector2 Font::get_size_text(const String &text) { + int w; + int h; + + TTF_SizeText(_font, text.c_str(), &w, &h); + + return Vector2(w, h); +} + +Vector2 Font::get_size_utf8(const String &text) { + int w; + int h; + + TTF_SizeUTF8(_font, text.c_str(), &w, &h); + + return Vector2(w, h); +} + +Image *Font::render_text_solid(const String &text, const Color &fg) { if (!_font) { return nullptr; } - - return TTF_RenderText_Blended(_font, text.c_str(), color.to_sdl_color()); + + return new Image(TTF_RenderText_Solid(_font, text.c_str(), fg.to_sdl_color())); +} + +Image *Font::render_utf8_solid(const String &text, const Color &fg) { + if (!_font) { + return nullptr; + } + + return new Image(TTF_RenderUTF8_Solid(_font, text.c_str(), fg.to_sdl_color())); +} + +Image *Font::render_glyph_solid(const Uint16 ch, const Color &fg) { + if (!_font) { + return nullptr; + } + + return new Image(TTF_RenderGlyph_Solid(_font, ch, fg.to_sdl_color())); +} + +Image *Font::render_text_shaded(const String &text, const Color &fg, const Color &bg) { + if (!_font) { + return nullptr; + } + + return new Image(TTF_RenderText_Shaded(_font, text.c_str(), fg.to_sdl_color(), bg.to_sdl_color())); +} + +Image *Font::render_utf8_shaded(const String &text, const Color &fg, const Color &bg) { + if (!_font) { + return nullptr; + } + + return new Image(TTF_RenderUTF8_Shaded(_font, text.c_str(), fg.to_sdl_color(), bg.to_sdl_color())); +} + +Image *Font::render_glyph_shaded(const Uint16 ch, const Color &fg, const Color &bg) { + if (!_font) { + return nullptr; + } + + return new Image(TTF_RenderGlyph_Shaded(_font, ch, fg.to_sdl_color(), bg.to_sdl_color())); +} + +Image *Font::render_text_blended(const String &text, const Color &fg) { + if (!_font) { + return nullptr; + } + + return new Image(TTF_RenderText_Blended(_font, text.c_str(), fg.to_sdl_color())); +} + +Image *Font::render_utf8_blended(const String &text, const Color &fg) { + if (!_font) { + return nullptr; + } + + return new Image(TTF_RenderUTF8_Blended(_font, text.c_str(), fg.to_sdl_color())); +} + +Image *Font::render_text_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length) { + if (!_font) { + return nullptr; + } + + return new Image(TTF_RenderText_Blended_Wrapped(_font, text.c_str(), fg.to_sdl_color(), wrap_length)); +} + +Image *Font::render_utf8_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length) { + if (!_font) { + return nullptr; + } + + return new Image(TTF_RenderUTF8_Blended_Wrapped(_font, text.c_str(), fg.to_sdl_color(), wrap_length)); +} + +Image *Font::render_glyph_blended(const Uint16 ch, const Color &fg) { + if (!_font) { + return nullptr; + } + + return new Image(TTF_RenderGlyph_Blended(_font, ch, fg.to_sdl_color())); } void Font::load(const String &file_name, const int ptsize, const int index) { diff --git a/03_sdl_basics/16_sdl_ttf/font.h b/03_sdl_basics/16_sdl_ttf/font.h index 3272f44..acd4504 100644 --- a/03_sdl_basics/16_sdl_ttf/font.h +++ b/03_sdl_basics/16_sdl_ttf/font.h @@ -4,76 +4,74 @@ #include #include -#include "string.h" #include "color.h" +#include "string.h" +#include "vector2.h" +#include "image.h" class Font { public: - //extern DECLSPEC int SDLCALL TTF_GetFontStyle(const TTF_Font *font); - //extern DECLSPEC void SDLCALL TTF_SetFontStyle(TTF_Font *font, int style); - //extern DECLSPEC int SDLCALL TTF_GetFontOutline(const TTF_Font *font); - // extern DECLSPEC void SDLCALL TTF_SetFontOutline(TTF_Font *font, int outline); + struct GlyphMetric { + int minx; + int maxx; + int miny; + int maxy; + int advance; + }; - //extern DECLSPEC int SDLCALL TTF_GetFontHinting(const TTF_Font *font); - //extern DECLSPEC void SDLCALL TTF_SetFontHinting(TTF_Font *font, int hinting); + int get_style(); + void set_style(const int style); - //extern DECLSPEC int SDLCALL TTF_FontHeight(const TTF_Font *font); -/* - extern DECLSPEC int SDLCALL TTF_FontAscent(const TTF_Font *font); + int get_outline(); + void set_outline(const int outline); - extern DECLSPEC int SDLCALL TTF_FontDescent(const TTF_Font *font); + int get_hinting(); + void set_hinting(const int hinting); - extern DECLSPEC int SDLCALL TTF_FontLineSkip(const TTF_Font *font); + int get_height(); + int get_ascent(); + int get_descent(); + int get_line_skip(); - extern DECLSPEC int SDLCALL TTF_GetFontKerning(const TTF_Font *font); - extern DECLSPEC void SDLCALL TTF_SetFontKerning(TTF_Font *font, int allowed); + int get_kerning(); + void set_kerning(const int allowed); - extern DECLSPEC long SDLCALL TTF_FontFaces(const TTF_Font *font); + long get_faces(); - extern DECLSPEC int SDLCALL TTF_FontFaceIsFixedWidth(const TTF_Font *font); - extern DECLSPEC char * SDLCALL TTF_FontFaceFamilyName(const TTF_Font *font); - extern DECLSPEC char * SDLCALL TTF_FontFaceStyleName(const TTF_Font *font); + int get_face_is_fixed_width(); + char *get_face_family_name(); + char *get_font_face_style_name(); - extern DECLSPEC int SDLCALL TTF_GlyphIsProvided(const TTF_Font *font, Uint16 ch); + bool is_glyph_provided(Uint16 ch); - extern DECLSPEC int SDLCALL TTF_GlyphMetrics(TTF_Font *font, Uint16 ch,int *minx, int *maxx,int *miny, int *maxy, int *advance); + GlyphMetric glyph_metrics(Uint16 ch); - extern DECLSPEC int SDLCALL TTF_SizeText(TTF_Font *font, const char *text, int *w, int *h); - extern DECLSPEC int SDLCALL TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h); - extern DECLSPEC int SDLCALL TTF_SizeUNICODE(TTF_Font *font, const Uint16 *text, int *w, int *h); + Vector2 get_size_text(const String &text); + Vector2 get_size_utf8(const String &text); + Vector2 get_size_unicode(const String &text); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Solid(TTF_Font *font,const char *text, SDL_Color fg); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Solid(TTF_Font *font,const char *text, SDL_Color fg); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Solid(TTF_Font *font, const Uint16 *text, SDL_Color fg); + Image *render_text_solid(const String &text, const Color &fg); + Image *render_utf8_solid(const String &text, const Color &fg); + Image *render_glyph_solid(const Uint16 ch, const Color &fg); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Solid(TTF_Font *font, Uint16 ch, SDL_Color fg); + Image *render_text_shaded(const String &text, const Color &fg, const Color &bg); + Image *render_utf8_shaded(const String &text, const Color &fg, const Color &bg); + Image *render_glyph_shaded(const Uint16 ch, const Color &fg, const Color &bg); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Shaded(TTF_Font *font,const char *text, SDL_Color fg, SDL_Color bg); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Shaded(TTF_Font *font,const char *text, SDL_Color fg, SDL_Color bg); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Shaded(TTF_Font *font,const Uint16 *text, SDL_Color fg, SDL_Color bg); + Image *render_text_blended(const String &text, const Color &fg); + Image *render_utf8_blended(const String &text, const Color &fg); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Shaded(TTF_Font *font, Uint16 ch, SDL_Color fg, SDL_Color bg); + Image *render_text_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length); + Image *render_utf8_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended(TTF_Font *font, const char *text, SDL_Color fg); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended(TTF_Font *font,const char *text, SDL_Color fg); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended(TTF_Font *font, const Uint16 *text, SDL_Color fg); + Image *render_glyph_blended(const Uint16 ch, const Color &fg); - - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended_Wrapped(TTF_Font *font,const char *text, SDL_Color fg, Uint32 wrapLength); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended_Wrapped(TTF_Font *font,const char *text, SDL_Color fg, Uint32 wrapLength); - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended_Wrapped(TTF_Font *font,const Uint16 *text, SDL_Color fg, Uint32 wrapLength); - - extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Blended(TTF_Font *font, Uint16 ch, SDL_Color fg); - */ - - SDL_Surface *render_blended(const String &text, const Color &color); - - void load(const String &file_name, const int ptsize, const int index = -1); - void free(); + void load(const String &file_name, const int ptsize, const int index = -1); + void free(); Font(); - Font(const String &file_name, const int ptsize, const int index = -1); - ~Font(); + Font(const String &file_name, const int ptsize, const int index = -1); + ~Font(); private: TTF_Font *_font; diff --git a/03_sdl_basics/16_sdl_ttf/main_scene.cpp b/03_sdl_basics/16_sdl_ttf/main_scene.cpp index 8371f35..222a3b4 100644 --- a/03_sdl_basics/16_sdl_ttf/main_scene.cpp +++ b/03_sdl_basics/16_sdl_ttf/main_scene.cpp @@ -46,7 +46,7 @@ MainScene::MainScene() { _image = new Image("ti.bmp"); _texture = new Texture(_image); _font = new Font("./DejaVuSans.ttf", 16); - _ii = new Image(_font->render_blended("Adsdsda", Color(255, 0, 0, 255))); + _ii = _font->render_text_blended("Adsdsda", Color(255, 0, 0, 255)); _iit = new Texture(_ii); _s = new Sprite(_iit); diff --git a/03_sdl_basics/16_sdl_ttf/vector2.cpp b/03_sdl_basics/16_sdl_ttf/vector2.cpp new file mode 100755 index 0000000..62cbd40 --- /dev/null +++ b/03_sdl_basics/16_sdl_ttf/vector2.cpp @@ -0,0 +1,172 @@ +#include "vector2.h" + +#include + +#define EPSILON 0.00001 + +Vector2 Vector2::abs() const { + Vector2 b; + + b.x = x >= 0 ? x : -x; + b.y = y >= 0 ? y : -y; + + return b; +} + +float Vector2::angle() const { + return atan2(x, y); +} + +float Vector2::angle_to(const Vector2 &b) const { + return atan2(cross(b), dot(b)); +} + +float Vector2::cross(const Vector2 &b) const { + return x * b.y - y * b.x; +} + +Vector2 Vector2::clamped(float len) const { + return normalized() * len; +} + +Vector2 Vector2::direction_to(const Vector2 &b) const { + return (b - *this).normalized(); +} + +float Vector2::distance_to_squared(const Vector2 &b) const { + return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y); +} + +float Vector2::distance_to(const Vector2 &b) const { + return sqrt((x - b.x) * (x - b.x) + (y - b.y) * (y - b.y)); +} + +float Vector2::dot(const Vector2 &b) const { + return x * b.x + y * b.y; +} + +bool Vector2::is_equal_approx(const Vector2 &b) const { + if (x + EPSILON < b.x && x - EPSILON > b.x && y + EPSILON < b.y && y - EPSILON > b.y) { + return true; + } + + return false; +} + +float Vector2::length() const { + return sqrt(x * x + y * y); +} + +float Vector2::length_squared() const { + return x * x + y * y; +} + +Vector2 Vector2::lerp(const Vector2 &b, const float t) const { + Vector2 v; + + v.x = x + (t * (b.x - x)); + v.y = y + (t * (b.y - y)); + + return v; +} + +Vector2 Vector2::normalized() const { + Vector2 v; + + float l = length_squared(); + + if (l != 0) { + l = sqrt(l); + + v.x = x / l; + v.y = y / l; + } + + return v; +} + +void Vector2::normalize() { + float l = length_squared(); + + if (l != 0) { + l = sqrt(l); + + x = x / l; + y = y / l; + } +} + +void Vector2::add(const Vector2 &b) { + x += b.x; + y += b.y; +} + +void Vector2::sub(const Vector2 &b) { + x -= b.x; + y -= b.y; +} + +Vector2::Vector2() { + x = 0; + y = 0; +} + +Vector2::Vector2(const Vector2 &b) { + x = b.x; + y = b.y; +} + +Vector2::Vector2(const float p_x, const float p_y) { + x = p_x; + y = p_y; +} + +Vector2 &Vector2::operator+=(const Vector2 &b) { + x += b.x; + y += b.y; + + return *this; +} + +Vector2 &Vector2::operator-=(const Vector2 &b) { + x -= b.x; + y -= b.y; + + return *this; +} + +Vector2 &Vector2::operator*=(const float b) { + x *= b; + y *= b; + + return *this; +} + +Vector2 operator*(Vector2 lhs, const float rhs) { + lhs.x *= rhs; + lhs.y *= rhs; + + return lhs; +} + +Vector2 operator+(Vector2 lhs, const Vector2 &rhs) { + lhs.x += rhs.x; + lhs.y += rhs.y; + + return lhs; +} + +Vector2 operator-(Vector2 lhs, const Vector2 &rhs) { + lhs.x -= rhs.x; + lhs.y -= rhs.y; + + return lhs; +} + +bool operator==(const Vector2 &a, const Vector2 &b) { + return a.is_equal_approx(b); +} + +bool operator!=(const Vector2 &a, const Vector2 &b) { + return !(a == b); +} diff --git a/03_sdl_basics/16_sdl_ttf/vector2.h b/03_sdl_basics/16_sdl_ttf/vector2.h new file mode 100755 index 0000000..2a2f186 --- /dev/null +++ b/03_sdl_basics/16_sdl_ttf/vector2.h @@ -0,0 +1,48 @@ +#ifndef VECTOR2_H +#define VECTOR2_H + +class Vector2 { +public: + Vector2 abs() const; + float angle() const; + float angle_to(const Vector2 &b) const; + float cross(const Vector2 &b) const; + Vector2 clamped(float len) const; + Vector2 direction_to(const Vector2 &b) const; + float distance_to_squared(const Vector2 &b) const; + float distance_to(const Vector2 &b) const; + float dot(const Vector2 &b) const; + bool is_equal_approx(const Vector2 &b) const; + + float length() const; + float length_squared() const; + + Vector2 lerp(const Vector2 &b, const float t) const; + + Vector2 normalized() const; + void normalize(); + + void add(const Vector2 &b); + void sub(const Vector2 &b); + + Vector2(); + Vector2(const Vector2 &b); + Vector2(const float p_x, const float p_y); + + Vector2 &operator+=(const Vector2 &b); + Vector2 &operator-=(const Vector2 &b); + + friend Vector2 operator+(Vector2 lhs, const Vector2 &rhs); + friend Vector2 operator-(Vector2 lhs, const Vector2 &rhs); + + Vector2 &operator*=(const float b); + friend Vector2 operator*(Vector2 lhs, const float rhs); + + friend bool operator==(const Vector2 &a, const Vector2 &b); + friend bool operator!=(const Vector2 &a, const Vector2 &b); + + float x; + float y; +}; + +#endif diff --git a/03_sdl_basics/16_sdl_ttf/vector3.cpp b/03_sdl_basics/16_sdl_ttf/vector3.cpp new file mode 100755 index 0000000..beb369d --- /dev/null +++ b/03_sdl_basics/16_sdl_ttf/vector3.cpp @@ -0,0 +1,190 @@ +#include "vector3.h" + +#include + +#define EPSILON 0.00001 + +Vector3 Vector3::abs() const { + Vector3 b; + + b.x = x >= 0 ? x : -x; + b.y = y >= 0 ? y : -y; + b.z = z >= 0 ? z : -z; + + return b; +} + +float Vector3::angle_to(const Vector3 &b) const { + return atan2(cross(b).length(), dot(b)); +} + +Vector3 Vector3::cross(const Vector3 &b) const { + Vector3 v; + + v.x = (y * b.z) - (z * b.y); + v.y = (z * b.x) - (x * b.z); + v.z = (x * b.y) - (y * b.x); + + return v; +} + +Vector3 Vector3::clamped(float len) const { + return normalized() * len; +} + +Vector3 Vector3::direction_to(const Vector3 &b) const { + return (b - *this).normalized(); +} + +float Vector3::distance_to_squared(const Vector3 &b) const { + return (b - *this).length_squared(); +} + +float Vector3::distance_to(const Vector3 &b) const { + return (b - *this).length(); +} + +float Vector3::dot(const Vector3 &b) const { + return x * b.x + y * b.y + z * b.z; +} + +bool Vector3::is_equal_approx(const Vector3 &b) const { + if (x + EPSILON < b.x && x - EPSILON > b.x && y + EPSILON < b.y && y - EPSILON > b.y && + z + EPSILON < b.z && z - EPSILON > b.z) { + return true; + } + + return false; +} + +float Vector3::length() const { + return sqrt(x * x + y * y + z * z); +} + +float Vector3::length_squared() const { + return x * x + y * y + z * z; +} + +Vector3 Vector3::lerp(const Vector3 &b, const float t) const { + Vector3 v; + + v.x = x + (t * (b.x - x)); + v.y = y + (t * (b.y - y)); + v.z = z + (t * (b.z - z)); + + return v; +} + +Vector3 Vector3::normalized() const { + Vector3 v; + + float l = length_squared(); + + if (l != 0) { + l = sqrt(l); + + v.x = x / l; + v.y = y / l; + v.z = z / l; + } + + return v; +} + +void Vector3::normalize() { + float l = length_squared(); + + if (l != 0) { + l = sqrt(l); + + x = x / l; + y = y / l; + z = z / l; + } +} + +void Vector3::add(const Vector3 &b) { + x += b.x; + y += b.y; + z += b.z; +} + +void Vector3::sub(const Vector3 &b) { + x -= b.x; + y -= b.y; + z -= b.z; +} + +Vector3::Vector3() { + x = 0; + y = 0; + z = 0; +} + +Vector3::Vector3(const Vector3 &b) { + x = b.x; + y = b.y; + z = b.z; +} + +Vector3::Vector3(const float p_x, const float p_y, const float p_z) { + x = p_x; + y = p_y; + z = p_z; +} + +Vector3 &Vector3::operator+=(const Vector3 &b) { + x += b.x; + y += b.y; + z += b.z; + + return *this; +} + +Vector3 &Vector3::operator-=(const Vector3 &b) { + x -= b.x; + y -= b.y; + z -= b.z; + + return *this; +} + +Vector3 &Vector3::operator*=(const float b) { + x *= b; + y *= b; + z *= b; + + return *this; +} + +Vector3 operator*(Vector3 lhs, const float rhs) { + lhs.x *= rhs; + lhs.y *= rhs; + lhs.z *= rhs; + + return lhs; +} + +Vector3 operator+(Vector3 lhs, const Vector3 &rhs) { + lhs.x += rhs.x; + lhs.y += rhs.y; + lhs.z += rhs.z; + + return lhs; +} + +Vector3 operator-(Vector3 lhs, const Vector3 &rhs) { + lhs.x -= rhs.x; + lhs.y -= rhs.y; + lhs.z -= rhs.z; + + return lhs; +} + +bool operator==(const Vector3 &a, const Vector3 &b) { + return a.is_equal_approx(b); +} + +bool operator!=(const Vector3 &a, const Vector3 &b) { + return !(a == b); +} diff --git a/03_sdl_basics/16_sdl_ttf/vector3.h b/03_sdl_basics/16_sdl_ttf/vector3.h new file mode 100755 index 0000000..9e7ae3c --- /dev/null +++ b/03_sdl_basics/16_sdl_ttf/vector3.h @@ -0,0 +1,48 @@ +#ifndef VECTOR3_H +#define VECTOR3_H + +class Vector3 { +public: + Vector3 abs() const; + float angle_to(const Vector3 &b) const; + Vector3 cross(const Vector3 &b) const; + Vector3 clamped(float len) const; + Vector3 direction_to(const Vector3 &b) const; + float distance_to_squared(const Vector3 &b) const; + float distance_to(const Vector3 &b) const; + float dot(const Vector3 &b) const; + bool is_equal_approx(const Vector3 &b) const; + + float length() const; + float length_squared() const; + + Vector3 lerp(const Vector3 &b, const float t) const; + + Vector3 normalized() const; + void normalize(); + + void add(const Vector3 &b); + void sub(const Vector3 &b); + + Vector3(); + Vector3(const Vector3 &b); + Vector3(const float p_x, const float p_y, const float p_z); + + Vector3 &operator+=(const Vector3 &b); + Vector3 &operator-=(const Vector3 &b); + + friend Vector3 operator+(Vector3 lhs, const Vector3 &rhs); + friend Vector3 operator-(Vector3 lhs, const Vector3 &rhs); + + Vector3 &operator*=(const float b); + friend Vector3 operator*(Vector3 lhs, const float rhs); + + friend bool operator==(const Vector3 &a, const Vector3 &b); + friend bool operator!=(const Vector3 &a, const Vector3 &b); + + float x; + float y; + float z; +}; + +#endif