From 70d23facf91e4e1c2d7e60e74d3b88663f301812 Mon Sep 17 00:00:00 2001 From: Ronja Date: Thu, 27 Oct 2022 15:38:24 +0200 Subject: [PATCH] Fonts laden korrigiert --- .idea/deploymentTargetDropDown.xml | 17 ------ app/src/main/cpp/AssetManager.cpp | 10 ++-- app/src/main/cpp/AssetManager.h | 2 + app/src/main/cpp/NativeEngine.cpp | 93 ++++++++++++++++++------------ app/src/main/cpp/NativeEngine.h | 5 -- app/src/main/cpp/Renderer.cpp | 31 ++++++++-- app/src/main/cpp/Renderer.h | 10 ++++ app/src/main/cpp/Texture.cpp | 15 +++++ app/src/main/cpp/Texture.h | 10 ++++ app/src/main/cpp/TouchInput.h | 6 +- 10 files changed, 130 insertions(+), 69 deletions(-) delete mode 100644 .idea/deploymentTargetDropDown.xml diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml deleted file mode 100644 index 80067e1..0000000 --- a/.idea/deploymentTargetDropDown.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/cpp/AssetManager.cpp b/app/src/main/cpp/AssetManager.cpp index e43eed3..1266df3 100644 --- a/app/src/main/cpp/AssetManager.cpp +++ b/app/src/main/cpp/AssetManager.cpp @@ -34,14 +34,14 @@ bool AssetManager::loadFontData(const char* path, float char_height, FontData* p if (!loadFile(path, &ttf_data)) return false; - static unsigned char bitmap_memory[512 * 512];; + static unsigned char bitmap_memory[STBTT_PH_VALUE * STBTT_PH_VALUE]; FontData font_data = {}; auto* data = reinterpret_cast(ttf_data.data); stbtt_BakeFontBitmap(data, 0, char_height, bitmap_memory, - 512, 512, + STBTT_PH_VALUE, STBTT_PH_VALUE, 32, 225, // latin-1 printable characters range font_data.char_data); font_data.char_height = char_height; @@ -74,16 +74,16 @@ bool AssetManager::loadFontBitmap(const char *path, float char_height, Texture* FileBuffer ttf_data = {}; if (!loadFile(path, &ttf_data)) return false; - static unsigned char bitmap_memory[512 * 512]; + static unsigned char bitmap_memory[STBTT_PH_VALUE * STBTT_PH_VALUE]; stbtt_bakedchar char_data[225]; stbtt_BakeFontBitmap(reinterpret_cast(ttf_data.data), 0, char_height, bitmap_memory, - 512, 512, + STBTT_PH_VALUE, STBTT_PH_VALUE, 32, 225, // latin-1 printable characters range char_data); - *p_texture = Texture(512, 512, bitmap_memory, GL_R8); + *p_texture = Texture(STBTT_PH_VALUE, STBTT_PH_VALUE, bitmap_memory, GL_R8); releaseFileBuffer(ttf_data); return true; } diff --git a/app/src/main/cpp/AssetManager.h b/app/src/main/cpp/AssetManager.h index 05c215b..b53ed1b 100644 --- a/app/src/main/cpp/AssetManager.h +++ b/app/src/main/cpp/AssetManager.h @@ -63,4 +63,6 @@ protected: AssetManager& operator=(const AssetManager&) = delete; }; +const int STBTT_PH_VALUE = 1024; + #endif diff --git a/app/src/main/cpp/NativeEngine.cpp b/app/src/main/cpp/NativeEngine.cpp index 60463fe..64802df 100644 --- a/app/src/main/cpp/NativeEngine.cpp +++ b/app/src/main/cpp/NativeEngine.cpp @@ -7,6 +7,8 @@ #include "AndroidAssetManager.h" #include "Renderer.h" #include "TouchInput.h" +/*#include "Game.h" +#include "base.h"*/ #include #include @@ -48,10 +50,11 @@ NativeEngine::NativeEngine(struct android_app *app) // // Du kannst schon Assets laden, da oben der AssetManager erzeugt wurde. + // Ende INTEGRATION + // BEISPIELCODE m_smiley = StringRepository::global->internString("smiley_PNG42.png"); - m_smiley_pos = { 100, 100 }; - AssetManager::ptr->loadFontData("Milky Honey.ttf", 24.f, &m_font); + //m_smiley_pos = { 100, 100 }; // ENDE VOM BEISPIELCODE } @@ -100,6 +103,12 @@ void NativeEngine::gameLoop() android_app_clear_key_events(input_buffer); } if (input_buffer->motionEventsCount > 0) { + float rel_size = m_display_height; + if (m_display_height > m_display_width){ + rel_size = m_display_width; + } + rel_size *= 0.04f; + //float rel_size = MIN(m_display_height, m_display_width) * 0.04f; for (unsigned int i = 0; i < input_buffer->motionEventsCount; ++i) { GameActivityMotionEvent motion_event = input_buffer->motionEvents[i]; if (motion_event.action == AMOTION_EVENT_ACTION_DOWN) { @@ -114,6 +123,29 @@ void NativeEngine::gameLoop() m_last_down.y = y; } } + else if (motion_event.action == AMOTION_EVENT_ACTION_MOVE) { + if (motion_event.pointerCount > 0) { + GameActivityPointerAxes pointer = motion_event.pointers[0]; + float x = GameActivityPointerAxes_getX(&pointer); + float y = GameActivityPointerAxes_getY(&pointer); + + if (!m_in_motion) { + // Record this motion + m_in_motion = true; + m_last_down.x = x; + m_last_down.y = y; + } + + float dist = sqrtf((x - m_last_down.x) * (x - m_last_down.x) + + (y - m_last_down.y) * (y - m_last_down.y)); + if (dist >= rel_size) { + input_events[input_event_count].kind = TouchInputEventKind::InSwipe; + input_events[input_event_count].start = {m_last_down.x, m_last_down.y}; + input_events[input_event_count].end = {x, y}; + ++input_event_count; + } + } + } else if (motion_event.action == AMOTION_EVENT_ACTION_UP) { if (!m_in_motion) { ALOGW("Got an UP motion without a corresponding DOWN motion"); @@ -127,8 +159,7 @@ void NativeEngine::gameLoop() float dist = sqrtf((x - m_last_down.x) * (x - m_last_down.x) + (y - m_last_down.y) * (y - m_last_down.y)); - - if (dist < 5.f) { + if (dist < rel_size) { // TAP ALOGI("TAP at %f %f", x, y); input_events[input_event_count].kind = TouchInputEventKind::Tap; @@ -141,9 +172,9 @@ void NativeEngine::gameLoop() ALOGI("Swipe from %f %f to %f %f", m_last_down.x, m_last_down.y, x, y); - input_events[input_event_count].kind = TouchInputEventKind::Tap; - input_events[input_event_count].start = { m_last_down.x, m_last_down.y }; - input_events[input_event_count].end = { x, y }; + input_events[input_event_count].kind = TouchInputEventKind::EndSwipe; + input_events[input_event_count].start = {m_last_down.x, m_last_down.y}; + input_events[input_event_count].end = {x, y}; ++input_event_count; } m_in_motion = false; @@ -158,6 +189,9 @@ void NativeEngine::gameLoop() android_app_clear_motion_events(input_buffer); } } + else { + m_in_motion = false; + } if (isAnimating() && Renderer::ptr) { // INTEGRATION Rufe hier deine "gameloop"/"update" funktion auf, die als Parameter @@ -173,14 +207,25 @@ void NativeEngine::gameLoop() // float display_width, // float display_height) + /*static bool erster_aufruf = true; + if (erster_aufruf){ + computeWindowWidthHeight(m_display_width, m_display_height); + initKrimiDinner(); + erster_aufruf = false; + } + + krimiDinnerLoop(&m_state, input_events, input_event_count, + m_display_width, m_display_height);*/ + + //ENDE INTEGRATION // BEISPIELCODE - static float t = 1.f; + static float x = 1.f; static float d = -0.01f; - t += d; - if (t <= 0.f) + x += d; + if ( x <= 0.f) d *= -1.f; - else if (t >= 1.f) + else if (x >= 1.f) d *= -1.f; if (input_event_count > 0) { @@ -190,31 +235,7 @@ void NativeEngine::gameLoop() } Renderer::ptr->addRect(100, 100, 500, 500, 0.3f, 0.3f, 0.3f, 1.f); - Renderer::ptr->addRect(m_smiley_pos.x, m_smiley_pos.y, 500, 500, 0.f, t*t, 1.f - t*t, 1.f, m_smiley); - - - float x = 50; - float y = 50; - - y += m_font.baseline_adjust; - const char* text = "Hallo Welt"; - for (const char* c = text; *c != '\0'; ++c) { - if (*c >= 32) { - stbtt_aligned_quad quad; - stbtt_GetBakedQuad(m_font.char_data, 512, 512, (int)*c - 32, &x, &y, &quad, 1); - float w = quad.x1 - quad.x0; - float h = quad.y1 - quad.y0; - - - Renderer::ptr->addFontRect( - x, y, - w, h, - 24.f, - StringRepository::global->internString("Milky Honey.ttf"), - quad.s0, quad.t0, - quad.s1, quad.t1); - } - } + Renderer::ptr->addRect(m_smiley_pos.x, m_smiley_pos.y, 500, 500, 0.f, x*x, 1.f - x*x, 1.f, m_smiley); // ENDE VOM BEISPIELCODE } diff --git a/app/src/main/cpp/NativeEngine.h b/app/src/main/cpp/NativeEngine.h index b87b7b6..c906cfd 100644 --- a/app/src/main/cpp/NativeEngine.h +++ b/app/src/main/cpp/NativeEngine.h @@ -8,8 +8,6 @@ #include "StringRepository.h" #include "GameState.h" -#include "Font.h" - #include #include @@ -63,9 +61,6 @@ private: StringHandle m_smiley; Position m_smiley_pos; - StringHandle m_font_bitmap; - FontData m_font; - Position m_last_down; bool m_in_motion; diff --git a/app/src/main/cpp/Renderer.cpp b/app/src/main/cpp/Renderer.cpp index 8247028..d34ff8b 100644 --- a/app/src/main/cpp/Renderer.cpp +++ b/app/src/main/cpp/Renderer.cpp @@ -1,6 +1,7 @@ #include "Renderer.h" #include "Log.h" #include "AssetManager.h" +#include "Texture.h" #include @@ -229,12 +230,13 @@ void Renderer::addFontRect(float x, float h, float char_height, StringHandle font, + StringHandle id, float src_x0, float src_y0, float src_x1, float src_y1) { - addFontRect(x, y, w, h, 0.f, 0.f, 0.f, 1.f, char_height, font, src_x0, src_y0, src_x1, src_y1); + addFontRect(x, y, w, h, 0.f, 0.f, 0.f, 1.f, char_height, font, id, src_x0, src_y0, src_x1, src_y1); } void Renderer::addFontRect(float x, @@ -247,18 +249,19 @@ void Renderer::addFontRect(float x, float a, float char_height, StringHandle font, + StringHandle id, float src_x0, float src_y0, float src_x1, float src_y1) { - if (m_textures.find(font) == m_textures.end()) { + if (m_textures.find(id) == m_textures.end()) { Texture tex; if (!AssetManager::ptr->loadFontBitmap(StringRepository::global->getString(font), char_height, &tex)) { ALOGE("Failed to load texture"); return; } - m_textures.insert(std::make_pair(font, tex)); + m_textures.insert(std::make_pair(id, tex)); } Rect rect = {}; @@ -276,7 +279,7 @@ void Renderer::addFontRect(float x, rect.a = a; rect.expand_r = 1; m_rects.push_back(rect); - m_draw_textures.push_back(font); + m_draw_textures.push_back(id); } void Renderer::renderFrame(float width, float height) @@ -284,7 +287,8 @@ void Renderer::renderFrame(float width, float height) assert(m_rects.size() == m_draw_textures.size()); glViewport(0, 0, static_cast(width), static_cast(height)); - glClearColor(0.8f, 0.3f, 0.3f, 1.f); + //glClearColor(0.8f, 0.3f, 0.3f, 1.f); + glClearColor(0.0f, 0.0f, 0.0f, 1.f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); const auto rect_count = static_cast(m_rects.size()); @@ -340,4 +344,21 @@ void Renderer::renderFrame(float width, float height) } m_rects.clear(); m_draw_textures.clear(); +} + +void Renderer::getTextureSize( + StringHandle texture, + unsigned int* w, + unsigned int* h) +{ + if (m_textures.find(texture) == m_textures.end()) { + Texture tex; + if (!AssetManager::ptr->loadTexture(StringRepository::global->getString(texture), &tex)) { + ALOGE("Failed to load texture"); + return; + } + m_textures.insert(std::make_pair(texture, tex)); + } + Texture tex = m_textures[texture]; + tex.getTextureSize(w, h); } \ No newline at end of file diff --git a/app/src/main/cpp/Renderer.h b/app/src/main/cpp/Renderer.h index ee81378..7a16d58 100644 --- a/app/src/main/cpp/Renderer.h +++ b/app/src/main/cpp/Renderer.h @@ -131,6 +131,7 @@ public: float h, float char_height, StringHandle font, + StringHandle id, float src_x0, float src_y0, float src_x1, @@ -161,6 +162,7 @@ public: float a, float char_height, StringHandle font, + StringHandle id, float src_x0, float src_y0, float src_x1, @@ -173,6 +175,14 @@ public: /// @param height height of the screen void renderFrame(float width, float height); + /// @brief loads texture (if not loaded) to get texture size + /// \param w output width in pixels + /// \param h output height in pixels + void getTextureSize( + StringHandle texture, + unsigned int* w, + unsigned int* h); + private: Renderer(); ~Renderer(); diff --git a/app/src/main/cpp/Texture.cpp b/app/src/main/cpp/Texture.cpp index 9cf6fe4..e9e2001 100644 --- a/app/src/main/cpp/Texture.cpp +++ b/app/src/main/cpp/Texture.cpp @@ -21,6 +21,8 @@ Texture::Texture(unsigned int width, unsigned int height, const void *data) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); + width_px = width; + height_px = height; } Texture::Texture(unsigned int width, unsigned int height, const void* data, GLint format) @@ -44,6 +46,8 @@ Texture::Texture(unsigned int width, unsigned int height, const void* data, GLin glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); + width_px = width; + height_px = height; } void Texture::destroy() @@ -54,4 +58,15 @@ void Texture::destroy() void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, m_texture); +} + +void Texture::getTextureSize( + unsigned int* w, + unsigned int* h){ + if (w != NULL){ + *w = width_px; + } + if (h != NULL){ + *h = height_px; + } } \ No newline at end of file diff --git a/app/src/main/cpp/Texture.h b/app/src/main/cpp/Texture.h index ec5a205..2e02886 100644 --- a/app/src/main/cpp/Texture.h +++ b/app/src/main/cpp/Texture.h @@ -30,8 +30,18 @@ public: /// @brief Binds the texture to @c GL_TEXTURE_2D void bind() const; + /// @brief get texture size + /// \param w output width in pixels + /// \param h output height in pixels + void getTextureSize( + unsigned int* w, + unsigned int* h); + private: GLuint m_texture; + + int width_px; + int height_px; }; #endif diff --git a/app/src/main/cpp/TouchInput.h b/app/src/main/cpp/TouchInput.h index 35f9397..7b2f85d 100644 --- a/app/src/main/cpp/TouchInput.h +++ b/app/src/main/cpp/TouchInput.h @@ -13,7 +13,11 @@ enum class TouchInputEventKind Tap, /// The user "swiped" from a start position to an end position - Swipe + EndSwipe, + + /// + InSwipe + /// The user "swiped" from a start position to an end position, but didn't stop yet }; /// @brief Describes a touch input event