You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
238 lines
5.0 KiB
C
238 lines
5.0 KiB
C
//---------------------------------------------------------------------------
|
|
#include <SDL/SDL.h>
|
|
#include "video/colors.h"
|
|
#include "video/gbuffer.h"
|
|
//---------------------------------------------------------------------------
|
|
|
|
static SDL_Surface *screen = NULL;
|
|
|
|
typedef struct {
|
|
int lastkey;
|
|
int lastkeysym;
|
|
int lastkeymod;
|
|
} keyb;
|
|
|
|
static keyb keys[100];
|
|
static int keybufferlen;
|
|
|
|
int timer_tick = 0;
|
|
|
|
static Uint32 timer_handler(Uint32 interval, void *param)
|
|
{
|
|
timer_tick ++;
|
|
return(interval);
|
|
}
|
|
|
|
int startptc(char *label)
|
|
{
|
|
SDL_Color palette[256];
|
|
int qi;
|
|
|
|
lastkey = -1;
|
|
lastkeysym = -1;
|
|
lastkeymod = -1;
|
|
|
|
keybufferlen = 0;
|
|
|
|
if ( (screen = SDL_SetVideoMode(WINSIZEX,WINSIZEY,8,SDL_SWSURFACE)) == NULL)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
memset(&palette,0x00,sizeof(palette));
|
|
for (qi = 0; qi < sizeof(palette)/sizeof(unsigned int); qi++)
|
|
{
|
|
palette[qi].r = (colors[qi] >> 16) & 0xff;
|
|
palette[qi].g = (colors[qi] >> 8) & 0xff;
|
|
palette[qi].b = colors[qi] & 0xff;
|
|
}
|
|
|
|
SDL_SetColors(screen, palette, 0, 256);
|
|
|
|
if ( SDL_LockSurface(screen) < 0 )
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
SDL_WM_SetCaption(label, label);
|
|
|
|
SDL_AddTimer(1000/20, timer_handler, NULL);
|
|
|
|
return 1;
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
int getkeys()
|
|
{
|
|
int i;
|
|
|
|
if (keybufferlen > 0)
|
|
{
|
|
lastkey = keys[0].lastkey;
|
|
lastkeysym = keys[0].lastkeysym;
|
|
lastkeymod = keys[0].lastkeymod;
|
|
|
|
for (i=1; i<keybufferlen; i++)
|
|
memcpy(&keys[i-1],&keys[i],sizeof(keyb));
|
|
|
|
keybufferlen--;
|
|
|
|
if (lastkey != -1)
|
|
return 1;
|
|
|
|
}
|
|
return 0;
|
|
|
|
}
|
|
|
|
void events()
|
|
{
|
|
SDL_Event event;
|
|
int count = 0;
|
|
|
|
while ( SDL_PollEvent(&event) && (count<10) )
|
|
{
|
|
switch (event.type)
|
|
{
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
break;
|
|
|
|
case SDL_KEYDOWN:
|
|
if (keybufferlen < 99)
|
|
{
|
|
keys[keybufferlen].lastkey = event.key.keysym.unicode;
|
|
keys[keybufferlen].lastkeysym = event.key.keysym.sym;
|
|
keys[keybufferlen].lastkeymod = event.key.keysym.mod;
|
|
|
|
if ( (keys[keybufferlen].lastkeymod & KMOD_RSHIFT) || (keys[keybufferlen].lastkeymod & KMOD_LSHIFT) )
|
|
keys[keybufferlen].lastkeymod |= KMOD_SHIFT;
|
|
|
|
// if ( (keys[keybufferlen].lastkeymod & KMOD_RALT) || (keys[keybufferlen].lastkeymod & KMOD_LALT) )
|
|
// keys[keybufferlen].lastkeymod |= KMOD_ALT;
|
|
|
|
if ( (keys[keybufferlen].lastkeymod & KMOD_RCTRL) || (keys[keybufferlen].lastkeymod & KMOD_LCTRL) )
|
|
keys[keybufferlen].lastkeymod |= KMOD_CTRL;
|
|
|
|
if ((keys[keybufferlen].lastkeymod & KMOD_RALT) == KMOD_RALT)
|
|
if ((keys[keybufferlen].lastkey < 32) || (keys[keybufferlen].lastkey >= 128))
|
|
keys[keybufferlen].lastkey = 0;
|
|
|
|
keybufferlen++;
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
|
if (keybufferlen < 100)
|
|
{
|
|
keys[keybufferlen].lastkey = -1;
|
|
keys[keybufferlen].lastkeysym = -1;
|
|
keys[keybufferlen++].lastkeymod = -1;
|
|
}
|
|
break;
|
|
|
|
case SDL_QUIT:
|
|
keys[0].lastkey = -2;
|
|
keys[0].lastkeysym = -1;
|
|
keybufferlen = 1;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
count ++;
|
|
}
|
|
|
|
}
|
|
void updateptc()
|
|
{
|
|
SDL_UnlockSurface(screen);
|
|
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
|
SDL_LockSurface(screen);
|
|
events();
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
void endptc()
|
|
{
|
|
SDL_UnlockSurface(screen);
|
|
SDL_FreeSurface(screen);
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
void drawchar(int cx, int cy, unsigned char ch, unsigned char fgcolor, unsigned char bgcolor, unsigned char *charset)
|
|
{
|
|
int charsetstart, qi, qj;
|
|
int mempos;
|
|
char tempreg;
|
|
|
|
char *pixels = screen->pixels;
|
|
|
|
charsetstart = (ch << 3);
|
|
|
|
cy = cy << 3;
|
|
cx = cx << 3;
|
|
for (qi = cy; qi < cy + 8; qi++)
|
|
{
|
|
tempreg = charset[charsetstart++];
|
|
mempos = (2 + qi) * screen->pitch + cx + 2;
|
|
|
|
if ((tempreg & 0x80) == 0x80)
|
|
pixels[mempos++] = fgcolor;
|
|
else
|
|
pixels[mempos++] = bgcolor;
|
|
if ((tempreg & 0x40) == 0x40)
|
|
pixels[mempos++] = fgcolor;
|
|
else
|
|
pixels[mempos++] = bgcolor;
|
|
if ((tempreg & 0x20) == 0x20)
|
|
pixels[mempos++] = fgcolor;
|
|
else
|
|
pixels[mempos++] = bgcolor;
|
|
if ((tempreg & 0x10) == 0x10)
|
|
pixels[mempos++] = fgcolor;
|
|
else
|
|
pixels[mempos++] = bgcolor;
|
|
if ((tempreg & 0x08) == 0x08)
|
|
pixels[mempos++] = fgcolor;
|
|
else
|
|
pixels[mempos++] = bgcolor;
|
|
if ((tempreg & 0x04) == 0x04)
|
|
pixels[mempos++] = fgcolor;
|
|
else
|
|
pixels[mempos++] = bgcolor;
|
|
if ((tempreg & 0x02) == 0x02)
|
|
pixels[mempos++] = fgcolor;
|
|
else
|
|
pixels[mempos++] = bgcolor;
|
|
if ((tempreg & 0x01) == 0x01)
|
|
pixels[mempos++] = fgcolor;
|
|
else
|
|
pixels[mempos++] = bgcolor;
|
|
|
|
}
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
void drawstring(int cx, int cy, unsigned char *str, unsigned char fgcolor, unsigned char bgcolor, void *charset)
|
|
{
|
|
int qi;
|
|
|
|
while (*str != 0)
|
|
{
|
|
if (*str == '\r')
|
|
{
|
|
for (qi = cx; qi<C_SIZEX; qi++)
|
|
drawchar(qi, cy, 0x20, fgcolor, bgcolor, (unsigned char *) charset);
|
|
|
|
str++;
|
|
}
|
|
else
|
|
drawchar(cx++, cy, *str++, fgcolor, bgcolor, (unsigned char *) charset);
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|