From e2d33a53eb1dacc32c8efd383d27b5ec2b9dfd91 Mon Sep 17 00:00:00 2001 From: jean-marcharvengt Date: Sat, 10 Oct 2020 11:25:28 +0200 Subject: [PATCH] add usb keyboard support to doom --- MCUME_teensy41/teensydoom/emuapi.cpp | 9 +- MCUME_teensy41/teensydoom/emuapi.h | 2 +- MCUME_teensy41/teensydoom/i_scale.c | 1452 ------------------- MCUME_teensy41/teensydoom/i_scale.h | 53 - MCUME_teensy41/teensydoom/i_video.c | 21 +- MCUME_teensy41/teensydoom/platform_config.h | 2 +- MCUME_teensy41/teensydoom/r_main.c | 10 +- MCUME_teensy41/teensydoom/r_state.h | 4 +- MCUME_teensy41/teensydoom/teensydoom.ino | 26 +- 9 files changed, 60 insertions(+), 1519 deletions(-) delete mode 100644 MCUME_teensy41/teensydoom/i_scale.c delete mode 100644 MCUME_teensy41/teensydoom/i_scale.h diff --git a/MCUME_teensy41/teensydoom/emuapi.cpp b/MCUME_teensy41/teensydoom/emuapi.cpp index 402bd16..78bbcb4 100644 --- a/MCUME_teensy41/teensydoom/emuapi.cpp +++ b/MCUME_teensy41/teensydoom/emuapi.cpp @@ -1153,15 +1153,14 @@ FRESULT f_read (FIL* fp, void* buff, unsigned int btr, unsigned int * br) int nr = fds[i].f.read(buff, btr); //emu_printf("fread"); //emu_printi(btr); - emu_printi(nr); + //emu_printi(nr); *br = nr; if (nr <= 0) return(FR_DISK_ERR); else return(FR_OK); } - //else { - //} + unsigned char buffer[256]; @@ -1185,8 +1184,8 @@ FRESULT f_read (FIL* fp, void* buff, unsigned int btr, unsigned int * br) } } *br = byteread; - emu_printi(byteread); - if (byteread <= 0) + //emu_printi(byteread); + if (byteread <= 0) return(FR_DISK_ERR); else return(FR_OK); diff --git a/MCUME_teensy41/teensydoom/emuapi.h b/MCUME_teensy41/teensydoom/emuapi.h index b57dda6..00d5336 100644 --- a/MCUME_teensy41/teensydoom/emuapi.h +++ b/MCUME_teensy41/teensydoom/emuapi.h @@ -3,7 +3,7 @@ #include "platform_config.h" -#define CUSTOM_SND 1 +//#define CUSTOM_SND 1 //#define TIMER_REND 1 #define EXTRA_HEAP 0x10 diff --git a/MCUME_teensy41/teensydoom/i_scale.c b/MCUME_teensy41/teensydoom/i_scale.c deleted file mode 100644 index f88c694..0000000 --- a/MCUME_teensy41/teensydoom/i_scale.c +++ /dev/null @@ -1,1452 +0,0 @@ -// -// Copyright(C) 1993-1996 Id Software, Inc. -// Copyright(C) 2005-2014 Simon Howard -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// DESCRIPTION: -// Screen scale-up code: -// 1x,2x,3x,4x pixel doubling -// Aspect ratio-correcting stretch functions -// - -#include -#include -#include - -#include "doomtype.h" - -#include "i_video.h" -#include "m_argv.h" -#include "z_zone.h" - -#if defined(_MSC_VER) && !defined(__cplusplus) -#define inline __inline -#endif - -// Should be I_VideoBuffer - -static byte *src_buffer; - -// Destination buffer, ie. screen->pixels. - -static byte *dest_buffer; - -// Pitch of destination buffer, ie. screen->pitch. - -static int dest_pitch; - -// Lookup tables used for aspect ratio correction stretching code. -// stretch_tables[0] : 20% / 80% -// stretch_tables[1] : 40% / 60% -// All other combinations can be reached from these two tables. - -static byte *stretch_tables[2] = { NULL, NULL }; - -// 50%/50% stretch table, for 800x600 squash mode - -static byte *half_stretch_table = NULL; - -// Called to set the source and destination buffers before doing the -// scale. - -void I_InitScale(byte *_src_buffer, byte *_dest_buffer, int _dest_pitch) -{ - src_buffer = _src_buffer; - dest_buffer = _dest_buffer; - dest_pitch = _dest_pitch; -} - -// -// Pixel doubling scale-up functions. -// - -// 1x scale doesn't really do any scaling: it just copies the buffer -// a line at a time for when pitch != SCREENWIDTH (!native_surface) - -static boolean I_Scale1x(int x1, int y1, int x2, int y2) -{ - byte *bufp, *screenp; - int y; - int w = x2 - x1; - - // Need to byte-copy from buffer into the screen buffer - - bufp = src_buffer + y1 * SCREENWIDTH + x1; - screenp = (byte *) dest_buffer + y1 * dest_pitch + x1; - - for (y=y1; y 240) - - for (y=0; y 480) - - for (y=0; y 720) - - for (y=0; y 960) - - for (y=0; y 1200) - - for (y=0; y 0) - { - screenp = (byte *) dest_buffer + 2 * dest_pitch; - - for (y=0; y<1198; y += 3) - { - memset(screenp, 0, 1600); - - screenp += dest_pitch * 3; - } - } - - return true; -} - -screen_mode_t mode_stretch_5x = { - SCREENWIDTH * 5, SCREENHEIGHT_4_3 * 5, - I_InitStretchTables, - I_Stretch5x, - false, -}; - -// -// Aspect ratio correcting "squash" functions. -// -// These do the opposite of the "stretch" functions above: while the -// stretch functions increase the vertical dimensions, the squash -// functions decrease the horizontal dimensions for the same result. -// -// The same blend tables from the stretch functions are reused; as -// a result, the dimensions are *slightly* wrong (eg. 320x200 should -// squash to 266x200, but actually squashes to 256x200). -// - -// -// 1x squashed scale (256x200) -// - -static inline void WriteSquashedLine1x(byte *dest, byte *src) -{ - int x; - - for (x=0; x multiples of 320x240) - -extern screen_mode_t mode_stretch_1x; -extern screen_mode_t mode_stretch_2x; -extern screen_mode_t mode_stretch_3x; -extern screen_mode_t mode_stretch_4x; -extern screen_mode_t mode_stretch_5x; - -// Horizontally squashed modes (320x200 -> multiples of 256x200) - -extern screen_mode_t mode_squash_1x; -extern screen_mode_t mode_squash_2x; -extern screen_mode_t mode_squash_3x; -extern screen_mode_t mode_squash_4x; -extern screen_mode_t mode_squash_5x; - -#endif /* #ifndef __I_SCALE__ */ - diff --git a/MCUME_teensy41/teensydoom/i_video.c b/MCUME_teensy41/teensydoom/i_video.c index cc932c0..2e70e50 100644 --- a/MCUME_teensy41/teensydoom/i_video.c +++ b/MCUME_teensy41/teensydoom/i_video.c @@ -196,7 +196,26 @@ void I_GetEvent (void) event.data1 = KEY_ENTER; D_PostEvent (&event); } - + if ( (!(oldjoystick & 0x40)) && (joystick & 0x40) ) { + event.type = ev_keydown; + event.data1 = KEY_USE; + D_PostEvent (&event); + } + else if ( (!(joystick & 0x40)) && (oldjoystick & 0x40) ) { + event.type = ev_keyup; + event.data1 = KEY_USE; + D_PostEvent (&event); + } + if ( (!(oldjoystick & 0x80)) && (joystick & 0x80) ) { + event.type = ev_keydown; + event.data1 = KEY_TAB; + D_PostEvent (&event); + } + else if ( (!(joystick & 0x80)) && (oldjoystick & 0x80) ) { + event.type = ev_keyup; + event.data1 = KEY_TAB; + D_PostEvent (&event); + } oldjoystick = joystick; } diff --git a/MCUME_teensy41/teensydoom/platform_config.h b/MCUME_teensy41/teensydoom/platform_config.h index db001e3..aeedac5 100644 --- a/MCUME_teensy41/teensydoom/platform_config.h +++ b/MCUME_teensy41/teensydoom/platform_config.h @@ -7,7 +7,7 @@ //#define INVX 1 //#define INVY 1 //#define HAS_SND 1 -//#define HAS_USBKEY 1 +#define HAS_USBKEY 1 //#define HAS_I2CKBD 1 //#define ILI9341 1 diff --git a/MCUME_teensy41/teensydoom/r_main.c b/MCUME_teensy41/teensydoom/r_main.c index 22278fe..fbb5296 100644 --- a/MCUME_teensy41/teensydoom/r_main.c +++ b/MCUME_teensy41/teensydoom/r_main.c @@ -90,12 +90,13 @@ angle_t clipangle; // maps the visible view angles to screen X coordinates, // flattening the arc to a flat projection plane. // There will be many angles mapped to the same X. -int viewangletox[FINEANGLES/2]; +int * viewangletox = NULL; // The xtoviewangleangle[] table maps a screen pixel // to the lowest viewangle that maps back to x ranges // from clipangle to -clipangle. -angle_t xtoviewangle[SCREENWIDTH+1]; +angle_t * xtoviewangle = NULL; + lighttable_t* scalelight[LIGHTLEVELS][MAXLIGHTSCALE]; lighttable_t* scalelightfixed[MAXLIGHTSCALE]; @@ -504,6 +505,8 @@ fixed_t R_ScaleFromGlobalAngle (angle_t visangle) // void R_InitTables (void) { + + // UNUSED: now getting from tables.c #if 0 int i; @@ -544,6 +547,9 @@ void R_InitTextureMapping (void) int t; fixed_t focallength; + if (xtoviewangle == NULL) xtoviewangle = emu_Malloc(sizeof(angle_t)*(SCREENWIDTH+1)); + if (viewangletox == NULL) viewangletox = emu_Malloc(sizeof(int)*(FINEANGLES/2)); + // Use tangent table to generate viewangletox: // viewangletox will give the next greatest x // after the view angle. diff --git a/MCUME_teensy41/teensydoom/r_state.h b/MCUME_teensy41/teensydoom/r_state.h index 2a60e2f..625ef20 100644 --- a/MCUME_teensy41/teensydoom/r_state.h +++ b/MCUME_teensy41/teensydoom/r_state.h @@ -105,8 +105,8 @@ extern player_t* viewplayer; // ? extern angle_t clipangle; -extern int viewangletox[FINEANGLES/2]; -extern angle_t xtoviewangle[SCREENWIDTH+1]; +extern int * viewangletox; +extern angle_t * xtoviewangle; //extern fixed_t finetangent[FINEANGLES/2]; extern fixed_t rw_distance; diff --git a/MCUME_teensy41/teensydoom/teensydoom.ino b/MCUME_teensy41/teensydoom/teensydoom.ino index 46098aa..d4600b8 100644 --- a/MCUME_teensy41/teensydoom/teensydoom.ino +++ b/MCUME_teensy41/teensydoom/teensydoom.ino @@ -22,6 +22,7 @@ static int skip=0; static unsigned long long mscount=0; volatile unsigned int systime; int joystick=0; +static int usbjoystick=0; static void vblCount() { if (vbl) { @@ -137,18 +138,21 @@ void loop(void) if (action == ACTION_RUNTFT) { toggleMenu(false); tft.fillScreenNoDma( RGBVAL16(0x00,0x00,0x00) ); - tft.startDMA(); + tft.startDMA(); char filepath[80]; strcpy(filepath, ROMSDIR); strcat(filepath, "/"); strcat(filepath, wad); D_DoomMain(filepath); Serial.println("init end"); + usbjoystick=0; + emu_start(); } delay(20); } else { - int k=emu_ReadKeys(); + int k = usbjoystick; + if (k==0) k = emu_ReadKeys(); joystick = 0; if ( (k & MASK_JOY1_DOWN) || (k & MASK_JOY2_DOWN) ) joystick|=0x02; if ( (k & MASK_JOY1_UP) || (k & MASK_JOY2_UP) ) joystick|=0x01; @@ -156,6 +160,8 @@ void loop(void) if ( (k & MASK_JOY1_RIGHT) || (k & MASK_JOY2_RIGHT) ) joystick|=0x08; if ( (k & MASK_JOY1_BTN) || (k & MASK_JOY2_BTN) ) joystick|=0x10; if ( (k & MASK_KEY_USER1) ) joystick|=0x20; + if ( (k & MASK_KEY_USER2) ) joystick|=0x40; + if ( (k & MASK_KEY_USER3) ) joystick|=0x80; D_DoomLoop(); emu_DrawVsync(); } @@ -163,9 +169,25 @@ void loop(void) void emu_KeyboardOnDown(int keymodifer, int key) { + if (key == 218) usbjoystick|=MASK_JOY1_UP; // UP + else if (key == 217) usbjoystick|=MASK_JOY1_DOWN; // DOWN + else if (key == 216) usbjoystick|=MASK_JOY1_LEFT; // LEFT + else if (key == 215) usbjoystick|=MASK_JOY1_RIGHT; // RIGHT + else if (key == 102) usbjoystick|=MASK_JOY1_BTN; // FIRE + else if (key == 10) usbjoystick|=MASK_KEY_USER1; // RETURN + else if (key == 32) usbjoystick|=MASK_KEY_USER2; // USE + else if (key == 9) usbjoystick|=MASK_KEY_USER3; // TAB } void emu_KeyboardOnUp(int keymodifer, int key) { + if (key == 218) usbjoystick&=~MASK_JOY1_UP; // UP + else if (key == 217) usbjoystick&=~MASK_JOY1_DOWN; // DOWN + else if (key == 216) usbjoystick&=~MASK_JOY1_LEFT; // LEFT + else if (key == 215) usbjoystick&=~MASK_JOY1_RIGHT; // RIGHT + else if (key == 102) usbjoystick&=~MASK_JOY1_BTN; // FIRE + else if (key == 10) usbjoystick&=~MASK_KEY_USER1; // RETURN + else if (key == 32) usbjoystick&=~MASK_KEY_USER2; // USE + else if (key == 9) usbjoystick&=~MASK_KEY_USER3; // TAB }