| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | adding a thread |
AG> You said that if we needed help with threads, we should ask...
Here you go. This was not a great sample for threads, because I didn't
need to use any semaphores (come up with a more complicated problem next
time ), although I could have.
I'm not sure which compiler you're using -- I used both IBM and Watcom on this:
// note -- columns & rows are reversed in your program; I didn't fix it.
// note2 -- you do not need a VioSetCurPos() before a vio call that
// takes row, col as parameters.
#define INCL_VIO
#define INCL_DOS
#define INCL_NOPMAPI
#include
#include
#include
#include
#include
int pick_colour(void);
unsigned char contents[2];
#define ROW 5
#define COLUMN 5
static SHORT row = 0, column = 0;
void SpinThread(void *vpDie)
{
unsigned i=0;
BYTE attr = 7;
char c[4] = "-\\|/";
int * volatile pDie = (int *)vpDie;
while(*pDie == FALSE){
attr = column | ((row + 7) << 4);
attr &= 0x7f; /*PLF Tue 95-06-13 10:58:18*/
VioWrtCharStrAtt(c+i, 1, column + COLUMN, row + ROW, &attr, 0);
i++;
if(i == sizeof(c))
i = 0;
DosSleep(50L);
}
printf("bye!\n");
}
int main(void)
{
int value;
VIOMODEINFO ModeData;
ModeData.cb = sizeof(ModeData);
VioGetMode( &ModeData, 0 );
value = pick_colour();
printf("\nColour picked was: %d", value);
return 0;
}
unsigned int getkey(void)
{
unsigned int c = getch();
if(c == 0 || c == 0xe0)
c = getch() << 8;
return c;
}
int pick_colour()
{
VIOCURSORINFO CursorData;
unsigned int input = 0;
int die;
VioScrollUp(0, 0, -1, -1, -1, " \x7", 0); // cls
VioGetCurType( &CursorData, 0 );
/* for(row = 0; row < 8; row++) */
for(row = 0; row < 16; row++)
{
for(column = 0; column < 16; column++)
{
/* contents[0] = 7; */
contents[0] = 254;
contents[1] = column;
contents[1] |= row << 4;
VioWrtNCell((PBYTE)&contents, 1, column + COLUMN, row + ROW, 0);
}
}
row = 0;
column = 0;
CursorData.attr = -1;
VioSetCurType( &CursorData, 0 );
die = FALSE;
_beginthread(SpinThread, NULL, 0x4000, &die);
for(;;)
{
input = getkey();
contents[0] = 254;
contents[1] = column;
contents[1] |= row << 4;
VioWrtNCell((PBYTE)&contents, 1, column + COLUMN, row + ROW, 0);
switch(input)
{
case 0x4800: // up
column --;
break;
case 0x4b00: // left
row--;
break;
case 0x4d00: // right
row ++;
break;
case 0x5000: // down
column ++;
break;
}
if (input == 27 || input == 13) break;
if(row > 15) row = 0;
else if(row < 0) row = 15;
if(column > 15) column = 0;
else if(column < 0) column = 15;
}
VioSetCurPos( 23, 0, 0 );
die = TRUE; // Ask thread to die; we could have used an EventSem
// for this.
DosSleep(101L); // Give enough time to see the "bye" message, else the
// program might end before we see it. Not really
// important.
CursorData.attr = 1;
VioSetCurType( &CursorData, 0 );
if(input == 27)
return -1;
else
return ((row << 4) + column);
}
--- Maximus/2 2.02p1
* Origin: Sol 3/Toronto (905)858-8488 (1:259/414)SEEN-BY: 105/42 620/243 711/401 409 410 413 430 807 808 809 934 955 712/407 SEEN-BY: 712/515 628 704 713/888 800/1 7877/2809 @PATH: 259/414 400 99 250/702 3615/50 396/1 270/101 105/103 42 712/515 @PATH: 711/808 809 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.