#include "rendering.h" #include "tier0/platform.h" #include "tier1/utlvector.h" #include "console.h" #include "tier1/commandline.h" #include "input.h" #define NS_PRIVATE_IMPLEMENTATION #define MTL_PRIVATE_IMPLEMENTATION #define MTK_PRIVATE_IMPLEMENTATION #define CA_PRIVATE_IMPLEMENTATION #include "tier0/minmax_off.h" #include "QuartzCore/CAMetalLayer.hpp" #include "Metal/Metal.hpp" #include "SDL3/SDL.h" #include "SDL3/SDL_keycode.h" #include "SDL3/SDL_metal.h" #include "SDL3/SDL_events.h" #include "tier0/minmax.h" char g_bConfigNotify = 0; uint32_t g_nWindowWidth = 1280; uint32_t g_nWindowHeight = 720; SDL_Window *g_window; SDL_MetalView g_mlView; CA::MetalLayer *g_mlLayer; MTL::Device *g_mlDevice; MTL::CommandQueue *g_mlCommandQueue; NS::AutoreleasePool *g_mlPool; #if defined(__APPLE__) && defined(__MACH__) #include "TargetConditionals.h" #if TARGET_OS_IPHONE // iOS #define SDL_METAL_VIEW UI::View #define STBI_NO_THREAD_LOCALS #else // macOS #define SDL_METAL_VIEW NS::View #endif #else // Other platforms #endif void IInput::SetMouseMode( EMouseMode mode ) { switch (mode) { case MOUSE_MODE_GAME: SDL_SetWindowRelativeMouseMode(g_window, true); return; default: SDL_SetWindowRelativeMouseMode(g_window, false); return; } } EInputKey ISDL_KeyName(SDL_Keycode key) { switch(key) { case SDLK_ESCAPE: return KEY_ESCAPE; case SDLK_1: return KEY_1; case SDLK_2: return KEY_2; case SDLK_3: return KEY_3; case SDLK_4: return KEY_4; case SDLK_5: return KEY_5; case SDLK_6: return KEY_6; case SDLK_7: return KEY_7; case SDLK_8: return KEY_8; case SDLK_9: return KEY_9; case SDLK_0: return KEY_0; case SDLK_A: return KEY_A; case SDLK_B: return KEY_B; case SDLK_C: return KEY_C; case SDLK_D: return KEY_D; case SDLK_E: return KEY_E; case SDLK_F: return KEY_F; case SDLK_G: return KEY_G; case SDLK_H: return KEY_H; case SDLK_I: return KEY_I; case SDLK_J: return KEY_J; case SDLK_K: return KEY_K; case SDLK_L: return KEY_L; case SDLK_M: return KEY_M; case SDLK_N: return KEY_N; case SDLK_O: return KEY_O; case SDLK_P: return KEY_P; case SDLK_Q: return KEY_Q; case SDLK_R: return KEY_R; case SDLK_S: return KEY_S; case SDLK_T: return KEY_T; case SDLK_U: return KEY_U; case SDLK_V: return KEY_V; case SDLK_W: return KEY_W; case SDLK_X: return KEY_X; case SDLK_Y: return KEY_Y; case SDLK_Z: return KEY_Z; } return KEY_NONE; }; void IVideo_HandleEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { SDL_KeyboardEvent *key = &event.key; SDL_MouseMotionEvent *motion = &event.motion; switch (event.type) { case SDL_EVENT_WINDOW_RESIZED: g_nWindowWidth = event.window.data1; g_nWindowHeight = event.window.data2; g_bConfigNotify = 2; break; case SDL_EVENT_KEY_DOWN: if (!key->repeat) IInput::KeyEvent(ISDL_KeyName(key->key),KEY_EVENT_TYPE_DOWN); break; case SDL_EVENT_KEY_UP: key = &event.key; if (!key->repeat) IInput::KeyEvent(ISDL_KeyName(key->key),KEY_EVENT_TYPE_UP); break; case SDL_EVENT_MOUSE_MOTION: IInput::AxisEvent(AXIS_MOUSE_X, motion->yrel*0.022); IInput::AxisEvent(AXIS_MOUSE_Y, -motion->xrel*0.022); break; } }; }; void IVideo::Init() { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); g_window = SDL_CreateWindow("rtt", 1280, 720, SDL_WINDOW_METAL); 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); g_mlLayer->setPixelFormat(MTL::PixelFormatBGRA8Unorm); g_mlCommandQueue = g_mlDevice->newCommandQueue(); g_mlLayer->setDrawableSize(CGSizeMake(1280, 720)); } 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)); CA::MetalDrawable *drawable = g_mlLayer->nextDrawable(); if (!drawable) return; 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(); pool->release(); } void IVideo::Deinit() { g_mlCommandQueue->release(); g_mlDevice->release(); };