foxygit / xexkit Log in
commits tags

/examples/minimal/main.cpp · 2.36 KB

raw
// The smallest useful title: a picture at the output's resolution, a square
// moved with the left stick, a beep on A, and the Guide over it. Leave with
// the Guide's Xbox Home (or Back+Start).
#include <cmath>
#include <cstdio>

#include "xex_runtime.h"
#include "xex_video.h"
#include "xex_input.h"
#include "xex_audio.h"

int main(int, char **) {
    if (xex_video_init() != 0) {
        xex_notify(xex_video_error());
        xex_sleep_ms(3000);
        return 1;
    }
    xex_audio_open();

    float x = 200, y = 200;
    int beep = 0;            // samples of beep left
    double phase = 0;

    for (;;) {
        xex_pad pad;
        xex_input_get(0, &pad);
        // never end by yourself while the Guide shows (see the wiki)
        if ((pad.buttons & XEX_PAD_BACK) && (pad.buttons & XEX_PAD_START) && !xex_video_guide_visible())
            break;
        x += pad.lx / 4096.0f;
        y -= pad.ly / 4096.0f;
        if (pad.buttons & XEX_PAD_A) beep = 4800;

        // keep ~4 audio frames (21 ms) queued; fill more often for less latency
        while (xex_audio_queued() < 4) {
            float *frame = xex_audio_next();
            if (!frame) break;
            for (int i = 0; i < XEX_AUDIO_FRAME_SAMPLES; i++) {
                float v = 0;
                if (beep > 0) {
                    v = 0.3f * (float)sin(phase);
                    phase += 2 * M_PI * 440 / XEX_AUDIO_RATE;
                    beep--;
                }
                frame[i] = v;                              // front left
                frame[XEX_AUDIO_FRAME_SAMPLES + i] = v;    // front right
            }
            xex_audio_queue();
        }

        xex_frame f = xex_video_begin();   // waits until the previous flip is on screen
        if (x < 0) x = 0;
        if (y < 0) y = 0;
        if (x > f.window_width - 64) x = f.window_width - 64;
        if (y > f.window_height - 64) y = f.window_height - 64;
        for (int py = 0; py < f.window_height; py++)
            for (int px = 0; px < f.window_width; px++) {
                bool in = px >= x && px < x + 64 && py >= y && py < y + 64;
                uint32_t c = in ? xex_video_rgb(255, 0, 77) : xex_video_rgb(29, 43, 83);
                f.pixels[xex_video_tiled_index(f.window_x + px, f.window_y + py, f.pitch)] = c;
            }
        xex_video_present();               // flips at the next vertical blank
    }
    return 0;
}