-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
cmake_minimum_required(VERSION 3.13) | ||
project(Hello2D) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
if (NOT CMAKE_BUILD_TYPE) | ||
set(CMAKE_BUILD_TYPE "Release") | ||
endif () | ||
|
||
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR $CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64") | ||
set(ARCH x64) | ||
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64") | ||
set(ARCH arm64) | ||
else () | ||
set(ARCH x86) | ||
endif () | ||
|
||
file(GLOB SWIFTSHADER_LIBS ../vendor/swiftshader/mac/${ARCH}/*.${CMAKE_SHARED_LIBRARY_SUFFIX}) | ||
list(APPEND HELLO_2D_LIBS ${SWIFTSHADER_LIBS}) | ||
list(APPEND HELLO_2D_INCLUDES ../vendor/swiftshader/include) | ||
|
||
if (APPLE) | ||
find_library(APPLICATION_SERVICES_FRAMEWORK ApplicationServices REQUIRED) | ||
list(APPEND HELLO_2D_LIBS ${APPLICATION_SERVICES_FRAMEWORK}) | ||
find_library(QUARTZ_CORE QuartzCore REQUIRED) | ||
list(APPEND HELLO_2D_LIBS ${QUARTZ_CORE}) | ||
find_library(COCOA Cocoa REQUIRED) | ||
list(APPEND HELLO_2D_LIBS ${COCOA}) | ||
find_library(FOUNDATION Foundation REQUIRED) | ||
list(APPEND HELLO_2D_LIBS ${FOUNDATION}) | ||
find_library(ICONV_LIBRARIES NAMES iconv libiconv libiconv-2 c) | ||
list(APPEND HELLO_2D_LIBS ${ICONV_LIBRARIES}) | ||
find_library(CORE_MEDIA CoreMedia) | ||
list(APPEND HELLO_2D_LIBS ${CORE_MEDIA}) | ||
else () | ||
find_package(Threads) | ||
list(APPEND HELLO_2D_LIBS ${CMAKE_THREAD_LIBS_INIT}) | ||
list(APPEND HELLO_2D_LIBS dl) | ||
list(APPEND HELLO_2D_COMPILE_OPTIONS -fPIC -pthread) | ||
endif () | ||
|
||
set(TGFX_BUILD_DRAWERS ON) | ||
set(TGFX_USE_SWIFTSHADER ON) | ||
set(TGFX_USE_FREETYPE ON) | ||
|
||
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) | ||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../ ./tgfx) | ||
|
||
file(GLOB_RECURSE HELLO_2D_SOURCE_FILES src/*.*) | ||
add_executable(Hello2D ${HELLO_2D_SOURCE_FILES}) | ||
target_include_directories(Hello2D PRIVATE ${HELLO_2D_INCLUDES} src) | ||
target_link_libraries(Hello2D tgfx-drawers tgfx ${HELLO_2D_LIBS}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Tencent is pleased to support the open source community by making tgfx available. | ||
// | ||
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/BSD-3-Clause | ||
// | ||
// unless required by applicable law or agreed to in writing, software distributed under the | ||
// license is distributed on an "as is" basis, without warranties or conditions of any kind, | ||
// either express or implied. see the license for the specific language governing permissions | ||
// and limitations under the license. | ||
// | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include <filesystem> | ||
#include <fstream> | ||
#include "drawers/Drawer.h" | ||
#include "tgfx/gpu/Surface.h" | ||
#include "tgfx/opengl/GLDevice.h" | ||
#include "tgfx/platform/Print.h" | ||
|
||
static std::string GetRootPath() { | ||
std::filesystem::path filePath = __FILE__; | ||
auto dir = filePath.parent_path().string(); | ||
return std::filesystem::path(dir + "/../..").lexically_normal(); | ||
} | ||
|
||
static void SaveFile(std::shared_ptr<tgfx::Data> data, const std::string& output) { | ||
std::filesystem::path path = output; | ||
std::filesystem::create_directories(path.parent_path()); | ||
std::ofstream out(path); | ||
out.write(reinterpret_cast<const char*>(data->data()), | ||
static_cast<std::streamsize>(data->size())); | ||
out.close(); | ||
} | ||
|
||
int main() { | ||
auto rootPath = GetRootPath(); | ||
drawers::AppHost appHost(720, 720, 2.0f); | ||
auto image = tgfx::Image::MakeFromFile(rootPath + "resources/assets/bridge.jpg"); | ||
appHost.addImage("bridge", std::move(image)); | ||
auto typeface = tgfx::Typeface::MakeFromPath(rootPath + "resources/font/NotoSansSC-Regular.otf"); | ||
appHost.addTypeface("default", std::move(typeface)); | ||
typeface = tgfx::Typeface::MakeFromPath(rootPath + "resources/font/NotoColorEmoji.ttf"); | ||
appHost.addTypeface("emoji", std::move(typeface)); | ||
|
||
auto device = tgfx::GLDevice::Make(); | ||
if (device == nullptr) { | ||
tgfx::PrintError("Failed to create the Device!"); | ||
return -1; | ||
} | ||
auto context = device->lockContext(); | ||
if (context == nullptr) { | ||
tgfx::PrintError("Failed to lock the Context!"); | ||
return -1; | ||
} | ||
auto surface = tgfx::Surface::Make(context, appHost.width(), appHost.height()); | ||
auto canvas = surface->getCanvas(); | ||
auto drawerNames = drawers::Drawer::Names(); | ||
for (auto& name : drawerNames) { | ||
auto drawer = drawers::Drawer::GetByName(name); | ||
canvas->clear(); | ||
drawer->draw(canvas, &appHost); | ||
tgfx::Bitmap bitmap = {}; | ||
bitmap.allocPixels(surface->width(), surface->height()); | ||
auto pixels = bitmap.lockPixels(); | ||
auto success = surface->readPixels(bitmap.info(), pixels); | ||
bitmap.unlockPixels(); | ||
if (!success) { | ||
tgfx::PrintError("Failed to readPixels!"); | ||
return -1; | ||
} | ||
auto data = bitmap.encode(); | ||
if (data == nullptr) { | ||
tgfx::PrintError("Failed to encode bitmap!"); | ||
return -1; | ||
} | ||
SaveFile(data, "out/" + name + ".png"); | ||
} | ||
device->unlock(); | ||
tgfx::PrintLog("All images have been saved to the 'out/' directory"); | ||
return 0; | ||
} |