Added for reference
This commit is contained in:
parent
13e023f856
commit
e44fe9f8ab
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
*.log
|
||||||
|
*.o
|
||||||
|
*.bak
|
||||||
|
ff-*
|
||||||
|
imgui.ini
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
11
.project
Normal file
11
.project
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>ff-sdl-emcc-test</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
26
Makefile
Normal file
26
Makefile
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
#CXXFLAGS = -O2 -g -fmessage-length=0 -Ilib/soil2 -Ilib/imgui -Ilib/imgui_sdl -I/usr/include
|
||||||
|
CXXFLAGS = -O2 -g -fmessage-length=0 -Ilib/soil2 -Ilib/imgui -Ilib/imgui_sdl -s USE_SDL=2 -Wall -std=c++14
|
||||||
|
|
||||||
|
LDFLAGS = -s USE_SDL=2 -s ALLOW_MEMORY_GROWTH=1 -O2 --preload-file data --separate-asm --js-library preboot.js
|
||||||
|
|
||||||
|
SITE_CXX_OBJ = $(patsubst %.cpp,%.o,$(wildcard src/*.cpp)) $(patsubst %.cpp,%.o,$(wildcard src/*/*.cpp))
|
||||||
|
LIBS_CXX_OBJ = $(patsubst %.cpp,%.o,$(wildcard lib/*/*.cpp))
|
||||||
|
LIBS_C_OBJ = $(patsubst %.c,%.o,$(wildcard lib/*/*.c))
|
||||||
|
|
||||||
|
OBJS = $(LIBS_C_OBJ) $(LIBS_CXX_OBJ) $(SITE_CXX_OBJ)
|
||||||
|
|
||||||
|
LIBS = -lGL -lGLU
|
||||||
|
# -lSDL2
|
||||||
|
|
||||||
|
TARGET = ff-site.js
|
||||||
|
|
||||||
|
CC = emcc
|
||||||
|
CXX = emcc
|
||||||
|
|
||||||
|
$(TARGET): $(OBJS)
|
||||||
|
$(CXX) -o $(TARGET) $(OBJS) $(LIBS) $(LDFLAGS)
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJS) $(TARGET)
|
BIN
data/font/fontawesome-webfont.ttf
Normal file
BIN
data/font/fontawesome-webfont.ttf
Normal file
Binary file not shown.
BIN
data/font/lato-blackitalic-webfont.ttf
Normal file
BIN
data/font/lato-blackitalic-webfont.ttf
Normal file
Binary file not shown.
BIN
data/font/roboto-bold.ttf
Normal file
BIN
data/font/roboto-bold.ttf
Normal file
Binary file not shown.
BIN
data/home-pulsefire.png
Normal file
BIN
data/home-pulsefire.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 742 KiB |
BIN
data/home-tesla.png
Normal file
BIN
data/home-tesla.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 452 KiB |
BIN
data/pulsefire-flash.png
Normal file
BIN
data/pulsefire-flash.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 256 KiB |
BIN
data/pulsefire-pwm.png
Normal file
BIN
data/pulsefire-pwm.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 130 KiB |
BIN
data/vasc-list.png
Normal file
BIN
data/vasc-list.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 347 KiB |
54
lib/imgui/imconfig.h
Normal file
54
lib/imgui/imconfig.h
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// USER IMPLEMENTATION
|
||||||
|
// This file contains compile-time options for ImGui.
|
||||||
|
// Other options (memory allocation overrides, callbacks, etc.) can be set at runtime via the ImGuiIO structure - ImGui::GetIO().
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
//---- Define assertion handler. Defaults to calling assert().
|
||||||
|
//#define IM_ASSERT(_EXPR) MyAssert(_EXPR)
|
||||||
|
|
||||||
|
//---- Define attributes of all API symbols declarations, e.g. for DLL under Windows.
|
||||||
|
//#define IMGUI_API __declspec( dllexport )
|
||||||
|
//#define IMGUI_API __declspec( dllimport )
|
||||||
|
|
||||||
|
//---- Include imgui_user.h at the end of imgui.h
|
||||||
|
//#define IMGUI_INCLUDE_IMGUI_USER_H
|
||||||
|
|
||||||
|
//---- Don't implement default handlers for Windows (so as not to link with OpenClipboard() and others Win32 functions)
|
||||||
|
//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS
|
||||||
|
//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCS
|
||||||
|
|
||||||
|
//---- Don't implement help and test window functionality (ShowUserGuide()/ShowStyleEditor()/ShowTestWindow() methods will be empty)
|
||||||
|
//#define IMGUI_DISABLE_TEST_WINDOWS
|
||||||
|
|
||||||
|
//---- Don't define obsolete functions names
|
||||||
|
//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||||
|
|
||||||
|
//---- Pack colors to BGRA instead of RGBA (remove need to post process vertex buffer in back ends)
|
||||||
|
//#define IMGUI_USE_BGRA_PACKED_COLOR
|
||||||
|
|
||||||
|
//---- Implement STB libraries in a namespace to avoid conflicts
|
||||||
|
//#define IMGUI_STB_NAMESPACE ImGuiStb
|
||||||
|
|
||||||
|
//---- Define constructor and implicit cast operators to convert back<>forth from your math types and ImVec2/ImVec4.
|
||||||
|
/*
|
||||||
|
#define IM_VEC2_CLASS_EXTRA \
|
||||||
|
ImVec2(const MyVec2& f) { x = f.x; y = f.y; } \
|
||||||
|
operator MyVec2() const { return MyVec2(x,y); }
|
||||||
|
|
||||||
|
#define IM_VEC4_CLASS_EXTRA \
|
||||||
|
ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \
|
||||||
|
operator MyVec4() const { return MyVec4(x,y,z,w); }
|
||||||
|
*/
|
||||||
|
|
||||||
|
//---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files.
|
||||||
|
//---- e.g. create variants of the ImGui::Value() helper for your low-level math types, or your own widgets/helpers.
|
||||||
|
/*
|
||||||
|
namespace ImGui
|
||||||
|
{
|
||||||
|
void Value(const char* prefix, const MyMatrix44& v, const char* float_format = NULL);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
9848
lib/imgui/imgui.cpp
Normal file
9848
lib/imgui/imgui.cpp
Normal file
File diff suppressed because it is too large
Load diff
1400
lib/imgui/imgui.h
Normal file
1400
lib/imgui/imgui.h
Normal file
File diff suppressed because it is too large
Load diff
2638
lib/imgui/imgui_demo.cpp
Normal file
2638
lib/imgui/imgui_demo.cpp
Normal file
File diff suppressed because it is too large
Load diff
2393
lib/imgui/imgui_draw.cpp
Normal file
2393
lib/imgui/imgui_draw.cpp
Normal file
File diff suppressed because it is too large
Load diff
775
lib/imgui/imgui_internal.h
Normal file
775
lib/imgui/imgui_internal.h
Normal file
|
@ -0,0 +1,775 @@
|
||||||
|
// dear imgui, v1.50 WIP
|
||||||
|
// (internals)
|
||||||
|
|
||||||
|
// You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility!
|
||||||
|
// Implement maths operators for ImVec2 (disabled by default to not collide with using IM_VEC2_CLASS_EXTRA along with your own math types+operators)
|
||||||
|
// #define IMGUI_DEFINE_MATH_OPERATORS
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef IMGUI_VERSION
|
||||||
|
#error Must include imgui.h before imgui_internal.h
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdio.h> // FILE*
|
||||||
|
#include <math.h> // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning (push)
|
||||||
|
#pragma warning (disable: 4251) // class 'xxx' needs to have dll-interface to be used by clients of struct 'xxx' // when IMGUI_API is set to__declspec(dllexport)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Wunused-function" // for stb_textedit.h
|
||||||
|
#pragma clang diagnostic ignored "-Wmissing-prototypes" // for stb_textedit.h
|
||||||
|
#pragma clang diagnostic ignored "-Wold-style-cast"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Forward Declarations
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
struct ImRect;
|
||||||
|
struct ImGuiColMod;
|
||||||
|
struct ImGuiStyleMod;
|
||||||
|
struct ImGuiGroupData;
|
||||||
|
struct ImGuiSimpleColumns;
|
||||||
|
struct ImGuiDrawContext;
|
||||||
|
struct ImGuiTextEditState;
|
||||||
|
struct ImGuiIniData;
|
||||||
|
struct ImGuiMouseCursorData;
|
||||||
|
struct ImGuiPopupRef;
|
||||||
|
struct ImGuiWindow;
|
||||||
|
|
||||||
|
typedef int ImGuiLayoutType; // enum ImGuiLayoutType_
|
||||||
|
typedef int ImGuiButtonFlags; // enum ImGuiButtonFlags_
|
||||||
|
typedef int ImGuiTreeNodeFlags; // enum ImGuiTreeNodeFlags_
|
||||||
|
typedef int ImGuiSliderFlags; // enum ImGuiSliderFlags_
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
// STB libraries
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace ImGuiStb
|
||||||
|
{
|
||||||
|
|
||||||
|
#undef STB_TEXTEDIT_STRING
|
||||||
|
#undef STB_TEXTEDIT_CHARTYPE
|
||||||
|
#define STB_TEXTEDIT_STRING ImGuiTextEditState
|
||||||
|
#define STB_TEXTEDIT_CHARTYPE ImWchar
|
||||||
|
#define STB_TEXTEDIT_GETWIDTH_NEWLINE -1.0f
|
||||||
|
#include "stb_textedit.h"
|
||||||
|
|
||||||
|
} // namespace ImGuiStb
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Context
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef GImGui
|
||||||
|
extern IMGUI_API ImGuiContext* GImGui; // Current implicit ImGui context pointer
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Helpers
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR)))
|
||||||
|
#define IM_PI 3.14159265358979323846f
|
||||||
|
#define IM_OFFSETOF(_TYPE,_ELM) ((size_t)&(((_TYPE*)0)->_ELM))
|
||||||
|
|
||||||
|
// Helpers: UTF-8 <> wchar
|
||||||
|
IMGUI_API int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count
|
||||||
|
IMGUI_API int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end); // return input UTF-8 bytes count
|
||||||
|
IMGUI_API int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_remaining = NULL); // return input UTF-8 bytes count
|
||||||
|
IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count)
|
||||||
|
IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string as UTF-8 code-points
|
||||||
|
|
||||||
|
// Helpers: Misc
|
||||||
|
IMGUI_API ImU32 ImHash(const void* data, int data_size, ImU32 seed = 0); // Pass data_size==0 for zero-terminated strings
|
||||||
|
IMGUI_API void* ImLoadFileToMemory(const char* filename, const char* file_open_mode, int* out_file_size = NULL, int padding_bytes = 0);
|
||||||
|
IMGUI_API FILE* ImOpenFile(const char* filename, const char* file_open_mode);
|
||||||
|
IMGUI_API bool ImIsPointInTriangle(const ImVec2& p, const ImVec2& a, const ImVec2& b, const ImVec2& c);
|
||||||
|
static inline bool ImCharIsSpace(int c) { return c == ' ' || c == '\t' || c == 0x3000; }
|
||||||
|
static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; }
|
||||||
|
|
||||||
|
// Helpers: String
|
||||||
|
IMGUI_API int ImStricmp(const char* str1, const char* str2);
|
||||||
|
IMGUI_API int ImStrnicmp(const char* str1, const char* str2, int count);
|
||||||
|
IMGUI_API char* ImStrdup(const char* str);
|
||||||
|
IMGUI_API int ImStrlenW(const ImWchar* str);
|
||||||
|
IMGUI_API const ImWchar*ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin); // Find beginning-of-line
|
||||||
|
IMGUI_API const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end);
|
||||||
|
IMGUI_API int ImFormatString(char* buf, int buf_size, const char* fmt, ...) IM_PRINTFARGS(3);
|
||||||
|
IMGUI_API int ImFormatStringV(char* buf, int buf_size, const char* fmt, va_list args);
|
||||||
|
|
||||||
|
// Helpers: Math
|
||||||
|
// We are keeping those not leaking to the user by default, in the case the user has implicit cast operators between ImVec2 and its own types (when IM_VEC2_CLASS_EXTRA is defined)
|
||||||
|
#ifdef IMGUI_DEFINE_MATH_OPERATORS
|
||||||
|
static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x*rhs, lhs.y*rhs); }
|
||||||
|
static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x/rhs, lhs.y/rhs); }
|
||||||
|
static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x+rhs.x, lhs.y+rhs.y); }
|
||||||
|
static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x-rhs.x, lhs.y-rhs.y); }
|
||||||
|
static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x*rhs.x, lhs.y*rhs.y); }
|
||||||
|
static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x/rhs.x, lhs.y/rhs.y); }
|
||||||
|
static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; }
|
||||||
|
static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; }
|
||||||
|
static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; }
|
||||||
|
static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; }
|
||||||
|
static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x-rhs.x, lhs.y-rhs.y, lhs.z-rhs.z, lhs.w-rhs.w); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static inline int ImMin(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
|
||||||
|
static inline int ImMax(int lhs, int rhs) { return lhs >= rhs ? lhs : rhs; }
|
||||||
|
static inline float ImMin(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
|
||||||
|
static inline float ImMax(float lhs, float rhs) { return lhs >= rhs ? lhs : rhs; }
|
||||||
|
static inline ImVec2 ImMin(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(ImMin(lhs.x,rhs.x), ImMin(lhs.y,rhs.y)); }
|
||||||
|
static inline ImVec2 ImMax(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(ImMax(lhs.x,rhs.x), ImMax(lhs.y,rhs.y)); }
|
||||||
|
static inline int ImClamp(int v, int mn, int mx) { return (v < mn) ? mn : (v > mx) ? mx : v; }
|
||||||
|
static inline float ImClamp(float v, float mn, float mx) { return (v < mn) ? mn : (v > mx) ? mx : v; }
|
||||||
|
static inline ImVec2 ImClamp(const ImVec2& f, const ImVec2& mn, ImVec2 mx) { return ImVec2(ImClamp(f.x,mn.x,mx.x), ImClamp(f.y,mn.y,mx.y)); }
|
||||||
|
static inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; }
|
||||||
|
static inline float ImLerp(float a, float b, float t) { return a + (b - a) * t; }
|
||||||
|
static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); }
|
||||||
|
static inline float ImLengthSqr(const ImVec2& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y; }
|
||||||
|
static inline float ImLengthSqr(const ImVec4& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y + lhs.z*lhs.z + lhs.w*lhs.w; }
|
||||||
|
static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = lhs.x*lhs.x + lhs.y*lhs.y; if (d > 0.0f) return 1.0f / sqrtf(d); return fail_value; }
|
||||||
|
static inline float ImFloor(float f) { return (float)(int)f; }
|
||||||
|
static inline ImVec2 ImFloor(ImVec2 v) { return ImVec2((float)(int)v.x, (float)(int)v.y); }
|
||||||
|
|
||||||
|
// We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax.
|
||||||
|
// Defining a custom placement new() with a dummy parameter allows us to bypass including <new> which on some platforms complains when user has disabled exceptions.
|
||||||
|
#ifdef IMGUI_DEFINE_PLACEMENT_NEW
|
||||||
|
struct ImPlacementNewDummy {};
|
||||||
|
inline void* operator new(size_t, ImPlacementNewDummy, void* ptr) { return ptr; }
|
||||||
|
inline void operator delete(void*, ImPlacementNewDummy, void*) {}
|
||||||
|
#define IM_PLACEMENT_NEW(_PTR) new(ImPlacementNewDummy(), _PTR)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Types
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
enum ImGuiButtonFlags_
|
||||||
|
{
|
||||||
|
ImGuiButtonFlags_Repeat = 1 << 0, // hold to repeat
|
||||||
|
ImGuiButtonFlags_PressedOnClickRelease = 1 << 1, // (default) return pressed on click+release on same item (default if no PressedOn** flag is set)
|
||||||
|
ImGuiButtonFlags_PressedOnClick = 1 << 2, // return pressed on click (default requires click+release)
|
||||||
|
ImGuiButtonFlags_PressedOnRelease = 1 << 3, // return pressed on release (default requires click+release)
|
||||||
|
ImGuiButtonFlags_PressedOnDoubleClick = 1 << 4, // return pressed on double-click (default requires click+release)
|
||||||
|
ImGuiButtonFlags_FlattenChilds = 1 << 5, // allow interaction even if a child window is overlapping
|
||||||
|
ImGuiButtonFlags_DontClosePopups = 1 << 6, // disable automatically closing parent popup on press
|
||||||
|
ImGuiButtonFlags_Disabled = 1 << 7, // disable interaction
|
||||||
|
ImGuiButtonFlags_AlignTextBaseLine = 1 << 8, // vertically align button to match text baseline - ButtonEx() only
|
||||||
|
ImGuiButtonFlags_NoKeyModifiers = 1 << 9, // disable interaction if a key modifier is held
|
||||||
|
ImGuiButtonFlags_AllowOverlapMode = 1 << 10 // require previous frame HoveredId to either match id or be null before being usable
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImGuiSliderFlags_
|
||||||
|
{
|
||||||
|
ImGuiSliderFlags_Vertical = 1 << 0
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImGuiSelectableFlagsPrivate_
|
||||||
|
{
|
||||||
|
// NB: need to be in sync with last value of ImGuiSelectableFlags_
|
||||||
|
ImGuiSelectableFlags_Menu = 1 << 3,
|
||||||
|
ImGuiSelectableFlags_MenuItem = 1 << 4,
|
||||||
|
ImGuiSelectableFlags_Disabled = 1 << 5,
|
||||||
|
ImGuiSelectableFlags_DrawFillAvailWidth = 1 << 6
|
||||||
|
};
|
||||||
|
|
||||||
|
// FIXME: this is in development, not exposed/functional as a generic feature yet.
|
||||||
|
enum ImGuiLayoutType_
|
||||||
|
{
|
||||||
|
ImGuiLayoutType_Vertical,
|
||||||
|
ImGuiLayoutType_Horizontal
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImGuiPlotType
|
||||||
|
{
|
||||||
|
ImGuiPlotType_Lines,
|
||||||
|
ImGuiPlotType_Histogram
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImGuiDataType
|
||||||
|
{
|
||||||
|
ImGuiDataType_Int,
|
||||||
|
ImGuiDataType_Float,
|
||||||
|
ImGuiDataType_Float2,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ImGuiCorner
|
||||||
|
{
|
||||||
|
ImGuiCorner_TopLeft = 1 << 0, // 1
|
||||||
|
ImGuiCorner_TopRight = 1 << 1, // 2
|
||||||
|
ImGuiCorner_BottomRight = 1 << 2, // 4
|
||||||
|
ImGuiCorner_BottomLeft = 1 << 3, // 8
|
||||||
|
ImGuiCorner_All = 0x0F
|
||||||
|
};
|
||||||
|
|
||||||
|
// 2D axis aligned bounding-box
|
||||||
|
// NB: we can't rely on ImVec2 math operators being available here
|
||||||
|
struct IMGUI_API ImRect
|
||||||
|
{
|
||||||
|
ImVec2 Min; // Upper-left
|
||||||
|
ImVec2 Max; // Lower-right
|
||||||
|
|
||||||
|
ImRect() : Min(FLT_MAX,FLT_MAX), Max(-FLT_MAX,-FLT_MAX) {}
|
||||||
|
ImRect(const ImVec2& min, const ImVec2& max) : Min(min), Max(max) {}
|
||||||
|
ImRect(const ImVec4& v) : Min(v.x, v.y), Max(v.z, v.w) {}
|
||||||
|
ImRect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {}
|
||||||
|
|
||||||
|
ImVec2 GetCenter() const { return ImVec2((Min.x+Max.x)*0.5f, (Min.y+Max.y)*0.5f); }
|
||||||
|
ImVec2 GetSize() const { return ImVec2(Max.x-Min.x, Max.y-Min.y); }
|
||||||
|
float GetWidth() const { return Max.x-Min.x; }
|
||||||
|
float GetHeight() const { return Max.y-Min.y; }
|
||||||
|
ImVec2 GetTL() const { return Min; } // Top-left
|
||||||
|
ImVec2 GetTR() const { return ImVec2(Max.x, Min.y); } // Top-right
|
||||||
|
ImVec2 GetBL() const { return ImVec2(Min.x, Max.y); } // Bottom-left
|
||||||
|
ImVec2 GetBR() const { return Max; } // Bottom-right
|
||||||
|
bool Contains(const ImVec2& p) const { return p.x >= Min.x && p.y >= Min.y && p.x < Max.x && p.y < Max.y; }
|
||||||
|
bool Contains(const ImRect& r) const { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x < Max.x && r.Max.y < Max.y; }
|
||||||
|
bool Overlaps(const ImRect& r) const { return r.Min.y < Max.y && r.Max.y > Min.y && r.Min.x < Max.x && r.Max.x > Min.x; }
|
||||||
|
void Add(const ImVec2& rhs) { if (Min.x > rhs.x) Min.x = rhs.x; if (Min.y > rhs.y) Min.y = rhs.y; if (Max.x < rhs.x) Max.x = rhs.x; if (Max.y < rhs.y) Max.y = rhs.y; }
|
||||||
|
void Add(const ImRect& rhs) { if (Min.x > rhs.Min.x) Min.x = rhs.Min.x; if (Min.y > rhs.Min.y) Min.y = rhs.Min.y; if (Max.x < rhs.Max.x) Max.x = rhs.Max.x; if (Max.y < rhs.Max.y) Max.y = rhs.Max.y; }
|
||||||
|
void Expand(const float amount) { Min.x -= amount; Min.y -= amount; Max.x += amount; Max.y += amount; }
|
||||||
|
void Expand(const ImVec2& amount) { Min.x -= amount.x; Min.y -= amount.y; Max.x += amount.x; Max.y += amount.y; }
|
||||||
|
void Reduce(const ImVec2& amount) { Min.x += amount.x; Min.y += amount.y; Max.x -= amount.x; Max.y -= amount.y; }
|
||||||
|
void Clip(const ImRect& clip) { if (Min.x < clip.Min.x) Min.x = clip.Min.x; if (Min.y < clip.Min.y) Min.y = clip.Min.y; if (Max.x > clip.Max.x) Max.x = clip.Max.x; if (Max.y > clip.Max.y) Max.y = clip.Max.y; }
|
||||||
|
void Floor() { Min.x = (float)(int)Min.x; Min.y = (float)(int)Min.y; Max.x = (float)(int)Max.x; Max.y = (float)(int)Max.y; }
|
||||||
|
ImVec2 GetClosestPoint(ImVec2 p, bool on_edge) const
|
||||||
|
{
|
||||||
|
if (!on_edge && Contains(p))
|
||||||
|
return p;
|
||||||
|
if (p.x > Max.x) p.x = Max.x;
|
||||||
|
else if (p.x < Min.x) p.x = Min.x;
|
||||||
|
if (p.y > Max.y) p.y = Max.y;
|
||||||
|
else if (p.y < Min.y) p.y = Min.y;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Stacked color modifier, backup of modified data so we can restore it
|
||||||
|
struct ImGuiColMod
|
||||||
|
{
|
||||||
|
ImGuiCol Col;
|
||||||
|
ImVec4 BackupValue;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Stacked style modifier, backup of modified data so we can restore it. Data type inferred from the variable.
|
||||||
|
struct ImGuiStyleMod
|
||||||
|
{
|
||||||
|
ImGuiStyleVar VarIdx;
|
||||||
|
union { int BackupInt[2]; float BackupFloat[2]; };
|
||||||
|
ImGuiStyleMod(ImGuiStyleVar idx, int v) { VarIdx = idx; BackupInt[0] = v; }
|
||||||
|
ImGuiStyleMod(ImGuiStyleVar idx, float v) { VarIdx = idx; BackupFloat[0] = v; }
|
||||||
|
ImGuiStyleMod(ImGuiStyleVar idx, ImVec2 v) { VarIdx = idx; BackupFloat[0] = v.x; BackupFloat[1] = v.y; }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Stacked data for BeginGroup()/EndGroup()
|
||||||
|
struct ImGuiGroupData
|
||||||
|
{
|
||||||
|
ImVec2 BackupCursorPos;
|
||||||
|
ImVec2 BackupCursorMaxPos;
|
||||||
|
float BackupIndentX;
|
||||||
|
float BackupGroupOffsetX;
|
||||||
|
float BackupCurrentLineHeight;
|
||||||
|
float BackupCurrentLineTextBaseOffset;
|
||||||
|
float BackupLogLinePosY;
|
||||||
|
bool BackupActiveIdIsAlive;
|
||||||
|
bool AdvanceCursor;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Per column data for Columns()
|
||||||
|
struct ImGuiColumnData
|
||||||
|
{
|
||||||
|
float OffsetNorm; // Column start offset, normalized 0.0 (far left) -> 1.0 (far right)
|
||||||
|
//float IndentX;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Simple column measurement currently used for MenuItem() only. This is very short-sighted/throw-away code and NOT a generic helper.
|
||||||
|
struct IMGUI_API ImGuiSimpleColumns
|
||||||
|
{
|
||||||
|
int Count;
|
||||||
|
float Spacing;
|
||||||
|
float Width, NextWidth;
|
||||||
|
float Pos[8], NextWidths[8];
|
||||||
|
|
||||||
|
ImGuiSimpleColumns();
|
||||||
|
void Update(int count, float spacing, bool clear);
|
||||||
|
float DeclColumns(float w0, float w1, float w2);
|
||||||
|
float CalcExtraSpace(float avail_w);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Internal state of the currently focused/edited text input box
|
||||||
|
struct IMGUI_API ImGuiTextEditState
|
||||||
|
{
|
||||||
|
ImGuiID Id; // widget id owning the text state
|
||||||
|
ImVector<ImWchar> Text; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer.
|
||||||
|
ImVector<char> InitialText; // backup of end-user buffer at the time of focus (in UTF-8, unaltered)
|
||||||
|
ImVector<char> TempTextBuffer;
|
||||||
|
int CurLenA, CurLenW; // we need to maintain our buffer length in both UTF-8 and wchar format.
|
||||||
|
int BufSizeA; // end-user buffer size
|
||||||
|
float ScrollX;
|
||||||
|
ImGuiStb::STB_TexteditState StbState;
|
||||||
|
float CursorAnim;
|
||||||
|
bool CursorFollow;
|
||||||
|
bool SelectedAllMouseLock;
|
||||||
|
|
||||||
|
ImGuiTextEditState() { memset(this, 0, sizeof(*this)); }
|
||||||
|
void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking
|
||||||
|
void CursorClamp() { StbState.cursor = ImMin(StbState.cursor, CurLenW); StbState.select_start = ImMin(StbState.select_start, CurLenW); StbState.select_end = ImMin(StbState.select_end, CurLenW); }
|
||||||
|
bool HasSelection() const { return StbState.select_start != StbState.select_end; }
|
||||||
|
void ClearSelection() { StbState.select_start = StbState.select_end = StbState.cursor; }
|
||||||
|
void SelectAll() { StbState.select_start = 0; StbState.select_end = CurLenW; StbState.cursor = StbState.select_end; StbState.has_preferred_x = false; }
|
||||||
|
void OnKeyPressed(int key);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Data saved in imgui.ini file
|
||||||
|
struct ImGuiIniData
|
||||||
|
{
|
||||||
|
char* Name;
|
||||||
|
ImGuiID Id;
|
||||||
|
ImVec2 Pos;
|
||||||
|
ImVec2 Size;
|
||||||
|
bool Collapsed;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Mouse cursor data (used when io.MouseDrawCursor is set)
|
||||||
|
struct ImGuiMouseCursorData
|
||||||
|
{
|
||||||
|
ImGuiMouseCursor Type;
|
||||||
|
ImVec2 HotOffset;
|
||||||
|
ImVec2 Size;
|
||||||
|
ImVec2 TexUvMin[2];
|
||||||
|
ImVec2 TexUvMax[2];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Storage for current popup stack
|
||||||
|
struct ImGuiPopupRef
|
||||||
|
{
|
||||||
|
ImGuiID PopupId; // Set on OpenPopup()
|
||||||
|
ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup()
|
||||||
|
ImGuiWindow* ParentWindow; // Set on OpenPopup()
|
||||||
|
ImGuiID ParentMenuSet; // Set on OpenPopup()
|
||||||
|
ImVec2 MousePosOnOpen; // Copy of mouse position at the time of opening popup
|
||||||
|
|
||||||
|
ImGuiPopupRef(ImGuiID id, ImGuiWindow* parent_window, ImGuiID parent_menu_set, const ImVec2& mouse_pos) { PopupId = id; Window = NULL; ParentWindow = parent_window; ParentMenuSet = parent_menu_set; MousePosOnOpen = mouse_pos; }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Main state for ImGui
|
||||||
|
struct ImGuiContext
|
||||||
|
{
|
||||||
|
bool Initialized;
|
||||||
|
ImGuiIO IO;
|
||||||
|
ImGuiStyle Style;
|
||||||
|
ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back()
|
||||||
|
float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize()
|
||||||
|
float FontBaseSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Size of characters.
|
||||||
|
ImVec2 FontTexUvWhitePixel; // (Shortcut) == Font->TexUvWhitePixel
|
||||||
|
|
||||||
|
float Time;
|
||||||
|
int FrameCount;
|
||||||
|
int FrameCountEnded;
|
||||||
|
int FrameCountRendered;
|
||||||
|
ImVector<ImGuiWindow*> Windows;
|
||||||
|
ImVector<ImGuiWindow*> WindowsSortBuffer;
|
||||||
|
ImGuiWindow* CurrentWindow; // Being drawn into
|
||||||
|
ImVector<ImGuiWindow*> CurrentWindowStack;
|
||||||
|
ImGuiWindow* FocusedWindow; // Will catch keyboard inputs
|
||||||
|
ImGuiWindow* HoveredWindow; // Will catch mouse inputs
|
||||||
|
ImGuiWindow* HoveredRootWindow; // Will catch mouse inputs (for focus/move only)
|
||||||
|
ImGuiID HoveredId; // Hovered widget
|
||||||
|
bool HoveredIdAllowOverlap;
|
||||||
|
ImGuiID HoveredIdPreviousFrame;
|
||||||
|
ImGuiID ActiveId; // Active widget
|
||||||
|
ImGuiID ActiveIdPreviousFrame;
|
||||||
|
bool ActiveIdIsAlive;
|
||||||
|
bool ActiveIdIsJustActivated; // Set at the time of activation for one frame
|
||||||
|
bool ActiveIdAllowOverlap; // Set only by active widget
|
||||||
|
ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior)
|
||||||
|
ImGuiWindow* ActiveIdWindow;
|
||||||
|
ImGuiWindow* MovedWindow; // Track the child window we clicked on to move a window.
|
||||||
|
ImGuiID MovedWindowMoveId; // == MovedWindow->RootWindow->MoveId
|
||||||
|
ImVector<ImGuiIniData> Settings; // .ini Settings
|
||||||
|
float SettingsDirtyTimer; // Save .ini Settings on disk when time reaches zero
|
||||||
|
ImVector<ImGuiColMod> ColorModifiers; // Stack for PushStyleColor()/PopStyleColor()
|
||||||
|
ImVector<ImGuiStyleMod> StyleModifiers; // Stack for PushStyleVar()/PopStyleVar()
|
||||||
|
ImVector<ImFont*> FontStack; // Stack for PushFont()/PopFont()
|
||||||
|
ImVector<ImGuiPopupRef> OpenPopupStack; // Which popups are open (persistent)
|
||||||
|
ImVector<ImGuiPopupRef> CurrentPopupStack; // Which level of BeginPopup() we are in (reset every frame)
|
||||||
|
|
||||||
|
// Storage for SetNexWindow** and SetNextTreeNode*** functions
|
||||||
|
ImVec2 SetNextWindowPosVal;
|
||||||
|
ImVec2 SetNextWindowSizeVal;
|
||||||
|
ImVec2 SetNextWindowContentSizeVal;
|
||||||
|
bool SetNextWindowCollapsedVal;
|
||||||
|
ImGuiSetCond SetNextWindowPosCond;
|
||||||
|
ImGuiSetCond SetNextWindowSizeCond;
|
||||||
|
ImGuiSetCond SetNextWindowContentSizeCond;
|
||||||
|
ImGuiSetCond SetNextWindowCollapsedCond;
|
||||||
|
ImRect SetNextWindowSizeConstraintRect; // Valid if 'SetNextWindowSizeConstraint' is true
|
||||||
|
ImGuiSizeConstraintCallback SetNextWindowSizeConstraintCallback;
|
||||||
|
void* SetNextWindowSizeConstraintCallbackUserData;
|
||||||
|
bool SetNextWindowSizeConstraint;
|
||||||
|
bool SetNextWindowFocus;
|
||||||
|
bool SetNextTreeNodeOpenVal;
|
||||||
|
ImGuiSetCond SetNextTreeNodeOpenCond;
|
||||||
|
|
||||||
|
// Render
|
||||||
|
ImDrawData RenderDrawData; // Main ImDrawData instance to pass render information to the user
|
||||||
|
ImVector<ImDrawList*> RenderDrawLists[3];
|
||||||
|
float ModalWindowDarkeningRatio;
|
||||||
|
ImDrawList OverlayDrawList; // Optional software render of mouse cursors, if io.MouseDrawCursor is set + a few debug overlays
|
||||||
|
ImGuiMouseCursor MouseCursor;
|
||||||
|
ImGuiMouseCursorData MouseCursorData[ImGuiMouseCursor_Count_];
|
||||||
|
|
||||||
|
// Widget state
|
||||||
|
ImGuiTextEditState InputTextState;
|
||||||
|
ImFont InputTextPasswordFont;
|
||||||
|
ImGuiID ScalarAsInputTextId; // Temporary text input when CTRL+clicking on a slider, etc.
|
||||||
|
ImGuiStorage ColorEditModeStorage; // Store user selection of color edit mode
|
||||||
|
float DragCurrentValue; // Currently dragged value, always float, not rounded by end-user precision settings
|
||||||
|
ImVec2 DragLastMouseDelta;
|
||||||
|
float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio
|
||||||
|
float DragSpeedScaleSlow;
|
||||||
|
float DragSpeedScaleFast;
|
||||||
|
ImVec2 ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage?
|
||||||
|
char Tooltip[1024];
|
||||||
|
char* PrivateClipboard; // If no custom clipboard handler is defined
|
||||||
|
ImVec2 OsImePosRequest, OsImePosSet; // Cursor position request & last passed to the OS Input Method Editor
|
||||||
|
|
||||||
|
// Logging
|
||||||
|
bool LogEnabled;
|
||||||
|
FILE* LogFile; // If != NULL log to stdout/ file
|
||||||
|
ImGuiTextBuffer* LogClipboard; // Else log to clipboard. This is pointer so our GImGui static constructor doesn't call heap allocators.
|
||||||
|
int LogStartDepth;
|
||||||
|
int LogAutoExpandMaxDepth;
|
||||||
|
|
||||||
|
// Misc
|
||||||
|
float FramerateSecPerFrame[120]; // calculate estimate of framerate for user
|
||||||
|
int FramerateSecPerFrameIdx;
|
||||||
|
float FramerateSecPerFrameAccum;
|
||||||
|
int CaptureMouseNextFrame; // explicit capture via CaptureInputs() sets those flags
|
||||||
|
int CaptureKeyboardNextFrame;
|
||||||
|
char TempBuffer[1024*3+1]; // temporary text buffer
|
||||||
|
|
||||||
|
ImGuiContext()
|
||||||
|
{
|
||||||
|
Initialized = false;
|
||||||
|
Font = NULL;
|
||||||
|
FontSize = FontBaseSize = 0.0f;
|
||||||
|
FontTexUvWhitePixel = ImVec2(0.0f, 0.0f);
|
||||||
|
|
||||||
|
Time = 0.0f;
|
||||||
|
FrameCount = 0;
|
||||||
|
FrameCountEnded = FrameCountRendered = -1;
|
||||||
|
CurrentWindow = NULL;
|
||||||
|
FocusedWindow = NULL;
|
||||||
|
HoveredWindow = NULL;
|
||||||
|
HoveredRootWindow = NULL;
|
||||||
|
HoveredId = 0;
|
||||||
|
HoveredIdAllowOverlap = false;
|
||||||
|
HoveredIdPreviousFrame = 0;
|
||||||
|
ActiveId = 0;
|
||||||
|
ActiveIdPreviousFrame = 0;
|
||||||
|
ActiveIdIsAlive = false;
|
||||||
|
ActiveIdIsJustActivated = false;
|
||||||
|
ActiveIdAllowOverlap = false;
|
||||||
|
ActiveIdClickOffset = ImVec2(-1,-1);
|
||||||
|
ActiveIdWindow = NULL;
|
||||||
|
MovedWindow = NULL;
|
||||||
|
MovedWindowMoveId = 0;
|
||||||
|
SettingsDirtyTimer = 0.0f;
|
||||||
|
|
||||||
|
SetNextWindowPosVal = ImVec2(0.0f, 0.0f);
|
||||||
|
SetNextWindowSizeVal = ImVec2(0.0f, 0.0f);
|
||||||
|
SetNextWindowCollapsedVal = false;
|
||||||
|
SetNextWindowPosCond = 0;
|
||||||
|
SetNextWindowSizeCond = 0;
|
||||||
|
SetNextWindowContentSizeCond = 0;
|
||||||
|
SetNextWindowCollapsedCond = 0;
|
||||||
|
SetNextWindowSizeConstraintRect = ImRect();
|
||||||
|
SetNextWindowSizeConstraintCallback = NULL;
|
||||||
|
SetNextWindowSizeConstraintCallbackUserData = NULL;
|
||||||
|
SetNextWindowSizeConstraint = false;
|
||||||
|
SetNextWindowFocus = false;
|
||||||
|
SetNextTreeNodeOpenVal = false;
|
||||||
|
SetNextTreeNodeOpenCond = 0;
|
||||||
|
|
||||||
|
ScalarAsInputTextId = 0;
|
||||||
|
DragCurrentValue = 0.0f;
|
||||||
|
DragLastMouseDelta = ImVec2(0.0f, 0.0f);
|
||||||
|
DragSpeedDefaultRatio = 1.0f / 100.0f;
|
||||||
|
DragSpeedScaleSlow = 0.01f;
|
||||||
|
DragSpeedScaleFast = 10.0f;
|
||||||
|
ScrollbarClickDeltaToGrabCenter = ImVec2(0.0f, 0.0f);
|
||||||
|
memset(Tooltip, 0, sizeof(Tooltip));
|
||||||
|
PrivateClipboard = NULL;
|
||||||
|
OsImePosRequest = OsImePosSet = ImVec2(-1.0f, -1.0f);
|
||||||
|
|
||||||
|
ModalWindowDarkeningRatio = 0.0f;
|
||||||
|
OverlayDrawList._OwnerName = "##Overlay"; // Give it a name for debugging
|
||||||
|
MouseCursor = ImGuiMouseCursor_Arrow;
|
||||||
|
memset(MouseCursorData, 0, sizeof(MouseCursorData));
|
||||||
|
|
||||||
|
LogEnabled = false;
|
||||||
|
LogFile = NULL;
|
||||||
|
LogClipboard = NULL;
|
||||||
|
LogStartDepth = 0;
|
||||||
|
LogAutoExpandMaxDepth = 2;
|
||||||
|
|
||||||
|
memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame));
|
||||||
|
FramerateSecPerFrameIdx = 0;
|
||||||
|
FramerateSecPerFrameAccum = 0.0f;
|
||||||
|
CaptureMouseNextFrame = CaptureKeyboardNextFrame = -1;
|
||||||
|
memset(TempBuffer, 0, sizeof(TempBuffer));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Transient per-window data, reset at the beginning of the frame
|
||||||
|
// FIXME: That's theory, in practice the delimitation between ImGuiWindow and ImGuiDrawContext is quite tenuous and could be reconsidered.
|
||||||
|
struct IMGUI_API ImGuiDrawContext
|
||||||
|
{
|
||||||
|
ImVec2 CursorPos;
|
||||||
|
ImVec2 CursorPosPrevLine;
|
||||||
|
ImVec2 CursorStartPos;
|
||||||
|
ImVec2 CursorMaxPos; // Implicitly calculate the size of our contents, always extending. Saved into window->SizeContents at the end of the frame
|
||||||
|
float CurrentLineHeight;
|
||||||
|
float CurrentLineTextBaseOffset;
|
||||||
|
float PrevLineHeight;
|
||||||
|
float PrevLineTextBaseOffset;
|
||||||
|
float LogLinePosY;
|
||||||
|
int TreeDepth;
|
||||||
|
ImGuiID LastItemId;
|
||||||
|
ImRect LastItemRect;
|
||||||
|
bool LastItemHoveredAndUsable; // Item rectangle is hovered, and its window is currently interactable with (not blocked by a popup preventing access to the window)
|
||||||
|
bool LastItemHoveredRect; // Item rectangle is hovered, but its window may or not be currently interactable with (might be blocked by a popup preventing access to the window)
|
||||||
|
bool MenuBarAppending;
|
||||||
|
float MenuBarOffsetX;
|
||||||
|
ImVector<ImGuiWindow*> ChildWindows;
|
||||||
|
ImGuiStorage* StateStorage;
|
||||||
|
ImGuiLayoutType LayoutType;
|
||||||
|
|
||||||
|
// We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings.
|
||||||
|
float ItemWidth; // == ItemWidthStack.back(). 0.0: default, >0.0: width in pixels, <0.0: align xx pixels to the right of window
|
||||||
|
float TextWrapPos; // == TextWrapPosStack.back() [empty == -1.0f]
|
||||||
|
bool AllowKeyboardFocus; // == AllowKeyboardFocusStack.back() [empty == true]
|
||||||
|
bool ButtonRepeat; // == ButtonRepeatStack.back() [empty == false]
|
||||||
|
ImVector<float> ItemWidthStack;
|
||||||
|
ImVector<float> TextWrapPosStack;
|
||||||
|
ImVector<bool> AllowKeyboardFocusStack;
|
||||||
|
ImVector<bool> ButtonRepeatStack;
|
||||||
|
ImVector<ImGuiGroupData>GroupStack;
|
||||||
|
ImGuiColorEditMode ColorEditMode;
|
||||||
|
int StackSizesBackup[6]; // Store size of various stacks for asserting
|
||||||
|
|
||||||
|
float IndentX; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.)
|
||||||
|
float GroupOffsetX;
|
||||||
|
float ColumnsOffsetX; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API.
|
||||||
|
int ColumnsCurrent;
|
||||||
|
int ColumnsCount;
|
||||||
|
float ColumnsMinX;
|
||||||
|
float ColumnsMaxX;
|
||||||
|
float ColumnsStartPosY;
|
||||||
|
float ColumnsCellMinY;
|
||||||
|
float ColumnsCellMaxY;
|
||||||
|
bool ColumnsShowBorders;
|
||||||
|
ImGuiID ColumnsSetId;
|
||||||
|
ImVector<ImGuiColumnData> ColumnsData;
|
||||||
|
|
||||||
|
ImGuiDrawContext()
|
||||||
|
{
|
||||||
|
CursorPos = CursorPosPrevLine = CursorStartPos = CursorMaxPos = ImVec2(0.0f, 0.0f);
|
||||||
|
CurrentLineHeight = PrevLineHeight = 0.0f;
|
||||||
|
CurrentLineTextBaseOffset = PrevLineTextBaseOffset = 0.0f;
|
||||||
|
LogLinePosY = -1.0f;
|
||||||
|
TreeDepth = 0;
|
||||||
|
LastItemId = 0;
|
||||||
|
LastItemRect = ImRect(0.0f,0.0f,0.0f,0.0f);
|
||||||
|
LastItemHoveredAndUsable = LastItemHoveredRect = false;
|
||||||
|
MenuBarAppending = false;
|
||||||
|
MenuBarOffsetX = 0.0f;
|
||||||
|
StateStorage = NULL;
|
||||||
|
LayoutType = ImGuiLayoutType_Vertical;
|
||||||
|
ItemWidth = 0.0f;
|
||||||
|
ButtonRepeat = false;
|
||||||
|
AllowKeyboardFocus = true;
|
||||||
|
TextWrapPos = -1.0f;
|
||||||
|
ColorEditMode = ImGuiColorEditMode_RGB;
|
||||||
|
memset(StackSizesBackup, 0, sizeof(StackSizesBackup));
|
||||||
|
|
||||||
|
IndentX = 0.0f;
|
||||||
|
GroupOffsetX = 0.0f;
|
||||||
|
ColumnsOffsetX = 0.0f;
|
||||||
|
ColumnsCurrent = 0;
|
||||||
|
ColumnsCount = 1;
|
||||||
|
ColumnsMinX = ColumnsMaxX = 0.0f;
|
||||||
|
ColumnsStartPosY = 0.0f;
|
||||||
|
ColumnsCellMinY = ColumnsCellMaxY = 0.0f;
|
||||||
|
ColumnsShowBorders = true;
|
||||||
|
ColumnsSetId = 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Windows data
|
||||||
|
struct IMGUI_API ImGuiWindow
|
||||||
|
{
|
||||||
|
char* Name;
|
||||||
|
ImGuiID ID; // == ImHash(Name)
|
||||||
|
ImGuiWindowFlags Flags; // See enum ImGuiWindowFlags_
|
||||||
|
int IndexWithinParent; // Order within immediate parent window, if we are a child window. Otherwise 0.
|
||||||
|
ImVec2 PosFloat;
|
||||||
|
ImVec2 Pos; // Position rounded-up to nearest pixel
|
||||||
|
ImVec2 Size; // Current size (==SizeFull or collapsed title bar size)
|
||||||
|
ImVec2 SizeFull; // Size when non collapsed
|
||||||
|
ImVec2 SizeContents; // Size of contents (== extents reach of the drawing cursor) from previous frame
|
||||||
|
ImVec2 SizeContentsExplicit; // Size of contents explicitly set by the user via SetNextWindowContentSize()
|
||||||
|
ImRect ContentsRegionRect; // Maximum visible content position in window coordinates. ~~ (SizeContentsExplicit ? SizeContentsExplicit : Size - ScrollbarSizes) - CursorStartPos, per axis
|
||||||
|
ImVec2 WindowPadding; // Window padding at the time of begin. We need to lock it, in particular manipulation of the ShowBorder would have an effect
|
||||||
|
ImGuiID MoveId; // == window->GetID("#MOVE")
|
||||||
|
ImVec2 Scroll;
|
||||||
|
ImVec2 ScrollTarget; // target scroll position. stored as cursor position with scrolling canceled out, so the highest point is always 0.0f. (FLT_MAX for no change)
|
||||||
|
ImVec2 ScrollTargetCenterRatio; // 0.0f = scroll so that target position is at top, 0.5f = scroll so that target position is centered
|
||||||
|
bool ScrollbarX, ScrollbarY;
|
||||||
|
ImVec2 ScrollbarSizes;
|
||||||
|
float BorderSize;
|
||||||
|
bool Active; // Set to true on Begin()
|
||||||
|
bool WasActive;
|
||||||
|
bool Accessed; // Set to true when any widget access the current window
|
||||||
|
bool Collapsed; // Set when collapsing window to become only title-bar
|
||||||
|
bool SkipItems; // == Visible && !Collapsed
|
||||||
|
int BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs)
|
||||||
|
ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling)
|
||||||
|
int AutoFitFramesX, AutoFitFramesY;
|
||||||
|
bool AutoFitOnlyGrows;
|
||||||
|
int AutoPosLastDirection;
|
||||||
|
int HiddenFrames;
|
||||||
|
int SetWindowPosAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowPos() call will succeed with this particular flag.
|
||||||
|
int SetWindowSizeAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowSize() call will succeed with this particular flag.
|
||||||
|
int SetWindowCollapsedAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowCollapsed() call will succeed with this particular flag.
|
||||||
|
bool SetWindowPosCenterWanted;
|
||||||
|
|
||||||
|
ImGuiDrawContext DC; // Temporary per-window data, reset at the beginning of the frame
|
||||||
|
ImVector<ImGuiID> IDStack; // ID stack. ID are hashes seeded with the value at the top of the stack
|
||||||
|
ImRect ClipRect; // = DrawList->clip_rect_stack.back(). Scissoring / clipping rectangle. x1, y1, x2, y2.
|
||||||
|
ImRect WindowRectClipped; // = WindowRect just after setup in Begin(). == window->Rect() for root window.
|
||||||
|
int LastFrameActive;
|
||||||
|
float ItemWidthDefault;
|
||||||
|
ImGuiSimpleColumns MenuColumns; // Simplified columns storage for menu items
|
||||||
|
ImGuiStorage StateStorage;
|
||||||
|
float FontWindowScale; // Scale multiplier per-window
|
||||||
|
ImDrawList* DrawList;
|
||||||
|
ImGuiWindow* RootWindow; // If we are a child window, this is pointing to the first non-child parent window. Else point to ourself.
|
||||||
|
ImGuiWindow* RootNonPopupWindow; // If we are a child window, this is pointing to the first non-child non-popup parent window. Else point to ourself.
|
||||||
|
ImGuiWindow* ParentWindow; // If we are a child window, this is pointing to our parent window. Else point to NULL.
|
||||||
|
|
||||||
|
// Navigation / Focus
|
||||||
|
int FocusIdxAllCounter; // Start at -1 and increase as assigned via FocusItemRegister()
|
||||||
|
int FocusIdxTabCounter; // (same, but only count widgets which you can Tab through)
|
||||||
|
int FocusIdxAllRequestCurrent; // Item being requested for focus
|
||||||
|
int FocusIdxTabRequestCurrent; // Tab-able item being requested for focus
|
||||||
|
int FocusIdxAllRequestNext; // Item being requested for focus, for next update (relies on layout to be stable between the frame pressing TAB and the next frame)
|
||||||
|
int FocusIdxTabRequestNext; // "
|
||||||
|
|
||||||
|
public:
|
||||||
|
ImGuiWindow(const char* name);
|
||||||
|
~ImGuiWindow();
|
||||||
|
|
||||||
|
ImGuiID GetID(const char* str, const char* str_end = NULL);
|
||||||
|
ImGuiID GetID(const void* ptr);
|
||||||
|
ImGuiID GetIDNoKeepAlive(const char* str, const char* str_end = NULL);
|
||||||
|
|
||||||
|
ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x+Size.x, Pos.y+Size.y); }
|
||||||
|
float CalcFontSize() const { return GImGui->FontBaseSize * FontWindowScale; }
|
||||||
|
float TitleBarHeight() const { return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + GImGui->Style.FramePadding.y * 2.0f; }
|
||||||
|
ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight())); }
|
||||||
|
float MenuBarHeight() const { return (Flags & ImGuiWindowFlags_MenuBar) ? CalcFontSize() + GImGui->Style.FramePadding.y * 2.0f : 0.0f; }
|
||||||
|
ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); }
|
||||||
|
};
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Internal API
|
||||||
|
// No guarantee of forward compatibility here.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace ImGui
|
||||||
|
{
|
||||||
|
// We should always have a CurrentWindow in the stack (there is an implicit "Debug" window)
|
||||||
|
// If this ever crash because g.CurrentWindow is NULL it means that either
|
||||||
|
// - ImGui::NewFrame() has never been called, which is illegal.
|
||||||
|
// - You are calling ImGui functions after ImGui::Render() and before the next ImGui::NewFrame(), which is also illegal.
|
||||||
|
inline ImGuiWindow* GetCurrentWindowRead() { ImGuiContext& g = *GImGui; return g.CurrentWindow; }
|
||||||
|
inline ImGuiWindow* GetCurrentWindow() { ImGuiContext& g = *GImGui; g.CurrentWindow->Accessed = true; return g.CurrentWindow; }
|
||||||
|
IMGUI_API ImGuiWindow* GetParentWindow();
|
||||||
|
IMGUI_API ImGuiWindow* FindWindowByName(const char* name);
|
||||||
|
IMGUI_API void FocusWindow(ImGuiWindow* window);
|
||||||
|
|
||||||
|
IMGUI_API void EndFrame(); // Ends the ImGui frame. Automatically called by Render()! you most likely don't need to ever call that yourself directly. If you don't need to render you can call EndFrame() but you'll have wasted CPU already. If you don't need to render, don't create any windows instead!
|
||||||
|
|
||||||
|
IMGUI_API void SetActiveID(ImGuiID id, ImGuiWindow* window);
|
||||||
|
IMGUI_API void SetHoveredID(ImGuiID id);
|
||||||
|
IMGUI_API void KeepAliveID(ImGuiID id);
|
||||||
|
|
||||||
|
IMGUI_API void ItemSize(const ImVec2& size, float text_offset_y = 0.0f);
|
||||||
|
IMGUI_API void ItemSize(const ImRect& bb, float text_offset_y = 0.0f);
|
||||||
|
IMGUI_API bool ItemAdd(const ImRect& bb, const ImGuiID* id);
|
||||||
|
IMGUI_API bool IsClippedEx(const ImRect& bb, const ImGuiID* id, bool clip_even_when_logged);
|
||||||
|
IMGUI_API bool IsHovered(const ImRect& bb, ImGuiID id, bool flatten_childs = false);
|
||||||
|
IMGUI_API bool FocusableItemRegister(ImGuiWindow* window, bool is_active, bool tab_stop = true); // Return true if focus is requested
|
||||||
|
IMGUI_API void FocusableItemUnregister(ImGuiWindow* window);
|
||||||
|
IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_x, float default_y);
|
||||||
|
IMGUI_API float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x);
|
||||||
|
|
||||||
|
IMGUI_API void OpenPopupEx(const char* str_id, bool reopen_existing);
|
||||||
|
|
||||||
|
// NB: All position are in absolute pixels coordinates (not window coordinates)
|
||||||
|
// FIXME: All those functions are a mess and needs to be refactored into something decent. AVOID USING OUTSIDE OF IMGUI.CPP! NOT FOR PUBLIC CONSUMPTION.
|
||||||
|
// We need: a sort of symbol library, preferably baked into font atlas when possible + decent text rendering helpers.
|
||||||
|
IMGUI_API void RenderText(ImVec2 pos, const char* text, const char* text_end = NULL, bool hide_text_after_hash = true);
|
||||||
|
IMGUI_API void RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width);
|
||||||
|
IMGUI_API void RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0,0), const ImRect* clip_rect = NULL);
|
||||||
|
IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f);
|
||||||
|
IMGUI_API void RenderCollapseTriangle(ImVec2 pos, bool is_open, float scale = 1.0f);
|
||||||
|
IMGUI_API void RenderBullet(ImVec2 pos);
|
||||||
|
IMGUI_API void RenderCheckMark(ImVec2 pos, ImU32 col);
|
||||||
|
IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text.
|
||||||
|
|
||||||
|
IMGUI_API bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags = 0);
|
||||||
|
IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0,0), ImGuiButtonFlags flags = 0);
|
||||||
|
IMGUI_API bool CloseButton(ImGuiID id, const ImVec2& pos, float radius);
|
||||||
|
|
||||||
|
IMGUI_API bool SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_min, float v_max, float power, int decimal_precision, ImGuiSliderFlags flags = 0);
|
||||||
|
IMGUI_API bool SliderFloatN(const char* label, float* v, int components, float v_min, float v_max, const char* display_format, float power);
|
||||||
|
IMGUI_API bool SliderIntN(const char* label, int* v, int components, int v_min, int v_max, const char* display_format);
|
||||||
|
|
||||||
|
IMGUI_API bool DragBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_speed, float v_min, float v_max, int decimal_precision, float power);
|
||||||
|
IMGUI_API bool DragFloatN(const char* label, float* v, int components, float v_speed, float v_min, float v_max, const char* display_format, float power);
|
||||||
|
IMGUI_API bool DragIntN(const char* label, int* v, int components, float v_speed, int v_min, int v_max, const char* display_format);
|
||||||
|
|
||||||
|
IMGUI_API bool InputTextEx(const char* label, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback = NULL, void* user_data = NULL);
|
||||||
|
IMGUI_API bool InputFloatN(const char* label, float* v, int components, int decimal_precision, ImGuiInputTextFlags extra_flags);
|
||||||
|
IMGUI_API bool InputIntN(const char* label, int* v, int components, ImGuiInputTextFlags extra_flags);
|
||||||
|
IMGUI_API bool InputScalarEx(const char* label, ImGuiDataType data_type, void* data_ptr, void* step_ptr, void* step_fast_ptr, const char* scalar_format, ImGuiInputTextFlags extra_flags);
|
||||||
|
IMGUI_API bool InputScalarAsWidgetReplacement(const ImRect& aabb, const char* label, ImGuiDataType data_type, void* data_ptr, ImGuiID id, int decimal_precision);
|
||||||
|
|
||||||
|
IMGUI_API bool TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end = NULL);
|
||||||
|
IMGUI_API bool TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0); // Consume previous SetNextTreeNodeOpened() data, if any. May return true when logging
|
||||||
|
IMGUI_API void TreePushRawID(ImGuiID id);
|
||||||
|
|
||||||
|
IMGUI_API void PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size);
|
||||||
|
|
||||||
|
IMGUI_API int ParseFormatPrecision(const char* fmt, int default_value);
|
||||||
|
IMGUI_API float RoundScalar(float value, int decimal_precision);
|
||||||
|
|
||||||
|
} // namespace ImGui
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning (pop)
|
||||||
|
#endif
|
583
lib/imgui/stb_rect_pack.h
Normal file
583
lib/imgui/stb_rect_pack.h
Normal file
|
@ -0,0 +1,583 @@
|
||||||
|
// stb_rect_pack.h - v0.10 - public domain - rectangle packing
|
||||||
|
// Sean Barrett 2014
|
||||||
|
//
|
||||||
|
// Useful for e.g. packing rectangular textures into an atlas.
|
||||||
|
// Does not do rotation.
|
||||||
|
//
|
||||||
|
// Not necessarily the awesomest packing method, but better than
|
||||||
|
// the totally naive one in stb_truetype (which is primarily what
|
||||||
|
// this is meant to replace).
|
||||||
|
//
|
||||||
|
// Has only had a few tests run, may have issues.
|
||||||
|
//
|
||||||
|
// More docs to come.
|
||||||
|
//
|
||||||
|
// No memory allocations; uses qsort() and assert() from stdlib.
|
||||||
|
// Can override those by defining STBRP_SORT and STBRP_ASSERT.
|
||||||
|
//
|
||||||
|
// This library currently uses the Skyline Bottom-Left algorithm.
|
||||||
|
//
|
||||||
|
// Please note: better rectangle packers are welcome! Please
|
||||||
|
// implement them to the same API, but with a different init
|
||||||
|
// function.
|
||||||
|
//
|
||||||
|
// Credits
|
||||||
|
//
|
||||||
|
// Library
|
||||||
|
// Sean Barrett
|
||||||
|
// Minor features
|
||||||
|
// Martins Mozeiko
|
||||||
|
// Bugfixes / warning fixes
|
||||||
|
// Jeremy Jaussaud
|
||||||
|
//
|
||||||
|
// Version history:
|
||||||
|
//
|
||||||
|
// 0.10 (2016-10-25) remove cast-away-const to avoid warnings
|
||||||
|
// 0.09 (2016-08-27) fix compiler warnings
|
||||||
|
// 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0)
|
||||||
|
// 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0)
|
||||||
|
// 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort
|
||||||
|
// 0.05: added STBRP_ASSERT to allow replacing assert
|
||||||
|
// 0.04: fixed minor bug in STBRP_LARGE_RECTS support
|
||||||
|
// 0.01: initial release
|
||||||
|
//
|
||||||
|
// LICENSE
|
||||||
|
//
|
||||||
|
// This software is dual-licensed to the public domain and under the following
|
||||||
|
// license: you are granted a perpetual, irrevocable license to copy, modify,
|
||||||
|
// publish, and distribute this file as you see fit.
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// INCLUDE SECTION
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef STB_INCLUDE_STB_RECT_PACK_H
|
||||||
|
#define STB_INCLUDE_STB_RECT_PACK_H
|
||||||
|
|
||||||
|
#define STB_RECT_PACK_VERSION 1
|
||||||
|
|
||||||
|
#ifdef STBRP_STATIC
|
||||||
|
#define STBRP_DEF static
|
||||||
|
#else
|
||||||
|
#define STBRP_DEF extern
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct stbrp_context stbrp_context;
|
||||||
|
typedef struct stbrp_node stbrp_node;
|
||||||
|
typedef struct stbrp_rect stbrp_rect;
|
||||||
|
|
||||||
|
#ifdef STBRP_LARGE_RECTS
|
||||||
|
typedef int stbrp_coord;
|
||||||
|
#else
|
||||||
|
typedef unsigned short stbrp_coord;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
|
||||||
|
// Assign packed locations to rectangles. The rectangles are of type
|
||||||
|
// 'stbrp_rect' defined below, stored in the array 'rects', and there
|
||||||
|
// are 'num_rects' many of them.
|
||||||
|
//
|
||||||
|
// Rectangles which are successfully packed have the 'was_packed' flag
|
||||||
|
// set to a non-zero value and 'x' and 'y' store the minimum location
|
||||||
|
// on each axis (i.e. bottom-left in cartesian coordinates, top-left
|
||||||
|
// if you imagine y increasing downwards). Rectangles which do not fit
|
||||||
|
// have the 'was_packed' flag set to 0.
|
||||||
|
//
|
||||||
|
// You should not try to access the 'rects' array from another thread
|
||||||
|
// while this function is running, as the function temporarily reorders
|
||||||
|
// the array while it executes.
|
||||||
|
//
|
||||||
|
// To pack into another rectangle, you need to call stbrp_init_target
|
||||||
|
// again. To continue packing into the same rectangle, you can call
|
||||||
|
// this function again. Calling this multiple times with multiple rect
|
||||||
|
// arrays will probably produce worse packing results than calling it
|
||||||
|
// a single time with the full rectangle array, but the option is
|
||||||
|
// available.
|
||||||
|
|
||||||
|
struct stbrp_rect
|
||||||
|
{
|
||||||
|
// reserved for your use:
|
||||||
|
int id;
|
||||||
|
|
||||||
|
// input:
|
||||||
|
stbrp_coord w, h;
|
||||||
|
|
||||||
|
// output:
|
||||||
|
stbrp_coord x, y;
|
||||||
|
int was_packed; // non-zero if valid packing
|
||||||
|
|
||||||
|
}; // 16 bytes, nominally
|
||||||
|
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
|
||||||
|
// Initialize a rectangle packer to:
|
||||||
|
// pack a rectangle that is 'width' by 'height' in dimensions
|
||||||
|
// using temporary storage provided by the array 'nodes', which is 'num_nodes' long
|
||||||
|
//
|
||||||
|
// You must call this function every time you start packing into a new target.
|
||||||
|
//
|
||||||
|
// There is no "shutdown" function. The 'nodes' memory must stay valid for
|
||||||
|
// the following stbrp_pack_rects() call (or calls), but can be freed after
|
||||||
|
// the call (or calls) finish.
|
||||||
|
//
|
||||||
|
// Note: to guarantee best results, either:
|
||||||
|
// 1. make sure 'num_nodes' >= 'width'
|
||||||
|
// or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1'
|
||||||
|
//
|
||||||
|
// If you don't do either of the above things, widths will be quantized to multiples
|
||||||
|
// of small integers to guarantee the algorithm doesn't run out of temporary storage.
|
||||||
|
//
|
||||||
|
// If you do #2, then the non-quantized algorithm will be used, but the algorithm
|
||||||
|
// may run out of temporary storage and be unable to pack some rectangles.
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
|
||||||
|
// Optionally call this function after init but before doing any packing to
|
||||||
|
// change the handling of the out-of-temp-memory scenario, described above.
|
||||||
|
// If you call init again, this will be reset to the default (false).
|
||||||
|
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
|
||||||
|
// Optionally select which packing heuristic the library should use. Different
|
||||||
|
// heuristics will produce better/worse results for different data sets.
|
||||||
|
// If you call init again, this will be reset to the default.
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
STBRP_HEURISTIC_Skyline_default=0,
|
||||||
|
STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
|
||||||
|
STBRP_HEURISTIC_Skyline_BF_sortHeight
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// the details of the following structures don't matter to you, but they must
|
||||||
|
// be visible so you can handle the memory allocations for them
|
||||||
|
|
||||||
|
struct stbrp_node
|
||||||
|
{
|
||||||
|
stbrp_coord x,y;
|
||||||
|
stbrp_node *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct stbrp_context
|
||||||
|
{
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
int align;
|
||||||
|
int init_mode;
|
||||||
|
int heuristic;
|
||||||
|
int num_nodes;
|
||||||
|
stbrp_node *active_head;
|
||||||
|
stbrp_node *free_head;
|
||||||
|
stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// IMPLEMENTATION SECTION
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifdef STB_RECT_PACK_IMPLEMENTATION
|
||||||
|
#ifndef STBRP_SORT
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define STBRP_SORT qsort
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef STBRP_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#define STBRP_ASSERT assert
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define STBRP__NOTUSED(v) (void)(v)
|
||||||
|
#else
|
||||||
|
#define STBRP__NOTUSED(v) (void)sizeof(v)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
STBRP__INIT_skyline = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
|
||||||
|
{
|
||||||
|
switch (context->init_mode) {
|
||||||
|
case STBRP__INIT_skyline:
|
||||||
|
STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
|
||||||
|
context->heuristic = heuristic;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
STBRP_ASSERT(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
|
||||||
|
{
|
||||||
|
if (allow_out_of_mem)
|
||||||
|
// if it's ok to run out of memory, then don't bother aligning them;
|
||||||
|
// this gives better packing, but may fail due to OOM (even though
|
||||||
|
// the rectangles easily fit). @TODO a smarter approach would be to only
|
||||||
|
// quantize once we've hit OOM, then we could get rid of this parameter.
|
||||||
|
context->align = 1;
|
||||||
|
else {
|
||||||
|
// if it's not ok to run out of memory, then quantize the widths
|
||||||
|
// so that num_nodes is always enough nodes.
|
||||||
|
//
|
||||||
|
// I.e. num_nodes * align >= width
|
||||||
|
// align >= width / num_nodes
|
||||||
|
// align = ceil(width/num_nodes)
|
||||||
|
|
||||||
|
context->align = (context->width + context->num_nodes-1) / context->num_nodes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
#ifndef STBRP_LARGE_RECTS
|
||||||
|
STBRP_ASSERT(width <= 0xffff && height <= 0xffff);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (i=0; i < num_nodes-1; ++i)
|
||||||
|
nodes[i].next = &nodes[i+1];
|
||||||
|
nodes[i].next = NULL;
|
||||||
|
context->init_mode = STBRP__INIT_skyline;
|
||||||
|
context->heuristic = STBRP_HEURISTIC_Skyline_default;
|
||||||
|
context->free_head = &nodes[0];
|
||||||
|
context->active_head = &context->extra[0];
|
||||||
|
context->width = width;
|
||||||
|
context->height = height;
|
||||||
|
context->num_nodes = num_nodes;
|
||||||
|
stbrp_setup_allow_out_of_mem(context, 0);
|
||||||
|
|
||||||
|
// node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly)
|
||||||
|
context->extra[0].x = 0;
|
||||||
|
context->extra[0].y = 0;
|
||||||
|
context->extra[0].next = &context->extra[1];
|
||||||
|
context->extra[1].x = (stbrp_coord) width;
|
||||||
|
#ifdef STBRP_LARGE_RECTS
|
||||||
|
context->extra[1].y = (1<<30);
|
||||||
|
#else
|
||||||
|
context->extra[1].y = 65535;
|
||||||
|
#endif
|
||||||
|
context->extra[1].next = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find minimum y position if it starts at x1
|
||||||
|
static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
|
||||||
|
{
|
||||||
|
stbrp_node *node = first;
|
||||||
|
int x1 = x0 + width;
|
||||||
|
int min_y, visited_width, waste_area;
|
||||||
|
|
||||||
|
STBRP__NOTUSED(c);
|
||||||
|
|
||||||
|
STBRP_ASSERT(first->x <= x0);
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
// skip in case we're past the node
|
||||||
|
while (node->next->x <= x0)
|
||||||
|
++node;
|
||||||
|
#else
|
||||||
|
STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency
|
||||||
|
#endif
|
||||||
|
|
||||||
|
STBRP_ASSERT(node->x <= x0);
|
||||||
|
|
||||||
|
min_y = 0;
|
||||||
|
waste_area = 0;
|
||||||
|
visited_width = 0;
|
||||||
|
while (node->x < x1) {
|
||||||
|
if (node->y > min_y) {
|
||||||
|
// raise min_y higher.
|
||||||
|
// we've accounted for all waste up to min_y,
|
||||||
|
// but we'll now add more waste for everything we've visted
|
||||||
|
waste_area += visited_width * (node->y - min_y);
|
||||||
|
min_y = node->y;
|
||||||
|
// the first time through, visited_width might be reduced
|
||||||
|
if (node->x < x0)
|
||||||
|
visited_width += node->next->x - x0;
|
||||||
|
else
|
||||||
|
visited_width += node->next->x - node->x;
|
||||||
|
} else {
|
||||||
|
// add waste area
|
||||||
|
int under_width = node->next->x - node->x;
|
||||||
|
if (under_width + visited_width > width)
|
||||||
|
under_width = width - visited_width;
|
||||||
|
waste_area += under_width * (min_y - node->y);
|
||||||
|
visited_width += under_width;
|
||||||
|
}
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
*pwaste = waste_area;
|
||||||
|
return min_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int x,y;
|
||||||
|
stbrp_node **prev_link;
|
||||||
|
} stbrp__findresult;
|
||||||
|
|
||||||
|
static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
|
||||||
|
{
|
||||||
|
int best_waste = (1<<30), best_x, best_y = (1 << 30);
|
||||||
|
stbrp__findresult fr;
|
||||||
|
stbrp_node **prev, *node, *tail, **best = NULL;
|
||||||
|
|
||||||
|
// align to multiple of c->align
|
||||||
|
width = (width + c->align - 1);
|
||||||
|
width -= width % c->align;
|
||||||
|
STBRP_ASSERT(width % c->align == 0);
|
||||||
|
|
||||||
|
node = c->active_head;
|
||||||
|
prev = &c->active_head;
|
||||||
|
while (node->x + width <= c->width) {
|
||||||
|
int y,waste;
|
||||||
|
y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
|
||||||
|
if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL
|
||||||
|
// bottom left
|
||||||
|
if (y < best_y) {
|
||||||
|
best_y = y;
|
||||||
|
best = prev;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// best-fit
|
||||||
|
if (y + height <= c->height) {
|
||||||
|
// can only use it if it first vertically
|
||||||
|
if (y < best_y || (y == best_y && waste < best_waste)) {
|
||||||
|
best_y = y;
|
||||||
|
best_waste = waste;
|
||||||
|
best = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prev = &node->next;
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
best_x = (best == NULL) ? 0 : (*best)->x;
|
||||||
|
|
||||||
|
// if doing best-fit (BF), we also have to try aligning right edge to each node position
|
||||||
|
//
|
||||||
|
// e.g, if fitting
|
||||||
|
//
|
||||||
|
// ____________________
|
||||||
|
// |____________________|
|
||||||
|
//
|
||||||
|
// into
|
||||||
|
//
|
||||||
|
// | |
|
||||||
|
// | ____________|
|
||||||
|
// |____________|
|
||||||
|
//
|
||||||
|
// then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned
|
||||||
|
//
|
||||||
|
// This makes BF take about 2x the time
|
||||||
|
|
||||||
|
if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
|
||||||
|
tail = c->active_head;
|
||||||
|
node = c->active_head;
|
||||||
|
prev = &c->active_head;
|
||||||
|
// find first node that's admissible
|
||||||
|
while (tail->x < width)
|
||||||
|
tail = tail->next;
|
||||||
|
while (tail) {
|
||||||
|
int xpos = tail->x - width;
|
||||||
|
int y,waste;
|
||||||
|
STBRP_ASSERT(xpos >= 0);
|
||||||
|
// find the left position that matches this
|
||||||
|
while (node->next->x <= xpos) {
|
||||||
|
prev = &node->next;
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
|
||||||
|
y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
|
||||||
|
if (y + height < c->height) {
|
||||||
|
if (y <= best_y) {
|
||||||
|
if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
|
||||||
|
best_x = xpos;
|
||||||
|
STBRP_ASSERT(y <= best_y);
|
||||||
|
best_y = y;
|
||||||
|
best_waste = waste;
|
||||||
|
best = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tail = tail->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fr.prev_link = best;
|
||||||
|
fr.x = best_x;
|
||||||
|
fr.y = best_y;
|
||||||
|
return fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
|
||||||
|
{
|
||||||
|
// find best position according to heuristic
|
||||||
|
stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
|
||||||
|
stbrp_node *node, *cur;
|
||||||
|
|
||||||
|
// bail if:
|
||||||
|
// 1. it failed
|
||||||
|
// 2. the best node doesn't fit (we don't always check this)
|
||||||
|
// 3. we're out of memory
|
||||||
|
if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
|
||||||
|
res.prev_link = NULL;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// on success, create new node
|
||||||
|
node = context->free_head;
|
||||||
|
node->x = (stbrp_coord) res.x;
|
||||||
|
node->y = (stbrp_coord) (res.y + height);
|
||||||
|
|
||||||
|
context->free_head = node->next;
|
||||||
|
|
||||||
|
// insert the new node into the right starting point, and
|
||||||
|
// let 'cur' point to the remaining nodes needing to be
|
||||||
|
// stiched back in
|
||||||
|
|
||||||
|
cur = *res.prev_link;
|
||||||
|
if (cur->x < res.x) {
|
||||||
|
// preserve the existing one, so start testing with the next one
|
||||||
|
stbrp_node *next = cur->next;
|
||||||
|
cur->next = node;
|
||||||
|
cur = next;
|
||||||
|
} else {
|
||||||
|
*res.prev_link = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// from here, traverse cur and free the nodes, until we get to one
|
||||||
|
// that shouldn't be freed
|
||||||
|
while (cur->next && cur->next->x <= res.x + width) {
|
||||||
|
stbrp_node *next = cur->next;
|
||||||
|
// move the current node to the free list
|
||||||
|
cur->next = context->free_head;
|
||||||
|
context->free_head = cur;
|
||||||
|
cur = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// stitch the list back in
|
||||||
|
node->next = cur;
|
||||||
|
|
||||||
|
if (cur->x < res.x + width)
|
||||||
|
cur->x = (stbrp_coord) (res.x + width);
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
|
cur = context->active_head;
|
||||||
|
while (cur->x < context->width) {
|
||||||
|
STBRP_ASSERT(cur->x < cur->next->x);
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
STBRP_ASSERT(cur->next == NULL);
|
||||||
|
|
||||||
|
{
|
||||||
|
stbrp_node *L1 = NULL, *L2 = NULL;
|
||||||
|
int count=0;
|
||||||
|
cur = context->active_head;
|
||||||
|
while (cur) {
|
||||||
|
L1 = cur;
|
||||||
|
cur = cur->next;
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
cur = context->free_head;
|
||||||
|
while (cur) {
|
||||||
|
L2 = cur;
|
||||||
|
cur = cur->next;
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
STBRP_ASSERT(count == context->num_nodes+2);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int rect_height_compare(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const stbrp_rect *p = (const stbrp_rect *) a;
|
||||||
|
const stbrp_rect *q = (const stbrp_rect *) b;
|
||||||
|
if (p->h > q->h)
|
||||||
|
return -1;
|
||||||
|
if (p->h < q->h)
|
||||||
|
return 1;
|
||||||
|
return (p->w > q->w) ? -1 : (p->w < q->w);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int rect_width_compare(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const stbrp_rect *p = (const stbrp_rect *) a;
|
||||||
|
const stbrp_rect *q = (const stbrp_rect *) b;
|
||||||
|
if (p->w > q->w)
|
||||||
|
return -1;
|
||||||
|
if (p->w < q->w)
|
||||||
|
return 1;
|
||||||
|
return (p->h > q->h) ? -1 : (p->h < q->h);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int rect_original_order(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const stbrp_rect *p = (const stbrp_rect *) a;
|
||||||
|
const stbrp_rect *q = (const stbrp_rect *) b;
|
||||||
|
return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef STBRP_LARGE_RECTS
|
||||||
|
#define STBRP__MAXVAL 0xffffffff
|
||||||
|
#else
|
||||||
|
#define STBRP__MAXVAL 0xffff
|
||||||
|
#endif
|
||||||
|
|
||||||
|
STBRP_DEF void stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
// we use the 'was_packed' field internally to allow sorting/unsorting
|
||||||
|
for (i=0; i < num_rects; ++i) {
|
||||||
|
rects[i].was_packed = i;
|
||||||
|
#ifndef STBRP_LARGE_RECTS
|
||||||
|
STBRP_ASSERT(rects[i].w <= 0xffff && rects[i].h <= 0xffff);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort according to heuristic
|
||||||
|
STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
|
||||||
|
|
||||||
|
for (i=0; i < num_rects; ++i) {
|
||||||
|
if (rects[i].w == 0 || rects[i].h == 0) {
|
||||||
|
rects[i].x = rects[i].y = 0; // empty rect needs no space
|
||||||
|
} else {
|
||||||
|
stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
|
||||||
|
if (fr.prev_link) {
|
||||||
|
rects[i].x = (stbrp_coord) fr.x;
|
||||||
|
rects[i].y = (stbrp_coord) fr.y;
|
||||||
|
} else {
|
||||||
|
rects[i].x = rects[i].y = STBRP__MAXVAL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// unsort
|
||||||
|
STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
|
||||||
|
|
||||||
|
// set was_packed flags
|
||||||
|
for (i=0; i < num_rects; ++i)
|
||||||
|
rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
|
||||||
|
}
|
||||||
|
#endif
|
1322
lib/imgui/stb_textedit.h
Normal file
1322
lib/imgui/stb_textedit.h
Normal file
File diff suppressed because it is too large
Load diff
3287
lib/imgui/stb_truetype.h
Normal file
3287
lib/imgui/stb_truetype.h
Normal file
File diff suppressed because it is too large
Load diff
399
lib/imgui_sdl/imgui_impl_sdl.cpp
Normal file
399
lib/imgui_sdl/imgui_impl_sdl.cpp
Normal file
|
@ -0,0 +1,399 @@
|
||||||
|
// ImGui SDL2 binding with OpenGL
|
||||||
|
// You can copy and use unmodified imgui_impl_* files in your project.
|
||||||
|
// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
|
||||||
|
// See main.cpp for an example of using this.
|
||||||
|
// https://github.com/ocornut/imgui
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
#include "emscripten.h"
|
||||||
|
#include <GLES2/gl2.h>
|
||||||
|
#else
|
||||||
|
#include <SDL2/SDL_syswm.h>
|
||||||
|
#include <SDL2/SDL_opengl.h>
|
||||||
|
#include <GLES2/gl2.h>
|
||||||
|
#endif
|
||||||
|
#include "imgui.h"
|
||||||
|
#include "imgui_impl_sdl.h"
|
||||||
|
|
||||||
|
// Data
|
||||||
|
static double g_Time = 0.0f;
|
||||||
|
static bool g_FingerPressed = false;
|
||||||
|
static bool g_FingerMotion = false;
|
||||||
|
static float g_FingerX = -1;
|
||||||
|
static float g_FingerY = -1;
|
||||||
|
static bool g_MousePressed[3] = { false, false, false };
|
||||||
|
static float g_MouseWheel = 0.0f;
|
||||||
|
static GLuint g_FontTexture = 0;
|
||||||
|
static int g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
|
||||||
|
static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0;
|
||||||
|
static int g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0;
|
||||||
|
static unsigned int g_VboHandle = 0, g_ElementsHandle = 0;
|
||||||
|
|
||||||
|
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
|
||||||
|
// If text or lines are blurry when integrating ImGui in your engine:
|
||||||
|
// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
|
||||||
|
void ImGui_ImplSdl_RenderDrawLists(ImDrawData* draw_data) {
|
||||||
|
// Backup GL state
|
||||||
|
GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
|
||||||
|
GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
|
||||||
|
GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
|
||||||
|
GLint last_element_array_buffer; glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
|
||||||
|
GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
|
||||||
|
GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
|
||||||
|
GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
|
||||||
|
GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
|
||||||
|
|
||||||
|
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
glBlendEquation(GL_FUNC_ADD);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
glDisable(GL_CULL_FACE);
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
glEnable(GL_SCISSOR_TEST);
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
|
||||||
|
// Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
float fb_height = io.DisplaySize.y * io.DisplayFramebufferScale.y;
|
||||||
|
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
|
||||||
|
|
||||||
|
// Setup orthographic projection matrix
|
||||||
|
const float ortho_projection[4][4] =
|
||||||
|
{
|
||||||
|
{ 2.0f / io.DisplaySize.x, 0.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 2.0f / -io.DisplaySize.y, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, -1.0f, 0.0f },
|
||||||
|
{ -1.0f, 1.0f, 0.0f, 1.0f },
|
||||||
|
};
|
||||||
|
glUseProgram(g_ShaderHandle);
|
||||||
|
glUniform1i(g_AttribLocationTex, 0);
|
||||||
|
glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]);
|
||||||
|
|
||||||
|
for (int n = 0; n < draw_data->CmdListsCount; n++) {
|
||||||
|
const ImDrawList* cmd_list = draw_data->CmdLists[n];
|
||||||
|
const ImDrawIdx* idx_buffer_offset = 0;
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.size() * sizeof(ImDrawVert), (GLvoid*)&cmd_list->VtxBuffer.front(), GL_STREAM_DRAW);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ElementsHandle);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.size() * sizeof(ImDrawIdx), (GLvoid*)&cmd_list->IdxBuffer.front(), GL_STREAM_DRAW);
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(g_AttribLocationPosition);
|
||||||
|
glEnableVertexAttribArray(g_AttribLocationUV);
|
||||||
|
glEnableVertexAttribArray(g_AttribLocationColor);
|
||||||
|
|
||||||
|
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
|
||||||
|
glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, pos));
|
||||||
|
glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, uv));
|
||||||
|
glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col));
|
||||||
|
#undef OFFSETOF
|
||||||
|
for (const ImDrawCmd* pcmd = cmd_list->CmdBuffer.begin(); pcmd != cmd_list->CmdBuffer.end(); pcmd++) {
|
||||||
|
if (pcmd->UserCallback) {
|
||||||
|
pcmd->UserCallback(cmd_list, pcmd);
|
||||||
|
} else {
|
||||||
|
glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
|
||||||
|
glScissor((int)pcmd->ClipRect.x, (int)(fb_height - pcmd->ClipRect.w), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
|
||||||
|
glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, GL_UNSIGNED_SHORT, idx_buffer_offset);
|
||||||
|
}
|
||||||
|
idx_buffer_offset += pcmd->ElemCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore modified GL state
|
||||||
|
glUseProgram(last_program);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, last_texture);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer);
|
||||||
|
if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
|
||||||
|
if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
|
||||||
|
if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
|
||||||
|
if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char* ImGui_ImplSdl_GetClipboardText(void *) {
|
||||||
|
return SDL_GetClipboardText();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ImGui_ImplSdl_SetClipboardText(void *, const char* text) {
|
||||||
|
SDL_SetClipboardText(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSdl_ProcessEvents(bool* quitFlag) {
|
||||||
|
SDL_Event event;
|
||||||
|
while (SDL_PollEvent(&event)) {
|
||||||
|
ImGui_ImplSdl_ProcessEvent(&event);
|
||||||
|
if (event.type == SDL_QUIT) {
|
||||||
|
*quitFlag = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSdl_ProcessEvent(SDL_Event* event) {
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
switch (event->type) {
|
||||||
|
case SDL_MOUSEWHEEL: {
|
||||||
|
if (event->wheel.y > 0) {
|
||||||
|
g_MouseWheel = 1;
|
||||||
|
} else if (event->wheel.y < 0) {
|
||||||
|
g_MouseWheel = -1;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_FINGERMOTION: {
|
||||||
|
g_FingerMotion = true;
|
||||||
|
g_FingerX = event->tfinger.x;
|
||||||
|
g_FingerY = event->tfinger.y;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_FINGERDOWN: {
|
||||||
|
g_FingerPressed = true;
|
||||||
|
g_FingerMotion = true;
|
||||||
|
g_FingerX = event->tfinger.x;
|
||||||
|
g_FingerY = event->tfinger.y;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_FINGERUP: {
|
||||||
|
g_FingerPressed = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_MOUSEBUTTONDOWN: {
|
||||||
|
if (event->button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
|
||||||
|
if (event->button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
|
||||||
|
if (event->button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_TEXTINPUT: {
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.AddInputCharactersUTF8(event->text.text);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case SDL_KEYDOWN:
|
||||||
|
case SDL_KEYUP: {
|
||||||
|
int key = event->key.keysym.sym & ~SDLK_SCANCODE_MASK;
|
||||||
|
io.KeysDown[key] = (event->type == SDL_KEYDOWN);
|
||||||
|
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
|
||||||
|
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
|
||||||
|
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSdl_CreateFontsTexture() {
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Build texture atlas
|
||||||
|
unsigned char* pixels;
|
||||||
|
int width, height;
|
||||||
|
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits for OpenGL3 demo because it is more likely to be compatible with user's existing shader.
|
||||||
|
|
||||||
|
// Create OpenGL texture
|
||||||
|
glGenTextures(1, &g_FontTexture);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, g_FontTexture);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||||
|
|
||||||
|
// Store our identifier
|
||||||
|
io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
|
||||||
|
|
||||||
|
// Cleanup (don't clear the input data if you want to append new fonts later)
|
||||||
|
io.Fonts->ClearInputData();
|
||||||
|
io.Fonts->ClearTexData();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool ImGui_ImplSdl_CreateDeviceObjects()
|
||||||
|
{
|
||||||
|
// Backup GL state
|
||||||
|
GLint last_texture, last_array_buffer;
|
||||||
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
|
||||||
|
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
|
||||||
|
|
||||||
|
const GLchar *vertex_shader =
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
"precision highp float;\n"
|
||||||
|
#endif
|
||||||
|
"uniform mat4 ProjMtx;\n"
|
||||||
|
"attribute vec2 Position;\n"
|
||||||
|
"attribute vec2 UV;\n"
|
||||||
|
"attribute vec4 Color;\n"
|
||||||
|
"varying vec2 Frag_UV;\n"
|
||||||
|
"varying vec4 Frag_Color;\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" Frag_UV = UV;\n"
|
||||||
|
" Frag_Color = Color;\n"
|
||||||
|
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
const GLchar* fragment_shader =
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
"precision mediump float;\n"
|
||||||
|
#endif
|
||||||
|
"uniform sampler2D Texture;\n"
|
||||||
|
"varying vec2 Frag_UV;\n"
|
||||||
|
"varying vec4 Frag_Color;\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" gl_FragColor = Frag_Color * texture2D( Texture, Frag_UV.st);\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
g_ShaderHandle = glCreateProgram();
|
||||||
|
g_VertHandle = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
glShaderSource(g_VertHandle, 1, &vertex_shader, 0);
|
||||||
|
glShaderSource(g_FragHandle, 1, &fragment_shader, 0);
|
||||||
|
glCompileShader(g_VertHandle);
|
||||||
|
glCompileShader(g_FragHandle);
|
||||||
|
glAttachShader(g_ShaderHandle, g_VertHandle);
|
||||||
|
glAttachShader(g_ShaderHandle, g_FragHandle);
|
||||||
|
glLinkProgram(g_ShaderHandle);
|
||||||
|
|
||||||
|
g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture");
|
||||||
|
g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx");
|
||||||
|
g_AttribLocationPosition = glGetAttribLocation(g_ShaderHandle, "Position");
|
||||||
|
g_AttribLocationUV = glGetAttribLocation(g_ShaderHandle, "UV");
|
||||||
|
g_AttribLocationColor = glGetAttribLocation(g_ShaderHandle, "Color");
|
||||||
|
|
||||||
|
glGenBuffers(1, &g_VboHandle);
|
||||||
|
glGenBuffers(1, &g_ElementsHandle);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
|
||||||
|
glEnableVertexAttribArray(g_AttribLocationPosition);
|
||||||
|
glEnableVertexAttribArray(g_AttribLocationUV);
|
||||||
|
glEnableVertexAttribArray(g_AttribLocationColor);
|
||||||
|
|
||||||
|
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
|
||||||
|
glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, pos));
|
||||||
|
glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, uv));
|
||||||
|
glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col));
|
||||||
|
#undef OFFSETOF
|
||||||
|
|
||||||
|
ImGui_ImplSdl_CreateFontsTexture();
|
||||||
|
|
||||||
|
// Restore modified GL state
|
||||||
|
glBindTexture(GL_TEXTURE_2D, last_texture);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSdl_InvalidateDeviceObjects() {
|
||||||
|
if (g_FontTexture) {
|
||||||
|
glDeleteTextures(1, &g_FontTexture);
|
||||||
|
ImGui::GetIO().Fonts->TexID = 0;
|
||||||
|
g_FontTexture = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ImGui_ImplSdl_Init(SDL_Window *window)
|
||||||
|
{
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
io.KeyMap[ImGuiKey_Tab] = SDLK_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
|
||||||
|
io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
|
||||||
|
io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
|
||||||
|
io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
|
||||||
|
io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
|
||||||
|
io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
|
||||||
|
io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
|
||||||
|
io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
|
||||||
|
io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
|
||||||
|
io.KeyMap[ImGuiKey_Delete] = SDLK_DELETE;
|
||||||
|
io.KeyMap[ImGuiKey_Backspace] = SDLK_BACKSPACE;
|
||||||
|
io.KeyMap[ImGuiKey_Enter] = SDLK_RETURN;
|
||||||
|
io.KeyMap[ImGuiKey_Escape] = SDLK_ESCAPE;
|
||||||
|
io.KeyMap[ImGuiKey_A] = SDLK_a;
|
||||||
|
io.KeyMap[ImGuiKey_C] = SDLK_c;
|
||||||
|
io.KeyMap[ImGuiKey_V] = SDLK_v;
|
||||||
|
io.KeyMap[ImGuiKey_X] = SDLK_x;
|
||||||
|
io.KeyMap[ImGuiKey_Y] = SDLK_y;
|
||||||
|
io.KeyMap[ImGuiKey_Z] = SDLK_z;
|
||||||
|
|
||||||
|
io.RenderDrawListsFn = ImGui_ImplSdl_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer.
|
||||||
|
io.SetClipboardTextFn = ImGui_ImplSdl_SetClipboardText;
|
||||||
|
io.GetClipboardTextFn = ImGui_ImplSdl_GetClipboardText;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
SDL_SysWMinfo wmInfo;
|
||||||
|
SDL_VERSION(&wmInfo.version);
|
||||||
|
SDL_GetWindowWMInfo(window, &wmInfo);
|
||||||
|
io.ImeWindowHandle = wmInfo.info.win.window;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImGui_ImplSdl_Shutdown() {
|
||||||
|
ImGui_ImplSdl_InvalidateDeviceObjects();
|
||||||
|
ImGui::Shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
int cc = 0;
|
||||||
|
|
||||||
|
void ImGui_ImplSdl_NewFrame(SDL_Window *window)
|
||||||
|
{
|
||||||
|
if (!g_FontTexture) {
|
||||||
|
ImGui_ImplSdl_CreateDeviceObjects();
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Setup display size (every frame to accommodate for window resizing)
|
||||||
|
int w, h;
|
||||||
|
SDL_GetWindowSize(window, &w, &h);
|
||||||
|
cc++;
|
||||||
|
if (cc > 500) {
|
||||||
|
printf("canvas w: %d h: %d\n",w,h);
|
||||||
|
cc = 0;
|
||||||
|
}
|
||||||
|
io.DisplaySize = ImVec2((float)w, (float)h);
|
||||||
|
|
||||||
|
// Setup time step
|
||||||
|
Uint32 time = SDL_GetTicks();
|
||||||
|
double current_time = time / 1000.0;
|
||||||
|
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
|
||||||
|
g_Time = current_time;
|
||||||
|
|
||||||
|
// Setup inputs
|
||||||
|
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
|
||||||
|
int mx, my;
|
||||||
|
Uint32 mouseMask = SDL_GetMouseState(&mx, &my);
|
||||||
|
//printf("flags: 0x%.8X\n",SDL_GetWindowFlags(window));
|
||||||
|
if (SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_FOCUS) {
|
||||||
|
io.MousePos = ImVec2((float)mx, (float)my); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
|
||||||
|
} else {
|
||||||
|
io.MousePos = ImVec2(-1,-1);
|
||||||
|
}
|
||||||
|
if (g_FingerX != -1) {
|
||||||
|
io.MousePos = ImVec2(g_FingerX * ImGui::GetIO().DisplaySize.x, g_FingerY * ImGui::GetIO().DisplaySize.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
io.MouseDown[0] = g_MousePressed[0] || (mouseMask & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
|
||||||
|
io.MouseDown[1] = g_MousePressed[1] || (mouseMask & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
|
||||||
|
io.MouseDown[2] = g_MousePressed[2] || (mouseMask & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
|
||||||
|
g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
|
||||||
|
|
||||||
|
//printf("mousePos x: %f y: %f\n", io.MousePos.x, io.MousePos.y);
|
||||||
|
if (g_FingerPressed) {
|
||||||
|
io.MouseDown[0] = true;
|
||||||
|
}
|
||||||
|
if (g_FingerMotion || g_FingerPressed) {
|
||||||
|
//io.MousePos = ImVec2(g_FingerX * ImGui::GetIO().DisplaySize.x,g_FingerY * ImGui::GetIO().DisplaySize.y);
|
||||||
|
g_FingerMotion = false;
|
||||||
|
printf("touchPos x: %f(%f) y: %f(%f)\n", io.MousePos.x,g_FingerX, io.MousePos.y, g_FingerY);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
io.MouseWheel = g_MouseWheel;
|
||||||
|
g_MouseWheel = 0.0f;
|
||||||
|
|
||||||
|
// Hide OS mouse cursor if ImGui is drawing it
|
||||||
|
SDL_ShowCursor(io.MouseDrawCursor ? 0 : 1);
|
||||||
|
|
||||||
|
// Start the frame
|
||||||
|
ImGui::NewFrame();
|
||||||
|
}
|
18
lib/imgui_sdl/imgui_impl_sdl.h
Normal file
18
lib/imgui_sdl/imgui_impl_sdl.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// ImGui SDL2 binding with OpenGL
|
||||||
|
// You can copy and use unmodified imgui_impl_* files in your project.
|
||||||
|
// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
|
||||||
|
// See main.cpp for an example of using this.
|
||||||
|
// https://github.com/ocornut/imgui
|
||||||
|
|
||||||
|
struct SDL_Window;
|
||||||
|
typedef union SDL_Event SDL_Event;
|
||||||
|
|
||||||
|
IMGUI_API bool ImGui_ImplSdl_Init(SDL_Window *window);
|
||||||
|
IMGUI_API void ImGui_ImplSdl_Shutdown();
|
||||||
|
IMGUI_API void ImGui_ImplSdl_NewFrame(SDL_Window *window);
|
||||||
|
IMGUI_API bool ImGui_ImplSdl_ProcessEvent(SDL_Event* event);
|
||||||
|
IMGUI_API void ImGui_ImplSdl_ProcessEvents(bool* quitFlag);
|
||||||
|
|
||||||
|
// Use if you want to reset your rendering device without losing ImGui state.
|
||||||
|
IMGUI_API void ImGui_ImplSdl_InvalidateDeviceObjects();
|
||||||
|
IMGUI_API bool ImGui_ImplSdl_CreateDeviceObjects();
|
2040
lib/soil2/SOIL.c
Normal file
2040
lib/soil2/SOIL.c
Normal file
File diff suppressed because it is too large
Load diff
433
lib/soil2/SOIL.h
Normal file
433
lib/soil2/SOIL.h
Normal file
|
@ -0,0 +1,433 @@
|
||||||
|
/**
|
||||||
|
@mainpage SOIL
|
||||||
|
|
||||||
|
Jonathan Dummer
|
||||||
|
2007-07-26-10.36
|
||||||
|
|
||||||
|
Simple OpenGL Image Library
|
||||||
|
|
||||||
|
A tiny c library for uploading images as
|
||||||
|
textures into OpenGL. Also saving and
|
||||||
|
loading of images is supported.
|
||||||
|
|
||||||
|
I'm using Sean's Tool Box image loader as a base:
|
||||||
|
http://www.nothings.org/
|
||||||
|
|
||||||
|
I'm upgrading it to load TGA and DDS files, and a direct
|
||||||
|
path for loading DDS files straight into OpenGL textures,
|
||||||
|
when applicable.
|
||||||
|
|
||||||
|
Image Formats:
|
||||||
|
- BMP load & save
|
||||||
|
- TGA load & save
|
||||||
|
- DDS load & save
|
||||||
|
- PNG load
|
||||||
|
- JPG load
|
||||||
|
|
||||||
|
OpenGL Texture Features:
|
||||||
|
- resample to power-of-two sizes
|
||||||
|
- MIPmap generation
|
||||||
|
- compressed texture S3TC formats (if supported)
|
||||||
|
- can pre-multiply alpha for you, for better compositing
|
||||||
|
- can flip image about the y-axis (except pre-compressed DDS files)
|
||||||
|
|
||||||
|
Thanks to:
|
||||||
|
* Sean Barret - for the awesome stb_image
|
||||||
|
* Dan Venkitachalam - for finding some non-compliant DDS files, and patching some explicit casts
|
||||||
|
* everybody at gamedev.net
|
||||||
|
**/
|
||||||
|
|
||||||
|
#ifndef HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY
|
||||||
|
#define HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
The format of images that may be loaded (force_channels).
|
||||||
|
SOIL_LOAD_AUTO leaves the image in whatever format it was found.
|
||||||
|
SOIL_LOAD_L forces the image to load as Luminous (greyscale)
|
||||||
|
SOIL_LOAD_LA forces the image to load as Luminous with Alpha
|
||||||
|
SOIL_LOAD_RGB forces the image to load as Red Green Blue
|
||||||
|
SOIL_LOAD_RGBA forces the image to load as Red Green Blue Alpha
|
||||||
|
**/
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
SOIL_LOAD_AUTO = 0,
|
||||||
|
SOIL_LOAD_L = 1,
|
||||||
|
SOIL_LOAD_LA = 2,
|
||||||
|
SOIL_LOAD_RGB = 3,
|
||||||
|
SOIL_LOAD_RGBA = 4
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
Passed in as reuse_texture_ID, will cause SOIL to
|
||||||
|
register a new texture ID using glGenTextures().
|
||||||
|
If the value passed into reuse_texture_ID > 0 then
|
||||||
|
SOIL will just re-use that texture ID (great for
|
||||||
|
reloading image assets in-game!)
|
||||||
|
**/
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
SOIL_CREATE_NEW_ID = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
flags you can pass into SOIL_load_OGL_texture()
|
||||||
|
and SOIL_create_OGL_texture().
|
||||||
|
(note that if SOIL_FLAG_DDS_LOAD_DIRECT is used
|
||||||
|
the rest of the flags with the exception of
|
||||||
|
SOIL_FLAG_TEXTURE_REPEATS will be ignored while
|
||||||
|
loading already-compressed DDS files.)
|
||||||
|
|
||||||
|
SOIL_FLAG_POWER_OF_TWO: force the image to be POT
|
||||||
|
SOIL_FLAG_MIPMAPS: generate mipmaps for the texture
|
||||||
|
SOIL_FLAG_TEXTURE_REPEATS: otherwise will clamp
|
||||||
|
SOIL_FLAG_MULTIPLY_ALPHA: for using (GL_ONE,GL_ONE_MINUS_SRC_ALPHA) blending
|
||||||
|
SOIL_FLAG_INVERT_Y: flip the image vertically
|
||||||
|
SOIL_FLAG_COMPRESS_TO_DXT: if the card can display them, will convert RGB to DXT1, RGBA to DXT5
|
||||||
|
SOIL_FLAG_DDS_LOAD_DIRECT: will load DDS files directly without _ANY_ additional processing
|
||||||
|
SOIL_FLAG_NTSC_SAFE_RGB: clamps RGB components to the range [16,235]
|
||||||
|
SOIL_FLAG_CoCg_Y: Google YCoCg; RGB=>CoYCg, RGBA=>CoCgAY
|
||||||
|
SOIL_FLAG_TEXTURE_RECTANGE: uses ARB_texture_rectangle ; pixel indexed & no repeat or MIPmaps or cubemaps
|
||||||
|
**/
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
SOIL_FLAG_POWER_OF_TWO = 1,
|
||||||
|
SOIL_FLAG_MIPMAPS = 2,
|
||||||
|
SOIL_FLAG_TEXTURE_REPEATS = 4,
|
||||||
|
SOIL_FLAG_MULTIPLY_ALPHA = 8,
|
||||||
|
SOIL_FLAG_INVERT_Y = 16,
|
||||||
|
SOIL_FLAG_COMPRESS_TO_DXT = 32,
|
||||||
|
SOIL_FLAG_DDS_LOAD_DIRECT = 64,
|
||||||
|
SOIL_FLAG_NTSC_SAFE_RGB = 128,
|
||||||
|
SOIL_FLAG_CoCg_Y = 256,
|
||||||
|
SOIL_FLAG_TEXTURE_RECTANGLE = 512
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
The types of images that may be saved.
|
||||||
|
(TGA supports uncompressed RGB / RGBA)
|
||||||
|
(BMP supports uncompressed RGB)
|
||||||
|
(DDS supports DXT1 and DXT5)
|
||||||
|
**/
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
SOIL_SAVE_TYPE_TGA = 0,
|
||||||
|
SOIL_SAVE_TYPE_BMP = 1,
|
||||||
|
SOIL_SAVE_TYPE_DDS = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
Defines the order of faces in a DDS cubemap.
|
||||||
|
I recommend that you use the same order in single
|
||||||
|
image cubemap files, so they will be interchangeable
|
||||||
|
with DDS cubemaps when using SOIL.
|
||||||
|
**/
|
||||||
|
#define SOIL_DDS_CUBEMAP_FACE_ORDER "EWUDNS"
|
||||||
|
|
||||||
|
/**
|
||||||
|
The types of internal fake HDR representations
|
||||||
|
|
||||||
|
SOIL_HDR_RGBE: RGB * pow( 2.0, A - 128.0 )
|
||||||
|
SOIL_HDR_RGBdivA: RGB / A
|
||||||
|
SOIL_HDR_RGBdivA2: RGB / (A*A)
|
||||||
|
**/
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
SOIL_HDR_RGBE = 0,
|
||||||
|
SOIL_HDR_RGBdivA = 1,
|
||||||
|
SOIL_HDR_RGBdivA2 = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
Loads an image from disk into an OpenGL texture.
|
||||||
|
\param filename the name of the file to upload as a texture
|
||||||
|
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||||
|
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||||
|
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||||
|
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||||
|
**/
|
||||||
|
unsigned int
|
||||||
|
SOIL_load_OGL_texture
|
||||||
|
(
|
||||||
|
const char *filename,
|
||||||
|
int force_channels,
|
||||||
|
unsigned int reuse_texture_ID,
|
||||||
|
unsigned int flags
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Loads 6 images from disk into an OpenGL cubemap texture.
|
||||||
|
\param x_pos_file the name of the file to upload as the +x cube face
|
||||||
|
\param x_neg_file the name of the file to upload as the -x cube face
|
||||||
|
\param y_pos_file the name of the file to upload as the +y cube face
|
||||||
|
\param y_neg_file the name of the file to upload as the -y cube face
|
||||||
|
\param z_pos_file the name of the file to upload as the +z cube face
|
||||||
|
\param z_neg_file the name of the file to upload as the -z cube face
|
||||||
|
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||||
|
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||||
|
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||||
|
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||||
|
**/
|
||||||
|
unsigned int
|
||||||
|
SOIL_load_OGL_cubemap
|
||||||
|
(
|
||||||
|
const char *x_pos_file,
|
||||||
|
const char *x_neg_file,
|
||||||
|
const char *y_pos_file,
|
||||||
|
const char *y_neg_file,
|
||||||
|
const char *z_pos_file,
|
||||||
|
const char *z_neg_file,
|
||||||
|
int force_channels,
|
||||||
|
unsigned int reuse_texture_ID,
|
||||||
|
unsigned int flags
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Loads 1 image from disk and splits it into an OpenGL cubemap texture.
|
||||||
|
\param filename the name of the file to upload as a texture
|
||||||
|
\param face_order the order of the faces in the file, any combination of NSWEUD, for North, South, Up, etc.
|
||||||
|
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||||
|
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||||
|
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||||
|
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||||
|
**/
|
||||||
|
unsigned int
|
||||||
|
SOIL_load_OGL_single_cubemap
|
||||||
|
(
|
||||||
|
const char *filename,
|
||||||
|
const char face_order[6],
|
||||||
|
int force_channels,
|
||||||
|
unsigned int reuse_texture_ID,
|
||||||
|
unsigned int flags
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Loads an HDR image from disk into an OpenGL texture.
|
||||||
|
\param filename the name of the file to upload as a texture
|
||||||
|
\param fake_HDR_format SOIL_HDR_RGBE, SOIL_HDR_RGBdivA, SOIL_HDR_RGBdivA2
|
||||||
|
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||||
|
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT
|
||||||
|
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||||
|
**/
|
||||||
|
unsigned int
|
||||||
|
SOIL_load_OGL_HDR_texture
|
||||||
|
(
|
||||||
|
const char *filename,
|
||||||
|
int fake_HDR_format,
|
||||||
|
int rescale_to_max,
|
||||||
|
unsigned int reuse_texture_ID,
|
||||||
|
unsigned int flags
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Loads an image from RAM into an OpenGL texture.
|
||||||
|
\param buffer the image data in RAM just as if it were still in a file
|
||||||
|
\param buffer_length the size of the buffer in bytes
|
||||||
|
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||||
|
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||||
|
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||||
|
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||||
|
**/
|
||||||
|
unsigned int
|
||||||
|
SOIL_load_OGL_texture_from_memory
|
||||||
|
(
|
||||||
|
const unsigned char *const buffer,
|
||||||
|
int buffer_length,
|
||||||
|
int force_channels,
|
||||||
|
unsigned int reuse_texture_ID,
|
||||||
|
unsigned int flags
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Loads 6 images from memory into an OpenGL cubemap texture.
|
||||||
|
\param x_pos_buffer the image data in RAM to upload as the +x cube face
|
||||||
|
\param x_pos_buffer_length the size of the above buffer
|
||||||
|
\param x_neg_buffer the image data in RAM to upload as the +x cube face
|
||||||
|
\param x_neg_buffer_length the size of the above buffer
|
||||||
|
\param y_pos_buffer the image data in RAM to upload as the +x cube face
|
||||||
|
\param y_pos_buffer_length the size of the above buffer
|
||||||
|
\param y_neg_buffer the image data in RAM to upload as the +x cube face
|
||||||
|
\param y_neg_buffer_length the size of the above buffer
|
||||||
|
\param z_pos_buffer the image data in RAM to upload as the +x cube face
|
||||||
|
\param z_pos_buffer_length the size of the above buffer
|
||||||
|
\param z_neg_buffer the image data in RAM to upload as the +x cube face
|
||||||
|
\param z_neg_buffer_length the size of the above buffer
|
||||||
|
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||||
|
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||||
|
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||||
|
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||||
|
**/
|
||||||
|
unsigned int
|
||||||
|
SOIL_load_OGL_cubemap_from_memory
|
||||||
|
(
|
||||||
|
const unsigned char *const x_pos_buffer,
|
||||||
|
int x_pos_buffer_length,
|
||||||
|
const unsigned char *const x_neg_buffer,
|
||||||
|
int x_neg_buffer_length,
|
||||||
|
const unsigned char *const y_pos_buffer,
|
||||||
|
int y_pos_buffer_length,
|
||||||
|
const unsigned char *const y_neg_buffer,
|
||||||
|
int y_neg_buffer_length,
|
||||||
|
const unsigned char *const z_pos_buffer,
|
||||||
|
int z_pos_buffer_length,
|
||||||
|
const unsigned char *const z_neg_buffer,
|
||||||
|
int z_neg_buffer_length,
|
||||||
|
int force_channels,
|
||||||
|
unsigned int reuse_texture_ID,
|
||||||
|
unsigned int flags
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Loads 1 image from RAM and splits it into an OpenGL cubemap texture.
|
||||||
|
\param buffer the image data in RAM just as if it were still in a file
|
||||||
|
\param buffer_length the size of the buffer in bytes
|
||||||
|
\param face_order the order of the faces in the file, any combination of NSWEUD, for North, South, Up, etc.
|
||||||
|
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||||
|
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||||
|
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||||
|
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||||
|
**/
|
||||||
|
unsigned int
|
||||||
|
SOIL_load_OGL_single_cubemap_from_memory
|
||||||
|
(
|
||||||
|
const unsigned char *const buffer,
|
||||||
|
int buffer_length,
|
||||||
|
const char face_order[6],
|
||||||
|
int force_channels,
|
||||||
|
unsigned int reuse_texture_ID,
|
||||||
|
unsigned int flags
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Creates a 2D OpenGL texture from raw image data. Note that the raw data is
|
||||||
|
_NOT_ freed after the upload (so the user can load various versions).
|
||||||
|
\param data the raw data to be uploaded as an OpenGL texture
|
||||||
|
\param width the width of the image in pixels
|
||||||
|
\param height the height of the image in pixels
|
||||||
|
\param channels the number of channels: 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||||
|
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||||
|
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT
|
||||||
|
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||||
|
**/
|
||||||
|
unsigned int
|
||||||
|
SOIL_create_OGL_texture
|
||||||
|
(
|
||||||
|
const unsigned char *const data,
|
||||||
|
int width, int height, int channels,
|
||||||
|
unsigned int reuse_texture_ID,
|
||||||
|
unsigned int flags
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Creates an OpenGL cubemap texture by splitting up 1 image into 6 parts.
|
||||||
|
\param data the raw data to be uploaded as an OpenGL texture
|
||||||
|
\param width the width of the image in pixels
|
||||||
|
\param height the height of the image in pixels
|
||||||
|
\param channels the number of channels: 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||||
|
\param face_order the order of the faces in the file, and combination of NSWEUD, for North, South, Up, etc.
|
||||||
|
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||||
|
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||||
|
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||||
|
**/
|
||||||
|
unsigned int
|
||||||
|
SOIL_create_OGL_single_cubemap
|
||||||
|
(
|
||||||
|
const unsigned char *const data,
|
||||||
|
int width, int height, int channels,
|
||||||
|
const char face_order[6],
|
||||||
|
unsigned int reuse_texture_ID,
|
||||||
|
unsigned int flags
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Captures the OpenGL window (RGB) and saves it to disk
|
||||||
|
\return 0 if it failed, otherwise returns 1
|
||||||
|
**/
|
||||||
|
int
|
||||||
|
SOIL_save_screenshot
|
||||||
|
(
|
||||||
|
const char *filename,
|
||||||
|
int image_type,
|
||||||
|
int x, int y,
|
||||||
|
int width, int height
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Loads an image from disk into an array of unsigned chars.
|
||||||
|
Note that *channels return the original channel count of the
|
||||||
|
image. If force_channels was other than SOIL_LOAD_AUTO,
|
||||||
|
the resulting image has force_channels, but *channels may be
|
||||||
|
different (if the original image had a different channel
|
||||||
|
count).
|
||||||
|
\return 0 if failed, otherwise returns 1
|
||||||
|
**/
|
||||||
|
unsigned char*
|
||||||
|
SOIL_load_image
|
||||||
|
(
|
||||||
|
const char *filename,
|
||||||
|
int *width, int *height, int *channels,
|
||||||
|
int force_channels
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Loads an image from memory into an array of unsigned chars.
|
||||||
|
Note that *channels return the original channel count of the
|
||||||
|
image. If force_channels was other than SOIL_LOAD_AUTO,
|
||||||
|
the resulting image has force_channels, but *channels may be
|
||||||
|
different (if the original image had a different channel
|
||||||
|
count).
|
||||||
|
\return 0 if failed, otherwise returns 1
|
||||||
|
**/
|
||||||
|
unsigned char*
|
||||||
|
SOIL_load_image_from_memory
|
||||||
|
(
|
||||||
|
const unsigned char *const buffer,
|
||||||
|
int buffer_length,
|
||||||
|
int *width, int *height, int *channels,
|
||||||
|
int force_channels
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Saves an image from an array of unsigned chars (RGBA) to disk
|
||||||
|
\return 0 if failed, otherwise returns 1
|
||||||
|
**/
|
||||||
|
int
|
||||||
|
SOIL_save_image
|
||||||
|
(
|
||||||
|
const char *filename,
|
||||||
|
int image_type,
|
||||||
|
int width, int height, int channels,
|
||||||
|
const unsigned char *const data
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Frees the image data (note, this is just C's "free()"...this function is
|
||||||
|
present mostly so C++ programmers don't forget to use "free()" and call
|
||||||
|
"delete []" instead [8^)
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
SOIL_free_image_data
|
||||||
|
(
|
||||||
|
unsigned char *img_data
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
This function resturn a pointer to a string describing the last thing
|
||||||
|
that happened inside SOIL. It can be used to determine why an image
|
||||||
|
failed to load.
|
||||||
|
**/
|
||||||
|
const char*
|
||||||
|
SOIL_last_result
|
||||||
|
(
|
||||||
|
void
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY */
|
632
lib/soil2/image_DXT.c
Normal file
632
lib/soil2/image_DXT.c
Normal file
|
@ -0,0 +1,632 @@
|
||||||
|
/*
|
||||||
|
Jonathan Dummer
|
||||||
|
2007-07-31-10.32
|
||||||
|
|
||||||
|
simple DXT compression / decompression code
|
||||||
|
|
||||||
|
public domain
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "image_DXT.h"
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* set this =1 if you want to use the covarince matrix method...
|
||||||
|
which is better than my method of using standard deviations
|
||||||
|
overall, except on the infintesimal chance that the power
|
||||||
|
method fails for finding the largest eigenvector */
|
||||||
|
#define USE_COV_MAT 1
|
||||||
|
|
||||||
|
/********* Function Prototypes *********/
|
||||||
|
/*
|
||||||
|
Takes a 4x4 block of pixels and compresses it into 8 bytes
|
||||||
|
in DXT1 format (color only, no alpha). Speed is valued
|
||||||
|
over prettyness, at least for now.
|
||||||
|
*/
|
||||||
|
void compress_DDS_color_block(
|
||||||
|
int channels,
|
||||||
|
const unsigned char *const uncompressed,
|
||||||
|
unsigned char compressed[8] );
|
||||||
|
/*
|
||||||
|
Takes a 4x4 block of pixels and compresses the alpha
|
||||||
|
component it into 8 bytes for use in DXT5 DDS files.
|
||||||
|
Speed is valued over prettyness, at least for now.
|
||||||
|
*/
|
||||||
|
void compress_DDS_alpha_block(
|
||||||
|
const unsigned char *const uncompressed,
|
||||||
|
unsigned char compressed[8] );
|
||||||
|
|
||||||
|
/********* Actual Exposed Functions *********/
|
||||||
|
int
|
||||||
|
save_image_as_DDS
|
||||||
|
(
|
||||||
|
const char *filename,
|
||||||
|
int width, int height, int channels,
|
||||||
|
const unsigned char *const data
|
||||||
|
)
|
||||||
|
{
|
||||||
|
/* variables */
|
||||||
|
FILE *fout;
|
||||||
|
unsigned char *DDS_data;
|
||||||
|
DDS_header header;
|
||||||
|
int DDS_size;
|
||||||
|
/* error check */
|
||||||
|
if( (NULL == filename) ||
|
||||||
|
(width < 1) || (height < 1) ||
|
||||||
|
(channels < 1) || (channels > 4) ||
|
||||||
|
(data == NULL ) )
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/* Convert the image */
|
||||||
|
if( (channels & 1) == 1 )
|
||||||
|
{
|
||||||
|
/* no alpha, just use DXT1 */
|
||||||
|
DDS_data = convert_image_to_DXT1( data, width, height, channels, &DDS_size );
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
/* has alpha, so use DXT5 */
|
||||||
|
DDS_data = convert_image_to_DXT5( data, width, height, channels, &DDS_size );
|
||||||
|
}
|
||||||
|
/* save it */
|
||||||
|
memset( &header, 0, sizeof( DDS_header ) );
|
||||||
|
header.dwMagic = ('D' << 0) | ('D' << 8) | ('S' << 16) | (' ' << 24);
|
||||||
|
header.dwSize = 124;
|
||||||
|
header.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_LINEARSIZE;
|
||||||
|
header.dwWidth = width;
|
||||||
|
header.dwHeight = height;
|
||||||
|
header.dwPitchOrLinearSize = DDS_size;
|
||||||
|
header.sPixelFormat.dwSize = 32;
|
||||||
|
header.sPixelFormat.dwFlags = DDPF_FOURCC;
|
||||||
|
if( (channels & 1) == 1 )
|
||||||
|
{
|
||||||
|
header.sPixelFormat.dwFourCC = ('D' << 0) | ('X' << 8) | ('T' << 16) | ('1' << 24);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
header.sPixelFormat.dwFourCC = ('D' << 0) | ('X' << 8) | ('T' << 16) | ('5' << 24);
|
||||||
|
}
|
||||||
|
header.sCaps.dwCaps1 = DDSCAPS_TEXTURE;
|
||||||
|
/* write it out */
|
||||||
|
fout = fopen( filename, "wb");
|
||||||
|
fwrite( &header, sizeof( DDS_header ), 1, fout );
|
||||||
|
fwrite( DDS_data, 1, DDS_size, fout );
|
||||||
|
fclose( fout );
|
||||||
|
/* done */
|
||||||
|
free( DDS_data );
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char* convert_image_to_DXT1(
|
||||||
|
const unsigned char *const uncompressed,
|
||||||
|
int width, int height, int channels,
|
||||||
|
int *out_size )
|
||||||
|
{
|
||||||
|
unsigned char *compressed;
|
||||||
|
int i, j, x, y;
|
||||||
|
unsigned char ublock[16*3];
|
||||||
|
unsigned char cblock[8];
|
||||||
|
int index = 0, chan_step = 1;
|
||||||
|
int block_count = 0;
|
||||||
|
/* error check */
|
||||||
|
*out_size = 0;
|
||||||
|
if( (width < 1) || (height < 1) ||
|
||||||
|
(NULL == uncompressed) ||
|
||||||
|
(channels < 1) || (channels > 4) )
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
/* for channels == 1 or 2, I do not step forward for R,G,B values */
|
||||||
|
if( channels < 3 )
|
||||||
|
{
|
||||||
|
chan_step = 0;
|
||||||
|
}
|
||||||
|
/* get the RAM for the compressed image
|
||||||
|
(8 bytes per 4x4 pixel block) */
|
||||||
|
*out_size = ((width+3) >> 2) * ((height+3) >> 2) * 8;
|
||||||
|
compressed = (unsigned char*)malloc( *out_size );
|
||||||
|
/* go through each block */
|
||||||
|
for( j = 0; j < height; j += 4 )
|
||||||
|
{
|
||||||
|
for( i = 0; i < width; i += 4 )
|
||||||
|
{
|
||||||
|
/* copy this block into a new one */
|
||||||
|
int idx = 0;
|
||||||
|
int mx = 4, my = 4;
|
||||||
|
if( j+4 >= height )
|
||||||
|
{
|
||||||
|
my = height - j;
|
||||||
|
}
|
||||||
|
if( i+4 >= width )
|
||||||
|
{
|
||||||
|
mx = width - i;
|
||||||
|
}
|
||||||
|
for( y = 0; y < my; ++y )
|
||||||
|
{
|
||||||
|
for( x = 0; x < mx; ++x )
|
||||||
|
{
|
||||||
|
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels];
|
||||||
|
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step];
|
||||||
|
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step+chan_step];
|
||||||
|
}
|
||||||
|
for( x = mx; x < 4; ++x )
|
||||||
|
{
|
||||||
|
ublock[idx++] = ublock[0];
|
||||||
|
ublock[idx++] = ublock[1];
|
||||||
|
ublock[idx++] = ublock[2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for( y = my; y < 4; ++y )
|
||||||
|
{
|
||||||
|
for( x = 0; x < 4; ++x )
|
||||||
|
{
|
||||||
|
ublock[idx++] = ublock[0];
|
||||||
|
ublock[idx++] = ublock[1];
|
||||||
|
ublock[idx++] = ublock[2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* compress the block */
|
||||||
|
++block_count;
|
||||||
|
compress_DDS_color_block( 3, ublock, cblock );
|
||||||
|
/* copy the data from the block into the main block */
|
||||||
|
for( x = 0; x < 8; ++x )
|
||||||
|
{
|
||||||
|
compressed[index++] = cblock[x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return compressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char* convert_image_to_DXT5(
|
||||||
|
const unsigned char *const uncompressed,
|
||||||
|
int width, int height, int channels,
|
||||||
|
int *out_size )
|
||||||
|
{
|
||||||
|
unsigned char *compressed;
|
||||||
|
int i, j, x, y;
|
||||||
|
unsigned char ublock[16*4];
|
||||||
|
unsigned char cblock[8];
|
||||||
|
int index = 0, chan_step = 1;
|
||||||
|
int block_count = 0, has_alpha;
|
||||||
|
/* error check */
|
||||||
|
*out_size = 0;
|
||||||
|
if( (width < 1) || (height < 1) ||
|
||||||
|
(NULL == uncompressed) ||
|
||||||
|
(channels < 1) || ( channels > 4) )
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
/* for channels == 1 or 2, I do not step forward for R,G,B vales */
|
||||||
|
if( channels < 3 )
|
||||||
|
{
|
||||||
|
chan_step = 0;
|
||||||
|
}
|
||||||
|
/* # channels = 1 or 3 have no alpha, 2 & 4 do have alpha */
|
||||||
|
has_alpha = 1 - (channels & 1);
|
||||||
|
/* get the RAM for the compressed image
|
||||||
|
(16 bytes per 4x4 pixel block) */
|
||||||
|
*out_size = ((width+3) >> 2) * ((height+3) >> 2) * 16;
|
||||||
|
compressed = (unsigned char*)malloc( *out_size );
|
||||||
|
/* go through each block */
|
||||||
|
for( j = 0; j < height; j += 4 )
|
||||||
|
{
|
||||||
|
for( i = 0; i < width; i += 4 )
|
||||||
|
{
|
||||||
|
/* local variables, and my block counter */
|
||||||
|
int idx = 0;
|
||||||
|
int mx = 4, my = 4;
|
||||||
|
if( j+4 >= height )
|
||||||
|
{
|
||||||
|
my = height - j;
|
||||||
|
}
|
||||||
|
if( i+4 >= width )
|
||||||
|
{
|
||||||
|
mx = width - i;
|
||||||
|
}
|
||||||
|
for( y = 0; y < my; ++y )
|
||||||
|
{
|
||||||
|
for( x = 0; x < mx; ++x )
|
||||||
|
{
|
||||||
|
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels];
|
||||||
|
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step];
|
||||||
|
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step+chan_step];
|
||||||
|
ublock[idx++] =
|
||||||
|
has_alpha * uncompressed[(j+y)*width*channels+(i+x)*channels+channels-1]
|
||||||
|
+ (1-has_alpha)*255;
|
||||||
|
}
|
||||||
|
for( x = mx; x < 4; ++x )
|
||||||
|
{
|
||||||
|
ublock[idx++] = ublock[0];
|
||||||
|
ublock[idx++] = ublock[1];
|
||||||
|
ublock[idx++] = ublock[2];
|
||||||
|
ublock[idx++] = ublock[3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for( y = my; y < 4; ++y )
|
||||||
|
{
|
||||||
|
for( x = 0; x < 4; ++x )
|
||||||
|
{
|
||||||
|
ublock[idx++] = ublock[0];
|
||||||
|
ublock[idx++] = ublock[1];
|
||||||
|
ublock[idx++] = ublock[2];
|
||||||
|
ublock[idx++] = ublock[3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* now compress the alpha block */
|
||||||
|
compress_DDS_alpha_block( ublock, cblock );
|
||||||
|
/* copy the data from the compressed alpha block into the main buffer */
|
||||||
|
for( x = 0; x < 8; ++x )
|
||||||
|
{
|
||||||
|
compressed[index++] = cblock[x];
|
||||||
|
}
|
||||||
|
/* then compress the color block */
|
||||||
|
++block_count;
|
||||||
|
compress_DDS_color_block( 4, ublock, cblock );
|
||||||
|
/* copy the data from the compressed color block into the main buffer */
|
||||||
|
for( x = 0; x < 8; ++x )
|
||||||
|
{
|
||||||
|
compressed[index++] = cblock[x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return compressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/********* Helper Functions *********/
|
||||||
|
int convert_bit_range( int c, int from_bits, int to_bits )
|
||||||
|
{
|
||||||
|
int b = (1 << (from_bits - 1)) + c * ((1 << to_bits) - 1);
|
||||||
|
return (b + (b >> from_bits)) >> from_bits;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rgb_to_565( int r, int g, int b )
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(convert_bit_range( r, 8, 5 ) << 11) |
|
||||||
|
(convert_bit_range( g, 8, 6 ) << 05) |
|
||||||
|
(convert_bit_range( b, 8, 5 ) << 00);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rgb_888_from_565( unsigned int c, int *r, int *g, int *b )
|
||||||
|
{
|
||||||
|
*r = convert_bit_range( (c >> 11) & 31, 5, 8 );
|
||||||
|
*g = convert_bit_range( (c >> 05) & 63, 6, 8 );
|
||||||
|
*b = convert_bit_range( (c >> 00) & 31, 5, 8 );
|
||||||
|
}
|
||||||
|
|
||||||
|
void compute_color_line_STDEV(
|
||||||
|
const unsigned char *const uncompressed,
|
||||||
|
int channels,
|
||||||
|
float point[3], float direction[3] )
|
||||||
|
{
|
||||||
|
const float inv_16 = 1.0f / 16.0f;
|
||||||
|
int i;
|
||||||
|
float sum_r = 0.0f, sum_g = 0.0f, sum_b = 0.0f;
|
||||||
|
float sum_rr = 0.0f, sum_gg = 0.0f, sum_bb = 0.0f;
|
||||||
|
float sum_rg = 0.0f, sum_rb = 0.0f, sum_gb = 0.0f;
|
||||||
|
/* calculate all data needed for the covariance matrix
|
||||||
|
( to compare with _rygdxt code) */
|
||||||
|
for( i = 0; i < 16*channels; i += channels )
|
||||||
|
{
|
||||||
|
sum_r += uncompressed[i+0];
|
||||||
|
sum_rr += uncompressed[i+0] * uncompressed[i+0];
|
||||||
|
sum_g += uncompressed[i+1];
|
||||||
|
sum_gg += uncompressed[i+1] * uncompressed[i+1];
|
||||||
|
sum_b += uncompressed[i+2];
|
||||||
|
sum_bb += uncompressed[i+2] * uncompressed[i+2];
|
||||||
|
sum_rg += uncompressed[i+0] * uncompressed[i+1];
|
||||||
|
sum_rb += uncompressed[i+0] * uncompressed[i+2];
|
||||||
|
sum_gb += uncompressed[i+1] * uncompressed[i+2];
|
||||||
|
}
|
||||||
|
/* convert the sums to averages */
|
||||||
|
sum_r *= inv_16;
|
||||||
|
sum_g *= inv_16;
|
||||||
|
sum_b *= inv_16;
|
||||||
|
/* and convert the squares to the squares of the value - avg_value */
|
||||||
|
sum_rr -= 16.0f * sum_r * sum_r;
|
||||||
|
sum_gg -= 16.0f * sum_g * sum_g;
|
||||||
|
sum_bb -= 16.0f * sum_b * sum_b;
|
||||||
|
sum_rg -= 16.0f * sum_r * sum_g;
|
||||||
|
sum_rb -= 16.0f * sum_r * sum_b;
|
||||||
|
sum_gb -= 16.0f * sum_g * sum_b;
|
||||||
|
/* the point on the color line is the average */
|
||||||
|
point[0] = sum_r;
|
||||||
|
point[1] = sum_g;
|
||||||
|
point[2] = sum_b;
|
||||||
|
#if USE_COV_MAT
|
||||||
|
/*
|
||||||
|
The following idea was from ryg.
|
||||||
|
(https://mollyrocket.com/forums/viewtopic.php?t=392)
|
||||||
|
The method worked great (less RMSE than mine) most of
|
||||||
|
the time, but had some issues handling some simple
|
||||||
|
boundary cases, like full green next to full red,
|
||||||
|
which would generate a covariance matrix like this:
|
||||||
|
|
||||||
|
| 1 -1 0 |
|
||||||
|
| -1 1 0 |
|
||||||
|
| 0 0 0 |
|
||||||
|
|
||||||
|
For a given starting vector, the power method can
|
||||||
|
generate all zeros! So no starting with {1,1,1}
|
||||||
|
as I was doing! This kind of error is still a
|
||||||
|
slight posibillity, but will be very rare.
|
||||||
|
*/
|
||||||
|
/* use the covariance matrix directly
|
||||||
|
(1st iteration, don't use all 1.0 values!) */
|
||||||
|
sum_r = 1.0f;
|
||||||
|
sum_g = 2.718281828f;
|
||||||
|
sum_b = 3.141592654f;
|
||||||
|
direction[0] = sum_r*sum_rr + sum_g*sum_rg + sum_b*sum_rb;
|
||||||
|
direction[1] = sum_r*sum_rg + sum_g*sum_gg + sum_b*sum_gb;
|
||||||
|
direction[2] = sum_r*sum_rb + sum_g*sum_gb + sum_b*sum_bb;
|
||||||
|
/* 2nd iteration, use results from the 1st guy */
|
||||||
|
sum_r = direction[0];
|
||||||
|
sum_g = direction[1];
|
||||||
|
sum_b = direction[2];
|
||||||
|
direction[0] = sum_r*sum_rr + sum_g*sum_rg + sum_b*sum_rb;
|
||||||
|
direction[1] = sum_r*sum_rg + sum_g*sum_gg + sum_b*sum_gb;
|
||||||
|
direction[2] = sum_r*sum_rb + sum_g*sum_gb + sum_b*sum_bb;
|
||||||
|
/* 3rd iteration, use results from the 2nd guy */
|
||||||
|
sum_r = direction[0];
|
||||||
|
sum_g = direction[1];
|
||||||
|
sum_b = direction[2];
|
||||||
|
direction[0] = sum_r*sum_rr + sum_g*sum_rg + sum_b*sum_rb;
|
||||||
|
direction[1] = sum_r*sum_rg + sum_g*sum_gg + sum_b*sum_gb;
|
||||||
|
direction[2] = sum_r*sum_rb + sum_g*sum_gb + sum_b*sum_bb;
|
||||||
|
#else
|
||||||
|
/* use my standard deviation method
|
||||||
|
(very robust, a tiny bit slower and less accurate) */
|
||||||
|
direction[0] = sqrt( sum_rr );
|
||||||
|
direction[1] = sqrt( sum_gg );
|
||||||
|
direction[2] = sqrt( sum_bb );
|
||||||
|
/* which has a greater component */
|
||||||
|
if( sum_gg > sum_rr )
|
||||||
|
{
|
||||||
|
/* green has greater component, so base the other signs off of green */
|
||||||
|
if( sum_rg < 0.0f )
|
||||||
|
{
|
||||||
|
direction[0] = -direction[0];
|
||||||
|
}
|
||||||
|
if( sum_gb < 0.0f )
|
||||||
|
{
|
||||||
|
direction[2] = -direction[2];
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
/* red has a greater component */
|
||||||
|
if( sum_rg < 0.0f )
|
||||||
|
{
|
||||||
|
direction[1] = -direction[1];
|
||||||
|
}
|
||||||
|
if( sum_rb < 0.0f )
|
||||||
|
{
|
||||||
|
direction[2] = -direction[2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void LSE_master_colors_max_min(
|
||||||
|
int *cmax, int *cmin,
|
||||||
|
int channels,
|
||||||
|
const unsigned char *const uncompressed )
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
/* the master colors */
|
||||||
|
int c0[3], c1[3];
|
||||||
|
/* used for fitting the line */
|
||||||
|
float sum_x[] = { 0.0f, 0.0f, 0.0f };
|
||||||
|
float sum_x2[] = { 0.0f, 0.0f, 0.0f };
|
||||||
|
float dot_max = 1.0f, dot_min = -1.0f;
|
||||||
|
float vec_len2 = 0.0f;
|
||||||
|
float dot;
|
||||||
|
/* error check */
|
||||||
|
if( (channels < 3) || (channels > 4) )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
compute_color_line_STDEV( uncompressed, channels, sum_x, sum_x2 );
|
||||||
|
vec_len2 = 1.0f / ( 0.00001f +
|
||||||
|
sum_x2[0]*sum_x2[0] + sum_x2[1]*sum_x2[1] + sum_x2[2]*sum_x2[2] );
|
||||||
|
/* finding the max and min vector values */
|
||||||
|
dot_max =
|
||||||
|
(
|
||||||
|
sum_x2[0] * uncompressed[0] +
|
||||||
|
sum_x2[1] * uncompressed[1] +
|
||||||
|
sum_x2[2] * uncompressed[2]
|
||||||
|
);
|
||||||
|
dot_min = dot_max;
|
||||||
|
for( i = 1; i < 16; ++i )
|
||||||
|
{
|
||||||
|
dot =
|
||||||
|
(
|
||||||
|
sum_x2[0] * uncompressed[i*channels+0] +
|
||||||
|
sum_x2[1] * uncompressed[i*channels+1] +
|
||||||
|
sum_x2[2] * uncompressed[i*channels+2]
|
||||||
|
);
|
||||||
|
if( dot < dot_min )
|
||||||
|
{
|
||||||
|
dot_min = dot;
|
||||||
|
} else if( dot > dot_max )
|
||||||
|
{
|
||||||
|
dot_max = dot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* and the offset (from the average location) */
|
||||||
|
dot = sum_x2[0]*sum_x[0] + sum_x2[1]*sum_x[1] + sum_x2[2]*sum_x[2];
|
||||||
|
dot_min -= dot;
|
||||||
|
dot_max -= dot;
|
||||||
|
/* post multiply by the scaling factor */
|
||||||
|
dot_min *= vec_len2;
|
||||||
|
dot_max *= vec_len2;
|
||||||
|
/* OK, build the master colors */
|
||||||
|
for( i = 0; i < 3; ++i )
|
||||||
|
{
|
||||||
|
/* color 0 */
|
||||||
|
c0[i] = (int)(0.5f + sum_x[i] + dot_max * sum_x2[i]);
|
||||||
|
if( c0[i] < 0 )
|
||||||
|
{
|
||||||
|
c0[i] = 0;
|
||||||
|
} else if( c0[i] > 255 )
|
||||||
|
{
|
||||||
|
c0[i] = 255;
|
||||||
|
}
|
||||||
|
/* color 1 */
|
||||||
|
c1[i] = (int)(0.5f + sum_x[i] + dot_min * sum_x2[i]);
|
||||||
|
if( c1[i] < 0 )
|
||||||
|
{
|
||||||
|
c1[i] = 0;
|
||||||
|
} else if( c1[i] > 255 )
|
||||||
|
{
|
||||||
|
c1[i] = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* down_sample (with rounding?) */
|
||||||
|
i = rgb_to_565( c0[0], c0[1], c0[2] );
|
||||||
|
j = rgb_to_565( c1[0], c1[1], c1[2] );
|
||||||
|
if( i > j )
|
||||||
|
{
|
||||||
|
*cmax = i;
|
||||||
|
*cmin = j;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
*cmax = j;
|
||||||
|
*cmin = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
compress_DDS_color_block
|
||||||
|
(
|
||||||
|
int channels,
|
||||||
|
const unsigned char *const uncompressed,
|
||||||
|
unsigned char compressed[8]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
/* variables */
|
||||||
|
int i;
|
||||||
|
int next_bit;
|
||||||
|
int enc_c0, enc_c1;
|
||||||
|
int c0[4], c1[4];
|
||||||
|
float color_line[] = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||||
|
float vec_len2 = 0.0f, dot_offset = 0.0f;
|
||||||
|
/* stupid order */
|
||||||
|
int swizzle4[] = { 0, 2, 3, 1 };
|
||||||
|
/* get the master colors */
|
||||||
|
LSE_master_colors_max_min( &enc_c0, &enc_c1, channels, uncompressed );
|
||||||
|
/* store the 565 color 0 and color 1 */
|
||||||
|
compressed[0] = (enc_c0 >> 0) & 255;
|
||||||
|
compressed[1] = (enc_c0 >> 8) & 255;
|
||||||
|
compressed[2] = (enc_c1 >> 0) & 255;
|
||||||
|
compressed[3] = (enc_c1 >> 8) & 255;
|
||||||
|
/* zero out the compressed data */
|
||||||
|
compressed[4] = 0;
|
||||||
|
compressed[5] = 0;
|
||||||
|
compressed[6] = 0;
|
||||||
|
compressed[7] = 0;
|
||||||
|
/* reconstitute the master color vectors */
|
||||||
|
rgb_888_from_565( enc_c0, &c0[0], &c0[1], &c0[2] );
|
||||||
|
rgb_888_from_565( enc_c1, &c1[0], &c1[1], &c1[2] );
|
||||||
|
/* the new vector */
|
||||||
|
vec_len2 = 0.0f;
|
||||||
|
for( i = 0; i < 3; ++i )
|
||||||
|
{
|
||||||
|
color_line[i] = (float)(c1[i] - c0[i]);
|
||||||
|
vec_len2 += color_line[i] * color_line[i];
|
||||||
|
}
|
||||||
|
if( vec_len2 > 0.0f )
|
||||||
|
{
|
||||||
|
vec_len2 = 1.0f / vec_len2;
|
||||||
|
}
|
||||||
|
/* pre-proform the scaling */
|
||||||
|
color_line[0] *= vec_len2;
|
||||||
|
color_line[1] *= vec_len2;
|
||||||
|
color_line[2] *= vec_len2;
|
||||||
|
/* compute the offset (constant) portion of the dot product */
|
||||||
|
dot_offset = color_line[0]*c0[0] + color_line[1]*c0[1] + color_line[2]*c0[2];
|
||||||
|
/* store the rest of the bits */
|
||||||
|
next_bit = 8*4;
|
||||||
|
for( i = 0; i < 16; ++i )
|
||||||
|
{
|
||||||
|
/* find the dot product of this color, to place it on the line
|
||||||
|
(should be [-1,1]) */
|
||||||
|
int next_value = 0;
|
||||||
|
float dot_product =
|
||||||
|
color_line[0] * uncompressed[i*channels+0] +
|
||||||
|
color_line[1] * uncompressed[i*channels+1] +
|
||||||
|
color_line[2] * uncompressed[i*channels+2] -
|
||||||
|
dot_offset;
|
||||||
|
/* map to [0,3] */
|
||||||
|
next_value = (int)( dot_product * 3.0f + 0.5f );
|
||||||
|
if( next_value > 3 )
|
||||||
|
{
|
||||||
|
next_value = 3;
|
||||||
|
} else if( next_value < 0 )
|
||||||
|
{
|
||||||
|
next_value = 0;
|
||||||
|
}
|
||||||
|
/* OK, store this value */
|
||||||
|
compressed[next_bit >> 3] |= swizzle4[ next_value ] << (next_bit & 7);
|
||||||
|
next_bit += 2;
|
||||||
|
}
|
||||||
|
/* done compressing to DXT1 */
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
compress_DDS_alpha_block
|
||||||
|
(
|
||||||
|
const unsigned char *const uncompressed,
|
||||||
|
unsigned char compressed[8]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
/* variables */
|
||||||
|
int i;
|
||||||
|
int next_bit;
|
||||||
|
int a0, a1;
|
||||||
|
float scale_me;
|
||||||
|
/* stupid order */
|
||||||
|
int swizzle8[] = { 1, 7, 6, 5, 4, 3, 2, 0 };
|
||||||
|
/* get the alpha limits (a0 > a1) */
|
||||||
|
a0 = a1 = uncompressed[3];
|
||||||
|
for( i = 4+3; i < 16*4; i += 4 )
|
||||||
|
{
|
||||||
|
if( uncompressed[i] > a0 )
|
||||||
|
{
|
||||||
|
a0 = uncompressed[i];
|
||||||
|
} else if( uncompressed[i] < a1 )
|
||||||
|
{
|
||||||
|
a1 = uncompressed[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* store those limits, and zero the rest of the compressed dataset */
|
||||||
|
compressed[0] = a0;
|
||||||
|
compressed[1] = a1;
|
||||||
|
/* zero out the compressed data */
|
||||||
|
compressed[2] = 0;
|
||||||
|
compressed[3] = 0;
|
||||||
|
compressed[4] = 0;
|
||||||
|
compressed[5] = 0;
|
||||||
|
compressed[6] = 0;
|
||||||
|
compressed[7] = 0;
|
||||||
|
/* store the all of the alpha values */
|
||||||
|
next_bit = 8*2;
|
||||||
|
scale_me = 7.9999f / (a0 - a1);
|
||||||
|
for( i = 3; i < 16*4; i += 4 )
|
||||||
|
{
|
||||||
|
/* convert this alpha value to a 3 bit number */
|
||||||
|
int svalue;
|
||||||
|
int value = (int)((uncompressed[i] - a1) * scale_me);
|
||||||
|
svalue = swizzle8[ value&7 ];
|
||||||
|
/* OK, store this value, start with the 1st byte */
|
||||||
|
compressed[next_bit >> 3] |= svalue << (next_bit & 7);
|
||||||
|
if( (next_bit & 7) > 5 )
|
||||||
|
{
|
||||||
|
/* spans 2 bytes, fill in the start of the 2nd byte */
|
||||||
|
compressed[1 + (next_bit >> 3)] |= svalue >> (8 - (next_bit & 7) );
|
||||||
|
}
|
||||||
|
next_bit += 3;
|
||||||
|
}
|
||||||
|
/* done compressing to DXT1 */
|
||||||
|
}
|
123
lib/soil2/image_DXT.h
Normal file
123
lib/soil2/image_DXT.h
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
/*
|
||||||
|
Jonathan Dummer
|
||||||
|
2007-07-31-10.32
|
||||||
|
|
||||||
|
simple DXT compression / decompression code
|
||||||
|
|
||||||
|
public domain
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef HEADER_IMAGE_DXT
|
||||||
|
#define HEADER_IMAGE_DXT
|
||||||
|
|
||||||
|
/**
|
||||||
|
Converts an image from an array of unsigned chars (RGB or RGBA) to
|
||||||
|
DXT1 or DXT5, then saves the converted image to disk.
|
||||||
|
\return 0 if failed, otherwise returns 1
|
||||||
|
**/
|
||||||
|
int
|
||||||
|
save_image_as_DDS
|
||||||
|
(
|
||||||
|
const char *filename,
|
||||||
|
int width, int height, int channels,
|
||||||
|
const unsigned char *const data
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
take an image and convert it to DXT1 (no alpha)
|
||||||
|
**/
|
||||||
|
unsigned char*
|
||||||
|
convert_image_to_DXT1
|
||||||
|
(
|
||||||
|
const unsigned char *const uncompressed,
|
||||||
|
int width, int height, int channels,
|
||||||
|
int *out_size
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
take an image and convert it to DXT5 (with alpha)
|
||||||
|
**/
|
||||||
|
unsigned char*
|
||||||
|
convert_image_to_DXT5
|
||||||
|
(
|
||||||
|
const unsigned char *const uncompressed,
|
||||||
|
int width, int height, int channels,
|
||||||
|
int *out_size
|
||||||
|
);
|
||||||
|
|
||||||
|
/** A bunch of DirectDraw Surface structures and flags **/
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
unsigned int dwMagic;
|
||||||
|
unsigned int dwSize;
|
||||||
|
unsigned int dwFlags;
|
||||||
|
unsigned int dwHeight;
|
||||||
|
unsigned int dwWidth;
|
||||||
|
unsigned int dwPitchOrLinearSize;
|
||||||
|
unsigned int dwDepth;
|
||||||
|
unsigned int dwMipMapCount;
|
||||||
|
unsigned int dwReserved1[ 11 ];
|
||||||
|
|
||||||
|
/* DDPIXELFORMAT */
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
unsigned int dwSize;
|
||||||
|
unsigned int dwFlags;
|
||||||
|
unsigned int dwFourCC;
|
||||||
|
unsigned int dwRGBBitCount;
|
||||||
|
unsigned int dwRBitMask;
|
||||||
|
unsigned int dwGBitMask;
|
||||||
|
unsigned int dwBBitMask;
|
||||||
|
unsigned int dwAlphaBitMask;
|
||||||
|
}
|
||||||
|
sPixelFormat;
|
||||||
|
|
||||||
|
/* DDCAPS2 */
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
unsigned int dwCaps1;
|
||||||
|
unsigned int dwCaps2;
|
||||||
|
unsigned int dwDDSX;
|
||||||
|
unsigned int dwReserved;
|
||||||
|
}
|
||||||
|
sCaps;
|
||||||
|
unsigned int dwReserved2;
|
||||||
|
}
|
||||||
|
DDS_header ;
|
||||||
|
|
||||||
|
/* the following constants were copied directly off the MSDN website */
|
||||||
|
|
||||||
|
/* The dwFlags member of the original DDSURFACEDESC2 structure
|
||||||
|
can be set to one or more of the following values. */
|
||||||
|
#define DDSD_CAPS 0x00000001
|
||||||
|
#define DDSD_HEIGHT 0x00000002
|
||||||
|
#define DDSD_WIDTH 0x00000004
|
||||||
|
#define DDSD_PITCH 0x00000008
|
||||||
|
#define DDSD_PIXELFORMAT 0x00001000
|
||||||
|
#define DDSD_MIPMAPCOUNT 0x00020000
|
||||||
|
#define DDSD_LINEARSIZE 0x00080000
|
||||||
|
#define DDSD_DEPTH 0x00800000
|
||||||
|
|
||||||
|
/* DirectDraw Pixel Format */
|
||||||
|
#define DDPF_ALPHAPIXELS 0x00000001
|
||||||
|
#define DDPF_FOURCC 0x00000004
|
||||||
|
#define DDPF_RGB 0x00000040
|
||||||
|
|
||||||
|
/* The dwCaps1 member of the DDSCAPS2 structure can be
|
||||||
|
set to one or more of the following values. */
|
||||||
|
#define DDSCAPS_COMPLEX 0x00000008
|
||||||
|
#define DDSCAPS_TEXTURE 0x00001000
|
||||||
|
#define DDSCAPS_MIPMAP 0x00400000
|
||||||
|
|
||||||
|
/* The dwCaps2 member of the DDSCAPS2 structure can be
|
||||||
|
set to one or more of the following values. */
|
||||||
|
#define DDSCAPS2_CUBEMAP 0x00000200
|
||||||
|
#define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
|
||||||
|
#define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
|
||||||
|
#define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
|
||||||
|
#define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
|
||||||
|
#define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
|
||||||
|
#define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
|
||||||
|
#define DDSCAPS2_VOLUME 0x00200000
|
||||||
|
|
||||||
|
#endif /* HEADER_IMAGE_DXT */
|
435
lib/soil2/image_helper.c
Normal file
435
lib/soil2/image_helper.c
Normal file
|
@ -0,0 +1,435 @@
|
||||||
|
/*
|
||||||
|
Jonathan Dummer
|
||||||
|
|
||||||
|
image helper functions
|
||||||
|
|
||||||
|
MIT license
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "image_helper.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/* Upscaling the image uses simple bilinear interpolation */
|
||||||
|
int
|
||||||
|
up_scale_image
|
||||||
|
(
|
||||||
|
const unsigned char* const orig,
|
||||||
|
int width, int height, int channels,
|
||||||
|
unsigned char* resampled,
|
||||||
|
int resampled_width, int resampled_height
|
||||||
|
)
|
||||||
|
{
|
||||||
|
float dx, dy;
|
||||||
|
int x, y, c;
|
||||||
|
|
||||||
|
/* error(s) check */
|
||||||
|
if ( (width < 1) || (height < 1) ||
|
||||||
|
(resampled_width < 2) || (resampled_height < 2) ||
|
||||||
|
(channels < 1) ||
|
||||||
|
(NULL == orig) || (NULL == resampled) )
|
||||||
|
{
|
||||||
|
/* signify badness */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
for each given pixel in the new map, find the exact location
|
||||||
|
from the original map which would contribute to this guy
|
||||||
|
*/
|
||||||
|
dx = (width - 1.0f) / (resampled_width - 1.0f);
|
||||||
|
dy = (height - 1.0f) / (resampled_height - 1.0f);
|
||||||
|
for ( y = 0; y < resampled_height; ++y )
|
||||||
|
{
|
||||||
|
/* find the base y index and fractional offset from that */
|
||||||
|
float sampley = y * dy;
|
||||||
|
int inty = (int)sampley;
|
||||||
|
/* if( inty < 0 ) { inty = 0; } else */
|
||||||
|
if( inty > height - 2 ) { inty = height - 2; }
|
||||||
|
sampley -= inty;
|
||||||
|
for ( x = 0; x < resampled_width; ++x )
|
||||||
|
{
|
||||||
|
float samplex = x * dx;
|
||||||
|
int intx = (int)samplex;
|
||||||
|
int base_index;
|
||||||
|
/* find the base x index and fractional offset from that */
|
||||||
|
/* if( intx < 0 ) { intx = 0; } else */
|
||||||
|
if( intx > width - 2 ) { intx = width - 2; }
|
||||||
|
samplex -= intx;
|
||||||
|
/* base index into the original image */
|
||||||
|
base_index = (inty * width + intx) * channels;
|
||||||
|
for ( c = 0; c < channels; ++c )
|
||||||
|
{
|
||||||
|
/* do the sampling */
|
||||||
|
float value = 0.5f;
|
||||||
|
value += orig[base_index]
|
||||||
|
*(1.0f-samplex)*(1.0f-sampley);
|
||||||
|
value += orig[base_index+channels]
|
||||||
|
*(samplex)*(1.0f-sampley);
|
||||||
|
value += orig[base_index+width*channels]
|
||||||
|
*(1.0f-samplex)*(sampley);
|
||||||
|
value += orig[base_index+width*channels+channels]
|
||||||
|
*(samplex)*(sampley);
|
||||||
|
/* move to the next channel */
|
||||||
|
++base_index;
|
||||||
|
/* save the new value */
|
||||||
|
resampled[y*resampled_width*channels+x*channels+c] =
|
||||||
|
(unsigned char)(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* done */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mipmap_image
|
||||||
|
(
|
||||||
|
const unsigned char* const orig,
|
||||||
|
int width, int height, int channels,
|
||||||
|
unsigned char* resampled,
|
||||||
|
int block_size_x, int block_size_y
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int mip_width, mip_height;
|
||||||
|
int i, j, c;
|
||||||
|
|
||||||
|
/* error check */
|
||||||
|
if( (width < 1) || (height < 1) ||
|
||||||
|
(channels < 1) || (orig == NULL) ||
|
||||||
|
(resampled == NULL) ||
|
||||||
|
(block_size_x < 1) || (block_size_y < 1) )
|
||||||
|
{
|
||||||
|
/* nothing to do */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
mip_width = width / block_size_x;
|
||||||
|
mip_height = height / block_size_y;
|
||||||
|
if( mip_width < 1 )
|
||||||
|
{
|
||||||
|
mip_width = 1;
|
||||||
|
}
|
||||||
|
if( mip_height < 1 )
|
||||||
|
{
|
||||||
|
mip_height = 1;
|
||||||
|
}
|
||||||
|
for( j = 0; j < mip_height; ++j )
|
||||||
|
{
|
||||||
|
for( i = 0; i < mip_width; ++i )
|
||||||
|
{
|
||||||
|
for( c = 0; c < channels; ++c )
|
||||||
|
{
|
||||||
|
const int index = (j*block_size_y)*width*channels + (i*block_size_x)*channels + c;
|
||||||
|
int sum_value;
|
||||||
|
int u,v;
|
||||||
|
int u_block = block_size_x;
|
||||||
|
int v_block = block_size_y;
|
||||||
|
int block_area;
|
||||||
|
/* do a bit of checking so we don't over-run the boundaries
|
||||||
|
(necessary for non-square textures!) */
|
||||||
|
if( block_size_x * (i+1) > width )
|
||||||
|
{
|
||||||
|
u_block = width - i*block_size_y;
|
||||||
|
}
|
||||||
|
if( block_size_y * (j+1) > height )
|
||||||
|
{
|
||||||
|
v_block = height - j*block_size_y;
|
||||||
|
}
|
||||||
|
block_area = u_block*v_block;
|
||||||
|
/* for this pixel, see what the average
|
||||||
|
of all the values in the block are.
|
||||||
|
note: start the sum at the rounding value, not at 0 */
|
||||||
|
sum_value = block_area >> 1;
|
||||||
|
for( v = 0; v < v_block; ++v )
|
||||||
|
for( u = 0; u < u_block; ++u )
|
||||||
|
{
|
||||||
|
sum_value += orig[index + v*width*channels + u*channels];
|
||||||
|
}
|
||||||
|
resampled[j*mip_width*channels + i*channels + c] = sum_value / block_area;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
scale_image_RGB_to_NTSC_safe
|
||||||
|
(
|
||||||
|
unsigned char* orig,
|
||||||
|
int width, int height, int channels
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const float scale_lo = 16.0f - 0.499f;
|
||||||
|
const float scale_hi = 235.0f + 0.499f;
|
||||||
|
int i, j;
|
||||||
|
int nc = channels;
|
||||||
|
unsigned char scale_LUT[256];
|
||||||
|
/* error check */
|
||||||
|
if( (width < 1) || (height < 1) ||
|
||||||
|
(channels < 1) || (orig == NULL) )
|
||||||
|
{
|
||||||
|
/* nothing to do */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/* set up the scaling Look Up Table */
|
||||||
|
for( i = 0; i < 256; ++i )
|
||||||
|
{
|
||||||
|
scale_LUT[i] = (unsigned char)((scale_hi - scale_lo) * i / 255.0f + scale_lo);
|
||||||
|
}
|
||||||
|
/* for channels = 2 or 4, ignore the alpha component */
|
||||||
|
nc -= 1 - (channels & 1);
|
||||||
|
/* OK, go through the image and scale any non-alpha components */
|
||||||
|
for( i = 0; i < width*height*channels; i += channels )
|
||||||
|
{
|
||||||
|
for( j = 0; j < nc; ++j )
|
||||||
|
{
|
||||||
|
orig[i+j] = scale_LUT[orig[i+j]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char clamp_byte( int x ) { return ( (x) < 0 ? (0) : ( (x) > 255 ? 255 : (x) ) ); }
|
||||||
|
|
||||||
|
/*
|
||||||
|
This function takes the RGB components of the image
|
||||||
|
and converts them into YCoCg. 3 components will be
|
||||||
|
re-ordered to CoYCg (for optimum DXT1 compression),
|
||||||
|
while 4 components will be ordered CoCgAY (for DXT5
|
||||||
|
compression).
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
convert_RGB_to_YCoCg
|
||||||
|
(
|
||||||
|
unsigned char* orig,
|
||||||
|
int width, int height, int channels
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
/* error check */
|
||||||
|
if( (width < 1) || (height < 1) ||
|
||||||
|
(channels < 3) || (channels > 4) ||
|
||||||
|
(orig == NULL) )
|
||||||
|
{
|
||||||
|
/* nothing to do */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
/* do the conversion */
|
||||||
|
if( channels == 3 )
|
||||||
|
{
|
||||||
|
for( i = 0; i < width*height*3; i += 3 )
|
||||||
|
{
|
||||||
|
int r = orig[i+0];
|
||||||
|
int g = (orig[i+1] + 1) >> 1;
|
||||||
|
int b = orig[i+2];
|
||||||
|
int tmp = (2 + r + b) >> 2;
|
||||||
|
/* Co */
|
||||||
|
orig[i+0] = clamp_byte( 128 + ((r - b + 1) >> 1) );
|
||||||
|
/* Y */
|
||||||
|
orig[i+1] = clamp_byte( g + tmp );
|
||||||
|
/* Cg */
|
||||||
|
orig[i+2] = clamp_byte( 128 + g - tmp );
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
for( i = 0; i < width*height*4; i += 4 )
|
||||||
|
{
|
||||||
|
int r = orig[i+0];
|
||||||
|
int g = (orig[i+1] + 1) >> 1;
|
||||||
|
int b = orig[i+2];
|
||||||
|
unsigned char a = orig[i+3];
|
||||||
|
int tmp = (2 + r + b) >> 2;
|
||||||
|
/* Co */
|
||||||
|
orig[i+0] = clamp_byte( 128 + ((r - b + 1) >> 1) );
|
||||||
|
/* Cg */
|
||||||
|
orig[i+1] = clamp_byte( 128 + g - tmp );
|
||||||
|
/* Alpha */
|
||||||
|
orig[i+2] = a;
|
||||||
|
/* Y */
|
||||||
|
orig[i+3] = clamp_byte( g + tmp );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* done */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
This function takes the YCoCg components of the image
|
||||||
|
and converts them into RGB. See above.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
convert_YCoCg_to_RGB
|
||||||
|
(
|
||||||
|
unsigned char* orig,
|
||||||
|
int width, int height, int channels
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
/* error check */
|
||||||
|
if( (width < 1) || (height < 1) ||
|
||||||
|
(channels < 3) || (channels > 4) ||
|
||||||
|
(orig == NULL) )
|
||||||
|
{
|
||||||
|
/* nothing to do */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
/* do the conversion */
|
||||||
|
if( channels == 3 )
|
||||||
|
{
|
||||||
|
for( i = 0; i < width*height*3; i += 3 )
|
||||||
|
{
|
||||||
|
int co = orig[i+0] - 128;
|
||||||
|
int y = orig[i+1];
|
||||||
|
int cg = orig[i+2] - 128;
|
||||||
|
/* R */
|
||||||
|
orig[i+0] = clamp_byte( y + co - cg );
|
||||||
|
/* G */
|
||||||
|
orig[i+1] = clamp_byte( y + cg );
|
||||||
|
/* B */
|
||||||
|
orig[i+2] = clamp_byte( y - co - cg );
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
for( i = 0; i < width*height*4; i += 4 )
|
||||||
|
{
|
||||||
|
int co = orig[i+0] - 128;
|
||||||
|
int cg = orig[i+1] - 128;
|
||||||
|
unsigned char a = orig[i+2];
|
||||||
|
int y = orig[i+3];
|
||||||
|
/* R */
|
||||||
|
orig[i+0] = clamp_byte( y + co - cg );
|
||||||
|
/* G */
|
||||||
|
orig[i+1] = clamp_byte( y + cg );
|
||||||
|
/* B */
|
||||||
|
orig[i+2] = clamp_byte( y - co - cg );
|
||||||
|
/* A */
|
||||||
|
orig[i+3] = a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* done */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
float
|
||||||
|
find_max_RGBE
|
||||||
|
(
|
||||||
|
unsigned char *image,
|
||||||
|
int width, int height
|
||||||
|
)
|
||||||
|
{
|
||||||
|
float max_val = 0.0f;
|
||||||
|
unsigned char *img = image;
|
||||||
|
int i, j;
|
||||||
|
for( i = width * height; i > 0; --i )
|
||||||
|
{
|
||||||
|
/* float scale = powf( 2.0f, img[3] - 128.0f ) / 255.0f; */
|
||||||
|
float scale = ldexp( 1.0f / 255.0f, (int)(img[3]) - 128 );
|
||||||
|
for( j = 0; j < 3; ++j )
|
||||||
|
{
|
||||||
|
if( img[j] * scale > max_val )
|
||||||
|
{
|
||||||
|
max_val = img[j] * scale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* next pixel */
|
||||||
|
img += 4;
|
||||||
|
}
|
||||||
|
return max_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
RGBE_to_RGBdivA
|
||||||
|
(
|
||||||
|
unsigned char *image,
|
||||||
|
int width, int height,
|
||||||
|
int rescale_to_max
|
||||||
|
)
|
||||||
|
{
|
||||||
|
/* local variables */
|
||||||
|
int i, iv;
|
||||||
|
unsigned char *img = image;
|
||||||
|
float scale = 1.0f;
|
||||||
|
/* error check */
|
||||||
|
if( (!image) || (width < 1) || (height < 1) )
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/* convert (note: no negative numbers, but 0.0 is possible) */
|
||||||
|
if( rescale_to_max )
|
||||||
|
{
|
||||||
|
scale = 255.0f / find_max_RGBE( image, width, height );
|
||||||
|
}
|
||||||
|
for( i = width * height; i > 0; --i )
|
||||||
|
{
|
||||||
|
/* decode this pixel, and find the max */
|
||||||
|
float r,g,b,e, m;
|
||||||
|
/* e = scale * powf( 2.0f, img[3] - 128.0f ) / 255.0f; */
|
||||||
|
e = scale * ldexp( 1.0f / 255.0f, (int)(img[3]) - 128 );
|
||||||
|
r = e * img[0];
|
||||||
|
g = e * img[1];
|
||||||
|
b = e * img[2];
|
||||||
|
m = (r > g) ? r : g;
|
||||||
|
m = (b > m) ? b : m;
|
||||||
|
/* and encode it into RGBdivA */
|
||||||
|
iv = (m != 0.0f) ? (int)(255.0f / m) : 1.0f;
|
||||||
|
iv = (iv < 1) ? 1 : iv;
|
||||||
|
img[3] = (iv > 255) ? 255 : iv;
|
||||||
|
iv = (int)(img[3] * r + 0.5f);
|
||||||
|
img[0] = (iv > 255) ? 255 : iv;
|
||||||
|
iv = (int)(img[3] * g + 0.5f);
|
||||||
|
img[1] = (iv > 255) ? 255 : iv;
|
||||||
|
iv = (int)(img[3] * b + 0.5f);
|
||||||
|
img[2] = (iv > 255) ? 255 : iv;
|
||||||
|
/* and on to the next pixel */
|
||||||
|
img += 4;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
RGBE_to_RGBdivA2
|
||||||
|
(
|
||||||
|
unsigned char *image,
|
||||||
|
int width, int height,
|
||||||
|
int rescale_to_max
|
||||||
|
)
|
||||||
|
{
|
||||||
|
/* local variables */
|
||||||
|
int i, iv;
|
||||||
|
unsigned char *img = image;
|
||||||
|
float scale = 1.0f;
|
||||||
|
/* error check */
|
||||||
|
if( (!image) || (width < 1) || (height < 1) )
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/* convert (note: no negative numbers, but 0.0 is possible) */
|
||||||
|
if( rescale_to_max )
|
||||||
|
{
|
||||||
|
scale = 255.0f * 255.0f / find_max_RGBE( image, width, height );
|
||||||
|
}
|
||||||
|
for( i = width * height; i > 0; --i )
|
||||||
|
{
|
||||||
|
/* decode this pixel, and find the max */
|
||||||
|
float r,g,b,e, m;
|
||||||
|
/* e = scale * powf( 2.0f, img[3] - 128.0f ) / 255.0f; */
|
||||||
|
e = scale * ldexp( 1.0f / 255.0f, (int)(img[3]) - 128 );
|
||||||
|
r = e * img[0];
|
||||||
|
g = e * img[1];
|
||||||
|
b = e * img[2];
|
||||||
|
m = (r > g) ? r : g;
|
||||||
|
m = (b > m) ? b : m;
|
||||||
|
/* and encode it into RGBdivA */
|
||||||
|
iv = (m != 0.0f) ? (int)sqrtf( 255.0f * 255.0f / m ) : 1.0f;
|
||||||
|
iv = (iv < 1) ? 1 : iv;
|
||||||
|
img[3] = (iv > 255) ? 255 : iv;
|
||||||
|
iv = (int)(img[3] * img[3] * r / 255.0f + 0.5f);
|
||||||
|
img[0] = (iv > 255) ? 255 : iv;
|
||||||
|
iv = (int)(img[3] * img[3] * g / 255.0f + 0.5f);
|
||||||
|
img[1] = (iv > 255) ? 255 : iv;
|
||||||
|
iv = (int)(img[3] * img[3] * b / 255.0f + 0.5f);
|
||||||
|
img[2] = (iv > 255) ? 255 : iv;
|
||||||
|
/* and on to the next pixel */
|
||||||
|
img += 4;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
115
lib/soil2/image_helper.h
Normal file
115
lib/soil2/image_helper.h
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
/*
|
||||||
|
Jonathan Dummer
|
||||||
|
|
||||||
|
Image helper functions
|
||||||
|
|
||||||
|
MIT license
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef HEADER_IMAGE_HELPER
|
||||||
|
#define HEADER_IMAGE_HELPER
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
This function upscales an image.
|
||||||
|
Not to be used to create MIPmaps,
|
||||||
|
but to make it square,
|
||||||
|
or to make it a power-of-two sized.
|
||||||
|
**/
|
||||||
|
int
|
||||||
|
up_scale_image
|
||||||
|
(
|
||||||
|
const unsigned char* const orig,
|
||||||
|
int width, int height, int channels,
|
||||||
|
unsigned char* resampled,
|
||||||
|
int resampled_width, int resampled_height
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
This function downscales an image.
|
||||||
|
Used for creating MIPmaps,
|
||||||
|
the incoming image should be a
|
||||||
|
power-of-two sized.
|
||||||
|
**/
|
||||||
|
int
|
||||||
|
mipmap_image
|
||||||
|
(
|
||||||
|
const unsigned char* const orig,
|
||||||
|
int width, int height, int channels,
|
||||||
|
unsigned char* resampled,
|
||||||
|
int block_size_x, int block_size_y
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
This function takes the RGB components of the image
|
||||||
|
and scales each channel from [0,255] to [16,235].
|
||||||
|
This makes the colors "Safe" for display on NTSC
|
||||||
|
displays. Note that this is _NOT_ a good idea for
|
||||||
|
loading images like normal- or height-maps!
|
||||||
|
**/
|
||||||
|
int
|
||||||
|
scale_image_RGB_to_NTSC_safe
|
||||||
|
(
|
||||||
|
unsigned char* orig,
|
||||||
|
int width, int height, int channels
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
This function takes the RGB components of the image
|
||||||
|
and converts them into YCoCg. 3 components will be
|
||||||
|
re-ordered to CoYCg (for optimum DXT1 compression),
|
||||||
|
while 4 components will be ordered CoCgAY (for DXT5
|
||||||
|
compression).
|
||||||
|
**/
|
||||||
|
int
|
||||||
|
convert_RGB_to_YCoCg
|
||||||
|
(
|
||||||
|
unsigned char* orig,
|
||||||
|
int width, int height, int channels
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
This function takes the YCoCg components of the image
|
||||||
|
and converts them into RGB. See above.
|
||||||
|
**/
|
||||||
|
int
|
||||||
|
convert_YCoCg_to_RGB
|
||||||
|
(
|
||||||
|
unsigned char* orig,
|
||||||
|
int width, int height, int channels
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Converts an HDR image from an array
|
||||||
|
of unsigned chars (RGBE) to RGBdivA
|
||||||
|
\return 0 if failed, otherwise returns 1
|
||||||
|
**/
|
||||||
|
int
|
||||||
|
RGBE_to_RGBdivA
|
||||||
|
(
|
||||||
|
unsigned char *image,
|
||||||
|
int width, int height,
|
||||||
|
int rescale_to_max
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Converts an HDR image from an array
|
||||||
|
of unsigned chars (RGBE) to RGBdivA2
|
||||||
|
\return 0 if failed, otherwise returns 1
|
||||||
|
**/
|
||||||
|
int
|
||||||
|
RGBE_to_RGBdivA2
|
||||||
|
(
|
||||||
|
unsigned char *image,
|
||||||
|
int width, int height,
|
||||||
|
int rescale_to_max
|
||||||
|
);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* HEADER_IMAGE_HELPER */
|
3682
lib/soil2/stb_image_aug.c
Normal file
3682
lib/soil2/stb_image_aug.c
Normal file
File diff suppressed because it is too large
Load diff
354
lib/soil2/stb_image_aug.h
Normal file
354
lib/soil2/stb_image_aug.h
Normal file
|
@ -0,0 +1,354 @@
|
||||||
|
/* stbi-1.16 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
|
||||||
|
when you control the images you're loading
|
||||||
|
|
||||||
|
QUICK NOTES:
|
||||||
|
Primarily of interest to game developers and other people who can
|
||||||
|
avoid problematic images and only need the trivial interface
|
||||||
|
|
||||||
|
JPEG baseline (no JPEG progressive, no oddball channel decimations)
|
||||||
|
PNG non-interlaced
|
||||||
|
BMP non-1bpp, non-RLE
|
||||||
|
TGA (not sure what subset, if a subset)
|
||||||
|
PSD (composited view only, no extra channels)
|
||||||
|
HDR (radiance rgbE format)
|
||||||
|
writes BMP,TGA (define STBI_NO_WRITE to remove code)
|
||||||
|
decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code)
|
||||||
|
supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
|
||||||
|
|
||||||
|
TODO:
|
||||||
|
stbi_info_*
|
||||||
|
|
||||||
|
history:
|
||||||
|
1.16 major bugfix - convert_format converted one too many pixels
|
||||||
|
1.15 initialize some fields for thread safety
|
||||||
|
1.14 fix threadsafe conversion bug; header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
|
||||||
|
1.13 threadsafe
|
||||||
|
1.12 const qualifiers in the API
|
||||||
|
1.11 Support installable IDCT, colorspace conversion routines
|
||||||
|
1.10 Fixes for 64-bit (don't use "unsigned long")
|
||||||
|
optimized upsampling by Fabian "ryg" Giesen
|
||||||
|
1.09 Fix format-conversion for PSD code (bad global variables!)
|
||||||
|
1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
|
||||||
|
1.07 attempt to fix C++ warning/errors again
|
||||||
|
1.06 attempt to fix C++ warning/errors again
|
||||||
|
1.05 fix TGA loading to return correct *comp and use good luminance calc
|
||||||
|
1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
|
||||||
|
1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
|
||||||
|
1.02 support for (subset of) HDR files, float interface for preferred access to them
|
||||||
|
1.01 fix bug: possible bug in handling right-side up bmps... not sure
|
||||||
|
fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all
|
||||||
|
1.00 interface to zlib that skips zlib header
|
||||||
|
0.99 correct handling of alpha in palette
|
||||||
|
0.98 TGA loader by lonesock; dynamically add loaders (untested)
|
||||||
|
0.97 jpeg errors on too large a file; also catch another malloc failure
|
||||||
|
0.96 fix detection of invalid v value - particleman@mollyrocket forum
|
||||||
|
0.95 during header scan, seek to markers in case of padding
|
||||||
|
0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
|
||||||
|
0.93 handle jpegtran output; verbose errors
|
||||||
|
0.92 read 4,8,16,24,32-bit BMP files of several formats
|
||||||
|
0.91 output 24-bit Windows 3.0 BMP files
|
||||||
|
0.90 fix a few more warnings; bump version number to approach 1.0
|
||||||
|
0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
|
||||||
|
0.60 fix compiling as c++
|
||||||
|
0.59 fix warnings: merge Dave Moore's -Wall fixes
|
||||||
|
0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
|
||||||
|
0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less
|
||||||
|
than 16 available
|
||||||
|
0.56 fix bug: zlib uncompressed mode len vs. nlen
|
||||||
|
0.55 fix bug: restart_interval not initialized to 0
|
||||||
|
0.54 allow NULL for 'int *comp'
|
||||||
|
0.53 fix bug in png 3->4; speedup png decoding
|
||||||
|
0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
|
||||||
|
0.51 obey req_comp requests, 1-component jpegs return as 1-component,
|
||||||
|
on 'test' only check type, not whether we support this variant
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef HEADER_STB_IMAGE_AUGMENTED
|
||||||
|
#define HEADER_STB_IMAGE_AUGMENTED
|
||||||
|
|
||||||
|
//// begin header file ////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// Limitations:
|
||||||
|
// - no progressive/interlaced support (jpeg, png)
|
||||||
|
// - 8-bit samples only (jpeg, png)
|
||||||
|
// - not threadsafe
|
||||||
|
// - channel subsampling of at most 2 in each dimension (jpeg)
|
||||||
|
// - no delayed line count (jpeg) -- IJG doesn't support either
|
||||||
|
//
|
||||||
|
// Basic usage (see HDR discussion below):
|
||||||
|
// int x,y,n;
|
||||||
|
// unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
|
||||||
|
// // ... process data if not NULL ...
|
||||||
|
// // ... x = width, y = height, n = # 8-bit components per pixel ...
|
||||||
|
// // ... replace '0' with '1'..'4' to force that many components per pixel
|
||||||
|
// stbi_image_free(data)
|
||||||
|
//
|
||||||
|
// Standard parameters:
|
||||||
|
// int *x -- outputs image width in pixels
|
||||||
|
// int *y -- outputs image height in pixels
|
||||||
|
// int *comp -- outputs # of image components in image file
|
||||||
|
// int req_comp -- if non-zero, # of image components requested in result
|
||||||
|
//
|
||||||
|
// The return value from an image loader is an 'unsigned char *' which points
|
||||||
|
// to the pixel data. The pixel data consists of *y scanlines of *x pixels,
|
||||||
|
// with each pixel consisting of N interleaved 8-bit components; the first
|
||||||
|
// pixel pointed to is top-left-most in the image. There is no padding between
|
||||||
|
// image scanlines or between pixels, regardless of format. The number of
|
||||||
|
// components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
|
||||||
|
// If req_comp is non-zero, *comp has the number of components that _would_
|
||||||
|
// have been output otherwise. E.g. if you set req_comp to 4, you will always
|
||||||
|
// get RGBA output, but you can check *comp to easily see if it's opaque.
|
||||||
|
//
|
||||||
|
// An output image with N components has the following components interleaved
|
||||||
|
// in this order in each pixel:
|
||||||
|
//
|
||||||
|
// N=#comp components
|
||||||
|
// 1 grey
|
||||||
|
// 2 grey, alpha
|
||||||
|
// 3 red, green, blue
|
||||||
|
// 4 red, green, blue, alpha
|
||||||
|
//
|
||||||
|
// If image loading fails for any reason, the return value will be NULL,
|
||||||
|
// and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
|
||||||
|
// can be queried for an extremely brief, end-user unfriendly explanation
|
||||||
|
// of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
|
||||||
|
// compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
|
||||||
|
// more user-friendly ones.
|
||||||
|
//
|
||||||
|
// Paletted PNG and BMP images are automatically depalettized.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// ===========================================================================
|
||||||
|
//
|
||||||
|
// HDR image support (disable by defining STBI_NO_HDR)
|
||||||
|
//
|
||||||
|
// stb_image now supports loading HDR images in general, and currently
|
||||||
|
// the Radiance .HDR file format, although the support is provided
|
||||||
|
// generically. You can still load any file through the existing interface;
|
||||||
|
// if you attempt to load an HDR file, it will be automatically remapped to
|
||||||
|
// LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
|
||||||
|
// both of these constants can be reconfigured through this interface:
|
||||||
|
//
|
||||||
|
// stbi_hdr_to_ldr_gamma(2.2f);
|
||||||
|
// stbi_hdr_to_ldr_scale(1.0f);
|
||||||
|
//
|
||||||
|
// (note, do not use _inverse_ constants; stbi_image will invert them
|
||||||
|
// appropriately).
|
||||||
|
//
|
||||||
|
// Additionally, there is a new, parallel interface for loading files as
|
||||||
|
// (linear) floats to preserve the full dynamic range:
|
||||||
|
//
|
||||||
|
// float *data = stbi_loadf(filename, &x, &y, &n, 0);
|
||||||
|
//
|
||||||
|
// If you load LDR images through this interface, those images will
|
||||||
|
// be promoted to floating point values, run through the inverse of
|
||||||
|
// constants corresponding to the above:
|
||||||
|
//
|
||||||
|
// stbi_ldr_to_hdr_scale(1.0f);
|
||||||
|
// stbi_ldr_to_hdr_gamma(2.2f);
|
||||||
|
//
|
||||||
|
// Finally, given a filename (or an open file or memory block--see header
|
||||||
|
// file for details) containing image data, you can query for the "most
|
||||||
|
// appropriate" interface to use (that is, whether the image is HDR or
|
||||||
|
// not), using:
|
||||||
|
//
|
||||||
|
// stbi_is_hdr(char *filename);
|
||||||
|
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
#include <stdio.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define STBI_VERSION 1
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
STBI_default = 0, // only used for req_comp
|
||||||
|
|
||||||
|
STBI_grey = 1,
|
||||||
|
STBI_grey_alpha = 2,
|
||||||
|
STBI_rgb = 3,
|
||||||
|
STBI_rgb_alpha = 4,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef unsigned char stbi_uc;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// WRITING API
|
||||||
|
|
||||||
|
#if !defined(STBI_NO_WRITE) && !defined(STBI_NO_STDIO)
|
||||||
|
// write a BMP/TGA file given tightly packed 'comp' channels (no padding, nor bmp-stride-padding)
|
||||||
|
// (you must include the appropriate extension in the filename).
|
||||||
|
// returns TRUE on success, FALSE if couldn't open file, error writing file
|
||||||
|
extern int stbi_write_bmp (char const *filename, int x, int y, int comp, void *data);
|
||||||
|
extern int stbi_write_tga (char const *filename, int x, int y, int comp, void *data);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// PRIMARY API - works on images of any type
|
||||||
|
|
||||||
|
// load image by filename, open file, or memory buffer
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
extern stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
|
||||||
|
#endif
|
||||||
|
extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
// for stbi_load_from_file, file pointer is left pointing immediately after image
|
||||||
|
|
||||||
|
#ifndef STBI_NO_HDR
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
extern float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#endif
|
||||||
|
extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
|
||||||
|
extern void stbi_hdr_to_ldr_gamma(float gamma);
|
||||||
|
extern void stbi_hdr_to_ldr_scale(float scale);
|
||||||
|
|
||||||
|
extern void stbi_ldr_to_hdr_gamma(float gamma);
|
||||||
|
extern void stbi_ldr_to_hdr_scale(float scale);
|
||||||
|
|
||||||
|
#endif // STBI_NO_HDR
|
||||||
|
|
||||||
|
// get a VERY brief reason for failure
|
||||||
|
// NOT THREADSAFE
|
||||||
|
extern char *stbi_failure_reason (void);
|
||||||
|
|
||||||
|
// free the loaded image -- this is just free()
|
||||||
|
extern void stbi_image_free (void *retval_from_stbi_load);
|
||||||
|
|
||||||
|
// get image dimensions & components without fully decoding
|
||||||
|
extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
||||||
|
extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
extern int stbi_info (char const *filename, int *x, int *y, int *comp);
|
||||||
|
extern int stbi_is_hdr (char const *filename);
|
||||||
|
extern int stbi_is_hdr_from_file(FILE *f);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ZLIB client - used by PNG, available for other purposes
|
||||||
|
|
||||||
|
extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
|
||||||
|
extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
|
||||||
|
extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
|
||||||
|
|
||||||
|
extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
|
||||||
|
extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
|
||||||
|
|
||||||
|
// TYPE-SPECIFIC ACCESS
|
||||||
|
|
||||||
|
// is it a jpeg?
|
||||||
|
extern int stbi_jpeg_test_memory (stbi_uc const *buffer, int len);
|
||||||
|
extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
||||||
|
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
extern stbi_uc *stbi_jpeg_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern int stbi_jpeg_test_file (FILE *f);
|
||||||
|
extern stbi_uc *stbi_jpeg_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
|
||||||
|
extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp);
|
||||||
|
extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// is it a png?
|
||||||
|
extern int stbi_png_test_memory (stbi_uc const *buffer, int len);
|
||||||
|
extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern int stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
||||||
|
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
extern stbi_uc *stbi_png_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern int stbi_png_info (char const *filename, int *x, int *y, int *comp);
|
||||||
|
extern int stbi_png_test_file (FILE *f);
|
||||||
|
extern stbi_uc *stbi_png_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// is it a bmp?
|
||||||
|
extern int stbi_bmp_test_memory (stbi_uc const *buffer, int len);
|
||||||
|
|
||||||
|
extern stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
extern int stbi_bmp_test_file (FILE *f);
|
||||||
|
extern stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// is it a tga?
|
||||||
|
extern int stbi_tga_test_memory (stbi_uc const *buffer, int len);
|
||||||
|
|
||||||
|
extern stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
extern int stbi_tga_test_file (FILE *f);
|
||||||
|
extern stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// is it a psd?
|
||||||
|
extern int stbi_psd_test_memory (stbi_uc const *buffer, int len);
|
||||||
|
|
||||||
|
extern stbi_uc *stbi_psd_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
extern int stbi_psd_test_file (FILE *f);
|
||||||
|
extern stbi_uc *stbi_psd_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// is it an hdr?
|
||||||
|
extern int stbi_hdr_test_memory (stbi_uc const *buffer, int len);
|
||||||
|
|
||||||
|
extern float * stbi_hdr_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern stbi_uc *stbi_hdr_load_rgbe (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
extern int stbi_hdr_test_file (FILE *f);
|
||||||
|
extern float * stbi_hdr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern stbi_uc *stbi_hdr_load_rgbe_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// define new loaders
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int (*test_memory)(stbi_uc const *buffer, int len);
|
||||||
|
stbi_uc * (*load_from_memory)(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
int (*test_file)(FILE *f);
|
||||||
|
stbi_uc * (*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#endif
|
||||||
|
} stbi_loader;
|
||||||
|
|
||||||
|
// register a loader by filling out the above structure (you must defined ALL functions)
|
||||||
|
// returns 1 if added or already added, 0 if not added (too many loaders)
|
||||||
|
// NOT THREADSAFE
|
||||||
|
extern int stbi_register_loader(stbi_loader *loader);
|
||||||
|
|
||||||
|
// define faster low-level operations (typically SIMD support)
|
||||||
|
#if STBI_SIMD
|
||||||
|
typedef void (*stbi_idct_8x8)(uint8 *out, int out_stride, short data[64], unsigned short *dequantize);
|
||||||
|
// compute an integer IDCT on "input"
|
||||||
|
// input[x] = data[x] * dequantize[x]
|
||||||
|
// write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
|
||||||
|
// CLAMP results to 0..255
|
||||||
|
typedef void (*stbi_YCbCr_to_RGB_run)(uint8 *output, uint8 const *y, uint8 const *cb, uint8 const *cr, int count, int step);
|
||||||
|
// compute a conversion from YCbCr to RGB
|
||||||
|
// 'count' pixels
|
||||||
|
// write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
|
||||||
|
// y: Y input channel
|
||||||
|
// cb: Cb input channel; scale/biased to be 0..255
|
||||||
|
// cr: Cr input channel; scale/biased to be 0..255
|
||||||
|
|
||||||
|
extern void stbi_install_idct(stbi_idct_8x8 func);
|
||||||
|
extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
|
||||||
|
#endif // STBI_SIMD
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//// end header file /////////////////////////////////////////////////////
|
||||||
|
#endif // STBI_INCLUDE_STB_IMAGE_H
|
21
lib/soil2/stbi_DDS_aug.h
Normal file
21
lib/soil2/stbi_DDS_aug.h
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
/*
|
||||||
|
adding DDS loading support to stbi
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef HEADER_STB_IMAGE_DDS_AUGMENTATION
|
||||||
|
#define HEADER_STB_IMAGE_DDS_AUGMENTATION
|
||||||
|
|
||||||
|
// is it a DDS file?
|
||||||
|
extern int stbi_dds_test_memory (stbi_uc const *buffer, int len);
|
||||||
|
|
||||||
|
extern stbi_uc *stbi_dds_load (char *filename, int *x, int *y, int *comp, int req_comp);
|
||||||
|
extern stbi_uc *stbi_dds_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
extern int stbi_dds_test_file (FILE *f);
|
||||||
|
extern stbi_uc *stbi_dds_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//// end header file /////////////////////////////////////////////////////
|
||||||
|
#endif // HEADER_STB_IMAGE_DDS_AUGMENTATION
|
511
lib/soil2/stbi_DDS_aug_c.h
Normal file
511
lib/soil2/stbi_DDS_aug_c.h
Normal file
|
@ -0,0 +1,511 @@
|
||||||
|
|
||||||
|
/// DDS file support, does decoding, _not_ direct uploading
|
||||||
|
/// (use SOIL for that ;-)
|
||||||
|
|
||||||
|
/// A bunch of DirectDraw Surface structures and flags
|
||||||
|
typedef struct {
|
||||||
|
unsigned int dwMagic;
|
||||||
|
unsigned int dwSize;
|
||||||
|
unsigned int dwFlags;
|
||||||
|
unsigned int dwHeight;
|
||||||
|
unsigned int dwWidth;
|
||||||
|
unsigned int dwPitchOrLinearSize;
|
||||||
|
unsigned int dwDepth;
|
||||||
|
unsigned int dwMipMapCount;
|
||||||
|
unsigned int dwReserved1[ 11 ];
|
||||||
|
|
||||||
|
// DDPIXELFORMAT
|
||||||
|
struct {
|
||||||
|
unsigned int dwSize;
|
||||||
|
unsigned int dwFlags;
|
||||||
|
unsigned int dwFourCC;
|
||||||
|
unsigned int dwRGBBitCount;
|
||||||
|
unsigned int dwRBitMask;
|
||||||
|
unsigned int dwGBitMask;
|
||||||
|
unsigned int dwBBitMask;
|
||||||
|
unsigned int dwAlphaBitMask;
|
||||||
|
} sPixelFormat;
|
||||||
|
|
||||||
|
// DDCAPS2
|
||||||
|
struct {
|
||||||
|
unsigned int dwCaps1;
|
||||||
|
unsigned int dwCaps2;
|
||||||
|
unsigned int dwDDSX;
|
||||||
|
unsigned int dwReserved;
|
||||||
|
} sCaps;
|
||||||
|
unsigned int dwReserved2;
|
||||||
|
} DDS_header ;
|
||||||
|
|
||||||
|
// the following constants were copied directly off the MSDN website
|
||||||
|
|
||||||
|
// The dwFlags member of the original DDSURFACEDESC2 structure
|
||||||
|
// can be set to one or more of the following values.
|
||||||
|
#define DDSD_CAPS 0x00000001
|
||||||
|
#define DDSD_HEIGHT 0x00000002
|
||||||
|
#define DDSD_WIDTH 0x00000004
|
||||||
|
#define DDSD_PITCH 0x00000008
|
||||||
|
#define DDSD_PIXELFORMAT 0x00001000
|
||||||
|
#define DDSD_MIPMAPCOUNT 0x00020000
|
||||||
|
#define DDSD_LINEARSIZE 0x00080000
|
||||||
|
#define DDSD_DEPTH 0x00800000
|
||||||
|
|
||||||
|
// DirectDraw Pixel Format
|
||||||
|
#define DDPF_ALPHAPIXELS 0x00000001
|
||||||
|
#define DDPF_FOURCC 0x00000004
|
||||||
|
#define DDPF_RGB 0x00000040
|
||||||
|
|
||||||
|
// The dwCaps1 member of the DDSCAPS2 structure can be
|
||||||
|
// set to one or more of the following values.
|
||||||
|
#define DDSCAPS_COMPLEX 0x00000008
|
||||||
|
#define DDSCAPS_TEXTURE 0x00001000
|
||||||
|
#define DDSCAPS_MIPMAP 0x00400000
|
||||||
|
|
||||||
|
// The dwCaps2 member of the DDSCAPS2 structure can be
|
||||||
|
// set to one or more of the following values.
|
||||||
|
#define DDSCAPS2_CUBEMAP 0x00000200
|
||||||
|
#define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
|
||||||
|
#define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
|
||||||
|
#define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
|
||||||
|
#define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
|
||||||
|
#define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
|
||||||
|
#define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
|
||||||
|
#define DDSCAPS2_VOLUME 0x00200000
|
||||||
|
|
||||||
|
static int dds_test(stbi *s)
|
||||||
|
{
|
||||||
|
// check the magic number
|
||||||
|
if (get8(s) != 'D') return 0;
|
||||||
|
if (get8(s) != 'D') return 0;
|
||||||
|
if (get8(s) != 'S') return 0;
|
||||||
|
if (get8(s) != ' ') return 0;
|
||||||
|
// check header size
|
||||||
|
if (get32le(s) != 124) return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
int stbi_dds_test_file (FILE *f)
|
||||||
|
{
|
||||||
|
stbi s;
|
||||||
|
int r,n = ftell(f);
|
||||||
|
start_file(&s,f);
|
||||||
|
r = dds_test(&s);
|
||||||
|
fseek(f,n,SEEK_SET);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int stbi_dds_test_memory (stbi_uc const *buffer, int len)
|
||||||
|
{
|
||||||
|
stbi s;
|
||||||
|
start_mem(&s,buffer, len);
|
||||||
|
return dds_test(&s);
|
||||||
|
}
|
||||||
|
|
||||||
|
// helper functions
|
||||||
|
int stbi_convert_bit_range( int c, int from_bits, int to_bits )
|
||||||
|
{
|
||||||
|
int b = (1 << (from_bits - 1)) + c * ((1 << to_bits) - 1);
|
||||||
|
return (b + (b >> from_bits)) >> from_bits;
|
||||||
|
}
|
||||||
|
void stbi_rgb_888_from_565( unsigned int c, int *r, int *g, int *b )
|
||||||
|
{
|
||||||
|
*r = stbi_convert_bit_range( (c >> 11) & 31, 5, 8 );
|
||||||
|
*g = stbi_convert_bit_range( (c >> 05) & 63, 6, 8 );
|
||||||
|
*b = stbi_convert_bit_range( (c >> 00) & 31, 5, 8 );
|
||||||
|
}
|
||||||
|
void stbi_decode_DXT1_block(
|
||||||
|
unsigned char uncompressed[16*4],
|
||||||
|
unsigned char compressed[8] )
|
||||||
|
{
|
||||||
|
int next_bit = 4*8;
|
||||||
|
int i, r, g, b;
|
||||||
|
int c0, c1;
|
||||||
|
unsigned char decode_colors[4*4];
|
||||||
|
// find the 2 primary colors
|
||||||
|
c0 = compressed[0] + (compressed[1] << 8);
|
||||||
|
c1 = compressed[2] + (compressed[3] << 8);
|
||||||
|
stbi_rgb_888_from_565( c0, &r, &g, &b );
|
||||||
|
decode_colors[0] = r;
|
||||||
|
decode_colors[1] = g;
|
||||||
|
decode_colors[2] = b;
|
||||||
|
decode_colors[3] = 255;
|
||||||
|
stbi_rgb_888_from_565( c1, &r, &g, &b );
|
||||||
|
decode_colors[4] = r;
|
||||||
|
decode_colors[5] = g;
|
||||||
|
decode_colors[6] = b;
|
||||||
|
decode_colors[7] = 255;
|
||||||
|
if( c0 > c1 )
|
||||||
|
{
|
||||||
|
// no alpha, 2 interpolated colors
|
||||||
|
decode_colors[8] = (2*decode_colors[0] + decode_colors[4]) / 3;
|
||||||
|
decode_colors[9] = (2*decode_colors[1] + decode_colors[5]) / 3;
|
||||||
|
decode_colors[10] = (2*decode_colors[2] + decode_colors[6]) / 3;
|
||||||
|
decode_colors[11] = 255;
|
||||||
|
decode_colors[12] = (decode_colors[0] + 2*decode_colors[4]) / 3;
|
||||||
|
decode_colors[13] = (decode_colors[1] + 2*decode_colors[5]) / 3;
|
||||||
|
decode_colors[14] = (decode_colors[2] + 2*decode_colors[6]) / 3;
|
||||||
|
decode_colors[15] = 255;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
// 1 interpolated color, alpha
|
||||||
|
decode_colors[8] = (decode_colors[0] + decode_colors[4]) / 2;
|
||||||
|
decode_colors[9] = (decode_colors[1] + decode_colors[5]) / 2;
|
||||||
|
decode_colors[10] = (decode_colors[2] + decode_colors[6]) / 2;
|
||||||
|
decode_colors[11] = 255;
|
||||||
|
decode_colors[12] = 0;
|
||||||
|
decode_colors[13] = 0;
|
||||||
|
decode_colors[14] = 0;
|
||||||
|
decode_colors[15] = 0;
|
||||||
|
}
|
||||||
|
// decode the block
|
||||||
|
for( i = 0; i < 16*4; i += 4 )
|
||||||
|
{
|
||||||
|
int idx = ((compressed[next_bit>>3] >> (next_bit & 7)) & 3) * 4;
|
||||||
|
next_bit += 2;
|
||||||
|
uncompressed[i+0] = decode_colors[idx+0];
|
||||||
|
uncompressed[i+1] = decode_colors[idx+1];
|
||||||
|
uncompressed[i+2] = decode_colors[idx+2];
|
||||||
|
uncompressed[i+3] = decode_colors[idx+3];
|
||||||
|
}
|
||||||
|
// done
|
||||||
|
}
|
||||||
|
void stbi_decode_DXT23_alpha_block(
|
||||||
|
unsigned char uncompressed[16*4],
|
||||||
|
unsigned char compressed[8] )
|
||||||
|
{
|
||||||
|
int i, next_bit = 0;
|
||||||
|
// each alpha value gets 4 bits
|
||||||
|
for( i = 3; i < 16*4; i += 4 )
|
||||||
|
{
|
||||||
|
uncompressed[i] = stbi_convert_bit_range(
|
||||||
|
(compressed[next_bit>>3] >> (next_bit&7)) & 15,
|
||||||
|
4, 8 );
|
||||||
|
next_bit += 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void stbi_decode_DXT45_alpha_block(
|
||||||
|
unsigned char uncompressed[16*4],
|
||||||
|
unsigned char compressed[8] )
|
||||||
|
{
|
||||||
|
int i, next_bit = 8*2;
|
||||||
|
unsigned char decode_alpha[8];
|
||||||
|
// each alpha value gets 3 bits, and the 1st 2 bytes are the range
|
||||||
|
decode_alpha[0] = compressed[0];
|
||||||
|
decode_alpha[1] = compressed[1];
|
||||||
|
if( decode_alpha[0] > decode_alpha[1] )
|
||||||
|
{
|
||||||
|
// 6 step intermediate
|
||||||
|
decode_alpha[2] = (6*decode_alpha[0] + 1*decode_alpha[1]) / 7;
|
||||||
|
decode_alpha[3] = (5*decode_alpha[0] + 2*decode_alpha[1]) / 7;
|
||||||
|
decode_alpha[4] = (4*decode_alpha[0] + 3*decode_alpha[1]) / 7;
|
||||||
|
decode_alpha[5] = (3*decode_alpha[0] + 4*decode_alpha[1]) / 7;
|
||||||
|
decode_alpha[6] = (2*decode_alpha[0] + 5*decode_alpha[1]) / 7;
|
||||||
|
decode_alpha[7] = (1*decode_alpha[0] + 6*decode_alpha[1]) / 7;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
// 4 step intermediate, pluss full and none
|
||||||
|
decode_alpha[2] = (4*decode_alpha[0] + 1*decode_alpha[1]) / 5;
|
||||||
|
decode_alpha[3] = (3*decode_alpha[0] + 2*decode_alpha[1]) / 5;
|
||||||
|
decode_alpha[4] = (2*decode_alpha[0] + 3*decode_alpha[1]) / 5;
|
||||||
|
decode_alpha[5] = (1*decode_alpha[0] + 4*decode_alpha[1]) / 5;
|
||||||
|
decode_alpha[6] = 0;
|
||||||
|
decode_alpha[7] = 255;
|
||||||
|
}
|
||||||
|
for( i = 3; i < 16*4; i += 4 )
|
||||||
|
{
|
||||||
|
int idx = 0, bit;
|
||||||
|
bit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;
|
||||||
|
idx += bit << 0;
|
||||||
|
++next_bit;
|
||||||
|
bit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;
|
||||||
|
idx += bit << 1;
|
||||||
|
++next_bit;
|
||||||
|
bit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;
|
||||||
|
idx += bit << 2;
|
||||||
|
++next_bit;
|
||||||
|
uncompressed[i] = decode_alpha[idx & 7];
|
||||||
|
}
|
||||||
|
// done
|
||||||
|
}
|
||||||
|
void stbi_decode_DXT_color_block(
|
||||||
|
unsigned char uncompressed[16*4],
|
||||||
|
unsigned char compressed[8] )
|
||||||
|
{
|
||||||
|
int next_bit = 4*8;
|
||||||
|
int i, r, g, b;
|
||||||
|
int c0, c1;
|
||||||
|
unsigned char decode_colors[4*3];
|
||||||
|
// find the 2 primary colors
|
||||||
|
c0 = compressed[0] + (compressed[1] << 8);
|
||||||
|
c1 = compressed[2] + (compressed[3] << 8);
|
||||||
|
stbi_rgb_888_from_565( c0, &r, &g, &b );
|
||||||
|
decode_colors[0] = r;
|
||||||
|
decode_colors[1] = g;
|
||||||
|
decode_colors[2] = b;
|
||||||
|
stbi_rgb_888_from_565( c1, &r, &g, &b );
|
||||||
|
decode_colors[3] = r;
|
||||||
|
decode_colors[4] = g;
|
||||||
|
decode_colors[5] = b;
|
||||||
|
// Like DXT1, but no choicees:
|
||||||
|
// no alpha, 2 interpolated colors
|
||||||
|
decode_colors[6] = (2*decode_colors[0] + decode_colors[3]) / 3;
|
||||||
|
decode_colors[7] = (2*decode_colors[1] + decode_colors[4]) / 3;
|
||||||
|
decode_colors[8] = (2*decode_colors[2] + decode_colors[5]) / 3;
|
||||||
|
decode_colors[9] = (decode_colors[0] + 2*decode_colors[3]) / 3;
|
||||||
|
decode_colors[10] = (decode_colors[1] + 2*decode_colors[4]) / 3;
|
||||||
|
decode_colors[11] = (decode_colors[2] + 2*decode_colors[5]) / 3;
|
||||||
|
// decode the block
|
||||||
|
for( i = 0; i < 16*4; i += 4 )
|
||||||
|
{
|
||||||
|
int idx = ((compressed[next_bit>>3] >> (next_bit & 7)) & 3) * 3;
|
||||||
|
next_bit += 2;
|
||||||
|
uncompressed[i+0] = decode_colors[idx+0];
|
||||||
|
uncompressed[i+1] = decode_colors[idx+1];
|
||||||
|
uncompressed[i+2] = decode_colors[idx+2];
|
||||||
|
}
|
||||||
|
// done
|
||||||
|
}
|
||||||
|
static stbi_uc *dds_load(stbi *s, int *x, int *y, int *comp, int req_comp)
|
||||||
|
{
|
||||||
|
// all variables go up front
|
||||||
|
stbi_uc *dds_data = NULL;
|
||||||
|
stbi_uc block[16*4];
|
||||||
|
stbi_uc compressed[8];
|
||||||
|
int flags, DXT_family;
|
||||||
|
int has_alpha, has_mipmap;
|
||||||
|
int is_compressed, cubemap_faces;
|
||||||
|
int block_pitch, num_blocks;
|
||||||
|
DDS_header header;
|
||||||
|
int i, sz, cf;
|
||||||
|
// load the header
|
||||||
|
if( sizeof( DDS_header ) != 128 )
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
getn( s, (stbi_uc*)(&header), 128 );
|
||||||
|
// and do some checking
|
||||||
|
if( header.dwMagic != (('D' << 0) | ('D' << 8) | ('S' << 16) | (' ' << 24)) ) return NULL;
|
||||||
|
if( header.dwSize != 124 ) return NULL;
|
||||||
|
flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
|
||||||
|
if( (header.dwFlags & flags) != flags ) return NULL;
|
||||||
|
/* According to the MSDN spec, the dwFlags should contain
|
||||||
|
DDSD_LINEARSIZE if it's compressed, or DDSD_PITCH if
|
||||||
|
uncompressed. Some DDS writers do not conform to the
|
||||||
|
spec, so I need to make my reader more tolerant */
|
||||||
|
if( header.sPixelFormat.dwSize != 32 ) return NULL;
|
||||||
|
flags = DDPF_FOURCC | DDPF_RGB;
|
||||||
|
if( (header.sPixelFormat.dwFlags & flags) == 0 ) return NULL;
|
||||||
|
if( (header.sCaps.dwCaps1 & DDSCAPS_TEXTURE) == 0 ) return NULL;
|
||||||
|
// get the image data
|
||||||
|
s->img_x = header.dwWidth;
|
||||||
|
s->img_y = header.dwHeight;
|
||||||
|
s->img_n = 4;
|
||||||
|
is_compressed = (header.sPixelFormat.dwFlags & DDPF_FOURCC) / DDPF_FOURCC;
|
||||||
|
has_alpha = (header.sPixelFormat.dwFlags & DDPF_ALPHAPIXELS) / DDPF_ALPHAPIXELS;
|
||||||
|
has_mipmap = (header.sCaps.dwCaps1 & DDSCAPS_MIPMAP) && (header.dwMipMapCount > 1);
|
||||||
|
cubemap_faces = (header.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP) / DDSCAPS2_CUBEMAP;
|
||||||
|
/* I need cubemaps to have square faces */
|
||||||
|
cubemap_faces &= (s->img_x == s->img_y);
|
||||||
|
cubemap_faces *= 5;
|
||||||
|
cubemap_faces += 1;
|
||||||
|
block_pitch = (s->img_x+3) >> 2;
|
||||||
|
num_blocks = block_pitch * ((s->img_y+3) >> 2);
|
||||||
|
/* let the user know what's going on */
|
||||||
|
*x = s->img_x;
|
||||||
|
*y = s->img_y;
|
||||||
|
*comp = s->img_n;
|
||||||
|
/* is this uncompressed? */
|
||||||
|
if( is_compressed )
|
||||||
|
{
|
||||||
|
/* compressed */
|
||||||
|
// note: header.sPixelFormat.dwFourCC is something like (('D'<<0)|('X'<<8)|('T'<<16)|('1'<<24))
|
||||||
|
DXT_family = 1 + (header.sPixelFormat.dwFourCC >> 24) - '1';
|
||||||
|
if( (DXT_family < 1) || (DXT_family > 5) ) return NULL;
|
||||||
|
/* check the expected size...oops, nevermind...
|
||||||
|
those non-compliant writers leave
|
||||||
|
dwPitchOrLinearSize == 0 */
|
||||||
|
// passed all the tests, get the RAM for decoding
|
||||||
|
sz = (s->img_x)*(s->img_y)*4*cubemap_faces;
|
||||||
|
dds_data = (unsigned char*)malloc( sz );
|
||||||
|
/* do this once for each face */
|
||||||
|
for( cf = 0; cf < cubemap_faces; ++ cf )
|
||||||
|
{
|
||||||
|
// now read and decode all the blocks
|
||||||
|
for( i = 0; i < num_blocks; ++i )
|
||||||
|
{
|
||||||
|
// where are we?
|
||||||
|
int bx, by, bw=4, bh=4;
|
||||||
|
int ref_x = 4 * (i % block_pitch);
|
||||||
|
int ref_y = 4 * (i / block_pitch);
|
||||||
|
// get the next block's worth of compressed data, and decompress it
|
||||||
|
if( DXT_family == 1 )
|
||||||
|
{
|
||||||
|
// DXT1
|
||||||
|
getn( s, compressed, 8 );
|
||||||
|
stbi_decode_DXT1_block( block, compressed );
|
||||||
|
} else if( DXT_family < 4 )
|
||||||
|
{
|
||||||
|
// DXT2/3
|
||||||
|
getn( s, compressed, 8 );
|
||||||
|
stbi_decode_DXT23_alpha_block ( block, compressed );
|
||||||
|
getn( s, compressed, 8 );
|
||||||
|
stbi_decode_DXT_color_block ( block, compressed );
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
// DXT4/5
|
||||||
|
getn( s, compressed, 8 );
|
||||||
|
stbi_decode_DXT45_alpha_block ( block, compressed );
|
||||||
|
getn( s, compressed, 8 );
|
||||||
|
stbi_decode_DXT_color_block ( block, compressed );
|
||||||
|
}
|
||||||
|
// is this a partial block?
|
||||||
|
if( ref_x + 4 > s->img_x )
|
||||||
|
{
|
||||||
|
bw = s->img_x - ref_x;
|
||||||
|
}
|
||||||
|
if( ref_y + 4 > s->img_y )
|
||||||
|
{
|
||||||
|
bh = s->img_y - ref_y;
|
||||||
|
}
|
||||||
|
// now drop our decompressed data into the buffer
|
||||||
|
for( by = 0; by < bh; ++by )
|
||||||
|
{
|
||||||
|
int idx = 4*((ref_y+by+cf*s->img_x)*s->img_x + ref_x);
|
||||||
|
for( bx = 0; bx < bw*4; ++bx )
|
||||||
|
{
|
||||||
|
|
||||||
|
dds_data[idx+bx] = block[by*16+bx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* done reading and decoding the main image...
|
||||||
|
skip MIPmaps if present */
|
||||||
|
if( has_mipmap )
|
||||||
|
{
|
||||||
|
int block_size = 16;
|
||||||
|
if( DXT_family == 1 )
|
||||||
|
{
|
||||||
|
block_size = 8;
|
||||||
|
}
|
||||||
|
for( i = 1; i < header.dwMipMapCount; ++i )
|
||||||
|
{
|
||||||
|
int mx = s->img_x >> (i + 2);
|
||||||
|
int my = s->img_y >> (i + 2);
|
||||||
|
if( mx < 1 )
|
||||||
|
{
|
||||||
|
mx = 1;
|
||||||
|
}
|
||||||
|
if( my < 1 )
|
||||||
|
{
|
||||||
|
my = 1;
|
||||||
|
}
|
||||||
|
skip( s, mx*my*block_size );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}/* per cubemap face */
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
/* uncompressed */
|
||||||
|
DXT_family = 0;
|
||||||
|
s->img_n = 3;
|
||||||
|
if( has_alpha )
|
||||||
|
{
|
||||||
|
s->img_n = 4;
|
||||||
|
}
|
||||||
|
*comp = s->img_n;
|
||||||
|
sz = s->img_x*s->img_y*s->img_n*cubemap_faces;
|
||||||
|
dds_data = (unsigned char*)malloc( sz );
|
||||||
|
/* do this once for each face */
|
||||||
|
for( cf = 0; cf < cubemap_faces; ++ cf )
|
||||||
|
{
|
||||||
|
/* read the main image for this face */
|
||||||
|
getn( s, &dds_data[cf*s->img_x*s->img_y*s->img_n], s->img_x*s->img_y*s->img_n );
|
||||||
|
/* done reading and decoding the main image...
|
||||||
|
skip MIPmaps if present */
|
||||||
|
if( has_mipmap )
|
||||||
|
{
|
||||||
|
for( i = 1; i < header.dwMipMapCount; ++i )
|
||||||
|
{
|
||||||
|
int mx = s->img_x >> i;
|
||||||
|
int my = s->img_y >> i;
|
||||||
|
if( mx < 1 )
|
||||||
|
{
|
||||||
|
mx = 1;
|
||||||
|
}
|
||||||
|
if( my < 1 )
|
||||||
|
{
|
||||||
|
my = 1;
|
||||||
|
}
|
||||||
|
skip( s, mx*my*s->img_n );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* data was BGR, I need it RGB */
|
||||||
|
for( i = 0; i < sz; i += s->img_n )
|
||||||
|
{
|
||||||
|
unsigned char temp = dds_data[i];
|
||||||
|
dds_data[i] = dds_data[i+2];
|
||||||
|
dds_data[i+2] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* finished decompressing into RGBA,
|
||||||
|
adjust the y size if we have a cubemap
|
||||||
|
note: sz is already up to date */
|
||||||
|
s->img_y *= cubemap_faces;
|
||||||
|
*y = s->img_y;
|
||||||
|
// did the user want something else, or
|
||||||
|
// see if all the alpha values are 255 (i.e. no transparency)
|
||||||
|
has_alpha = 0;
|
||||||
|
if( s->img_n == 4)
|
||||||
|
{
|
||||||
|
for( i = 3; (i < sz) && (has_alpha == 0); i += 4 )
|
||||||
|
{
|
||||||
|
has_alpha |= (dds_data[i] < 255);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( (req_comp <= 4) && (req_comp >= 1) )
|
||||||
|
{
|
||||||
|
// user has some requirements, meet them
|
||||||
|
if( req_comp != s->img_n )
|
||||||
|
{
|
||||||
|
dds_data = convert_format( dds_data, s->img_n, req_comp, s->img_x, s->img_y );
|
||||||
|
*comp = s->img_n;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
// user had no requirements, only drop to RGB is no alpha
|
||||||
|
if( (has_alpha == 0) && (s->img_n == 4) )
|
||||||
|
{
|
||||||
|
dds_data = convert_format( dds_data, 4, 3, s->img_x, s->img_y );
|
||||||
|
*comp = 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// OK, done
|
||||||
|
return dds_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef STBI_NO_STDIO
|
||||||
|
stbi_uc *stbi_dds_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp)
|
||||||
|
{
|
||||||
|
stbi s;
|
||||||
|
start_file(&s,f);
|
||||||
|
return dds_load(&s,x,y,comp,req_comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
stbi_uc *stbi_dds_load (char *filename, int *x, int *y, int *comp, int req_comp)
|
||||||
|
{
|
||||||
|
stbi_uc *data;
|
||||||
|
FILE *f = fopen(filename, "rb");
|
||||||
|
if (!f) return NULL;
|
||||||
|
data = stbi_dds_load_from_file(f,x,y,comp,req_comp);
|
||||||
|
fclose(f);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
stbi_uc *stbi_dds_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
||||||
|
{
|
||||||
|
stbi s;
|
||||||
|
start_mem(&s,buffer, len);
|
||||||
|
return dds_load(&s,x,y,comp,req_comp);
|
||||||
|
}
|
40
preboot.js
Normal file
40
preboot.js
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
|
||||||
|
mergeInto(LibraryManager.library, {
|
||||||
|
ff_browser_add_resize_handler : function(resizeHandler) {
|
||||||
|
var handlerFuncResize = function(event) {
|
||||||
|
var pageCanvas = document.getElementById('canvas');
|
||||||
|
if (pageCanvas === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Runtime.dynCall('vii', resizeHandler, [ pageCanvas.clientWidth, pageCanvas.clientHeight ]);
|
||||||
|
}
|
||||||
|
window.addEventListener('resize', handlerFuncResize, true);
|
||||||
|
},
|
||||||
|
ff_browser_add_popstate_handler : function(popstateHandler) {
|
||||||
|
var handlerFuncPopState = function(event) {
|
||||||
|
if (event.state == null || event.state.hash === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var hashPtr = allocate(intArrayFromString(event.state.hash), 'i8', ALLOC_NORMAL); // free in c code
|
||||||
|
|
||||||
|
console.log('js popstate: '+event.state.hash+' ptr: '+hashPtr);
|
||||||
|
Runtime.dynCall('vi', popstateHandler, [ hashPtr ]);
|
||||||
|
}
|
||||||
|
window.addEventListener('popstate', handlerFuncPopState);
|
||||||
|
|
||||||
|
if (window.location.hash && window.location.hash !== '') {
|
||||||
|
console.log('calling init page: '+window.location.hash.substring(1));
|
||||||
|
handlerFuncPopState({state:{page:window.location.hash.substring(1)}});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ff_browser_pushstate : function(hrefHash) {
|
||||||
|
var hrefHashStr = Pointer_stringify(hrefHash); // only display then in pop we don't need to reverse again.
|
||||||
|
console.log('js pushstate: '+hrefHashStr);
|
||||||
|
history.pushState({hash:hrefHashStr}, document.title, window.location.protocol+window.location.hostname+window.location.pathname+'#'+hrefHashStr);
|
||||||
|
},
|
||||||
|
ff_browser_openlink : function(urlPtr) {
|
||||||
|
var url = Pointer_stringify(urlPtr);
|
||||||
|
console.log('js openurl: '+url);
|
||||||
|
window.open(url,'_self');
|
||||||
|
}
|
||||||
|
});
|
106
src/page/PageComponent.h
Normal file
106
src/page/PageComponent.h
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
#ifndef _PAGE_COMPONENT_H
|
||||||
|
#define _PAGE_COMPONENT_H
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#include "PageFont.h"
|
||||||
|
#include "PageDraw.h"
|
||||||
|
#include "PageWindow.h"
|
||||||
|
|
||||||
|
class PageText: public PageDrawComponent {
|
||||||
|
private:
|
||||||
|
const char* text;
|
||||||
|
PageFontType font = NORMAL;
|
||||||
|
bool bullet = false;
|
||||||
|
ImVec4 color;
|
||||||
|
bool colorFlag = false;
|
||||||
|
bool multiLine = false;
|
||||||
|
bool multiLineReadOnly = true;
|
||||||
|
public:
|
||||||
|
PageText(const char* text) {
|
||||||
|
this->text = text;
|
||||||
|
}
|
||||||
|
void drawComponent(void) {
|
||||||
|
PAGE_FONT.pushFont(font);
|
||||||
|
if (colorFlag) {
|
||||||
|
ImGui::PushStyleColor(ImGuiCol_Text, color);
|
||||||
|
}
|
||||||
|
if (bullet) {
|
||||||
|
ImGui::Bullet();
|
||||||
|
}
|
||||||
|
if (multiLine) {
|
||||||
|
char* textLine = (char*)text;
|
||||||
|
ImGui::InputTextMultiline("##source", textLine, strlen(textLine), ImVec2(-1.0f, ImGui::GetTextLineHeight() * 16), ImGuiInputTextFlags_AllowTabInput | (multiLineReadOnly ? ImGuiInputTextFlags_ReadOnly : 0));
|
||||||
|
} else {
|
||||||
|
ImGui::Text(text);
|
||||||
|
}
|
||||||
|
if (colorFlag) {
|
||||||
|
ImGui::PopStyleColor();
|
||||||
|
}
|
||||||
|
PAGE_FONT.popFont(font);
|
||||||
|
}
|
||||||
|
PageText* setFont(PageFontType font) {
|
||||||
|
this->font = font;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
PageText* setBullet() {
|
||||||
|
bullet = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
PageText* setColor(ImVec4 color) {
|
||||||
|
this->color = color;
|
||||||
|
colorFlag = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
PageText* setMultiLine() {
|
||||||
|
multiLine = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class PageImage: public PageDrawComponent {
|
||||||
|
private:
|
||||||
|
void* image;
|
||||||
|
ImVec2 size = ImVec2(100,100);
|
||||||
|
ImColor tintColor = ImColor(255,255,255,255);
|
||||||
|
ImColor borderColor = ImColor(255,255,255,128);
|
||||||
|
public:
|
||||||
|
PageImage(void* image) {
|
||||||
|
this->image = image;
|
||||||
|
}
|
||||||
|
void drawComponent(void) {
|
||||||
|
ImGui::Image((void *)this->image, this->size, ImVec2(0,0), ImVec2(1,1), this->tintColor , this->borderColor);
|
||||||
|
}
|
||||||
|
PageImage* setSize(ImVec2 size) {
|
||||||
|
this->size = size;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
PageImage* setSize(float x, float y) {
|
||||||
|
return this->setSize(ImVec2(x,y));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
class PageLink: public PageDrawComponent {
|
||||||
|
private:
|
||||||
|
const char* text;
|
||||||
|
PageFontType font = NORMAL;
|
||||||
|
bool clicked = false;
|
||||||
|
public:
|
||||||
|
PageLink(const char* text) {
|
||||||
|
this->text = text;
|
||||||
|
}
|
||||||
|
void drawComponent(void) {
|
||||||
|
PAGE_FONT.pushFont(font);
|
||||||
|
if (ImGui::Selectable(text, &clicked)) {
|
||||||
|
if (PAGE_WINDOW.platformOpenUrl != NULL) {
|
||||||
|
PAGE_WINDOW.platformOpenUrl(text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//ImGui::SameLine();
|
||||||
|
PAGE_FONT.popFont(font);
|
||||||
|
}
|
||||||
|
PageLink* setFont(PageFontType font) {
|
||||||
|
this->font = font;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
76
src/page/PageController.cpp
Normal file
76
src/page/PageController.cpp
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
#include "PageController.h"
|
||||||
|
|
||||||
|
PageController::PageController(const char* id, const char* title) {
|
||||||
|
this->id = id;
|
||||||
|
this->title = title;
|
||||||
|
this->buildText(this->getTitle())->setFont(HEADER_MAIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* PageController::getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* PageController::getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageController::loadImage(const char* filename) {
|
||||||
|
printf("loadImage: %s\n",filename);
|
||||||
|
void* imageId = (void*)SOIL_load_OGL_texture(filename, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS);
|
||||||
|
if(imageId == NULL) {
|
||||||
|
printf("Failed to load texture\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this->images.insert(std::pair<const char*,void*>(filename,imageId));
|
||||||
|
}
|
||||||
|
|
||||||
|
int PageController::getImageCount() {
|
||||||
|
return this->images.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
int PageController::getDrawCount() {
|
||||||
|
return this->components.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageController::drawLayer() {
|
||||||
|
for (auto comp : this->components) {
|
||||||
|
comp->drawComponent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageController::addComponent(PageDrawComponent* component) {
|
||||||
|
this->components.push_back(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageController::setMenu(bool menu) {
|
||||||
|
this->menu = menu;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PageController::getMenu() {
|
||||||
|
return this->menu;
|
||||||
|
}
|
||||||
|
|
||||||
|
PageText* PageController::buildText(const char* text) {
|
||||||
|
PageText* result = new PageText(text);
|
||||||
|
addComponent(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
PageLink* PageController::buildLink(const char* url) {
|
||||||
|
PageLink* result = new PageLink(url);
|
||||||
|
addComponent(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
PageImage* PageController::buildImage(void* imageRef) {
|
||||||
|
PageImage* result = new PageImage(imageRef);
|
||||||
|
addComponent(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
PageImage* PageController::buildImage(const char* filename) {
|
||||||
|
if (images.find(filename) == images.end()) {
|
||||||
|
loadImage(filename);
|
||||||
|
}
|
||||||
|
return buildImage(images.at(filename));
|
||||||
|
}
|
45
src/page/PageController.h
Normal file
45
src/page/PageController.h
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
#ifndef _PAGE_CONTROLLER_H
|
||||||
|
#define _PAGE_CONTROLLER_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include "PageComponent.h"
|
||||||
|
#include "PageDraw.h"
|
||||||
|
// TODO: goto SDL_Image ?
|
||||||
|
#include "SOIL.h"
|
||||||
|
|
||||||
|
|
||||||
|
class PageController: public PageDrawLayer {
|
||||||
|
private:
|
||||||
|
const char* id;
|
||||||
|
const char* title;
|
||||||
|
bool menu = true;
|
||||||
|
std::map <const char* , void*> images;
|
||||||
|
std::vector<PageDrawComponent*> components;
|
||||||
|
|
||||||
|
void loadImage(const char* filename);
|
||||||
|
PageImage* buildImage(void* imageRef);
|
||||||
|
public:
|
||||||
|
PageController(const char* id, const char* title);
|
||||||
|
|
||||||
|
const char* getId();
|
||||||
|
const char* getTitle();
|
||||||
|
|
||||||
|
int getImageCount();
|
||||||
|
|
||||||
|
int getDrawCount();
|
||||||
|
void drawLayer(); // from super
|
||||||
|
|
||||||
|
void addComponent(PageDrawComponent* component);
|
||||||
|
void setMenu(bool menu);
|
||||||
|
bool getMenu();
|
||||||
|
|
||||||
|
PageText* buildText(const char* text);
|
||||||
|
PageLink* buildLink(const char* url);
|
||||||
|
PageImage* buildImage(const char* filename);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
15
src/page/PageDraw.h
Normal file
15
src/page/PageDraw.h
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
#ifndef _PAGE_DRAW_H
|
||||||
|
#define _PAGE_DRAW_H
|
||||||
|
|
||||||
|
class PageDrawLayer {
|
||||||
|
public:
|
||||||
|
virtual void drawLayer(void) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class PageDrawComponent {
|
||||||
|
public:
|
||||||
|
virtual void drawComponent(void) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
69
src/page/PageFont.cpp
Normal file
69
src/page/PageFont.cpp
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
#include "PageFont.h"
|
||||||
|
|
||||||
|
PageFont::PageFont() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageFont::loadFonts() {
|
||||||
|
printf("fonts loadings\n");
|
||||||
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
// NOTE: Load fonts from large to small else scaling doesn't work
|
||||||
|
|
||||||
|
ImFontConfig fontConfigHeaderMain;
|
||||||
|
fontConfigHeaderMain.MergeMode = false;
|
||||||
|
fontConfigHeaderMain.PixelSnapH = true;
|
||||||
|
ImFont* fontHeaderMain = io.Fonts->AddFontFromFileTTF("data/font/roboto-bold.ttf", 24.0f, &fontConfigHeaderMain);
|
||||||
|
|
||||||
|
ImFontConfig fontConfigHeaderSub;
|
||||||
|
fontConfigHeaderSub.MergeMode = false;
|
||||||
|
fontConfigHeaderSub.PixelSnapH = true;
|
||||||
|
ImFont* fontHeaderSub = io.Fonts->AddFontFromFileTTF("data/font/roboto-bold.ttf", 20.0f, &fontConfigHeaderSub);
|
||||||
|
|
||||||
|
ImFontConfig fontConfigNormal;
|
||||||
|
fontConfigNormal.MergeMode = false;
|
||||||
|
fontConfigNormal.PixelSnapH = true;
|
||||||
|
io.Fonts->AddFontFromFileTTF("data/font/lato-blackitalic-webfont.ttf", 16.0f, &fontConfigNormal);
|
||||||
|
static ImWchar iconRange[] = { 0xf000, 0xf3ff, 0 };
|
||||||
|
ImFontConfig fontConfigNormalIcon;
|
||||||
|
fontConfigNormalIcon.MergeMode = true;
|
||||||
|
fontConfigNormalIcon.PixelSnapH = true;
|
||||||
|
io.Fonts->AddFontFromFileTTF("data/font/fontawesome-webfont.ttf", 16.0f, &fontConfigNormalIcon, iconRange);
|
||||||
|
|
||||||
|
ImFontConfig fontConfigSmall;
|
||||||
|
fontConfigSmall.MergeMode = false;
|
||||||
|
fontConfigSmall.PixelSnapH = true;
|
||||||
|
ImFont* fontSmall = io.Fonts->AddFontFromFileTTF("data/font/lato-blackitalic-webfont.ttf", 12.0f, &fontConfigSmall);
|
||||||
|
|
||||||
|
this->fonts.insert(std::pair<PageFontType,ImFont*>(HEADER_MAIN, fontHeaderMain));
|
||||||
|
this->fonts.insert(std::pair<PageFontType,ImFont*>(HEADER_SUB, fontHeaderSub));
|
||||||
|
this->fonts.insert(std::pair<PageFontType,ImFont*>(SMALL, fontSmall));
|
||||||
|
|
||||||
|
// swap ImGui main font to normal
|
||||||
|
ImFont* font0 = io.Fonts->Fonts.Data[0];
|
||||||
|
ImFont* font2 = io.Fonts->Fonts.Data[2];
|
||||||
|
io.Fonts->Fonts.Data[0] = font2;
|
||||||
|
io.Fonts->Fonts.Data[2] = font0;
|
||||||
|
|
||||||
|
printf("fonts initialized\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageFont::pushFont(PageFontType type) {
|
||||||
|
if (NORMAL == type) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this->fonts.find(type) == this->fonts.end()) {
|
||||||
|
printf("ERR: unknown font: %i\n",type);
|
||||||
|
ImGui::PushFont(this->fonts.at(SMALL)); // else pop will error
|
||||||
|
} else {
|
||||||
|
ImGui::PushFont(this->fonts.at(type));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageFont::popFont(PageFontType type) {
|
||||||
|
if (NORMAL == type) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ImGui::PopFont();
|
||||||
|
}
|
||||||
|
|
||||||
|
PageFont PAGE_FONT;
|
||||||
|
|
27
src/page/PageFont.h
Normal file
27
src/page/PageFont.h
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#ifndef _PAGE_FONT_H
|
||||||
|
#define _PAGE_FONT_H
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
enum PageFontType {
|
||||||
|
HEADER_MAIN,
|
||||||
|
HEADER_SUB,
|
||||||
|
NORMAL,
|
||||||
|
SMALL,
|
||||||
|
};
|
||||||
|
|
||||||
|
class PageFont {
|
||||||
|
private:
|
||||||
|
std::map <PageFontType, ImFont*> fonts;
|
||||||
|
public:
|
||||||
|
PageFont();
|
||||||
|
void loadFonts();
|
||||||
|
void pushFont(PageFontType font);
|
||||||
|
void popFont(PageFontType font);
|
||||||
|
};
|
||||||
|
|
||||||
|
extern PageFont PAGE_FONT;
|
||||||
|
|
||||||
|
#endif
|
130
src/page/PageWindow.cpp
Normal file
130
src/page/PageWindow.cpp
Normal file
|
@ -0,0 +1,130 @@
|
||||||
|
#include "PageWindow.h"
|
||||||
|
|
||||||
|
ImGuiWindowFlags WINDOW_PANE_FLAGS = ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_NoFocusOnAppearing|ImGuiWindowFlags_NoBringToFrontOnFocus;
|
||||||
|
ImGuiWindowFlags WINDOW_BACKGROUND_FLAGS = WINDOW_PANE_FLAGS|ImGuiWindowFlags_NoScrollbar|ImGuiWindowFlags_NoInputs;
|
||||||
|
|
||||||
|
PageWindow::PageWindow() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageWindow::renderMenu() {
|
||||||
|
ImGui::SetNextWindowPos(ImVec2(ImGui::GetIO().DisplaySize.x * layoutMarginX, ImGui::GetIO().DisplaySize.y * layoutMarginY), ImGuiSetCond_Always);
|
||||||
|
ImGui::SetNextWindowSize(ImVec2((ImGui::GetIO().DisplaySize.x * (1.0f - layoutMarginX)) - (ImGui::GetIO().DisplaySize.x * layoutMarginX), menuHeight), ImGuiSetCond_Always);
|
||||||
|
ImGui::Begin("Menu###menu", &menuRender , ImVec2(300, 200), 0.5f, WINDOW_PANE_FLAGS);
|
||||||
|
PAGE_FONT.pushFont(HEADER_MAIN);
|
||||||
|
bool first = true;
|
||||||
|
for (auto page : pages) {
|
||||||
|
if (!page.second->getMenu()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!first) {
|
||||||
|
ImGui::SameLine();
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
if (ImGui::Button(page.second->getTitle())) {
|
||||||
|
this->setPageCurrent(page.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PAGE_FONT.popFont(HEADER_MAIN);
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageWindow::renderPage(void) {
|
||||||
|
int preHeight = this->menuHeight;
|
||||||
|
if (!menuRender) {
|
||||||
|
preHeight = 0;
|
||||||
|
}
|
||||||
|
int postHeight = this->footerHeight;
|
||||||
|
if (!footerRender) {
|
||||||
|
postHeight = 0;
|
||||||
|
}
|
||||||
|
ImGui::SetNextWindowPos(ImVec2(ImGui::GetIO().DisplaySize.x * layoutMarginX, (ImGui::GetIO().DisplaySize.y * layoutMarginY)*2 + preHeight), ImGuiSetCond_Always);
|
||||||
|
ImGui::SetNextWindowSize(ImVec2((ImGui::GetIO().DisplaySize.x * (1.0f - layoutMarginX)) - (ImGui::GetIO().DisplaySize.x * layoutMarginX), ImGui::GetIO().DisplaySize.y - preHeight - postHeight - (ImGui::GetIO().DisplaySize.y * layoutMarginY)*4 ), ImGuiSetCond_Always);
|
||||||
|
ImGui::Begin("Home###content", &pageRender , ImVec2(300, 200), 0.5f, WINDOW_PANE_FLAGS);
|
||||||
|
|
||||||
|
if (pageCurrent == NULL) {
|
||||||
|
ImGui::Text("NULL");
|
||||||
|
} else {
|
||||||
|
pageCurrent->drawLayer();
|
||||||
|
}
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageWindow::renderFooter(void) {
|
||||||
|
ImGui::SetNextWindowPos(ImVec2(ImGui::GetIO().DisplaySize.x * layoutMarginX, ImGui::GetIO().DisplaySize.y - footerHeight - (ImGui::GetIO().DisplaySize.y * layoutMarginY)), ImGuiSetCond_Always);
|
||||||
|
ImGui::SetNextWindowSize(ImVec2((ImGui::GetIO().DisplaySize.x * (1.0f - layoutMarginX)) - (ImGui::GetIO().DisplaySize.x * layoutMarginX), footerHeight), ImGuiSetCond_Always);
|
||||||
|
ImGui::Begin("Footer###footer", &footerRender , ImVec2(300, 200), 0.5f, WINDOW_PANE_FLAGS);
|
||||||
|
ImGui::Text("(c)2016 ff");
|
||||||
|
ImGui::SameLine();
|
||||||
|
ImGui::Text(" -<-- -->- ");
|
||||||
|
ImGui::SameLine();
|
||||||
|
ImGui::Text("Compiled:");
|
||||||
|
ImGui::SameLine();
|
||||||
|
ImGui::Text(__DATE__);
|
||||||
|
ImGui::SameLine();
|
||||||
|
ImGui::Text(__TIME__);
|
||||||
|
if (footerRenderFPS) {
|
||||||
|
ImGui::SameLine();
|
||||||
|
ImGui::Text(" - ");
|
||||||
|
ImGui::SameLine();
|
||||||
|
ImGui::Text("(%.1f FPS)",ImGui::GetIO().Framerate);
|
||||||
|
}
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PageWindow::draw() {
|
||||||
|
if (footerRender) {
|
||||||
|
this->renderFooter();
|
||||||
|
}
|
||||||
|
if (pageRender) {
|
||||||
|
this->renderPage();
|
||||||
|
}
|
||||||
|
if (menuRender) {
|
||||||
|
this->renderMenu();
|
||||||
|
}
|
||||||
|
for (auto comp : layers) {
|
||||||
|
comp->drawLayer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PageDrawLayer* PageWindow::addLayer(PageDrawLayer* layer) {
|
||||||
|
layers.push_back(layer);
|
||||||
|
return layer;
|
||||||
|
}
|
||||||
|
|
||||||
|
PageController* PageWindow::addPage(PageController* page) {
|
||||||
|
pages.insert(std::pair<const char*,PageController*>(page->getId(),page));
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
|
||||||
|
PageController* PageWindow::addPage(const char* id, const char* title) {
|
||||||
|
return addPage(new PageController(id,title));
|
||||||
|
}
|
||||||
|
|
||||||
|
PageController* PageWindow::findPage(char* id) {
|
||||||
|
for (auto page : pages) {
|
||||||
|
if (strcmp (page.first, id) != 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return page.second;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageWindow::setPageCurrent(PageController* page) {
|
||||||
|
if (page != NULL) {
|
||||||
|
this->pageCurrent = page;
|
||||||
|
} else {
|
||||||
|
this->pageCurrent = this->pageNotFound;
|
||||||
|
}
|
||||||
|
if (platformPageCb != NULL) {
|
||||||
|
platformPageCb(this->pageCurrent->getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageWindow::setPageNotFound(PageController* page) {
|
||||||
|
this->pageNotFound = page;
|
||||||
|
}
|
||||||
|
|
||||||
|
PageWindow PAGE_WINDOW;
|
||||||
|
|
65
src/page/PageWindow.h
Normal file
65
src/page/PageWindow.h
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
#ifndef _PAGE_WINDOW_H
|
||||||
|
#define _PAGE_WINDOW_H
|
||||||
|
|
||||||
|
#include "PageController.h"
|
||||||
|
#include "PageDraw.h"
|
||||||
|
#include "PageFont.h"
|
||||||
|
#include "imgui.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <map>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_opengl.h>
|
||||||
|
|
||||||
|
|
||||||
|
class PageWindow {
|
||||||
|
private:
|
||||||
|
std::map <const char*, PageController*> pages;
|
||||||
|
std::vector<PageDrawLayer*> layers;
|
||||||
|
void renderMenu();
|
||||||
|
void renderPage();
|
||||||
|
void renderFooter();
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
SDL_Window *window;
|
||||||
|
SDL_GLContext glcontext;
|
||||||
|
bool windowRun = true;
|
||||||
|
bool windowFullScreen = false;
|
||||||
|
bool footerRender = true;
|
||||||
|
bool footerRenderFPS = true;
|
||||||
|
int footerHeight = 30;
|
||||||
|
//bool backgroundRender = true;
|
||||||
|
ImVec4 backgroundClearColor = ImColor(8, 54, 127);
|
||||||
|
bool menuRender = true;
|
||||||
|
int menuHeight = 50;
|
||||||
|
bool pageRender = true;
|
||||||
|
PageController* pageCurrent;
|
||||||
|
PageController* pageNotFound;
|
||||||
|
float layoutMarginX = 0.07f;
|
||||||
|
float layoutMarginY = 0.01f;
|
||||||
|
void (*platformOpenUrl)(char* url) = NULL;
|
||||||
|
void (*platformPageCb)(char* pageId) = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
PageWindow();
|
||||||
|
void draw();
|
||||||
|
PageDrawLayer* addLayer(PageDrawLayer* layer);
|
||||||
|
PageController* addPage(PageController* page);
|
||||||
|
PageController* addPage(const char* id, const char* title);
|
||||||
|
PageController* findPage(char* id);
|
||||||
|
void setPageCurrent(PageController* page);
|
||||||
|
void setPageNotFound(PageController* page);
|
||||||
|
|
||||||
|
void printDebug() {
|
||||||
|
for (auto page : pages) {
|
||||||
|
printf("page: %s comp: %i img: %i\n",page.second->getId(),page.second->getDrawCount(),page.second->getImageCount());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
extern ImGuiWindowFlags WINDOW_PANE_FLAGS;
|
||||||
|
extern ImGuiWindowFlags WINDOW_BACKGROUND_FLAGS;
|
||||||
|
|
||||||
|
extern PageWindow PAGE_WINDOW;
|
||||||
|
|
||||||
|
#endif
|
72
src/site/FFSiteBackground.cpp
Normal file
72
src/site/FFSiteBackground.cpp
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
#include "FFSiteBackground.h"
|
||||||
|
|
||||||
|
struct A {
|
||||||
|
const char* text = "ForwardFire";
|
||||||
|
ImVec4 color = ImVec4(0.0f,0.5f,0.3f,1.0f);;
|
||||||
|
int bgSpeed = 1;
|
||||||
|
float x = 10.0f;
|
||||||
|
float xOff = 2.0f;
|
||||||
|
float y = 20.0f;
|
||||||
|
float yOff = 3.0f;
|
||||||
|
};
|
||||||
|
|
||||||
|
static const int LETTERS_MAX = 25;
|
||||||
|
static A letters[LETTERS_MAX];
|
||||||
|
|
||||||
|
static bool rr = true;
|
||||||
|
|
||||||
|
FFSiteBackground::FFSiteBackground() {
|
||||||
|
for (int i=0;i<LETTERS_MAX;i++) {
|
||||||
|
A* a = &letters[i];
|
||||||
|
a->bgSpeed = rand() % 100;
|
||||||
|
a->x = rand() % (int)ImGui::GetIO().DisplaySize.x;
|
||||||
|
a->y = rand() % (int)ImGui::GetIO().DisplaySize.y;
|
||||||
|
a->color = ImVec4(0.0f,(i+1)*0.1f,(i+2)*0.01f,(i+3)*0.01f);
|
||||||
|
|
||||||
|
a->xOff = rand() % 3 + 0.1f;
|
||||||
|
a->yOff = rand() % 4 + 0.1f;
|
||||||
|
|
||||||
|
int r = rand() % 5;
|
||||||
|
if (r == 2) {
|
||||||
|
a->xOff = 0 - a->xOff;
|
||||||
|
}
|
||||||
|
if (r == 3) {
|
||||||
|
a->yOff = 0 - a->yOff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void FFSiteBackground::drawLayer(void) {
|
||||||
|
ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize, ImGuiSetCond_Always);
|
||||||
|
ImGui::Begin("Background###background", &rr , ImGui::GetIO().DisplaySize, 0.0f, WINDOW_BACKGROUND_FLAGS);
|
||||||
|
for (int i=0;i<LETTERS_MAX;i++) {
|
||||||
|
A* a = &letters[i];
|
||||||
|
a->bgSpeed--;
|
||||||
|
if (a->bgSpeed < 0) {
|
||||||
|
a->bgSpeed = 2; //rand() % 10;
|
||||||
|
|
||||||
|
if (a->x > ImGui::GetIO().DisplaySize.x) {
|
||||||
|
a->xOff = 0 - a->xOff;
|
||||||
|
a->x = ImGui::GetIO().DisplaySize.x;
|
||||||
|
}
|
||||||
|
if (a->x < 0) {
|
||||||
|
a->xOff = 0 - a->xOff;
|
||||||
|
a->x = 0;
|
||||||
|
}
|
||||||
|
if (a->y > ImGui::GetIO().DisplaySize.y) {
|
||||||
|
a->yOff = 0 - a->yOff;
|
||||||
|
a->y = ImGui::GetIO().DisplaySize.y;
|
||||||
|
}
|
||||||
|
if (a->y < 0) {
|
||||||
|
a->yOff = 0 - a->yOff;
|
||||||
|
a->y = 0;
|
||||||
|
}
|
||||||
|
a->x = a->x+a->xOff;
|
||||||
|
a->y = a->y+a->yOff;
|
||||||
|
}
|
||||||
|
ImGui::SetCursorPos(ImVec2(a->x, a->y));
|
||||||
|
ImGui::TextColored(a->color, a->text);
|
||||||
|
}
|
||||||
|
ImGui::End();
|
||||||
|
}
|
16
src/site/FFSiteBackground.h
Normal file
16
src/site/FFSiteBackground.h
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef _FF_SITE_BACKGROUND_H
|
||||||
|
#define _FF_SITE_BACKGROUND_H
|
||||||
|
|
||||||
|
#include "../page/PageDraw.h"
|
||||||
|
#include "../page/PageWindow.h"
|
||||||
|
#include "imgui.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
class FFSiteBackground: public PageDrawLayer {
|
||||||
|
private:
|
||||||
|
public:
|
||||||
|
FFSiteBackground();
|
||||||
|
void drawLayer(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
105
src/site/FFSiteDebug.cpp
Normal file
105
src/site/FFSiteDebug.cpp
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
#include "FFSiteDebug.h"
|
||||||
|
|
||||||
|
void FFSiteDebug::showSettingsWindow(bool* p_open) {
|
||||||
|
ImGui::SetNextWindowSize(ImVec2(300, 200), ImGuiSetCond_FirstUseEver);
|
||||||
|
ImGui::Begin("Test Settings", p_open);
|
||||||
|
ImGui::ColorEdit3("Background", (float*) &PAGE_WINDOW.backgroundClearColor);
|
||||||
|
ImGui::SliderFloat("Margin X", &PAGE_WINDOW.layoutMarginX, 0.0, 0.4);
|
||||||
|
ImGui::SliderFloat("Margin Y", &PAGE_WINDOW.layoutMarginY, 0.0, 0.2);
|
||||||
|
ImGui::Checkbox("Render menu", &PAGE_WINDOW.menuRender);
|
||||||
|
ImGui::Checkbox("Render footer", &PAGE_WINDOW.footerRender);
|
||||||
|
ImGui::Checkbox("Render FPS", &PAGE_WINDOW.footerRenderFPS);
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FFSiteDebug::showLayoutWindow(bool* p_open) {
|
||||||
|
ImGui::SetNextWindowSize(ImVec2(300, 200), ImGuiSetCond_FirstUseEver);
|
||||||
|
ImGui::Begin("Test Layout", p_open);
|
||||||
|
|
||||||
|
ImGui::BeginChild("Sub1", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.33f,100), true, 0);
|
||||||
|
ImGui::SmallButton("Hello 11");ImGui::SameLine();
|
||||||
|
ImGui::SmallButton("Hello 12");ImGui::SameLine();
|
||||||
|
ImGui::SmallButton("Hello 13");ImGui::SameLine();
|
||||||
|
ImGui::SmallButton("Hello 14");
|
||||||
|
ImGui::SmallButton("Hello 15");ImGui::SameLine();
|
||||||
|
ImGui::SmallButton("Hello 16");
|
||||||
|
ImGui::SmallButton("Hello 17");ImGui::SameLine();
|
||||||
|
ImGui::SmallButton("Hello 18");ImGui::SameLine();
|
||||||
|
ImGui::EndChild();
|
||||||
|
ImGui::SameLine();
|
||||||
|
|
||||||
|
static bool bb = true;
|
||||||
|
|
||||||
|
//ImGui::PushStyleVar(ImGuiStyleVar_ChildWindowRounding, 5.0f);
|
||||||
|
ImGui::BeginChild("Sub2", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.66f,100), true, 0);
|
||||||
|
if (bb) {
|
||||||
|
ImGui::Text("Hello 22");
|
||||||
|
} else {
|
||||||
|
ImGui::Text("Hello 22222222222222.....");
|
||||||
|
}
|
||||||
|
bb = !ImGui::IsItemHovered();
|
||||||
|
ImGui::EndChild();
|
||||||
|
|
||||||
|
//ImGui::PopStyleVar();
|
||||||
|
|
||||||
|
static bool bc = true;
|
||||||
|
float f = 15.0f;
|
||||||
|
if (bc) {
|
||||||
|
f+=10;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui::BeginGroup();
|
||||||
|
ImGui::PushStyleVar(ImGuiStyleVar_ChildWindowRounding, f);
|
||||||
|
ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowContentRegionWidth(),-1), false, ImGuiWindowFlags_ShowBorders);
|
||||||
|
ImGui::Text("Hello Main");
|
||||||
|
ImGui::EndChild();
|
||||||
|
ImGui::PopStyleVar();
|
||||||
|
ImGui::EndGroup();
|
||||||
|
bc = !ImGui::IsItemHovered();
|
||||||
|
bc = !bc;
|
||||||
|
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FFSiteDebug::drawComponent(void) {
|
||||||
|
if (ImGui::Button("\uf241 Fullscreen")) {
|
||||||
|
if (PAGE_WINDOW.windowFullScreen) {
|
||||||
|
SDL_SetWindowFullscreen(PAGE_WINDOW.window, 0);
|
||||||
|
} else {
|
||||||
|
SDL_SetWindowFullscreen(PAGE_WINDOW.window, SDL_WINDOW_FULLSCREEN);
|
||||||
|
}
|
||||||
|
PAGE_WINDOW.windowFullScreen = !PAGE_WINDOW.windowFullScreen;
|
||||||
|
}
|
||||||
|
if (ImGui::Button("\uf1fa Test Demo")) {
|
||||||
|
renderDemo ^= 1;
|
||||||
|
}
|
||||||
|
if (ImGui::Button("\uf1fe Test Metrics")) {
|
||||||
|
renderMetrics ^= 1;
|
||||||
|
}
|
||||||
|
if (ImGui::Button("\uf1fb Test Layout")) {
|
||||||
|
renderLayout ^= 1;
|
||||||
|
}
|
||||||
|
if (ImGui::Button("\uf1ff Page Options")) {
|
||||||
|
renderSettings ^= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FFSiteDebug::drawLayer(void) {
|
||||||
|
if (renderDemo) {
|
||||||
|
ImGui::SetNextWindowPos(ImVec2(50, 50), ImGuiSetCond_FirstUseEver);
|
||||||
|
ImGui::ShowTestWindow(&renderDemo);
|
||||||
|
}
|
||||||
|
if (renderMetrics) {
|
||||||
|
ImGui::SetNextWindowPos(ImVec2(60, 60), ImGuiSetCond_FirstUseEver);
|
||||||
|
ImGui::ShowMetricsWindow(&renderMetrics);
|
||||||
|
}
|
||||||
|
if (renderLayout) {
|
||||||
|
ImGui::SetNextWindowPos(ImVec2(70, 70), ImGuiSetCond_FirstUseEver);
|
||||||
|
showLayoutWindow(&renderLayout);
|
||||||
|
}
|
||||||
|
if (renderSettings) {
|
||||||
|
ImGui::SetNextWindowPos(ImVec2(80, 80), ImGuiSetCond_FirstUseEver);
|
||||||
|
showSettingsWindow(&renderSettings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
23
src/site/FFSiteDebug.h
Normal file
23
src/site/FFSiteDebug.h
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#ifndef _FF_SITE_DEBUG_H
|
||||||
|
#define _FF_SITE_DEBUG_H
|
||||||
|
|
||||||
|
#include "../page/PageDraw.h"
|
||||||
|
#include "../page/PageWindow.h"
|
||||||
|
#include "imgui.h"
|
||||||
|
|
||||||
|
class FFSiteDebug: public PageDrawComponent,public PageDrawLayer {
|
||||||
|
private:
|
||||||
|
bool renderDemo = false;
|
||||||
|
bool renderMetrics = false;
|
||||||
|
bool renderLayout = false;
|
||||||
|
bool renderSettings = false;
|
||||||
|
void showLayoutWindow(bool* p_open);
|
||||||
|
void showSettingsWindow(bool* p_open);
|
||||||
|
public:
|
||||||
|
FFSiteDebug() {
|
||||||
|
}
|
||||||
|
void drawComponent(void);
|
||||||
|
void drawLayer(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue