<commit_before>#include "window.h"
#include "vector.h"
#include "log.h"
#include <assert.h>
#include <stdlib.h>

#include <SDL/SDL.h>

unsigned char g_tick = 0;

typedef struct {
    machine_t* machine;
    viewer_t* viewer;
    char* assets_path;
    SDL_Surface* bg;
    SDL_Surface* display;
    vector_t* sprites;
    vector_t* imgs;
    unsigned char send_input;
    vector_t* input_mapping;
    // TODO other sdl stuff
} window_data_t;

typedef struct {
    char const* name;
    unsigned keysym;
    unsigned inputbit;
} input_mapping_pair_t;
static int _input_mapping_pair_by_keysym(void const* a, void const* b)
{
    input_mapping_pair_t** left = (input_mapping_pair_t**)a;
    input_mapping_pair_t** right = (input_mapping_pair_t**)b;
    jaklog(DEBUG, JAK_STR, "bykeysym comparing: ");
    jaklog(DEBUG, JAK_NUM, &(*left)->keysym);
    jaklog(DEBUG, JAK_STR, " and ");
    jaklog(DEBUG, JAK_NUM|JAK_LN, &(*right)->keysym);
    return (*left)->keysym - (*right)->keysym;
}
static int _input_mapping_pair_by_name(void const* a, void const* b)
{
    input_mapping_pair_t** left = (input_mapping_pair_t**)a;
    input_mapping_pair_t** right = (input_mapping_pair_t**)b;
    return strcmp((*left)->name, (*right)->name);
}
static int _input_mapping_pair_find_keysym(void const* key, void const* b)
{
    unsigned* left = (unsigned*)key;
    input_mapping_pair_t** right = (input_mapping_pair_t**)b;
    jaklog(DEBUG, JAK_STR, "comparing: ");
    jaklog(DEBUG, JAK_NUM, key);
    jaklog(DEBUG, JAK_STR, " and ");
    jaklog(DEBUG, JAK_NUM|JAK_LN, &(*right)->keysym);
    return *left - (*right)->keysym;
}
static input_mapping_pair_t _window_key_mapping[] = {
    { "left", SDLK_LEFT, LEFT },
    { "right", SDLK_RIGHT, RIGHT },
    { "up", SDLK_UP, UP },
    { "down", SDLK_DOWN, DOWN },
    { "fire", SDLK_1, FIRE },
    { "alt", SDLK_2, ALT },
    { "start", SDLK_9, START },
    { "toggle", SDLK_0, TOGGLE },
    { "upleft", SDLK_q, UPLEFT },
    { "upright", SDLK_p, UPRIGHT },
    { "downleft", SDLK_a, DOWNLEFT },
    { "downright", SDLK_l, DOWNRIGHT }
};
static void _window_init_input_mapping(vector_t** this)
{
    size_t i = 0;
    input_mapping_pair_t* p = &_window_key_mapping[0];
    *this = new_vector_of(12);
    for(; i < 12; ++i, ++p) {
        (*this)->set(*this, i, p);
    }
    (*this)->sort(*this, _input_mapping_pair_by_keysym);
}

typedef struct {
    unsigned x, y;
    SDL_Surface* img;
} sdl_sprite_t;

typedef struct {
    char const* name;
    SDL_Surface* img;
} sdl_charimg_t;
int sdl_charimg_sortf(void const* a, void const* b) {
    sdl_charimg_t** left = (sdl_charimg_t**)a;
    sdl_charimg_t** right = (sdl_charimg_t**)b;
    return strcmp((*left)->name, (*right)->name);
}
int sdl_charimg_findf(void const* a, void const* b) {
    char* left = (char*)a;
    sdl_charimg_t** right = (sdl_charimg_t**)b;
    return strcmp(left, (*right)->name);
}

static sdl_sprite_t* new_sdl_sprite(unsigned x, unsigned y, SDL_Surface* img)
{
    sdl_sprite_t* ret = (sdl_sprite_t*)malloc(sizeof(sdl_sprite_t));
    ret->x = x;
    ret->y = y;
    ret->img = img;
    return ret;
}

static void delete_sdl_sprite(sdl_sprite_t** this)
{
    if(!*this) return;
    free(*this);
    *this = NULL;
}

static void _window_viewer_impl_nudge(struct viewer_s* this, void* source)
{
    window_t* window = (window_t*)this->_data;
    machine_t* machine = (machine_t*)source;
    window_data_t* data = (window_data_t*)window->_data;
    assert(machine == data->machine);
    window->redraw(window);
}

static viewer_t* new_window_viewer_impl(window_t* parent)
{
    viewer_t* ret = (viewer_t*)malloc(sizeof(viewer_t));
    ret->_data = parent;
    ret->nudge = &_window_viewer_impl_nudge;
    return ret;
}

