1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-22 14:33:51 +00:00
8bitworkshop/presets/c64/scroll5.c

129 lines
2.9 KiB
C
Raw Normal View History

2020-02-02 18:17:31 +00:00
#include "common.h"
//#link "common.c"
#include "scrolling.h"
//#link "scrolling.c"
#include "sprites.h"
//#link "sprites.c"
2022-08-11 20:27:20 +00:00
#include <cbm_petscii_charmap.h>
2022-07-20 19:32:47 +00:00
static void draw_cell(word ofs, byte x, byte y) {
2020-02-02 18:17:31 +00:00
byte xx = x + origin_x;
byte yy = y + origin_y;
byte ch = xx ^ yy;
hidbuf[ofs] = ch; // character
2020-02-04 17:58:46 +00:00
colorbuf[ofs] = ch; // color
2020-02-02 18:17:31 +00:00
}
void scroll_draw_column(byte col) {
byte y;
2022-07-20 19:32:47 +00:00
word ofs = col;
2020-02-02 18:17:31 +00:00
for (y=0; y<ROWS; y++) {
2022-07-20 19:32:47 +00:00
draw_cell(ofs, col, y);
ofs += COLS;
2020-02-02 18:17:31 +00:00
}
}
void scroll_draw_row(byte row) {
byte x;
2022-07-20 19:32:47 +00:00
word ofs = row * COLS;
2020-02-02 18:17:31 +00:00
for (x=0; x<COLS; x++) {
2022-07-20 19:32:47 +00:00
draw_cell(ofs, x, row);
++ofs;
2020-02-02 18:17:31 +00:00
}
}
/*{w:24,h:21,bpp:1,brev:1}*/
const char SPRITE1[3*21] = {
0x00,0x7F,0x00,0x01,0xFF,0xC0,0x03,0xFF,0xE0,
0x03,0xE7,0xE0,0x07,0xD9,0xF0,0x07,0xDF,0xF0,
0x07,0xD9,0xF0,0x03,0xE7,0xE0,0x03,0xFF,0xE0,
0x03,0xFF,0xE0,0x02,0xFF,0xA0,0x01,0x7F,0x40,
0x01,0x3E,0x40,0x00,0x9C,0x80,0x00,0x9C,0x80,
0x00,0x49,0x00,0x00,0x49,0x00,0x00,0x3E,0x00,
0x00,0x3E,0x00,0x00,0x3E,0x00,0x00,0x1C,0x00
};
2022-07-20 19:32:47 +00:00
int playerx = 0;
int playery = 0;
int camerax = 0;
int cameray = 0;
void update_player() {
2022-08-09 14:47:55 +00:00
sprite_draw(0, playerx-camerax+160, playery-cameray+140, 192);
2022-07-20 19:32:47 +00:00
}
void camera_follow(byte moving) {
int dx, dy;
dx = camerax - playerx;
dy = cameray - playery;
if (moving && abs(dx) < 32 && abs(dy) < 32) return;
dx >>= 4;
dy >>= 4;
if (dx) {
if (dx > 8) dx = 8;
else if (dx < -8) dx = -8;
camerax -= dx;
}
if (dy) {
if (dy > 8) dy = 8;
else if (dy < -8) dy = -8;
cameray -= dy;
}
2022-08-25 20:52:04 +00:00
scroll_xy(dx, dy);
2022-07-20 19:32:47 +00:00
}
2020-02-02 18:17:31 +00:00
void main(void) {
clrscr();
printf("\r\n\r\n\r\n Hello World!");
2022-07-20 19:32:47 +00:00
printf("\r\n\r\n\r\n Use the joystick to move");
printf("\r\n\r\n\r\n And the camera will follow");
2020-02-02 18:17:31 +00:00
// setup scrolling library
scroll_setup();
2022-08-11 20:27:20 +00:00
VIC.bordercolor = 12;
2020-02-02 18:17:31 +00:00
// setup sprite library and copy sprite to VIC bank
sprite_clear();
2022-08-11 20:27:20 +00:00
sprite_set_shapes(SPRITE1, 192, 1);
2022-07-20 19:32:47 +00:00
sprshad.spr_color[0] = 13;
2020-02-02 18:17:31 +00:00
// install the joystick driver
joy_install (joy_static_stddrv);
2022-07-20 19:32:47 +00:00
2020-02-02 18:17:31 +00:00
// infinite loop
2022-07-20 19:32:47 +00:00
while (1) {
static char speed;
static char joy;
static bool slowframe = false;
2020-02-02 18:17:31 +00:00
// get joystick bits
2024-10-30 18:58:10 +00:00
joy = joy_read(1);
// speed up scrolling while button pressed
2022-07-20 19:32:47 +00:00
speed = JOY_BTN_1(joy) ? 3 : 1;
// if we copied screen memory last frame,
// double speed of player for this frame
if (slowframe) speed *= 2;
2020-02-02 18:17:31 +00:00
// move sprite based on arrow keys
2022-07-20 19:32:47 +00:00
if (JOY_LEFT(joy)) playerx -= speed;
if (JOY_RIGHT(joy)) playerx += speed;
if (JOY_UP(joy)) playery -= speed;
if (JOY_DOWN(joy)) playery += speed;
// move the camera?
camera_follow(joy);
slowframe = swap_needed;
2020-02-02 18:17:31 +00:00
// animate sprite in shadow sprite ram
2022-07-20 19:32:47 +00:00
update_player();
2022-08-09 19:39:27 +00:00
// wait for end of frame
2023-11-29 17:15:55 +00:00
wait_vblank();
2022-07-20 19:32:47 +00:00
// then update sprite registers
sprite_update(visbuf);
2020-02-02 18:17:31 +00:00
// update scroll registers
// and swap screens if we must
scroll_update();
}
}