Esempio n. 1
0
static inline void _handle_event(game *g, SDL_Event e) {
    if (e.type == SDL_QUIT) {
        g->state = ENDED;
    }
    if (e.type == SDL_KEYUP) {
        switch(e.key.keysym.sym) {
            // Fills
            case(SDLK_0):
            case(SDLK_1):
            case(SDLK_2):
            case(SDLK_3):
            case(SDLK_4):
            case(SDLK_5):
            case(SDLK_6):
            case(SDLK_7):
            case(SDLK_8):
            case(SDLK_9):
                fill(g->w, e.key.keysym.sym - SDLK_0);
                g->state = PAUSED;
                break;

            case(SDLK_r):
                fill(g->w, RANDOM);
                g->state = PAUSED;
                break;

            // Quit
            case(SDLK_ESCAPE):
            case(SDLK_q): g->state = ENDED; break;

            // Overlay
            case(SDLK_TAB): g->o.enabled = 0; break;

            // Toggle vsync
            case(SDLK_v):
                if (e.key.keysym.mod & (KMOD_CTRL|KMOD_CAPS)) {
                    char *clip_text = SDL_GetClipboardText();
                    size_t clip_len = SDL_strlen(clip_text);
                    for (; clip_len > 0; clip_len--) {
                        if (clip_text[clip_len-1] == '\n') {
                            continue;
                        }
                        break;
                    }
                    world *dec_w = deserialize_world_b64(clip_text, clip_len);
                    if (dec_w != NULL) {
                        printf("Decoded world! %ux%u\n", dec_w->xlim, dec_w->ylim);
                        free(g->w);
                        g->w = dec_w;
                        _init_world_display(g);
                        _world_vertices(g);
                        _setup_world(g);
                        _reset_camera(g);
                        _setup_camera(g);
                        _update_camera(g);
                    }
                    SDL_free(clip_text);
                } else {
                    g->vsync = !g->vsync;
                    SDL_GL_SetSwapInterval(g->vsync);
                }
                break;

            // End running
            case(SDLK_n): g->state = PAUSED; break;
            // Toggle running
            case(SDLK_SPACE):
                g->state = g->state == RUNNING ? PAUSED : RUNNING;
                break;
            // Toggle sub-state
            case(SDLK_h):
                if (g->w->state != CALC) {
                    world_half_step(g->w);
                }
                g->step = g->step == WHOLE ? HALF : WHOLE;
                break;
            // Change color scheme
            case(SDLK_c):
                if (e.key.keysym.mod & KMOD_SHIFT) {
                    _update_colors(g, g->color_scheme - 1);
                } else if (e.key.keysym.mod & (KMOD_CTRL|KMOD_CAPS)) {
                    size_t ser_size;
                    char *enc_world = serialize_world_b64(g->w, &ser_size);
                    printf("Copying to clipboard: %lu bytes\n", ser_size);
                    if (SDL_SetClipboardText(enc_world) != 0) {
                        printf("SDL_Error: %s\n", SDL_GetError());
                    }
                    free(enc_world);
                } else {
                    _update_colors(g, g->color_scheme + 1);
                }
                g->o.fps_upd = 1;
                break;
            // Switch projection type
            case(SDLK_o):
                g->d.ortho = !g->d.ortho;
                _setup_camera(g);
                _set_view(g);
                break;
            // Reset camera
            case(SDLK_u):
                _reset_camera(g);
                break;
            // Toggle padding
            case(SDLK_p):
                g->d.padding = !g->d.padding;
                _world_vertices(g);
                _setup_world(g);
                break;
            // Save file
            case(SDLK_x):
                if (g->filename != NULL) {
                    printf("Saving to file: %s\n", g->filename);
                    write_to_file(g->filename, g->w, BASE64);
                }
                break;
        }
    } else if (e.type == SDL_KEYDOWN) {
        switch(e.key.keysym.sym) {
            // Start running
            case(SDLK_n): g->state = RUNNING; break;
            // Single step
            case(SDLK_m):
                g->state = PAUSED;
                world_half_step(g->w);
                break;
            // Translate up
            case(SDLK_w):
            case(SDLK_UP):
                _move_camera(g, UP);
                break;
            // Translate left
            case(SDLK_a):
            case(SDLK_LEFT):
                _move_camera(g, LEFT);
                break;
            // Translate up
            case(SDLK_s):
            case(SDLK_DOWN):
                _move_camera(g, DOWN);
                break;
            // Translate right
            case(SDLK_d):
            case(SDLK_RIGHT):
                _move_camera(g, RIGHT);
                break;

            // Overlay
            case(SDLK_TAB): g->o.enabled = 1; break;
        }
    } else if (e.type == SDL_MOUSEBUTTONDOWN) {
        switch (e.button.button) {
            // Invert cell under cursor
            case(SDL_BUTTON_LEFT):
                g->state = PAUSED;
                _handle_mouse_click(g, e.button.x, e.button.y);
                break;
        }
    } else if (e.type == SDL_MOUSEWHEEL) {
        _calc_zoom(g, e.wheel.y > 0 ? 1 : -1);
    }
}
Esempio n. 2
0
void glfw_win_t::_draw_cb() {
	_set_view();
}
Esempio n. 3
0
static inline void _update_camera(game *g) {
    _set_view(g);
    _zoom_view(g);
    mat4x4_mul(g->d.mvp, g->d.proj, g->d.view);
}