foxygit / fake-08-xbox360 Log in
main xbox360 commits tags

/platform/xex/tests/video/main.cpp · 3.75 KB

raw
// Runtime check 3: CPU-drawn frames through the title's own GPU ring
// (xex_video) and controller input (xex_input). Moving gradient, a square
// moved with the d-pad/left stick, A/B/X/Y change its colour, a bar shows
// the frame rate. Back+Start returns to the dashboard.
#include <cstdio>
#include <cstdint>
#include <cstdarg>

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

// Diagnostics: every trace line is appended to game:/video-log.txt (the
// file is closed after each line so it survives a hang).
static void logLine(const char *fmt, ...) {
    char line[256];
    va_list ap;
    va_start(ap, fmt);
    vsnprintf(line, sizeof line, fmt, ap);
    va_end(ap);
    printf("video: %s\n", line);
    if (FILE *f = fopen("game:/video-log.txt", "a")) {
        fprintf(f, "%s\n", line);
        fclose(f);
    }
}

int main(int, char **) {
    remove("game:/video-log.txt");
    logLine("start");
    xex_video_trace = logLine;
    if (xex_video_init() != 0) {
        logLine("init failed: %s", xex_video_error());
        xex_notify("video init failed");
        xex_sleep_ms(4000);
        return 1;
    }

    int sx = 200, sy = 200;
    uint32_t colour = xex_video_rgb(255, 0, 77);
    uint64_t freq = xex_timebase_freq();
    uint64_t last = xex_timebase(), second = last;
    int frames = 0, fps = 0;
    unsigned t = 0;

    logLine("init ok");
    uint64_t startTime = xex_timebase();
    int seconds = 0;
    for (;;) {
        xex_pad pad;
        int connected = xex_input_get(0, &pad);
        // stop by itself after 20 s, so a black screen still tells whether the loop runs
        if (xex_timebase() - startTime > 20 * freq) {
            logLine("20 s elapsed, exiting");
            break;
        }
        if ((pad.buttons & XEX_PAD_BACK) && (pad.buttons & XEX_PAD_START)) break;

        int speed = 6;
        if ((pad.buttons & XEX_PAD_LEFT) || pad.lx < -12000) sx -= speed;
        if ((pad.buttons & XEX_PAD_RIGHT) || pad.lx > 12000) sx += speed;
        if ((pad.buttons & XEX_PAD_UP) || pad.ly > 12000) sy -= speed;
        if ((pad.buttons & XEX_PAD_DOWN) || pad.ly < -12000) sy += speed;
        if (pad.buttons & XEX_PAD_A) colour = xex_video_rgb(0, 228, 54);
        if (pad.buttons & XEX_PAD_B) colour = xex_video_rgb(255, 0, 77);
        if (pad.buttons & XEX_PAD_X) colour = xex_video_rgb(41, 173, 255);
        if (pad.buttons & XEX_PAD_Y) colour = xex_video_rgb(255, 236, 39);

        xex_frame f = xex_video_begin();
        if (sx < 0) sx = 0;
        if (sy < 0) sy = 0;
        if (sx > f.window_width - 128) sx = f.window_width - 128;
        if (sy > f.window_height - 128) sy = f.window_height - 128;

        for (int y = 0; y < f.window_height; y++) {
            int py = f.window_y + y;
            for (int x = 0; x < f.window_width; x++) {
                uint32_t c;
                if (x >= sx && x < sx + 128 && y >= sy && y < sy + 128) {
                    c = colour;
                } else if (y < 24 && x < fps * 8) {
                    c = xex_video_rgb(255, 255, 255);
                } else {
                    c = xex_video_rgb((uint8_t)(x + t), (uint8_t)(y + t / 2), 96);
                }
                f.pixels[xex_video_tiled_index(f.window_x + x, py, f.pitch)] = c;
            }
        }
        xex_video_present();
        t += 2;

        frames++;
        uint64_t now = xex_timebase();
        if (now - second >= freq) {
            fps = frames;
            logLine("second %d: %d fps, %dx%d window at %d,%d, pad %s buttons %04x",
                    ++seconds, fps, f.window_width, f.window_height, f.window_x, f.window_y,
                    connected == 0 ? "ok" : "none", pad.buttons);
            frames = 0;
            second = now;
        }
        last = now;
    }
    (void)last;
    logLine("exit");
    return 0;
}