M .gitignore => .gitignore +2 -0
@@ 1,6 1,8 @@
lib
build
+test/generated
+
# cmake
CMakeCache.txt
CMakeFiles
M CMakeLists.txt => CMakeLists.txt +11 -0
@@ 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}
+)
M src/zge/contrib/cube.cpp => src/zge/contrib/cube.cpp +4 -0
@@ 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
M src/zge/core/display.cpp => src/zge/core/display.cpp +6 -11
@@ 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());
M src/zge/core/run_loop.cpp => src/zge/core/run_loop.cpp +18 -3
@@ 11,10 11,15 @@
#include <thread>
#include <SDL2/SDL.h>
+#if __has_include(<pthread.h>)
+# include <pthread.h>
+# include <sched.h>
+# define HAS_PTHREADS 1
+#endif
+
#if __APPLE__
#include <mach/mach.h>
#include <mach/mach_time.h>
-#include <pthread.h>
#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<ZSchedulableRef> 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
}
M src/zge/graph/node.cpp => src/zge/graph/node.cpp +8 -8
@@ 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<ZLightRef> lights;
- for (ZNodeRef child : _children) {
+ for (const ZNodeRef &child : _children) {
ZLightRef light = std::dynamic_pointer_cast<ZLight>(child);
if (light) {
lights.push_back(light);
M src/zge/text/font.cpp => src/zge/text/font.cpp +27 -12
@@ 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
M src/zge/text/font.h => src/zge/text/font.h +3 -5
@@ 9,21 9,19 @@
#include <zge/core/foundation.h>
#include <zge/text/glyph.h>
+#include <zge/util/data.h>
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;
M test/src/fps_node.cpp => test/src/fps_node.cpp +2 -3
@@ 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);
A test/src/monaco.cpp => test/src/monaco.cpp +13 -0
@@ 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)
+{}
A test/src/monaco.h => test/src/monaco.h +13 -0
@@ 0,0 1,13 @@
+#pragma once
+
+#include <zge/zge.h>
+
+class Monaco : public zge::ZFont {
+public:
+ Monaco(float size);
+ ~Monaco() = default;
+
+ ZGE_DEFINE_SREF_FUNCTIONS(Monaco);
+};
+
+ZGE_DEFINE_SREF_TYPE(Monaco);
M test/src/multiscene.cpp => test/src/multiscene.cpp +3 -4
@@ 7,6 7,7 @@
//
#include "multiscene.h"
+#include "monaco.h"
#include <cmath>
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;
M test/src/persp_cube_scene.cpp => test/src/persp_cube_scene.cpp +28 -4
@@ 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<Z3DCamera>(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<Z3DCamera>(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;
}
}
}
M test/src/text_scene.cpp => test/src/text_scene.cpp +2 -3
@@ 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);
}