gcc / g ++的基本图形库与graphics.h一样 [英] Basic Graphics library for gcc/g++ on par with graphics.h

查看:264
本文介绍了gcc / g ++的基本图形库与graphics.h一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道可以在
中使用的标准图形库与gcc / g ++结合吗?我只想绘制线条,
圆形,多边形,三角形等。我还想要字体和颜色
支持。 Library with graphics.h is expected。

Does anyone know of a standard graphics library that can be used in conjunction with gcc/g++? I only want to be able to draw lines, circles, polygons, triangles etc. I would also like to have font and color support. Library on par with graphics.h is much expected.

谢谢。

Thanks.

推荐答案

有完全相同的问题。但我想这就是我们使用CLI所得到的结果。

had EXACTLY the same problem. But I guess that's what we get for using a CLI.

所以我决定写我自己的。它是一个输出Windows类型BMP文件的单一功能。我需要为我的MinGW / GCC数字计算器输出一些中间直方图,并且我不想混淆库,制作文件和所有这些。您只需将几行代码剪切到您的.C文件中并构建它即可。它有ZERO外部调用,链接或任何。

So I just decided to write my own. Its a single function that outputs a Windows-type BMP file. I needed to output some intermediate histograms for my MinGW/GCC number-cruncher, and I didn't want to mess with libraries, make files and all of that. You just cut and past the few lines of code into your .C file and build it. It has ZERO external calls, links or whatever.

您只需更改三个数组中的元素:红色[],绿色[]和蓝色],调用writeBMP()函数与文件名,你可以看到你通过将图像绘制到绘图,或资源管理器或其他东西。

You just change the elements in the three arrays: red[], green[], and blue[], call the writeBMP() function with a filename and you can see what you did by bringing the image into paint, or explorer or something.

你会认为它是最愚蠢的东西,或者你见过的最天才的东西。这里是它的源代码:

You'll either think its the dumbest thing, or the most genius thing you've ever seen. Here is the source code for it:

#include <stdio.h>
#include <memory.h>

// absolute minimalist graphics support for C language
// (especially for my buddy's running MinGW/GCC)
//
// Requires no libraries, no external includes, no make files, NOTHING else required!!!
// To build right out of the box type:   gcc graph.c     and execute it.
//
// Instructions: Copy all of this code into your c/c++ source file (or into another *.c file and
// include it with '#include "graph.c"' keeping it in the same directory as your main program.
// Set the various values to the size of BMP you need, and its ready to go.
//
// Just call writeBMP and it will output a BMP file based on the contents of the 3 arrays below.
// note: this uses the example #1 used in the Wikipedia's BMP file article
// Programmed by Woody Stanford email: woodystanford@yahoo.com

//global arrays to write graphics to
char red[420][140];
char green[420][140];
char blue[420][140];

// IMPORTANT: image width is required to be an even multiple of 4.
// 410 won't work, 420 will, sorry but bmp file padding requires it.

writeBMP(char *fname)
{
FILE *fptr;

long sx=420; //image width - MANUALLY SET THIS VALUE
long sy=140; //image height - MANUALLY SET THIS VALUE

long fs,is,a,b; //file size, image size, counters

//minimalist BMP file output harcoded for RGB (24 bits per pixel)
char bmp_hdr[54]={0x42,0x4D,0x46,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x36,0x00, 0x00,0x00,
          0x28,0x00,0x00,0x00, 0x02,0x00,0x00,0x00, 0x02,0x00,0x00,0x00, 0x01,0x00,
          0x18,0x00,0x00,0x00, 0x00,0x00,0x10,0x00, 0x00,0x00,0x13,0x0B, 0x00,0x00,
          0x13,0x0B,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00};

fs=54+(sx*sy*3);    //set fs to size of header + (sx * sy * 3)
is=(sx*sy*3);       //set is to sx*sy*3

memcpy((void *)&bmp_hdr[2],(void *)&fs,4);
memcpy((void *)&bmp_hdr[18],(void *)&sx,4);
memcpy((void *)&bmp_hdr[22],(void *)&sy,4);
memcpy((void *)&bmp_hdr[34],(void *)&is,4);

fptr=fopen(fname,"wb");
fwrite(bmp_hdr,54,1,fptr);
for (a=0;a<sy;a++)
    for (b=0;b<sx;b++)
    {
        fwrite(&blue[b][a],1,1,fptr);   
        fwrite(&green[b][a],1,1,fptr);  
        fwrite(&red[b][a],1,1,fptr);    
    }
fclose(fptr);
}

main()
{
int x,y;
printf("Just a stub main function. Use your own main function.\n\n");

/*
//  Example code: (uncomment to see demo)
//  sets background color to light gray and then generates the BMP file
for (y=0;y<120;y++)
    for (x=0;x<420;x++)
        { red[x][y]=127; green[x][y]=127; blue[x][y]=127; }

writeBMP("woody2.bmp");
printf("Example BMP file written.\n\n");
*/
return;
}

/* HTML code for Internet Explorer page to refresh image display every 5 seconds:

<html>
<head>
<meta http-equiv="refresh" content="5">
<meta http-equiv="cache-control" content="no-store">
</head>
Your Image<br>
<center><img src="c:\woody\woody2.bmp"></center>
</html>

note: Check your browser's cache settings if it doesn't refresh right
*/

这篇关于gcc / g ++的基本图形库与graphics.h一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