int main() { InitializeSystem(); SysTick_Config(HCLKFrequency()/100); InitializeLEDs(); SetLEDs(0x01); uint8_t *framebuffer1=(uint8_t *)0x20000000; uint8_t *framebuffer2=(uint8_t *)0x20010000; SetLEDs(0x03); memset(framebuffer1,0,320*200); memset(framebuffer2,0,320*200); SetLEDs(0x07); IntializeVGAScreenMode320x200(framebuffer1); #define NumberOfStars 1050 static struct Star { int x,y,dx,f; } stars[NumberOfStars]; for(int i=0;i<NumberOfStars;i++) { stars[i].x=(RandomInteger()%352-16)<<12; stars[i].y=RandomInteger()%200; int z=sqrti((NumberOfStars-1-i)*NumberOfStars)*1000/NumberOfStars; stars[i].dx=6000*1200/(z+200); stars[i].f=(6-(z*7)/1000)+(RandomInteger()%6)*7; } const RLEBitmap *sprites[7*6]={ &Star1_0,&Star2_0,&Star3_0,&Star4_0,&Star5_0,&Star6_0,&Star7_0, &Star1_1,&Star2_1,&Star3_1,&Star4_1,&Star5_1,&Star6_1,&Star7_1, &Star1_2,&Star2_2,&Star3_2,&Star4_2,&Star5_2,&Star6_2,&Star7_2, &Star1_3,&Star2_3,&Star3_3,&Star4_3,&Star5_3,&Star6_3,&Star7_3, &Star1_4,&Star2_4,&Star3_4,&Star4_4,&Star5_4,&Star6_4,&Star7_4, &Star1_5,&Star2_5,&Star3_5,&Star4_5,&Star5_5,&Star6_5,&Star7_5, }; Bitmap frame1,frame2; InitializeBitmap(&frame1,320,200,320,framebuffer1); InitializeBitmap(&frame2,320,200,320,framebuffer2); int frame=0; while(1) { WaitVBL(); Bitmap *currframe; if(frame&1) { currframe=&frame2; SetFrameBuffer(framebuffer1); } else { currframe=&frame1; SetFrameBuffer(framebuffer2); } ClearBitmap(currframe); for(int i=0;i<NumberOfStars;i++) { DrawRLEBitmap(currframe,sprites[stars[i].f], (stars[i].x>>12)-16,stars[i].y-16); stars[i].x-=stars[i].dx; if(stars[i].x<=-16<<12) { stars[i].x=(320+16)<<12; stars[i].y=RandomInteger()%200; stars[i].f=(stars[i].f%7)+(RandomInteger()%6)*7; } } frame++; } }
int main() { RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); InitializeLEDs(); InitializeUserInterface(); InitializeRandom(); InitializeRumble(); //Clear framebuffers memset(ADDR_FRAMEBUFFER1, 0x00, 320*200); memset(ADDR_FRAMEBUFFER2, 0x00, 320*200); //Create drawing surfaces InitializeBitmap(&frame1,320,200,320,ADDR_FRAMEBUFFER1); InitializeBitmap(&frame2,320,200,320,ADDR_FRAMEBUFFER2); //Switch on VGA IntializeVGAScreenMode320x200(ADDR_FRAMEBUFFER1); if(!TheGame) { EnableDebugOutput(DEBUG_USART); fprintf(stderr, "PANIC: Game structure pointer is NULL\r\n"); return -1; } if(TheGame->currentState) TheGame->currentState->Init(NULL); if(!TheGame->currentState->Update || !TheGame->currentState->Draw) { fprintf(stderr, "PANIC: Update and/or Draw function pointer is NULL\r\n"); return -1; } Gamestate *currentState = TheGame->currentState; while(1) { oldTime = currentTime; currentTime = SysTickCounter; if (currentState != TheGame->currentState) { Gamestate *newState = TheGame->currentState; if (!newState->initialized) { newState->Init(currentState); newState->initialized = true; } if (newState->OnEnter) newState->OnEnter(currentState); currentState = newState; } //Swap Buffers if(frame&1) { drawingSurface=&frame2; SetFrameBuffer(ADDR_FRAMEBUFFER1); } else { drawingSurface=&frame1; SetFrameBuffer(ADDR_FRAMEBUFFER2); } //Update and draw currentState->Update(currentTime - oldTime); currentState->Draw(drawingSurface); frame++; if (currentState != TheGame->currentState && TheGame->currentState->OnLeave) { currentState->OnLeave(currentState); } //VSync WaitVBL(); } }