code:
#ifdef USE_OPENGL

/*
Description of Ken's filter to improve LZW compression of DXT1 format by ~15%: (as tested with the HRP)

 To increase LZW patterns, I store each field of the DXT block structure separately.
 Here are the 3 DXT fields:
 1.  __int64 alpha_4x4; //DXT3 only (16 byte structure size when included)
 2.  short rgb0, rgb1;
 3.  int32_t index_4x4;

 Each field is then stored with its own specialized algorithm.
 1. I haven't done much testing with this field - I just copy it raw without any transform for now.

 2. For rgb0 and rgb1, I use a "green" filter like this:
 g = g;
 r = r-g;
 b = b-g;
 For grayscale, this makes the stream: x,0,0,x,0,0,x,0,0,... instead of x,x,x,x,x,x,x,x,...
 Q:what was the significance of choosing green? A:largest/most dominant component
 Believe it or not, this gave 1% better compression :P
 I tried subtracting each componenet with the previous pixel, but strangely it hurt compression.
 Oh, the joy of trial & error. :)

 3. For index_4x4, I transform the ordering of 2-bit indices in the DXT blocks from this:
 0123 0123 0123  ---- ---- ----
 4567 4567 4567  ---- ---- ----
 89ab 89ab 89ab  ---- ---- ----
 cdef cdef cdef  ---- ---- ----
 To this: (I swap x & y axes)
 048c 048c 048c  |||| |||| ||||
 159d 159d 159d  |||| |||| ||||
 26ae 26ae 26ae  |||| |||| ||||
 37bf 37bf 37bf  |||| |||| ||||
 The trick is: going from the bottom of the 4th line to the top of the 5th line
 is the exact same jump (geometrically) as from 5th to 6th, etc.. This is not true in the top case.
 These silly tricks will increase patterns and therefore make LZW compress better.
 I think this improved compression by a few % :)
 */

#include "compat.h"
#include "build.h"
#include "texcache.h"
#include "lz4.h"

#include <fcntl.h>
#ifdef _WIN32
# include <io.h>
#else
# include <unistd.h>
#endif

#ifndef EDUKE32_GLES
static uint16_t dxt_hicosub(uint16_t c)
{
    int32_t r, g, b;
    g = ((c>> 5)&63);
    r = ((c>>11)-(g>>1))&31;
    b = ((c>> 0)-(g>>1))&31;
    return ((r<<11)+(g<<5)+b);
}

static uint16_t dedxt_hicoadd(uint16_t c)
{
    int32_t r, g, b;
    g = ((c>> 5)&63);
    r = ((c>>11)+(g>>1))&31;
    b = ((c>> 0)+(g>>1))&31;
    return ((r<<11)+(g<<5)+b);
}
#endif

void dxt_handle_io(int32_t len, void *midbuf, char *packbuf)
{
    void *writebuf;
    int32_t j, cleng;

    if (glusetexcache == 2)
    {
        cleng = LZ4_compress_default((const char*)midbuf, packbuf, len, len);

        if (cleng <= 0 || cleng > len-1)
        {
            cleng = len;
            writebuf = midbuf;
        }
        else writebuf = packbuf;
    }
    else
    {
        cleng = len;
        writebuf = midbuf;
    }

    // native -> external (little endian)
    j = B_LITTLE32(cleng);
    buildvfs_fwrite(&j, sizeof(j), 1, texcache.dataFilePtr);
    buildvfs_fwrite(writebuf, cleng, 1, texcache.dataFilePtr);
}

