Skip to content

Commit

Permalink
Move SDL init into Ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 7, 2024
1 parent 6b2d6d0 commit 5bbfed2
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 48 deletions.
2 changes: 0 additions & 2 deletions Submodules/UIKit/include/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class Application {
~Application();

private:
SDL_Window* window;

sk_sp<SkTypeface> typeface;
float fRotationAngle = 0;

Expand Down
4 changes: 2 additions & 2 deletions Submodules/UIKit/include/SkiaCtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bool platformRunLoop(std::function<bool()> loop);

class SkiaCtx {
public:
virtual ~SkiaCtx() {}
virtual ~SkiaCtx() = default;
virtual sk_sp<SkSurface> getBackbufferSurface() = 0;
virtual sk_sp<GrDirectContext> directContext() = 0;
virtual void flushAndSubmit(sk_sp<SkSurface> surface);
Expand All @@ -29,6 +29,6 @@ class SkiaCtx {
sk_sp<SkFontMgr> fontMgr;
};

std::unique_ptr<SkiaCtx> MakeSkiaCtx(SDL_Window* window);
std::unique_ptr<SkiaCtx> MakeSkiaCtx();

}
2 changes: 1 addition & 1 deletion Submodules/UIKit/include/platforms/SkiaCtx_sdlBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NXKit {

class SkiaCtx_sdlBase : public SkiaCtx {
public:
SkiaCtx_sdlBase(SDL_Window *window);
SkiaCtx_sdlBase();

virtual void swapBuffers() override;

Expand Down
2 changes: 1 addition & 1 deletion Submodules/UIKit/include/platforms/ios/SkiaCtx_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace NXKit {

class SkiaCtx_ios : public SkiaCtx_sdlBase {
public:
SkiaCtx_ios(SDL_Window *window);
SkiaCtx_ios();

sk_sp<SkSurface> getBackbufferSurface() override;

Expand Down
2 changes: 1 addition & 1 deletion Submodules/UIKit/include/platforms/macos/SkiaCtx_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NXKit {

class SkiaCtx_macos: public SkiaCtx_sdlBase {
public:
SkiaCtx_macos(SDL_Window* window);
SkiaCtx_macos();

sk_sp<SkSurface> getBackbufferSurface() override;
// float getScaleFactor() override;
Expand Down
31 changes: 3 additions & 28 deletions Submodules/UIKit/lib/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,18 @@ Application* Application::shared = nullptr;
int Application::resizingEventWatcher(void* data, SDL_Event* event) {
if (event->type == SDL_WINDOWEVENT &&
event->window.event == SDL_WINDOWEVENT_RESIZED) {
SDL_Window* win = SDL_GetWindowFromID(event->window.windowID);
if (win == (SDL_Window*)data) {
Application::shared->render();
}
((Application*) data)->render();
}
return 0;
}

Application::Application() {
shared = this;

SDL_Init(SDL_INIT_EVERYTHING);

Uint32 flags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE;
#ifdef PLATFORM_IOS
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);

flags |= SDL_WINDOW_OPENGL;
#endif

window = SDL_CreateWindow("Window", 12, 12, 1280, 720, flags);
auto context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);

skiaCtx = MakeSkiaCtx(window);
skiaCtx = MakeSkiaCtx();

SDL_AddEventWatch(resizingEventWatcher, window);
SDL_AddEventWatch(resizingEventWatcher, this);

SkFontStyle style;
typeface = skiaCtx->getFontMgr()->matchFamilyStyle(nullptr, style);
Expand Down
4 changes: 2 additions & 2 deletions Submodules/UIKit/lib/platforms/SkiaCtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ using namespace NXKit;

void SkiaCtx::flushAndSubmit(sk_sp<SkSurface> surface) {
if (auto dContext = directContext()) {
dContext->flushAndSubmit(surface.get(), GrSyncCpu::kNo);
dContext->flushAndSubmit(surface.get(), GrSyncCpu::kYes);
}
}
}
31 changes: 27 additions & 4 deletions Submodules/UIKit/lib/platforms/SkiaCtx_sdlBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,31 @@

using namespace NXKit;

SkiaCtx_sdlBase::SkiaCtx_sdlBase(SDL_Window *window): window(window)
{ }
SkiaCtx_sdlBase::SkiaCtx_sdlBase()
{
SDL_Init(SDL_INIT_EVERYTHING);
Uint32 flags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE;

#ifdef USE_GLES
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);

flags |= SDL_WINDOW_OPENGL;
#endif

window = SDL_CreateWindow("Window", 12, 12, 1280, 720, flags);
auto context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
}

void SkiaCtx_sdlBase::swapBuffers() {
SDL_GL_SwapWindow(window);
Expand All @@ -19,5 +42,5 @@ float SkiaCtx_sdlBase::getScaleFactor() {
int w, h, dw, dh;
SDL_GetWindowSize(window, &w, &h);
SDL_GL_GetDrawableSize(window, &dw, &dh);
return dw / w;
}
return (float)dw / (float)w;
}
6 changes: 3 additions & 3 deletions Submodules/UIKit/lib/platforms/apple/ios/SkiaCtx_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

using namespace NXKit;

SkiaCtx_ios::SkiaCtx_ios(SDL_Window* window): SkiaCtx_sdlBase(window) {
SkiaCtx_ios::SkiaCtx_ios(): SkiaCtx_sdlBase() {
SkGraphics::Init();
initContext();

Expand Down Expand Up @@ -66,8 +66,8 @@
nullptr, &props);
}

std::unique_ptr<SkiaCtx> NXKit::MakeSkiaCtx(SDL_Window* window) {
return std::make_unique<SkiaCtx_ios>(window);
std::unique_ptr<SkiaCtx> NXKit::MakeSkiaCtx() {
return std::make_unique<SkiaCtx_ios>();
}

bool NXKit::platformRunLoop(std::function<bool()> loop) {
Expand Down
8 changes: 4 additions & 4 deletions Submodules/UIKit/lib/platforms/apple/macos/SkiaCtx_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

using namespace NXKit;

SkiaCtx_macos::SkiaCtx_macos(SDL_Window* window): SkiaCtx_sdlBase(window) {
SkiaCtx_macos::SkiaCtx_macos(): SkiaCtx_sdlBase() {
SkGraphics::Init();
initContext();

Expand Down Expand Up @@ -66,12 +66,12 @@
nullptr, &props);
}

std::unique_ptr<SkiaCtx> NXKit::MakeSkiaCtx(SDL_Window* window) {
return std::make_unique<SkiaCtx_macos>(window);
std::unique_ptr<SkiaCtx> NXKit::MakeSkiaCtx() {
return std::make_unique<SkiaCtx_macos>();
}

bool NXKit::platformRunLoop(std::function<bool()> loop) {
@autoreleasepool {
return loop();
}
}
}
6 changes: 6 additions & 0 deletions Submodules/cmake/toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ elseif (PLATFORM_IOS)
message("Building for iOS")
add_definitions( -DPLATFORM_IOS )

set(USE_GLES ON)

set(BUILD_SHARED_LIBS OFF)
set(PLATFORM OS64)
set(CMAKE_TOOLCHAIN_FILE ${EXTERN_PATH}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE PATH "vcpkg toolchain file")
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE ${EXTERN_PATH}/cmake/ios.toolchain.cmake CACHE PATH "ios toolchain file")
set(CMAKE_XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "1,2") # iphone, ipad
endif ()

if (USE_GLES)
add_definitions( -DUSE_GLES )
endif ()

# Setup LibRomFS
check_libromfs_generator()

Expand Down

0 comments on commit 5bbfed2

Please sign in to comment.