分页: 1 / 1

[问题]SDL有没有类似TC的图形API...[己解决]

发表于 : 2007-09-29 16:34
weilichun
比如 像 circle(...)
rectangle(int x1,int y1,int x2,int y2);

我在网上下载的SDL 库,下面的代码可以编译执行。。但是我没找到画图的。。[不是我非得找这个,我们老师要求TC图形编程,我得找个linux下的替代品]

#include <stdlib.h>
#include <SDL/SDL.h>
int main() {
SDL_Surface *screen;
Uint32 color;
if ( SDL_Init( SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "无法初始化SDL: %s\n", SDL_GetError());
exit(1);
}
screen = SDL_SetVideoMode(1280, 800, 24, SDL_SWSURFACE); /*640 X 480 X 16位色*/
if ( screen == NULL ) {
fprintf(stderr, "无法设置640x480x16位色的视频模式:%s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
int i;
int width,height;
for ( i = 0; i < 256; i++) {
width=rand()%1280;
height=rand()%800;
color = SDL_MapRGB(screen->format, rand()%256, (i*i)%256, rand()%256); /*蓝色*/
SDL_FillRect(screen, &screen->clip_rect, color); /*整个屏幕填充颜色*/
SDL_UpdateRect(screen, width,height,rand()%1280, rand()%800); /*update screen*/
/*
SDL_Delay(5);
*/
printf("%d\n",i);
}
//SDL_Delay(5000); /*Delay for 5 seconds*/
}

http://wiki.gp2x.org/wiki/Writing_an_SDL_Hello_World

发表于 : 2007-09-29 17:02
weilichun

Demo

发表于 : 2007-09-29 17:34
weilichun

代码: 全选

#include <stdlib.h>
#include <SDL/SDL.h>
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 800
#define SCREEN_DEPTH 8

int main(int argc, char *argv[]) {
    SDL_Surface *screen;
    Uint8       *p;
    int         x = 10; //x coordinate of our pixel
    int         y = 20; //y coordinate of our pixel
    /* Initialize SDL */
    SDL_Init(SDL_INIT_VIDEO);
    /* Initialize the screen / window */
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
    
    /* Make p point to the place we want to draw the pixel */
    p = (Uint8 *)screen->pixels + y * screen->pitch + x * screen->format->BytesPerPixel;
    int i;
    while(1){
        for ( x = 0; x <SCREEN_WIDTH; x++) {
            for ( y = 0; y < SCREEN_HEIGHT; y++) {
                p = (Uint8 *)screen->pixels + y * screen->pitch + x * screen->format->BytesPerPixel;
                *p=rand()%256;
            }
        }
        /* update the screen (aka double buffering) */
        SDL_Flip(screen);
        //SDL_Delay(10);
    }
    /* Draw the pixel! */
    *p=0xff;
    
}

Re: [问题]SDL有没有类似TC的图形API...

发表于 : 2007-10-06 0:25
ailantian
weilichun 写了:比如 像 circle(...)
rectangle(int x1,int y1,int x2,int y2);

我在网上下载的SDL 库,下面的代码可以编译执行。。但是我没找到画图的。。[不是我非得找这个,我们老师要求TC图形编程,我得找个linux下的替代品]

#include <stdlib.h>
#include <SDL/SDL.h>
int main() {
SDL_Surface *screen;
Uint32 color;
if ( SDL_Init( SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "无法初始化SDL: %s\n", SDL_GetError());
exit(1);
}
screen = SDL_SetVideoMode(1280, 800, 24, SDL_SWSURFACE); /*640 X 480 X 16位色*/
if ( screen == NULL ) {
fprintf(stderr, "无法设置640x480x16位色的视频模式:%s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
int i;
int width,height;
for ( i = 0; i < 256; i++) {
width=rand()%1280;
height=rand()%800;
color = SDL_MapRGB(screen->format, rand()%256, (i*i)%256, rand()%256); /*蓝色*/
SDL_FillRect(screen, &screen->clip_rect, color); /*整个屏幕填充颜色*/
SDL_UpdateRect(screen, width,height,rand()%1280, rand()%800); /*update screen*/
/*
SDL_Delay(5);
*/
printf("%d\n",i);
}
//SDL_Delay(5000); /*Delay for 5 seconds*/
}

你需要的是svgalib这样的东西.非常古老而简陋的东西
http://www.svgalib.org/jay/beginners_gu ... guide.html

Re: [问题]SDL有没有类似TC的图形API...

发表于 : 2007-10-06 22:33
weilichun
你需要的是svgalib这样的东西.非常古老而简陋的东西
http://www.svgalib.org/jay/beginners_gu ... guide.html
感谢楼上,这个非常合适。
在synatips里安装svgalib就可以了。
下面是一个Demo。

代码: 全选

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <vga.h>
#define WIDTH 800
#define HEIGHT 600
#define COLOR_DEPTH 256


int main(void) {

	srand(time(NULL));
	vga_init();
	vga_setmode(G800x600x256); 
	int x=0,y=0; 
	double radius=0.0;
	int color=2;
	int cx=400,cy=300;//center x,center y
	double angle=0.0;
	while(1){
		if(vga_getkey())
			break;
		x=radius*cos(angle)+cx;
		y=radius*sin(angle)+cy;
		vga_setcolor(color);
		vga_drawpixel(x,y);
		radius+=0.0001;
		angle+=0.0001;
		color=angle/M_PI+1;
		if(radius>1500)
			break;
	}
	getchar();
	vga_setmode(TEXT);
	return 0;
}
编译的时候需要添加 -lvga -lvgagl

代码: 全选

gcc  -o demo    -l vga -l vgagl  demo.c 

Re: [问题]SDL有没有类似TC的图形API...

发表于 : 2007-10-07 18:33
ailantian
weilichun 写了:
你需要的是svgalib这样的东西.非常古老而简陋的东西
http://www.svgalib.org/jay/beginners_gu ... guide.html
感谢楼上,这个非常合适。
在synatips里安装svgalib就可以了。
下面是一个Demo。

代码: 全选

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <vga.h>
#define WIDTH 800
#define HEIGHT 600
#define COLOR_DEPTH 256


int main(void) {

	srand(time(NULL));
	vga_init();
	vga_setmode(G800x600x256); 
	int x=0,y=0; 
	double radius=0.0;
	int color=2;
	int cx=400,cy=300;//center x,center y
	double angle=0.0;
	while(1){
		if(vga_getkey())
			break;
		x=radius*cos(angle)+cx;
		y=radius*sin(angle)+cy;
		vga_setcolor(color);
		vga_drawpixel(x,y);
		radius+=0.0001;
		angle+=0.0001;
		color=angle/M_PI+1;
		if(radius>1500)
			break;
	}
	getchar();
	vga_setmode(TEXT);
	return 0;
}
编译的时候需要添加 -lvga -lvgagl

代码: 全选

gcc  -o demo    -l vga -l vgagl  demo.c 
呵呵,给你个好玩的程序
#include "math.h"
#include "vga.h"

//int leaf()
int main()
{
vga_init();
vga_setmode(G800x600x256);
vga_setcolor(4);
int i;
float x=0,y=0,r,u=0;

srand(time(0));
for(i=0;i<=500000;i++)
{
r=rand()%100;

if (r < 1)
{
x = 0;
y = .16 * y;
}
if (r>=1 && r<86)
{
u = .85 * x + .04 * y;
y = -.04*x + .85 * y + 1.6;
x = u;
}
if (r>=86 && r<97)
{
u = .2 * x - .26 * y;
y = .23 * x + .22 * y + 1.6;
x = u;
}
if (r>=97)
{
u = -.15 * x + .28 * y;
y = .26 * x + .24 * y +.44;
x = u;
}
vga_drawpixel(45*x+230,480-45*y);
}
sleep(3);
vga_setmode(0);

return 0;
}
ailantian@vax:~/mypg/download/half_code$
gcc -o uu uu.c -lvga就可以了
图片

发表于 : 2007-10-07 18:51
pwsxp
分形?妙

发表于 : 2007-10-07 20:30
weilichun
真是不错。对了,你的截图是怎么做出来的呀。
我在运行这些程序的时候,好像是在虚拟终端运行的。 :oops: :oops: :oops:

Re: 强

发表于 : 2007-10-08 12:33
ailantian
weilichun 写了:真是不错。对了,你的截图是怎么做出来的呀。
我在运行这些程序的时候,好像是在虚拟终端运行的。 :oops: :oops: :oops:
上面的图片不是截屏的,是直接写入png文件的,需要libpng.已经不是使用svgalib了.当初也是为了得到一个
图片.
可以参考一下这里,反正代码都是网上的,分形树叶的代码也是网上找的,改改就能用,

代码: 全选

#include "string.h"
#include "stdio.h"
#include "png.h"
#include "math.h"

int write_png(char *filename)
{
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;

int ERROR = 1;

fp = fopen(filename, "wb");
if (fp == NULL) {
printf("Can't open file : %s\n", filename);
return ERROR;
}

png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

if (png_ptr == NULL) {
printf("ERROR : png_create_write_struct \n");
fclose(fp);
return ERROR;
}

/* Allocate/initialize the image information data. REQUIRED */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
printf("ERROR : png_create_info_struct \n");
fclose(fp);
png_destroy_write_struct(&png_ptr, png_infopp_NULL);
return (ERROR);
}

/* Set error handling. REQUIRED if you aren't supplying your own
* error handling functions in the png_create_write_struct() call.
*/
if (setjmp(png_jmpbuf(png_ptr)))
{
/* If we get here, we had a problem writing the file */
printf("ERROR when reading the file \n");
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
return (ERROR);
}

/* set up the output control if you are using standard C streams */
png_init_io(png_ptr, fp);

const int width = 800;
const int height = 600;

png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

png_write_info(png_ptr, info_ptr);

png_uint_32 k;
png_byte image[height][width];
png_bytep row_pointers[height];
//main functions
int i;
float x=0,y=0,r,u=0;

srand(time(0));
for(i=0;i<=500000;i++)
{
r=rand()%100;

if (r < 1)
{
x = 0;
y = .16 * y;
}
if (r>=1 && r<86)
{
u = .85 * x + .04 * y;
y = -.04*x + .85 * y + 1.6;
x = u;
}
if (r>=86 && r<97)
{
u = .2 * x - .26 * y;
y = .23 * x + .22 * y + 1.6;
x = u;
}
if (r>=97)
{
u = -.15 * x + .28 * y;
y = .26 * x + .24 * y +.44;
x = u;
}


int m,n;
m=45*x+230;
n=480-45*y;
/* if(m<0)
m=fabs(m);
if(n<0)
n=fabs(n);
*/ image[m][n]=240;
// printf("image[%d][%d]=255\n",m,n);



/* int m,n;
m=x;
n=y;
memset(image[n][m],255,sizeof(png_byte));
*/

/*
int m,n;
n=y;
for(n=0;n
{
if(x==m)
{image[m][n],255,sizeof(png_byte);}
}

*/


/* for(m=0;m
{
for(n=0;n
{
if(width==x&&height==y)
// image[n][m]=char(255);
memset(image[n][m],255,sizeof(png_byte));
}
}
*/
//setpixel(x,y);
// image[45*x+230][480-45*y]=char(255);
}

//*
// fade out
for (k = 0; k < height; k++) {
// memset(image[k], k / 2, width);
// memset(image[k],240, width);
row_pointers[k] = image[k];
}
/*/
// sinusoid
memset(image, 255, sizeof(image));

for (k = 0; k < height; k++) {
double r = double(k) / height * 6 * 3.14159265358;
int y = sin(r) * (width / 2 - 1);
image[k][y + width / 2] = 0;
row_pointers[k] = image[k];
}
//*/

png_write_image(png_ptr, row_pointers);

png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);

/* close the file */
fclose(fp);
return 0;
}

int main()
{
write_png("testpnggray.png");
}
好了,对了还要说说编译的命令gcc -o testpnggray testpnggray.c -lpng

Re: 强

发表于 : 2007-10-08 21:01
weilichun
上面的图片不是截屏的,是直接写入png文件的,需要libpng.已经不是使用svgalib了.当初也是为了得到一个
图片.
可以参考一下这里,反正代码都是网上的,分形树叶的代码也是网上找的,改改就能用,
真是这样,由长见识了。嘿嘿。感谢楼上。
:D :D