-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Impl Image.load & Add example for Backend.setIcons
- Loading branch information
Showing
8 changed files
with
59 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[submodule "external/cimgui"] | ||
path = external/cimgui | ||
url = https://github.com/cimgui/cimgui | ||
[submodule "external/stb"] | ||
path = external/stb | ||
url = https://github.com/nothings/stb.git |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#define STB_IMAGE_IMPLEMENTATION | ||
#include "stb_image.h" | ||
#include "imgui4cj.h" | ||
|
||
#include <assert.h> | ||
|
||
// foreign func imgui4cj_loadImage(result: CPointer<CImage>, filename: CString, pErrMsg: CPointer<CPointer<UInt8>>): Int32 | ||
IMGUI4CJ_API int imgui4cj_loadImage(CImage *result, const char *filename, const char ** pErrMsg) { | ||
assert(result && filename && pErrMsg); | ||
|
||
int channels; | ||
if ((result->pixels = stbi_load(filename, &result->width, &result->height, &channels, 4)) == NULL) { | ||
*pErrMsg = stbi_failure_reason(); | ||
return -1; | ||
} | ||
return 0; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,42 @@ | ||
package imgui4cj.utils | ||
|
||
import std.fs.File | ||
import std.io.InputStream | ||
import std.collection.ArrayList | ||
import std.fs.Path | ||
|
||
// TODO: Replace Image and related functions with std/3rd library used to process image | ||
foreign func imgui4cj_loadImage(result: CPointer<CImage>, filename: CString, pErrMsg: CPointer<CPointer<UInt8>>): Int32 | ||
|
||
@C | ||
public struct CImage { | ||
protected struct CImage { | ||
var width: Int32 = 0 | ||
var height: Int32 = 0 | ||
var pixels: CPointer<Byte> = CPointer<Byte>() | ||
|
||
public func release() { | ||
unsafe { LibC.free<Byte>(pixels) } | ||
} | ||
var pixels: CPointer<UInt8> = CPointer<UInt8>() | ||
} | ||
|
||
public struct Image { | ||
var width: Int32 = 0 | ||
var height: Int32 = 0 | ||
var pixels: ArrayList<Byte> = ArrayList<Byte>([]) | ||
|
||
public func toCImage(): CImage { | ||
var result = CImage() | ||
result.width = width | ||
result.height = height | ||
result.pixels = unsafe { LibC.malloc<Byte>(count: pixels.size) } | ||
|
||
unsafe { // Copy from pixels to result.pixels | ||
var p = result.pixels | ||
for (byte in pixels) { | ||
p.write(byte) | ||
p += 1 | ||
} | ||
} | ||
public class Image { | ||
private Image(private let cImage: CImage) { | ||
} | ||
|
||
return result | ||
~init() { | ||
unsafe { LibC.free<UInt8>(cImage.pixels) } | ||
} | ||
} | ||
|
||
public func tryLoadImage(_: InputStream): ?Image { | ||
// TODO: Impt it: read many image formats as pixels | ||
throw UnsupportedException("to be implemented") | ||
} | ||
protected func getCImage(): CImage { | ||
return cImage | ||
} | ||
|
||
public func tryLoadImage(filename: String): ?Image { | ||
try (fp = File.openRead(filename)) { | ||
return tryLoadImage(fp) | ||
public static func load(filename: Path): Image { | ||
return load(filename.toString()) | ||
} | ||
return None | ||
} | ||
|
||
public func loadImage(filename: String): Image { | ||
return tryLoadImage(filename).getOrThrow { | ||
IllegalArgumentException("invalid image file: ${filename}") | ||
public static func load(filename: String): Image { | ||
try (filenameRes = unsafe { LibC.mallocCString(filename) }.asResource()) { | ||
var cImage: CImage = CImage() | ||
var errMsg: CPointer<UInt8> = CPointer<UInt8>() | ||
let err = unsafe { imgui4cj_loadImage(inout cImage, filenameRes.value, inout errMsg) } | ||
if (err != 0) { | ||
throw IllegalArgumentException("Load image ${filename} failed: ${CString(errMsg)}") | ||
} | ||
return Image(cImage) | ||
} | ||
throw IllegalStateException("illegal state") | ||
} | ||
} |