initial commit
This commit is contained in:
commit
7e427e4dcc
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
out/
|
||||||
52
GNUmakefile
Normal file
52
GNUmakefile
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env -S gmake -f
|
||||||
|
|
||||||
|
# if need be, set these variables to whatever you need them to be
|
||||||
|
MKDIR ?= mkdir -p
|
||||||
|
|
||||||
|
EXTENSION ?= out
|
||||||
|
CFLAGS ?= -Wall -Wpedantic -Wextra -g -Og
|
||||||
|
CXXFLAGS ?= -Wall -Wpedantic -Wextra -g -Og
|
||||||
|
LDFLAGS ?=
|
||||||
|
|
||||||
|
NAME := suwi
|
||||||
|
libs := m portaudio SDL3
|
||||||
|
includes := src/
|
||||||
|
cflags := $(addprefix -I,${includes}) ${CFLAGS}
|
||||||
|
cxxflags := $(addprefix -I,${includes}) ${CXXFLAGS}
|
||||||
|
ldflags := $(addprefix -l,${libs}) ${LDFLAGS}
|
||||||
|
LD := ${CXX}
|
||||||
|
|
||||||
|
ns := out/${NS}
|
||||||
|
|
||||||
|
csrcs := $(wildcard src/*.c)
|
||||||
|
cxxsrcs := $(wildcard src/*.c++)
|
||||||
|
objs := ${csrcs:src/%.c=${ns}/%.o} ${cxxsrcs:src/%.c++=${ns}/%.o}
|
||||||
|
deps := $(wildcard out/*.d)
|
||||||
|
|
||||||
|
.PHONY: all res bin run clean
|
||||||
|
|
||||||
|
all: res bin
|
||||||
|
bin: ${ns}/${NAME}.${EXTENSION}
|
||||||
|
run: ${ns}/${NAME}.${EXTENSION}
|
||||||
|
$<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
${RM} -r ${ns}/
|
||||||
|
|
||||||
|
${ns}/${NAME}.${EXTENSION}: ${objs}
|
||||||
|
${LD} ${ldflags} -o $@ $^
|
||||||
|
|
||||||
|
${ns}/%.o: src/%.c | ${ns}/
|
||||||
|
${CC} ${cflags} -c -o $@ -MMD -MP -MF ${@:.o=.d} $<
|
||||||
|
|
||||||
|
${ns}/%.o: src/%.c++ | ${ns}/
|
||||||
|
${CXX} ${cxxflags} -c -o $@ -MMD -MP -MF ${@:.o=.d} $<
|
||||||
|
|
||||||
|
%/:
|
||||||
|
@${MKDIR} $@
|
||||||
|
|
||||||
|
#include assets.mk
|
||||||
|
|
||||||
|
ifeq ($(filter clean mostlyclean,${MAKECMDGOALS}),)
|
||||||
|
include ${deps}
|
||||||
|
endif
|
||||||
311
src/KHR/khrplatform.h
Normal file
311
src/KHR/khrplatform.h
Normal file
@ -0,0 +1,311 @@
|
|||||||
|
#ifndef __khrplatform_h_
|
||||||
|
#define __khrplatform_h_
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Copyright (c) 2008-2018 The Khronos Group Inc.
|
||||||
|
**
|
||||||
|
** Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
** copy of this software and/or associated documentation files (the
|
||||||
|
** "Materials"), to deal in the Materials without restriction, including
|
||||||
|
** without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
** distribute, sublicense, and/or sell copies of the Materials, and to
|
||||||
|
** permit persons to whom the Materials are furnished to do so, subject to
|
||||||
|
** the following conditions:
|
||||||
|
**
|
||||||
|
** The above copyright notice and this permission notice shall be included
|
||||||
|
** in all copies or substantial portions of the Materials.
|
||||||
|
**
|
||||||
|
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Khronos platform-specific types and definitions.
|
||||||
|
*
|
||||||
|
* The master copy of khrplatform.h is maintained in the Khronos EGL
|
||||||
|
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
|
||||||
|
* The last semantic modification to khrplatform.h was at commit ID:
|
||||||
|
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
|
||||||
|
*
|
||||||
|
* Adopters may modify this file to suit their platform. Adopters are
|
||||||
|
* encouraged to submit platform specific modifications to the Khronos
|
||||||
|
* group so that they can be included in future versions of this file.
|
||||||
|
* Please submit changes by filing pull requests or issues on
|
||||||
|
* the EGL Registry repository linked above.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* See the Implementer's Guidelines for information about where this file
|
||||||
|
* should be located on your system and for more details of its use:
|
||||||
|
* http://www.khronos.org/registry/implementers_guide.pdf
|
||||||
|
*
|
||||||
|
* This file should be included as
|
||||||
|
* #include <KHR/khrplatform.h>
|
||||||
|
* by Khronos client API header files that use its types and defines.
|
||||||
|
*
|
||||||
|
* The types in khrplatform.h should only be used to define API-specific types.
|
||||||
|
*
|
||||||
|
* Types defined in khrplatform.h:
|
||||||
|
* khronos_int8_t signed 8 bit
|
||||||
|
* khronos_uint8_t unsigned 8 bit
|
||||||
|
* khronos_int16_t signed 16 bit
|
||||||
|
* khronos_uint16_t unsigned 16 bit
|
||||||
|
* khronos_int32_t signed 32 bit
|
||||||
|
* khronos_uint32_t unsigned 32 bit
|
||||||
|
* khronos_int64_t signed 64 bit
|
||||||
|
* khronos_uint64_t unsigned 64 bit
|
||||||
|
* khronos_intptr_t signed same number of bits as a pointer
|
||||||
|
* khronos_uintptr_t unsigned same number of bits as a pointer
|
||||||
|
* khronos_ssize_t signed size
|
||||||
|
* khronos_usize_t unsigned size
|
||||||
|
* khronos_float_t signed 32 bit floating point
|
||||||
|
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
|
||||||
|
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
|
||||||
|
* nanoseconds
|
||||||
|
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
|
||||||
|
* khronos_boolean_enum_t enumerated boolean type. This should
|
||||||
|
* only be used as a base type when a client API's boolean type is
|
||||||
|
* an enum. Client APIs which use an integer or other type for
|
||||||
|
* booleans cannot use this as the base type for their boolean.
|
||||||
|
*
|
||||||
|
* Tokens defined in khrplatform.h:
|
||||||
|
*
|
||||||
|
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
|
||||||
|
*
|
||||||
|
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
|
||||||
|
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
|
||||||
|
*
|
||||||
|
* Calling convention macros defined in this file:
|
||||||
|
* KHRONOS_APICALL
|
||||||
|
* KHRONOS_APIENTRY
|
||||||
|
* KHRONOS_APIATTRIBUTES
|
||||||
|
*
|
||||||
|
* These may be used in function prototypes as:
|
||||||
|
*
|
||||||
|
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
|
||||||
|
* int arg1,
|
||||||
|
* int arg2) KHRONOS_APIATTRIBUTES;
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
|
||||||
|
# define KHRONOS_STATIC 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
* Definition of KHRONOS_APICALL
|
||||||
|
*-------------------------------------------------------------------------
|
||||||
|
* This precedes the return type of the function in the function prototype.
|
||||||
|
*/
|
||||||
|
#if defined(KHRONOS_STATIC)
|
||||||
|
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
|
||||||
|
* header compatible with static linking. */
|
||||||
|
# define KHRONOS_APICALL
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
# define KHRONOS_APICALL __declspec(dllimport)
|
||||||
|
#elif defined (__SYMBIAN32__)
|
||||||
|
# define KHRONOS_APICALL IMPORT_C
|
||||||
|
#elif defined(__ANDROID__)
|
||||||
|
# define KHRONOS_APICALL __attribute__((visibility("default")))
|
||||||
|
#else
|
||||||
|
# define KHRONOS_APICALL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
* Definition of KHRONOS_APIENTRY
|
||||||
|
*-------------------------------------------------------------------------
|
||||||
|
* This follows the return type of the function and precedes the function
|
||||||
|
* name in the function prototype.
|
||||||
|
*/
|
||||||
|
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
|
||||||
|
/* Win32 but not WinCE */
|
||||||
|
# define KHRONOS_APIENTRY __stdcall
|
||||||
|
#else
|
||||||
|
# define KHRONOS_APIENTRY
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
* Definition of KHRONOS_APIATTRIBUTES
|
||||||
|
*-------------------------------------------------------------------------
|
||||||
|
* This follows the closing parenthesis of the function prototype arguments.
|
||||||
|
*/
|
||||||
|
#if defined (__ARMCC_2__)
|
||||||
|
#define KHRONOS_APIATTRIBUTES __softfp
|
||||||
|
#else
|
||||||
|
#define KHRONOS_APIATTRIBUTES
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
* basic type definitions
|
||||||
|
*-----------------------------------------------------------------------*/
|
||||||
|
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Using <stdint.h>
|
||||||
|
*/
|
||||||
|
#include <stdint.h>
|
||||||
|
typedef int32_t khronos_int32_t;
|
||||||
|
typedef uint32_t khronos_uint32_t;
|
||||||
|
typedef int64_t khronos_int64_t;
|
||||||
|
typedef uint64_t khronos_uint64_t;
|
||||||
|
#define KHRONOS_SUPPORT_INT64 1
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 1
|
||||||
|
/*
|
||||||
|
* To support platform where unsigned long cannot be used interchangeably with
|
||||||
|
* inptr_t (e.g. CHERI-extended ISAs), we can use the stdint.h intptr_t.
|
||||||
|
* Ideally, we could just use (u)intptr_t everywhere, but this could result in
|
||||||
|
* ABI breakage if khronos_uintptr_t is changed from unsigned long to
|
||||||
|
* unsigned long long or similar (this results in different C++ name mangling).
|
||||||
|
* To avoid changes for existing platforms, we restrict usage of intptr_t to
|
||||||
|
* platforms where the size of a pointer is larger than the size of long.
|
||||||
|
*/
|
||||||
|
#if defined(__SIZEOF_LONG__) && defined(__SIZEOF_POINTER__)
|
||||||
|
#if __SIZEOF_POINTER__ > __SIZEOF_LONG__
|
||||||
|
#define KHRONOS_USE_INTPTR_T
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#elif defined(__VMS ) || defined(__sgi)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Using <inttypes.h>
|
||||||
|
*/
|
||||||
|
#include <inttypes.h>
|
||||||
|
typedef int32_t khronos_int32_t;
|
||||||
|
typedef uint32_t khronos_uint32_t;
|
||||||
|
typedef int64_t khronos_int64_t;
|
||||||
|
typedef uint64_t khronos_uint64_t;
|
||||||
|
#define KHRONOS_SUPPORT_INT64 1
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 1
|
||||||
|
|
||||||
|
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Win32
|
||||||
|
*/
|
||||||
|
typedef __int32 khronos_int32_t;
|
||||||
|
typedef unsigned __int32 khronos_uint32_t;
|
||||||
|
typedef __int64 khronos_int64_t;
|
||||||
|
typedef unsigned __int64 khronos_uint64_t;
|
||||||
|
#define KHRONOS_SUPPORT_INT64 1
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 1
|
||||||
|
|
||||||
|
#elif defined(__sun__) || defined(__digital__)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sun or Digital
|
||||||
|
*/
|
||||||
|
typedef int khronos_int32_t;
|
||||||
|
typedef unsigned int khronos_uint32_t;
|
||||||
|
#if defined(__arch64__) || defined(_LP64)
|
||||||
|
typedef long int khronos_int64_t;
|
||||||
|
typedef unsigned long int khronos_uint64_t;
|
||||||
|
#else
|
||||||
|
typedef long long int khronos_int64_t;
|
||||||
|
typedef unsigned long long int khronos_uint64_t;
|
||||||
|
#endif /* __arch64__ */
|
||||||
|
#define KHRONOS_SUPPORT_INT64 1
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 1
|
||||||
|
|
||||||
|
#elif 0
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Hypothetical platform with no float or int64 support
|
||||||
|
*/
|
||||||
|
typedef int khronos_int32_t;
|
||||||
|
typedef unsigned int khronos_uint32_t;
|
||||||
|
#define KHRONOS_SUPPORT_INT64 0
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 0
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generic fallback
|
||||||
|
*/
|
||||||
|
#include <stdint.h>
|
||||||
|
typedef int32_t khronos_int32_t;
|
||||||
|
typedef uint32_t khronos_uint32_t;
|
||||||
|
typedef int64_t khronos_int64_t;
|
||||||
|
typedef uint64_t khronos_uint64_t;
|
||||||
|
#define KHRONOS_SUPPORT_INT64 1
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 1
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Types that are (so far) the same on all platforms
|
||||||
|
*/
|
||||||
|
typedef signed char khronos_int8_t;
|
||||||
|
typedef unsigned char khronos_uint8_t;
|
||||||
|
typedef signed short int khronos_int16_t;
|
||||||
|
typedef unsigned short int khronos_uint16_t;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Types that differ between LLP64 and LP64 architectures - in LLP64,
|
||||||
|
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
|
||||||
|
* to be the only LLP64 architecture in current use.
|
||||||
|
*/
|
||||||
|
#ifdef KHRONOS_USE_INTPTR_T
|
||||||
|
typedef intptr_t khronos_intptr_t;
|
||||||
|
typedef uintptr_t khronos_uintptr_t;
|
||||||
|
#elif defined(_WIN64)
|
||||||
|
typedef signed long long int khronos_intptr_t;
|
||||||
|
typedef unsigned long long int khronos_uintptr_t;
|
||||||
|
#else
|
||||||
|
typedef signed long int khronos_intptr_t;
|
||||||
|
typedef unsigned long int khronos_uintptr_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(_WIN64)
|
||||||
|
typedef signed long long int khronos_ssize_t;
|
||||||
|
typedef unsigned long long int khronos_usize_t;
|
||||||
|
#else
|
||||||
|
typedef signed long int khronos_ssize_t;
|
||||||
|
typedef unsigned long int khronos_usize_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if KHRONOS_SUPPORT_FLOAT
|
||||||
|
/*
|
||||||
|
* Float type
|
||||||
|
*/
|
||||||
|
typedef float khronos_float_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if KHRONOS_SUPPORT_INT64
|
||||||
|
/* Time types
|
||||||
|
*
|
||||||
|
* These types can be used to represent a time interval in nanoseconds or
|
||||||
|
* an absolute Unadjusted System Time. Unadjusted System Time is the number
|
||||||
|
* of nanoseconds since some arbitrary system event (e.g. since the last
|
||||||
|
* time the system booted). The Unadjusted System Time is an unsigned
|
||||||
|
* 64 bit value that wraps back to 0 every 584 years. Time intervals
|
||||||
|
* may be either signed or unsigned.
|
||||||
|
*/
|
||||||
|
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
|
||||||
|
typedef khronos_int64_t khronos_stime_nanoseconds_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Dummy value used to pad enum types to 32 bits.
|
||||||
|
*/
|
||||||
|
#ifndef KHRONOS_MAX_ENUM
|
||||||
|
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enumerated boolean type
|
||||||
|
*
|
||||||
|
* Values other than zero should be considered to be true. Therefore
|
||||||
|
* comparisons should not be made against KHRONOS_TRUE.
|
||||||
|
*/
|
||||||
|
typedef enum {
|
||||||
|
KHRONOS_FALSE = 0,
|
||||||
|
KHRONOS_TRUE = 1,
|
||||||
|
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
|
||||||
|
} khronos_boolean_enum_t;
|
||||||
|
|
||||||
|
#endif /* __khrplatform_h_ */
|
||||||
188
src/batch.c
Normal file
188
src/batch.c
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
#include <limits.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <glad/gles2.h>
|
||||||
|
|
||||||
|
#include "batch.h"
|
||||||
|
|
||||||
|
struct batch {
|
||||||
|
struct vertices { // 8 bytes
|
||||||
|
int16_t x, y; // 4 bytes
|
||||||
|
uint16_t s, t; // 4 bytes
|
||||||
|
// must subtract 1 after multiplying
|
||||||
|
} (*vertices)[4];
|
||||||
|
unsigned (*indices)[6];
|
||||||
|
struct index {
|
||||||
|
GLuint texture;
|
||||||
|
unsigned count;
|
||||||
|
} *index;
|
||||||
|
unsigned elements; // no point in using size_t here
|
||||||
|
GLint aVertCoord, aTexCoord, uTex;
|
||||||
|
GLuint vbo, ebo, program;
|
||||||
|
} batch = {
|
||||||
|
.vertices = NULL,
|
||||||
|
.indices = NULL,
|
||||||
|
.index = NULL,
|
||||||
|
.elements = 0,
|
||||||
|
// GLint
|
||||||
|
.aVertCoord = -1,
|
||||||
|
.aTexCoord = -1,
|
||||||
|
.uTex = -1,
|
||||||
|
// GLuint
|
||||||
|
.vbo = 0,
|
||||||
|
.ebo = 0,
|
||||||
|
.program = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void batch_insert(struct vertices v[4], GLuint texture);
|
||||||
|
|
||||||
|
void batch_blit_flippable(int x, int y, struct texture *tex, struct crop *crop) {
|
||||||
|
if (crop->w < 0) {
|
||||||
|
x -= crop->w;
|
||||||
|
}
|
||||||
|
if (crop->h < 0) {
|
||||||
|
y -= crop->h;
|
||||||
|
}
|
||||||
|
batch_blit(x, y, tex, crop);
|
||||||
|
}
|
||||||
|
|
||||||
|
void batch_blit(int x, int y, struct texture *tex, struct crop *crop) {
|
||||||
|
unsigned s1, s2, t1, t2;
|
||||||
|
if (tex->flipped) {
|
||||||
|
s1 = tex->x + crop->y;
|
||||||
|
t1 = tex->y + crop->x;
|
||||||
|
s2 = s1 + abs(crop->h);
|
||||||
|
t2 = t1 + abs(crop->w);
|
||||||
|
} else {
|
||||||
|
s1 = tex->x + crop->x;
|
||||||
|
t1 = tex->y + crop->y;
|
||||||
|
s2 = s1 + abs(crop->w);
|
||||||
|
t2 = t1 + abs(crop->h);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct vertices vi = {
|
||||||
|
.x = x, .y = y,
|
||||||
|
.s = s1 * tex->size,
|
||||||
|
.t = t1 * tex->size,
|
||||||
|
};
|
||||||
|
struct vertices v[4] = {vi, vi, vi, vi};
|
||||||
|
|
||||||
|
v[1].x += crop->w;
|
||||||
|
v[2].y += crop->h;
|
||||||
|
if (tex->flipped) {
|
||||||
|
v[2].s = (s2 * tex->size) - 1;
|
||||||
|
v[1].t = (t2 * tex->size) - 1;
|
||||||
|
} else {
|
||||||
|
v[1].s = (s2 * tex->size) - 1;
|
||||||
|
v[2].t = (t2 * tex->size) - 1;
|
||||||
|
}
|
||||||
|
v[3].x += crop->w;
|
||||||
|
v[3].y += crop->h;
|
||||||
|
v[3].s = (s2 * tex->size) - 1;
|
||||||
|
v[3].t = (t2 * tex->size) - 1;
|
||||||
|
batch_insert(v, tex->texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void batch_insert(struct vertices v[4], GLuint texture) {
|
||||||
|
unsigned i, offset = 0;
|
||||||
|
for (i = 0; batch.index[i].texture < texture && batch.index[i].count; i++) {
|
||||||
|
offset += batch.index[i].count;
|
||||||
|
}
|
||||||
|
if (batch.index[i].texture > texture || batch.index[i].count == 0) {
|
||||||
|
// insert new bucket
|
||||||
|
size_t s;
|
||||||
|
for (s = 0; batch.index[i + s].count; s++)
|
||||||
|
;
|
||||||
|
memmove(batch.index + i + 1, batch.index + i, sizeof (*batch.index) * s);
|
||||||
|
if (batch.index[i].count == 0) {
|
||||||
|
batch.index[i + 1].count = 0;
|
||||||
|
}
|
||||||
|
batch.index[i].texture = texture;
|
||||||
|
batch.index[i].count = 1;
|
||||||
|
} else {
|
||||||
|
offset += batch.index[i].count;
|
||||||
|
batch.index[i].count++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
struct vertices swap1[4], swap2[4];
|
||||||
|
memcpy(swap2, v, sizeof (swap2));
|
||||||
|
while (batch.index[i].count) {
|
||||||
|
memcpy(swap1, batch.vertices[offset], sizeof (swap1));
|
||||||
|
memcpy(batch.vertices + offset, swap2, sizeof (swap2));
|
||||||
|
memcpy(swap2, swap1, sizeof (swap2));
|
||||||
|
offset += batch.index[i].count;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
memcpy(batch.vertices + offset, swap2, sizeof (swap2));
|
||||||
|
}
|
||||||
|
|
||||||
|
void batch_flush(void) {
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, batch.vbo);
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.ebo);
|
||||||
|
|
||||||
|
glBufferSubData(GL_ARRAY_BUFFER, 0, 4 * sizeof (*batch.vertices) * batch.elements, batch.vertices);
|
||||||
|
|
||||||
|
glUseProgram(batch.program);
|
||||||
|
|
||||||
|
glVertexAttribPointer(batch.aVertCoord, 2, GL_SHORT, GL_FALSE, sizeof (struct vertices), (void *) 0);
|
||||||
|
glVertexAttribPointer(batch.aTexCoord, 2, GL_UNSIGNED_SHORT, GL_TRUE, sizeof (struct vertices), (void *) 4);
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(batch.aVertCoord);
|
||||||
|
glEnableVertexAttribArray(batch.aTexCoord);
|
||||||
|
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glUniform1i(batch.uTex, 0);
|
||||||
|
for (unsigned i = 0, offset = 0; batch.index[i].count; i++, offset += batch.index[i].count) {
|
||||||
|
glBindTexture(GL_TEXTURE_2D, batch.index[i].texture);
|
||||||
|
glDrawElements(GL_TRIANGLES, batch.index[i].count * 6, GL_UNSIGNED_INT, (void *) ((uintptr_t) offset * 6));
|
||||||
|
}
|
||||||
|
|
||||||
|
glDisableVertexAttribArray(batch.aVertCoord);
|
||||||
|
glDisableVertexAttribArray(batch.aTexCoord);
|
||||||
|
|
||||||
|
batch.index->count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int batch_init(unsigned batch_size, GLuint program) {
|
||||||
|
batch.vertices = malloc(sizeof (*batch.vertices) * batch_size);
|
||||||
|
unsigned (*indices)[6] /*batch.indicies*/ = malloc(sizeof (* /*batch.*/indices) * batch_size);
|
||||||
|
batch.index = malloc(sizeof (*batch.index) * 16);
|
||||||
|
if (batch.vertices == NULL || /*batch.*/indices == NULL || batch.index == NULL) {
|
||||||
|
free(batch.vertices);
|
||||||
|
free(indices);
|
||||||
|
free(batch.index);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
batch.index->count = 0;
|
||||||
|
batch.elements = batch_size;
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < batch_size; i++) {
|
||||||
|
/*batch.*/indices[i][0] = i * 4 + 0;
|
||||||
|
/*batch.*/indices[i][1] = i * 4 + 1;
|
||||||
|
/*batch.*/indices[i][2] = i * 4 + 2;
|
||||||
|
/*batch.*/indices[i][3] = i * 4 + 3;
|
||||||
|
/*batch.*/indices[i][4] = i * 4 + 1;
|
||||||
|
/*batch.*/indices[i][5] = i * 4 + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
batch.aVertCoord = glGetAttribLocation(program, "aVertCoord");
|
||||||
|
batch.aTexCoord = glGetAttribLocation(program, "aTexCoord");
|
||||||
|
batch.uTex = glGetUniformLocation(program, "uTex");
|
||||||
|
|
||||||
|
glGenBuffers(1, &batch.vbo);
|
||||||
|
glGenBuffers(1, &batch.ebo);
|
||||||
|
batch.program = program;
|
||||||
|
|
||||||
|
// just allocate
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, batch.vbo);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof (*batch.vertices) * batch.elements, NULL, GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
|
// we can fill up this one already
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.ebo);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof (* /*batch.*/indices) * batch.elements, /*batch.*/indices, GL_STATIC_DRAW);
|
||||||
|
free(indices);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
33
src/batch.h
Normal file
33
src/batch.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <glad/gles2.h>
|
||||||
|
|
||||||
|
struct texture {
|
||||||
|
GLuint texture;
|
||||||
|
unsigned x, y/*, w, h*/;
|
||||||
|
unsigned size: CHAR_BIT * sizeof (unsigned) - 1;
|
||||||
|
bool flipped: 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct crop {
|
||||||
|
unsigned x, y;
|
||||||
|
int w, h;
|
||||||
|
};
|
||||||
|
|
||||||
|
void batch_blit(int x, int y, struct texture *tex, struct crop *crop);
|
||||||
|
void batch_blit_flippable(int x, int y, struct texture *tex, struct crop *crop);
|
||||||
|
|
||||||
|
void batch_flush(void);
|
||||||
|
|
||||||
|
int batch_init(unsigned batch_size, GLuint program);
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
31
src/default.glsl
Normal file
31
src/default.glsl
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#version 100
|
||||||
|
|
||||||
|
##
|
||||||
|
|
||||||
|
precision mediump float;
|
||||||
|
|
||||||
|
@@
|
||||||
|
|
||||||
|
varying vec2 vTexCoord;
|
||||||
|
|
||||||
|
%%
|
||||||
|
|
||||||
|
attribute vec2 aVertCoord;
|
||||||
|
attribute vec2 aTexCoord;
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
vTexCoord = aTexCoord;
|
||||||
|
gl_Position = vec4(aVertCoord, 0.0, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
##
|
||||||
|
|
||||||
|
uniform sampler2D uTex;
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
vec4 Color = texture2D(uTex, vTexCoord)
|
||||||
|
if (Color.a < 0.5) {
|
||||||
|
discard;
|
||||||
|
}
|
||||||
|
gl_FragColor = Color;
|
||||||
|
}
|
||||||
6136
src/glad/gles2.h
Normal file
6136
src/glad/gles2.h
Normal file
File diff suppressed because one or more lines are too long
3234
src/gles2.c
Normal file
3234
src/gles2.c
Normal file
File diff suppressed because it is too large
Load Diff
52
src/main.c++
Normal file
52
src/main.c++
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#include <glad/gles2.h>
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
#define WINDOW_WIDTH 128
|
||||||
|
#define WINDOW_HEIGHT 128
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
clock_t begin = clock();
|
||||||
|
|
||||||
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
||||||
|
|
||||||
|
SDL_Window *window = SDL_CreateWindow("suwi", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL /*| SDL_WINDOW_RESIZABLE*/ | SDL_WINDOW_TRANSPARENT);
|
||||||
|
|
||||||
|
SDL_GLContext context = SDL_GL_CreateContext(window);
|
||||||
|
|
||||||
|
int version = gladLoadGLES2((GLADloadfunc) SDL_GL_GetProcAddress);
|
||||||
|
if (version == 0) {
|
||||||
|
puts("error: GLES2 failed to initialize");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
fprintf(stderr, "info: ver %u\n", version);
|
||||||
|
|
||||||
|
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||||
|
SDL_ShowWindow(window);
|
||||||
|
|
||||||
|
fprintf(stderr, "info: init took %lu ms\n", ((clock() - begin) * 1000) / CLOCKS_PER_SEC);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
SDL_Event evt;
|
||||||
|
while (SDL_PollEvent(&evt)) {
|
||||||
|
switch (evt.type) {
|
||||||
|
case SDL_EVENT_QUIT:
|
||||||
|
return 0;
|
||||||
|
default:;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
glClearColor(0.5, 0.5, 0.5, 0.0);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
SDL_GL_SwapWindow(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
260
src/rect-pack.c
Normal file
260
src/rect-pack.c
Normal file
@ -0,0 +1,260 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "rect-pack.h"
|
||||||
|
#include "rect-sort.h"
|
||||||
|
|
||||||
|
#define NO_MATCH (~(size_t) 0)
|
||||||
|
|
||||||
|
#define min(a, b) (a > b? b: a)
|
||||||
|
#define max(a, b) (a > b? a: b)
|
||||||
|
|
||||||
|
struct slot {
|
||||||
|
unsigned x, y;
|
||||||
|
unsigned w, h;
|
||||||
|
unsigned bin;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int try_merge(struct slot *a, struct slot *b) {
|
||||||
|
if (a->bin != b->bin) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (a->x == b->x + b->w && a->y == b->y && a->h == b->h) {
|
||||||
|
*b = (struct slot) {b->x, a->y, a->w + b->w, a->h, a->bin};
|
||||||
|
} else if (a->x + a->w == b->x && a->y == b->y && a->h == b->h) {
|
||||||
|
*b = (struct slot) {a->x, a->y, a->w + b->w, a->h, a->bin};
|
||||||
|
} else if (a->y == b->y + b->h && a->x == b->x && a->w == b->w) {
|
||||||
|
*b = (struct slot) {a->x, b->y, a->w, a->h + b->h, a->bin};
|
||||||
|
} else if (a->y + a->w == b->y && a->x == b->x && a->w == b->w) {
|
||||||
|
*b = (struct slot) {a->x, a->y, a->w, a->h + b->h, a->bin};
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
static void split(unsigned w, unsigned h, struct slot *slots, struct slot *from_, size_t *count) {
|
||||||
|
struct slot to, from = *from_;
|
||||||
|
*from_ = slots[--(*count)];
|
||||||
|
//fprintf(stderr, "%u %ux%u \\ ", from.bin, from.w, from.h);
|
||||||
|
|
||||||
|
if (from.w - w < from.h - h) {
|
||||||
|
to = (struct slot) {from.x + w, from.y, from.w - w, h, from.bin};
|
||||||
|
from = (struct slot) {from.x, from.y + h, from.w, from.h - h, from.bin};
|
||||||
|
} else {
|
||||||
|
to = (struct slot) {from.x, from.y + h, w, from.h - h, from.bin};
|
||||||
|
from = (struct slot) {from.x + w, from.y, from.w - w, from.h, from.bin};
|
||||||
|
}
|
||||||
|
//fprintf(stderr, "%ux%u -> %ux%u + %ux%u\n", w, h, to.w, to.h, from.w, from.h); // mathematicians when i use -> and + rather than = and ⋃
|
||||||
|
|
||||||
|
bool to_nonempty = (to.w && to.h), from_nonempty = (from.w && from.h);
|
||||||
|
|
||||||
|
if (to_nonempty) {
|
||||||
|
for (size_t i = 0; i < *count; i++) {
|
||||||
|
if (try_merge(&to, slots + i)) {
|
||||||
|
goto to_done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slots[(*count)++] = to;
|
||||||
|
}
|
||||||
|
to_done:
|
||||||
|
|
||||||
|
if (from_nonempty) {
|
||||||
|
for (size_t i = 0; i < *count; i++) {
|
||||||
|
if (try_merge(&from, slots + i)) {
|
||||||
|
goto from_done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slots[(*count)++] = from;
|
||||||
|
}
|
||||||
|
from_done:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct pack_result guilotine_(struct rect **left, size_t left_count, unsigned maxwidth, unsigned maxheight, unsigned bins, unsigned maxbins);
|
||||||
|
|
||||||
|
struct pack_result guilotine(struct rect *rects, size_t items, unsigned maxsize, unsigned maxbins) {
|
||||||
|
if (items == 0)
|
||||||
|
return (struct pack_result) {0, 0, 0, 0};
|
||||||
|
unsigned bins = 1;
|
||||||
|
if (bins > maxbins) {
|
||||||
|
for (size_t i = 0; i < items; i++) {
|
||||||
|
rects[i].bin = GUILOTINE_NOT_PACKED;
|
||||||
|
}
|
||||||
|
return (struct pack_result) {0, 0, 0, 0};
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t left_count = items;
|
||||||
|
struct rect **left = malloc(sizeof (void *) * left_count);
|
||||||
|
for (size_t i = 0; i < items; i++) {
|
||||||
|
left[i] = rects + i;
|
||||||
|
}
|
||||||
|
qsort(left, items, sizeof (void *), sort_ls);
|
||||||
|
|
||||||
|
return guilotine_(left, left_count, maxsize, maxsize, 0, maxbins);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct pack_result guilotine_(struct rect **left_base, size_t left_count, unsigned maxwidth, unsigned maxheight, unsigned bins, unsigned maxbins) {
|
||||||
|
fprintf(stderr, "%zu %ux%u, %u\n", left_count, maxwidth, maxheight, maxbins - bins);
|
||||||
|
struct rect **left = left_base;
|
||||||
|
unsigned in_last_bin = 0;
|
||||||
|
size_t slot_count = 1;
|
||||||
|
struct slot *slots = malloc(sizeof (*slots) * (left_count + maxbins));
|
||||||
|
slots[0] = (struct slot) {0, 0, maxwidth, maxheight, bins++};
|
||||||
|
|
||||||
|
unsigned const firstbin = bins; // used later
|
||||||
|
|
||||||
|
while (left_count > 0) {
|
||||||
|
unsigned fitness = ~0;
|
||||||
|
size_t best_slot = NO_MATCH;
|
||||||
|
for (size_t s = 0; s < slot_count; s++) {
|
||||||
|
if ((left[0]->w <= slots[s].w && left[0]->h <= slots[s].h) ||
|
||||||
|
(left[0]->w <= slots[s].h && left[0]->h <= slots[s].w)) {
|
||||||
|
unsigned new_fitness = min(
|
||||||
|
min(slots[s].w - left[0]->w, slots[s].h - left[0]->h),
|
||||||
|
min(slots[s].w - left[0]->h, slots[s].h - left[0]->w)
|
||||||
|
);
|
||||||
|
if (new_fitness < fitness) {
|
||||||
|
fitness = new_fitness;
|
||||||
|
best_slot = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (best_slot == NO_MATCH) {
|
||||||
|
if (bins < maxbins && left[0]->w <= max(maxwidth, maxheight) && left[0]->h <= max(maxwidth, maxheight)) {
|
||||||
|
best_slot = slot_count;
|
||||||
|
slots[slot_count++] = (struct slot) {0, 0, maxwidth, maxheight, bins++};
|
||||||
|
in_last_bin = 0;
|
||||||
|
} else {
|
||||||
|
for (size_t i = 0; i < left_count; i++) {
|
||||||
|
left[i]->bin = GUILOTINE_NOT_PACKED;
|
||||||
|
}
|
||||||
|
free(slots);
|
||||||
|
free(left_base);
|
||||||
|
return (struct pack_result) {0, 0, 0, 0};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct slot *best = slots + best_slot;
|
||||||
|
bool normal = left[0]->w <= best->w && left[0]->h <= best->h;
|
||||||
|
bool flipped = left[0]->w <= best->h && left[0]->h <= best->w;
|
||||||
|
|
||||||
|
if (!(normal && flipped)) {
|
||||||
|
left[0]->x = best->x;
|
||||||
|
left[0]->y = best->y;
|
||||||
|
left[0]->bin = best->bin;
|
||||||
|
if (flipped) {
|
||||||
|
left[0]->flipped = true;
|
||||||
|
split(left[0]->h, left[0]->w, slots, best, &slot_count);
|
||||||
|
} else {
|
||||||
|
left[0]->flipped = false;
|
||||||
|
split(left[0]->w, left[0]->h, slots, best, &slot_count);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
left[0]->x = best->x;
|
||||||
|
left[0]->y = best->y;
|
||||||
|
left[0]->bin = best->bin;
|
||||||
|
unsigned normal_fitness = min(best->w - left[0]->w, best->h - left[0]->h);
|
||||||
|
unsigned flipped_fitness = min(best->w - left[0]->h, best->h - left[0]->w);
|
||||||
|
if (flipped_fitness < normal_fitness) {
|
||||||
|
// flipped
|
||||||
|
left[0]->flipped = true;
|
||||||
|
split(left[0]->h, left[0]->w, slots, best, &slot_count);
|
||||||
|
} else {
|
||||||
|
// normal
|
||||||
|
left[0]->flipped = false;
|
||||||
|
split(left[0]->w, left[0]->h, slots, best, &slot_count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (left[0]->bin == bins - 1) {
|
||||||
|
in_last_bin++;
|
||||||
|
}
|
||||||
|
|
||||||
|
left++;
|
||||||
|
left_count--;
|
||||||
|
//memmove(left + 0, left + 1, sizeof (*left) * --left_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
//left is discarded by this point
|
||||||
|
struct rect *tighter = malloc(sizeof (*tighter) * in_last_bin);
|
||||||
|
struct rect **other_left = malloc(sizeof (void *) * in_last_bin);
|
||||||
|
for (size_t i = bins - firstbin, j = 0; j < in_last_bin; i++) {
|
||||||
|
// we can skip the first (bins - 1) items
|
||||||
|
// since under normal circumstances
|
||||||
|
// theyre guaranteed to be placed in previous bins
|
||||||
|
if (left_base[i]->bin == bins - 1) {
|
||||||
|
tighter[j] = *(left_base[i]);
|
||||||
|
other_left[j] = tighter + j;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//fprintf(stderr, "%u in %ux%u bin %u\n", in_last_bin, maxsize, maxsize, bins - 1);
|
||||||
|
struct pack_result last_bin = guilotine_(other_left, in_last_bin, maxwidth / 2, maxheight / 2, bins - 1, bins);
|
||||||
|
if (last_bin.bins == 0) {
|
||||||
|
//left is discarded by this point
|
||||||
|
struct rect *tighter = malloc(sizeof (*tighter) * in_last_bin);
|
||||||
|
struct rect **other_left = malloc(sizeof (void *) * in_last_bin);
|
||||||
|
for (size_t i = bins - firstbin, j = 0; j < in_last_bin; i++) {
|
||||||
|
// we can skip the first (bins - 1) items
|
||||||
|
// since under normal circumstances
|
||||||
|
// theyre guaranteed to be placed in previous bins
|
||||||
|
if (left_base[i]->bin == bins - 1) {
|
||||||
|
tighter[j] = *(left_base[i]);
|
||||||
|
other_left[j] = tighter + j;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*struct pack_result*/ last_bin = guilotine_(other_left, in_last_bin, maxwidth / 2, maxheight, bins - 1, bins);
|
||||||
|
if (last_bin.bins == 0) {
|
||||||
|
last_bin.last_bin_size = max(maxwidth, maxheight);
|
||||||
|
last_bin.last_bin_width = maxwidth;
|
||||||
|
last_bin.last_bin_height = maxheight;
|
||||||
|
} else {
|
||||||
|
// success
|
||||||
|
//fprintf(stderr, "last %u -> %u\n", maxsize, maxsize / 2);
|
||||||
|
for (size_t i = bins - firstbin, j = 0; j < in_last_bin; i++) {
|
||||||
|
if (left_base[i]->bin == bins - 1) {
|
||||||
|
*(left_base[i]) = tighter[j++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(tighter);
|
||||||
|
} else {
|
||||||
|
// success
|
||||||
|
//fprintf(stderr, "last %u -> %u\n", maxsize, maxsize / 2);
|
||||||
|
for (size_t i = bins - firstbin, j = 0; j < in_last_bin; i++) {
|
||||||
|
if (left_base[i]->bin == bins - 1) {
|
||||||
|
*(left_base[i]) = tighter[j++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//free(other_left);
|
||||||
|
free(tighter);
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
fprintf(stdout, "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 %u %u\">", (maxsize + 16) * bins - 16, maxsize);
|
||||||
|
for (unsigned i = 0; i < bins; i++) {
|
||||||
|
unsigned binx = (maxsize + 16) * i, biny = 0;
|
||||||
|
fprintf(stdout, "<view id=\"%u\" viewBox=\"%u %u %u %u\"/>", i, binx, biny, maxsize, maxsize);
|
||||||
|
}
|
||||||
|
fputs("<style>rect{stroke:black;}.bg{fill:gray;}.flipped{fill:green;}.normal{fill:blue}text{font-family:monospace;text-anchor:middle;alignment-baseline:central;}</style>", stdout);
|
||||||
|
for (unsigned i = 0; i < items; i++) {
|
||||||
|
unsigned w = rects[i].flipped? rects[i].h: rects[i].w;
|
||||||
|
unsigned h = rects[i].flipped? rects[i].w: rects[i].h;
|
||||||
|
unsigned binx = (maxsize + 16) * rects[i].bin, biny = 0;
|
||||||
|
fprintf(stdout, "<rect x=\"%u.5\" y=\"%u.5\" width=\"%u\" height=\"%u\" class=\"%s\"/><text x=\"%u\" y=\"%u\" style=\"font-size: %upx\">%u</text>", rects[i].x + binx, rects[i].y + biny, w - 1, h - 1, rects[i].flipped? "flipped": "normal", rects[i].x + w / 2 + binx, rects[i].y + h / 2 + biny, h > 20? 20: h, i);
|
||||||
|
}
|
||||||
|
for (unsigned i = 0; i < slot_count; i++) {
|
||||||
|
unsigned binx = (maxsize + 16) * slots[i].bin, biny = 0;
|
||||||
|
fprintf(stdout, "<rect x=\"%u.5\" y=\"%u.5\" width=\"%u\" height=\"%u\" class=\"bg\"/>", slots[i].x + binx, slots[i].y + biny, slots[i].w - 1, slots[i].h - 1);
|
||||||
|
}
|
||||||
|
fputs("</svg>", stdout);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
free(left_base);
|
||||||
|
free(slots);
|
||||||
|
fprintf(stderr, "%u %u %u %u\n", bins, last_bin.last_bin_size, last_bin.last_bin_width, last_bin.last_bin_height);
|
||||||
|
return (struct pack_result) {bins, last_bin.last_bin_size, last_bin.last_bin_width, last_bin.last_bin_height};
|
||||||
|
}
|
||||||
33
src/rect-pack.h
Normal file
33
src/rect-pack.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define GUILOTINE_NOT_PACKED ((~0u) >> 1)
|
||||||
|
|
||||||
|
struct rect {
|
||||||
|
unsigned x, y; // output: where in the bin to put the rectangle
|
||||||
|
unsigned w, h; // input: width and height, not modified by packing func
|
||||||
|
bool flipped: 1; // output: whether to swap the width and height when placing the rectangle
|
||||||
|
unsigned bin: // output: which bin to put the rectangle in
|
||||||
|
CHAR_BIT * sizeof (unsigned) - 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct pack_result {
|
||||||
|
// set to all-0 on failure
|
||||||
|
unsigned bins; // how many bins were needed
|
||||||
|
unsigned last_bin_size; // the size of the last bin, which gets shrunk, if possible
|
||||||
|
unsigned last_bin_width;
|
||||||
|
unsigned last_bin_height;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct pack_result guilotine(struct rect *rects, size_t items, unsigned maxsize, unsigned maxbins);
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
52
src/rect-sort.c
Normal file
52
src/rect-sort.c
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#include "rect-pack.h"
|
||||||
|
#include "rect-sort.h"
|
||||||
|
|
||||||
|
#define min(a, b) (a > b? b: a)
|
||||||
|
#define max(a, b) (a > b? a: b)
|
||||||
|
|
||||||
|
int sort_a(void const *a_, void const *b_) {
|
||||||
|
struct rect const *a = *(struct rect **) a_, *b = *(struct rect **) b_;
|
||||||
|
unsigned aa = a->w * a->h, bb = b->w * b->h;
|
||||||
|
return bb - aa;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sort_ss(void const *a_, void const *b_) {
|
||||||
|
struct rect const *a = *(struct rect **) a_, *b = *(struct rect **) b_;
|
||||||
|
unsigned const amin = min(a->w, a->h), amax = max(a->w, a->h);
|
||||||
|
unsigned const bmin = min(b->w, b->h), bmax = max(b->w, b->h);
|
||||||
|
if (amin != bmin) {
|
||||||
|
return bmin - amin;
|
||||||
|
} else {
|
||||||
|
return bmax - amax;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int sort_ls(void const *a_, void const *b_) {
|
||||||
|
struct rect const *a = *(struct rect **) a_, *b = *(struct rect **) b_;
|
||||||
|
unsigned const amin = min(a->w, a->h), amax = max(a->w, a->h);
|
||||||
|
unsigned const bmin = min(b->w, b->h), bmax = max(b->w, b->h);
|
||||||
|
if (amax != bmax) {
|
||||||
|
return bmax - amax;
|
||||||
|
} else {
|
||||||
|
return bmin - amin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int sort_perim(void const *a_, void const *b_) {
|
||||||
|
struct rect const *a = *(struct rect **) a_, *b = *(struct rect **) b_;
|
||||||
|
return (b->w + b->h) - (a->w + a->h);
|
||||||
|
}
|
||||||
|
|
||||||
|
int sort_diff(void const *a_, void const *b_) {
|
||||||
|
struct rect const *a = *(struct rect **) a_, *b = *(struct rect **) b_;
|
||||||
|
unsigned const amin = min(a->w, a->h), amax = max(a->w, a->h);
|
||||||
|
unsigned const bmin = min(b->w, b->h), bmax = max(b->w, b->h);
|
||||||
|
return (bmax - bmin) - (amax - amin);
|
||||||
|
}
|
||||||
|
|
||||||
|
int sort_ratio(void const *a_, void const *b_) {
|
||||||
|
struct rect const *a = *(struct rect **) a_, *b = *(struct rect **) b_;
|
||||||
|
unsigned const amin = min(a->w, a->h), amax = max(a->w, a->h);
|
||||||
|
unsigned const bmin = min(b->w, b->h), bmax = max(b->w, b->h);
|
||||||
|
return bmin * amax - amin * bmax;
|
||||||
|
}
|
||||||
16
src/rect-sort.h
Normal file
16
src/rect-sort.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int sort_a(void const *, void const *);
|
||||||
|
int sort_ss(void const *, void const *);
|
||||||
|
int sort_ls(void const *, void const *);
|
||||||
|
int sort_perim(void const *, void const *);
|
||||||
|
int sort_diff(void const *, void const *);
|
||||||
|
int sort_ratio(void const *, void const *);
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
30
src/spr.2Dsolid.glsl
Normal file
30
src/spr.2Dsolid.glsl
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#version 100
|
||||||
|
|
||||||
|
##
|
||||||
|
|
||||||
|
precision mediump float;
|
||||||
|
|
||||||
|
@@
|
||||||
|
|
||||||
|
varying vec2 vTexCoord;
|
||||||
|
|
||||||
|
%%
|
||||||
|
|
||||||
|
attribute vec2 aVertCoord;
|
||||||
|
attribute vec2 aTexCoord;
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
vTexCoord = aTexCoord;
|
||||||
|
gl_Position = vec4(aVertCoord * vec2(1.0 / 256.0), 0.0, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
##
|
||||||
|
|
||||||
|
uniform sampler2D uTex;
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
if (texture2D(uTex, vTexCoord).r < 0.5) {
|
||||||
|
discard;
|
||||||
|
}
|
||||||
|
gl_FragColor = vec4(1.0);
|
||||||
|
}
|
||||||
15
src/up.normal.frag
Normal file
15
src/up.normal.frag
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#version 100
|
||||||
|
|
||||||
|
precision mediump float;
|
||||||
|
|
||||||
|
varying vec2 vTexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D uTex;
|
||||||
|
uniform vec4 uForeGround;
|
||||||
|
uniform vec4 uBackGround;
|
||||||
|
uniform vec4 uBackDrop;
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
float Color = texture2D(uTex, vTexCoord).r;
|
||||||
|
gl_FragColor = mix(uBg, uFg, Color);
|
||||||
|
}
|
||||||
23
src/up.trans.frag
Normal file
23
src/up.trans.frag
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#version 100
|
||||||
|
|
||||||
|
precision mediump float;
|
||||||
|
|
||||||
|
varying vec2 vTexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D uTex;
|
||||||
|
uniform vec4 uForeGround;
|
||||||
|
uniform vec4 uBackGround;
|
||||||
|
uniform vec4 uBackDrop;
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
vec3 d = vec3(-1.0, 0.0, +1.0) / 256.0;
|
||||||
|
float Color = texture2D(uTex, vTexCoord).r;
|
||||||
|
float ColorL = texture2D(uTex, vTexCoord + d.xy).r;
|
||||||
|
float ColorR = texture2D(uTex, vTexCoord + dzy).r;
|
||||||
|
float ColorU = texture2D(uTex, vTexCoord + d.yx).r;
|
||||||
|
float ColorD = texture2D(uTex, vTexCoord + d.yz).r;
|
||||||
|
if (Color + ColorL + ColorR + ColorU + ColorD < 0.5) {
|
||||||
|
discard;
|
||||||
|
}
|
||||||
|
gl_FragColor = mix(uBg, uFg, Color);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user