Created
September 3, 2020 11:47
-
-
Save planetis-m/e39abac03c90cb37066ffb96b7b552b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
CStringArray* = object | |
len*: int | |
impl*: cstringArray | |
proc `=destroy`*(cstrs: var CStringArray) = | |
if cstrs.impl != nil: | |
for i in 0 ..< cstrs.len: | |
deallocShared(cstrs.impl[i]) | |
deallocShared(cstrs.impl) | |
proc `=`*(dest: var CStringArray, source: CStringArray) = | |
if dest.impl != source.impl: | |
`=destroy`(dest) | |
wasMoved(dest) | |
dest.impl = cast[cstringArray](allocShared(sizeof(cstring) * source.len)) | |
for i in 0 ..< source.len: | |
let cstrLen = source.impl[i].len + 1 | |
dest.impl[i] = cast[cstring](allocShared(cstrLen)) | |
copyMem(dest.impl[i], addr source.impl[i][0], cstrLen) | |
proc newCStringArray*(len: Natural): CStringArray = | |
let impl = cast[cstringArray](allocShared0(sizeof(cstring) * len)) | |
result = CStringArray(len: len, impl: impl) | |
proc newCStringArray*(a: openArray[cstring]): CStringArray = | |
let impl = cast[cstringArray](allocShared(a.len * sizeof(cstring))) | |
let x = cast[ptr UncheckedArray[cstring]](a) | |
for i in 0 ..< a.len: | |
let cstrLen = x[i].len + 1 | |
impl[i] = cast[cstring](alloc(cstrLen)) | |
copyMem(impl[i], addr x[i][0], cstrLen) | |
result = CStringArray(len: a.len, impl: impl) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sdl2, cstr, ../repos/vulkanim/vulkan | |
{.link: "/usr/lib/libvulkan.so".} | |
proc main = | |
let | |
windowWidth = 1366'i32 | |
windowHeight = 768'i32 | |
doAssert sdl2.init(INIT_VIDEO or INIT_EVENTS) == SdlSuccess | |
let window = createWindow("SDL/Vulkan Skeleton", SDL_WINDOWPOS_UNDEFINED, | |
SDL_WINDOWPOS_UNDEFINED, windowWidth, windowHeight, SDL_WINDOW_VULKAN) | |
var extensionCount = 0'u32 | |
doAssert vulkanGetInstanceExtensions(window, addr extensionCount, nil) == True32 | |
var extensionNames = newCStringArray(extensionCount) | |
doAssert vulkanGetInstanceExtensions(window, addr extensionCount, extensionNames.impl) == True32 | |
var appInfo = VkApplicationInfo( | |
sType: vkStructureTypeApplicationInfo, | |
pApplicationName: "SDL/Vulkan Skeleton", | |
applicationVersion: vkMakeVersion(0, 0, 1), | |
pEngineName: "No Engine", | |
engineVersion: vkMakeVersion(0, 0, 1), | |
apiVersion: vkApiVersion10) | |
var createInfo = VkInstanceCreateInfo( | |
sType: vkStructureTypeInstanceCreateInfo, | |
pApplicationInfo: addr appInfo, | |
enabledExtensionCount: extensionCount, | |
ppEnabledExtensionNames: extensionNames.impl) | |
var instance = vkNullHandle.VkInstance | |
doAssert vkCreateInstance(addr createInfo, nil, addr instance) == vkSuccess | |
var surface = vkNullHandle.VkSurfaceKHR | |
doAssert vulkanCreateSurface(window, instance, addr surface) == True32 | |
var physicalDevice = vkNullHandle.VkPhysicalDevice | |
var queueFamilyIndex: uint32 | |
var queue: VkQueue | |
var device = vkNullHandle.VkDevice | |
var queuePriority = 1.0'f32 | |
var deviceQueueCreateInfo = VkDeviceQueueCreateInfo( | |
sType: vkStructureTypeDeviceQueueCreateInfo, | |
queueFamilyIndex: queueFamilyIndex, | |
queueCount: 1'u32, | |
pQueuePriorities: addr queuePriority) # shouldn't this be an UncheckedArray[cfloat] | |
var enabledExtensionNames = newCStringArray([vkKhrSwapchainExtensionName.cstring]) | |
var deviceCreateInfo = VkDeviceCreateInfo( | |
sType: vkStructureTypeDeviceCreateInfo, | |
queueCreateInfoCount: 1, | |
pQueueCreateInfos: addr deviceQueueCreateInfo, | |
enabledExtensionCount: enabledExtensionNames.len.uint32, | |
ppEnabledExtensionNames: enabledExtensionNames.impl) | |
doAssert vkCreateDevice(physicalDevice, addr deviceCreateInfo, nil, addr device) == vkSuccess | |
vkGetDeviceQueue(device, queueFamilyIndex, 0, addr queue) | |
var | |
event: Event | |
running = true | |
while running: | |
while pollEvent(event): | |
if event.kind == QuitEvent: | |
running = false | |
vkDestroySurfaceKHR(instance, surface, nil) | |
destroy(window) | |
vkDestroyInstance(instance, nil) | |
sdl2.quit() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment