From 4a7beacbdc66eb4c62e798231b56a3fdc8ce01bb Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 30 Apr 2018 20:53:07 -0400 Subject: [PATCH] add keyboard and mouse tests --- TEST/HELPERS.C | 28 ++++++++++++++++++++++ TEST/HELPERS.H | 3 +++ TEST/KBRD.C | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ TEST/KBRD.H | 7 ++++++ TEST/MAKEFILE | 3 +++ TEST/MOUSE.C | 48 ++++++++++++++++++++++++++++++++++++++ TEST/MOUSE.H | 7 ++++++ TEST/TEST.C | 18 +++++++++------ 8 files changed, 170 insertions(+), 7 deletions(-) create mode 100644 TEST/HELPERS.C create mode 100644 TEST/KBRD.C create mode 100644 TEST/KBRD.H create mode 100644 TEST/MOUSE.C create mode 100644 TEST/MOUSE.H diff --git a/TEST/HELPERS.C b/TEST/HELPERS.C new file mode 100644 index 0000000..b9ef569 --- /dev/null +++ b/TEST/HELPERS.C @@ -0,0 +1,28 @@ +#include "helpers.h" +#include "dgl.h" +#include +#include +#include + +void clrscr(unsigned char attr) { + union REGS r; + r.h.ah = 6; + r.h.al = 0; // 0 = clear + r.h.ch = 0; // y1 + r.h.cl = 0; // x1 + r.h.dh = 24; // y2 + r.h.dl = 79; // x2 + r.h.bh = attr; // attr (char + color) to fill with + int386(0x10, &r, &r); + gotoxy(0, 0); +} + +void gotoxy(int x, int y) { + union REGS r; + r.h.ah = 2; + r.h.bh = 0; + r.h.dh = y; + r.h.dl = x; + int386(0x10, &r, &r); +} + diff --git a/TEST/HELPERS.H b/TEST/HELPERS.H index 3e3f7b0..bc765b3 100644 --- a/TEST/HELPERS.H +++ b/TEST/HELPERS.H @@ -21,3 +21,6 @@ extern void _breakpoint(); #endif +void clrscr(unsigned char attr); +void gotoxy(int x, int y); + diff --git a/TEST/KBRD.C b/TEST/KBRD.C new file mode 100644 index 0000000..e6742cf --- /dev/null +++ b/TEST/KBRD.C @@ -0,0 +1,63 @@ +#include "kbrd.h" +#include "dgl.h" +#include +#include +#include "helpers.h" + +void display_key_states(void) { + int x, y; + + gotoxy(0, 2); + for (y = 0; y < 8; ++y) { + for (x = 0; x < 16; ++x) { + unsigned char pressed; + pressed = keys[y * 16 + x] > 0; + printf("%d", pressed); + } + printf("\n"); + } +} + +void test_keyboard(void) { + boolean result; + KEY k; + + clrscr(0); + + ASSERT(keyboard_is_initialized() == FALSE); + + result = keyboard_init(); + ASSERT(result == TRUE); + ASSERT(keyboard_is_initialized() == TRUE); + + printf("Keyboard state\n"); + display_key_states(); + + while (!keys[1]) { + display_key_states(); + } + + // delay to give enough time for key to be released before next ... + delay(500); + + gotoxy(0, 18); + printf("Press any key ...\n"); + + k = keyboard_read_key(); + printf("Pressed key %d\n", k); + + // same thing, giving enough time for key to be released ... + delay(500); + printf("Press that same key again ...\n"); + keyboard_wait_for_key(k); + + result = keyboard_shutdown(); + ASSERT(result == TRUE); + ASSERT(keyboard_is_initialized() == FALSE); + + gotoxy(0, 23); + printf("Press a key to continue...\n"); + + getch(); +} + diff --git a/TEST/KBRD.H b/TEST/KBRD.H new file mode 100644 index 0000000..669fd43 --- /dev/null +++ b/TEST/KBRD.H @@ -0,0 +1,7 @@ +#ifndef DGL_TEST_KBRD_H_INCLUDED +#define DGL_TEST_KBRD_H_INCLUDED + +void test_keyboard(void); + +#endif + diff --git a/TEST/MAKEFILE b/TEST/MAKEFILE index 682e55c..4862194 100644 --- a/TEST/MAKEFILE +++ b/TEST/MAKEFILE @@ -4,7 +4,10 @@ target_name = test object_files = blit.obj & fixed.obj & + helpers.obj & + kbrd.obj & line.obj & + mouse.obj & pal.obj & pcx.obj & pset.obj & diff --git a/TEST/MOUSE.C b/TEST/MOUSE.C new file mode 100644 index 0000000..412cf23 --- /dev/null +++ b/TEST/MOUSE.C @@ -0,0 +1,48 @@ +#include "mouse.h" +#include "dgl.h" +#include +#include +#include "helpers.h" + +void test_mouse(void) { + boolean result; + + clrscr(0); + + ASSERT(mouse_is_initialized() == FALSE); + + result = mouse_init(); + ASSERT(result == TRUE); + ASSERT(mouse_is_initialized() == TRUE); + + // this may seem a little weird to check for a mouse AFTER init, but + // it's the init call that determines if a mouse is present :) + if (mouse_is_present()) { + printf("Mouse detected. Showing mouse state.\n"); + + mouse_show(); + + while (!kbhit()) { + gotoxy(0, 2); + printf("X: %03d\n", mouse_x); + printf("Y: %03d\n", mouse_y); + printf("Buttons: %03d\n", mouse_buttons); + } + + mouse_hide(); + + } else { + printf("Mouse not detected. Skipping.\n"); + } + + // need to close down the mouse subsystem even if one is not present + result = mouse_shutdown(); + ASSERT(result == TRUE); + ASSERT(mouse_is_initialized() == FALSE); + + gotoxy(0, 23); + printf("Press a key to continue...\n"); + + getch(); +} + diff --git a/TEST/MOUSE.H b/TEST/MOUSE.H new file mode 100644 index 0000000..9eec163 --- /dev/null +++ b/TEST/MOUSE.H @@ -0,0 +1,7 @@ +#ifndef DGL_TEST_MOUSE_H_INCLUDED +#define DGL_TEST_MOUSE_H_INCLUDED + +void test_mouse(void); + +#endif + diff --git a/TEST/TEST.C b/TEST/TEST.C index ba833cc..e46cd04 100644 --- a/TEST/TEST.C +++ b/TEST/TEST.C @@ -3,16 +3,18 @@ #include "dgl.h" #include "helpers.h" -#include "fixed.h" -#include "vector2.h" -#include "surface.h" -#include "pset.h" -#include "line.h" -#include "rect.h" #include "blit.h" -#include "text.h" +#include "fixed.h" +#include "kbrd.h" +#include "line.h" +#include "mouse.h" #include "pal.h" #include "pcx.h" +#include "pset.h" +#include "rect.h" +#include "surface.h" +#include "text.h" +#include "vector2.h" SURFACE *backbuffer = NULL; @@ -39,6 +41,8 @@ int main(void) { ASSERT(backbuffer != NULL); ASSERT(backbuffer->width == 320 && backbuffer->height == 200); + test_keyboard(); + test_mouse(); test_fixed(); test_vector2i(); test_vector2f();