예제 #1
0
파일: main.cpp 프로젝트: Bjwebb/opengl-test
void process_events( void ) {
    SDL_Event event;
    while( SDL_PollEvent( &event ) ) {
        switch( event.type ) {
        case SDL_KEYDOWN:
            handle_keydown( &event.key.keysym );
            break;
        case SDL_KEYUP:
            handle_keyup( &event.key.keysym );
            break;
        case SDL_QUIT:
            quit_app( 0 );
            break;
        }

    }
}
예제 #2
0
파일: main.c 프로젝트: Cocuyo17/gcodeview
int main(int argc, char* argv[]) {
	msgbuf = malloc(256);
	msgbuf[0] = 0;

	currentLayer = 0;
	cache = true;

	int longIndex;
	int opt;
	do {
		opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
		if (opt != -1) {
			switch( opt ) {
				case 'l':
					currentLayer = strtol(optarg, NULL, 10);
					break;

				case 'w':
					extrusionWidth = strtof(optarg, NULL);
					break;

				case 'n':
					printf("DISABLING CACHE\n");
					cache = false;
					break;

				case 'h':   /* fall-through is intentional */
				case '?':
					display_usage();
					break;

				case 0:     /* long option without a short arg */
					//if( strcmp( "randomize", longOpts[longIndex].name ) == 0 ) {
					//	globalArgs.randomized = 1;
					//}
					break;

				default:
					/* You won't actually get here. */
					break;
			}
		}
	}
	while (opt != -1);

	if (optind >= argc)
		display_usage();

	int fd = open(argv[optind], 0);
	if (fd == -1)
		die("Open ", argv[optind]);

	struct stat filestats;
	if (fstat(fd, &filestats) == -1)
		die("fstat ", argv[optind]);

	filesz = filestats.st_size;

	printf("File is %d long\n", filesz);

#ifdef __linux__
	gcodefile = mmap(NULL, filesz, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0);
#elif defined __APPLE__
	gcodefile = mmap(NULL, filesz, PROT_READ, MAP_PRIVATE, fd, 0);
#else
	#error "don't know how to mmap on this system!"
#endif

	if (gcodefile == MAP_FAILED)
		die("mmap ", argv[optind]);
	gcodefile_end = &gcodefile[filesz];

	busy = BUSY_SCANFILE;

	scanLines();

	if (currentLayer >= layerCount)
		currentLayer = layerCount - 1;

	//for (int i = 0; i < layerCount; i++)
	//	printf("Layer %3d starts at %7d and is %7d bytes long\n", i, layer[i].index - gcodefile, layer[i].size);

	Running = true;
	Surf_Display = NULL;

	if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
		die("SDL_init", "");

	if (FcInitLoadConfigAndFonts() == ((void *) FcTrue))
		die("FontConfig Init","");

	// from http://www.spinics.net/lists/font-config/msg03050.html
		FcPattern *pat, *match;
		FcResult result;
		char *file;
		int index;
		pat = FcPatternCreate();
		FcPatternAddString(pat, FC_FAMILY, (FcChar8 *) "Mono");
		FcConfigSubstitute(NULL, pat, FcMatchPattern);
		FcDefaultSubstitute(pat);
		match = FcFontMatch(NULL, pat, &result);
		FcPatternGetString(match, FC_FILE, 0, (FcChar8 **) &file);
		FcPatternGetInteger(match, FC_INDEX, 0, &index);


	font = ftglCreateExtrudeFont(file);
	if (!font)
		die("FTGL createFont", "");

	FcPatternDestroy (match);
	FcPatternDestroy (pat);

	#ifdef	OPENGL
		transX = transY = 0.0;
		zoomFactor = 1.0;

		resize(600, 600);
	#else
		viewPortL = viewPortT = 0.0;
		viewPortR = viewPortB = 200.0;
		zoomFactor = 3.0;
		resize(viewPortR * zoomFactor, viewPortB * zoomFactor);
	#endif

	SDL_WM_SetCaption("gcodeview", 0);

	drawLayer(currentLayer);

	layerVelocity = 0;

	timerIdle = SDL_AddTimer(20, &timerCallback, (void *) TIMER_IDLE);

	SDL_Event Event;
	while(Running != false) {
		if (busy) {
			Event.type = SDL_NOEVENT;
			SDL_PollEvent(&Event);
		}
		else {
			if (SDL_WaitEvent(&Event) == 0)
				die("SDL_WaitEvent", "");
		}
		//SDL_RemoveTimer(timerIdle);
		switch (Event.type) {
			case SDL_NOEVENT:
				if (busy & BUSY_SCANFILE) {
					// TODO: scan next layer
					scanLine();
					if ((busy & BUSY_SCANFILE) == 0) {
						if (cache) {
							printf("File scanned, rendering...\n");
							busy = BUSY_RENDER;
						}
						else {
							printf("File scanned.\n");
							busy = 0;
						}
					}
				}
				else if ((busy & BUSY_RENDER) && cache) {
					bool allRendered = true;
					int i;
					// TODO: render next layer in background
					for (i = 0; i < layerCount; i++) {
						if (layer[i].glList == 0) {
							layer[i].glList = glGenLists(1);
							glNewList(layer[i].glList, GL_COMPILE);
							glBegin(GL_QUADS);
							for (int j = SHADOW_LAYERS; j >= 1; j--) {
								if (i - j > 0)
									render_layer(i - j, SHADOW_ALPHA - (j - 1) * (SHADOW_ALPHA / SHADOW_LAYERS));
							}
							render_layer(i, 1.0);
							glEnd();
							glEndList();
							layer[i].flags |= LD_LISTGENERATED;
							allRendered = false;
							break;
						}
					}
					if (allRendered) {
						printf("All %d layers rendered\n", i);
						busy &= ~BUSY_RENDER;
					}
				}
				break;
			case SDL_QUIT:
				Running = false;
				break;
			case SDL_VIDEORESIZE:
				resize(Event.resize.w, Event.resize.h);
				break;
			case SDL_VIDEOEXPOSE:
				render();
				break;
			case SDL_MOUSEBUTTONDOWN:
				handle_mousedown(Event.button);
				break;
			case SDL_MOUSEBUTTONUP:
				handle_mouseup(Event.button);
				break;
			case SDL_MOUSEMOTION:
				handle_mousemove(Event.motion);
				break;
			case SDL_ACTIVEEVENT: // lose or gain focus
				break;
			case SDL_KEYDOWN:
				handle_keydown(Event.key);
				break;
			case SDL_KEYUP:
				handle_keyup(Event.key);
				break;
			case SDL_USEREVENT:
				handle_userevent(Event.user);
				break;
			default:
				printf("SDL Event %d\n", Event.type);
				break;
		}
		//idle code
		//if (busy)
		//	timerIdle = SDL_AddTimer(20, &timerCallback, (void *) TIMER_IDLE);
	}
	if (timerKeyRepeat)
		SDL_RemoveTimer(timerKeyRepeat);
	if (timerDragRender)
		SDL_RemoveTimer(timerDragRender);
	free(layer);
	SDL_FreeSurface(Surf_Display);
	SDL_Quit();
	return 0;
}