int32_t dedxt_handle_io(int32_t j /* TODO: better name */,
                               void *midbuf, int32_t mbufsiz, char *packbuf, int32_t ispacked)
{
    void *inbuf;
    int32_t cleng;

    if (texcache_readdata(&cleng, sizeof(int32_t)))
        return -1;

    // external (little endian) -> native
    cleng = B_LITTLE32(cleng);

    inbuf = (ispacked && cleng < j) ? packbuf : midbuf;

    if (texcache.rw_mmap.is_mapped() && texcache.rw_mmap.mapped_length() >= texcache.dataFilePos + cleng)
    {
        if (ispacked && cleng < j)
        {
            if (LZ4_decompress_safe(texcache.rw_mmap.data() + texcache.dataFilePos, (char*)midbuf, cleng, mbufsiz) <= 0)
            {
                texcache.dataFilePos += cleng;
                return -1;
            }
        }
        else Bmemcpy(inbuf, texcache.rw_mmap.data() + texcache.dataFilePos, cleng);

        texcache.dataFilePos += cleng;
    }
    else
    {
        buildvfs_fseek_abs(texcache.dataFilePtr, texcache.dataFilePos);
        texcache.dataFilePos += cleng;

        if (buildvfs_fread(inbuf, cleng, 1, texcache.dataFilePtr) != 1)
            return -1;

        if (ispacked && cleng < j)
            if (LZ4_decompress_safe(packbuf, (char*)midbuf, cleng, mbufsiz) <= 0)
                return -1;
    }

    return 0;
}

#ifndef EDUKE32_GLES
// NOTE: <pict> members are in external (little) endianness.
int32_t dxtfilter(const texcachepicture *pict, const char *pic, void *midbuf, char *packbuf, uint32_t miplen)
{
    uint32_t j, k, offs, stride;
    char *cptr;

    if ((pict->format == (signed) B_LITTLE32(GL_COMPRESSED_RGB_S3TC_DXT1_EXT)) ||
            (pict->format == (signed) B_LITTLE32(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT))) { offs = 0; stride = 8; }
    else if ((pict->format == (signed) B_LITTLE32(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT)) ||
             (pict->format == (signed) B_LITTLE32(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT))) { offs = 8; stride = 16; }
    else
        { offs = 0; stride = 8; }

    if (stride == 16) //If DXT3...
    {
        //alpha_4x4
        cptr = (char *)midbuf;
        for (k=0; k<8; k++) *cptr++ = pic[k];
        for (j=stride; (unsigned)j<miplen; j+=stride)
            for (k=0; k<8; k++) *cptr++ = pic[j+k];

        dxt_handle_io(tabledivide32(miplen, stride)<<3, midbuf, packbuf);
    }

    //rgb0,rgb1
    cptr = (char *)midbuf;
    for (k=0; k<=2; k+=2)
        for (j=0; (unsigned)j<miplen; j+=stride)
            { B_BUF16(cptr, dxt_hicosub(B_UNBUF16(&pic[offs+j+k]))); cptr += 2; }

    dxt_handle_io(tabledivide32(miplen, stride)<<2, midbuf, packbuf);

    //index_4x4
    cptr = (char *)midbuf;
    for (j=0; (unsigned)j<miplen; j+=stride)
    {
        const char *c2 = &pic[j+offs+4];
        cptr[0] = ((c2[0]>>0)&3) + (((c2[1]>>0)&3)<<2) + (((c2[2]>>0)&3)<<4) + (((c2[3]>>0)&3)<<6);
        cptr[1] = ((c2[0]>>2)&3) + (((c2[1]>>2)&3)<<2) + (((c2[2]>>2)&3)<<4) + (((c2[3]>>2)&3)<<6);
        cptr[2] = ((c2[0]>>4)&3) + (((c2[1]>>4)&3)<<2) + (((c2[2]>>4)&3)<<4) + (((c2[3]>>4)&3)<<6);
        cptr[3] = ((c2[0]>>6)&3) + (((c2[1]>>6)&3)<<2) + (((c2[2]>>6)&3)<<4) + (((c2[3]>>6)&3)<<6);
        cptr += 4;
    }

    dxt_handle_io(tabledivide32(miplen, stride)<<2, midbuf, packbuf);

    return 0;
}

