Pixels, Surfaces, Renderer, oh my...
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# Simple examples for using hdk-sdl.
|
||||
|
||||
add_executable(HelloWorld HelloWorld.cpp)
|
||||
target_link_libraries(HelloWorld PRIVATE hdk-sdl)
|
||||
add_executable(HelloRenderer2D HelloRenderer2D.cpp)
|
||||
target_link_libraries(HelloRenderer2D PRIVATE hdk-sdl)
|
||||
@@ -0,0 +1,66 @@
|
||||
#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
|
||||
namespace hello_renderer_2d {
|
||||
using namespace hdk;
|
||||
class HelloRenderer2D : public sdl::PublicApp {
|
||||
public:
|
||||
const sdl::Log& AppLog { sdl::Log::Application };
|
||||
grid::eps::TapScope taps, loud;
|
||||
sdl::evt::AllTypes events;
|
||||
const sdl::evt::EventConduit::Callback_t LOG_EVENT = [&](SDL_Event* event) {
|
||||
auto description = sdl::Event::GetDescription<1024>(event);
|
||||
AppLog.Debug("Event received: %s", description.c_str());
|
||||
};
|
||||
virtual SDL_AppResult Event(SDL_Event* event) override {
|
||||
// Get a description of the event for logging, you can adjust the buffer size as needed, defaults to 512
|
||||
events.Trigger(event);
|
||||
return AppStatus;
|
||||
}
|
||||
|
||||
sdl::Window window { nullptr };
|
||||
sdl::Renderer renderer { nullptr };
|
||||
|
||||
virtual SDL_AppResult Init(int argc, char* argv[]) override {
|
||||
sdl::Log::SetPriorities(SDL_LOG_PRIORITY_DEBUG);
|
||||
auto [w, r] = sdl::CreateWindowAndRenderer("Hello World", 800, 600, 0);
|
||||
window = w;
|
||||
renderer = r;
|
||||
if (!window || !renderer) {
|
||||
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);
|
||||
taps << events.Quit.Attach([&](SDL_Event* event) {
|
||||
AppStatus = SDL_APP_SUCCESS;
|
||||
printf("Quit event received, exiting...\n");
|
||||
});
|
||||
|
||||
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!");
|
||||
loud.PauseAll();
|
||||
});
|
||||
|
||||
taps << events.Window[window_id].MouseEnter.Attach([&](SDL_Event* event) {
|
||||
AppLog.Info("Mouse entered the window!");
|
||||
loud.ResumeAll();
|
||||
});
|
||||
return AppStatus;
|
||||
}
|
||||
|
||||
virtual SDL_AppResult Iterate() override {
|
||||
renderer.SetDrawColor(255, 0, 0, 0);
|
||||
renderer.FillRect(nullptr);
|
||||
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());
|
||||
renderer.Present();
|
||||
return AppStatus;
|
||||
}
|
||||
};
|
||||
}
|
||||
// We tell the HDK_SDL_MAIN_CLASS macro to use our HelloRenderer2D class
|
||||
HDK_SDL_MAIN_CLASS(hello_renderer_2d::HelloRenderer2D)
|
||||
// this builds all the SDL main boilerplate and connects it to our HelloRenderer2D class
|
||||
@@ -1,27 +0,0 @@
|
||||
#include <hdk/sdl.hpp>
|
||||
|
||||
using namespace hdk::sdl;
|
||||
|
||||
Window window { nullptr };
|
||||
//Renderer renderer { nullptr };
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
window = Window::Create("Hello World", 800, 600, 0);
|
||||
if (!window) {
|
||||
SDL_Log("Failed to create window: %s", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
//
|
||||
SDL_Event event;
|
||||
bool running = true;
|
||||
while (running) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_EVENT_QUIT) {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
window = nullptr; // Explicitly release the window before quitting SDL
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user