Implement mouse support

This commit is contained in:
Ry 2022-10-23 17:18:05 -07:00
parent 7aeb636392
commit 2e9d1be053
7 changed files with 107 additions and 14 deletions

View File

@ -6,6 +6,7 @@ CFILES = src/main.c \
src/bus.c \
src/cpu.c \
src/framebuffer.c \
src/mouse.c \
src/screen.c
$(TARGET): $(CFILES)

View File

@ -11,14 +11,16 @@
#include "bus.h"
#include "cpu.h"
#include "framebuffer.h"
#include "mouse.h"
extern fox32_vm_t vm;
extern mouse_t mouse;
int bus_io_read(void *user, uint32_t *value, uint32_t port) {
(void) user;
switch (port) {
case 0x80000000 ... 0x8000031F: { // overlay port
uint8_t overlay_number = (port & 0x000000FF);
uint8_t overlay_number = port & 0x000000FF;
uint8_t setting = (port & 0x0000FF00) >> 8;
switch (setting) {
case 0x00: {
@ -49,6 +51,26 @@ int bus_io_read(void *user, uint32_t *value, uint32_t port) {
break;
};
case 0x80000400 ... 0x80000401: { // mouse port
uint8_t setting = port & 0x000000FF;
switch (setting) {
case 0x00: {
// button states
if (mouse.clicked) *value |= 0b001;
if (mouse.released) *value |= 0b010;
if (mouse.held) *value |= 0b100; else *value &= !0b100;
break;
};
case 0x01: {
// position
*value = (mouse.y << 16) | mouse.x;
break;
};
}
break;
};
}
return 0;
@ -97,6 +119,27 @@ int bus_io_write(void *user, uint32_t value, uint32_t port) {
break;
};
case 0x80000400 ... 0x80000401: { // mouse port
uint8_t setting = port & 0x000000FF;
switch (setting) {
case 0x00: {
// button states
mouse.clicked = value & 0b001;
mouse.released = value & 0b010;
mouse.held = value & 0b100;
break;
};
case 0x01: {
// position
mouse.x = value & 0x0000FFFF;
mouse.y = (value & 0xFFFF0000) >> 16;
break;
};
}
break;
};
}
return 0;

View File

@ -10,6 +10,7 @@
#include "bus.h"
#include "cpu.h"
#include "framebuffer.h"
#include "mouse.h"
#include "screen.h"
#include "../fox32rom.h"
@ -46,9 +47,9 @@ int main(int argc, char *argv[]) {
draw_framebuffer,
0,
0,
0,
0,
0
mouse_pressed,
mouse_released,
mouse_moved
);
ScreenInit();

36
src/mouse.c Normal file
View File

@ -0,0 +1,36 @@
#include <SDL2/SDL.h>
#include <getopt.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mouse.h"
#include "screen.h"
mouse_t mouse;
void mouse_moved(int dx, int dy) {
mouse.x += dx;
mouse.y += dy;
if (mouse.x > 0x8000) mouse.x = 0;
if (mouse.x > 640) mouse.x = 640;
if (mouse.y > 0x8000) mouse.y = 0;
if (mouse.y > 480) mouse.y = 480;
}
void mouse_pressed(int button) {
(void) button; // TODO: check which button was clicked
mouse.clicked = true;
mouse.held = true;
}
void mouse_released(int button) {
(void) button; // TODO: check which button was released
mouse.released = true;
mouse.held = false;
}

12
src/mouse.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
typedef struct {
uint16_t x, y;
bool clicked;
bool released;
bool held;
} mouse_t;
void mouse_moved(int dx, int dy);
void mouse_pressed(int button);
void mouse_released(int button);

View File

@ -111,7 +111,7 @@ int ScreenProcessEvents() {
case SDL_MOUSEMOTION: {
if (ScreenMouseGrabbed) {
if (MainScreen.MouseMoved)
MainScreen.MouseMoved(&MainScreen, event.motion.xrel, event.motion.yrel);
MainScreen.MouseMoved(event.motion.xrel, event.motion.yrel);
}
break;
}
@ -127,14 +127,14 @@ int ScreenProcessEvents() {
}
if (MainScreen.MousePressed)
MainScreen.MousePressed(&MainScreen, event.button.button);
MainScreen.MousePressed(event.button.button);
break;
}
case SDL_MOUSEBUTTONUP: {
if (MainScreen.MouseReleased)
MainScreen.MouseReleased(&MainScreen, event.button.button);
MainScreen.MouseReleased(event.button.button);
break;
}
@ -149,12 +149,12 @@ int ScreenProcessEvents() {
}
if (MainScreen.KeyPressed)
MainScreen.KeyPressed(&MainScreen, event.key.keysym.scancode);
MainScreen.KeyPressed(event.key.keysym.scancode);
break;
case SDL_KEYUP:
if (MainScreen.KeyReleased)
MainScreen.KeyReleased(&MainScreen, event.key.keysym.scancode);
MainScreen.KeyReleased(event.key.keysym.scancode);
break;
}
}

View File

@ -5,11 +5,11 @@
struct Screen;
typedef void (*ScreenDrawF)(struct Screen *screen);
typedef void (*ScreenKeyPressedF)(struct Screen *screen, int sdlscancode);
typedef void (*ScreenKeyReleasedF)(struct Screen *screen, int sdlscancode);
typedef void (*ScreenMousePressedF)(struct Screen *screen, int button);
typedef void (*ScreenMouseReleasedF)(struct Screen *screen, int button);
typedef void (*ScreenMouseMovedF)(struct Screen *screen, int dx, int dy);
typedef void (*ScreenKeyPressedF)(int sdl_scancode);
typedef void (*ScreenKeyReleasedF)(int sdl_scancode);
typedef void (*ScreenMousePressedF)(int button);
typedef void (*ScreenMouseReleasedF)(int button);
typedef void (*ScreenMouseMovedF)(int dx, int dy);
struct Screen {
int Width;