Beispielprogramme in C/Joystick Abfrage

Aus C64-Wiki
Zur Navigation springenZur Suche springen

C-Version des Beispielprogramms im Artikel Joystick

/*
  Compiler: cc65
*/
#include <stdint.h>
#include <string.h>

#include <c64.h>
#include <conio.h>
#include <peekpoke.h>

#define SPRITE_NUMBER   13
#define SPEED   2

const uint8_t SPRITE_DATA[] = {240, 224, 144, 8, 4, 2, 1};

enum Joystick
{ 
    oben = 1,
    unten = 2,
    links = 4,
    rechts = 8,
    feuer = 16
};

void init_sprite(void)
{
    uint8_t i;
    uint8_t *sprite_data = (uint8_t *)(SPRITE_NUMBER * 64);
    memset(sprite_data, 0, 63);
    for (i = 0; i < 7; ++i) sprite_data[i * 3] = SPRITE_DATA[i];
    POKE(2040, SPRITE_NUMBER);
    VIC.spr_ena = 0x01;
    VIC.spr0_color = COLOR_YELLOW;
    VIC.bgcolor0 = COLOR_BLACK;
    VIC.spr_exp_x = 0x00;
}

void main(void)
{
    uint8_t x, y;

    x = y = 150;
    CIA1.ddra = 0xe0;

    init_sprite();
    clrscr();
    textcolor(COLOR_YELLOW);

    while ((CIA1.pra & feuer) != 0)
    {
        if ((CIA1.pra & oben) == 0) y -= SPEED;
        if ((CIA1.pra & unten) == 0) y += SPEED;
        if ((CIA1.pra & links) == 0) x -= SPEED;
        if ((CIA1.pra & rechts) == 0) x += SPEED;

        if (x > 251) x = 24;
        if (x < 24) x = 250;

        if (y < 50) y = 250;
        if (y > 250) y = 50;

        gotoxy(0, 0);
        cprintf("X-Pos: %3d Y-Pos: %3d", x, y);
        VIC.spr0_x = x;
        VIC.spr0_y = y;
    }

    CIA1.ddra = 0xff;
}

Eine Variante die den Joystick-Treiber von cc65 verwendet:

/*
  Compiler: cc65
*/
#include <stdint.h>
#include <string.h>

#include <c64.h>
#include <conio.h>
#include <joystick.h>
#include <peekpoke.h>

#define SPRITE_NUMBER   13
#define SPEED   2
#define JOY_NUMBER  1

const uint8_t SPRITE_DATA[] = {240, 224, 144, 8, 4, 2, 1};

void init_sprite(void)
{
    uint8_t i;
    uint8_t *sprite_data = (uint8_t *)(SPRITE_NUMBER * 64);
    memset(sprite_data, 0, 63);
    for (i = 0; i < 7; ++i) sprite_data[i * 3] = SPRITE_DATA[i];
    POKE(2040, SPRITE_NUMBER);
    VIC.spr_ena = 0x01;
    VIC.spr0_color = COLOR_YELLOW;
    VIC.bgcolor0 = COLOR_BLACK;
    VIC.spr_exp_x = 0x00;
}

void main(void)
{
    uint8_t x, y, joy_state;

    if (joy_install(joy_static_stddrv) != JOY_ERR_OK) {
        cputs("Can't init joystick!");
        return;
    }

    init_sprite();
    clrscr();
    textcolor(COLOR_YELLOW);
    x = y = 150;
    while (! JOY_BTN_1(joy_state = joy_read(JOY_NUMBER)))
    {
        if (JOY_UP(joy_state)) y -= SPEED;
        if (JOY_DOWN(joy_state)) y += SPEED;
        if (JOY_LEFT(joy_state)) x -= SPEED;
        if (JOY_RIGHT(joy_state)) x += SPEED;

        if (x > 251) x = 24;
        if (x < 24) x = 250;

        if (y < 50) y = 250;
        if (y > 250) y = 50;

        gotoxy(0, 0);
        cprintf("X-Pos: %3d Y-Pos: %3d", x, y);
        VIC.spr0_x = x;
        VIC.spr0_y = y;
    }
    joy_uninstall();
}