Skip to content

Commit

Permalink
some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Jun 2, 2024
1 parent d6b5db9 commit 59a7e7e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 57 deletions.
2 changes: 1 addition & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ subPackage {
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/cube.d" "src/examples/mrt.d" "src/examples/blend.d" "src/examples/imgui.d" "src/examples/droptest.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast -Dartifact"
preBuildCommands "zig build -Doptimize=ReleaseFast -Dartifact"
}
subPackage {
name "imgui"
Expand Down
72 changes: 34 additions & 38 deletions src/examples/droptest.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,26 @@ import sfetch = sokol.fetch;
import imgui = sokol.imgui;
import log = sokol.log;

extern (C):

enum MAX_FILE_SIZE = 1024 * 1024;

enum LoadState
{
LOADSTATE_UNKNOWN = 0,
LOADSTATE_SUCCESS,
LOADSTATE_FAILED,
LOADSTATE_FILE_TOO_BIG,
Unknown = 0,
Success,
Failed,
FileTooBig,
}

struct State
{
LoadState load_state = LoadState.LOADSTATE_UNKNOWN;
LoadState load_state = LoadState.Unknown;
uint size = 0;
ubyte[MAX_FILE_SIZE] buffer = 0;
}

static State state;

void init()
extern (C) void init() @safe @nogc nothrow
{
sg.Desc gfx = {
environment: sgapp.environment,
Expand All @@ -43,7 +41,7 @@ void init()
imgui.Desc imgui_desc = {0};
imgui.setup(imgui_desc);

// closure/lambda
// ifndef emscripten
static if((){version(Emscripten) return false; else return true;}())
{
sfetch.Desc fetch_desc = {
Expand All @@ -55,8 +53,9 @@ void init()
}
}

void frame()
extern (C) void frame() @trusted @nogc nothrow
{
// ifndef emscripten
static if((){version(Emscripten) return false;else return true;}())
{
sfetch.dowork;
Expand All @@ -76,18 +75,18 @@ void frame()
igSetNextWindowPos(window_pos, ImGuiCond_.ImGuiCond_Once, window_pos_pivot);
igSetNextWindowSize(window_size, ImGuiCond_.ImGuiCond_Once);
igBegin("Drop a file!".ptr, null, ImGuiWindowFlags_.ImGuiWindowFlags_None);
if(state.load_state != LoadState.LOADSTATE_UNKNOWN)
if(state.load_state != LoadState.Unknown)
{
igText("%s:", sapp.getDroppedFilePath(0));
}
switch (state.load_state) {
case LoadState.LOADSTATE_FAILED:
case LoadState.Failed:
igText("LOAD FAILED!");
break;
case LoadState.LOADSTATE_FILE_TOO_BIG:
case LoadState.FileTooBig:
igText("FILE TOO BIG!");
break;
case LoadState.LOADSTATE_SUCCESS:
case LoadState.Success:
igSeparator;
renderFileContent;
break;
Expand All @@ -104,7 +103,7 @@ void frame()
sg.commit;
}

void event(const(sapp.Event)* ev)
extern (C) void event(const(sapp.Event)* ev) @trusted @nogc nothrow
{
imgui.simgui_handle_event(ev);
if (ev.type == sapp.EventType.Files_dropped)
Expand Down Expand Up @@ -132,7 +131,7 @@ void event(const(sapp.Event)* ev)
}
}

void cleanup()
extern (C) void cleanup() @safe @nogc nothrow
{
// ifndef emscripten
static if((){version(Emscripten) return false;else return true;}())
Expand All @@ -143,7 +142,7 @@ void cleanup()
sg.shutdown;
}

void main()
extern (C) void main() @safe @nogc nothrow
{
sapp.Desc runner = {
window_title: "droptest.d",
Expand All @@ -161,7 +160,7 @@ void main()
sapp.run(runner);
}

void renderFileContent()
void renderFileContent() @nogc nothrow
{
immutable int bytes_per_line = 16; // keep this 2^N
immutable int num_lines = (state.size + (bytes_per_line - 1)) / bytes_per_line;
Expand Down Expand Up @@ -209,55 +208,48 @@ void renderFileContent()
version (Emscripten)
{
// the async-loading callback for sapp_html5_fetch_dropped_file
void emsc_load_callback(const (sapp.Html5FetchResponse*) response)
extern (C) void emsc_load_callback(const (sapp.Html5FetchResponse*) response) @nogc nothrow
{
if (response.succeeded)
{
state.load_state = LoadState.LOADSTATE_SUCCESS;
state.load_state = LoadState.Success;
state.size = cast(uint) response.data.size;
}
else if (response.error_code == sapp.Html5FetchError.Fetch_error_buffer_too_small)
{
state.load_state = LoadState.LOADSTATE_FILE_TOO_BIG;
state.load_state = LoadState.FileTooBig;
}
else
{
state.load_state = LoadState.LOADSTATE_FAILED;
state.load_state = LoadState.Failed;
}
}
}
else
{
// the async-loading callback for native platforms
void native_load_callback(const (sfetch.Response*) response)
extern (C) void native_load_callback(const (sfetch.Response*) response) @nogc nothrow
{
if (response.fetched)
{
state.load_state = LoadState.LOADSTATE_SUCCESS;
state.load_state = LoadState.Success;
state.size = cast(uint) response.data.size;
}
else if (response.error_code == sfetch.Error.Buffer_too_small)
{
state.load_state = LoadState.LOADSTATE_FILE_TOO_BIG;
state.load_state = LoadState.FileTooBig;
}
else
{
state.load_state = LoadState.LOADSTATE_FAILED;
state.load_state = LoadState.Failed;
}
}
}

// -- CIMGUI -- //
@nogc nothrow:
extern (C)
struct ImVec2
{
float x = 0.0f;
float y = 0.0f;
}

extern (C) bool igBeginChild_Str(scope const(char)* str_id, const ImVec2 size, bool border, ImGuiWindowFlags flags);
extern (C) bool igBegin(const(char)* name, bool* p_open, ImGuiWindowFlags flags);
extern (C) bool igBegin(const(char)* name, scope bool* p_open, ImGuiWindowFlags flags);
extern (C) void igEnd();
extern (C) void igSetNextWindowPos(const ImVec2 pos, ImGuiCond cond, const ImVec2 pivot);
extern (C) void igSetNextWindowSize(const ImVec2 size, ImGuiCond cond);
Expand All @@ -271,7 +263,14 @@ extern (C) void igEndChild();
extern (C) void igSeparator();
extern (C) float igGetTextLineHeight();
extern (C) bool ImGuiListClipper_Step(scope ImGuiListClipper* self);
extern (C)

extern (C):
struct ImVec2
{
float x = 0.0f;
float y = 0.0f;
}

enum ImGuiColorEditFlags_
{
ImGuiColorEditFlags_None = 0,
Expand Down Expand Up @@ -309,7 +308,6 @@ enum ImGuiColorEditFlags_
ImGuiColorEditFlags_InputMask_ = ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_InputHSV
}

extern (C)
enum ImGuiWindowFlags_
{
ImGuiWindowFlags_None = 0,
Expand Down Expand Up @@ -346,7 +344,6 @@ enum ImGuiWindowFlags_
ImGuiWindowFlags_ChildMenu = 1 << 28
}

extern (C)
enum ImGuiCond_
{
ImGuiCond_None = 0,
Expand All @@ -356,7 +353,6 @@ enum ImGuiCond_
ImGuiCond_Appearing = 1 << 3
}

extern (C):
alias ImGuiCond = int;
alias ImGuiColorEditFlags = int;
alias ImGuiWindowFlags = int;
Expand Down
31 changes: 15 additions & 16 deletions src/examples/imgui.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// imgui.d
//
// Using cimgui+sokol, based on https://github.com/floooh/cimgui-sokol-starterkit
// Using cimgui + sokol, based on https://github.com/floooh/sokol-zig-imgui-sample
//------------------------------------------------------------------------------

module examples.imgui;
Expand All @@ -13,6 +13,7 @@ import imgui = sokol.imgui;
import log = sokol.log;

extern (C):
@safe:

struct State
{
Expand All @@ -28,7 +29,7 @@ struct State

static State state;

void init()
void init() @safe @nogc nothrow
{
sg.Desc gfx = {
environment: sgapp.environment,
Expand All @@ -39,7 +40,7 @@ void init()
imgui.setup(imgui_desc);
}

void frame()
void frame() @trusted @nogc nothrow
{
imgui.FrameDesc imgui_desc = {
width: sapp.width(),
Expand Down Expand Up @@ -73,18 +74,18 @@ void frame()
sg.commit;
}

void event(const(sapp.Event)* ev)
void event(const(sapp.Event)* ev) @trusted @nogc nothrow
{
imgui.simgui_handle_event(ev);
}

void cleanup()
void cleanup() @safe @nogc nothrow
{
imgui.shutdown;
sg.shutdown;
}

void main()
void main() @safe @nogc nothrow
{
sapp.Desc runner = {
window_title: "imgui.d",
Expand All @@ -101,19 +102,20 @@ void main()
}

// -- CIMGUI -- //
extern (C)
@nogc nothrow:
extern (C) bool igBegin(scope const(char)* name, scope bool* p_open, ImGuiWindowFlags flags);
extern (C) void igEnd();
extern (C) void igSetNextWindowPos(const ImVec2 pos, ImGuiCond cond, const ImVec2 pivot);
extern (C) void igSetNextWindowSize(const ImVec2 size, ImGuiCond cond);
extern (C) bool igColorEdit3(scope const(char)* label, ref float[3] col, ImGuiColorEditFlags flags);

extern (C):
struct ImVec2
{
float x = 0.0f;
float y = 0.0f;
}

extern (C) bool igBegin(const(char)* name, bool* p_open, ImGuiWindowFlags flags);
extern (C) void igEnd();
extern (C) void igSetNextWindowPos(const ImVec2 pos, ImGuiCond cond, const ImVec2 pivot);
extern (C) void igSetNextWindowSize(const ImVec2 size, ImGuiCond cond);
extern (C) bool igColorEdit3(const(char)* label, ref float[3] col, ImGuiColorEditFlags flags);
extern (C)
enum ImGuiColorEditFlags_
{
ImGuiColorEditFlags_None = 0,
Expand Down Expand Up @@ -151,7 +153,6 @@ enum ImGuiColorEditFlags_
ImGuiColorEditFlags_InputMask_ = ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_InputHSV
}

extern (C)
enum ImGuiWindowFlags_
{
ImGuiWindowFlags_None = 0,
Expand Down Expand Up @@ -188,7 +189,6 @@ enum ImGuiWindowFlags_
ImGuiWindowFlags_ChildMenu = 1 << 28
}

extern (C)
enum ImGuiCond_
{
ImGuiCond_None = 0,
Expand All @@ -198,7 +198,6 @@ enum ImGuiCond_
ImGuiCond_Appearing = 1 << 3
}

extern (C):
alias ImGuiCond = int;
alias ImGuiColorEditFlags = int;
alias ImGuiWindowFlags = int;
3 changes: 1 addition & 2 deletions src/sokol/c/sokol_imgui.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "sokol_defines.h"
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_log.h"
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include <cimgui.h>
#include "cimgui.h"
#include "sokol_imgui.h"

0 comments on commit 59a7e7e

Please sign in to comment.