// NOTE: <pict> members are in native endianness.
int32_t dedxtfilter(const texcachepicture *pict, char *pic, void *midbuf, char *packbuf, int32_t ispacked)
{
    int32_t j, k, offs, stride;
    char *cptr;

    if ((pict->format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT) ||
            (pict->format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT)) { offs = 0; stride = 8; }
    else if ((pict->format == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT) ||
             (pict->format == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)) { offs = 8; stride = 16; }
    else
        { offs = 0; stride = 8; }

    if (stride == 16) //If DXT3...
    {
        //alpha_4x4
        if (dedxt_handle_io(tabledivide32(pict->size, stride)*8, midbuf, pict->size, packbuf, ispacked))
            return -1;

        cptr = (char *)midbuf;
        for (k=0; k<8; k++) pic[k] = *cptr++;
        for (j=stride; j<pict->size; j+=stride)
            for (k=0; k<8; k++) pic[j+k] = (*cptr++);
    }

    //rgb0,rgb1
    if (dedxt_handle_io(tabledivide32(pict->size, stride)*4, midbuf, pict->size, packbuf, ispacked))
        return -1;

    cptr = (char *)midbuf;
    for (k=0; k<=2; k+=2)
    {
        for (j=0; j<pict->size; j+=stride)
        {
            B_BUF16(&pic[offs+j+k], dedxt_hicoadd(B_UNBUF16(cptr)));
            cptr += 2;
        }
    }

    //index_4x4:
    if (dedxt_handle_io(tabledivide32(pict->size, stride)*4, midbuf, pict->size, packbuf, ispacked))
        return -1;

    cptr = (char *)midbuf;
    for (j=0; j<pict->size; j+=stride)
    {
        pic[j+offs+4] = ((cptr[0]>>0)&3) + (((cptr[1]>>0)&3)<<2) + (((cptr[2]>>0)&3)<<4) + (((cptr[3]>>0)&3)<<6);
        pic[j+offs+5] = ((cptr[0]>>2)&3) + (((cptr[1]>>2)&3)<<2) + (((cptr[2]>>2)&3)<<4) + (((cptr[3]>>2)&3)<<6);
        pic[j+offs+6] = ((cptr[0]>>4)&3) + (((cptr[1]>>4)&3)<<2) + (((cptr[2]>>4)&3)<<4) + (((cptr[3]>>4)&3)<<6);
        pic[j+offs+7] = ((cptr[0]>>6)&3) + (((cptr[1]>>6)&3)<<2) + (((cptr[2]>>6)&3)<<4) + (((cptr[3]>>6)&3)<<6);
        cptr += 4;
    }

    return 0;
}
#endif

#endif

repo_name: orochi/nblood

license: mit


code:
#include "compat.h"
#include <dlfcn.h>

#define dynamicgtkfoo__
#include "dynamicgtk.h"

static void *handle = NULL;
struct _dynamicgtksyms dynamicgtksyms;

#define GETDLSYM(x) { \
	dynamicgtksyms.x = (x ## _ptr) dlsym(handle, (failsym = (const char *)#x)); \
	if (!dynamicgtksyms.x ) { err = 1; break; } \
}

