test/snake.c

changeset 33
e7ddb52facd3
parent 32
86468a71dd73
child 36
e26b4ac1661c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/snake.c	Wed Mar 06 23:08:03 2024 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + * Copyright 2023 Mike Becker. All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions are met:
    1.10 + *
    1.11 + *   1. Redistributions of source code must retain the above copyright
    1.12 + *      notice, this list of conditions and the following disclaimer.
    1.13 + *
    1.14 + *   2. Redistributions in binary form must reproduce the above copyright
    1.15 + *      notice, this list of conditions and the following disclaimer in the
    1.16 + *      documentation and/or other materials provided with the distribution.
    1.17 + *
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.28 + * POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +
    1.31 +#include <ascension/ascension.h>
    1.32 +#include <cx/printf.h>
    1.33 +
    1.34 +static void update_fps_counter(AscSceneNode *node) {
    1.35 +    // addition and multiplication is more efficient testing for zero
    1.36 +    // at an unnoticible cost of imprecision
    1.37 +    static unsigned last_fps = 0u;
    1.38 +    static unsigned debounce = 999u;
    1.39 +    unsigned fps = 1000u;
    1.40 +    debounce += asc_context.elapsed_millis;
    1.41 +    if (debounce >= 1000u) {
    1.42 +        debounce = 0;
    1.43 +        fps /= asc_context.elapsed_millis;
    1.44 +        if (fps != last_fps) {
    1.45 +            last_fps = fps;
    1.46 +            snprintf(((AscText*)node)->text, 9, "%u FPS", fps);
    1.47 +            asc_node_update(node);
    1.48 +        }
    1.49 +    }
    1.50 +}
    1.51 +
    1.52 +static void create_fps_counter(void) {
    1.53 +    asc_set_font(asc_font(ASC_FONT_REGULAR, 24));
    1.54 +    asc_ink_rgb(255, 0, 0);
    1.55 +    AscText* text = asc_text(10, 10, "XXXXX FPS");
    1.56 +    asc_scene_add_behavior(asc_node(text), update_fps_counter);
    1.57 +}
    1.58 +
    1.59 +int main(int argc, char** argv) {
    1.60 +    asc_context_initialize();
    1.61 +    if (asc_has_error()) {
    1.62 +        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
    1.63 +                "Fatal Error",asc_get_error(),NULL);
    1.64 +        return 1;
    1.65 +    }
    1.66 +
    1.67 +    AscWindowSettings settings;
    1.68 +    asc_window_settings_init_defaults(&settings);
    1.69 +    settings.title = "Sandbox Application";
    1.70 +
    1.71 +    AscWindow *window = asc_window_initialize(0, &settings);
    1.72 +    asc_shader_initialize_predefined();
    1.73 +
    1.74 +    // create fps counter
    1.75 +    create_fps_counter();
    1.76 +
    1.77 +    // Main Loop
    1.78 +    do {
    1.79 +        // quit application on any error
    1.80 +        if (asc_has_error()) {
    1.81 +            SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
    1.82 +                    "Fatal Error", asc_get_error(), window->window);
    1.83 +            asc_clear_error();
    1.84 +            break;
    1.85 +        }
    1.86 +    } while (asc_loop_next());
    1.87 +
    1.88 +    asc_context_destroy();
    1.89 +    return 0;
    1.90 +}
    1.91 +

mercurial