Fonts laden korrigiert

This commit is contained in:
Ronja 2022-10-27 15:38:24 +02:00
parent 69d35b5d15
commit 70d23facf9
10 changed files with 130 additions and 69 deletions

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="R52N60RNQLZ" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2022-10-24T10:55:25.742010800Z" />
</component>
</project>

View File

@ -34,14 +34,14 @@ bool AssetManager::loadFontData(const char* path, float char_height, FontData* p
if (!loadFile(path, &ttf_data)) if (!loadFile(path, &ttf_data))
return false; return false;
static unsigned char bitmap_memory[512 * 512];; static unsigned char bitmap_memory[STBTT_PH_VALUE * STBTT_PH_VALUE];
FontData font_data = {}; FontData font_data = {};
auto* data = reinterpret_cast<const unsigned char*>(ttf_data.data); auto* data = reinterpret_cast<const unsigned char*>(ttf_data.data);
stbtt_BakeFontBitmap(data, stbtt_BakeFontBitmap(data,
0, 0,
char_height, char_height,
bitmap_memory, bitmap_memory,
512, 512, STBTT_PH_VALUE, STBTT_PH_VALUE,
32, 225, // latin-1 printable characters range 32, 225, // latin-1 printable characters range
font_data.char_data); font_data.char_data);
font_data.char_height = char_height; font_data.char_height = char_height;
@ -74,16 +74,16 @@ bool AssetManager::loadFontBitmap(const char *path, float char_height, Texture*
FileBuffer ttf_data = {}; FileBuffer ttf_data = {};
if (!loadFile(path, &ttf_data)) if (!loadFile(path, &ttf_data))
return false; 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_bakedchar char_data[225];
stbtt_BakeFontBitmap(reinterpret_cast<const unsigned char*>(ttf_data.data), stbtt_BakeFontBitmap(reinterpret_cast<const unsigned char*>(ttf_data.data),
0, 0,
char_height, char_height,
bitmap_memory, bitmap_memory,
512, 512, STBTT_PH_VALUE, STBTT_PH_VALUE,
32, 225, // latin-1 printable characters range 32, 225, // latin-1 printable characters range
char_data); 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); releaseFileBuffer(ttf_data);
return true; return true;
} }

View File

@ -63,4 +63,6 @@ protected:
AssetManager& operator=(const AssetManager&) = delete; AssetManager& operator=(const AssetManager&) = delete;
}; };
const int STBTT_PH_VALUE = 1024;
#endif #endif

View File

@ -7,6 +7,8 @@
#include "AndroidAssetManager.h" #include "AndroidAssetManager.h"
#include "Renderer.h" #include "Renderer.h"
#include "TouchInput.h" #include "TouchInput.h"
/*#include "Game.h"
#include "base.h"*/
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
@ -48,10 +50,11 @@ NativeEngine::NativeEngine(struct android_app *app)
// //
// Du kannst schon Assets laden, da oben der AssetManager erzeugt wurde. // Du kannst schon Assets laden, da oben der AssetManager erzeugt wurde.
// Ende INTEGRATION
// BEISPIELCODE // BEISPIELCODE
m_smiley = StringRepository::global->internString("smiley_PNG42.png"); m_smiley = StringRepository::global->internString("smiley_PNG42.png");
m_smiley_pos = { 100, 100 }; //m_smiley_pos = { 100, 100 };
AssetManager::ptr->loadFontData("Milky Honey.ttf", 24.f, &m_font);
// ENDE VOM BEISPIELCODE // ENDE VOM BEISPIELCODE
} }
@ -100,6 +103,12 @@ void NativeEngine::gameLoop()
android_app_clear_key_events(input_buffer); android_app_clear_key_events(input_buffer);
} }
if (input_buffer->motionEventsCount > 0) { 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) { for (unsigned int i = 0; i < input_buffer->motionEventsCount; ++i) {
GameActivityMotionEvent motion_event = input_buffer->motionEvents[i]; GameActivityMotionEvent motion_event = input_buffer->motionEvents[i];
if (motion_event.action == AMOTION_EVENT_ACTION_DOWN) { if (motion_event.action == AMOTION_EVENT_ACTION_DOWN) {
@ -114,6 +123,29 @@ void NativeEngine::gameLoop()
m_last_down.y = y; 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) { else if (motion_event.action == AMOTION_EVENT_ACTION_UP) {
if (!m_in_motion) { if (!m_in_motion) {
ALOGW("Got an UP motion without a corresponding DOWN 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) + float dist = sqrtf((x - m_last_down.x) * (x - m_last_down.x) +
(y - m_last_down.y) * (y - m_last_down.y)); (y - m_last_down.y) * (y - m_last_down.y));
if (dist < rel_size) {
if (dist < 5.f) {
// TAP // TAP
ALOGI("TAP at %f %f", x, y); ALOGI("TAP at %f %f", x, y);
input_events[input_event_count].kind = TouchInputEventKind::Tap; input_events[input_event_count].kind = TouchInputEventKind::Tap;
@ -141,9 +172,9 @@ void NativeEngine::gameLoop()
ALOGI("Swipe from %f %f to %f %f", ALOGI("Swipe from %f %f to %f %f",
m_last_down.x, m_last_down.y, m_last_down.x, m_last_down.y,
x, y); x, y);
input_events[input_event_count].kind = TouchInputEventKind::Tap; 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].start = {m_last_down.x, m_last_down.y};
input_events[input_event_count].end = { x, y }; input_events[input_event_count].end = {x, y};
++input_event_count; ++input_event_count;
} }
m_in_motion = false; m_in_motion = false;
@ -158,6 +189,9 @@ void NativeEngine::gameLoop()
android_app_clear_motion_events(input_buffer); android_app_clear_motion_events(input_buffer);
} }
} }
else {
m_in_motion = false;
}
if (isAnimating() && Renderer::ptr) { if (isAnimating() && Renderer::ptr) {
// INTEGRATION Rufe hier deine "gameloop"/"update" funktion auf, die als Parameter // INTEGRATION Rufe hier deine "gameloop"/"update" funktion auf, die als Parameter
@ -173,14 +207,25 @@ void NativeEngine::gameLoop()
// float display_width, // float display_width,
// float display_height) // 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 // BEISPIELCODE
static float t = 1.f; static float x = 1.f;
static float d = -0.01f; static float d = -0.01f;
t += d; x += d;
if (t <= 0.f) if ( x <= 0.f)
d *= -1.f; d *= -1.f;
else if (t >= 1.f) else if (x >= 1.f)
d *= -1.f; d *= -1.f;
if (input_event_count > 0) { 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(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); 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);
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);
}
}
// ENDE VOM BEISPIELCODE // ENDE VOM BEISPIELCODE
} }

