Exemplo n.º 1
0
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;
        }

    }
}
Exemplo n.º 2
0
void update_input()
{
    if (SDL_PollEvent(&event)) {
        switch(event.type) {
            case SDL_QUIT:
                quit = true;
                break;
            case SDL_KEYDOWN:
                handle_keypress(event.key.keysym.sym);
                break;
            case SDL_KEYUP:
                handle_keyup(event.key.keysym.sym);
                break;
        }
    }

    if (accel->up > 0) accel->up--;
    if (accel->down > 0) accel->down--;
    if (accel->left > 0) accel->left--;
    if (accel->right > 0) accel->right--;

    if (speed->up > 0) speed->up--;
    if (speed->down > 0) speed->down--;
    if (speed->left > 0) speed->left--;
    if (speed->right > 0) speed->right--;

    if (pressed_keys->up) accel->up = 2;
    if (pressed_keys->down) accel->down = 2;
    if (pressed_keys->left) accel->left = 2;
    if (pressed_keys->right) accel->right = 2;

    speed->up += accel->up;
    speed->down += accel->down;
    speed->left += accel->left;
    speed->right += accel->right;

    if (speed->up > max_speed) speed->up = max_speed;
    if (speed->down > max_speed) speed->down = max_speed;
    if (speed->left > max_speed) speed->left = max_speed;
    if (speed->right > max_speed) speed->right = max_speed;

    ship->rect.x += (speed->right - speed->left);
    ship->rect.y += (speed->down - speed->up);
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
Arquivo: main.cpp Projeto: HuiD/ece590
int eventLoop(SDL_Surface * screen) {
    SDL_Event event;
    int animationCounter = 0;
    while(1) {
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_KEYUP:
                if(handle_keyup(event.key.keysym.sym)==MENU) {
                    gameLoopCleanUp();
                    return MENU;
                }
                break;
            case SDL_KEYDOWN:
                handle_key(event.key.keysym.sym);
                break;
            }


        }

        handleNetwork();

        std::map<int, Hero*>::iterator it;
        for(it=heroGroup.begin(); it!=heroGroup.end(); ++it)
        {
            if (it->second->getVisible())
                it->second->update(blocks, colList, heroGroup, bombGroup, explosionGroup);
        }
        if(moved) {
            hero_pos newPos=heroGroup[myId]->getPos();
            heromessage msg;
            msg.LoadByte(newPos.x, newPos.y, newPos.id);
            udpclient->Send(msg,tcpclient->getIpAddress() ,-1);
            moved=false;
        }
        if(bombed)
        {
            int bombx, bomby, level;
            bombx=heroGroup[myId]->getBombx();
            bomby=heroGroup[myId]->getBomby();
            level=heroGroup[myId]->getBombLevel();
            bombmessage bmsg;
            bmsg.LoadByte(bombx, bomby, level);
            udpclient->Send(bmsg, tcpclient->getIpAddress(), -1);
            bombed=false;
        }
        for (int i = 0; i < blocks.size(); i++) {
            blocks[i]->update(colList, heroGroup, explosionGroup, upgradeGroup);
        }

        for (int i = 0; i < bombGroup.size(); i++) {
            bombGroup.at(i)->update(blocks, colList, heroGroup, bombGroup, explosionGroup);
        }
        for (int i = 0; i < explosionGroup.size(); i++) {
            explosionGroup.at(i)->update();
        }
        for (int i = 0; i < upgradeGroup.size(); i++) {
            upgradeGroup.at(i)->update();
        }

        for (int i = 0; i < colList.size(); i++) {
            CollisionPair * tmp = colList.at(i);
            if (tmp->isCollided()) {
                tmp->onCollision();
            }
        }

        background->blit(screen);

        if (Mix_PlayingMusic() == 0) {
            if (Mix_PlayMusic(mainMusic, -1) == -1)
                fprintf(stderr, "Unable to play WAV file: %s\n", Mix_GetError());
        }

        for (int j = 0; j < bombGroup.size(); j++) {
            bombGroup.at(j)->blit(screen);
        }
        for (int j = 0; j < explosionGroup.size(); j++) {
            SDL_Rect tmp = explosionGroup.at(j)->getShowPart();
            explosionGroup.at(j)->blit(screen, &tmp);
        }
        for (int i = 0; i < blocks.size(); i++) {
            blocks.at(i)->blit(screen);
        }

        for (int i = 0; i < upgradeGroup.size(); i++) {
            upgradeGroup.at(i)->blit(screen);
        }
        for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
            it->second->blit(screen);
        }

        for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
            sprintf(textbuf[it->first], "Player %d = %d", it->first+1, it->second->getLife());
            text_image[it->first] =  TTF_RenderText_Solid(text_font, textbuf[it->first], font_color);
            int offsetX = it->first<2 ? 0:1;
            int offsetY = it->first%2==0 ? 0:1;
            textDest[it->first].x = 50+750*offsetX;
            textDest[it->first].y = 10+50*offsetY;
            textDest[it->first].w = text_image[it->first]->w;
            textDest[it->first].h = text_image[it->first]->h;
            SDL_BlitSurface(text_image[it->first], NULL, screen, &(textDest[it->first]));
            SDL_FreeSurface(text_image[it->first]);
        }


        // game over
        bool iWin = false;
        if (heroGroup[myId]->getLife()!=0) {
            iWin = true;
            for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
                if (it->first != myId) {
                    if (it->second->getLife() != 0) {
                        iWin = false;
                        break;
                    }
                }
            }
        }
        bool iLose=false;
        if (heroGroup[myId]->getLife()==0) {
            iLose = true;
            int counter = 0;
            for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
                if (it->first != myId) {
                    if (it->second->getLife() != 0) {
                        counter++;
                        if (counter>1) {
                            iLose = false;
                            break;
                        }
                    }
                }
            }
        }

        if (iWin) {
            animationCounter++;
            if (animationCounter>20) {
                winScreen->blit(screen);
                SDL_Flip(screen);
                SDL_Delay(3000);
                gameLoopCleanUp();
                return MENU;
            }
        }
        else if (iLose) {
            animationCounter++;
            if (animationCounter>20) {
                loseScreen->blit(screen);
                SDL_Flip(screen);
                SDL_Delay(3000);
                gameLoopCleanUp();
                return MENU;
            }
        }

        /* since its double buffered, make
         the changes show up*/
        SDL_Flip(screen);

        SDL_Delay(50);
    }
    return 0;
}