Implement mouse support
This commit is contained in:
parent
7aeb636392
commit
2e9d1be053
1
Makefile
1
Makefile
|
@ -6,6 +6,7 @@ CFILES = src/main.c \
|
||||||
src/bus.c \
|
src/bus.c \
|
||||||
src/cpu.c \
|
src/cpu.c \
|
||||||
src/framebuffer.c \
|
src/framebuffer.c \
|
||||||
|
src/mouse.c \
|
||||||
src/screen.c
|
src/screen.c
|
||||||
|
|
||||||
$(TARGET): $(CFILES)
|
$(TARGET): $(CFILES)
|
||||||
|
|
45
src/bus.c
45
src/bus.c
|
@ -11,14 +11,16 @@
|
||||||
#include "bus.h"
|
#include "bus.h"
|
||||||
#include "cpu.h"
|
#include "cpu.h"
|
||||||
#include "framebuffer.h"
|
#include "framebuffer.h"
|
||||||
|
#include "mouse.h"
|
||||||
|
|
||||||
extern fox32_vm_t vm;
|
extern fox32_vm_t vm;
|
||||||
|
extern mouse_t mouse;
|
||||||
|
|
||||||
int bus_io_read(void *user, uint32_t *value, uint32_t port) {
|
int bus_io_read(void *user, uint32_t *value, uint32_t port) {
|
||||||
(void) user;
|
(void) user;
|
||||||
switch (port) {
|
switch (port) {
|
||||||
case 0x80000000 ... 0x8000031F: { // overlay port
|
case 0x80000000 ... 0x8000031F: { // overlay port
|
||||||
uint8_t overlay_number = (port & 0x000000FF);
|
uint8_t overlay_number = port & 0x000000FF;
|
||||||
uint8_t setting = (port & 0x0000FF00) >> 8;
|
uint8_t setting = (port & 0x0000FF00) >> 8;
|
||||||
switch (setting) {
|
switch (setting) {
|
||||||
case 0x00: {
|
case 0x00: {
|
||||||
|
@ -49,6 +51,26 @@ int bus_io_read(void *user, uint32_t *value, uint32_t port) {
|
||||||
|
|
||||||
break;
|
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;
|
return 0;
|
||||||
|
@ -97,6 +119,27 @@ int bus_io_write(void *user, uint32_t value, uint32_t port) {
|
||||||
|
|
||||||
break;
|
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;
|
return 0;
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include "bus.h"
|
#include "bus.h"
|
||||||
#include "cpu.h"
|
#include "cpu.h"
|
||||||
#include "framebuffer.h"
|
#include "framebuffer.h"
|
||||||
|
#include "mouse.h"
|
||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
|
|
||||||
#include "../fox32rom.h"
|
#include "../fox32rom.h"
|
||||||
|
@ -46,9 +47,9 @@ int main(int argc, char *argv[]) {
|
||||||
draw_framebuffer,
|
draw_framebuffer,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
0,
|
mouse_pressed,
|
||||||
0,
|
mouse_released,
|
||||||
0
|
mouse_moved
|
||||||
);
|
);
|
||||||
|
|
||||||
ScreenInit();
|
ScreenInit();
|
||||||
|
|
36
src/mouse.c
Normal file
36
src/mouse.c
Normal 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
12
src/mouse.h
Normal 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);
|
10
src/screen.c
10
src/screen.c
|
@ -111,7 +111,7 @@ int ScreenProcessEvents() {
|
||||||
case SDL_MOUSEMOTION: {
|
case SDL_MOUSEMOTION: {
|
||||||
if (ScreenMouseGrabbed) {
|
if (ScreenMouseGrabbed) {
|
||||||
if (MainScreen.MouseMoved)
|
if (MainScreen.MouseMoved)
|
||||||
MainScreen.MouseMoved(&MainScreen, event.motion.xrel, event.motion.yrel);
|
MainScreen.MouseMoved(event.motion.xrel, event.motion.yrel);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -127,14 +127,14 @@ int ScreenProcessEvents() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MainScreen.MousePressed)
|
if (MainScreen.MousePressed)
|
||||||
MainScreen.MousePressed(&MainScreen, event.button.button);
|
MainScreen.MousePressed(event.button.button);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
case SDL_MOUSEBUTTONUP: {
|
case SDL_MOUSEBUTTONUP: {
|
||||||
if (MainScreen.MouseReleased)
|
if (MainScreen.MouseReleased)
|
||||||
MainScreen.MouseReleased(&MainScreen, event.button.button);
|
MainScreen.MouseReleased(event.button.button);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,12 +149,12 @@ int ScreenProcessEvents() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MainScreen.KeyPressed)
|
if (MainScreen.KeyPressed)
|
||||||
MainScreen.KeyPressed(&MainScreen, event.key.keysym.scancode);
|
MainScreen.KeyPressed(event.key.keysym.scancode);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_KEYUP:
|
case SDL_KEYUP:
|
||||||
if (MainScreen.KeyReleased)
|
if (MainScreen.KeyReleased)
|
||||||
MainScreen.KeyReleased(&MainScreen, event.key.keysym.scancode);
|
MainScreen.KeyReleased(event.key.keysym.scancode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/screen.h
10
src/screen.h
|
@ -5,11 +5,11 @@
|
||||||
struct Screen;
|
struct Screen;
|
||||||
|
|
||||||
typedef void (*ScreenDrawF)(struct Screen *screen);
|
typedef void (*ScreenDrawF)(struct Screen *screen);
|
||||||
typedef void (*ScreenKeyPressedF)(struct Screen *screen, int sdlscancode);
|
typedef void (*ScreenKeyPressedF)(int sdl_scancode);
|
||||||
typedef void (*ScreenKeyReleasedF)(struct Screen *screen, int sdlscancode);
|
typedef void (*ScreenKeyReleasedF)(int sdl_scancode);
|
||||||
typedef void (*ScreenMousePressedF)(struct Screen *screen, int button);
|
typedef void (*ScreenMousePressedF)(int button);
|
||||||
typedef void (*ScreenMouseReleasedF)(struct Screen *screen, int button);
|
typedef void (*ScreenMouseReleasedF)(int button);
|
||||||
typedef void (*ScreenMouseMovedF)(struct Screen *screen, int dx, int dy);
|
typedef void (*ScreenMouseMovedF)(int dx, int dy);
|
||||||
|
|
||||||
struct Screen {
|
struct Screen {
|
||||||
int Width;
|
int Width;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user