Example #1
0
void loop(s4 &game_state)
{
    ICameraSceneNode* camera = irrlicht->smgr->addCameraSceneNode();
    matrix4 ortho;
    ortho.buildProjectionMatrixOrthoLH(
        irrlicht->driver->getScreenSize().Width/ortho_scale,
        irrlicht->driver->getScreenSize().Height/ortho_scale,-1.0,1000.0);
    camera->setProjectionMatrix(ortho);
    camera->setPosition({0,0,-100});
    camera->setTarget({0,0,0});

    irrlicht->hud->setActiveCamera(camera);

    IBillboardSceneNode* qnode = irrlicht->smgr->addBillboardSceneNode();
    qnode->setMaterialFlag(EMF_WIREFRAME,true);

    // --------------------------------------
 
    p("---- Game loop start ----");

    d32         dt = 0.0f;
    const d32   maxDelta = 1.0f/60.0f * 5.0f; 
    const d32   tick_ms = 0.01666f; // TODO: change this back to 30ms?
    d32         render_dt = 0.0f;
    const d32   render_ms = RENDER_TIME_STEP;
    uint32      time_physics_prev = btclock->getTimeMicroseconds();

    while(irrlicht->device->run() && game_state == GAME_STATE_PLAY)
    {
        const uint32 time_physics_curr = btclock->getTimeMicroseconds();
        const d32 frameTime = ((d32)(time_physics_curr - time_physics_prev)) / 1000000.0; // todo: is this truncated correctly?
        time_physics_prev = time_physics_curr;
        d32 capped_dt = frameTime;
        if (capped_dt > maxDelta) { capped_dt = maxDelta; }

        render_dt += capped_dt;
        if ( render_dt >= render_ms )
        {
            render_dt = 0.0f;

            update_player();
            update_enemies();
            update_missiles(ply->missiles,ply->num_missile);

            irrlicht->hud->getRootSceneNode()->setPosition(camera->getPosition() + vector3df(0,0,100));

            irrlicht->driver->beginScene(true, true, SColor(255,59,120,140));
            irrlicht->smgr->drawAll();  
            irrlicht->driver->clearZBuffer(); 
            irrlicht->hud->drawAll();

            irrlicht->driver->endScene();
        }

        dt += capped_dt;
        while( dt >= tick_ms ) 
        {
            dt -= tick_ms;
            receiver->input();

            clear_quads(quadtree);
            step_player();
            step_enemies();
            step_missiles(ply->missiles, ply->num_missile); 

            QuadTree::Quad* fq = get_quad_from_pos(quadtree,ply->curr_pos);
            qnode->setSize(dimension2d<f32>(1,1));
            qnode->setPosition(fq->pos.irr());
            qnode->setSize(dimension2d<f32>(fq->width,fq->width));
 
            if (receiver->debug_key.state)
            {
                for (s4 i = 0; i < fleet->num_squad; i++)
                {
                    if (fleet->squads[i].mode != scatter)
                        fleet->squads[i].mode = scatter;
                    else
                        fleet->squads[i].mode = to_positions;
                }
            }
            if (receiver->restart.state) { game_state = GAME_STATE_RESET; return; }
            if (receiver->QUIT) { game_state = GAME_STATE_QUIT; return; }

            empty_transient_soft(memory);
        }

        alpha = dt / tick_ms;

        core::stringw str = L"Coquelicot // score: ";
        str += score;
        str += L" // FPS: ";
        str += (s32)irrlicht->driver->getFPS();
        irrlicht->device->setWindowCaption(str.c_str());
    } 
}
Example #2
0
int main(void)
{
#if !SDL
	//wdt_enable(WDTO_1S);

	// I/O ports

	DDRB = 0x17;
	PORTB = 0x00 | (1<<5);
	//input logic disable on adc3
	DIDR0 = (1<<ADC3D);

	//timer0
	
	//set clock divider to 1024
	TCCR0B |= (1<<CS02) | (1<<CS00);
	//compare value 49 -> 20Hz
	//OCR0B = 49;
	OCR0B = 3;
	//enable compare match B interrupt
	TIMSK |= (1<<OCIE0B);

	//timer1

	//      CK/64
	//TCCR1 = (1<<CS12) | (1<<CS11) | (1<<CS10);
	//overflow interrupt
	//TIMSK |= (1<<TOIE1);

	//external interrupts
	
	//enable pin change interrupt
	//GIMSK = (1<<PCIE);
	//enable only on PB3
	//PCMSK = (1<<3);
	
	//adc
	
	//       enable      clk/16
	//ADCSRA = (1<<ADEN) | (1<<ADPS2);
	//       enable      clk/2
	ADCSRA = (1<<ADEN) | (1<<ADPS0);

	/*//if it's a watchdog reset
	if(mcusr_mirror & (1<<WDRF)){
		if(getkey() == DIR_NONE){
			set_sleep_mode(SLEEP_MODE_PWR_DOWN);
			sleep_mode();
		}
	}*/

	//wdt_enable(WDTO_4S);

#else

	SDL_Init(SDL_INIT_VIDEO);
	if (!(screen = SDL_SetVideoMode(80, 48, 32, SDL_SWSURFACE)))
		return EXIT_FAILURE;

	SDL_WM_SetCaption("elladunkku", NULL);

#endif

	/*g_highscore = eeprom_read_byte(0);
	if(g_highscore == 0xff) g_highscore = 0;*/
	/*g_highscore = eeprom_read_word(0);
	if(g_highscore == 0xffff) g_highscore = 0;*/

start:

	lcd_init();

	//lcd_locate(10,2);
	//lcd_locate8((2<<4)+1);
	//lcd_printstrP(PSTR("C55 PRESENTS"));
	//_delay_ms(1000);

	sei();

	for(;;){

		next_level(true);
		draw_game();

		//main loop

		g_counter0 = 0;
		//TCNT0 = 0;
		for(;;){
#if SDL
			++g_counter0;
			SDL_WaitEvent(&g_event);
			if (g_event.type == SDL_QUIT)
				goto quit;
#endif

			// TODO: If nothing happens for a long time, go to sleep

			int8_t key = getkey();
			if(key == DIR_NONE)
				continue;

			uint8_t num_enemy_moves = move_player(key);
			switch(num_enemy_moves){
			case 2:
				step_enemies();
				draw_game();
			case 1:
				step_enemies();
			}
			draw_game();

			if(g_hp <= 0){
				_delay_ms(5000);
				while(getkey() == DIR_NONE);
				next_level(true); // Reset game
				draw_game();
			}

			/*int8_t lastdir_inv = -g_next_dir;
			while(g_counter0 < 6){
			//while(TCNT0 < 6*3){
				int8_t key = getkey();
				if(key == DIR_NONE) continue;
				if(key == lastdir_inv) continue;
				g_next_dir = key;
				g_random += g_counter0;
			}
			TCNT0 = 0;
			g_counter0 = 0;
			uint8_t ret = snake_move_and_draw();
			if(ret == 1){
				snake_draw(0xaa);
				draw_block(g_snake[g_snake_end], 0xff);
				//lcd_locate(83,5);
				lcd_locate8((0<<4)+4);
				if(g_points > g_highscore){
					g_highscore = g_points;
					//eeprom_write_byte(0, g_highscore);
					eeprom_write_word(0, g_highscore);
				}
				_delay_ms(200);
				break;
			}*/
		}

#if !SDL
		while(getkey() != DIR_NONE);
		//TCNT0 = 0;
		g_counter0 = 0;
		while(getkey() == DIR_NONE){
			if(g_counter0 >= 200){
				cli();

				/*lcd_cls();
				lcd_locate8((1<<4)+0);
				lcd_printstrP(PSTR("GREETINGS  ELLA  TEJEEZ  3210"));
				_delay_ms(2000);*/
				/*lcd_locate8((1<<4)+1);
				lcd_printstrP(PSTR("GREETINGS ELLA TEJEEZ"));
				lcd_locate8((2<<4)+1);
				lcd_printstrP(PSTR("ELLA"));
				lcd_locate8((3<<4)+1);
				lcd_printstrP(PSTR("TEJEEZ"));*/
				/*lcd_cls();
				lcd_locate8((1<<4)+3);
				lcd_put5digit(2048);
				lcd_locate8((2<<4)+3);
				lcd_put5digit(128);
				lcd_locate8((3<<4)+3);
				lcd_put5digit(55);
				_delay_ms(2000);*/
				
				lcd_cls();
				lcd_powerdown();

				ADCSRA = 0 | (1<<ADPS0);
				DIDR0 = 0;
				CLKPR = 0x80;
				CLKPR = (1<<CLKPS3); //256
				while(PINB & (1<<3));
				//while(getkey() == DIR_NONE);
				CLKPR = 0x80;
				CLKPR = 0;
				DIDR0 = (1<<ADC3D);
				ADCSRA = (1<<ADEN) | (1<<ADPS0);

				goto start;
			}
		}
#endif
	}

#if SDL
quit:
	SDL_Quit();
#endif
	return 0;
}