Changeset 508
- Timestamp:
- Nov 12, 2005, 4:14:38 PM (15 years ago)
- Location:
- libcaca/trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/src/caca.h
r496 r508 365 365 char* caca_get_html3(void); 366 366 char* caca_get_irc(void); 367 char* caca_get_ ANSI(int trailing);367 char* caca_get_ansi(int trailing); 368 368 /* @} */ 369 369 -
libcaca/trunk/src/graphics.c
r496 r508 238 238 #if defined(USE_GL) 239 239 240 static unsigned int const gl_bg _palette[] =240 static unsigned int const gl_bgpal[] = 241 241 { 242 242 0, … … 303 303 #if defined(USE_GL) 304 304 unsigned char gl_key = 0; 305 int gl_special_key =0;305 int gl_special_key = 0; 306 306 int gl_new_width; 307 307 int gl_new_height; 308 308 309 310 309 static void gl_handle_keyboard(unsigned char key, int x, int y) 311 310 { 312 311 gl_key = key; 313 312 } 313 314 314 static void gl_handle_special_key(int key, int x, int y) 315 315 { 316 gl_special_key = key; 317 } 318 static void gl_handle_reshape (int w, int h) 319 { 320 if(gl_bit) /* Do not handle reshaping at the first time*/ 321 { 322 323 gl_new_width = w; 324 gl_new_height = h; 325 326 gl_resized = 1; 327 } 328 else 329 gl_bit=1; 330 } 316 gl_special_key = key; 317 } 318 319 static void gl_handle_reshape(int w, int h) 320 { 321 if(gl_bit) /* Do not handle reshaping at the first time*/ 322 { 323 gl_new_width = w; 324 gl_new_height = h; 325 326 gl_resized = 1; 327 } 328 else 329 gl_bit = 1; 330 } 331 331 332 static void gl_handle_mouse(int button, int state, int x, int y) 332 333 { 333 gl_mouse_clicked = 1; 334 gl_mouse_button = button; 335 gl_mouse_state = state; 336 gl_mouse_x = x/gl_font_width; 337 gl_mouse_y = y/gl_font_height; 338 gl_mouse_changed = 1; 339 } 334 gl_mouse_clicked = 1; 335 gl_mouse_button = button; 336 gl_mouse_state = state; 337 gl_mouse_x = x / gl_font_width; 338 gl_mouse_y = y / gl_font_height; 339 gl_mouse_changed = 1; 340 } 341 340 342 static void gl_handle_mouse_motion(int x, int y) 341 343 { 342 gl_mouse_x = x/gl_font_width; 343 gl_mouse_y = y/gl_font_height; 344 gl_mouse_changed = 1; 345 } 346 347 #endif 348 349 344 gl_mouse_x = x / gl_font_width; 345 gl_mouse_y = y / gl_font_height; 346 gl_mouse_changed = 1; 347 } 348 #endif 350 349 351 350 /** \brief Set the default colour pair. … … 797 796 gettextinfo(&conio_ti); 798 797 conio_screen = malloc(2 * conio_ti.screenwidth 799 798 * conio_ti.screenheight * sizeof(char)); 800 799 if(conio_screen == NULL) 801 800 return -1; … … 994 993 #endif 995 994 #if defined(USE_GL) 996 997 998 999 1000 1001 995 if(_caca_driver == CACA_DRIVER_GL) 996 { 997 int i; 998 char *empty; 999 1000 if(getenv("CACA_GEOMETRY") && *(getenv("CACA_GEOMETRY"))) 1002 1001 sscanf(getenv("CACA_GEOMETRY"), 1003 1002 "%ux%u", &_caca_width, &_caca_height); … … 1008 1007 _caca_height = 32; 1009 1008 1010 gl_font_width = 9; 1011 gl_font_height = 15; 1012 1013 gl_width = _caca_width*gl_font_width; 1014 gl_height = _caca_height*gl_font_height; 1015 1016 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 1017 glutInitWindowSize(gl_width, gl_height); 1018 gl_window = glutCreateWindow("caca for GL"); 1019 1020 gluOrtho2D(0,gl_width, gl_height, 0); 1021 1022 glDisable(GL_CULL_FACE); 1023 glDisable(GL_DEPTH_TEST); 1024 1025 glutKeyboardFunc(gl_handle_keyboard); 1026 glutSpecialFunc(gl_handle_special_key); 1027 glutReshapeFunc(gl_handle_reshape); 1028 1029 glutMouseFunc(gl_handle_mouse); 1030 glutMotionFunc(gl_handle_mouse_motion); 1031 glutPassiveMotionFunc(gl_handle_mouse_motion); 1032 1033 1034 glLoadIdentity(); 1035 1036 glMatrixMode(GL_PROJECTION); 1037 glPushMatrix(); 1038 glLoadIdentity(); 1039 gluOrtho2D(0, gl_width, gl_height, 0); 1040 1041 glMatrixMode(GL_MODELVIEW); 1042 1043 1044 glClear(GL_COLOR_BUFFER_BIT); 1045 1046 1047 1048 empty = malloc(16*16*4); 1049 if(empty == NULL) 1009 gl_font_width = 9; 1010 gl_font_height = 15; 1011 1012 gl_width = _caca_width * gl_font_width; 1013 gl_height = _caca_height * gl_font_height; 1014 1015 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 1016 glutInitWindowSize(gl_width, gl_height); 1017 gl_window = glutCreateWindow("caca for GL"); 1018 1019 gluOrtho2D(0, gl_width, gl_height, 0); 1020 1021 glDisable(GL_CULL_FACE); 1022 glDisable(GL_DEPTH_TEST); 1023 1024 glutKeyboardFunc(gl_handle_keyboard); 1025 glutSpecialFunc(gl_handle_special_key); 1026 glutReshapeFunc(gl_handle_reshape); 1027 1028 glutMouseFunc(gl_handle_mouse); 1029 glutMotionFunc(gl_handle_mouse_motion); 1030 glutPassiveMotionFunc(gl_handle_mouse_motion); 1031 1032 glLoadIdentity(); 1033 1034 glMatrixMode(GL_PROJECTION); 1035 glPushMatrix(); 1036 glLoadIdentity(); 1037 gluOrtho2D(0, gl_width, gl_height, 0); 1038 1039 glMatrixMode(GL_MODELVIEW); 1040 1041 glClear(GL_COLOR_BUFFER_BIT); 1042 1043 empty = malloc(16 * 16 * 4); 1044 if(empty == NULL) 1050 1045 return -1; 1051 1046 1052 memset(empty, 255, 16*16*4); 1053 glEnable(GL_TEXTURE_2D); 1054 1055 for(i=0;i<94;i++) 1056 { 1057 glGenTextures(1,&id[i]); 1058 glBindTexture(GL_TEXTURE_2D, id[i]); 1059 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1060 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1061 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1062 16,16, 1063 0, GL_RGB, GL_UNSIGNED_BYTE, empty); 1064 } 1065 for(i=0;i<94;i++) 1066 { 1067 1068 glDisable(GL_TEXTURE_2D); 1069 glClear(GL_COLOR_BUFFER_BIT); 1070 1071 glColor3f(1,1,1); 1072 glRasterPos2f(0,15); 1073 glutBitmapCharacter(GLUT_BITMAP_9_BY_15,i+32); 1074 1075 1076 glEnable(GL_TEXTURE_2D); 1077 glBindTexture(GL_TEXTURE_2D,id[i]); 1078 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, gl_height-16, 16,16, 0); 1079 1080 glutMainLoopEvent(); 1081 glutPostRedisplay(); 1082 } 1047 memset(empty, 0xff, 16 * 16 * 4); 1048 glEnable(GL_TEXTURE_2D); 1049 1050 for(i = 0; i < 94; i++) 1051 { 1052 glGenTextures(1, &id[i]); 1053 glBindTexture(GL_TEXTURE_2D, id[i]); 1054 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1055 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1056 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1057 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty); 1083 1058 } 1059 1060 for(i = 0; i < 94; i++) 1061 { 1062 glDisable(GL_TEXTURE_2D); 1063 glClear(GL_COLOR_BUFFER_BIT); 1064 1065 glColor3f(1, 1, 1); 1066 glRasterPos2f(0, 15); 1067 glutBitmapCharacter(GLUT_BITMAP_9_BY_15, i + 32); 1068 1069 glEnable(GL_TEXTURE_2D); 1070 glBindTexture(GL_TEXTURE_2D, id[i]); 1071 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1072 0, gl_height - 16, 16, 16, 0); 1073 1074 glutMainLoopEvent(); 1075 glutPostRedisplay(); 1076 } 1077 } 1084 1078 else 1085 1079 #endif 1086 1080 #if defined(USE_NULL) 1087 1088 1089 1081 if(_caca_driver == CACA_DRIVER_NULL) 1082 { 1083 if(getenv("CACA_GEOMETRY") && *(getenv("CACA_GEOMETRY"))) 1090 1084 sscanf(getenv("CACA_GEOMETRY"), 1091 1085 "%ux%u", &_caca_width, &_caca_height); 1092 1086 if(!_caca_width) 1093 1087 _caca_width = 80; 1094 1088 if(!_caca_height) 1095 1089 _caca_height = 32; 1096 1097 1090 } 1091 else 1098 1092 #endif 1099 1093 { … … 1172 1166 #endif 1173 1167 #if defined(USE_GL) 1174 1175 1176 1177 1178 1168 if(_caca_driver == CACA_DRIVER_GL) 1169 { 1170 glutDestroyWindow(gl_window); 1171 } 1172 else 1179 1173 #endif 1180 1174 #if defined(USE_NULL) 1181 if(_caca_driver == CACA_DRIVER_NULL) 1182 { 1183 /* I'm bored to write 'nothing to do'. 1184 * So I don't. 1185 */ 1186 } 1187 else 1175 if(_caca_driver == CACA_DRIVER_NULL) 1176 { 1177 /* Nothing to do here. */ 1178 } 1179 else 1188 1180 #endif 1189 1181 { … … 1222 1214 #endif 1223 1215 #if defined(USE_GL) 1224 1225 1226 1227 1228 1216 if(_caca_driver == CACA_DRIVER_GL) 1217 { 1218 glutSetWindowTitle(title); 1219 } 1220 else 1229 1221 #endif 1230 1222 { … … 1262 1254 #endif 1263 1255 #if defined(USE_GL) 1264 1265 1266 1267 1268 1256 if(_caca_driver == CACA_DRIVER_GL) 1257 { 1258 return gl_width; 1259 } 1260 else 1269 1261 #endif 1270 1262 { … … 1302 1294 #endif 1303 1295 #if defined(USE_GL) 1304 1305 1306 1307 1308 1296 if(_caca_driver == CACA_DRIVER_GL) 1297 { 1298 return gl_height; 1299 } 1300 else 1309 1301 #endif 1310 1302 { … … 1476 1468 #endif 1477 1469 #if defined(USE_GL) 1478 if(_caca_driver == CACA_DRIVER_GL) 1479 { 1480 unsigned int x, y, offsetx, offsety; 1481 1482 1483 glClear(GL_COLOR_BUFFER_BIT); 1484 1485 offsety=0; 1486 for(y=0;y<gl_height;y+=gl_font_height) 1470 if(_caca_driver == CACA_DRIVER_GL) 1471 { 1472 unsigned int x, y, offsetx, offsety; 1473 1474 glClear(GL_COLOR_BUFFER_BIT); 1475 1476 offsety=0; 1477 for(y = 0; y < gl_height; y += gl_font_height) 1478 { 1479 offsetx = 0; 1480 for(x = 0; x < gl_width; x += gl_font_width) 1487 1481 { 1488 offsetx = 0;1489 for(x=0;x<gl_width;x+=gl_font_width)1490 {1491 1482 uint8_t *attr = cache_attr + offsetx + offsety * _caca_width; 1492 1483 int offset; 1493 1484 float br, bg, bb; 1494 offset = attr[0] >>4;1495 1496 br = ((gl_bg _palette[offset]&0x00FF0000)>>16)/255.0f;1497 bg = ((gl_bg _palette[offset]&0x0000FF00)>>8)/255.0f;1498 bb = ((gl_bg _palette[offset]&0x000000FF))/255.0f;1485 offset = attr[0] >> 4; 1486 1487 br = ((gl_bgpal[offset] & 0x00FF0000) >> 16) / 255.0f; 1488 bg = ((gl_bgpal[offset] & 0x0000FF00) >> 8) / 255.0f; 1489 bb = ((gl_bgpal[offset] & 0x000000FF)) / 255.0f; 1499 1490 1500 1491 glDisable(GL_TEXTURE_2D); 1501 1492 glColor3f(br, bg, bb); 1502 1493 glBegin(GL_QUADS); 1503 glVertex2f(x, y);1504 glVertex2f(x +gl_font_width,y);1505 glVertex2f(x +gl_font_width,y+gl_font_height);1506 glVertex2f(x, y+gl_font_height);1494 glVertex2f(x, y); 1495 glVertex2f(x + gl_font_width, y); 1496 glVertex2f(x + gl_font_width, y + gl_font_height); 1497 glVertex2f(x, y + gl_font_height); 1507 1498 glEnd(); 1508 1499 1509 1510 offsetx++; 1511 } 1500 offsetx++; 1501 } 1512 1502 1513 1503 offsety++; 1504 } 1505 1506 /* 2nd pass, avoids changing render state too much */ 1507 glEnable(GL_BLEND); 1508 glEnable(GL_TEXTURE_2D); 1509 glBlendFunc(GL_ONE, GL_ONE); 1510 1511 offsety = 0; 1512 for(y = 0; y < gl_height; y += gl_font_height) 1513 { 1514 offsetx = 0; 1515 for(x = 0; x < gl_width; x += gl_font_width) 1516 { 1517 uint8_t *attr = cache_attr + offsetx + offsety * _caca_width; 1518 unsigned char *chr = cache_char + offsetx + offsety * _caca_width; 1519 float fr, fg, fb; 1520 1521 fr = ((gl_bgpal[attr[0] & 0xf] & 0x00FF0000) >> 16) / 255.0f; 1522 fg = ((gl_bgpal[attr[0] & 0xf] & 0x0000FF00) >> 8) / 255.0f; 1523 fb = ((gl_bgpal[attr[0] & 0xf] & 0x000000FF)) / 255.0f; 1524 1525 if(chr[0] != ' ') 1526 { 1527 glBindTexture(GL_TEXTURE_2D, id[chr[0]-32]); 1528 1529 glColor3f(fr, fg, fb); 1530 glBegin(GL_QUADS); 1531 glTexCoord2f(0, 1); 1532 glVertex2f(x, y); 1533 glTexCoord2f(0.5, 1); 1534 glVertex2f(x + gl_font_width, y); 1535 glTexCoord2f(0.5, 0); 1536 glVertex2f(x + gl_font_width, y + gl_font_height); 1537 glTexCoord2f(0, 0); 1538 glVertex2f(x, y + gl_font_height); 1539 glEnd(); 1540 } 1541 offsetx++; 1514 1542 } 1515 1516 1517 1518 /* 2nd pass, avoids changing render state too much */ 1519 glEnable(GL_BLEND); 1520 glEnable(GL_TEXTURE_2D); 1521 glBlendFunc(GL_ONE, GL_ONE); 1522 1523 offsety=0; 1524 for(y=0;y<gl_height;y+=gl_font_height) 1525 { 1526 offsetx = 0; 1527 for(x=0;x<gl_width;x+=gl_font_width) 1528 { 1529 uint8_t *attr = cache_attr + offsetx + offsety * _caca_width; 1530 unsigned char *chr = cache_char + offsetx + offsety * _caca_width; 1531 float fr, fg, fb; 1532 1533 1534 fr = ((gl_bg_palette[attr[0] & 0xf]&0x00FF0000)>>16)/255.0f; 1535 fg = ((gl_bg_palette[attr[0] & 0xf]&0x0000FF00)>>8)/255.0f; 1536 fb = ((gl_bg_palette[attr[0] & 0xf]&0x000000FF))/255.0f; 1537 1538 if(chr[0] != ' ') 1539 { 1540 glBindTexture(GL_TEXTURE_2D, id[chr[0]-32]); 1541 1542 glColor3f(fr, fg, fb); 1543 glBegin(GL_QUADS); 1544 glTexCoord2f(0,1); 1545 glVertex2f(x,y); 1546 glTexCoord2f(0.5,1); 1547 glVertex2f(x+gl_font_width,y); 1548 glTexCoord2f(0.5,0); 1549 glVertex2f(x+gl_font_width,y+gl_font_height); 1550 glTexCoord2f(0,0); 1551 glVertex2f(x,y+gl_font_height); 1552 glEnd(); 1553 } 1554 offsetx++; 1555 } 1556 offsety++; 1557 } 1558 glDisable(GL_BLEND); 1559 glDisable(GL_TEXTURE_2D); 1560 1561 1562 1563 glutMainLoopEvent(); 1564 glutSwapBuffers(); 1565 glutPostRedisplay(); 1543 offsety++; 1566 1544 } 1567 else 1545 glDisable(GL_BLEND); 1546 glDisable(GL_TEXTURE_2D); 1547 1548 glutMainLoopEvent(); 1549 glutSwapBuffers(); 1550 glutPostRedisplay(); 1551 } 1552 else 1568 1553 #endif 1569 1554 #if defined(USE_NULL) 1570 if(_caca_driver == CACA_DRIVER_NULL) 1571 { 1572 /* Do I told you about my cat ? */ 1573 } 1555 if(_caca_driver == CACA_DRIVER_NULL) 1556 { 1557 /* Nothing to do here. */ 1558 } 1559 else 1574 1560 #endif 1575 1561 { … … 1697 1683 if(_caca_driver == CACA_DRIVER_NULL) 1698 1684 { 1699 /* \_o< pOaK */1700 /* By the way, we should never reach this,1701 * as events are not handled1702 */1703 }1685 /* \_o< pOaK 1686 * By the way, we should never reach this, 1687 * as events are not handled */ 1688 } 1689 else 1704 1690 #endif 1705 1691 { … … 2026 2012 * This function generates and returns an ANSI representation of 2027 2013 * the current image. 2028 * \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be able to cut/paste the result to a function like printf 2014 * \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be 2015 * able to cut/paste the result to a function like printf 2029 2016 * \return buffer containing generated ANSI codes as a big string 2030 2017 */ 2031 char * caca_get_ANSI(int trailing)2032 { 2033 static int const palette[] =2034 { 2035 30, 34, 32, 36, 31, 35, 33, 37, /* Both lines (light and dark) are the same, */2036 30, 34, 32, 36, 31, 35, 33, 37, /* light colors handling is done later */2018 char * caca_get_ansi(int trailing) 2019 { 2020 static int const palette[] = 2021 { 2022 30, 34, 32, 36, 31, 35, 33, 37, /* Both lines (light and dark) are the same, */ 2023 30, 34, 32, 36, 31, 35, 33, 37, /* light colors handling is done later */ 2037 2024 }; 2038 2025 … … 2041 2028 2042 2029 /* 20 bytes assumed for max length per pixel. 2043 Add height*9 to that (zeroes color at the end and jump to next line) 2044 */ 2030 * Add height*9 to that (zeroes color at the end and jump to next line) */ 2045 2031 buffer = malloc(((_caca_height*9) + (_caca_width * _caca_height * 20)) * sizeof(char)); 2046 2032 cur = buffer; … … 2062 2048 uint8_t c = linechar[x]; 2063 2049 2064 2065 2066 2067 2050 if(!trailing) 2051 cur += sprintf(cur, "\033["); 2052 else 2053 cur += sprintf(cur, "\\033["); 2068 2054 2069 if(fg>7)2070 2071 2072 2055 if(fg > 7) 2056 cur += sprintf(cur, "1;%d;%dm",fg,bg); 2057 else 2058 cur += sprintf(cur, "0;%d;%dm",fg,bg); 2073 2059 *cur++ = c; 2074 if((c=='%') && trailing)2075 *cur++=c;2060 if((c == '%') && trailing) 2061 *cur++ = c; 2076 2062 prevfg = fg; 2077 2063 prevbg = bg; 2078 2064 } 2079 if(!trailing) 2080 cur += sprintf(cur, "\033[0m\n\r"); 2081 else 2082 cur += sprintf(cur, "\\033[0m\\n\n"); 2083 } 2065 if(!trailing) 2066 cur += sprintf(cur, "\033[0m\n\r"); 2067 else 2068 cur += sprintf(cur, "\\033[0m\\n\n"); 2069 } 2070 2084 2071 /* Crop to really used size */ 2085 2072 buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char)); 2086 2073 2087 2074 return buffer; 2088 2089 } 2075 } 2076
Note: See TracChangeset
for help on using the changeset viewer.