brought back functionality from previous builds but now cross-platform

This commit is contained in:
2025-07-07 15:34:34 +03:00
parent 99eafb9443
commit 83bc9b7f16
61 changed files with 1210 additions and 581 deletions

View File

@@ -19,6 +19,7 @@
#include "SDL3/SDL_metal.h"
#include "SDL3/SDL_events.h"
#include "tier0/minmax.h"
#include "ml_video.h"
char g_bConfigNotify = 0;
uint32_t g_nWindowWidth = 1280;
@@ -29,7 +30,9 @@ SDL_MetalView g_mlView;
CA::MetalLayer *g_mlLayer;
MTL::Device *g_mlDevice;
MTL::CommandQueue *g_mlCommandQueue;
MTL::CommandBuffer *g_mlCommandBuffer;
NS::AutoreleasePool *g_mlPool;
MTL::Texture *g_mlDrawableTexture;
#if defined(__APPLE__) && defined(__MACH__)
#include "TargetConditionals.h"
@@ -114,7 +117,7 @@ void IVideo_HandleEvents()
SDL_MouseMotionEvent *motion = &event.motion;
switch (event.type)
{
case SDL_EVENT_WINDOW_RESIZED:
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
g_nWindowWidth = event.window.data1;
g_nWindowHeight = event.window.data2;
g_bConfigNotify = 2;
@@ -132,17 +135,51 @@ void IVideo_HandleEvents()
IInput::AxisEvent(AXIS_MOUSE_X, motion->yrel*0.022);
IInput::AxisEvent(AXIS_MOUSE_Y, -motion->xrel*0.022);
break;
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
{
SDL_GamepadAxis axis = (SDL_GamepadAxis)event.gaxis.axis;
float value = event.gaxis.value / 32768.0f;
if (abs(event.gaxis.value)<1000)
value = 0;
SDL_JoystickID id = event.gaxis.which;
if (axis == SDL_GAMEPAD_AXIS_RIGHTY)
{
IInput::AxisEvent(AXIS_CONTROLLER_PITCH, value);
}
if (axis == SDL_GAMEPAD_AXIS_RIGHTX)
{
IInput::AxisEvent(AXIS_CONTROLLER_YAW, -value);
}
}
break;
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
break;
case SDL_EVENT_GAMEPAD_BUTTON_UP:
break;
}
};
};
void APPLE_ConfigureLayer(CA::MetalLayer* pMetalLayer);
void IVideo::Init()
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
g_window = SDL_CreateWindow("rtt", 1280, 720, SDL_WINDOW_METAL);
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "0");
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMEPAD);
g_window = SDL_CreateWindow("rtt", 1280, 720, SDL_WINDOW_METAL | SDL_WINDOW_HIGH_PIXEL_DENSITY);
int nNumGamepads = 0;
SDL_JoystickID *pGamepads = SDL_GetGamepads(&nNumGamepads);
for ( uint32_t i = 0; i < nNumGamepads; i++ )
{
SDL_OpenGamepad(pGamepads[i]);
}
g_mlView = SDL_Metal_CreateView(g_window);
g_mlLayer = (CA::MetalLayer*)SDL_Metal_GetLayer(g_mlView);
g_mlDevice = MTL::CreateSystemDefaultDevice();
g_mlLayer->setDevice(g_mlDevice);
@@ -151,34 +188,42 @@ void IVideo::Init()
g_mlCommandQueue = g_mlDevice->newCommandQueue();
g_mlLayer->setDrawableSize(CGSizeMake(1280, 720));
g_mlLayer->setFramebufferOnly(false);
IMetal::Init();
}
}
void IVideo::Frame( float fDelta )
{
IVideo_HandleEvents();
NS::AutoreleasePool *pool = NS::AutoreleasePool::alloc()->init();
int w, h;
SDL_GetWindowSizeInPixels(g_window, &w, &h);
g_mlLayer->setDrawableSize(CGSizeMake(w, h));
g_mlLayer->setDrawableSize(CGSizeMake(g_nWindowWidth, g_nWindowHeight));
CA::MetalDrawable *drawable = g_mlLayer->nextDrawable();
if (!drawable)
return;
g_mlDrawableTexture = drawable->texture();
MTL::CommandBuffer *commandBuffer = g_mlCommandQueue->commandBuffer();
MTL::RenderPassDescriptor *renderPass = MTL::RenderPassDescriptor::alloc()->init();
renderPass->colorAttachments()->object(0)->setTexture(drawable->texture());
renderPass->colorAttachments()->object(0)->setLoadAction(MTL::LoadActionClear);
renderPass->colorAttachments()->object(0)->setStoreAction(MTL::StoreActionStore);
renderPass->colorAttachments()->object(0)->setClearColor(MTL::ClearColor(0.0, 0, 1.0, 1.0));
MTL::RenderCommandEncoder *renderEncoder = commandBuffer->renderCommandEncoder(renderPass);
renderEncoder->endEncoding();
commandBuffer->presentDrawable(drawable);
commandBuffer->commit();
g_mlCommandBuffer = g_mlCommandQueue->commandBuffer();
IMetal::Frame();
g_mlCommandBuffer->presentDrawable(drawable);
g_mlCommandBuffer->commit();
g_mlCommandBuffer->waitUntilCompleted();
for (auto &image: g_destroyImageBuffer)
{
image->release();
}
for (auto &buffer: g_destroyBuffersBuffer)
{
buffer->release();
}
g_destroyImageBuffer = {};
g_destroyBuffersBuffer = {};
pool->release();
g_bConfigNotify = 0;
}
void IVideo::Deinit()
@@ -186,3 +231,8 @@ void IVideo::Deinit()
g_mlCommandQueue->release();
g_mlDevice->release();
};
void IVideo::CreatePipelines( )
{
IMetal::CreatePipelines();
}