View File

@ -8,8 +8,6 @@
#include "StringRepository.h" #include "StringRepository.h"
#include "GameState.h" #include "GameState.h"
#include "Font.h"
#include <game-activity/native_app_glue/android_native_app_glue.h> #include <game-activity/native_app_glue/android_native_app_glue.h>
#include <EGL/egl.h> #include <EGL/egl.h>
@ -63,9 +61,6 @@ private:
StringHandle m_smiley; StringHandle m_smiley;
Position m_smiley_pos; Position m_smiley_pos;
StringHandle m_font_bitmap;
FontData m_font;
Position m_last_down; Position m_last_down;
bool m_in_motion; bool m_in_motion;

View File

@ -1,6 +1,7 @@
#include "Renderer.h" #include "Renderer.h"
#include "Log.h" #include "Log.h"
#include "AssetManager.h" #include "AssetManager.h"
#include "Texture.h"
#include <assert.h> #include <assert.h>
@ -229,12 +230,13 @@ void Renderer::addFontRect(float x,
float h, float h,
float char_height, float char_height,
StringHandle font, StringHandle font,
StringHandle id,
float src_x0, float src_x0,
float src_y0, float src_y0,
float src_x1, float src_x1,
float src_y1) 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, void Renderer::addFontRect(float x,
@ -247,18 +249,19 @@ void Renderer::addFontRect(float x,
float a, float a,
float char_height, float char_height,
StringHandle font, StringHandle font,
StringHandle id,
float src_x0, float src_x0,
float src_y0, float src_y0,
float src_x1, float src_x1,
float src_y1) float src_y1)
{ {
if (m_textures.find(font) == m_textures.end()) { if (m_textures.find(id) == m_textures.end()) {
Texture tex; Texture tex;
if (!AssetManager::ptr->loadFontBitmap(StringRepository::global->getString(font), char_height, &tex)) { if (!AssetManager::ptr->loadFontBitmap(StringRepository::global->getString(font), char_height, &tex)) {
ALOGE("Failed to load texture"); ALOGE("Failed to load texture");
return; return;
} }
m_textures.insert(std::make_pair(font, tex)); m_textures.insert(std::make_pair(id, tex));
} }
Rect rect = {}; Rect rect = {};
@ -276,7 +279,7 @@ void Renderer::addFontRect(float x,
rect.a = a; rect.a = a;
rect.expand_r = 1; rect.expand_r = 1;
m_rects.push_back(rect); m_rects.push_back(rect);
m_draw_textures.push_back(font); m_draw_textures.push_back(id);
} }
void Renderer::renderFrame(float width, float height) 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()); assert(m_rects.size() == m_draw_textures.size());
glViewport(0, 0, static_cast<int>(width), static_cast<int>(height)); glViewport(0, 0, static_cast<int>(width), static_cast<int>(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); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const auto rect_count = static_cast<GLsizei>(m_rects.size()); const auto rect_count = static_cast<GLsizei>(m_rects.size());
@ -341,3 +345,20 @@ void Renderer::renderFrame(float width, float height)
m_rects.clear(); m_rects.clear();
m_draw_textures.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);
}

View File

@ -131,6 +131,7 @@ public:
float h, float h,
float char_height, float char_height,
StringHandle font, StringHandle font,
StringHandle id,
float src_x0, float src_x0,
float src_y0, float src_y0,
float src_x1, float src_x1,
@ -161,6 +162,7 @@ public:
float a, float a,
float char_height, float char_height,
StringHandle font, StringHandle font,
StringHandle id,
float src_x0, float src_x0,
float src_y0, float src_y0,
float src_x1, float src_x1,
@ -173,6 +175,14 @@ public:
/// @param height height of the screen /// @param height height of the screen
void renderFrame(float width, float height); 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: private:
Renderer(); Renderer();
~Renderer(); ~Renderer();

View File

@ -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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
width_px = width;
height_px = height;
} }
Texture::Texture(unsigned int width, unsigned int height, const void* data, GLint format) 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
width_px = width;
height_px = height;
} }
void Texture::destroy() void Texture::destroy()
@ -55,3 +59,14 @@ void Texture::bind() const
{ {
glBindTexture(GL_TEXTURE_2D, m_texture); 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;
}
}

View File

@ -30,8 +30,18 @@ public:
/// @brief Binds the texture to @c GL_TEXTURE_2D /// @brief Binds the texture to @c GL_TEXTURE_2D
void bind() const; 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: private:
GLuint m_texture; GLuint m_texture;
int width_px;
int height_px;
}; };
#endif #endif

View File

@ -13,7 +13,11 @@ enum class TouchInputEventKind
Tap, Tap,
/// The user "swiped" from a start position to an end position /// 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 /// @brief Describes a touch input event