static void _window_clear_sprites(struct window_s* this)
{
    assert(this);
    window_data_t* data = (window_data_t*)this->_data;
    assert(data);
    size_t len = data->sprites->size(data->sprites);
    size_t i = 0;
    for(; i < len; ++i) {
        sdl_sprite_t* sprite = (sdl_sprite_t*)data->sprites->get(data->sprites, i);
        delete_sdl_sprite(&sprite);
    }
    delete_vector(&data->sprites);
}

static void _window_clear_imgs(struct window_s* this)
{
    assert(this);
    window_data_t* data = (window_data_t*)this->_data;
    assert(data);
    size_t l = data->imgs->size(data->imgs);
    size_t i = 0;
    for(; i < l; ++i) {
        sdl_charimg_t* s = (sdl_charimg_t*)data->imgs->get(data->imgs, i);
        SDL_FreeSurface(s->img);
    }
    delete_vector(&data->imgs);
}

static unsigned _window_init(struct window_s* this, char const* path, unsigned char send_input)
{
    assert(this);
    window_data_t* data = (window_data_t*)this->_data;
    assert(data);

    data->send_input = send_input;

    // init SDL
    if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK|SDL_INIT_TIMER) < 0) {
        return WINDOW_ERR_SDL_INIT;
    }

    // get game.assets path
    if(data->assets_path) free(data->assets_path);
    /* nothing */
    {
        size_t pathlen = strlen(path);
        char const* p = path + pathlen - 1;
        if(p < path) return WINDOW_ERR_INVALID_PATH;
        while(p >= path && *p != '.' && *p != '/') --p;
        if(p >= path && *p == '.') {
            // .assets/
            size_t l = p - path + 8;
            data->assets_path = (char*)malloc(sizeof(char) * l);
            strncpy(data->assets_path, path, p - path);
            strcpy(data->assets_path + (p - path), ".assets/");
            data->assets_path[l] = '\0';
        } else if((p >= path && *p == '/') || p < path) {
            size_t l = pathlen + 8;
            data->assets_path = (char*)malloc(sizeof(char) * l);
            strcpy(data->assets_path, path);
            strcpy(data->assets_path + l, ".assets/");
            data->assets_path[l] = '\0';
        } else {
            return WINDOW_ERR_INVALID_PATH;
        }<commit_msg>fix small bug<commit_after>#include "window.h"
#include "vector.h"
#include "log.h"
#include <assert.h>
#include <stdlib.h>

#include <SDL/SDL.h>

unsigned char g_tick = 0;

typedef struct {
    machine_t* machine;
    viewer_t* viewer;
    char* assets_path;
    SDL_Surface* bg;
    SDL_Surface* display;
    vector_t* sprites;
    vector_t* imgs;
    unsigned char send_input;
    vector_t* input_mapping;
    // TODO other sdl stuff
} window_data_t;

typedef struct {
    char const* name;
    unsigned keysym;
    unsigned inputbit;
} input_mapping_pair_t;
static int _input_mapping_pair_by_keysym(void const* a, void const* b)
{
    input_mapping_pair_t** left = (input_mapping_pair_t**)a;
    input_mapping_pair_t** right = (input_mapping_pair_t**)b;
    jaklog(DEBUG, JAK_STR, "bykeysym comparing: ");
    jaklog(DEBUG, JAK_NUM, &(*left)->keysym);
    jaklog(DEBUG, JAK_STR, " and ");
    jaklog(DEBUG, JAK_NUM|JAK_LN, &(*right)->keysym);
    return (*left)->keysym - (*right)->keysym;
}
static int _input_mapping_pair_by_name(void const* a, void const* b)
{
    input_mapping_pair_t** left = (input_mapping_pair_t**)a;
    input_mapping_pair_t** right = (input_mapping_pair_t**)b;
    return strcmp((*left)->name, (*right)->name);
}
static int _input_mapping_pair_find_keysym(void const* key, void const* b)
{
    unsigned* left = (unsigned*)key;
    input_mapping_pair_t** right = (input_mapping_pair_t**)b;
    jaklog(DEBUG, JAK_STR, "comparing: ");
    jaklog(DEBUG, JAK_NUM, key);
    jaklog(DEBUG, JAK_STR, " and ");
    jaklog(DEBUG, JAK_NUM|JAK_LN, &(*right)->keysym);
    return *left - (*right)->keysym;
}
static input_mapping_pair_t _window_key_mapping[] = {
    { "left", SDLK_LEFT, LEFT },
    { "right", SDLK_RIGHT, RIGHT },
    { "up", SDLK_UP, UP },
    { "down", SDLK_DOWN, DOWN },
    { "fire", SDLK_1, FIRE },
    { "alt", SDLK_2, ALT },
    { "start", SDLK_9, START },
    { "toggle", SDLK_0, TOGGLE },
    { "upleft", SDLK_q, UPLEFT },
    { "upright", SDLK_p, UPRIGHT },
    { "downleft", SDLK_a, DOWNLEFT },
    { "downright", SDLK_l, DOWNRIGHT }
};
static void _window_init_input_mapping(vector_t** this)
{
    size_t i = 0;
    input_mapping_pair_t* p = &_window_key_mapping[0];
    *this = new_vector_of(12);
    for(; i < 12; ++i, ++p) {
        (*this)->set(*this, i, p);
    }
    (*this)->sort(*this, _input_mapping_pair_by_keysym);
}

typedef struct {
    unsigned x, y;
    SDL_Surface* img;
} sdl_sprite_t;

typedef struct {
    char const* name;
    SDL_Surface* img;
} sdl_charimg_t;
int sdl_charimg_sortf(void const* a, void const* b) {
    sdl_charimg_t** left = (sdl_charimg_t**)a;
    sdl_charimg_t** right = (sdl_charimg_t**)b;
    return strcmp((*left)->name, (*right)->name);
}
int sdl_charimg_findf(void const* a, void const* b) {
    char* left = (char*)a;
    sdl_charimg_t** right = (sdl_charimg_t**)b;
    return strcmp(left, (*right)->name);
}

static sdl_sprite_t* new_sdl_sprite(unsigned x, unsigned y, SDL_Surface* img)
{
    sdl_sprite_t* ret = (sdl_sprite_t*)malloc(sizeof(sdl_sprite_t));
    ret->x = x;
    ret->y = y;
    ret->img = img;
    return ret;
}

static void delete_sdl_sprite(sdl_sprite_t** this)
{
    if(!*this) return;
    free(*this);
    *this = NULL;
}

static void _window_viewer_impl_nudge(struct viewer_s* this, void* source)
{
    window_t* window = (window_t*)this->_data;
    machine_t* machine = (machine_t*)source;
    window_data_t* data = (window_data_t*)window->_data;
    assert(machine == data->machine);
    window->redraw(window);
}

static viewer_t* new_window_viewer_impl(window_t* parent)
{
    viewer_t* ret = (viewer_t*)malloc(sizeof(viewer_t));
    ret->_data = parent;
    ret->nudge = &_window_viewer_impl_nudge;
    return ret;
}

static void _window_clear_sprites(struct window_s* this)
{
    assert(this);
    window_data_t* data = (window_data_t*)this->_data;
    assert(data);
    size_t len = data->sprites->size(data->sprites);
    size_t i = 0;
    for(; i < len; ++i) {
        sdl_sprite_t* sprite = (sdl_sprite_t*)data->sprites->get(data->sprites, i);
        delete_sdl_sprite(&sprite);
    }
    delete_vector(&data->sprites);
}

static void _window_clear_imgs(struct window_s* this)
{
    assert(this);
    window_data_t* data = (window_data_t*)this->_data;
    assert(data);
    size_t l = data->imgs->size(data->imgs);
    size_t i = 0;
    for(; i < l; ++i) {
        sdl_charimg_t* s = (sdl_charimg_t*)data->imgs->get(data->imgs, i);
        SDL_FreeSurface(s->img);
    }
    delete_vector(&data->imgs);
}

static unsigned _window_init(struct window_s* this, char const* path, unsigned char send_input)
{
    assert(this);
    window_data_t* data = (window_data_t*)this->_data;
    assert(data);

    data->send_input = send_input;

    // init SDL
    if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK|SDL_INIT_TIMER) < 0) {
        return WINDOW_ERR_SDL_INIT;
    }

    // get game.assets path
    if(data->assets_path) free(data->assets_path);
    /* nothing */
    {
        size_t pathlen = strlen(path);
        char const* p = path + pathlen - 1;
        if(p < path) return WINDOW_ERR_INVALID_PATH;
        while(p >= path && *p != '.' && *p != '/') --p;
        if(p >= path && *p == '.') {
            // .assets/
            size_t l = p - path + 8;
            data->assets_path = (char*)malloc(sizeof(char) * l);
            strncpy(data->assets_path, path, p - path);
            strcpy(data->assets_path + (p - path), ".assets/");
            data->assets_path[l] = '\0';
        } else if((p >= path && *p == '/' && (*p + 1) != '\0') || p < path) {
            size_t l = pathlen + 8;
            data->assets_path = (char*)malloc(sizeof(char) * l);
            strcpy(data->assets_path, path);
            strcpy(data->assets_path + l, ".assets/");
            data->assets_path[l] = '\0';
        } else {
            return WINDOW_ERR_INVALID_PATH;
        }
{'fraction_non_alphanumeric': 0.15672782874617738, 'fraction_numerical': 0.0033978933061501867, 'mean_word_length': 3.26249094858798, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4272325', 'n_tokens_mistral': 4588, 'n_tokens_neox': 4449, 'n_words': 889}