Current status of SDL.
This commit is contained in:
+29
-2
@@ -1,4 +1,31 @@
|
||||
# Simple examples for using hdk-sdl.
|
||||
# Simple examples for using hdk-sdl.
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
||||
option(HDK_SDL_INSTALL_EXAMPLES "Install hdk-sdl example binaries and assets" OFF)
|
||||
|
||||
set(HDK_SDL_EXAMPLES_ASSETS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/assets)
|
||||
set(HDK_SDL_EXAMPLES_ASSETS_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/../assets)
|
||||
|
||||
add_executable(HelloRenderer2D HelloRenderer2D.cpp)
|
||||
target_link_libraries(HelloRenderer2D PRIVATE hdk-sdl)
|
||||
target_link_libraries(HelloRenderer2D PRIVATE hdk-sdl)
|
||||
|
||||
# Stage shared example assets beside the example executables in the build tree.
|
||||
add_custom_command(TARGET HelloRenderer2D POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E rm -rf ${HDK_SDL_EXAMPLES_ASSETS_BUILD_DIR}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${HDK_SDL_EXAMPLES_ASSETS_SOURCE_DIR}
|
||||
${HDK_SDL_EXAMPLES_ASSETS_BUILD_DIR}
|
||||
COMMENT "Staging hdk-sdl example assets"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
if(HDK_SDL_INSTALL_EXAMPLES)
|
||||
install(TARGETS HelloRenderer2D
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
|
||||
install(DIRECTORY ${HDK_SDL_EXAMPLES_ASSETS_SOURCE_DIR}/
|
||||
DESTINATION assets
|
||||
)
|
||||
endif()
|
||||
@@ -1,7 +1,23 @@
|
||||
#include <hdk/sdl.hpp> // Hollow Wrapper to SDL
|
||||
#include <hdk/sdl/main.hpp> // Hollow Wrapper to SDL main boilerplate via hdk::sdl::ApplicationInterface & the macro HDK_SDL_MAIN_CLASS
|
||||
#include <filesystem>
|
||||
namespace hello_renderer_2d {
|
||||
using namespace hdk;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
fs::path GetAssetsRoot() {
|
||||
const char* base = SDL_GetBasePath();
|
||||
if (!base || *base == '\0') {
|
||||
return fs::path("assets");
|
||||
}
|
||||
fs::path exe_dir(base);
|
||||
return exe_dir / ".." / "assets";
|
||||
}
|
||||
|
||||
fs::path AssetPath(const fs::path& category, const fs::path& relative_path) {
|
||||
return GetAssetsRoot() / category / relative_path;
|
||||
}
|
||||
|
||||
class HelloRenderer2D : public sdl::PublicApp {
|
||||
public:
|
||||
const sdl::Log& AppLog { sdl::Log::Application };
|
||||
@@ -19,9 +35,12 @@ namespace hello_renderer_2d {
|
||||
|
||||
sdl::Window window { nullptr };
|
||||
sdl::Renderer renderer { nullptr };
|
||||
sdl::Surface surface { nullptr };
|
||||
sdl::Texture texture { nullptr };
|
||||
|
||||
virtual SDL_AppResult Init(int argc, char* argv[]) override {
|
||||
sdl::Log::SetPriorities(SDL_LOG_PRIORITY_DEBUG);
|
||||
SDL_SetAppMetadata("Hello Renderer 2D", "0.0.1", "institute.ufp.hdk.sdl.examples.hello_renderer_2d");
|
||||
sdl::Log::SetPriorities(SDL_LOG_PRIORITY_DEBUG);
|
||||
auto [w, r] = sdl::CreateWindowAndRenderer("Hello World", 800, 600, 0);
|
||||
window = w;
|
||||
renderer = r;
|
||||
@@ -29,14 +48,28 @@ namespace hello_renderer_2d {
|
||||
SDL_Log("Failed to create window or renderer: %s", SDL_GetError());
|
||||
return AppStatus = SDL_APP_FAILURE;
|
||||
}
|
||||
renderer.SetLogicalPresentation(800, 600, SDL_LOGICAL_PRESENTATION_LETTERBOX);
|
||||
renderer.SetDrawColor(255, 0, 0, 0);
|
||||
|
||||
AppLog.Info("Assets root: %s", GetAssetsRoot().lexically_normal().string().c_str());
|
||||
fs::path matStoneGray01Path = AssetPath(
|
||||
"textures", "sbs_-_base_materials_pack_128x128/Base Materials 128x128/Stone/Mat_Stone_Gray_01-128x128.png");
|
||||
AppLog.Info("Example texture path: %s", matStoneGray01Path.lexically_normal().string().c_str());
|
||||
surface = sdl::Surface::LoadSurface(matStoneGray01Path.string().c_str());
|
||||
if (!surface) {
|
||||
AppLog.Error("Failed to load surface: %s", SDL_GetError());
|
||||
return AppStatus = SDL_APP_FAILURE;
|
||||
}
|
||||
texture = renderer.CreateTextureFromSurface(surface);
|
||||
if (!texture) {
|
||||
AppLog.Error("Failed to create texture from surface: %s", SDL_GetError());
|
||||
return AppStatus = SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
taps << events.Quit.Attach([&](SDL_Event* event) {
|
||||
AppStatus = SDL_APP_SUCCESS;
|
||||
printf("Quit event received, exiting...\n");
|
||||
});
|
||||
|
||||
loud << events.Attach(LOG_EVENT);
|
||||
//loud << events.Attach(LOG_EVENT);
|
||||
SDL_WindowID window_id = window.GetID();
|
||||
taps << events.Window[window_id].MouseLeave.Attach([&](SDL_Event* event) {
|
||||
AppLog.Info("Mouse left the window!");
|
||||
@@ -47,15 +80,36 @@ namespace hello_renderer_2d {
|
||||
AppLog.Info("Mouse entered the window!");
|
||||
loud.ResumeAll();
|
||||
});
|
||||
|
||||
taps << events.Window[window_id].Moved % [&](SDL_Event* event) {
|
||||
AppLog.Info("Window moved to (%d, %d)", event->window.data1, event->window.data2);
|
||||
};
|
||||
renderer.SetLogicalPresentation(800, 600, SDL_LOGICAL_PRESENTATION_LETTERBOX);
|
||||
renderer.SetDrawColor(255, 0, 0, 0);
|
||||
renderer.FillRect(nullptr);
|
||||
renderer.Present();
|
||||
return AppStatus;
|
||||
}
|
||||
|
||||
virtual SDL_AppResult Iterate() override {
|
||||
renderer.SetDrawColor(255, 0, 0, 0);
|
||||
|
||||
renderer.SetViewport({0, 0, 800, 600});
|
||||
renderer.SetDrawColor(0, 0, 0, 0);
|
||||
renderer.FillRect(nullptr);
|
||||
renderer.RenderTextureTiled(texture, nullptr, 2.0f, nullptr, 1.0f);
|
||||
renderer.SetDrawColor(255, 255, 255, 255);
|
||||
renderer.DebugTextFormat(0, 0, "Displays: %d", sdl::Display::GetDisplays().size());
|
||||
renderer.DebugTextFormat(0, 20, "Render Drivers: %d", sdl::Renderer::GetNumRenderDrivers());
|
||||
auto displays = sdl::Display::GetDisplays();
|
||||
renderer.DebugTextFormat(0, 0, "Displays: %d", displays.size());
|
||||
renderer.DebugTextFormat(0, 20, "Render Drivers: %d", sdl::Renderer::GetNumRenderDrivers());
|
||||
|
||||
renderer.SetViewport({10, 300, 800, 300});
|
||||
for(auto & display : displays) {
|
||||
sdl::FRect fbounds = display.GetBounds();
|
||||
fbounds *= 0.15f; // scale down for better visibility
|
||||
renderer.SetDrawColor(255, 255, 255, 255);
|
||||
renderer.RenderRect(&fbounds);
|
||||
renderer.DebugTextFormat(fbounds.x+2, fbounds.y+2, " %s ", display.GetName());
|
||||
}
|
||||
renderer.Present();
|
||||
return AppStatus;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user