You are not logged in.
Dalam thread ini silahkan dimuat/dibahas fungsi-fungsi C yang pernah anda buat yang berkaitan dengan penanganan keyboard, mouse, dan screen dalam mode teks.
Itu dapat berupa:
* fungsi-fungsi baru;
* fungsi-fungsi standar yang anda perbarui dengan menambahkan fitur-fitur tertentu;
* fungsi-fungsi standar yang akan anda jelaskan logika pemrogramannya.
Fungsi-fungsi tersebut dapat ditulis dalam C atau bahasa lain yang dapat digunakan dari C.
Keterangan tentang sistem operasi dan kompiler yang digunakan dapat ditempatkan di awal posting anda, seperti [LINUX | GCC] atau [DOS | TC].
Last edited by mustofa (30-12-2008 09:11:35)
Offline
[DOS | TC2/BC3]
Prototipe fungsi, struktur, variabel, dan konstanta yang digunakan untuk penanganan console di Turbo C 2.1 atau Borland C 3.1 disimpan di dalam header-file conio.h
Struktur:
struct text_info {
unsigned char winleft;
unsigned char wintop;
unsigned char winright;
unsigned char winbottom;
unsigned char attribute;
unsigned char normattr;
unsigned char currmode;
unsigned char screenheight;
unsigned char screenwidth;
unsigned char curx;
unsigned char cury;
};
Mode text:
-1 atau LASTMODE
0 atau BW40
1 atau C40
2 atau BW80
3 atau C80
7 atau MONO
64 atau C4350 ---> BC3
Warna:
0 atau BLACK
1 atau BLUE,
2 atau GREEN,
3 atau CYAN,
4 atau RED,
5 atau MAGENTA,
6 atau BROWN,
7 atau LIGHTGRAY,
8 atau DARKGRAY,
9 atau LIGHTBLUE,
10 atau LIGHTGREEN,
11 atau LIGHTCYAN,
12 atau LIGHTRED,
13 atau LIGHTMAGENTA,
14 atau YELLOW,
15 atau WHITE
Blinking:
128 atau BLINK
Mode Kursor:
0 atau _NOCURSOR
1 atau _SOLIDCURSOR
2 atau _NORMALCURSOR
Variabel:
int directvideo;
int _wscroll; ---> BC3
Secara lengkap, isi dari conio.h adalah sebagai berikut
/* conio.h
Direct MSDOS console input/output.
Copyright (c) 1987, 1992 by Borland International
All Rights Reserved.
*/
#if !defined(__CONIO_H)
#define __CONIO_H
#if !defined(___DEFS_H)
#include <_defs.h>
#endif
#if !defined(_Windows)
#define _NOCURSOR 0
#define _SOLIDCURSOR 1
#define _NORMALCURSOR 2
struct text_info {
unsigned char winleft;
unsigned char wintop;
unsigned char winright;
unsigned char winbottom;
unsigned char attribute;
unsigned char normattr;
unsigned char currmode;
unsigned char screenheight;
unsigned char screenwidth;
unsigned char curx;
unsigned char cury;
};
enum text_modes { LASTMODE=-1, BW40=0, C40, BW80, C80, MONO=7, C4350=64 };
#if !defined(__COLORS)
#define __COLORS
enum COLORS {
BLACK, /* dark colors */
BLUE,
GREEN,
CYAN,
RED,
MAGENTA,
BROWN,
LIGHTGRAY,
DARKGRAY, /* light colors */
LIGHTBLUE,
LIGHTGREEN,
LIGHTCYAN,
LIGHTRED,
LIGHTMAGENTA,
YELLOW,
WHITE
};
#endif
#define BLINK 128 /* blink bit */
extern int _Cdecl directvideo;
extern int _Cdecl _wscroll;
#endif /* !_Windows */
#ifdef __cplusplus
extern "C" {
#endif
void _Cdecl clreol( void );
void _Cdecl clrscr( void );
void _Cdecl gotoxy( int __x, int __y );
int _Cdecl wherex( void );
int _Cdecl wherey( void );
int _Cdecl getch( void );
int _Cdecl getche( void );
int _Cdecl kbhit( void );
int _Cdecl putch( int __c );
#ifndef _PORT_DEFS
unsigned char _Cdecl inportb( unsigned __portid );
unsigned _Cdecl inport ( unsigned __portid );
int _Cdecl inp( unsigned __portid );
unsigned _Cdecl inpw( unsigned __portid );
void _Cdecl outportb( unsigned __portid, unsigned char __value );
void _Cdecl outport ( unsigned __portid, unsigned __value );
int _Cdecl outp( unsigned __portid, int __value );
unsigned _Cdecl outpw( unsigned __portid, unsigned __value );
#endif /* !_PORT_DEFS */
#if !defined(_Windows)
void _Cdecl delline( void );
int _Cdecl gettext( int __left, int __top,
int __right, int __bottom,
void *__destin);
void _Cdecl gettextinfo (struct text_info *__r );
void _Cdecl highvideo( void );
void _Cdecl insline( void );
void _Cdecl lowvideo( void );
int _Cdecl movetext( int __left, int __top,
int __right, int __bottom,
int __destleft, int __desttop );
void _Cdecl normvideo( void );
int _Cdecl puttext( int __left, int __top,
int __right, int __bottom,
void *__source );
void _Cdecl textattr( int __newattr );
void _Cdecl textbackground( int __newcolor );
void _Cdecl textcolor( int __newcolor );
void _Cdecl textmode( int __newmode );
void _Cdecl window( int __left, int __top, int __right, int __bottom);
void _Cdecl _setcursortype( int __cur_t );
char * _Cdecl cgets( char *__str );
int _Cdecl cprintf( const char *__format, ... );
int _Cdecl cputs( const char *__str );
int _Cdecl cscanf( const char *__format, ... );
char * _Cdecl getpass( const char *__prompt );
int _Cdecl ungetch( int __ch );
#endif /* !_Windows */
#ifndef _PORT_DEFS
#define _PORT_DEFS
/* These are in-line functions. These prototypes just clean up
some syntax checks and code generation.
*/
unsigned char _Cdecl __inportb__ (unsigned __portid);
unsigned _Cdecl __inportw__ (unsigned __portid);
unsigned char _Cdecl __outportb__(unsigned __portid, unsigned char __value);
unsigned _Cdecl __outportw__(unsigned __portid, unsigned __value);
#define inportb(__portid) __inportb__(__portid)
#define outportb(__portid, __value) ((void) __outportb__(__portid, __value))
#define inport(__portid) __inportw__(__portid)
#define outport(__portid, __value) ((void) __outportw__(__portid, __value))
/* MSC-compatible macros for port I/O */
#define inp(__portid) __inportb__ (__portid)
#define outp(__portid, __value) __outportb__(__portid, (unsigned char)__value)
#define inpw(__portid) __inportw__ (__portid)
#define outpw(__portid, __value) __outportw__(__portid, __value)
#endif /* _PORT_DEFS */
#ifdef __cplusplus
}
#endif
#endif /* __CONIO_H */Kode sumber fungsi-fungsi tersebut berada di <bcdir>\crtl\clib. Jika anda bermaksud untuk mengubah fungsi-fungsi tersebut silahkan anda pilih fungsi yang dimaksud, ubah sesuai kebutuhan anda, kompile ulang dalam model memori tertentu, dan gabung ke dalam library model memori tersebut.
Offline
[DOS | TC2/BC3]
Struktur text_info yang digunakan oleh TC/BC dapat diambil dengan menggunakan fungsi gettextinfo.
TC/BC menyimpan mode, atribut, dan atribut-asal teks di dalam field currmode dan attribute, dan normattr.
Atribut teks terdiri dari warna teks dan wana-dasar (background) teks.
Fungsi textcolor digunakan untuk menetapkan warna teks, fungsi textbackground digunakan untuk menetapkan warna-dasar teks, sementara fungsi textattr digunakan untuk menetapkan keduanya.
Format internal atribut teks adalah "kbbbcccc". Bit terkiri (k) adalah kode blinking, 3 bit tengah (bbb) adalah warna dasar, dan 4 bit terakhir (ccc) adalah warna teks.
TC/BC menetapkan koordinat kiri-atas layar sebagai kolom 1 baris 1 dan koordinat kanan-bawah sebagai kolom 40/80 baris 25/43/50 tergantung mode teks yang digunakan. Namun secara internal itu disimpan di field winleft, wintop, winright, dan winbottom menjadi 0, 0, 39/79, dan 24/42/49.
Begitu juga dengan koordinat kursor, secara visual itu mengacu pada koordinat 1,1 sementara secara internal itu mengacu pada koordinat 0,0 dan disimpan di dalam field curx dan cury.
Informasi tentang tinggi dan lebar layar disimpan dalam screenheight dan screenwidth.
Offline
wah saya gak ngerti pak...
gmn cara manggil fungsinya??
Offline
[DOS | TC2/BC3]
Fungsi textmode digunakan untuk mengubah mode tampilan teks sesuai yang diinginkan. Prototip fungsi ini adalah:
void textmode(int newmode);
dimana newmode adalah:
-1 atau LASTMODE
0 atau BW40
1 atau C40
2 atau BW80
3 atau C80
7 atau MONO
64 atau C4350 ---> BC3
Contoh:
#include <stdio.h>
#include <conio.h>
int mode;
int main()
{
while (1)
{ clrscr();
printf("Mode Teks:\n"
" -1: LASTMODE\n"
" 0: BW40\n"
" 1: C40\n"
" 2: BW80\n"
" 3: C80\n"
" 7: MONO\n"
" 64: C4350 ---> BC3\n"
" 99: Selesai\n"
"Pilih: ");
scanf("%d", &mode);
switch (mode)
{ case -1:
case 0:
case 1:
case 2:
case 3:
case 7:
case 64:
textmode(mode);
break;
case 99:
return 1;
}
}
}Offline
[DOS | TC2/BC3]
Atribut teks terdiri dari warna teks dan wana-dasar (background) teks.
Fungsi textcolor digunakan untuk menetapkan warna teks, fungsi textbackground digunakan untuk menetapkan warna-dasar teks, sementara fungsi textattr digunakan untuk menetapkan keduanya.
Format internal atribut teks adalah "kbbbcccc". Bit terkiri (k) adalah kode blinking, 3 bit tengah (bbb) adalah warna dasar, dan 4 bit terakhir (ccc) adalah warna teks.
Contoh:
#include <conio.h>
int mode;
int main()
{
clrscr();
textcolor(YELLOW);
textbackground(BLUE)
cprintf("Teks ini dicetak dalam warna YELLOW berdasar BLUE\r\n");
textattr(0x2F);
/*
2 = GREEN
F = WHITE
*/
cprintf("Teks ini dicetak dalam warna WHITE berdasar GREEN\r\n");
return 1;
}Offline
Only on Win OS… If someone would like to make colorized output on Linux/UNIX terminals that implement VT100 specification, I can advice to search for VT100 control codes, for example, for red text you can output "\033[22;31m" before the wanted string… The best for cross-platform support will be to write your own functions for colorized output, with different code for Win & UNIX systems. It can be reached with macroses like #ifdef WIN32 …
(upd) Link: http://www.unix.com/shell-programming-s … olour.html
Last edited by WST (26-12-2009 00:36:55)
Offline
WST wrote:
Only on Win OS… If someone would like to make colorized output on Linux/UNIX terminals that implement VT100 specification, I can advice to search for VT100 control codes, for example, for red text you can output "\033[22;31m" before the wanted string… The best for cross-platform support will be to write your own functions for colorized output, with different code for Win & UNIX systems. It can be reached with macroses like #ifdef WIN32 …
(upd) Link: http://www.unix.com/shell-programming-s … olour.html
Thank you for your advice. See http://www.mit.edu/~vona/VonaUtils/vona … Codes.html to get VT100 escape codes completely.
Offline
[DOS | TC2/BC3]
Fungsi putch digunakan untuk menaruh/menulis satu karakter, yang berkode tertentu, ke console. Contoh:
putch(65);
Itu dapat pula dituliskan menjadi
putch('A');
Fungsi getch() dan getche() digunakan untuk mengambil/membaca satu karakter dari keyboard. Fungsi ini akan mengembalikan kode karakter yang diambil, misalnya 65 untuk huruf 'A'. Fungsi getch hanya akan mengambil/membaca karakter saja sementara fungsi getche akan menuliskan karakter yang dibaca tersebut ke console. Jadi, perintah
getche();
sama dengan
ch = getch();
putch(ch);
Fungsi getch dan getche akan mengembalikan nilai 0 jika karakter yang diberikan berupa karakter perluasan, misalnya anda menekan tombol F1. Untuk mendapatkan kode karakter perluasan tersebut maka fungsi getch atau getche harus dipanggil kembali. Perhatikan cuplikan contoh berikut.
...
ch = getch();
if (ch)
cprintf("Anda menekan karakter berkode %d", ch);
else
{ ch = getch();
cprintf("Anda menekan karakter perluasan berkode %d", ch);
}
...Untuk menyederhanakan penggunaan fungsi tersebut, saya membuat satu fungsi baru yang diberi nama GetCh sebagai berikut.
#include <conio.h>
int GetCh(void)
{
int ch;
ch = getch();
if (!ch)
ch = 256 + getch();
return ch;
}Contoh penggunaan fungsi GetCh() akan dibahas dalam posting berikutnya.
Offline