Skip to content

Commit

Permalink
Integrating mouse support
Browse files Browse the repository at this point in the history
  • Loading branch information
ifilot committed May 26, 2024
1 parent 10652b2 commit 0c29339
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 12 deletions.
Binary file modified assets/tiles/tiles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/tiles/tiles.pyxel
Binary file not shown.
1 change: 1 addition & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#define TILE_EMPTY_CURSOR 0x02
#define TILE_BLACK 0x03
#define TILE_WHITE 0x04
#define TILE_MOUSE_CURSOR 0x0F
#define TILE_BLACK_CURSOR 0x13
#define TILE_WHITE_CURSOR 0x14

Expand Down
28 changes: 27 additions & 1 deletion src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ void computer_turn() {
uint8_t bestx[100];
uint8_t bestvalue = 0;
uint8_t i=0, j=0, k=0;
unsigned char keycode = 0;
clock_t start, next;

// keep track of time
Expand Down Expand Up @@ -476,6 +475,10 @@ void human_turn() {
unsigned char joystat1 = 0xFF;
unsigned char joystat2 = 0xFF;
unsigned char joyconn = 0xFF;
static uint8_t mouse_buttons = 0x00;
uint16_t *mouse_x = (uint16_t *)0x2;
uint16_t *mouse_y = (uint16_t *)0x4;
int8_t ccurx, ccury;

// first check whether the human can actually perform a valid move
// if not, we have to swap the turn again (and display an error message)
Expand Down Expand Up @@ -552,6 +555,29 @@ void human_turn() {
return;
}
}

// read mouse
asm("ldx #2");
asm("jsr $FF6B");
asm("sta %v", mouse_buttons);

// determine tile position based based on cursor position
ccurx = (*mouse_x >> 4) - board_offset_x;
ccury = (*mouse_y >> 4) - board_offset_y;
if(ccurx >= 0 && ccurx < boardsize && ccury >=0 && ccury < boardsize) {
set_cursor(ccury, ccurx);
}

if(mouse_buttons & 1) {
// wait until mouse button is released
while(mouse_buttons != 0x00) {
asm("ldx #2");
asm("jsr $FF6B");
asm("sta %v", mouse_buttons);
}
// place stone in current mouse location
place_stone(cury, curx, PROBE_NO, current_player);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "constants.h"
#include "video.h"
#include "sound.h"
#include "mouse.h"

extern uint8_t *board; // store board configuration
extern uint8_t *edgefield; // which board positions are 'active'
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void main() {

// enable mouse
init_mouse();
set_mouse_pointer(TILE_MOUSE_CURSOR);

while(1) {
while(gamestate == GAME_MENU) {
Expand Down
4 changes: 0 additions & 4 deletions src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ void game_menu() {
static unsigned char keycode;
unsigned short *mouse_x = (unsigned short *)0x2;
unsigned short *mouse_y = (unsigned short *)0x4;
static unsigned char mouse_buttons;

write_string("CX16-OTHELLO", 0, 1);
write_string("music by Crisps", 1, 5);
Expand Down Expand Up @@ -115,9 +114,6 @@ void game_menu() {
return;
}

// asm("jsr $FF6B");
// asm("sta %v", mouse_buttons);

// update sound buffer
sound_fill_buffers();
update_background_diagonal();
Expand Down
4 changes: 2 additions & 2 deletions src/mouse.s
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

.code
.proc _init_mouse: near
sec
jsr X16::Kernal::SCREEN_MODE
lda #1
ldx #40
ldy #30
jsr X16::Kernal::MOUSE_CONFIG
rts
.endproc
34 changes: 29 additions & 5 deletions src/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,40 @@ void set_sprite(uint8_t sprite_id, uint8_t posy, uint8_t posx) {
*
*/
void reset_sprites() {
uint8_t i,j;
uint8_t i;
unsigned long sprite_addr = 0x1FC00;

VERA.address = sprite_addr;
VERA.address_hi = sprite_addr >> 16;
VERA.address_hi |= 0b10000;

for(i=1; i<128; i++) {
for(j=0; j<8; j++) {
VERA.data0 = 0x00;
}
for(i=0; i<128; i++) {
VERA.data0 = 0x00;
VERA.data0 = 0x00;
VERA.data0 = 16;
VERA.data0 = 0x00;
VERA.data0 = 16;
VERA.data0 = 0x00;
VERA.data0 = 0b00001100;
VERA.data0 = 0b01010000;
}

set_mouse_pointer(TILE_MOUSE_CURSOR);
}

/**
* @brief Set the mouse pointer
*
* @param tile_id which tile to use
*/
void set_mouse_pointer(uint8_t tile_id) {
unsigned long sprite_addr = 0x1FC00;
uint32_t graph_addr = TILEBASE + (tile_id << 8);

VERA.address = sprite_addr;
VERA.address_hi = sprite_addr >> 16;
VERA.address_hi |= 0b10000;

VERA.data0 = graph_addr >> 5;
VERA.data0 = (graph_addr >> 13) | (1 << 7);
}
7 changes: 7 additions & 0 deletions src/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,11 @@ void set_sprite(uint8_t sprite_id, uint8_t posy, uint8_t posx);
*/
void reset_sprites();

/**
* @brief Set the mouse pointer
*
* @param tile_id which tile to use
*/
void set_mouse_pointer(uint8_t tile_id);

#endif // _VIDEO_H

0 comments on commit 0c29339

Please sign in to comment.