Added mouse module (love.mouse), updated doc/files.md

This commit is contained in:
rxi
2016-09-24 10:41:12 +01:00
parent 9a72cddbc9
commit dd44e8d1a0
6 changed files with 162 additions and 4 deletions

View File

@@ -18,6 +18,8 @@ timer.c | `love.timer` module
graphics.c | `love.graphics` module
keyboard.h | `love.keyboard` prototype and typedefs
keyboard.c | `love.keyboard` module and keyboard interrupt handling
mouse.h | `love.mouse` prototype and typedefs
mouse.c | `love.mouse` module and mouse handling
image.h | `Image` object prototypes and typedefs
image.c | `Image` object
quad.h | `Quad` object prototypes and typedefs

View File

@@ -1,4 +1,4 @@
/**
/**
* Copyright (c) 2016 rxi
*
* This library is free software; you can redistribute it and/or modify it
@@ -40,7 +40,7 @@ void keyboard_handler() {
keyboard_events.data[keyboard_events.writei & 31].code = code;
keyboard_events.data[keyboard_events.writei & 31].type = 0;
keyboard_events.writei++;
} else {
/* Key down */
code |= extended;
@@ -82,7 +82,7 @@ int keyboard_init(void) {
void keyboard_deinit(void) {
_go32_dpmi_set_protected_mode_interrupt_vector(9, &old_keyb_handler_seginfo);
_go32_dpmi_set_protected_mode_interrupt_vector(9, &old_keyb_handler_seginfo);
_go32_dpmi_free_iret_wrapper(&new_keyb_handler_seginfo);
}

View File

@@ -1,4 +1,4 @@
/**
/**
* Copyright (c) 2016 rxi
*
* This library is free software; you can redistribute it and/or modify it
@@ -23,6 +23,7 @@ int luaopen_system(lua_State *L);
int luaopen_graphics(lua_State *L);
int luaopen_timer(lua_State *L);
int luaopen_keyboard(lua_State *L);
int luaopen_mouse(lua_State *L);
int luaopen_love(lua_State *L) {
int i;
@@ -53,6 +54,7 @@ int luaopen_love(lua_State *L) {
{ "graphics", luaopen_graphics },
{ "timer", luaopen_timer },
{ "keyboard", luaopen_keyboard },
{ "mouse", luaopen_mouse },
{ 0 },
};
for (i = 0; mods[i].name; i++) {

View File

@@ -15,6 +15,7 @@
#include "vga.h"
#include "luaobj.h"
#include "keyboard.h"
#include "mouse.h"
#include "image.h"
#include "palette.h"
@@ -46,6 +47,7 @@ int main(void) {
vga_init();
palette_init();
keyboard_init();
mouse_init();
/* Init lua */
L = luaL_newstate();
@@ -62,6 +64,8 @@ int main(void) {
"if love.load then love.load() end\n"
"love.timer.step()\n"
"while true do\n"
/* Update mouse and handle mouse events */
"love.mouse.update()\n"
/* Keyboard Events*/
"for _, e in ipairs(love.keyboard.getEvents()) do\n"
"if e.type == 'down' then\n"

136
src/mouse.c Normal file
View File

@@ -0,0 +1,136 @@
/**
* Copyright (c) 2016 rxi
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See LICENSE for details.
*/
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include "luaobj.h"
#include "mouse.h"
int mouse_inited;
int mouse_x, mouse_y;
int mouse_lastX, mouse_lastY;
int mouse_buttonsPressed[MOUSE_BUTTON_MAX];
int mouse_buttonsReleased[MOUSE_BUTTON_MAX];
int mouse_buttonsDown[MOUSE_BUTTON_MAX];
void mouse_init(void) {
union REGS regs = {};
regs.x.ax = 0;
int86(0x33, &regs, &regs);
mouse_inited = regs.x.ax ? 1 : 0;
}
void mouse_update(void) {
if (!mouse_inited) {
return;
}
/* Update last mouse position */
mouse_lastX = mouse_x;
mouse_lastY = mouse_y;
/* Update mouse position */
union REGS regs = {};
regs.x.ax = 3;
int86(0x33, &regs, &regs);
mouse_x = regs.x.cx >> 1;
mouse_y = regs.x.dx;
/* Update button states */
int i;
for (i = 0; i < MOUSE_BUTTON_MAX; i++) {
/* Reset pressed/released */
mouse_buttonsPressed[i] = 0;
mouse_buttonsReleased[i] = 0;
/* Handle mouse pressed */
memset(&regs, 0, sizeof(regs));
regs.x.ax = 5;
regs.x.bx = i;
int86(0x33, &regs, &regs);
if (regs.x.bx) {
mouse_buttonsPressed[i] = 1;
mouse_buttonsDown[i] = 1;
}
/* Handle mouse released */
memset(&regs, 0, sizeof(regs));
regs.x.ax = 6;
regs.x.bx = i;
int86(0x33, &regs, &regs);
if (regs.x.bx) {
mouse_buttonsReleased[i] = 1;
mouse_buttonsDown[i] = 0;
}
}
}
/**
* Lua binds
*/
int l_mouse_getPosition(lua_State *L) {
lua_pushinteger(L, mouse_x);
lua_pushinteger(L, mouse_y);
return 2;
}
int l_mouse_getX(lua_State *L) {
lua_pushinteger(L, mouse_x);
return 1;
}
int l_mouse_getY(lua_State *L) {
lua_pushinteger(L, mouse_y);
return 1;
}
int l_mouse_isDown(lua_State *L) {
int n = lua_gettop(L);
int res = 0;
int i;
for (i = 1; i <= n; i++) {
int idx = luaL_checkinteger(L, i) - 1;
if (idx >= 0 && idx < MOUSE_BUTTON_MAX) {
res |= mouse_buttonsDown[idx];
}
}
lua_pushboolean(L, res);
return 1;
}
int l_mouse_update(lua_State *L) {
mouse_update();
return 0;
}
int l_mouse_getEvents(lua_State *L) {
return 0;
}
int luaopen_mouse(lua_State *L) {
luaL_Reg reg[] = {
{ "update", l_mouse_update },
{ "getEvents", l_mouse_getEvents },
{ "getPosition", l_mouse_getPosition },
{ "getX", l_mouse_getX },
{ "getY", l_mouse_getY },
{ "isDown", l_mouse_isDown },
{ 0, 0 },
};
luaL_newlib(L, reg);
return 1;
}

14
src/mouse.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef MOUSE_H
#define MOUSE_H
enum {
MOUSE_BUTTON_LEFT,
MOUSE_BUTTON_RIGHT,
MOUSE_BUTTON_MIDDLE,
MOUSE_BUTTON_MAX,
};
void mouse_init(void);
void mouse_update(void);
#endif