Update keyjam.exe to use SDL2

KeyJam is only used on Windows, so SDL1 support has been dropped.
Build this tool from the v0.7.0 branch if you need an SDL1-compatible
version of KeyJam.
This commit is contained in:
Michael Martin
2020-09-05 15:05:47 -07:00
parent 5f268d85d1
commit 6dff21ef52
2 changed files with 94 additions and 35 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
all: keyjam.c all: keyjam.c
gcc $(shell sdl-config --cflags) -o keyjam keyjam.c $(shell sdl-config --libs) -lSDL_gfx gcc $(shell sdl2-config --cflags) -o keyjam keyjam.c $(shell sdl2-config --libs) -lSDL2_gfx
clean: clean:
rm -f keyjam rm -f keyjam
+90 -31
View File
@@ -1,29 +1,79 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "SDL.h" #include <SDL.h>
#include "SDL_gfxPrimitives.h" #include <SDL2_gfxPrimitives.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define BUFFERSIZE 1024 #define BUFFERSIZE 1024
#define MAX_KEYS 32
static int keystate[SDLK_LAST]; static SDL_Keycode keystate[MAX_KEYS];
static char held_keys[BUFFERSIZE]; static char held_keys[BUFFERSIZE];
static char videoname[BUFFERSIZE]; static char videoname[BUFFERSIZE];
void static void
DrawCenteredText (SDL_Surface *s, int y, const char *c) InitHeldKeys (void)
{
int i;
for (i = 0; i < MAX_KEYS; ++i)
{
keystate[i] = 0;
}
}
static void
AddHeldKey (SDL_Keycode key)
{
int i;
for (i = 0; i < MAX_KEYS; ++i)
{
if (keystate[i] == key)
{
return;
}
if (keystate[i] == 0)
{
keystate[i] = key;
return;
}
}
}
static void
RemoveHeldKey (SDL_Keycode key)
{
int i;
for (i = 0; i < MAX_KEYS; ++i)
{
if (keystate[i] == key)
{
int j;
for (j = i + 1; j < MAX_KEYS; ++j)
{
keystate[j - 1] = keystate[j];
}
keystate[MAX_KEYS - 1] = 0;
}
}
}
static void
DrawCenteredText (SDL_Renderer *r, int y, const char *c)
{ {
int w = strlen (c) * 8; int w = strlen (c) * 8;
stringRGBA(s, (s->w - w) / 2, y, c, 0xc0, 0xc0, 0xc0, 0xff); stringRGBA (r, (SCREEN_WIDTH - w) / 2, y, c, 0xc0, 0xc0, 0xc0, 0xff);
} }
void static void
DrawScreen (SDL_Surface *s) DrawScreen (SDL_Renderer *s)
{ {
int y; int y;
SDL_FillRect (s, NULL, SDL_MapRGB (s->format, 0, 0, 0x80)); SDL_SetRenderDrawColor (s, 0, 0, 0x80, 0xFF);
SDL_RenderClear (s);
DrawCenteredText (s, 8, "Key Jamming"); DrawCenteredText (s, 8, "Key Jamming");
y = 32; y = 32;
@@ -48,7 +98,7 @@ DrawScreen (SDL_Surface *s)
DrawCenteredText (s, y, "register when simultaneously pressed."); DrawCenteredText (s, y, "register when simultaneously pressed.");
y += 16; y += 16;
DrawCenteredText (s, y, "You are using the following SDL driver:"); DrawCenteredText (s, y, "You are using the following SDL2 rendering engine:");
y += 8; y += 8;
DrawCenteredText (s, y, videoname); DrawCenteredText (s, y, videoname);
@@ -57,14 +107,16 @@ DrawScreen (SDL_Surface *s)
DrawCenteredText (s, 300, held_keys); DrawCenteredText (s, 300, held_keys);
SDL_Flip (s); SDL_RenderPresent (s);
} }
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
SDL_Event event; SDL_Event event;
SDL_Surface *screen; SDL_Window *window;
SDL_Renderer *screen;
SDL_RendererInfo renderer_info;
int quit = 0; int quit = 0;
int i, changed; int i, changed;
@@ -76,26 +128,31 @@ main (int argc, char *argv[])
} }
/* Set a video mode */ /* Set a video mode */
if (!(screen = SDL_SetVideoMode (640, 480, 0, SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT))) if (SDL_CreateWindowAndRenderer (SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &screen))
{ {
fprintf (stderr, "Could not set video mode: %s\n", SDL_GetError()); fprintf (stderr, "Could not set create window/renderer: %s\n", SDL_GetError ());
SDL_Quit (); SDL_Quit ();
exit (-1); exit (-1);
} }
SDL_WM_SetCaption ("Key Jammer", "Key Jammer"); SDL_SetWindowTitle (window, "Key Jammer");
if (!SDL_GetRendererInfo (screen, &renderer_info))
SDL_VideoDriverName (videoname, BUFFERSIZE);
for (i = 0; i < SDLK_LAST; i++)
{ {
keystate[i] = 0; strncpy (videoname, renderer_info.name, BUFFERSIZE);
} }
else
{
strncpy (videoname, "<unknown>", BUFFERSIZE);
}
videoname[BUFFERSIZE-1] = 0;
InitHeldKeys ();
DrawScreen (screen); DrawScreen (screen);
/* Loop until an SDL_QUIT event is found */ /* Loop until an SDL_QUIT event is found */
while( !quit ) { while (!quit)
{
changed = 0; changed = 0;
/* Poll for events */ /* Poll for events */
while (SDL_PollEvent (&event)) while (SDL_PollEvent (&event))
@@ -103,24 +160,28 @@ main (int argc, char *argv[])
switch(event.type) switch(event.type)
{ {
case SDL_KEYDOWN: case SDL_KEYDOWN:
keystate[event.key.keysym.sym] = 1; AddHeldKey (event.key.keysym.sym);
if (event.key.keysym.sym == SDLK_ESCAPE)
{
quit = 1;
}
changed = 1; changed = 1;
break; break;
case SDL_KEYUP: case SDL_KEYUP:
keystate[event.key.keysym.sym] = 0; RemoveHeldKey (event.key.keysym.sym);
changed = 1; changed = 1;
break; break;
case SDL_QUIT: case SDL_QUIT:
quit = 1; quit = 1;
break; break;
default: default:
break; break;
} }
} }
if (changed) { if (changed)
{
held_keys[0] = '\0'; held_keys[0] = '\0';
for (i = 0; i < SDLK_LAST; i++) for (i = 0; i < MAX_KEYS; i++)
{ {
if (keystate[i]) if (keystate[i])
{ {
@@ -128,7 +189,7 @@ main (int argc, char *argv[])
{ {
strcat (held_keys, " | "); strcat (held_keys, " | ");
} }
strcat(held_keys, SDL_GetKeyName (i)); strcat (held_keys, SDL_GetKeyName (keystate[i]));
} }
if (strlen (held_keys) > 200) if (strlen (held_keys) > 200)
{ {
@@ -145,14 +206,12 @@ main (int argc, char *argv[])
} }
DrawScreen (screen); DrawScreen (screen);
} }
if (keystate[SDLK_ESCAPE])
{
quit = 1;
}
SDL_Delay (20); SDL_Delay (20);
} }
/* Clean up */ /* Clean up */
SDL_DestroyRenderer (screen);
SDL_DestroyWindow (window);
SDL_Quit (); SDL_Quit ();
exit (0); exit (0);
} }