From 7614ce890487e85151e2386e6ccfb38f505212ca Mon Sep 17 00:00:00 2001 From: Charles Magahern Date: Sun, 30 May 2021 00:42:54 -0700 Subject: [PATCH] Random improvements --- .gitignore | 2 ++ CMakeLists.txt | 11 ++++++++++ src/zge/contrib/cube.cpp | 4 ++++ src/zge/core/display.cpp | 17 ++++++--------- src/zge/core/run_loop.cpp | 21 ++++++++++++++++--- src/zge/graph/node.cpp | 16 +++++++------- src/zge/text/font.cpp | 39 ++++++++++++++++++++++++----------- src/zge/text/font.h | 8 +++---- test/src/fps_node.cpp | 5 ++--- test/src/monaco.cpp | 13 ++++++++++++ test/src/monaco.h | 13 ++++++++++++ test/src/multiscene.cpp | 7 +++---- test/src/persp_cube_scene.cpp | 32 ++++++++++++++++++++++++---- test/src/text_scene.cpp | 5 ++--- 14 files changed, 140 insertions(+), 53 deletions(-) create mode 100644 test/src/monaco.cpp create mode 100644 test/src/monaco.h diff --git a/.gitignore b/.gitignore index a4cfc22..441fc5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ lib build +test/generated + # cmake CMakeCache.txt CMakeFiles diff --git a/CMakeLists.txt b/CMakeLists.txt index ddb7f6f..c0664a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,3 +78,14 @@ file(GLOB_RECURSE ZGETEST_HEADERS "test/src/*.h") add_executable(zgetest ${ZGETEST_SOURCES} ${ZGETEST_HEADERS}) target_link_libraries(zgetest PRIVATE zge) + +find_program(RESOURCE_COMPILER xxd) +if (NOT RESOURCE_COMPILER) + message (FATAL_ERROR "Requires xxd command from vim-common") +endif() + +# convert font file into includable header +execute_process( + COMMAND ${RESOURCE_COMPILER} -i test/resources/Monaco.dfont test/generated/Monaco.dfont.h + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} +) diff --git a/src/zge/contrib/cube.cpp b/src/zge/contrib/cube.cpp index 4c96f1d..022a695 100644 --- a/src/zge/contrib/cube.cpp +++ b/src/zge/contrib/cube.cpp @@ -172,8 +172,12 @@ void ZCube::render(ZRenderContextRef context) { ZGeometry::render(context); + context->set_depth_testing_enabled(true); + size_t num_triangles = sizeof(__cube_vertex_data) / sizeof(__cube_vertex_data[0]) / 3; context->draw_array(ZRENDER_MODE_TRIANGLES, _vertex_array, 0, num_triangles); + + context->set_depth_testing_enabled(false); } ZGE_END_NAMESPACE diff --git a/src/zge/core/display.cpp b/src/zge/core/display.cpp index 67bdc7a..6facd78 100644 --- a/src/zge/core/display.cpp +++ b/src/zge/core/display.cpp @@ -23,8 +23,8 @@ struct ZDisplayImpl { ZDisplay::ZDisplay(const ZDisplayMode &mode) : _impl(new ZDisplayImpl), - _display_mode(mode), _initialized(false), + _display_mode(mode), _captures_input(false) {} @@ -99,24 +99,19 @@ void ZDisplay::_init_window() SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); #endif - int width = 0; - int height = 0; - if (_display_mode.windowed) { - width = _display_mode.width; - height = _display_mode.height; - } else { + if (!_display_mode.windowed) { SDL_DisplayMode sdl_displaymode; SDL_GetDesktopDisplayMode(0, &sdl_displaymode); - width = sdl_displaymode.w; - height = sdl_displaymode.h; + _display_mode.width = sdl_displaymode.w; + _display_mode.height = sdl_displaymode.h; } // create the window _impl->window = SDL_CreateWindow(_display_mode.window_title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - width, - height, + (int)_display_mode.width, + (int)_display_mode.height, sdlflags); if (_impl->window == nullptr) { ZLogger::log_error("Could not create SDL window. %s", SDL_GetError()); diff --git a/src/zge/core/run_loop.cpp b/src/zge/core/run_loop.cpp index 1758390..3aaf67f 100644 --- a/src/zge/core/run_loop.cpp +++ b/src/zge/core/run_loop.cpp @@ -11,10 +11,15 @@ #include #include +#if __has_include() +# include +# include +# define HAS_PTHREADS 1 +#endif + #if __APPLE__ #include #include -#include #endif #define FRAMES_PER_SECOND 60 @@ -81,7 +86,10 @@ void ZRunloop::_main() const ZTimeInterval fps_interval(1.0 / (double)FRAMES_PER_SECOND); const std::vector schedulables = _schedulables; - _configure_render_thread(); + bool rtconfig_success = _configure_render_thread(); + if (!rtconfig_success) { + zlog("warning: failed to configure priority for render thread."); + } while (_running) { ZTime loop_start_time = ZUtil::get_current_time(); @@ -132,8 +140,15 @@ static bool _configure_render_thread(void) (thread_policy_t)&policy, THREAD_TIME_CONSTRAINT_POLICY_COUNT); return (policy_set_result == 0); +#elif HAS_PTHREADS + pthread_t t = pthread_self(); + struct sched_param params = { + .sched_priority = sched_get_priority_max(SCHED_FIFO) + }; + int rv = pthread_setschedparam(t, SCHED_FIFO, ¶ms); + + return (rv == 0); #else - // TODO: Configure render thread for other platforms. return false; #endif } diff --git a/src/zge/graph/node.cpp b/src/zge/graph/node.cpp index 10ce570..80fa740 100644 --- a/src/zge/graph/node.cpp +++ b/src/zge/graph/node.cpp @@ -160,13 +160,13 @@ void ZNode::render(ZRenderContextRef context) ZRect ZNode::get_bounds() const { - ZVector position = get_position(); + const ZVector &position = get_position(); return ZRect({position.get_x(), position.get_y()}, {0.0, 0.0}); } void ZNode::set_bounds(const ZRect &bounds) { - ZVector position = bounds.origin; + const ZVector &position = bounds.origin; set_position(position); } @@ -208,7 +208,7 @@ void ZNode::_draw(ZRenderContextRef context) void ZNode::_update_internal() { // update children - for (ZNodeRef child : _children) { + for (const ZNodeRef &child : _children) { child->_update_internal(); } @@ -217,7 +217,7 @@ void ZNode::_update_internal() // step actions ZNodeRef selfptr = shared_from_this(); - for (ZActionRef action : _actions) { + for (const ZActionRef &action : _actions) { if (!action->is_finished()) { action->step(selfptr); } @@ -239,7 +239,7 @@ void ZNode::_handle_input_event_internal(const ZEvent &event) handle_input_event(event); // send event to children - for (ZNodeRef child : _children) { + for (const ZNodeRef &child : _children) { child->_handle_input_event_internal(event); } } @@ -269,7 +269,7 @@ void ZNode::_on_enter_internal() { on_enter(); - for (ZNodeRef child : _children) { + for (const ZNodeRef &child : _children) { child->_on_enter_internal(); } } @@ -278,7 +278,7 @@ void ZNode::_on_exit_internal() { on_exit(); - for (ZNodeRef child : _children) { + for (const ZNodeRef &child : _children) { child->_on_exit_internal(); } } @@ -293,7 +293,7 @@ void ZNode::_prepare_camera(ZRenderContextRef context) void ZNode::_prepare_lights(ZRenderContextRef context) { std::vector lights; - for (ZNodeRef child : _children) { + for (const ZNodeRef &child : _children) { ZLightRef light = std::dynamic_pointer_cast(child); if (light) { lights.push_back(light); diff --git a/src/zge/text/font.cpp b/src/zge/text/font.cpp index bdc7108..96fb97a 100644 --- a/src/zge/text/font.cpp +++ b/src/zge/text/font.cpp @@ -28,18 +28,14 @@ private: }; struct _ZFontImpl { - std::string path; - float size; - FT_Face face; + ZDataConstRef font_data; + float size; + FT_Face face; }; ZFont::ZFont(const std::string &font_path, float size) : _impl(new _ZFontImpl) { - _impl->path = font_path; - _impl->size = size; - _impl->face = nullptr; - ZFontLibrary *library = ZFontLibrary::instance(); FT_Library *freetype = library->get_freetype_library(); @@ -53,11 +49,32 @@ ZFont::ZFont(const std::string &font_path, float size) : FT_Set_Char_Size(face, size * 64, size * 64, 96, 96); _impl->face = face; + _impl->size = size; } -ZFont::ZFont(const ZFont &cp) : - ZFont(cp.get_font_path(), cp.get_font_size()) -{} +ZFont::ZFont(ZDataConstRef font_data, float size) : + _impl(new _ZFontImpl) +{ + ZFontLibrary *library = ZFontLibrary::instance(); + FT_Library *freetype = library->get_freetype_library(); + + FT_Face face; + FT_Error err = FT_New_Memory_Face(*freetype, + (const FT_Byte *)font_data->get_bytes(), + font_data->get_length(), + 0, + &face); + if (err != 0) { + ZException ex(ZFILE_EXCEPTION_CODE); + ex.extra_info = "Failed to load font data."; + throw ex; + } + + FT_Set_Char_Size(face, size * 64, size * 64, 96, 96); + _impl->font_data = font_data; + _impl->size = size; + _impl->face = face; +} ZFont::ZFont(ZFont &&mv) : _impl(mv._impl.release()) @@ -72,8 +89,6 @@ ZFont::~ZFont() #pragma mark - Accessors -std::string ZFont::get_font_path() const { return _impl->path; } - float ZFont::get_font_size() const { return _impl->size; } float ZFont::get_line_height() const diff --git a/src/zge/text/font.h b/src/zge/text/font.h index b13774f..bd65c2c 100644 --- a/src/zge/text/font.h +++ b/src/zge/text/font.h @@ -9,21 +9,19 @@ #include #include +#include ZGE_BEGIN_NAMESPACE -class ZFont { +class ZFont : public ZNoncopyable { public: ZFont(const std::string &font_path, float size = 16.f); - ZFont(const ZFont &cp); + ZFont(ZDataConstRef font_data, float size = 16.f); ZFont(ZFont &&mv); ~ZFont(); ZGE_DEFINE_SREF_FUNCTIONS(ZFont); - /// path to the font file - std::string get_font_path() const; - /// font size in points float get_font_size() const; diff --git a/test/src/fps_node.cpp b/test/src/fps_node.cpp index b70cdc9..5b9ec3c 100644 --- a/test/src/fps_node.cpp +++ b/test/src/fps_node.cpp @@ -7,6 +7,7 @@ // #include "fps_node.h" +#include "monaco.h" FPSNode::FPSNode(zge::ZEngineRef engine) : _engine(engine), @@ -17,9 +18,7 @@ FPSNode::FPSNode(zge::ZEngineRef engine) : _timer->set_interval(zge::ZTimeInterval(0.5)); engine->get_application()->get_main_runloop()->schedule(_timer); -// zge::ZFontRef font = zge::ZFont::create("/System/Library/Fonts/Monaco.dfont", 32); - zge::ZFontRef font = zge::ZFont::create("/usr/share/fonts/truetype/inconsolata/Inconsolata.otf", 32); - _text_node->set_font(font); + _text_node->set_font(Monaco::create(32)); _text_node->set_text_color(zge::ZColor::white); this->add_child(_text_node); diff --git a/test/src/monaco.cpp b/test/src/monaco.cpp new file mode 100644 index 0000000..f273b53 --- /dev/null +++ b/test/src/monaco.cpp @@ -0,0 +1,13 @@ +#include "monaco.h" +#include "../generated/Monaco.dfont.h" + +static zge::ZDataConstRef monaco_font_data(void) +{ + return zge::ZData::create(test_resources_Monaco_dfont, + test_resources_Monaco_dfont_len, + false); +} + +Monaco::Monaco(float size) : + zge::ZFont(monaco_font_data(), size) +{} diff --git a/test/src/monaco.h b/test/src/monaco.h new file mode 100644 index 0000000..9f4bac0 --- /dev/null +++ b/test/src/monaco.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +class Monaco : public zge::ZFont { +public: + Monaco(float size); + ~Monaco() = default; + + ZGE_DEFINE_SREF_FUNCTIONS(Monaco); +}; + +ZGE_DEFINE_SREF_TYPE(Monaco); diff --git a/test/src/multiscene.cpp b/test/src/multiscene.cpp index fc93c1b..55bdf62 100644 --- a/test/src/multiscene.cpp +++ b/test/src/multiscene.cpp @@ -7,6 +7,7 @@ // #include "multiscene.h" +#include "monaco.h" #include using namespace zge; @@ -92,15 +93,13 @@ void Multiscene::_setup_picker_panel() ZSpriteNodeRef picker = ZSpriteNode::create(); picker->set_color(ZColor(0.2, 0.2, 0.2, 0.2)); picker->set_position(ZVector::zero); - picker->set_size({_viewport.size.width / 7.0f, _viewport.size.height}); + picker->set_size({_viewport.size.width / 6.0f, _viewport.size.height}); ZOrthoCameraRef camera = ZOrthoCamera::create(); camera->set_clipping_rect({0.0, 0.0, _viewport.size.width, _viewport.size.height}); picker->set_camera(camera); - const ZResourceBundle *bundle = ZResourceBundle::get_main_bundle(); -// ZFontRef font = ZFont::create(bundle->get_path_for_resource("Monaco.dfont"), 9.5); - ZFontRef font = ZFont::create("/usr/share/fonts/truetype/inconsolata/Inconsolata.otf", 9.5); + ZFontRef font = Monaco::create(14); const float vpadding = 5.0f; const float cellheight = 20.0f; diff --git a/test/src/persp_cube_scene.cpp b/test/src/persp_cube_scene.cpp index 9c558ee..ae8a94c 100644 --- a/test/src/persp_cube_scene.cpp +++ b/test/src/persp_cube_scene.cpp @@ -20,7 +20,7 @@ PerspectiveCubeScene::PerspectiveCubeScene(const ZRect &viewport) : // setup camera Z3DCameraRef camera = Z3DCameraRef(new Z3DCamera); camera->set_position({0.0, 0.0, 5.0}); - camera->set_look({0.0, 0.0, 0.0}); + camera->set_look({0.0, 0.0, -999.0}); camera->set_viewport_rect(_viewport); set_camera(camera); @@ -53,6 +53,9 @@ PerspectiveCubeScene::PerspectiveCubeScene(const ZRect &viewport) : void PerspectiveCubeScene::handle_input_event(const ZEvent &event) { + Z3DCameraRef camera = std::dynamic_pointer_cast(get_camera()); + ZVector pos = camera->get_position(); + if (event.type == ZMOUSE_MOVED_EVENT) { float tx = 0.0; float ty = 0.0; @@ -63,14 +66,35 @@ void PerspectiveCubeScene::handle_input_event(const ZEvent &event) tx = mouse_event.location.x - midx; ty = mouse_event.location.y - midy; - ZVector look = {tx / midx, -ty / midy, 0.0}; + ZVector look = {tx / midx, -ty / midy, -pos.z()}; Z3DCameraRef camera = std::dynamic_pointer_cast(get_camera()); camera->set_look(look); } else if (event.type == ZKEY_DOWN_EVENT) { - if (event.key_event.key == ZKEY_SPACE) { + switch (event.key_event.key) { + case ZKEY_SPACE: _cube1_material->set_color(ZColor::random_color()); - } else if (event.key_event.key == ZKEY_BACKSPACE) { + break; + case ZKEY_BACKSPACE: _cube2->remove_from_parent(); + break; + case ZKEY_UP: + pos.z() -= 0.1; + camera->set_position(pos); + break; + case ZKEY_DOWN: + pos.z() += 0.1; + camera->set_position(pos); + break; + case ZKEY_LEFT: + pos.x() -= 0.1; + camera->set_position(pos); + break; + case ZKEY_RIGHT: + pos.x() += 0.1; + camera->set_position(pos); + break; + default: + break; } } } diff --git a/test/src/text_scene.cpp b/test/src/text_scene.cpp index a0197c8..7b96095 100644 --- a/test/src/text_scene.cpp +++ b/test/src/text_scene.cpp @@ -7,6 +7,7 @@ // #include "text_scene.h" +#include "monaco.h" using namespace zge; @@ -24,9 +25,7 @@ TextScene::TextScene(const ZRect &viewport) : square->set_position({100.0, 250.0}); _scene->add_child(square); -// ZFontRef font = ZFont::create("/System/Library/Fonts/Monaco.dfont", 64); - ZFontRef font = ZFont::create("/usr/share/fonts/truetype/inconsolata/Inconsolata.otf", 64); - ZTextNodeRef text_node = ZTextNode::create("cool square!", font); + ZTextNodeRef text_node = ZTextNode::create("cool square!", Monaco::create(64)); text_node->set_position({100.0, 100.0}); _scene->add_child(text_node); } -- 2.45.2