summaryrefslogtreecommitdiff
path: root/md/writeup
diff options
context:
space:
mode:
Diffstat (limited to 'md/writeup')
-rw-r--r--md/writeup/linux_hello_world_in_swift.md (renamed from md/writeup/hello_world_swift.md)150
1 files changed, 76 insertions, 74 deletions
diff --git a/md/writeup/hello_world_swift.md b/md/writeup/linux_hello_world_in_swift.md
index 5e39407..94ce5de 100644
--- a/md/writeup/hello_world_swift.md
+++ b/md/writeup/linux_hello_world_in_swift.md
@@ -1,5 +1,5 @@
title:Linux Hello world in Swift
-keywords:linux,swift
+keywords:linux,swift,apple,c,sdl2
# Linux Hello world in Swift
## Intro
@@ -175,26 +175,26 @@ _Package.swift_
import PackageDescription
let package = Package(
- name: "Cint0",
- dependencies: [
- // Dependencies declare other packages that this package depends on.
- // .package(url: /* package url */, from: "1.0.0"),
- ],
-
- targets: [
- // Targets are the basic building blocks of a package. A target can define a module or a test suite.
- // Targets can depend on other targets in this package, and on products in packages which this package depends on.
- .target(
- name:"Cint0",
- dependencies: ["ccode"]),
- .testTarget(
- name: "Cint0Tests",
- dependencies: ["Cint0"]),
- .target(
- name:"ccode",
- dependencies: []),
-
- ]
+ name: "Cint0",
+ dependencies: [
+ // Dependencies declare other packages that this package depends on.
+ // .package(url: /* package url */, from: "1.0.0"),
+ ],
+
+ targets: [
+ // Targets are the basic building blocks of a package. A target can define a module or a test suite.
+ // Targets can depend on other targets in this package, and on products in packages which this package depends on.
+ .target(
+ name:"Cint0",
+ dependencies: ["ccode"]),
+ .testTarget(
+ name: "Cint0Tests",
+ dependencies: ["Cint0"]),
+ .target(
+ name:"ccode",
+ dependencies: []),
+
+ ]
)
```
@@ -237,27 +237,27 @@ _Package.swift_
import PackageDescription
let package = Package(
- name: "CSDL2",
- products: [
- // Products define the executables and libraries produced by a package, and make them visible to other packages.
- .library(
- name: "CSDL2",
- targets: ["CSDL2"]),
- ],
- dependencies: [
- // Dependencies declare other packages that this package depends on.
- // .package(url: /* package url */, from: "1.0.0"),
- ],
- targets: [
- // Targets are the basic building blocks of a package. A target can define a module or a test suite.
- // Targets can depend on other targets in this package, and on products in packages which this package depends on.
- .target(
- name: "CSDL2",
- dependencies: []),
- .testTarget(
- name: "CSDL2Tests",
- dependencies: ["CSDL2"]),
- ]
+ name: "CSDL2",
+ products: [
+ // Products define the executables and libraries produced by a package, and make them visible to other packages.
+ .library(
+ name: "CSDL2",
+ targets: ["CSDL2"]),
+ ],
+ dependencies: [
+ // Dependencies declare other packages that this package depends on.
+ // .package(url: /* package url */, from: "1.0.0"),
+ ],
+ targets: [
+ // Targets are the basic building blocks of a package. A target can define a module or a test suite.
+ // Targets can depend on other targets in this package, and on products in packages which this package depends on.
+ .target(
+ name: "CSDL2",
+ dependencies: []),
+ .testTarget(
+ name: "CSDL2Tests",
+ dependencies: ["CSDL2"]),
+ ]
)
```
@@ -266,9 +266,9 @@ Here is main thing, to tell Swift with headers to make accessible
_module.modulemap_
```
module CSDL2 {
- header "Headers/CSDL2-Header.h"
- link "SDL2"
- export *
+ header "Headers/CSDL2-Header.h"
+ link "SDL2"
+ export *
}
```
@@ -296,40 +296,42 @@ SDL_Renderer *renderer = NULL;
int main()
{
- SDL_Init(SDL_INIT_VIDEO);
- window = SDL_CreateWindow(
- "WEBASM",
- SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
- SCREEN_WIDTH, SCREEN_HEIGHT,
- SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
- renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
- int quit=0;
- while(0 == quit)
- {
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_QUIT:
- {
- quit = 1;
- break;
- }
- }
- }
- }
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
+ SDL_Init(SDL_INIT_VIDEO);
+ window = SDL_CreateWindow(
+ "WEBASM",
+ SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+ SCREEN_WIDTH, SCREEN_HEIGHT,
+ SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
+ renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
+ SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
+ int quit=0;
+ while(0 == quit)
+ {
+ SDL_Event event;
+ while (SDL_PollEvent(&event))
+ {
+ switch (event.type)
+ {
+ case SDL_QUIT:
+ {
+ quit = 1;
+ break;
+ }
+ }
+ }
+ }
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
}
```
Lets convert C example to working with Swift code
Pointers can be defined as OpaquePointer without type so far.
+
All functions are called same as in C.
+
Enum types if they are passed then can be accessed as NAME.rawValue .
@@ -363,7 +365,7 @@ SDL_DestroyWindow(windowPtr)
SDL_Quit()
```
-So far all Swift features looks like they are working out of the box, without
+So far all Swift features looks like they are working out of the box, without.
deep Swift knowledge was able to bind library, and used. Also this is largest code base
Swift code that I ever wrote in my life. So far it looks like easy language to learn.
@@ -380,7 +382,7 @@ Swift code that I ever wrote in my life. So far it looks like easy language to l
[06] [https://theswiftdev.com/how-to-call-c-code-from-swift/](https://theswiftdev.com/how-to-call-c-code-from-swift/)
[07] [https://github.com/KevinVitale/SwiftSDL/blob/master/Package.swift](https://github.com/KevinVitale/SwiftSDL/blob/master/Package.swift)
[08] [https://rderik.com/blog/making-a-c-library-available-in-swift-using-the-swift-package/](https://rderik.com/blog/making-a-c-library-available-in-swift-using-the-swift-package/)
-[09] [https://github.com/KevinVitale/SwiftSDL/tree/master/Sources](https://github.com/KevinVitale/SwiftSDL/tree/master/Sources)
+[09] [https://github.com/KevinVitale/SwiftSDL/tree/master/Sources](https://github.com/KevinVitale/SwiftSDL/tree/master/Sources)
[10] [https://www.uraimo.com/2016/04/07/swift-and-c-everything-you-need-to-know/#working-with-pointers](https://www.uraimo.com/2016/04/07/swift-and-c-everything-you-need-to-know/#working-with-pointers)
[11] [https://www.objc.io/blog/2018/01/30/opaque-vs-unsafe-pointers/](https://www.objc.io/blog/2018/01/30/opaque-vs-unsafe-pointers/)