int32_t dynamicgtk_init(void)
{
    int32_t err = 0;
    const char *failsym = NULL;
    const char *gtk_shared_object;

    if (handle) return 1;

#if defined __OpenBSD__
    gtk_shared_object = "libgtk-x11-2.0.so";
#else
    gtk_shared_object = "libgtk-x11-2.0.so.0";
#endif

    handle = dlopen(gtk_shared_object, RTLD_NOW|RTLD_GLOBAL);
    if (!handle) return -1;

    memset(&dynamicgtksyms, 0, sizeof(dynamicgtksyms));

    do
    {
        GETDLSYM(g_free)
        GETDLSYM(g_object_get_data)
        GETDLSYM(g_object_set_data)
        GETDLSYM(g_object_set_data_full)
        GETDLSYM(g_object_unref)
        GETDLSYM(g_signal_connect_data)
        GETDLSYM(g_signal_handlers_block_matched)
        GETDLSYM(g_signal_handlers_unblock_matched)
        GETDLSYM(g_type_check_instance_cast)
        GETDLSYM(gdk_pixbuf_from_pixdata)
        GETDLSYM(gdk_pixbuf_new_from_data)
        GETDLSYM(gtk_accel_group_new)
        GETDLSYM(gtk_alignment_new)
        GETDLSYM(gtk_box_get_type)
        GETDLSYM(gtk_box_pack_start)
        GETDLSYM(gtk_button_box_get_type)
        GETDLSYM(gtk_button_box_set_layout)
        GETDLSYM(gtk_button_new)
        GETDLSYM(gtk_cell_layout_get_type)
        GETDLSYM(gtk_cell_layout_pack_start)
        GETDLSYM(gtk_cell_layout_set_attributes)
        GETDLSYM(gtk_cell_renderer_text_new)
        GETDLSYM(gtk_check_button_new_with_mnemonic)
        GETDLSYM(gtk_combo_box_get_active)
        GETDLSYM(gtk_combo_box_get_active_iter)
        GETDLSYM(gtk_combo_box_get_active_text)
        GETDLSYM(gtk_combo_box_get_model)
        GETDLSYM(gtk_combo_box_get_type)
        GETDLSYM(gtk_combo_box_new_text)
        GETDLSYM(gtk_combo_box_new_with_model)
        GETDLSYM(gtk_combo_box_set_active)
        GETDLSYM(gtk_combo_box_set_active_iter)
        GETDLSYM(gtk_container_add)
        GETDLSYM(gtk_container_foreach)
        GETDLSYM(gtk_container_get_type)
        GETDLSYM(gtk_container_set_border_width)
        GETDLSYM(gtk_dialog_get_type)
        GETDLSYM(gtk_dialog_run)
        GETDLSYM(gtk_fixed_get_type)
        GETDLSYM(gtk_fixed_new)
        GETDLSYM(gtk_fixed_put)
        GETDLSYM(gtk_hbox_new)
        GETDLSYM(gtk_hbutton_box_new)
        GETDLSYM(gtk_image_new_from_pixbuf)
        GETDLSYM(gtk_image_new_from_stock)
        GETDLSYM(gtk_init_check)
        GETDLSYM(gtk_label_get_type)
        GETDLSYM(gtk_label_new)
        GETDLSYM(gtk_label_new_with_mnemonic)
        GETDLSYM(gtk_label_set_mnemonic_widget)
        GETDLSYM(gtk_list_store_append)
        GETDLSYM(gtk_list_store_clear)
        GETDLSYM(gtk_list_store_get_type)
        GETDLSYM(gtk_list_store_new)
        GETDLSYM(gtk_list_store_set)
        GETDLSYM(gtk_events_pending)
        GETDLSYM(gtk_main)
        GETDLSYM(gtk_main_iteration_do)
        GETDLSYM(gtk_main_quit)
        GETDLSYM(gtk_message_dialog_new)
        GETDLSYM(gtk_misc_get_type)
        GETDLSYM(gtk_misc_set_alignment)
        GETDLSYM(gtk_notebook_get_nth_page)
        GETDLSYM(gtk_notebook_get_type)
        GETDLSYM(gtk_notebook_new)
        GETDLSYM(gtk_notebook_set_current_page)
        GETDLSYM(gtk_notebook_set_tab_label)
        GETDLSYM(gtk_object_get_type)
        GETDLSYM(gtk_scrolled_window_get_type)
        GETDLSYM(gtk_scrolled_window_new)
        GETDLSYM(gtk_scrolled_window_set_policy)
        GETDLSYM(gtk_scrolled_window_set_shadow_type)
        GETDLSYM(gtk_table_get_type)
        GETDLSYM(gtk_table_new)
        GETDLSYM(gtk_table_attach)
        GETDLSYM(gtk_text_buffer_backspace)
        GETDLSYM(gtk_text_buffer_create_mark)
        GETDLSYM(gtk_text_buffer_delete_mark)
        GETDLSYM(gtk_text_buffer_get_end_iter)
        GETDLSYM(gtk_text_buffer_insert)
        // FIXME: should I put a #if !GTK_CHECK_VERSION(2,6,0)
        // around these three, or should I not care?
        GETDLSYM(gtk_text_iter_backward_cursor_position)
        GETDLSYM(gtk_text_iter_equal)
        GETDLSYM(gtk_text_buffer_delete_interactive)
        //
        GETDLSYM(gtk_text_view_get_buffer)
        GETDLSYM(gtk_text_view_get_type)
        GETDLSYM(gtk_text_view_new)
        GETDLSYM(gtk_text_view_scroll_to_mark)
        GETDLSYM(gtk_text_view_set_cursor_visible)
        GETDLSYM(gtk_text_view_set_editable)
        GETDLSYM(gtk_text_view_set_left_margin)
        GETDLSYM(gtk_text_view_set_right_margin)
        GETDLSYM(gtk_text_view_set_wrap_mode)
        GETDLSYM(gtk_toggle_button_get_active)
        GETDLSYM(gtk_toggle_button_get_type)
        GETDLSYM(gtk_toggle_button_set_active)
        GETDLSYM(gtk_tree_model_get)
        GETDLSYM(gtk_tree_model_get_iter)
        GETDLSYM(gtk_tree_model_get_path)
        GETDLSYM(gtk_tree_model_get_type)
        GETDLSYM(gtk_tree_path_get_indices)
        GETDLSYM(gtk_tree_path_new_from_indices)
        GETDLSYM(gtk_tree_selection_get_selected)
        GETDLSYM(gtk_tree_selection_select_iter)
        GETDLSYM(gtk_tree_selection_set_mode)
        GETDLSYM(gtk_tree_sortable_get_type)
        GETDLSYM(gtk_tree_sortable_set_sort_column_id)
        GETDLSYM(gtk_tree_sortable_set_sort_func)
        GETDLSYM(gtk_tree_view_append_column)
        GETDLSYM(gtk_tree_view_column_new_with_attributes)
        GETDLSYM(gtk_tree_view_column_set_expand)
        GETDLSYM(gtk_tree_view_column_set_min_width)
        GETDLSYM(gtk_tree_view_get_model)
        GETDLSYM(gtk_tree_view_get_selection)
        GETDLSYM(gtk_tree_view_get_type)
        GETDLSYM(gtk_tree_view_new_with_model)
        GETDLSYM(gtk_tree_view_set_enable_search)
        GETDLSYM(gtk_tree_view_set_headers_visible)
        GETDLSYM(gtk_vbox_new)
        GETDLSYM(gtk_widget_add_accelerator)
        GETDLSYM(gtk_widget_destroy)
        GETDLSYM(gtk_widget_ref)
        GETDLSYM(gtk_widget_set_sensitive)
        GETDLSYM(gtk_widget_set_size_request)
        GETDLSYM(gtk_widget_show_all)
        GETDLSYM(gtk_widget_show)
        GETDLSYM(gtk_widget_unref)
        GETDLSYM(gtk_window_add_accel_group)
        GETDLSYM(gtk_window_get_type)
        GETDLSYM(gtk_window_new)
        GETDLSYM(gtk_window_set_default_icon)
        GETDLSYM(gtk_window_set_position)
        GETDLSYM(gtk_window_set_resizable)
        GETDLSYM(gtk_window_set_title)
        GETDLSYM(gtk_window_set_type_hint)
        GETDLSYM(gtk_window_set_default)
        GETDLSYM(g_utf8_collate)
    }
    while (0);

    if (err)
    {
        //printf("Failed fetching symbol %s from GTK lib\n", failsym);
        dlclose(handle);
        handle = NULL;
        return -1;
    }
    return 0;
}

void dynamicgtk_uninit(void)
{
    if (handle) dlclose(handle);
    handle = NULL;
    memset(&dynamicgtksyms, 0, sizeof(dynamicgtksyms));
}


code:
#include "compat.h" // for UNREFERENCED_PARAMETER

#define ENET_IMPLEMENTATION
#include "enet.h"